Skip to content

Commit

Permalink
Implement enclave memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
ClawSeven committed Jun 9, 2023
1 parent 68f29a7 commit cd03ec9
Show file tree
Hide file tree
Showing 17 changed files with 1,240 additions and 16 deletions.
4 changes: 4 additions & 0 deletions sgx_trts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ 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"
buddy_system_allocator = "0.9.0"
spin = "0.9.4"
bitflags = "1.3"
16 changes: 14 additions & 2 deletions sgx_trts/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ macro_rules! is_page_aligned {
};
}

macro_rules! round_to {
($num:expr, $align:expr) => {
($num + $align - 1) & (!($align - 1))
};
}

macro_rules! round_to_page {
($num:expr) => {
($num + crate::arch::SE_PAGE_SIZE - 1) & (!(crate::arch::SE_PAGE_SIZE - 1))
};
}

macro_rules! trim_to {
($num:expr, $align:expr) => {
$num & (!($align - 1))
};
}

macro_rules! trim_to_page {
($num:expr) => {
$num & (!(crate::arch::SE_PAGE_SIZE - 1))
Expand Down Expand Up @@ -670,8 +682,8 @@ impl From<PageType> for SecinfoFlags {
impl From<PageInfo> for SecinfoFlags {
fn from(data: PageInfo) -> SecinfoFlags {
let typ = data.typ as u64;
let flags = data.flags.bits() as u64;
SecinfoFlags::from_bits_truncate((typ << 8) | flags)
let prot = data.prot.bits() as u64;
SecinfoFlags::from_bits_truncate((typ << 8) | prot)
}
}

Expand Down
8 changes: 7 additions & 1 deletion sgx_trts/src/call/ocall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ pub enum OCallIndex {
TrimCommit,
Modpr,
Mprotect,
Alloc,
Modify,
}

impl OCallIndex {
pub fn is_builtin_index(index: i32) -> bool {
(-5..=-2).contains(&index)
(-7..=-2).contains(&index)
}

pub fn is_builtin(&self) -> bool {
Expand All @@ -62,6 +64,8 @@ impl TryFrom<i32> for OCallIndex {
-3 => Ok(OCallIndex::TrimCommit),
-4 => Ok(OCallIndex::Modpr),
-5 => Ok(OCallIndex::Mprotect),
-6 => Ok(OCallIndex::Alloc),
-7 => Ok(OCallIndex::Modify),
_ => Err(u8::try_from(256_u16).unwrap_err()),
}
}
Expand All @@ -76,6 +80,8 @@ impl From<OCallIndex> for i32 {
OCallIndex::TrimCommit => -3,
OCallIndex::Modpr => -4,
OCallIndex::Mprotect => -5,
OCallIndex::Alloc => -6,
OCallIndex::Modify => -7,
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions sgx_trts/src/edmm/epc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ impl_enum! {
}
}

// ProtFlags may have richer meaning compared to ProtFlags
// ProtFlags and AllocFlags are confused to developer
// PageInfo->flags should change to PageInfo->prot
impl_bitflags! {
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PageFlags: u8 {
pub struct ProtFlags: u8 {
const NONE = 0x00;
const R = 0x01;
const W = 0x02;
Expand All @@ -51,7 +54,13 @@ impl_bitflags! {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PageInfo {
pub typ: PageType,
pub flags: PageFlags,
pub prot: ProtFlags,
}

impl Into<u32> for PageInfo {
fn into(self) -> u32 {
(Into::<u8>::into(self.typ) as u32) << 8 | (self.prot.bits() as u32)
}
}

unsafe impl ContiguousMemory for PageInfo {}
Expand Down Expand Up @@ -106,7 +115,7 @@ impl PageRange {
pub(crate) fn modify(&self) -> SgxResult {
for page in self.iter() {
let _ = page.modpe();
if !page.info.flags.contains(PageFlags::W | PageFlags::X) {
if !page.info.prot.contains(ProtFlags::W | ProtFlags::X) {
page.accept()?;
}
}
Expand Down
12 changes: 6 additions & 6 deletions sgx_trts/src/edmm/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cfg_if! {
#[cfg(not(any(feature = "sim", feature = "hyper")))]
mod hw {
use crate::arch::{self, Layout};
use crate::edmm::epc::{PageFlags, PageInfo, PageRange, PageType};
use crate::edmm::epc::{PageInfo, PageRange, PageType, ProtFlags};
use crate::edmm::layout::LayoutTable;
use crate::edmm::perm;
use crate::edmm::trim;
Expand All @@ -47,7 +47,7 @@ mod hw {
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::R | PageFlags::W | PageFlags::PENDING,
prot: ProtFlags::R | ProtFlags::W | ProtFlags::PENDING,
},
)?;
if (attr.attr & arch::PAGE_DIR_GROW_DOWN) == 0 {
Expand All @@ -74,7 +74,7 @@ mod hw {
count,
PageInfo {
typ: PageType::Trim,
flags: PageFlags::MODIFIED,
prot: ProtFlags::MODIFIED,
},
)?;
pages.accept_forward()?;
Expand All @@ -96,7 +96,7 @@ mod hw {
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::R | PageFlags::W | PageFlags::PENDING,
prot: ProtFlags::R | ProtFlags::W | ProtFlags::PENDING,
},
)?;
pages.accept_forward()?;
Expand Down Expand Up @@ -131,7 +131,7 @@ mod hw {
count,
PageInfo {
typ: PageType::Trim,
flags: PageFlags::MODIFIED,
prot: ProtFlags::MODIFIED,
},
)?;
pages.accept_forward()?;
Expand Down Expand Up @@ -196,7 +196,7 @@ mod hw {
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::PR | PageFlags::from_bits_truncate(perm),
prot: ProtFlags::PR | ProtFlags::from_bits_truncate(perm),
},
)?;

Expand Down
2 changes: 1 addition & 1 deletion sgx_trts/src/edmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub(crate) mod tcs;
#[cfg(not(any(feature = "sim", feature = "hyper")))]
pub(crate) mod trim;

pub use epc::{PageFlags, PageInfo, PageRange, PageType};
pub use epc::{PageInfo, PageRange, PageType, ProtFlags};
pub use mem::{apply_epc_pages, trim_epc_pages};
pub use perm::{modpr_ocall, mprotect_ocall};
70 changes: 70 additions & 0 deletions sgx_trts/src/edmm/perm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ cfg_if! {
mod hw {
use crate::arch::SE_PAGE_SHIFT;
use crate::call::{ocall, OCallIndex, OcAlloc};
use crate::edmm::{PageInfo, PageType};
use crate::emm::flags::AllocFlags;
use alloc::boxed::Box;
use core::convert::Into;
use sgx_types::error::{SgxResult, SgxStatus};
Expand Down Expand Up @@ -67,6 +69,74 @@ mod hw {

ocall(OCallIndex::Mprotect, Some(change.as_mut()))
}

// In keeping with Intel SDK, here we use the name page_properties,
// but page_type: PageType is more appropriate
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct EmmAllocOcall {
retval: i32,
addr: usize,
size: usize,
page_properties: u32,
alloc_flags: u32,
}

/// FIXME: fake alloc
pub fn alloc_ocall(
addr: usize,
length: usize,
page_type: PageType,
alloc_flags: AllocFlags,
) -> SgxResult {
let mut change = Box::try_new_in(
EmmAllocOcall {
retval: 0, // not sure
addr,
size: length,
page_properties: Into::<u8>::into(page_type) as u32,
alloc_flags: alloc_flags.bits(),
},
OcAlloc,
)
.map_err(|_| SgxStatus::OutOfMemory)?;

ocall(OCallIndex::Alloc, Some(change.as_mut()))
}

// In keeping with Intel SDK, here we use the name flags_from (si_flags),
// but we rename si_flags to page_info, here info_from: PageInfo is more appropriate
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct EmmModifyOcall {
retval: i32,
addr: usize,
size: usize,
flags_from: u32,
flags_to: u32,
}

/// FIXME: fake modify
pub fn modify_ocall(
addr: usize,
length: usize,
info_from: PageInfo,
info_to: PageInfo,
) -> SgxResult {
let mut change = Box::try_new_in(
EmmModifyOcall {
retval: 0,
addr,
size: length,
flags_from: Into::<u32>::into(info_from),
flags_to: Into::<u32>::into(info_to),
},
OcAlloc,
)
.map_err(|_| SgxStatus::OutOfMemory)?;

ocall(OCallIndex::Modify, Some(change.as_mut()))
}
}

#[cfg(any(feature = "sim", feature = "hyper"))]
Expand Down
6 changes: 3 additions & 3 deletions sgx_trts/src/edmm/tcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn mktcs(mk_tcs: NonNull<MkTcs>) -> SgxResult {
#[cfg(not(any(feature = "sim", feature = "hyper")))]
mod hw {
use crate::arch::{self, Layout, Tcs};
use crate::edmm::epc::{Page, PageFlags, PageInfo, PageType};
use crate::edmm::epc::{Page, PageInfo, PageType, ProtFlags};
use crate::enclave::MmLayout;
use crate::tcs::list;
use core::ptr;
Expand Down Expand Up @@ -123,7 +123,7 @@ mod hw {
tcs.as_ptr() as usize,
PageInfo {
typ: PageType::Tcs,
flags: PageFlags::MODIFIED,
prot: ProtFlags::MODIFIED,
},
)?;
page.accept()?;
Expand Down Expand Up @@ -175,7 +175,7 @@ mod hw {
tcs.as_ptr() as usize,
PageInfo {
typ: PageType::Trim,
flags: PageFlags::MODIFIED,
prot: ProtFlags::MODIFIED,
},
)?;
page.accept()?;
Expand Down
31 changes: 31 additions & 0 deletions sgx_trts/src/emm/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::alloc::{AllocError, Allocator, Layout};
use core::ptr::NonNull;

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

unsafe impl Allocator for ResAlloc {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
todo!()
}

#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
todo!()
}
}

#[derive(Clone)]
pub struct StaticAlloc;

unsafe impl Allocator for StaticAlloc {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
todo!()
}

#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
todo!()
}
}
Loading

0 comments on commit cd03ec9

Please sign in to comment.