use sgx_types::*;
use sgx_types::metadata::*;
pub const LAYOUT_ENTRY_NUM : usize = 42;
#[link(name = "sgx_trts")]
extern {
static g_global_data: global_data_t;
pub fn get_thread_data() -> * const c_void;
pub fn get_enclave_base() -> * const c_void;
pub fn get_heap_base() -> * const c_void;
pub fn get_heap_size() -> size_t;
}
#[repr(C)]
struct global_data_t {
enclave_size: usize,
heap_offset: usize,
heap_size: usize,
rsrv_offset: usize,
rsrv_size: usize,
thread_policy: usize,
td_template : thread_data_t,
tcs_template: [u8;TCS_TEMPLATE_SIZE], layout_entry_num : u32,
reserved : u32,
layout_table : [layout_t;LAYOUT_ENTRY_NUM],
}
#[repr(C)]
struct thread_data_t {
self_addr: usize,
last_sp: usize,
stack_base_addr: usize,
stack_limit_addr: usize,
first_ssa_gpr: usize,
stack_guard: usize,
flags: usize,
xsave_size: usize,
last_error: usize,
m_next: usize,
tls_addr: usize,
tls_array: usize,
exception_flag: usize,
cxx_thread_info: [usize; 6],
stack_commit_addr: usize,
}
#[derive(Copy, Clone)]
pub struct SgxGlobalData {
enclave_base: usize,
enclave_size: usize,
heap_base: usize,
heap_offset: usize,
heap_size: usize,
thread_policy: SgxThreadPolicy,
}
impl Default for SgxGlobalData {
fn default() -> Self {
Self::new()
}
}
impl SgxGlobalData {
pub fn new() -> Self {
SgxGlobalData {
enclave_base: rsgx_get_enclave_base() as usize,
enclave_size: rsgx_get_enclave_size(),
heap_base: rsgx_get_heap_base() as usize,
heap_offset: rsgx_get_heap_offset(),
heap_size: rsgx_get_heap_size(),
thread_policy: rsgx_get_thread_policy(),
}
}
pub fn enclave_base(&self) -> usize {
self.enclave_base
}
pub fn enclave_size(&self) -> usize {
self.enclave_size
}
pub fn heap_base(&self) -> usize {
self.heap_base
}
pub fn heap_offset(&self) -> usize {
self.heap_offset
}
pub fn heap_size(&self) -> usize {
self.heap_size
}
pub fn thread_policy(&self) -> SgxThreadPolicy {
self.thread_policy
}
}
#[allow(dead_code)]
#[derive(Copy, Clone, Default)]
pub struct SgxThreadData {
td_addr: usize,
last_sp: usize,
stack_base_addr: usize,
stack_limit_addr: usize,
first_ssa_gpr: usize,
stack_guard: usize,
xsave_size: usize,
last_error: usize,
tls_addr: usize,
tls_array: usize,
exception_flag: usize,
cxx_thread_info: [usize; 6],
}
impl SgxThreadData {
#[allow(clippy::cast_ptr_alignment)]
pub fn new() -> Self {
let td = unsafe {
let p = rsgx_get_thread_data() as * const thread_data_t;
&*p
};
SgxThreadData {
td_addr: td.self_addr,
last_sp: td.last_sp,
stack_base_addr: td.stack_base_addr,
stack_limit_addr: td.stack_limit_addr,
first_ssa_gpr: td.first_ssa_gpr,
stack_guard: td.stack_guard,
xsave_size: td.xsave_size,
last_error: td.last_error,
tls_addr: td.tls_addr,
tls_array: td.tls_array,
exception_flag: td.exception_flag,
cxx_thread_info: td.cxx_thread_info,
}
}
pub unsafe fn from_raw(raw: usize) -> Self {
let p = raw as * const thread_data_t;
let td = &*p;
SgxThreadData {
td_addr: td.self_addr,
last_sp: td.last_sp,
stack_base_addr: td.stack_base_addr,
stack_limit_addr: td.stack_limit_addr,
first_ssa_gpr: td.first_ssa_gpr,
stack_guard: td.stack_guard,
xsave_size: td.xsave_size,
last_error: td.last_error,
tls_addr: td.tls_addr,
tls_array: td.tls_array,
exception_flag: td.exception_flag,
cxx_thread_info: td.cxx_thread_info,
}
}
pub fn td_base(&self) -> usize {
self.td_addr
}
pub fn stack_base(&self) -> usize {
self.stack_base_addr
}
pub fn stack_limit(&self) -> usize {
self.stack_limit_addr
}
pub fn tls_base(&self) -> usize {
self.tls_addr
}
pub fn last_error(&self) -> usize {
self.last_error
}
pub fn exception_flag(&self) -> usize {
self.exception_flag
}
pub fn get_tcs(&self) -> usize {
self.stack_base() + STATIC_STACK_SIZE + SE_GUARD_PAGE_SIZE
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SgxThreadPolicy {
Bound,
Unbound
}
#[inline]
pub fn rsgx_get_thread_data() -> * const u8 {
unsafe { get_thread_data() as * const u8 }
}
#[inline]
pub fn rsgx_get_enclave_base() -> * const u8 {
unsafe { get_enclave_base() as * const u8 }
}
#[inline]
pub fn rsgx_get_enclave_size() -> usize {
unsafe{ g_global_data.enclave_size }
}
#[inline]
pub fn rsgx_get_heap_base() -> * const u8 {
unsafe { get_heap_base() as * const u8 }
}
#[inline]
pub fn rsgx_get_heap_offset() -> usize {
unsafe{ g_global_data.heap_offset }
}
#[inline]
pub fn rsgx_get_heap_size() -> usize {
unsafe { get_heap_size() }
}
#[inline]
pub fn rsgx_get_thread_policy() -> SgxThreadPolicy {
unsafe {
if g_global_data.thread_policy != 0 {
SgxThreadPolicy::Unbound
} else {
SgxThreadPolicy::Bound
}
}
}