Skip to content

Commit 99ae008

Browse files
committed
Remove range-type related methods
1 parent 6bd4f66 commit 99ae008

12 files changed

Lines changed: 208 additions & 227 deletions

File tree

samplecode/emm/enclave/src/lib.rs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
6969
let prot = ProtFlags::from_bits(pd.access as u8).unwrap();
7070
let rw_bit = unsafe { pd.pf.pfec.bits.rw() };
7171
if (rw_bit == 1) && (prot == ProtFlags::W) {
72-
if emm::user_mm_modify_perms(addr, SE_PAGE_SIZE, ProtFlags::W | ProtFlags::R).is_err() {
72+
if emm::mm_modify_perms(addr, SE_PAGE_SIZE, ProtFlags::W | ProtFlags::R).is_err() {
7373
panic!()
7474
};
7575
} else if (rw_bit == 0) && prot.contains(ProtFlags::R) {
76-
if emm::user_mm_modify_perms(addr, SE_PAGE_SIZE, prot).is_err() {
76+
if emm::mm_modify_perms(addr, SE_PAGE_SIZE, prot).is_err() {
7777
panic!()
7878
};
7979
} else {
@@ -87,12 +87,12 @@ pub extern "C" fn permission_pfhandler(info: &mut PfInfo, priv_data: *mut c_void
8787
fn test_modify_perms() -> SgxStatus {
8888
let mut pd = PfData::default();
8989
// example 1:
90-
let mut options = EmaOptions::new(0, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
90+
let mut options = EmaOptions::new(None, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
9191
options.handle(
9292
Some(permission_pfhandler),
9393
Some(&mut pd as *mut PfData as *mut c_void),
9494
);
95-
let base = emm::user_mm_alloc(&mut options).unwrap();
95+
let base = emm::mm_alloc_user(&mut options).unwrap();
9696

9797
let data = unsafe { (base as *const u8).read() };
9898
assert!(data == 0);
@@ -104,7 +104,7 @@ fn test_modify_perms() -> SgxStatus {
104104
// write success without PF
105105
assert!(unsafe { pd.pf.pfec.errcd } == 0);
106106

107-
let res = emm::user_mm_modify_perms(base, ALLOC_SIZE / 2, ProtFlags::R);
107+
let res = emm::mm_modify_perms(base, ALLOC_SIZE / 2, ProtFlags::R);
108108
assert!(res.is_ok());
109109

110110
pd.access = ProtFlags::R.bits() as i32;
@@ -156,55 +156,55 @@ fn test_dynamic_expand_tcs() -> SgxStatus {
156156
#[no_mangle]
157157
fn test_modify_types() -> SgxStatus {
158158
// example 1:
159-
let mut options = EmaOptions::new(0, SE_PAGE_SIZE, AllocFlags::COMMIT_NOW);
160-
let base = emm::user_mm_alloc(&mut options).unwrap();
159+
let mut options = EmaOptions::new(None, SE_PAGE_SIZE, AllocFlags::COMMIT_NOW);
160+
let base = emm::mm_alloc_user(&mut options).unwrap();
161161

162-
let res = emm::user_mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
162+
let res = emm::mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
163163
assert!(res.is_ok());
164164

165-
let res = emm::user_mm_uncommit(base, SE_PAGE_SIZE);
165+
let res = emm::mm_uncommit(base, SE_PAGE_SIZE);
166166
assert!(res.is_ok());
167167

168168
// example 2:
169-
let mut options = EmaOptions::new(0, SE_PAGE_SIZE, AllocFlags::COMMIT_NOW);
170-
let base = emm::user_mm_alloc(&mut options).unwrap();
169+
let mut options = EmaOptions::new(None, SE_PAGE_SIZE, AllocFlags::COMMIT_NOW);
170+
let base = emm::mm_alloc_user(&mut options).unwrap();
171171

172-
let res = emm::user_mm_modify_perms(base, SE_PAGE_SIZE, ProtFlags::NONE);
172+
let res = emm::mm_modify_perms(base, SE_PAGE_SIZE, ProtFlags::NONE);
173173
assert!(res.is_ok());
174174

175-
let res = emm::user_mm_uncommit(base, SE_PAGE_SIZE);
175+
let res = emm::mm_uncommit(base, SE_PAGE_SIZE);
176176
assert!(res.is_ok());
177177

178178
// example 3:
179-
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
179+
let res = emm::mm_dealloc(0, ALLOC_SIZE);
180180
assert!(res == Err(EINVAL));
181181

182-
let mut options = EmaOptions::new(0, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
183-
let base = emm::user_mm_alloc(&mut options).unwrap();
182+
let mut options = EmaOptions::new(None, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
183+
let base = emm::mm_alloc_user(&mut options).unwrap();
184184

185-
let res = emm::user_mm_modify_type(base + SE_PAGE_SIZE, SE_PAGE_SIZE, PageType::Frist);
185+
let res = emm::mm_modify_type(base + SE_PAGE_SIZE, SE_PAGE_SIZE, PageType::Frist);
186186
assert!(res == Err(EPERM));
187187

188-
let res = emm::user_mm_modify_perms(
188+
let res = emm::mm_modify_perms(
189189
base + SE_PAGE_SIZE,
190190
SE_PAGE_SIZE,
191191
ProtFlags::R | ProtFlags::X,
192192
);
193193
assert!(res.is_ok());
194194

195-
let res = emm::user_mm_modify_type(base + SE_PAGE_SIZE, SE_PAGE_SIZE, PageType::Tcs);
195+
let res = emm::mm_modify_type(base + SE_PAGE_SIZE, SE_PAGE_SIZE, PageType::Tcs);
196196
assert!(res == Err(EACCES));
197197

198-
let res = emm::user_mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
198+
let res = emm::mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
199199
assert!(res.is_ok());
200200

201-
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
201+
let res = emm::mm_uncommit(base, ALLOC_SIZE);
202202
assert!(res.is_ok());
203203

204-
let res = emm::user_mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
204+
let res = emm::mm_modify_type(base, SE_PAGE_SIZE, PageType::Tcs);
205205
assert!(res == Err(EACCES));
206206

207-
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
207+
let res = emm::mm_dealloc(base, ALLOC_SIZE);
208208
assert!(res.is_ok());
209209

210210
println!("Successfully run modify types!");
@@ -213,44 +213,48 @@ fn test_modify_types() -> SgxStatus {
213213

214214
#[no_mangle]
215215
fn test_commit_and_uncommit() -> SgxStatus {
216-
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
216+
let res = emm::mm_dealloc(0, ALLOC_SIZE);
217217
assert!(res == Err(EINVAL));
218218

219-
let mut options = EmaOptions::new(0, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
220-
let base = emm::user_mm_alloc(&mut options).unwrap();
219+
let mut options = EmaOptions::new(None, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
220+
let base = emm::mm_alloc_user(&mut options).unwrap();
221221

222-
let res = emm::user_mm_commit(base, ALLOC_SIZE);
222+
let res = emm::mm_commit(base, ALLOC_SIZE);
223223
assert!(res.is_ok());
224224

225-
let mut options = EmaOptions::new(base, ALLOC_SIZE, AllocFlags::COMMIT_NOW | AllocFlags::FIXED);
226-
let res = emm::user_mm_alloc(&mut options);
225+
let mut options = EmaOptions::new(
226+
Some(base),
227+
ALLOC_SIZE,
228+
AllocFlags::COMMIT_NOW | AllocFlags::FIXED,
229+
);
230+
let res = emm::mm_alloc_user(&mut options);
227231

228232
assert!(res == Err(EEXIST));
229233

230-
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
234+
let res = emm::mm_uncommit(base, ALLOC_SIZE);
231235
assert!(res.is_ok());
232236

233-
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
237+
let res = emm::mm_uncommit(base, ALLOC_SIZE);
234238
assert!(res.is_ok());
235239

236-
let res = emm::user_mm_commit(base, ALLOC_SIZE);
240+
let res = emm::mm_commit(base, ALLOC_SIZE);
237241
assert!(res.is_ok());
238242

239-
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
243+
let res = emm::mm_dealloc(base, ALLOC_SIZE);
240244
assert!(res.is_ok());
241245

242-
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
246+
let res = emm::mm_dealloc(base, ALLOC_SIZE);
243247
assert!(res == Err(EINVAL));
244248

245-
let res = emm::user_mm_uncommit(base, ALLOC_SIZE);
249+
let res = emm::mm_uncommit(base, ALLOC_SIZE);
246250
assert!(res == Err(EINVAL));
247251

248252
let mut options = EmaOptions::new(
249-
0,
253+
None,
250254
ALLOC_SIZE,
251255
AllocFlags::COMMIT_ON_DEMAND | AllocFlags::FIXED,
252256
);
253-
let base2 = emm::user_mm_alloc(&mut options).unwrap();
257+
let base2 = emm::mm_alloc_user(&mut options).unwrap();
254258

255259
assert!(base == base2);
256260

@@ -260,7 +264,7 @@ fn test_commit_and_uncommit() -> SgxStatus {
260264
ptr.add(ALLOC_SIZE - 1).write(0xFF);
261265
};
262266

263-
let res = emm::user_mm_dealloc(base2, ALLOC_SIZE);
267+
let res = emm::mm_dealloc(base2, ALLOC_SIZE);
264268
assert!(res.is_ok());
265269

266270
println!("Successfully run commit and uncommit!");
@@ -283,13 +287,13 @@ fn test_stack_expand() -> SgxStatus {
283287

284288
#[no_mangle]
285289
fn test_emm_alloc_dealloc() -> SgxStatus {
286-
let res = emm::user_mm_dealloc(0, ALLOC_SIZE);
290+
let res = emm::mm_dealloc(0, ALLOC_SIZE);
287291
assert!(res == Err(EINVAL));
288292

289-
let mut options = EmaOptions::new(0, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
290-
let base = emm::user_mm_alloc(&mut options).unwrap();
293+
let mut options = EmaOptions::new(None, ALLOC_SIZE, AllocFlags::COMMIT_NOW);
294+
let base = emm::mm_alloc_user(&mut options).unwrap();
291295

292-
let res = emm::user_mm_dealloc(base, ALLOC_SIZE);
296+
let res = emm::mm_dealloc(base, ALLOC_SIZE);
293297
assert!(res.is_ok());
294298
println!("Successfully run alloc and dealloc!");
295299
SgxStatus::Success

sgx_rsrvmm/src/rsrvmm/area.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::cmp::{self, Ordering};
2424
use core::convert::From;
2525
use core::ops::{Deref, DerefMut};
2626
use core::{fmt, panic};
27-
use sgx_trts::emm::{rts_mm_modify_perms, ProtFlags};
27+
use sgx_trts::emm::{mm_modify_perms, ProtFlags};
2828
use sgx_trts::trts;
2929
use sgx_types::error::errno::*;
3030
use sgx_types::error::OsResult;
@@ -367,7 +367,7 @@ impl MmArea {
367367
let (pe_needed, pr_needed) = self.is_needed_modify_perm(new_perm)?;
368368

369369
if pe_needed || pr_needed {
370-
let res = rts_mm_modify_perms(self.start(), count << SE_PAGE_SHIFT, prot);
370+
let res = mm_modify_perms(self.start(), count << SE_PAGE_SHIFT, prot);
371371
if res.is_err() {
372372
panic!()
373373
}

sgx_rsrvmm/src/rsrvmm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl RsrvMem {
161161
)
162162
};
163163

164-
let ret = emm::rts_mm_commit(start_addr, size >> SE_PAGE_SHIFT);
164+
let ret = emm::mm_commit(start_addr, size >> SE_PAGE_SHIFT);
165165
if ret.is_err() {
166166
self.committed_size = pre_committed;
167167
bail!(ENOMEM);

sgx_trts/src/capi.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use crate::call::{ocall, OCallIndex, OcBuffer};
2020
use crate::emm::ema::EmaOptions;
2121
use crate::emm::page::AllocFlags;
2222
use crate::emm::pfhandler::PfHandler;
23-
use crate::emm::range::{
23+
use crate::emm::vmmgr::{
2424
ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT, PAGE_TYPE_MASK,
25-
PAGE_TYPE_SHIFT,
25+
PAGE_TYPE_SHIFT, RangeType,
2626
};
27-
use crate::emm::{rts_mm_commit, rts_mm_uncommit, user_mm_alloc, PageInfo, PageType, ProtFlags};
27+
use crate::emm::{mm_commit, mm_uncommit, mm_alloc_user, PageInfo, PageType, ProtFlags, self};
2828
use crate::enclave::{self, is_within_enclave, MmLayout};
2929
use crate::error;
3030
use crate::rand::rand;
@@ -181,7 +181,19 @@ pub unsafe extern "C" fn sgx_is_outside_enclave(p: *const u8, len: usize) -> i32
181181
#[inline]
182182
#[no_mangle]
183183
pub unsafe extern "C" fn sgx_commit_rts_pages(addr: usize, count: usize) -> i32 {
184-
if rts_mm_commit(addr, count << SE_PAGE_SHIFT).is_ok() {
184+
let len = count << SE_PAGE_SHIFT;
185+
match emm::check_addr(addr, len) {
186+
Ok(typ) => {
187+
if typ != RangeType::Rts {
188+
return -1;
189+
}
190+
}
191+
Err(_) => {
192+
return -1;
193+
}
194+
}
195+
196+
if mm_commit(addr, len).is_ok() {
185197
0
186198
} else {
187199
-1
@@ -191,7 +203,18 @@ pub unsafe extern "C" fn sgx_commit_rts_pages(addr: usize, count: usize) -> i32
191203
#[inline]
192204
#[no_mangle]
193205
pub unsafe extern "C" fn sgx_uncommit_rts_pages(addr: usize, count: usize) -> i32 {
194-
if rts_mm_uncommit(addr, count << SE_PAGE_SHIFT).is_ok() {
206+
let len = count << SE_PAGE_SHIFT;
207+
match emm::check_addr(addr, len) {
208+
Ok(typ) => {
209+
if typ != RangeType::Rts {
210+
return -1;
211+
}
212+
}
213+
Err(_) => {
214+
return -1;
215+
}
216+
}
217+
if mm_uncommit(addr, len).is_ok() {
195218
0
196219
} else {
197220
-1
@@ -276,7 +299,7 @@ pub unsafe extern "C" fn sgx_mm_alloc(
276299
let mut options = EmaOptions::new(addr, size, alloc_flags);
277300
options.info(info).handle(handler, priv_data);
278301

279-
match user_mm_alloc(&options) {
302+
match mm_alloc_user(&options) {
280303
Ok(base) => {
281304
*out_addr = base as *mut u8;
282305
0

sgx_trts/src/emm/alloc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use spin::{Mutex, Once};
3131

3232
use super::ema::EmaOptions;
3333
use super::page::AllocFlags;
34-
use super::range::{RangeType, RM};
34+
use super::vmmgr::{RangeType, VMMGR};
3535
use super::{PageInfo, PageType, ProtFlags};
3636
use sgx_types::error::OsResult;
3737

@@ -465,7 +465,7 @@ impl Reserve {
465465
// Here we alloc at least INIT_MEM_SIZE size,
466466
// but commit rsize memory, the remaining memory is COMMIT_ON_DEMAND
467467
let increment = self.incr_size.max(rsize);
468-
let mut range_manage = RM.get().unwrap().lock();
468+
let mut vmmgr = VMMGR.get().unwrap().lock();
469469

470470
let mut options = EmaOptions::new(None, increment + 2 * GUARD_SIZE, AllocFlags::RESERVED);
471471

@@ -475,7 +475,7 @@ impl Reserve {
475475
prot: ProtFlags::NONE,
476476
})
477477
.alloc(AllocType::new_static());
478-
let base = range_manage.alloc(&options, RangeType::User)?;
478+
let base = vmmgr.alloc(&options, RangeType::User)?;
479479

480480
let mut options = EmaOptions::new(
481481
Some(base + GUARD_SIZE),
@@ -484,10 +484,10 @@ impl Reserve {
484484
);
485485

486486
options.alloc(AllocType::new_static());
487-
let base = range_manage.alloc(&options, RangeType::User)?;
487+
let base = vmmgr.alloc(&options, RangeType::User)?;
488488

489-
range_manage.commit(base, rsize, RangeType::User)?;
490-
drop(range_manage);
489+
vmmgr.commit(base, rsize)?;
490+
drop(vmmgr);
491491

492492
unsafe {
493493
self.write_chunk(base, increment);

0 commit comments

Comments
 (0)