@@ -34,14 +34,10 @@ use crate::trts::Version;
3434use crate :: veh:: { ExceptionHandler , ExceptionInfo } ;
3535
3636use super :: alloc:: ResAlloc ;
37+ use super :: alloc:: StaticAlloc ;
3738use super :: bitmap:: BitArray ;
3839use super :: flags:: AllocFlags ;
3940
40- // pub struct Box<T, A = Global>(_, _)
41- // where
42- // A: Allocator,
43- // T: ?Sized;
44-
4541#[ repr( C ) ]
4642#[ derive( Clone ) ]
4743pub struct EMA < A >
8177 ) -> SgxResult < Self > {
8278 // check flags' eligibility
8379 AllocFlags :: try_from ( alloc_flags. bits ( ) ) ?;
80+
8481 if start != 0
8582 && length != 0
8683 && is_within_enclave ( start as * const u8 , length)
@@ -103,38 +100,33 @@ where
103100 }
104101 }
105102
106- // Returns a newly allocated ema in charging of the memory in the range [addr, len).
107- // After the call, the original ema will be left containing the elements [0, addr)
103+ // Returns a newly allocated ema in charging of the memory in the range [addr, len).
104+ // After the call, the original ema will be left containing the elements [0, addr)
108105 // with its previous capacity unchanged.
109- pub fn split ( & mut self , addr : usize ) -> SgxResult < Box < EMA < A > , A > > {
106+ pub fn split ( & mut self , addr : usize ) -> SgxResult < Box < EMA < A > , A > > {
110107 let l_start = self . start ;
111108 let l_length = addr - l_start;
112109
113110 let r_start = addr;
114111 let r_length = ( self . start + self . length ) - addr;
115112
116- let new_bitarray = match & mut self . eaccept_map {
113+ let new_bitarray = match & mut self . eaccept_map {
117114 Some ( bitarray) => {
118115 let pos = ( addr - self . start ) >> crate :: arch:: SE_PAGE_SHIFT ;
119116 // split self.eaccept_map
120117 Some ( bitarray. split ( pos) ?)
121118 }
122- None => {
123- None
124- }
119+ None => None ,
125120 } ;
126-
121+
127122 // 这里之后可以优化
128123 // 1. self.clone() 会把原有的bitmap重新alloc并复制一份,但其实clone之后这里是None即可
129124 // 2. 使用Box::new_in 会把 self.clone() 这部分在栈上的数据再拷贝一份到Box新申请的内存区域
130- let mut new_ema: Box < EMA < A > , A > = Box :: new_in (
131- self . clone ( ) ,
132- self . alloc . clone ( )
133- ) ;
125+ let mut new_ema: Box < EMA < A > , A > = Box :: new_in ( self . clone ( ) , self . alloc . clone ( ) ) ;
134126
135127 self . start = l_start;
136128 self . length = l_length;
137-
129+
138130 new_ema. start = r_start;
139131 new_ema. length = r_length;
140132 new_ema. eaccept_map = new_bitarray;
@@ -145,7 +137,11 @@ where
145137 // If the previous ema is divided into three parts -> (left ema, middle ema, right ema), return (middle ema, right ema).
146138 // If the previous ema is divided into two parts -> (left ema, right ema)
147139 // end split: return (None, right ema), start split: return (left ema, None)
148- fn split_into_three ( & mut self , start : usize , length : usize ) -> SgxResult < ( Option < Box < EMA < A > , A > > , Option < Box < EMA < A > , A > > ) > {
140+ fn split_into_three (
141+ & mut self ,
142+ start : usize ,
143+ length : usize ,
144+ ) -> SgxResult < ( Option < Box < EMA < A > , A > > , Option < Box < EMA < A > , A > > ) > {
149145 if start > self . start {
150146 let mut new_ema = self . split ( start) ?;
151147 if new_ema. start + new_ema. length > start + length {
@@ -224,6 +220,28 @@ where
224220 }
225221 }
226222
223+ // Attension, return EACCES SgxStatus may be more appropriate
224+ pub fn commit_check ( & self ) -> SgxResult {
225+ if self . info . prot . intersects ( ProtFlags :: R | ProtFlags :: W ) {
226+ return Err ( SgxStatus :: InvalidParameter ) ;
227+ }
228+
229+ if self . info . typ != PageType :: Reg {
230+ return Err ( SgxStatus :: InvalidParameter ) ;
231+ }
232+
233+ if self . alloc_flags . contains ( AllocFlags :: RESERVED ) {
234+ return Err ( SgxStatus :: InvalidParameter ) ;
235+ }
236+
237+ Ok ( ( ) )
238+ }
239+
240+ /// commit all the memory in this ema
241+ pub fn commit_self ( & mut self ) -> SgxResult {
242+ self . commit ( self . start , self . length )
243+ }
244+
227245 /// ema_do_commit
228246 pub fn commit ( & mut self , start : usize , length : usize ) -> SgxResult {
229247 ensure ! (
@@ -260,8 +278,10 @@ where
260278 /// uncommit EPC page
261279 pub fn uncommit ( & mut self , start : usize , length : usize , prot : ProtFlags ) -> SgxResult {
262280 // need READ for trimming
263- ensure ! ( self . info. prot != ProtFlags :: NONE && self . eaccept_map. is_some( ) ,
264- SgxStatus :: InvalidParameter ) ;
281+ ensure ! (
282+ self . info. prot != ProtFlags :: NONE && self . eaccept_map. is_some( ) ,
283+ SgxStatus :: InvalidParameter
284+ ) ;
265285
266286 if self . alloc_flags . contains ( AllocFlags :: RESERVED ) {
267287 return Ok ( ( ) ) ;
@@ -303,21 +323,23 @@ where
303323 }
304324
305325 let block_length = block_end - block_start;
306- perm:: modify_ocall ( block_start, block_length,
307- PageInfo {
326+ perm:: modify_ocall (
327+ block_start,
328+ block_length,
329+ PageInfo {
308330 typ : self . info . typ ,
309331 prot,
310332 } ,
311- PageInfo {
333+ PageInfo {
312334 typ : PageType :: Trim ,
313335 prot,
314336 } ,
315337 ) ?;
316338
317339 let pages = PageRange :: new (
318- block_start,
319- block_length / crate :: arch:: SE_PAGE_SIZE ,
320- trim_info
340+ block_start,
341+ block_length / crate :: arch:: SE_PAGE_SIZE ,
342+ trim_info,
321343 ) ?;
322344
323345 let init_idx = ( block_start - self . start ) >> crate :: arch:: SE_PAGE_SHIFT ;
@@ -328,12 +350,14 @@ where
328350 }
329351
330352 // eaccept trim notify
331- perm:: modify_ocall ( block_start, block_length,
332- PageInfo {
353+ perm:: modify_ocall (
354+ block_start,
355+ block_length,
356+ PageInfo {
333357 typ : PageType :: Trim ,
334358 prot,
335359 } ,
336- PageInfo {
360+ PageInfo {
337361 typ : PageType :: Trim ,
338362 prot,
339363 } ,
@@ -401,7 +425,7 @@ where
401425 ) ?;
402426 }
403427
404- Ok ( ( ) )
428+ Ok ( ( ) )
405429 }
406430
407431 pub fn dealloc ( & mut self ) -> SgxResult {
@@ -421,10 +445,26 @@ where
421445 round_to ! ( curr_end, align)
422446 }
423447
448+ pub fn end ( & self ) -> usize {
449+ self . start + self . length
450+ }
451+
424452 pub fn start ( & self ) -> usize {
425453 self . start
426454 }
427455
456+ pub fn len ( & self ) -> usize {
457+ self . length
458+ }
459+
460+ pub fn lower_than_addr ( & self , addr : usize ) -> bool {
461+ self . end ( ) <= addr
462+ }
463+
464+ pub fn higher_than_addr ( & self , addr : usize ) -> bool {
465+ self . start >= addr
466+ }
467+
428468 // get and set attributes
429469 pub fn set_flags ( flags : AllocFlags ) -> SgxResult < ( ) > {
430470 todo ! ( )
@@ -443,12 +483,11 @@ where
443483 }
444484}
445485
446- //
486+ //
447487// intrusive_adapter!(pub RegEmaAda = Box<EMA<ResAlloc>, ResAlloc>: EMA<ResAlloc> { link: LinkedListLink });
448488
449489// regular ema adapter
450- intrusive_adapter ! ( pub RegEmaAda = Box <EMA <ResAlloc >>: EMA <ResAlloc > { link: LinkedListLink } ) ;
490+ intrusive_adapter ! ( pub RegEmaAda = ResAlloc , Box <EMA <ResAlloc >, ResAlloc >: EMA <ResAlloc > { link: LinkedListLink } ) ;
451491
452492// reserve ema adapter
453- intrusive_adapter ! ( pub ResEmaAda = Box <EMA <ResAlloc >>: EMA <ResAlloc > { link: LinkedListLink } ) ;
454-
493+ intrusive_adapter ! ( pub ResEmaAda = StaticAlloc , Box <EMA <StaticAlloc >, StaticAlloc >: EMA <StaticAlloc > { link: LinkedListLink } ) ;
0 commit comments