From 2769358790438aee638acdf7df33721aa1b507a2 Mon Sep 17 00:00:00 2001 From: Jonathan Shamir Date: Sun, 24 Mar 2024 17:47:09 +0200 Subject: [PATCH] [PAL/LinuxSGX] add ocall_reset_clock which returns tsc and timezone info --- pal/src/host/linux-sgx/enclave_ocalls.c | 15 ++++++++++++++- pal/src/host/linux-sgx/enclave_ocalls.h | 2 ++ pal/src/host/linux-sgx/host_ocalls.c | 8 +++++++- pal/src/host/linux-sgx/pal_ocall_types.h | 5 +++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pal/src/host/linux-sgx/enclave_ocalls.c b/pal/src/host/linux-sgx/enclave_ocalls.c index b944a129c8..0b06d05b59 100644 --- a/pal/src/host/linux-sgx/enclave_ocalls.c +++ b/pal/src/host/linux-sgx/enclave_ocalls.c @@ -1766,7 +1766,7 @@ int ocall_shutdown(int sockfd, int how) { return retval; } -int ocall_gettime(uint64_t* microsec_ptr) { +int ocall_reset_time(uint64_t* microsec_ptr, uint64_t* tsc_ptr, int* tz_minutewest_ptr, int* tz_dsttime_ptr) { int retval = 0; struct ocall_gettime* ocall_gettime_args; @@ -1804,13 +1804,26 @@ int ocall_gettime(uint64_t* microsec_ptr) { break; } } + *microsec_ptr = MAX(microsec, expected_microsec); + if (tsc_ptr != NULL) { + *tsc_ptr = COPY_UNTRUSTED_VALUE(&ocall_gettime_args->tsc); + } + if (tz_minutewest_ptr != NULL && tz_dsttime_ptr != NULL) { + *tz_minutewest_ptr = COPY_UNTRUSTED_VALUE(&ocall_gettime_args->tz_minuteswest); + *tz_dsttime_ptr = COPY_UNTRUSTED_VALUE(&ocall_gettime_args->tz_minuteswest); + } } sgx_reset_ustack(old_ustack); return retval; } +int ocall_gettime(uint64_t* microsec_ptr) +{ + return ocall_reset_time(microsec_ptr, NULL, NULL, NULL); +} + void ocall_sched_yield(void) { void* old_ustack = sgx_prepare_ustack(); diff --git a/pal/src/host/linux-sgx/enclave_ocalls.h b/pal/src/host/linux-sgx/enclave_ocalls.h index 9b3a4d7c64..3dda405176 100644 --- a/pal/src/host/linux-sgx/enclave_ocalls.h +++ b/pal/src/host/linux-sgx/enclave_ocalls.h @@ -89,6 +89,8 @@ int ocall_create_process(size_t nargs, const char** args, uintptr_t (*reserved_m int ocall_futex(uint32_t* uaddr, int op, int val, uint64_t* timeout_us); +int ocall_reset_time(uint64_t* microsec, uint64_t* tsc, int* tz_minuteswest, int* tz_dsttime); + int ocall_gettime(uint64_t* microsec); void ocall_sched_yield(void); diff --git a/pal/src/host/linux-sgx/host_ocalls.c b/pal/src/host/linux-sgx/host_ocalls.c index dd73c1866e..a6e8245c1f 100644 --- a/pal/src/host/linux-sgx/host_ocalls.c +++ b/pal/src/host/linux-sgx/host_ocalls.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "cpu.h" #include "debug_map.h" @@ -603,8 +604,13 @@ static long sgx_ocall_shutdown(void* args) { static long sgx_ocall_gettime(void* args) { struct ocall_gettime* ocall_gettime_args = args; struct timeval tv; - DO_SYSCALL(gettimeofday, &tv, NULL); + struct timezone tz; + unsigned long long tsc = __rdtsc(); + DO_SYSCALL(gettimeofday, &tv, &tz); ocall_gettime_args->microsec = tv.tv_sec * (uint64_t)1000000 + tv.tv_usec; + ocall_gettime_args->tsc = tsc; + ocall_gettime_args->tz_minuteswest = tz.tz_minuteswest; + ocall_gettime_args->tz_dsttime = tz.tz_dsttime; return 0; } diff --git a/pal/src/host/linux-sgx/pal_ocall_types.h b/pal/src/host/linux-sgx/pal_ocall_types.h index 2d699ec648..cf3be4696a 100644 --- a/pal/src/host/linux-sgx/pal_ocall_types.h +++ b/pal/src/host/linux-sgx/pal_ocall_types.h @@ -293,6 +293,11 @@ struct ocall_shutdown { struct ocall_gettime { uint64_t microsec; + uint64_t tsc; + + /* for struct timezone initialization */ + int tz_minuteswest; + int tz_dsttime; }; struct ocall_poll {