Skip to content

Commit

Permalink
Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
ClawSeven committed Oct 10, 2023
1 parent aa1fc66 commit 4ba6592
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 339 deletions.
26 changes: 13 additions & 13 deletions sgx_trts/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#![allow(clippy::enum_variant_names)]

use crate::emm::{PageInfo, PageType};
use crate::emm::{self, PageType};
use crate::tcs::tc;
use crate::version::*;
use crate::xsave;
Expand Down Expand Up @@ -744,11 +744,11 @@ impl From<PageType> for SecInfoFlags {
}
}

impl From<edmm::PageInfo> for SecInfoFlags {
fn from(data: edmm::PageInfo) -> SecInfoFlags {
impl From<emm::PageInfo> for SecInfoFlags {
fn from(data: emm::PageInfo) -> SecInfoFlags {
let typ = data.typ as u64;
let prot = data.prot.bits() as u64;
SecinfoFlags::from_bits_truncate((typ << 8) | prot)
SecInfoFlags::from_bits_truncate((typ << 8) | prot)
}
}

Expand Down Expand Up @@ -807,33 +807,33 @@ impl From<SecInfoFlags> for SecInfo {
}
}

impl From<edmm::PageInfo> for SecInfo {
fn from(data: edmm::PageInfo) -> SecInfo {
impl From<emm::PageInfo> for SecInfo {
fn from(data: emm::PageInfo) -> SecInfo {
SecInfo::from(SecInfoFlags::from(data))
}
}

#[repr(C, align(32))]
#[derive(Clone, Copy, Debug)]
pub struct PageInfo {
pub struct CPageInfo {
pub linaddr: u64,
pub srcpage: u64,
pub secinfo: u64,
pub secs: u64,
}

impl PageInfo {
pub const ALIGN_SIZE: usize = mem::size_of::<PageInfo>();
impl CPageInfo {
pub const ALIGN_SIZE: usize = mem::size_of::<CPageInfo>();
}

impl AsRef<[u8; PageInfo::ALIGN_SIZE]> for PageInfo {
fn as_ref(&self) -> &[u8; PageInfo::ALIGN_SIZE] {
impl AsRef<[u8; CPageInfo::ALIGN_SIZE]> for CPageInfo {
fn as_ref(&self) -> &[u8; CPageInfo::ALIGN_SIZE] {
unsafe { &*(self as *const _ as *const _) }
}
}

impl AsRef<Align32<[u8; PageInfo::ALIGN_SIZE]>> for PageInfo {
fn as_ref(&self) -> &Align32<[u8; PageInfo::ALIGN_SIZE]> {
impl AsRef<Align32<[u8; CPageInfo::ALIGN_SIZE]>> for CPageInfo {
fn as_ref(&self) -> &Align32<[u8; CPageInfo::ALIGN_SIZE]> {
unsafe { &*(self as *const _ as *const _) }
}
}
6 changes: 3 additions & 3 deletions sgx_trts/src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::emm::ema::EmaOptions;
use crate::emm::page::AllocFlags;
use crate::emm::pfhandler::PfHandler;
use crate::emm::vmmgr::{
ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT, PAGE_TYPE_MASK,
PAGE_TYPE_SHIFT, RangeType,
RangeType, ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT,
PAGE_TYPE_MASK, PAGE_TYPE_SHIFT,
};
use crate::emm::{mm_commit, mm_uncommit, mm_alloc_user, PageInfo, PageType, ProtFlags, self};
use crate::emm::{self, mm_alloc_user, mm_commit, mm_uncommit, PageInfo, PageType, ProtFlags};
use crate::enclave::{self, is_within_enclave, MmLayout};
use crate::error;
use crate::rand::rand;
Expand Down
8 changes: 6 additions & 2 deletions sgx_trts/src/emm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const SIZE_MASK: usize = !(EXACT_MATCH_INCREMENT - 1);
static mut STATIC_MEM: [u8; STATIC_MEM_SIZE] = [0; STATIC_MEM_SIZE];

/// Lowest level: Allocator for static memory
///
/// TODO: reimplement static allocator with monotone increasing policies
static STATIC: Once<LockedHeap<32>> = Once::new();

/// Second level: Allocator for reserve memory
Expand Down Expand Up @@ -228,7 +230,9 @@ impl BlockUsed {

intrusive_adapter!(BlockFreeAda = UnsafeRef<BlockFree>: BlockFree { link: Link });

/// Interior allocator for reserve memory management,
/// Interior allocator for reserve memory management
///
/// TODO: implement slab allocator mechanism
pub struct Reserve {
exact_blocks: [SinglyLinkedList<BlockFreeAda>; 256],
large_blocks: SinglyLinkedList<BlockFreeAda>,
Expand Down Expand Up @@ -391,7 +395,7 @@ impl Reserve {
return Ok(self.block_to_payload(block));
};

// AllocType new block from chunks
// Alloc new block from chunks
block = self.alloc_from_chunks(bsize);
if block.is_none() {
let chunk_size = size_of::<Chunk>();
Expand Down
49 changes: 28 additions & 21 deletions sgx_trts/src/emm/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ use sgx_types::error::OsResult;

use super::alloc::AllocType;

const BYTE_SIZE: usize = 8;
macro_rules! bytes_num {
($num:expr) => {
($num + BYTE_SIZE - 1) / BYTE_SIZE
};
}

#[repr(C)]
#[derive(Debug)]
pub struct BitArray {
Expand All @@ -37,7 +44,7 @@ pub struct BitArray {
impl BitArray {
/// Init BitArray with all zero bits
pub fn new(bits: usize, alloc: AllocType) -> OsResult<Self> {
let bytes = (bits + 7) / 8;
let bytes = bytes_num!(bits);

// FIXME: return error if OOM
let data = match alloc {
Expand Down Expand Up @@ -66,8 +73,8 @@ impl BitArray {
return Err(EACCES);
}

let byte_index = index / 8;
let bit_index = index % 8;
let byte_index = index / BYTE_SIZE;
let bit_index = index % BYTE_SIZE;
let bit_mask = 1 << bit_index;
let data = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };
Ok((data.get(byte_index).unwrap() & bit_mask) != 0)
Expand All @@ -88,8 +95,8 @@ impl BitArray {
if index >= self.bits {
return Err(EACCES);
}
let byte_index = index / 8;
let bit_index = index % 8;
let byte_index = index / BYTE_SIZE;
let bit_index = index % BYTE_SIZE;
let bit_mask = 1 << bit_index;

let data = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };
Expand Down Expand Up @@ -120,33 +127,33 @@ impl BitArray {
pub fn split(&mut self, pos: usize) -> OsResult<BitArray> {
assert!(pos > 0 && pos < self.bits);

let byte_index = pos / 8;
let bit_index = pos % 8;
let byte_index = pos / BYTE_SIZE;
let bit_index = pos % BYTE_SIZE;

let l_bits = pos;
let l_bytes = (l_bits + 7) / 8;
let lbits = pos;
let lbytes = bytes_num!(lbits);

let r_bits = self.bits - l_bits;
let r_bytes = (r_bits + 7) / 8;
let rbits = self.bits - lbits;
let rbytes = bytes_num!(rbits);

let r_array = Self::new(r_bits, self.alloc)?;
let rarray = Self::new(rbits, self.alloc)?;

let r_data = unsafe { core::slice::from_raw_parts_mut(r_array.data, r_array.bytes) };
let l_data = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };
let rdata = unsafe { core::slice::from_raw_parts_mut(rarray.data, rarray.bytes) };
let ldata = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };

for (idx, item) in r_data[..(r_bytes - 1)].iter_mut().enumerate() {
for (idx, item) in rdata[..(rbytes - 1)].iter_mut().enumerate() {
// current byte index in previous bit_array
let curr_idx = idx + byte_index;
let low_bits = l_data[curr_idx] >> bit_index;
let high_bits = l_data[curr_idx + 1] << (8 - bit_index);
let low_bits = ldata[curr_idx] >> bit_index;
let high_bits = ldata[curr_idx + 1] << (8 - bit_index);
*item = high_bits | low_bits;
}
r_data[r_bytes - 1] = l_data[self.bytes - 1] >> bit_index;
rdata[rbytes - 1] = ldata[self.bytes - 1] >> bit_index;

self.bits = l_bits;
self.bytes = l_bytes;
self.bits = lbits;
self.bytes = lbytes;

Ok(r_array)
Ok(rarray)
}
}

Expand Down
Loading

0 comments on commit 4ba6592

Please sign in to comment.