Skip to content

Commit 126f917

Browse files
committed
Implement interior memory range management
1 parent cd03ec9 commit 126f917

File tree

9 files changed

+413
-104
lines changed

9 files changed

+413
-104
lines changed

sgx_trts/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ hyper = ["sgx_types/hyper"]
3939
sgx_types = { path = "../sgx_types" }
4040
sgx_crypto_sys = { path = "../sgx_crypto/sgx_crypto_sys" }
4141
sgx_tlibc_sys = { path = "../sgx_libc/sgx_tlibc_sys" }
42-
intrusive-collections = "0.9.5"
42+
43+
intrusive-collections = { git = "https://github.com/ClawSeven/intrusive-rs.git", rev = "3db5618" }
4344
buddy_system_allocator = "0.9.0"
4445
spin = "0.9.4"
4546
bitflags = "1.3"

sgx_trts/src/edmm/epc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ impl_enum! {
2626
#[repr(u8)]
2727
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2828
pub enum PageType {
29-
Secs = 0,
29+
// Secs = 0,
30+
None = 0,
3031
Tcs = 1,
3132
Reg = 2,
32-
Va = 3,
33+
// Va = 3,
3334
Trim = 4,
35+
Frist = 5,
36+
Rest = 6,
3437
}
3538
}
3639

sgx_trts/src/emm/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::alloc::{AllocError, Allocator, Layout};
22
use core::ptr::NonNull;
33

44
/// alloc layout memory from Reserve region
5-
#[derive(Clone)]
5+
#[derive(Clone, Copy)]
66
pub struct ResAlloc;
77

88
unsafe impl Allocator for ResAlloc {
@@ -16,7 +16,7 @@ unsafe impl Allocator for ResAlloc {
1616
}
1717
}
1818

19-
#[derive(Clone)]
19+
#[derive(Clone, Copy)]
2020
pub struct StaticAlloc;
2121

2222
unsafe impl Allocator for StaticAlloc {

sgx_trts/src/emm/bitmap.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ impl<A: Allocator + Clone> BitArray<A> {
9999
break;
100100
}
101101
}
102-
true_range.push((start,end));
102+
true_range.push((start, end));
103103
}
104104

105-
return true_range;
105+
return true_range;
106106
}
107107

108108
/// Set the value of the bit at a given index.
@@ -155,6 +155,4 @@ impl<A: Allocator + Clone> BitArray<A> {
155155
}
156156
}
157157

158-
159-
160-
// FIXME: add more unit test
158+
// FIXME: add more unit test

sgx_trts/src/emm/ema.rs

+74-35
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ use crate::trts::Version;
3434
use crate::veh::{ExceptionHandler, ExceptionInfo};
3535

3636
use super::alloc::ResAlloc;
37+
use super::alloc::StaticAlloc;
3738
use super::bitmap::BitArray;
3839
use 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)]
4743
pub struct EMA<A>
@@ -81,6 +77,7 @@ where
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 });

sgx_trts/src/emm/flags.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,33 @@ use sgx_types::error::{SgxResult, SgxStatus};
2323
bitflags! {
2424
// 用bitflags的话,在ema输入的时候可能存在RESERVED & COMMIT_NOW 需要check一下
2525
pub struct AllocFlags: u32 {
26-
const RESERVED = 0b0000_0001;
27-
const COMMIT_NOW = 0b0000_0010;
28-
const COMMIT_ON_DEMAND = 0b0000_0100;
29-
const SYSTEM = 0b0001_0000;
30-
const GROWSDOWN = 0x0010_0000;
31-
const GROWSUP = 0x0100_0000;
26+
const RESERVED = 0b0001;
27+
const COMMIT_NOW = 0b0010;
28+
const COMMIT_ON_DEMAND = 0b0100;
29+
const GROWSDOWN = 0b00010000;
30+
const GROWSUP = 0b00100000;
31+
const FIXED = 0b01000000;
3232
}
3333
}
3434

3535
impl AllocFlags {
3636
pub fn try_from(value: u32) -> SgxResult<Self> {
3737
match value {
38-
0b0001_0001 => Ok(Self::RESERVED | Self::SYSTEM),
39-
0b0010_0001 => Ok(Self::RESERVED | Self::GROWSDOWN),
40-
0b0100_0001 => Ok(Self::RESERVED | Self::COMMIT_ON_DEMAND),
41-
0b0001_0010 => Ok(Self::COMMIT_NOW | Self::SYSTEM),
42-
0b0010_0010 => Ok(Self::COMMIT_NOW | Self::GROWSDOWN),
43-
0b0100_0010 => Ok(Self::COMMIT_NOW | Self::COMMIT_ON_DEMAND),
44-
0b0001_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::SYSTEM),
45-
0b0010_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSDOWN),
46-
0b0100_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::COMMIT_ON_DEMAND),
38+
0b0000_0001 => Ok(Self::RESERVED),
39+
0b0000_0010 => Ok(Self::COMMIT_NOW),
40+
0b0000_0100 => Ok(Self::COMMIT_ON_DEMAND),
41+
0b0001_0000 => Ok(Self::GROWSDOWN),
42+
0b0010_0000 => Ok(Self::GROWSUP),
43+
0b0100_0000 => Ok(Self::FIXED),
44+
0b0001_0001 => Ok(Self::RESERVED | Self::GROWSDOWN),
45+
0b0010_0001 => Ok(Self::RESERVED | Self::GROWSUP),
46+
0b0100_0001 => Ok(Self::RESERVED | Self::FIXED),
47+
0b0001_0010 => Ok(Self::COMMIT_NOW | Self::GROWSDOWN),
48+
0b0010_0010 => Ok(Self::COMMIT_NOW | Self::GROWSUP),
49+
0b0100_0010 => Ok(Self::COMMIT_NOW | Self::FIXED),
50+
0b0001_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSDOWN),
51+
0b0010_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSUP),
52+
0b0100_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::FIXED),
4753
_ => Err(SgxStatus::InvalidParameter),
4854
}
4955
}

0 commit comments

Comments
 (0)