Skip to content

Commit 60caea5

Browse files
committed
Couple EMM with rt initialization
1 parent 4ef6ca2 commit 60caea5

File tree

10 files changed

+405
-45
lines changed

10 files changed

+405
-45
lines changed

sgx_trts/src/edmm/mem.rs

+41
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ mod hw {
3131
use crate::edmm::perm;
3232
use crate::edmm::trim;
3333
use crate::elf::program::Type;
34+
use crate::emm::flags::AllocFlags;
35+
use crate::emm::range::RM;
3436
use crate::enclave::parse;
3537
use crate::enclave::MmLayout;
3638
use crate::feature::{SysFeatures, Version};
@@ -190,6 +192,45 @@ mod hw {
190192
Ok(())
191193
}
192194

195+
pub fn init_segment_emas() -> SgxResult {
196+
let elf = parse::new_elf()?;
197+
let text_relo = parse::has_text_relo()?;
198+
199+
let base = MmLayout::image_base();
200+
for phdr in elf.program_iter() {
201+
let typ = phdr.get_type().unwrap_or(Type::Null);
202+
203+
if typ == Type::Load {
204+
let mut perm = ProtFlags::R;
205+
let start = base + trim_to_page!(phdr.virtual_addr() as usize);
206+
let end =
207+
base + round_to_page!(phdr.virtual_addr() as usize + phdr.mem_size() as usize);
208+
209+
if phdr.flags().is_write() || text_relo {
210+
perm |= ProtFlags::W;
211+
}
212+
if phdr.flags().is_execute() {
213+
perm |= ProtFlags::X;
214+
}
215+
216+
let mut range_manage = RM.get().unwrap().lock();
217+
range_manage.init_static_region(
218+
start,
219+
end - start,
220+
AllocFlags::SYSTEM,
221+
PageInfo {
222+
typ: PageType::Reg,
223+
prot: perm,
224+
},
225+
None,
226+
None,
227+
)?;
228+
}
229+
}
230+
231+
Ok(())
232+
}
233+
193234
fn modify_perm(addr: usize, count: usize, perm: u8) -> SgxResult {
194235
let pages = PageRange::new(
195236
addr,

sgx_trts/src/emm/alloc.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License..
17+
118
use core::alloc::{AllocError, Allocator, Layout};
219
use core::ptr::NonNull;
320

sgx_trts/src/emm/bitmap.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License..
17+
1718
use alloc::boxed::Box;
1819
use alloc::vec;
1920
use core::alloc::Allocator;

sgx_trts/src/emm/ema.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl EMA {
7777
alloc: Alloc,
7878
) -> SgxResult<Self> {
7979
// check alloc flags' eligibility
80-
AllocFlags::try_from(alloc_flags.bits())?;
80+
// AllocFlags::try_from(alloc_flags.bits())?;
8181

8282
if start != 0
8383
&& length != 0
@@ -576,6 +576,25 @@ impl EMA {
576576
(addr >= self.start) && (addr < self.start + self.length)
577577
}
578578

579+
pub fn set_eaccept_map_full(&mut self) -> SgxResult {
580+
if self.eaccept_map.is_none() {
581+
let eaccept_map = match self.alloc {
582+
Alloc::Reserve => {
583+
let page_num = self.length >> SE_PAGE_SHIFT;
584+
BitArray::new(page_num, Alloc::Reserve)?
585+
}
586+
Alloc::Static => {
587+
let page_num = self.length >> SE_PAGE_SHIFT;
588+
BitArray::new(page_num, Alloc::Static)?
589+
}
590+
};
591+
self.eaccept_map = Some(eaccept_map);
592+
} else {
593+
self.eaccept_map.as_mut().unwrap().set_full();
594+
}
595+
Ok(())
596+
}
597+
579598
fn set_flags(&mut self, flags: AllocFlags) {
580599
self.alloc_flags = flags;
581600
}

sgx_trts/src/emm/flags.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License..
1717

1818
use bitflags::bitflags;
19-
use sgx_types::error::{SgxResult, SgxStatus};
2019

2120
bitflags! {
2221
pub struct AllocFlags: u32 {
@@ -26,28 +25,6 @@ bitflags! {
2625
const GROWSDOWN = 0b00010000;
2726
const GROWSUP = 0b00100000;
2827
const FIXED = 0b01000000;
29-
}
30-
}
31-
32-
impl AllocFlags {
33-
pub fn try_from(value: u32) -> SgxResult<Self> {
34-
match value {
35-
0b0000_0001 => Ok(Self::RESERVED),
36-
0b0000_0010 => Ok(Self::COMMIT_NOW),
37-
0b0000_0100 => Ok(Self::COMMIT_ON_DEMAND),
38-
0b0001_0000 => Ok(Self::GROWSDOWN),
39-
0b0010_0000 => Ok(Self::GROWSUP),
40-
0b0100_0000 => Ok(Self::FIXED),
41-
0b0001_0001 => Ok(Self::RESERVED | Self::GROWSDOWN),
42-
0b0010_0001 => Ok(Self::RESERVED | Self::GROWSUP),
43-
0b0100_0001 => Ok(Self::RESERVED | Self::FIXED),
44-
0b0001_0010 => Ok(Self::COMMIT_NOW | Self::GROWSDOWN),
45-
0b0010_0010 => Ok(Self::COMMIT_NOW | Self::GROWSUP),
46-
0b0100_0010 => Ok(Self::COMMIT_NOW | Self::FIXED),
47-
0b0001_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSDOWN),
48-
0b0010_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSUP),
49-
0b0100_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::FIXED),
50-
_ => Err(SgxStatus::InvalidParameter),
51-
}
28+
const SYSTEM = 0b10000000;
5229
}
5330
}

sgx_trts/src/emm/init.rs

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License..
17+
18+
use sgx_types::error::SgxResult;
19+
20+
use crate::arch::{Layout, LayoutEntry};
21+
use crate::edmm::mem::init_segment_emas;
22+
use crate::edmm::{PageInfo, PageType, ProtFlags};
23+
use crate::emm::flags::AllocFlags;
24+
use crate::emm::interior::Alloc;
25+
use crate::emm::range::{RangeType, EMA_PROT_MASK};
26+
use crate::enclave::MmLayout;
27+
use crate::{arch, emm::range::RM};
28+
29+
use super::{interior::init_alloc, range::init_range_manage, user::init_user_range};
30+
31+
pub fn init_emm(user_start: usize, user_end: usize) {
32+
// check user_start not equals to 0
33+
init_user_range(user_start, user_end);
34+
init_range_manage();
35+
init_alloc();
36+
}
37+
38+
pub fn init_rts_emas() -> SgxResult {
39+
init_segment_emas()?;
40+
init_rts_contexts_emas(arch::Global::get().layout_table(), 0)?;
41+
Ok(())
42+
}
43+
44+
fn init_rts_contexts_emas(table: &[Layout], offset: usize) -> SgxResult {
45+
unsafe {
46+
for (i, layout) in table.iter().enumerate() {
47+
if is_group_id!(layout.group.id) {
48+
let mut step = 0_usize;
49+
for _ in 0..layout.group.load_times {
50+
step += layout.group.load_step as usize;
51+
init_rts_contexts_emas(&table[i - layout.group.entry_count as usize..i], step)?;
52+
}
53+
} else {
54+
build_rts_context_emas(&layout.entry, offset)?;
55+
}
56+
}
57+
Ok(())
58+
}
59+
}
60+
61+
fn build_rts_context_emas(entry: &LayoutEntry, offset: usize) -> SgxResult {
62+
let rva = offset + (entry.rva as usize);
63+
assert!(is_page_aligned!(rva));
64+
65+
// TODO: not sure get_enclave_base() equal to elrange_base or image_base
66+
let addr = MmLayout::elrange_base() + (rva as usize);
67+
let size = (entry.page_count << arch::SE_PAGE_SHIFT) as usize;
68+
let mut range_manage = RM.get().unwrap().lock();
69+
70+
// entry is guard page or has EREMOVE, build a reserved ema
71+
if (entry.si_flags == 0) || (entry.attributes & arch::PAGE_ATTR_EREMOVE != 0) {
72+
range_manage.init_static_region(
73+
addr,
74+
size,
75+
AllocFlags::RESERVED | AllocFlags::SYSTEM,
76+
PageInfo {
77+
typ: PageType::None,
78+
prot: ProtFlags::NONE,
79+
},
80+
None,
81+
None,
82+
)?;
83+
return Ok(());
84+
}
85+
86+
let post_remove = (entry.attributes & arch::PAGE_ATTR_POST_REMOVE) != 0;
87+
let post_add = (entry.attributes & arch::PAGE_ATTR_POST_ADD) != 0;
88+
let static_min = (entry.attributes & arch::PAGE_ATTR_EADD) != 0;
89+
90+
if post_remove {
91+
// TODO: maybe AllocFlags need more flags or PageType is not None
92+
range_manage.init_static_region(
93+
addr,
94+
size,
95+
AllocFlags::SYSTEM,
96+
PageInfo {
97+
typ: PageType::None,
98+
prot: ProtFlags::R | ProtFlags::W,
99+
},
100+
None,
101+
None,
102+
)?;
103+
104+
range_manage.dealloc(addr, size, RangeType::Rts)?;
105+
}
106+
107+
if post_add {
108+
let commit_direction = if entry.id == arch::LAYOUT_ID_STACK_MAX
109+
|| entry.id == arch::LAYOUT_ID_STACK_DYN_MAX
110+
|| entry.id == arch::LAYOUT_ID_STACK_DYN_MIN
111+
{
112+
AllocFlags::GROWSDOWN
113+
} else {
114+
AllocFlags::GROWSUP
115+
};
116+
117+
// TODO: revise alloc and not use int
118+
range_manage.alloc_inner(
119+
Some(addr),
120+
size,
121+
AllocFlags::COMMIT_ON_DEMAND
122+
| commit_direction
123+
| AllocFlags::SYSTEM
124+
| AllocFlags::FIXED,
125+
PageInfo {
126+
typ: PageType::Reg,
127+
prot: ProtFlags::R | ProtFlags::W,
128+
},
129+
None,
130+
None,
131+
RangeType::Rts,
132+
Alloc::Reserve,
133+
)?;
134+
} else if static_min {
135+
let info = if entry.id == arch::LAYOUT_ID_TCS {
136+
PageInfo {
137+
typ: PageType::Tcs,
138+
prot: ProtFlags::NONE,
139+
}
140+
} else {
141+
PageInfo {
142+
typ: PageType::Reg,
143+
prot: ProtFlags::from_bits_truncate(
144+
(entry.si_flags as usize & EMA_PROT_MASK) as u8,
145+
),
146+
}
147+
};
148+
range_manage.init_static_region(addr, size, AllocFlags::SYSTEM, info, None, None)?;
149+
}
150+
151+
Ok(())
152+
}

sgx_trts/src/emm/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License..
1717

18-
use self::{interior::init_alloc, range::init_range_manage, user::init_user_range};
19-
2018
pub(crate) mod alloc;
2119
pub(crate) mod bitmap;
2220
pub(crate) mod ema;
2321
pub(crate) mod flags;
22+
pub(crate) mod init;
2423
#[cfg(not(any(feature = "sim", feature = "hyper")))]
2524
pub(crate) mod interior;
2625
mod pfhandler;
2726
pub(crate) mod range;
2827
pub(crate) mod user;
29-
30-
fn init_emm(user_start: usize, user_end: usize) {
31-
init_user_range(user_start, user_end);
32-
init_range_manage();
33-
init_alloc();
34-
}

0 commit comments

Comments
 (0)