17
17
18
18
use buddy_system_allocator:: LockedHeap ;
19
19
use intrusive_collections:: intrusive_adapter;
20
+ use intrusive_collections:: linked_list:: CursorMut ;
20
21
use intrusive_collections:: { LinkedList , LinkedListLink } ;
21
22
22
23
use alloc:: boxed:: Box ;
@@ -30,8 +31,9 @@ use spin::{Mutex, Once};
30
31
use sgx_types:: error:: { SgxResult , SgxStatus } ;
31
32
use sgx_types:: types:: ProtectPerm ;
32
33
34
+ use crate :: emm:: alloc:: ResAlloc ;
33
35
use 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 } ;
35
37
use crate :: enclave:: is_within_enclave;
36
38
37
39
use super :: ema:: ResEmaAda ;
@@ -40,7 +42,7 @@ const STATIC_MEM_SIZE: usize = 65536;
40
42
41
43
/// first level: static memory
42
44
static STATIC : LockedHeap < 32 > = LockedHeap :: empty ( ) ;
43
-
45
+
44
46
static mut STATIC_MEM : [ u8 ; STATIC_MEM_SIZE ] = [ 0 ; STATIC_MEM_SIZE ] ;
45
47
46
48
pub fn init ( ) {
@@ -185,57 +187,130 @@ impl Reserve {
185
187
todo ! ( )
186
188
}
187
189
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
+
188
240
// Find a free space of size at least 'size' bytes in reserve region,
189
241
// 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 > ) > {
191
247
let user_range = USER_RANGE . get ( ) . unwrap ( ) ;
192
248
let user_base = user_range. start ;
193
249
let user_end = user_range. end ;
194
250
195
- // no ema in list
196
- if self . emas . is_empty ( ) {
197
- let mut addr = 0 ;
251
+ let mut addr = 0 ;
198
252
253
+ let mut cursor: CursorMut < ' _ , ResEmaAda > = self . emas . front_mut ( ) ;
254
+ // no ema in list
255
+ if cursor. is_null ( ) {
199
256
if user_base >= len {
200
257
addr = trim_to ! ( user_base - len, align) ;
201
258
if is_within_enclave ( addr as * const u8 , len) {
202
- return Ok ( addr) ;
259
+ return Ok ( ( addr, cursor ) ) ;
203
260
}
204
261
} else {
205
262
addr = round_to ! ( user_end, align) ;
206
263
if is_within_enclave ( addr as * const u8 , len) {
207
- return Ok ( addr) ;
264
+ return Ok ( ( addr, cursor ) ) ;
208
265
}
209
266
}
210
267
return Err ( SgxStatus :: InvalidParameter ) ;
211
268
}
212
269
270
+ let mut cursor_next = cursor. peek_next ( ) ;
213
271
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 ( ) ;
218
275
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) ) ;
232
283
}
233
284
}
234
285
cursor. move_next ( ) ;
286
+ cursor_next = cursor. peek_next ( ) ;
235
287
}
236
288
289
+ addr = cursor. get ( ) . map ( |ema| ema. aligned_end ( align) ) . unwrap ( ) ;
237
290
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 )
239
314
}
240
315
}
241
316
0 commit comments