/*******************************************************************************1* Copyright (c) 2013, 2016 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#include "CudaCommon.hpp"23#include "java/com_ibm_cuda_CudaLinker.h"2425#ifdef OMR_OPT_CUDA2627/**28* Add a module fragment to the set of linker inputs.29*30* Class: com.ibm.cuda.CudaLinker31* Method: add32* Signature: (IJI[BLjava/lang/String;J)V33*34* @param[in] env the JNI interface pointer35* @param[in] (unused) the class pointer36* @param[in] deviceId the device identifier37* @param[in] linker the linker state pointer38* @param[in] type the type of input in image39* @param[in] image the input content40* @param[in] name an optional name for use in log messages41* @param[in] options an optional options pointer42*/43void JNICALL44Java_com_ibm_cuda_CudaLinker_add45(JNIEnv * env, jclass, jint deviceId, jlong linker, jint type, jbyteArray image, jstring name, jlong options)46{47J9VMThread * thread = (J9VMThread *)env;4849Trc_cuda_linkerAdd_entry(thread, deviceId, (J9CudaLinker)linker, type, image, name, (J9CudaJitOptions *)options);5051int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;52jbyte * imageData = env->GetByteArrayElements(image, NULL);53const char * nameData = NULL;5455if (NULL != name) {56nameData = env->GetStringUTFChars(name, NULL);57}5859if ((NULL == imageData) || ((NULL != name) && (NULL == nameData))) {60Trc_cuda_linkerAdd_fail(thread);61} else {62PORT_ACCESS_FROM_ENV(env);6364error = j9cuda_linkerAddData(65(uint32_t)deviceId,66(J9CudaLinker)linker,67(J9CudaJitInputType)type,68imageData,69(uintptr_t)env->GetArrayLength(image),70nameData,71(J9CudaJitOptions *)options);72}7374if (NULL != nameData) {75env->ReleaseStringUTFChars(name, nameData);76}7778if (NULL != imageData) {79env->ReleaseByteArrayElements(image, imageData, JNI_ABORT);80}8182if (0 != error) {83throwCudaException(env, error);84}8586Trc_cuda_linkerAdd_exit(thread);87}8889/**90* Complete linking the module.91*92* Class: com.ibm.cuda.CudaLinker93* Method: complete94* Signature: (IJ)[B95*96* @param[in] env the JNI interface pointer97* @param[in] (unused) the class pointer98* @param[in] deviceId the device identifier99* @param[in] linker the linker state pointer100* @return the completed module ready for loading101*/102jbyteArray JNICALL103Java_com_ibm_cuda_CudaLinker_complete104(JNIEnv * env, jclass, jint deviceId, jlong linker)105{106J9VMThread * thread = (J9VMThread *)env;107108Trc_cuda_linkerComplete_entry(thread, deviceId, (J9CudaLinker)linker);109110PORT_ACCESS_FROM_ENV(env);111112void * data = NULL;113uintptr_t size = 0;114int32_t error = j9cuda_linkerComplete((uint32_t)deviceId, (J9CudaLinker)linker, &data, &size);115jbyteArray result = NULL;116117if (0 != error) {118throwCudaException(env, error);119} else {120result = env->NewByteArray((jsize)size);121122if (env->ExceptionCheck()) {123Trc_cuda_linkerComplete_exception(thread);124result = NULL;125} else if (NULL == result) {126Trc_cuda_linkerComplete_fail(thread);127} else {128env->SetByteArrayRegion(result, 0, (jsize)size, (jbyte *)data);129}130}131132Trc_cuda_linkerComplete_exit(thread, result);133134return result;135}136137#endif /* OMR_OPT_CUDA */138139/**140* Create a new linker session.141*142* Class: com.ibm.cuda.CudaLinker143* Method: create144* Signature: (IJ)J145*146* @param[in] env the JNI interface pointer147* @param[in] (unused) the class pointer148* @param[in] deviceId the device identifier149* @param[in] options an optional options pointer150* @return the linker state pointer151*/152jlong JNICALL153Java_com_ibm_cuda_CudaLinker_create154(JNIEnv * env, jclass, jint deviceId, jlong options)155{156J9VMThread * thread = (J9VMThread *)env;157158Trc_cuda_linkerCreate_entry(thread, deviceId, (J9CudaJitOptions *)options);159160J9CudaLinker linker = NULL;161int32_t error = J9CUDA_ERROR_NO_DEVICE;162#ifdef OMR_OPT_CUDA163PORT_ACCESS_FROM_ENV(env);164error = j9cuda_linkerCreate((uint32_t)deviceId, (J9CudaJitOptions *)options, &linker);165#endif /* OMR_OPT_CUDA */166167if (0 != error) {168throwCudaException(env, error);169}170171Trc_cuda_linkerCreate_exit(thread, linker);172173return (jlong)linker;174}175176#ifdef OMR_OPT_CUDA177178/**179* Destroy a linker session.180*181* Class: com.ibm.cuda.CudaLinker182* Method: destroy183* Signature: (IJ)V184*185* @param[in] env the JNI interface pointer186* @param[in] (unused) the class pointer187* @param[in] deviceId the device identifier188* @param[in] linker the linker state pointer189*/190void JNICALL191Java_com_ibm_cuda_CudaLinker_destroy192(JNIEnv * env, jclass, jint deviceId, jlong linker)193{194J9VMThread * thread = (J9VMThread *)env;195196Trc_cuda_linkerDestroy_entry(thread, deviceId, (J9CudaLinker)linker);197198PORT_ACCESS_FROM_ENV(env);199200int32_t error = j9cuda_linkerDestroy((uint32_t)deviceId, (J9CudaLinker)linker);201202if (0 != error) {203throwCudaException(env, error);204}205206Trc_cuda_linkerDestroy_exit(thread);207}208209#endif /* OMR_OPT_CUDA */210211212