Skip to content

Commit d7f36ca

Browse files
committed
Using new insecure_time ABI interface and avoiding usercalls when possible
1 parent b19329a commit d7f36ca

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

library/Cargo.lock

+13-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ dependencies = [
9292

9393
[[package]]
9494
name = "fortanix-sgx-abi"
95-
version = "0.5.0"
96-
source = "registry+https://github.com/rust-lang/crates.io-index"
97-
checksum = "57cafc2274c10fab234f176b25903ce17e690fca7597090d50880e047a0389c5"
95+
version = "0.6.0"
96+
source = "git+https://github.com/fortanix/rust-sgx.git?branch=raoul%2Frte-204-enable_rdtscp_in_enclaves_by_default#e4cffce87c6e5ebd1a3cee5233140d3e034c1a2a"
9897
dependencies = [
9998
"compiler_builtins",
10099
"rustc-std-workspace-core",
@@ -156,6 +155,16 @@ dependencies = [
156155
"rustc-std-workspace-core",
157156
]
158157

158+
[[package]]
159+
name = "insecure-time"
160+
version = "0.1.0"
161+
source = "git+https://github.com/fortanix/rust-sgx.git?branch=raoul%2Frte-204-enable_rdtscp_in_enclaves_by_default#e4cffce87c6e5ebd1a3cee5233140d3e034c1a2a"
162+
dependencies = [
163+
"compiler_builtins",
164+
"rustc-std-workspace-alloc",
165+
"rustc-std-workspace-core",
166+
]
167+
159168
[[package]]
160169
name = "libc"
161170
version = "0.2.162"
@@ -334,6 +343,7 @@ dependencies = [
334343
"fortanix-sgx-abi",
335344
"hashbrown",
336345
"hermit-abi",
346+
"insecure-time",
337347
"libc",
338348
"miniz_oxide",
339349
"object",

library/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ rustc-demangle.debug = 0
4545
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }
4646
rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' }
4747
rustc-std-workspace-std = { path = 'rustc-std-workspace-std' }
48+
fortanix-sgx-abi = { git = "https://github.com/fortanix/rust-sgx.git", branch = "raoul/rte-204-enable_rdtscp_in_enclaves_by_default" }

library/std/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ rand_xorshift = "0.3.0"
6767
dlmalloc = { version = "0.2.4", features = ['rustc-dep-of-std'] }
6868

6969
[target.x86_64-fortanix-unknown-sgx.dependencies]
70-
fortanix-sgx-abi = { version = "0.5.0", features = [
70+
fortanix-sgx-abi = { version = "0.6.0", features = [
7171
'rustc-dep-of-std',
7272
], public = true }
73+
insecure-time = { version = "0.1", git = "https://github.com/fortanix/rust-sgx.git", branch = "raoul/rte-204-enable_rdtscp_in_enclaves_by_default", default-features = false, features = ["rustc-dep-of-std"] }
7374

7475
[target.'cfg(target_os = "hermit")'.dependencies]
7576
hermit-abi = { version = "0.4.0", features = [

library/std/src/sys/pal/sgx/abi/usercalls/alloc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ unsafe impl UserSafeSized for Return {}
5959
unsafe impl UserSafeSized for Cancel {}
6060
#[unstable(feature = "sgx_platform", issue = "56975")]
6161
unsafe impl<T: UserSafeSized> UserSafeSized for [T; 2] {}
62+
#[unstable(feature = "sgx_platform", issue = "56975")]
63+
unsafe impl UserSafeSized for InsecureTimeInfo {}
6264

6365
/// A type that can be represented in memory as one or more `UserSafeSized`s.
6466
#[unstable(feature = "sgx_platform", issue = "56975")]

library/std/src/sys/pal/sgx/abi/usercalls/mod.rs

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::cmp;
22
use crate::io::{Error as IoError, ErrorKind, IoSlice, IoSliceMut, Result as IoResult};
33
use crate::random::{DefaultRandomSource, Random};
4+
use crate::sync::OnceLock;
45
use crate::time::{Duration, Instant};
6+
use crate::ops::Add;
7+
use insecure_time::{Freq, Tsc, TscBuilder, LearningFreqTscBuilder, NativeTime, NoRdtscTscBuilder};
58

69
pub(crate) mod alloc;
710
#[macro_use]
@@ -10,6 +13,7 @@ pub(crate) mod raw;
1013
mod tests;
1114

1215
use self::raw::*;
16+
use self::alloc::UserRef;
1317

1418
/// Usercall `read`. See the ABI documentation for more information.
1519
///
@@ -249,11 +253,79 @@ pub fn send(event_set: u64, tcs: Option<Tcs>) -> IoResult<()> {
249253
unsafe { raw::send(event_set, tcs).from_sgx_result() }
250254
}
251255

256+
#[derive(Copy, Clone, PartialOrd, PartialEq)]
257+
struct SgxTime(u64);
258+
259+
impl SgxTime {
260+
const NANOS_PER_SEC: u32 = 1_000_000_000;
261+
262+
pub fn as_duration(&self) -> Duration {
263+
Duration::new(self.0 / Self::NANOS_PER_SEC as u64, (self.0 % Self::NANOS_PER_SEC as u64) as _)
264+
}
265+
}
266+
267+
impl Add<Duration> for SgxTime {
268+
type Output = SgxTime;
269+
270+
fn add(self, other: Duration) -> Self::Output {
271+
let t = self.0 + other.as_secs() * Self::NANOS_PER_SEC as u64 + other.subsec_nanos() as u64;
272+
SgxTime(t)
273+
}
274+
}
275+
276+
impl NativeTime for SgxTime {
277+
fn minimum() -> SgxTime {
278+
SgxTime(0)
279+
}
280+
281+
fn abs_diff(&self, other: &Self) -> Duration {
282+
Duration::from_nanos(self.0.abs_diff(other.0))
283+
}
284+
285+
fn now() -> Self {
286+
let (t, _info) = unsafe { raw::insecure_time() };
287+
SgxTime(t)
288+
}
289+
}
290+
252291
/// Usercall `insecure_time`. See the ABI documentation for more information.
253292
#[unstable(feature = "sgx_platform", issue = "56975")]
254293
pub fn insecure_time() -> Duration {
255-
let t = unsafe { raw::insecure_time() };
256-
Duration::new(t / 1_000_000_000, (t % 1_000_000_000) as _)
294+
static TSC: OnceLock<Tsc<SgxTime>> = OnceLock::new();
295+
296+
let tsc = TSC.get_or_init(|| {
297+
let (_t, info) = unsafe { raw::insecure_time() };
298+
let freq = if !info.is_null() {
299+
let info = unsafe { UserRef::<InsecureTimeInfo>::from_ptr(info).to_enclave() };
300+
if info.frequency != 0 {
301+
Some(Freq::from_u64(info.frequency))
302+
} else {
303+
// Enclave runner passed in info, but the host didn't report frequency. In
304+
// the current implementation of the runner, that can't happen, but we may need
305+
// something like that in the future when we pass in additional info. Just being
306+
// very defensive here.
307+
None
308+
}
309+
} else {
310+
// Old enclave runner that doesn't pass in information
311+
None
312+
};
313+
314+
if let Some(freq) = freq {
315+
LearningFreqTscBuilder::new()
316+
.set_initial_frequency(freq)
317+
.set_frequency_learning_period(Duration::from_secs(1))
318+
.set_max_acceptable_drift(Duration::from_millis(1))
319+
.set_max_sync_interval(Duration::from_secs(60))
320+
.set_monotonic_time()
321+
.build()
322+
} else {
323+
NoRdtscTscBuilder::new()
324+
.set_monotonic_time()
325+
.build()
326+
}
327+
});
328+
tsc.now().as_duration()
257329
}
258330

259331
/// Usercall `alloc`. See the ABI documentation for more information.

0 commit comments

Comments
 (0)