Skip to content

Commit

Permalink
Update fortanix-sgx-abi and export some useful SGX usercall traits
Browse files Browse the repository at this point in the history
Update fortanix-sgx-abi to 0.5.0 to add support for cancel queue (see
fortanix/rust-sgx#405 and
fortanix/rust-sgx#404).

Export some useful traits for processing SGX usercall. This is needed
for fortanix/rust-sgx#404 to avoid duplication.
  • Loading branch information
mzohreva committed Aug 16, 2022
1 parent 803d162 commit 996138c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rand = "0.7"
dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }

[target.x86_64-fortanix-unknown-sgx.dependencies]
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'] }

[target.'cfg(target_os = "hermit")'.dependencies]
hermit-abi = { version = "0.2.0", features = ['rustc-dep-of-std'] }
Expand Down
1 change: 1 addition & 0 deletions std/src/os/fortanix_sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod usercalls {
free, insecure_time, launch_thread, read, read_alloc, send, wait, write,
};
pub use crate::sys::abi::usercalls::raw::{do_usercall, Usercalls as UsercallNrs};
pub use crate::sys::abi::usercalls::raw::{Register, RegisterArgument, ReturnValue};

// fortanix-sgx-abi re-exports
pub use crate::sys::abi::usercalls::raw::Error;
Expand Down
2 changes: 2 additions & 0 deletions std/src/sys/sgx/abi/usercalls/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ unsafe impl UserSafeSized for Usercall {}
#[unstable(feature = "sgx_platform", issue = "56975")]
unsafe impl UserSafeSized for Return {}
#[unstable(feature = "sgx_platform", issue = "56975")]
unsafe impl UserSafeSized for Cancel {}
#[unstable(feature = "sgx_platform", issue = "56975")]
unsafe impl<T: UserSafeSized> UserSafeSized for [T; 2] {}

/// A type that can be represented in memory as one or more `UserSafeSized`s.
Expand Down
8 changes: 7 additions & 1 deletion std/src/sys/sgx/abi/usercalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,17 @@ fn check_os_error(err: Result) -> i32 {
}
}

trait FromSgxResult {
/// Translate the raw result of an SGX usercall.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub trait FromSgxResult {
/// Return type
type Return;

/// Translate the raw result of an SGX usercall.
fn from_sgx_result(self) -> IoResult<Self::Return>;
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl<T> FromSgxResult for (Result, T) {
type Return = T;

Expand All @@ -310,6 +315,7 @@ impl<T> FromSgxResult for (Result, T) {
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl FromSgxResult for Result {
type Return = ();

Expand Down
24 changes: 21 additions & 3 deletions std/src/sys/sgx/abi/usercalls/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ pub unsafe fn do_usercall(
(a, b)
}

type Register = u64;
/// A value passed or returned in a CPU register.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub type Register = u64;

trait RegisterArgument {
/// Translate a type from/to Register to be used as an argument.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub trait RegisterArgument {
/// Translate a Register to Self.
fn from_register(_: Register) -> Self;
/// Translate self to a Register.
fn into_register(self) -> Register;
}

trait ReturnValue {
/// Translate a pair of Registers to the raw usercall return value.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub trait ReturnValue {
/// Translate a pair of Registers to the raw usercall return value.
fn from_registers(call: &'static str, regs: (Register, Register)) -> Self;
}

Expand All @@ -68,6 +77,7 @@ macro_rules! define_usercalls {

macro_rules! define_ra {
(< $i:ident > $t:ty) => {
#[unstable(feature = "sgx_platform", issue = "56975")]
impl<$i> RegisterArgument for $t {
fn from_register(a: Register) -> Self {
a as _
Expand All @@ -78,6 +88,7 @@ macro_rules! define_ra {
}
};
($i:ty as $t:ty) => {
#[unstable(feature = "sgx_platform", issue = "56975")]
impl RegisterArgument for $t {
fn from_register(a: Register) -> Self {
a as $i as _
Expand All @@ -88,6 +99,7 @@ macro_rules! define_ra {
}
};
($t:ty) => {
#[unstable(feature = "sgx_platform", issue = "56975")]
impl RegisterArgument for $t {
fn from_register(a: Register) -> Self {
a as _
Expand All @@ -112,6 +124,7 @@ define_ra!(usize as isize);
define_ra!(<T> *const T);
define_ra!(<T> *mut T);

#[unstable(feature = "sgx_platform", issue = "56975")]
impl RegisterArgument for bool {
fn from_register(a: Register) -> bool {
if a != 0 { true } else { false }
Expand All @@ -121,6 +134,7 @@ impl RegisterArgument for bool {
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl<T: RegisterArgument> RegisterArgument for Option<NonNull<T>> {
fn from_register(a: Register) -> Option<NonNull<T>> {
NonNull::new(a as _)
Expand All @@ -130,12 +144,14 @@ impl<T: RegisterArgument> RegisterArgument for Option<NonNull<T>> {
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl ReturnValue for ! {
fn from_registers(call: &'static str, _regs: (Register, Register)) -> Self {
rtabort!("Usercall {call}: did not expect to be re-entered");
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl ReturnValue for () {
fn from_registers(call: &'static str, usercall_retval: (Register, Register)) -> Self {
rtassert!(usercall_retval.0 == 0);
Expand All @@ -144,13 +160,15 @@ impl ReturnValue for () {
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl<T: RegisterArgument> ReturnValue for T {
fn from_registers(call: &'static str, usercall_retval: (Register, Register)) -> Self {
rtassert!(usercall_retval.1 == 0);
T::from_register(usercall_retval.0)
}
}

#[unstable(feature = "sgx_platform", issue = "56975")]
impl<T: RegisterArgument, U: RegisterArgument> ReturnValue for (T, U) {
fn from_registers(_call: &'static str, regs: (Register, Register)) -> Self {
(T::from_register(regs.0), U::from_register(regs.1))
Expand Down

0 comments on commit 996138c

Please sign in to comment.