@@ -34,14 +34,10 @@ use crate::trts::Version;
34
34
use crate :: veh:: { ExceptionHandler , ExceptionInfo } ;
35
35
36
36
use super :: alloc:: ResAlloc ;
37
+ use super :: alloc:: StaticAlloc ;
37
38
use super :: bitmap:: BitArray ;
38
39
use super :: flags:: AllocFlags ;
39
40
40
- // pub struct Box<T, A = Global>(_, _)
41
- // where
42
- // A: Allocator,
43
- // T: ?Sized;
44
-
45
41
#[ repr( C ) ]
46
42
#[ derive( Clone ) ]
47
43
pub struct EMA < A >
81
77
) -> SgxResult < Self > {
82
78
// check flags' eligibility
83
79
AllocFlags :: try_from ( alloc_flags. bits ( ) ) ?;
80
+
84
81
if start != 0
85
82
&& length != 0
86
83
&& is_within_enclave ( start as * const u8 , length)
@@ -103,38 +100,33 @@ where
103
100
}
104
101
}
105
102
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)
108
105
// 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 > > {
110
107
let l_start = self . start ;
111
108
let l_length = addr - l_start;
112
109
113
110
let r_start = addr;
114
111
let r_length = ( self . start + self . length ) - addr;
115
112
116
- let new_bitarray = match & mut self . eaccept_map {
113
+ let new_bitarray = match & mut self . eaccept_map {
117
114
Some ( bitarray) => {
118
115
let pos = ( addr - self . start ) >> crate :: arch:: SE_PAGE_SHIFT ;
119
116
// split self.eaccept_map
120
117
Some ( bitarray. split ( pos) ?)
121
118
}
122
- None => {
123
- None
124
- }
119
+ None => None ,
125
120
} ;
126
-
121
+
127
122
// 这里之后可以优化
128
123
// 1. self.clone() 会把原有的bitmap重新alloc并复制一份,但其实clone之后这里是None即可
129
124
// 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 ( ) ) ;
134
126
135
127
self . start = l_start;
136
128
self . length = l_length;
137
-
129
+
138
130
new_ema. start = r_start;
139
131
new_ema. length = r_length;
140
132
new_ema. eaccept_map = new_bitarray;
@@ -145,7 +137,11 @@ where
145
137
// If the previous ema is divided into three parts -> (left ema, middle ema, right ema), return (middle ema, right ema).
146
138
// If the previous ema is divided into two parts -> (left ema, right ema)
147
139
// 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 > > ) > {
149
145
if start > self . start {
150
146
let mut new_ema = self . split ( start) ?;
151
147
if new_ema. start + new_ema. length > start + length {
@@ -224,6 +220,28 @@ where
224
220
}
225
221
}
226
222
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
+
227
245
/// ema_do_commit
228
246
pub fn commit ( & mut self , start : usize , length : usize ) -> SgxResult {
229
247
ensure ! (
@@ -260,8 +278,10 @@ where
260
278
/// uncommit EPC page
261
279
pub fn uncommit ( & mut self , start : usize , length : usize , prot : ProtFlags ) -> SgxResult {
262
280
// 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
+ ) ;
265
285
266
286
if self . alloc_flags . contains ( AllocFlags :: RESERVED ) {
267
287
return Ok ( ( ) ) ;
@@ -303,21 +323,23 @@ where
303
323
}
304
324
305
325
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 {
308
330
typ : self . info . typ ,
309
331
prot,
310
332
} ,
311
- PageInfo {
333
+ PageInfo {
312
334
typ : PageType :: Trim ,
313
335
prot,
314
336
} ,
315
337
) ?;
316
338
317
339
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,
321
343
) ?;
322
344
323
345
let init_idx = ( block_start - self . start ) >> crate :: arch:: SE_PAGE_SHIFT ;
@@ -328,12 +350,14 @@ where
328
350
}
329
351
330
352
// 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 {
333
357
typ : PageType :: Trim ,
334
358
prot,
335
359
} ,
336
- PageInfo {
360
+ PageInfo {
337
361
typ : PageType :: Trim ,
338
362
prot,
339
363
} ,
@@ -401,7 +425,7 @@ where
401
425
) ?;
402
426
}
403
427
404
- Ok ( ( ) )
428
+ Ok ( ( ) )
405
429
}
406
430
407
431
pub fn dealloc ( & mut self ) -> SgxResult {
@@ -421,10 +445,26 @@ where
421
445
round_to ! ( curr_end, align)
422
446
}
423
447
448
+ pub fn end ( & self ) -> usize {
449
+ self . start + self . length
450
+ }
451
+
424
452
pub fn start ( & self ) -> usize {
425
453
self . start
426
454
}
427
455
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
+
428
468
// get and set attributes
429
469
pub fn set_flags ( flags : AllocFlags ) -> SgxResult < ( ) > {
430
470
todo ! ( )
@@ -443,12 +483,11 @@ where
443
483
}
444
484
}
445
485
446
- //
486
+ //
447
487
// intrusive_adapter!(pub RegEmaAda = Box<EMA<ResAlloc>, ResAlloc>: EMA<ResAlloc> { link: LinkedListLink });
448
488
449
489
// 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 } ) ;
451
491
452
492
// 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