1717
1818use buddy_system_allocator:: LockedHeap ;
1919use intrusive_collections:: intrusive_adapter;
20+ use intrusive_collections:: linked_list:: CursorMut ;
2021use intrusive_collections:: { LinkedList , LinkedListLink } ;
2122
2223use alloc:: boxed:: Box ;
@@ -30,8 +31,9 @@ use spin::{Mutex, Once};
3031use sgx_types:: error:: { SgxResult , SgxStatus } ;
3132use sgx_types:: types:: ProtectPerm ;
3233
34+ use crate :: emm:: alloc:: ResAlloc ;
3335use crate :: emm:: ema:: EMA ;
34- use crate :: emm:: user:: { USER_RANGE , self , is_within_user_range} ;
36+ use crate :: emm:: user:: { self , is_within_rts_range , is_within_user_range, USER_RANGE } ;
3537use crate :: enclave:: is_within_enclave;
3638
3739use super :: ema:: ResEmaAda ;
@@ -40,7 +42,7 @@ const STATIC_MEM_SIZE: usize = 65536;
4042
4143/// first level: static memory
4244static STATIC : LockedHeap < 32 > = LockedHeap :: empty ( ) ;
43-
45+
4446static mut STATIC_MEM : [ u8 ; STATIC_MEM_SIZE ] = [ 0 ; STATIC_MEM_SIZE ] ;
4547
4648pub fn init ( ) {
@@ -185,57 +187,130 @@ impl Reserve {
185187 todo ! ( )
186188 }
187189
190+ fn search_ema_range (
191+ & mut self ,
192+ addr : usize ,
193+ len : usize ,
194+ ) -> SgxResult < ( Box < EMA < ResAlloc > > , usize ) > {
195+ let mut cursor: CursorMut < ' _ , ResEmaAda > = self . emas . front_mut ( ) ;
196+ let ema_box = cursor. as_cursor ( ) . clone_pointer ( ) . unwrap ( ) ;
197+
198+ let ptr = Box :: into_raw ( ema_box) as * const EMA < ResAlloc > ;
199+
200+ let cursor_mut = unsafe { self . emas . cursor_mut_from_ptr ( ptr) } ;
201+
202+ todo ! ( )
203+ }
204+
205+ // Find a free space at addr with 'len' bytes in reserve region,
206+ // the request space mustn't intersect with existed ema node.
207+ // If success, return the next ema cursor.
208+ fn find_free_region_at (
209+ & mut self ,
210+ addr : usize ,
211+ len : usize ,
212+ ) -> SgxResult < CursorMut < ' _ , ResEmaAda > > {
213+ if !is_within_enclave ( addr as * const u8 , len) || !is_within_rts_range ( addr, len) {
214+ return Err ( SgxStatus :: InvalidParameter ) ;
215+ }
216+
217+ let mut cursor: CursorMut < ' _ , ResEmaAda > = self . emas . front_mut ( ) ;
218+ while !cursor. is_null ( ) {
219+ let start_curr = cursor. get ( ) . map ( |ema| ema. start ( ) ) . unwrap ( ) ;
220+ let end_curr = start_curr + cursor. get ( ) . map ( |ema| ema. len ( ) ) . unwrap ( ) ;
221+ if start_curr >= addr + len {
222+ return Ok ( cursor) ;
223+ }
224+
225+ if addr >= end_curr {
226+ cursor. move_next ( ) ;
227+ } else {
228+ break ;
229+ }
230+ }
231+
232+ // means addr is larger than the end of the last ema node
233+ if cursor. is_null ( ) {
234+ return Ok ( cursor) ;
235+ }
236+
237+ return Err ( SgxStatus :: InvalidParameter ) ;
238+ }
239+
188240 // Find a free space of size at least 'size' bytes in reserve region,
189241 // return the start address
190- fn find_free_region ( & mut self , len : usize , align : usize ) -> SgxResult < usize > {
242+ fn find_free_region (
243+ & mut self ,
244+ len : usize ,
245+ align : usize ,
246+ ) -> SgxResult < ( usize , CursorMut < ' _ , ResEmaAda > ) > {
191247 let user_range = USER_RANGE . get ( ) . unwrap ( ) ;
192248 let user_base = user_range. start ;
193249 let user_end = user_range. end ;
194250
195- // no ema in list
196- if self . emas . is_empty ( ) {
197- let mut addr = 0 ;
251+ let mut addr = 0 ;
198252
253+ let mut cursor: CursorMut < ' _ , ResEmaAda > = self . emas . front_mut ( ) ;
254+ // no ema in list
255+ if cursor. is_null ( ) {
199256 if user_base >= len {
200257 addr = trim_to ! ( user_base - len, align) ;
201258 if is_within_enclave ( addr as * const u8 , len) {
202- return Ok ( addr) ;
259+ return Ok ( ( addr, cursor ) ) ;
203260 }
204261 } else {
205262 addr = round_to ! ( user_end, align) ;
206263 if is_within_enclave ( addr as * const u8 , len) {
207- return Ok ( addr) ;
264+ return Ok ( ( addr, cursor ) ) ;
208265 }
209266 }
210267 return Err ( SgxStatus :: InvalidParameter ) ;
211268 }
212269
270+ let mut cursor_next = cursor. peek_next ( ) ;
213271
214- let mut cursor = self . emas . cursor_mut ( ) ;
215- while !cursor. is_null ( ) {
216- let curr_end = cursor. get ( )
217- . map ( |ema| ema. aligned_end ( align) ) . unwrap ( ) ;
272+ // ema is_null means pointing to the Null object, not means this ema is empty
273+ while !cursor_next. is_null ( ) {
274+ let curr_end = cursor. get ( ) . map ( |ema| ema. aligned_end ( align) ) . unwrap ( ) ;
218275
219- cursor. move_next ( ) ;
220- if cursor. is_null ( ) {
221- break ;
222- }
223-
224- let next_start = cursor. get ( )
225- . map ( |ema| ema. start ( ) ) . unwrap ( ) ;
226-
227- if curr_end < next_start {
228- let free_size = next_start - curr_end;
229- // 这里或许得用is_within_rts
230- if free_size < len && is_within_enclave ( curr_end as * const u8 , len) {
231- return Ok ( curr_end) ;
276+ let start_next = cursor_next. get ( ) . map ( |ema| ema. start ( ) ) . unwrap ( ) ;
277+
278+ if curr_end < start_next {
279+ let free_size = start_next - curr_end;
280+ if free_size < len && is_within_rts_range ( curr_end, len) {
281+ cursor. move_next ( ) ;
282+ return Ok ( ( curr_end, cursor) ) ;
232283 }
233284 }
234285 cursor. move_next ( ) ;
286+ cursor_next = cursor. peek_next ( ) ;
235287 }
236288
289+ addr = cursor. get ( ) . map ( |ema| ema. aligned_end ( align) ) . unwrap ( ) ;
237290
238- todo ! ( )
291+ if is_within_enclave ( addr as * const u8 , len) && is_within_rts_range ( addr, len) {
292+ cursor. move_next ( ) ;
293+ return Ok ( ( addr, cursor) ) ;
294+ }
295+
296+ // Cursor moves to emas->front_mut.
297+ // Firstly cursor moves to None, then moves to linkedlist head
298+ cursor. move_next ( ) ;
299+ cursor. move_next ( ) ;
300+
301+ // Back to the first ema to check rts region before user region
302+ let start_first = cursor. get ( ) . map ( |ema| ema. start ( ) ) . unwrap ( ) ;
303+ if start_first < len {
304+ return Err ( SgxStatus :: InvalidParameter ) ;
305+ }
306+
307+ addr = trim_to ! ( start_first, align) ;
308+
309+ if is_within_enclave ( addr as * const u8 , len) && is_within_rts_range ( addr, len) {
310+ return Ok ( ( addr, cursor) ) ;
311+ }
312+
313+ Err ( SgxStatus :: InvalidParameter )
239314 }
240315}
241316
0 commit comments