Skip to content

Commit

Permalink
[PAL/LinuxSGX] add ocall_reset_clock which returns tsc and timezone info
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-sha committed Apr 1, 2024
1 parent 0ee48ee commit 2769358
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pal/src/host/linux-sgx/enclave_ocalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions pal/src/host/linux-sgx/enclave_ocalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion pal/src/host/linux-sgx/host_ocalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/signal.h>
#include <x86intrin.h>

#include "cpu.h"
#include "debug_map.h"
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions pal/src/host/linux-sgx/pal_ocall_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2769358

Please sign in to comment.