Skip to content

Commit

Permalink
Implement interior memory range management
Browse files Browse the repository at this point in the history
  • Loading branch information
ClawSeven committed Jul 2, 2023
1 parent cd03ec9 commit 126f917
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 104 deletions.
3 changes: 2 additions & 1 deletion sgx_trts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ hyper = ["sgx_types/hyper"]
sgx_types = { path = "../sgx_types" }
sgx_crypto_sys = { path = "../sgx_crypto/sgx_crypto_sys" }
sgx_tlibc_sys = { path = "../sgx_libc/sgx_tlibc_sys" }
intrusive-collections = "0.9.5"

intrusive-collections = { git = "https://github.com/ClawSeven/intrusive-rs.git", rev = "3db5618" }
buddy_system_allocator = "0.9.0"
spin = "0.9.4"
bitflags = "1.3"
7 changes: 5 additions & 2 deletions sgx_trts/src/edmm/epc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ impl_enum! {
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PageType {
Secs = 0,
// Secs = 0,
None = 0,
Tcs = 1,
Reg = 2,
Va = 3,
// Va = 3,
Trim = 4,
Frist = 5,
Rest = 6,
}
}

Expand Down
4 changes: 2 additions & 2 deletions sgx_trts/src/emm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::alloc::{AllocError, Allocator, Layout};
use core::ptr::NonNull;

/// alloc layout memory from Reserve region
#[derive(Clone)]
#[derive(Clone, Copy)]
pub struct ResAlloc;

unsafe impl Allocator for ResAlloc {
Expand All @@ -16,7 +16,7 @@ unsafe impl Allocator for ResAlloc {
}
}

#[derive(Clone)]
#[derive(Clone, Copy)]
pub struct StaticAlloc;

unsafe impl Allocator for StaticAlloc {
Expand Down
8 changes: 3 additions & 5 deletions sgx_trts/src/emm/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ impl<A: Allocator + Clone> BitArray<A> {
break;
}
}
true_range.push((start,end));
true_range.push((start, end));
}

return true_range;
return true_range;
}

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



// FIXME: add more unit test
// FIXME: add more unit test
109 changes: 74 additions & 35 deletions sgx_trts/src/emm/ema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ use crate::trts::Version;
use crate::veh::{ExceptionHandler, ExceptionInfo};

use super::alloc::ResAlloc;
use super::alloc::StaticAlloc;
use super::bitmap::BitArray;
use super::flags::AllocFlags;

// pub struct Box<T, A = Global>(_, _)
// where
// A: Allocator,
// T: ?Sized;

#[repr(C)]
#[derive(Clone)]
pub struct EMA<A>
Expand Down Expand Up @@ -81,6 +77,7 @@ where
) -> SgxResult<Self> {
// check flags' eligibility
AllocFlags::try_from(alloc_flags.bits())?;

if start != 0
&& length != 0
&& is_within_enclave(start as *const u8, length)
Expand All @@ -103,38 +100,33 @@ where
}
}

// Returns a newly allocated ema in charging of the memory in the range [addr, len).
// After the call, the original ema will be left containing the elements [0, addr)
// Returns a newly allocated ema in charging of the memory in the range [addr, len).
// After the call, the original ema will be left containing the elements [0, addr)
// with its previous capacity unchanged.
pub fn split(&mut self, addr: usize) -> SgxResult<Box<EMA<A>,A>> {
pub fn split(&mut self, addr: usize) -> SgxResult<Box<EMA<A>, A>> {
let l_start = self.start;
let l_length = addr - l_start;

let r_start = addr;
let r_length = (self.start + self.length) - addr;

let new_bitarray = match &mut self.eaccept_map{
let new_bitarray = match &mut self.eaccept_map {
Some(bitarray) => {
let pos = (addr - self.start) >> crate::arch::SE_PAGE_SHIFT;
// split self.eaccept_map
Some(bitarray.split(pos)?)
}
None => {
None
}
None => None,
};

// 这里之后可以优化
// 1. self.clone() 会把原有的bitmap重新alloc并复制一份,但其实clone之后这里是None即可
// 2. 使用Box::new_in 会把 self.clone() 这部分在栈上的数据再拷贝一份到Box新申请的内存区域
let mut new_ema: Box<EMA<A>,A> = Box::new_in(
self.clone(),
self.alloc.clone()
);
let mut new_ema: Box<EMA<A>, A> = Box::new_in(self.clone(), self.alloc.clone());

self.start = l_start;
self.length = l_length;

new_ema.start = r_start;
new_ema.length = r_length;
new_ema.eaccept_map = new_bitarray;
Expand All @@ -145,7 +137,11 @@ where
// If the previous ema is divided into three parts -> (left ema, middle ema, right ema), return (middle ema, right ema).
// If the previous ema is divided into two parts -> (left ema, right ema)
// end split: return (None, right ema), start split: return (left ema, None)
fn split_into_three(&mut self, start: usize, length: usize) -> SgxResult<(Option<Box<EMA<A>,A>>, Option<Box<EMA<A>,A>>)> {
fn split_into_three(
&mut self,
start: usize,
length: usize,
) -> SgxResult<(Option<Box<EMA<A>, A>>, Option<Box<EMA<A>, A>>)> {
if start > self.start {
let mut new_ema = self.split(start)?;
if new_ema.start + new_ema.length > start + length {
Expand Down Expand Up @@ -224,6 +220,28 @@ where
}
}

// Attension, return EACCES SgxStatus may be more appropriate
pub fn commit_check(&self) -> SgxResult {
if self.info.prot.intersects(ProtFlags::R | ProtFlags::W) {
return Err(SgxStatus::InvalidParameter);
}

if self.info.typ != PageType::Reg {
return Err(SgxStatus::InvalidParameter);
}

if self.alloc_flags.contains(AllocFlags::RESERVED) {
return Err(SgxStatus::InvalidParameter);
}

Ok(())
}

/// commit all the memory in this ema
pub fn commit_self(&mut self) -> SgxResult {
self.commit(self.start, self.length)
}

/// ema_do_commit
pub fn commit(&mut self, start: usize, length: usize) -> SgxResult {
ensure!(
Expand Down Expand Up @@ -260,8 +278,10 @@ where
/// uncommit EPC page
pub fn uncommit(&mut self, start: usize, length: usize, prot: ProtFlags) -> SgxResult {
// need READ for trimming
ensure!(self.info.prot != ProtFlags::NONE && self.eaccept_map.is_some(),
SgxStatus::InvalidParameter);
ensure!(
self.info.prot != ProtFlags::NONE && self.eaccept_map.is_some(),
SgxStatus::InvalidParameter
);

if self.alloc_flags.contains(AllocFlags::RESERVED) {
return Ok(());
Expand Down Expand Up @@ -303,21 +323,23 @@ where
}

let block_length = block_end - block_start;
perm::modify_ocall(block_start, block_length,
PageInfo {
perm::modify_ocall(
block_start,
block_length,
PageInfo {
typ: self.info.typ,
prot,
},
PageInfo {
PageInfo {
typ: PageType::Trim,
prot,
},
)?;

let pages = PageRange::new(
block_start,
block_length / crate::arch::SE_PAGE_SIZE,
trim_info
block_start,
block_length / crate::arch::SE_PAGE_SIZE,
trim_info,
)?;

let init_idx = (block_start - self.start) >> crate::arch::SE_PAGE_SHIFT;
Expand All @@ -328,12 +350,14 @@ where
}

// eaccept trim notify
perm::modify_ocall(block_start, block_length,
PageInfo {
perm::modify_ocall(
block_start,
block_length,
PageInfo {
typ: PageType::Trim,
prot,
},
PageInfo {
PageInfo {
typ: PageType::Trim,
prot,
},
Expand Down Expand Up @@ -401,7 +425,7 @@ where
)?;
}

Ok(())
Ok(())
}

pub fn dealloc(&mut self) -> SgxResult {
Expand All @@ -421,10 +445,26 @@ where
round_to!(curr_end, align)
}

pub fn end(&self) -> usize {
self.start + self.length
}

pub fn start(&self) -> usize {
self.start
}

pub fn len(&self) -> usize {
self.length
}

pub fn lower_than_addr(&self, addr: usize) -> bool {
self.end() <= addr
}

pub fn higher_than_addr(&self, addr: usize) -> bool {
self.start >= addr
}

// get and set attributes
pub fn set_flags(flags: AllocFlags) -> SgxResult<()> {
todo!()
Expand All @@ -443,12 +483,11 @@ where
}
}

//
//
// intrusive_adapter!(pub RegEmaAda = Box<EMA<ResAlloc>, ResAlloc>: EMA<ResAlloc> { link: LinkedListLink });

// regular ema adapter
intrusive_adapter!(pub RegEmaAda = Box<EMA<ResAlloc>>: EMA<ResAlloc> { link: LinkedListLink });
intrusive_adapter!(pub RegEmaAda = ResAlloc, Box<EMA<ResAlloc>, ResAlloc>: EMA<ResAlloc> { link: LinkedListLink });

// reserve ema adapter
intrusive_adapter!(pub ResEmaAda = Box<EMA<ResAlloc>>: EMA<ResAlloc> { link: LinkedListLink });

intrusive_adapter!(pub ResEmaAda = StaticAlloc, Box<EMA<StaticAlloc>, StaticAlloc>: EMA<StaticAlloc> { link: LinkedListLink });
36 changes: 21 additions & 15 deletions sgx_trts/src/emm/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,33 @@ use sgx_types::error::{SgxResult, SgxStatus};
bitflags! {
// 用bitflags的话,在ema输入的时候可能存在RESERVED & COMMIT_NOW 需要check一下
pub struct AllocFlags: u32 {
const RESERVED = 0b0000_0001;
const COMMIT_NOW = 0b0000_0010;
const COMMIT_ON_DEMAND = 0b0000_0100;
const SYSTEM = 0b0001_0000;
const GROWSDOWN = 0x0010_0000;
const GROWSUP = 0x0100_0000;
const RESERVED = 0b0001;
const COMMIT_NOW = 0b0010;
const COMMIT_ON_DEMAND = 0b0100;
const GROWSDOWN = 0b00010000;
const GROWSUP = 0b00100000;
const FIXED = 0b01000000;
}
}

impl AllocFlags {
pub fn try_from(value: u32) -> SgxResult<Self> {
match value {
0b0001_0001 => Ok(Self::RESERVED | Self::SYSTEM),
0b0010_0001 => Ok(Self::RESERVED | Self::GROWSDOWN),
0b0100_0001 => Ok(Self::RESERVED | Self::COMMIT_ON_DEMAND),
0b0001_0010 => Ok(Self::COMMIT_NOW | Self::SYSTEM),
0b0010_0010 => Ok(Self::COMMIT_NOW | Self::GROWSDOWN),
0b0100_0010 => Ok(Self::COMMIT_NOW | Self::COMMIT_ON_DEMAND),
0b0001_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::SYSTEM),
0b0010_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSDOWN),
0b0100_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::COMMIT_ON_DEMAND),
0b0000_0001 => Ok(Self::RESERVED),
0b0000_0010 => Ok(Self::COMMIT_NOW),
0b0000_0100 => Ok(Self::COMMIT_ON_DEMAND),
0b0001_0000 => Ok(Self::GROWSDOWN),
0b0010_0000 => Ok(Self::GROWSUP),
0b0100_0000 => Ok(Self::FIXED),
0b0001_0001 => Ok(Self::RESERVED | Self::GROWSDOWN),
0b0010_0001 => Ok(Self::RESERVED | Self::GROWSUP),
0b0100_0001 => Ok(Self::RESERVED | Self::FIXED),
0b0001_0010 => Ok(Self::COMMIT_NOW | Self::GROWSDOWN),
0b0010_0010 => Ok(Self::COMMIT_NOW | Self::GROWSUP),
0b0100_0010 => Ok(Self::COMMIT_NOW | Self::FIXED),
0b0001_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSDOWN),
0b0010_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::GROWSUP),
0b0100_0100 => Ok(Self::COMMIT_ON_DEMAND | Self::FIXED),
_ => Err(SgxStatus::InvalidParameter),
}
}
Expand Down
Loading

0 comments on commit 126f917

Please sign in to comment.