rust_utee/api/
tee_api_mm.rs1use std::{
8 alloc::{Layout, alloc, alloc_zeroed, realloc},
9 ffi::c_void,
10 ptr,
11 sync::atomic::{AtomicPtr, Ordering},
12};
13
14use crate::{
15 api::tee_api_panic::TEE_Panic,
16 tee_api_defines::{TEE_MALLOC_FILL_ZERO, TEE_MALLOC_NO_FILL, TEE_MALLOC_NO_SHARE, TEE_SUCCESS},
17 tee_api_types::TEE_Result,
18};
19
20static TEE_API_INSTANCE_DATA: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
21
22pub const TEE_NULL_SIZED_VA: *mut c_void = 1 as *mut c_void;
27pub const TEE_NULL_SIZED_NO_SHARE_VA: *mut c_void = 2 as *mut c_void;
28
29pub const TEE_USER_MEM_HINT_NO_FILL_ZERO: u32 = 0x80000000;
31
32#[unsafe(no_mangle)]
33pub extern "C" fn TEE_CheckMemoryAccessRights(
34 _accessFlags: u32,
35 _buffer: *mut c_void,
36 _size: usize,
37) -> TEE_Result {
38 TEE_SUCCESS
39}
40
41#[unsafe(no_mangle)]
42pub extern "C" fn TEE_SetInstanceData(instanceData: *const c_void) {
43 TEE_API_INSTANCE_DATA.store(instanceData as _, Ordering::Release);
44}
45#[unsafe(no_mangle)]
46pub extern "C" fn TEE_GetInstanceData() -> *const c_void {
47 TEE_API_INSTANCE_DATA.load(Ordering::Acquire)
48}
49
50#[unsafe(no_mangle)]
51pub extern "C" fn TEE_Malloc(size: usize, hint: u32) -> *mut c_void {
52 let layout = match Layout::from_size_align(size, std::mem::align_of::<u8>()) {
53 Ok(layout) => layout,
54 Err(_) => return ptr::null_mut(),
55 };
56
57 match hint {
58 TEE_MALLOC_FILL_ZERO => {
59 if size == 0 {
60 return TEE_NULL_SIZED_VA;
61 }
62 unsafe { alloc_zeroed(layout) as _ }
63 }
64 TEE_MALLOC_NO_FILL => {
65 TEE_Panic(0);
66 ptr::null_mut()
67 }
68 TEE_MALLOC_NO_SHARE => {
69 if size == 0 {
70 return TEE_NULL_SIZED_NO_SHARE_VA;
71 }
72 unsafe { alloc_zeroed(layout) as _ }
73 }
74 hint if hint == TEE_MALLOC_NO_FILL | TEE_MALLOC_NO_SHARE => {
75 if size == 0 {
76 return TEE_NULL_SIZED_NO_SHARE_VA;
77 }
78 unsafe { alloc(layout) as _ }
79 }
80 TEE_USER_MEM_HINT_NO_FILL_ZERO => {
81 if size == 0 {
82 return TEE_NULL_SIZED_VA;
83 }
84 unsafe { alloc(layout) as _ }
85 }
86 _ => {
87 eprintln!("Invalid hint: 0x{:x}", hint);
88 ptr::null_mut()
89 }
90 }
91}
92
93#[unsafe(no_mangle)]
94pub extern "C" fn TEE_Realloc(buffer: *mut c_void, newSize: usize) -> *mut c_void {
95 let layout = match Layout::from_size_align(newSize, std::mem::align_of::<u8>()) {
96 Ok(layout) => layout,
97 Err(_) => return ptr::null_mut(),
98 };
99
100 if newSize == 0 {
101 TEE_Free(buffer);
102 }
103
104 unsafe { realloc(buffer as _, layout, newSize) as _ }
105}
106
107#[unsafe(no_mangle)]
108pub extern "C" fn TEE_Free(buffer: *mut c_void) {
109 unsafe { libc::free(buffer) }
110}
111
112#[unsafe(no_mangle)]
113pub extern "C" fn TEE_MemMove(dest: *mut c_void, src: *const c_void, size: usize) {
114 unsafe { libc::memmove(dest, src, size) };
115}
116
117#[unsafe(no_mangle)]
118pub extern "C" fn TEE_MemCompare(
119 _buffer1: *const c_void,
120 _buffer2: *const c_void,
121 _size: usize,
122) -> i32 {
123 -1
124}
125
126#[unsafe(no_mangle)]
127pub extern "C" fn TEE_MemFill(buff: *mut c_void, x: u32, size: usize) {
128 unsafe { libc::memset(buff, x as _, size) };
129}