mirror of
https://github.com/vosen/ZLUDA.git
synced 2025-08-02 14:57:43 +03:00
Fix formatting
This commit is contained in:
@ -1,426 +1,426 @@
|
|||||||
use crate::r#impl::{context, device};
|
use crate::r#impl::{context, device};
|
||||||
use comgr::Comgr;
|
use comgr::Comgr;
|
||||||
use cuda_types::cuda::*;
|
use cuda_types::cuda::*;
|
||||||
use hip_runtime_sys::*;
|
use hip_runtime_sys::*;
|
||||||
use zluda_common::{FromCuda, LiveCheck};
|
use std::{
|
||||||
use std::{
|
ffi::{c_void, CStr, CString},
|
||||||
ffi::{c_void, CStr, CString},
|
mem, ptr, slice,
|
||||||
mem, ptr, slice,
|
sync::OnceLock,
|
||||||
sync::OnceLock,
|
usize,
|
||||||
usize,
|
};
|
||||||
};
|
use zluda_common::{FromCuda, LiveCheck};
|
||||||
|
|
||||||
#[cfg_attr(windows, path = "os_win.rs")]
|
#[cfg_attr(windows, path = "os_win.rs")]
|
||||||
#[cfg_attr(not(windows), path = "os_unix.rs")]
|
#[cfg_attr(not(windows), path = "os_unix.rs")]
|
||||||
mod os;
|
mod os;
|
||||||
|
|
||||||
pub(crate) struct GlobalState {
|
pub(crate) struct GlobalState {
|
||||||
pub devices: Vec<Device>,
|
pub devices: Vec<Device>,
|
||||||
pub comgr: Comgr,
|
pub comgr: Comgr,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Device {
|
pub(crate) struct Device {
|
||||||
pub(crate) _comgr_isa: CString,
|
pub(crate) _comgr_isa: CString,
|
||||||
primary_context: LiveCheck<context::Context>,
|
primary_context: LiveCheck<context::Context>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
pub(crate) fn primary_context<'a>(&'a self) -> (&'a context::Context, CUcontext) {
|
pub(crate) fn primary_context<'a>(&'a self) -> (&'a context::Context, CUcontext) {
|
||||||
unsafe {
|
unsafe {
|
||||||
(
|
(
|
||||||
self.primary_context.data.assume_init_ref(),
|
self.primary_context.data.assume_init_ref(),
|
||||||
self.primary_context.as_handle(),
|
self.primary_context.as_handle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn device(dev: i32) -> Result<&'static Device, CUerror> {
|
pub(crate) fn device(dev: i32) -> Result<&'static Device, CUerror> {
|
||||||
global_state()?
|
global_state()?
|
||||||
.devices
|
.devices
|
||||||
.get(dev as usize)
|
.get(dev as usize)
|
||||||
.ok_or(CUerror::INVALID_DEVICE)
|
.ok_or(CUerror::INVALID_DEVICE)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn global_state() -> Result<&'static GlobalState, CUerror> {
|
pub(crate) fn global_state() -> Result<&'static GlobalState, CUerror> {
|
||||||
static GLOBAL_STATE: OnceLock<Result<GlobalState, CUerror>> = OnceLock::new();
|
static GLOBAL_STATE: OnceLock<Result<GlobalState, CUerror>> = OnceLock::new();
|
||||||
fn cast_slice<'a>(bytes: &'a [i8]) -> &'a [u8] {
|
fn cast_slice<'a>(bytes: &'a [i8]) -> &'a [u8] {
|
||||||
unsafe { slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len()) }
|
unsafe { slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len()) }
|
||||||
}
|
}
|
||||||
GLOBAL_STATE
|
GLOBAL_STATE
|
||||||
.get_or_init(|| {
|
.get_or_init(|| {
|
||||||
let mut device_count = 0;
|
let mut device_count = 0;
|
||||||
unsafe { hipGetDeviceCount(&mut device_count) }?;
|
unsafe { hipGetDeviceCount(&mut device_count) }?;
|
||||||
let comgr = Comgr::new().map_err(|_| CUerror::UNKNOWN)?;
|
let comgr = Comgr::new().map_err(|_| CUerror::UNKNOWN)?;
|
||||||
Ok(GlobalState {
|
Ok(GlobalState {
|
||||||
comgr,
|
comgr,
|
||||||
devices: (0..device_count)
|
devices: (0..device_count)
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
let mut props = unsafe { mem::zeroed() };
|
let mut props = unsafe { mem::zeroed() };
|
||||||
unsafe { hipGetDevicePropertiesR0600(&mut props, i) }?;
|
unsafe { hipGetDevicePropertiesR0600(&mut props, i) }?;
|
||||||
Ok::<_, CUerror>(Device {
|
Ok::<_, CUerror>(Device {
|
||||||
_comgr_isa: CStr::from_bytes_until_nul(cast_slice(
|
_comgr_isa: CStr::from_bytes_until_nul(cast_slice(
|
||||||
&props.gcnArchName[..],
|
&props.gcnArchName[..],
|
||||||
))
|
))
|
||||||
.map_err(|_| CUerror::UNKNOWN)?
|
.map_err(|_| CUerror::UNKNOWN)?
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
primary_context: LiveCheck::new(context::Context::new(i)),
|
primary_context: LiveCheck::new(context::Context::new(i)),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()?,
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_err(|e| *e)
|
.map_err(|e| *e)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn init(flags: ::core::ffi::c_uint) -> CUresult {
|
pub(crate) fn init(flags: ::core::ffi::c_uint) -> CUresult {
|
||||||
unsafe { hipInit(flags) }?;
|
unsafe { hipInit(flags) }?;
|
||||||
global_state()?;
|
global_state()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UnknownBuffer<const S: usize> {
|
struct UnknownBuffer<const S: usize> {
|
||||||
buffer: std::cell::UnsafeCell<[u32; S]>,
|
buffer: std::cell::UnsafeCell<[u32; S]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const S: usize> UnknownBuffer<S> {
|
impl<const S: usize> UnknownBuffer<S> {
|
||||||
const fn new() -> Self {
|
const fn new() -> Self {
|
||||||
UnknownBuffer {
|
UnknownBuffer {
|
||||||
buffer: std::cell::UnsafeCell::new([0; S]),
|
buffer: std::cell::UnsafeCell::new([0; S]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const fn len(&self) -> usize {
|
const fn len(&self) -> usize {
|
||||||
S
|
S
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<const S: usize> Sync for UnknownBuffer<S> {}
|
unsafe impl<const S: usize> Sync for UnknownBuffer<S> {}
|
||||||
|
|
||||||
static UNKNOWN_BUFFER1: UnknownBuffer<1024> = UnknownBuffer::new();
|
static UNKNOWN_BUFFER1: UnknownBuffer<1024> = UnknownBuffer::new();
|
||||||
static UNKNOWN_BUFFER2: UnknownBuffer<14> = UnknownBuffer::new();
|
static UNKNOWN_BUFFER2: UnknownBuffer<14> = UnknownBuffer::new();
|
||||||
|
|
||||||
struct DarkApi {}
|
struct DarkApi {}
|
||||||
|
|
||||||
impl ::dark_api::cuda::CudaDarkApi for DarkApi {
|
impl ::dark_api::cuda::CudaDarkApi for DarkApi {
|
||||||
unsafe extern "system" fn get_module_from_cubin(
|
unsafe extern "system" fn get_module_from_cubin(
|
||||||
_module: *mut cuda_types::cuda::CUmodule,
|
_module: *mut cuda_types::cuda::CUmodule,
|
||||||
_fatbinc_wrapper: *const cuda_types::dark_api::FatbincWrapper,
|
_fatbinc_wrapper: *const cuda_types::dark_api::FatbincWrapper,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn cudart_interface_fn2(
|
unsafe extern "system" fn cudart_interface_fn2(
|
||||||
pctx: *mut cuda_types::cuda::CUcontext,
|
pctx: *mut cuda_types::cuda::CUcontext,
|
||||||
hip_dev: hipDevice_t,
|
hip_dev: hipDevice_t,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
let pctx = match pctx.as_mut() {
|
let pctx = match pctx.as_mut() {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => return CUresult::ERROR_INVALID_VALUE,
|
None => return CUresult::ERROR_INVALID_VALUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
device::primary_context_retain(pctx, hip_dev)
|
device::primary_context_retain(pctx, hip_dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn get_module_from_cubin_ext1(
|
unsafe extern "system" fn get_module_from_cubin_ext1(
|
||||||
_result: *mut cuda_types::cuda::CUmodule,
|
_result: *mut cuda_types::cuda::CUmodule,
|
||||||
_fatbinc_wrapper: *const cuda_types::dark_api::FatbincWrapper,
|
_fatbinc_wrapper: *const cuda_types::dark_api::FatbincWrapper,
|
||||||
_arg3: *mut std::ffi::c_void,
|
_arg3: *mut std::ffi::c_void,
|
||||||
_arg4: *mut std::ffi::c_void,
|
_arg4: *mut std::ffi::c_void,
|
||||||
_arg5: u32,
|
_arg5: u32,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn cudart_interface_fn7(_arg1: usize) -> cuda_types::cuda::CUresult {
|
unsafe extern "system" fn cudart_interface_fn7(_arg1: usize) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn get_module_from_cubin_ext2(
|
unsafe extern "system" fn get_module_from_cubin_ext2(
|
||||||
_fatbin_header: *const cuda_types::dark_api::FatbinHeader,
|
_fatbin_header: *const cuda_types::dark_api::FatbinHeader,
|
||||||
_result: *mut cuda_types::cuda::CUmodule,
|
_result: *mut cuda_types::cuda::CUmodule,
|
||||||
_arg3: *mut std::ffi::c_void,
|
_arg3: *mut std::ffi::c_void,
|
||||||
_arg4: *mut std::ffi::c_void,
|
_arg4: *mut std::ffi::c_void,
|
||||||
_arg5: u32,
|
_arg5: u32,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn get_unknown_buffer1(
|
unsafe extern "system" fn get_unknown_buffer1(
|
||||||
ptr: *mut *mut std::ffi::c_void,
|
ptr: *mut *mut std::ffi::c_void,
|
||||||
size: *mut usize,
|
size: *mut usize,
|
||||||
) -> () {
|
) -> () {
|
||||||
*ptr = UNKNOWN_BUFFER1.buffer.get() as *mut std::ffi::c_void;
|
*ptr = UNKNOWN_BUFFER1.buffer.get() as *mut std::ffi::c_void;
|
||||||
*size = UNKNOWN_BUFFER1.len();
|
*size = UNKNOWN_BUFFER1.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn get_unknown_buffer2(
|
unsafe extern "system" fn get_unknown_buffer2(
|
||||||
ptr: *mut *mut std::ffi::c_void,
|
ptr: *mut *mut std::ffi::c_void,
|
||||||
size: *mut usize,
|
size: *mut usize,
|
||||||
) -> () {
|
) -> () {
|
||||||
*ptr = UNKNOWN_BUFFER2.buffer.get() as *mut std::ffi::c_void;
|
*ptr = UNKNOWN_BUFFER2.buffer.get() as *mut std::ffi::c_void;
|
||||||
*size = UNKNOWN_BUFFER2.len();
|
*size = UNKNOWN_BUFFER2.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn context_local_storage_put(
|
unsafe extern "system" fn context_local_storage_put(
|
||||||
cu_ctx: CUcontext,
|
cu_ctx: CUcontext,
|
||||||
key: *mut c_void,
|
key: *mut c_void,
|
||||||
value: *mut c_void,
|
value: *mut c_void,
|
||||||
dtor_cb: Option<extern "system" fn(CUcontext, *mut c_void, *mut c_void)>,
|
dtor_cb: Option<extern "system" fn(CUcontext, *mut c_void, *mut c_void)>,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
let _ctx = if cu_ctx.0 != ptr::null_mut() {
|
let _ctx = if cu_ctx.0 != ptr::null_mut() {
|
||||||
cu_ctx
|
cu_ctx
|
||||||
} else {
|
} else {
|
||||||
let mut current_ctx: CUcontext = CUcontext(ptr::null_mut());
|
let mut current_ctx: CUcontext = CUcontext(ptr::null_mut());
|
||||||
context::get_current(&mut current_ctx)?;
|
context::get_current(&mut current_ctx)?;
|
||||||
current_ctx
|
current_ctx
|
||||||
};
|
};
|
||||||
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&_ctx)?;
|
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&_ctx)?;
|
||||||
ctx_obj.with_state_mut(|state: &mut context::ContextState| {
|
ctx_obj.with_state_mut(|state: &mut context::ContextState| {
|
||||||
state.storage.insert(
|
state.storage.insert(
|
||||||
key as usize,
|
key as usize,
|
||||||
context::StorageData {
|
context::StorageData {
|
||||||
value: value as usize,
|
value: value as usize,
|
||||||
reset_cb: dtor_cb,
|
reset_cb: dtor_cb,
|
||||||
handle: _ctx,
|
handle: _ctx,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn context_local_storage_delete(
|
unsafe extern "system" fn context_local_storage_delete(
|
||||||
cu_ctx: CUcontext,
|
cu_ctx: CUcontext,
|
||||||
key: *mut c_void,
|
key: *mut c_void,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&cu_ctx)?;
|
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&cu_ctx)?;
|
||||||
ctx_obj.with_state_mut(|state: &mut context::ContextState| {
|
ctx_obj.with_state_mut(|state: &mut context::ContextState| {
|
||||||
state.storage.remove(&(key as usize));
|
state.storage.remove(&(key as usize));
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn context_local_storage_get(
|
unsafe extern "system" fn context_local_storage_get(
|
||||||
value: *mut *mut c_void,
|
value: *mut *mut c_void,
|
||||||
cu_ctx: CUcontext,
|
cu_ctx: CUcontext,
|
||||||
key: *mut c_void,
|
key: *mut c_void,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
let mut _ctx: CUcontext;
|
let mut _ctx: CUcontext;
|
||||||
if cu_ctx.0 == ptr::null_mut() {
|
if cu_ctx.0 == ptr::null_mut() {
|
||||||
_ctx = context::get_current_context()?;
|
_ctx = context::get_current_context()?;
|
||||||
} else {
|
} else {
|
||||||
_ctx = cu_ctx
|
_ctx = cu_ctx
|
||||||
};
|
};
|
||||||
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&_ctx)?;
|
let ctx_obj: &context::Context = FromCuda::<_, CUerror>::from_cuda(&_ctx)?;
|
||||||
ctx_obj.with_state(|state: &context::ContextState| {
|
ctx_obj.with_state(|state: &context::ContextState| {
|
||||||
match state.storage.get(&(key as usize)) {
|
match state.storage.get(&(key as usize)) {
|
||||||
Some(data) => *value = data.value as *mut c_void,
|
Some(data) => *value = data.value as *mut c_void,
|
||||||
None => return CUresult::ERROR_INVALID_HANDLE,
|
None => return CUresult::ERROR_INVALID_HANDLE,
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn ctx_create_v2_bypass(
|
unsafe extern "system" fn ctx_create_v2_bypass(
|
||||||
_pctx: *mut cuda_types::cuda::CUcontext,
|
_pctx: *mut cuda_types::cuda::CUcontext,
|
||||||
_flags: ::std::os::raw::c_uint,
|
_flags: ::std::os::raw::c_uint,
|
||||||
_dev: cuda_types::cuda::CUdevice,
|
_dev: cuda_types::cuda::CUdevice,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn heap_alloc(
|
unsafe extern "system" fn heap_alloc(
|
||||||
_heap_alloc_record_ptr: *mut *const std::ffi::c_void,
|
_heap_alloc_record_ptr: *mut *const std::ffi::c_void,
|
||||||
_arg2: usize,
|
_arg2: usize,
|
||||||
_arg3: usize,
|
_arg3: usize,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn heap_free(
|
unsafe extern "system" fn heap_free(
|
||||||
_heap_alloc_record_ptr: *const std::ffi::c_void,
|
_heap_alloc_record_ptr: *const std::ffi::c_void,
|
||||||
_arg2: *mut usize,
|
_arg2: *mut usize,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn device_get_attribute_ext(
|
unsafe extern "system" fn device_get_attribute_ext(
|
||||||
_dev: cuda_types::cuda::CUdevice,
|
_dev: cuda_types::cuda::CUdevice,
|
||||||
_attribute: std::ffi::c_uint,
|
_attribute: std::ffi::c_uint,
|
||||||
_unknown: std::ffi::c_int,
|
_unknown: std::ffi::c_int,
|
||||||
_result: *mut [usize; 2],
|
_result: *mut [usize; 2],
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn device_get_something(
|
unsafe extern "system" fn device_get_something(
|
||||||
_result: *mut std::ffi::c_uchar,
|
_result: *mut std::ffi::c_uchar,
|
||||||
_dev: cuda_types::cuda::CUdevice,
|
_dev: cuda_types::cuda::CUdevice,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn integrity_check(
|
unsafe extern "system" fn integrity_check(
|
||||||
version: u32,
|
version: u32,
|
||||||
unix_seconds: u64,
|
unix_seconds: u64,
|
||||||
result: *mut [u64; 2],
|
result: *mut [u64; 2],
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
let current_process = std::process::id();
|
let current_process = std::process::id();
|
||||||
let current_thread = os::current_thread();
|
let current_thread = os::current_thread();
|
||||||
|
|
||||||
let integrity_check_table = EXPORT_TABLE.INTEGRITY_CHECK.as_ptr().cast();
|
let integrity_check_table = EXPORT_TABLE.INTEGRITY_CHECK.as_ptr().cast();
|
||||||
let cudart_table = EXPORT_TABLE.CUDART_INTERFACE.as_ptr().cast();
|
let cudart_table = EXPORT_TABLE.CUDART_INTERFACE.as_ptr().cast();
|
||||||
let fn_address = EXPORT_TABLE.INTEGRITY_CHECK[1];
|
let fn_address = EXPORT_TABLE.INTEGRITY_CHECK[1];
|
||||||
|
|
||||||
let devices = get_device_hash_info()?;
|
let devices = get_device_hash_info()?;
|
||||||
let device_count = devices.len() as u32;
|
let device_count = devices.len() as u32;
|
||||||
let get_device = |dev| devices[dev as usize];
|
let get_device = |dev| devices[dev as usize];
|
||||||
|
|
||||||
let hash = ::dark_api::integrity_check(
|
let hash = ::dark_api::integrity_check(
|
||||||
version,
|
version,
|
||||||
unix_seconds,
|
unix_seconds,
|
||||||
cuda_types::cuda::CUDA_VERSION,
|
cuda_types::cuda::CUDA_VERSION,
|
||||||
current_process,
|
current_process,
|
||||||
current_thread,
|
current_thread,
|
||||||
integrity_check_table,
|
integrity_check_table,
|
||||||
cudart_table,
|
cudart_table,
|
||||||
fn_address,
|
fn_address,
|
||||||
device_count,
|
device_count,
|
||||||
get_device,
|
get_device,
|
||||||
);
|
);
|
||||||
*result = hash;
|
*result = hash;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn context_check(
|
unsafe extern "system" fn context_check(
|
||||||
_ctx_in: cuda_types::cuda::CUcontext,
|
_ctx_in: cuda_types::cuda::CUcontext,
|
||||||
result1: *mut u32,
|
result1: *mut u32,
|
||||||
_result2: *mut *const std::ffi::c_void,
|
_result2: *mut *const std::ffi::c_void,
|
||||||
) -> cuda_types::cuda::CUresult {
|
) -> cuda_types::cuda::CUresult {
|
||||||
*result1 = 0;
|
*result1 = 0;
|
||||||
CUresult::SUCCESS
|
CUresult::SUCCESS
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn check_fn3() -> u32 {
|
unsafe extern "system" fn check_fn3() -> u32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_device_hash_info() -> Result<Vec<::dark_api::DeviceHashinfo>, CUerror> {
|
fn get_device_hash_info() -> Result<Vec<::dark_api::DeviceHashinfo>, CUerror> {
|
||||||
let mut device_count = 0;
|
let mut device_count = 0;
|
||||||
device::get_count(&mut device_count)?;
|
device::get_count(&mut device_count)?;
|
||||||
|
|
||||||
(0..device_count)
|
(0..device_count)
|
||||||
.map(|dev| {
|
.map(|dev| {
|
||||||
let mut guid = CUuuid_st { bytes: [0; 16] };
|
let mut guid = CUuuid_st { bytes: [0; 16] };
|
||||||
unsafe { crate::cuDeviceGetUuid(&mut guid, dev)? };
|
unsafe { crate::cuDeviceGetUuid(&mut guid, dev)? };
|
||||||
|
|
||||||
let mut pci_domain = 0;
|
let mut pci_domain = 0;
|
||||||
device::get_attribute(
|
device::get_attribute(
|
||||||
&mut pci_domain,
|
&mut pci_domain,
|
||||||
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID,
|
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID,
|
||||||
dev,
|
dev,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let mut pci_bus = 0;
|
let mut pci_bus = 0;
|
||||||
device::get_attribute(
|
device::get_attribute(
|
||||||
&mut pci_bus,
|
&mut pci_bus,
|
||||||
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_BUS_ID,
|
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_BUS_ID,
|
||||||
dev,
|
dev,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let mut pci_device = 0;
|
let mut pci_device = 0;
|
||||||
device::get_attribute(
|
device::get_attribute(
|
||||||
&mut pci_device,
|
&mut pci_device,
|
||||||
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID,
|
CUdevice_attribute::CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID,
|
||||||
dev,
|
dev,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(::dark_api::DeviceHashinfo {
|
Ok(::dark_api::DeviceHashinfo {
|
||||||
guid,
|
guid,
|
||||||
pci_domain,
|
pci_domain,
|
||||||
pci_bus,
|
pci_bus,
|
||||||
pci_device,
|
pci_device,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
static EXPORT_TABLE: ::dark_api::cuda::CudaDarkApiGlobalTable =
|
static EXPORT_TABLE: ::dark_api::cuda::CudaDarkApiGlobalTable =
|
||||||
::dark_api::cuda::CudaDarkApiGlobalTable::new::<DarkApi>();
|
::dark_api::cuda::CudaDarkApiGlobalTable::new::<DarkApi>();
|
||||||
|
|
||||||
pub(crate) fn get_export_table(
|
pub(crate) fn get_export_table(
|
||||||
pp_export_table: &mut *const ::core::ffi::c_void,
|
pp_export_table: &mut *const ::core::ffi::c_void,
|
||||||
p_export_table_id: &CUuuid,
|
p_export_table_id: &CUuuid,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
if let Some(table) = EXPORT_TABLE.get(p_export_table_id) {
|
if let Some(table) = EXPORT_TABLE.get(p_export_table_id) {
|
||||||
*pp_export_table = table.start();
|
*pp_export_table = table.start();
|
||||||
cuda_types::cuda::CUresult::SUCCESS
|
cuda_types::cuda::CUresult::SUCCESS
|
||||||
} else {
|
} else {
|
||||||
cuda_types::cuda::CUresult::ERROR_INVALID_VALUE
|
cuda_types::cuda::CUresult::ERROR_INVALID_VALUE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_version(version: &mut ::core::ffi::c_int) -> CUresult {
|
pub(crate) fn get_version(version: &mut ::core::ffi::c_int) -> CUresult {
|
||||||
*version = cuda_types::cuda::CUDA_VERSION as i32;
|
*version = cuda_types::cuda::CUDA_VERSION as i32;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) unsafe fn get_proc_address(
|
pub(crate) unsafe fn get_proc_address(
|
||||||
symbol: &CStr,
|
symbol: &CStr,
|
||||||
pfn: &mut *mut ::core::ffi::c_void,
|
pfn: &mut *mut ::core::ffi::c_void,
|
||||||
cuda_version: ::core::ffi::c_int,
|
cuda_version: ::core::ffi::c_int,
|
||||||
flags: cuda_types::cuda::cuuint64_t,
|
flags: cuda_types::cuda::cuuint64_t,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
get_proc_address_v2(symbol, pfn, cuda_version, flags, None)
|
get_proc_address_v2(symbol, pfn, cuda_version, flags, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) unsafe fn get_proc_address_v2(
|
pub(crate) unsafe fn get_proc_address_v2(
|
||||||
symbol: &CStr,
|
symbol: &CStr,
|
||||||
pfn: &mut *mut ::core::ffi::c_void,
|
pfn: &mut *mut ::core::ffi::c_void,
|
||||||
cuda_version: ::core::ffi::c_int,
|
cuda_version: ::core::ffi::c_int,
|
||||||
flags: cuda_types::cuda::cuuint64_t,
|
flags: cuda_types::cuda::cuuint64_t,
|
||||||
symbol_status: Option<&mut cuda_types::cuda::CUdriverProcAddressQueryResult>,
|
symbol_status: Option<&mut cuda_types::cuda::CUdriverProcAddressQueryResult>,
|
||||||
) -> CUresult {
|
) -> CUresult {
|
||||||
// This implementation is mostly the same as cuGetProcAddress_v2 in zluda_dump. We may want to factor out the duplication at some point.
|
// This implementation is mostly the same as cuGetProcAddress_v2 in zluda_dump. We may want to factor out the duplication at some point.
|
||||||
fn raw_match(name: &[u8], flag: u64, version: i32) -> *mut ::core::ffi::c_void {
|
fn raw_match(name: &[u8], flag: u64, version: i32) -> *mut ::core::ffi::c_void {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
include!("../../../zluda_bindgen/src/process_table.rs")
|
include!("../../../zluda_bindgen/src/process_table.rs")
|
||||||
}
|
}
|
||||||
let fn_ptr = raw_match(symbol.to_bytes(), flags, cuda_version);
|
let fn_ptr = raw_match(symbol.to_bytes(), flags, cuda_version);
|
||||||
match fn_ptr as usize {
|
match fn_ptr as usize {
|
||||||
0 => {
|
0 => {
|
||||||
if let Some(symbol_status) = symbol_status {
|
if let Some(symbol_status) = symbol_status {
|
||||||
*symbol_status = cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_SYMBOL_NOT_FOUND;
|
*symbol_status = cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_SYMBOL_NOT_FOUND;
|
||||||
}
|
}
|
||||||
*pfn = ptr::null_mut();
|
*pfn = ptr::null_mut();
|
||||||
CUresult::ERROR_NOT_FOUND
|
CUresult::ERROR_NOT_FOUND
|
||||||
}
|
}
|
||||||
usize::MAX => {
|
usize::MAX => {
|
||||||
if let Some(symbol_status) = symbol_status {
|
if let Some(symbol_status) = symbol_status {
|
||||||
*symbol_status = cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_VERSION_NOT_SUFFICIENT;
|
*symbol_status = cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_VERSION_NOT_SUFFICIENT;
|
||||||
}
|
}
|
||||||
*pfn = ptr::null_mut();
|
*pfn = ptr::null_mut();
|
||||||
CUresult::ERROR_NOT_FOUND
|
CUresult::ERROR_NOT_FOUND
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(symbol_status) = symbol_status {
|
if let Some(symbol_status) = symbol_status {
|
||||||
*symbol_status =
|
*symbol_status =
|
||||||
cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_SUCCESS;
|
cuda_types::cuda::CUdriverProcAddressQueryResult::CU_GET_PROC_ADDRESS_SUCCESS;
|
||||||
}
|
}
|
||||||
*pfn = fn_ptr;
|
*pfn = fn_ptr;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn profiler_start() -> CUresult {
|
pub(crate) fn profiler_start() -> CUresult {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn profiler_stop() -> CUresult {
|
pub(crate) fn profiler_stop() -> CUresult {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user