Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x86: 64-bit time #994

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion options/ansi/include/bits/ansi/time_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#ifndef MLIBC_TIME_T
#define MLIBC_TIME_T

typedef long time_t;
#include <bits/types.h>
typedef __mlibc_int64 time_t;

#endif

8 changes: 7 additions & 1 deletion options/ansi/include/bits/ansi/timespec.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
#define MLIBC_TIMESPEC_H

#include <bits/ansi/time_t.h>
#include <bits/field-padding.h>

// Equivalent of timespec64 in glibc.
// Should be used only with 64-bit syscalls
// or with appropriate compat syscalls.
struct timespec {
time_t tv_sec;
long tv_nsec;
// tv_nspec is required to be long by the C standard.
// However linux kernel expects long long. So we add padding.
__MLIBC_FIELD_PADDED(long, long long, tv_nsec);
};

#endif // MLIBC_TIMESPEC_H
Expand Down
9 changes: 9 additions & 0 deletions options/internal/include/bits/field-padding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef MLIBC_FIELD_PADDING_H
#define MLIBC_FIELD_PADDING_H

#define __MLIBC_FIELD_PADDED(T, AT, F) \
AT : (sizeof(AT)-sizeof(T))*8*(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__); \
T F; \
AT : (sizeof(AT)-sizeof(T))*8*(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)

#endif
74 changes: 69 additions & 5 deletions sysdeps/linux/generic/sysdeps.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <asm/ioctls.h>
#include <stdint.h>
#include <errno.h>
#include <limits.h>

Expand Down Expand Up @@ -235,7 +236,11 @@ int sys_vm_unmap(void *pointer, size_t size) {

int sys_clock_get(int clock, time_t *secs, long *nanos) {
struct timespec tp = {};
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_syscall(SYS_clock_gettime, clock, &tp);
#else
auto ret = do_syscall(SYS_clock_gettime64, clock, &tp);
#endif
if (int e = sc_error(ret); e)
return e;
*secs = tp.tv_sec;
Expand All @@ -245,7 +250,11 @@ int sys_clock_get(int clock, time_t *secs, long *nanos) {

int sys_clock_getres(int clock, time_t *secs, long *nanos) {
struct timespec tp = {};
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_syscall(SYS_clock_getres, clock, &tp);
#else
auto ret = do_syscall(SYS_clock_getres_time64, clock, &tp);
#endif
if (int e = sc_error(ret); e)
return e;
*secs = tp.tv_sec;
Expand All @@ -272,14 +281,22 @@ int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat
}

int sys_statfs(const char *path, struct statfs *buf) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_cp_syscall(SYS_statfs, path, buf);
#else
auto ret = do_cp_syscall(SYS_statfs64, path, buf);
#endif
if (int e = sc_error(ret); e)
return e;
return 0;
}

int sys_fstatfs(int fd, struct statfs *buf) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_cp_syscall(SYS_fstatfs, fd, buf);
#else
auto ret = do_cp_syscall(SYS_fstatfs64, fd, buf);
#endif
if (int e = sc_error(ret); e)
return e;
return 0;
Expand Down Expand Up @@ -307,11 +324,11 @@ int sys_sigaction(int signum, const struct sigaction *act,

static_assert(sizeof(sigset_t) == 8);

auto ret = do_syscall(SYS_rt_sigaction, signum, act ?
&kernel_act : NULL, oldact ?
&kernel_oldact : NULL, sizeof(sigset_t));
if (int e = sc_error(ret); e)
return e;
auto ret = do_syscall(SYS_rt_sigaction, signum, act ?
&kernel_act : NULL, oldact ?
&kernel_oldact : NULL, sizeof(sigset_t));
if (int e = sc_error(ret); e)
return e;

if (oldact) {
oldact->sa_handler = kernel_oldact.handler;
Expand Down Expand Up @@ -763,9 +780,42 @@ int sys_setpriority(int which, id_t who, int prio) {
}

int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_syscall(SYS_setitimer, which, new_value, old_value);

if (int e = sc_error(ret); e)
return e;

#else
if (new_value->it_interval.tv_sec > INT32_MAX)
return -ENOTSUP;
if (new_value->it_value.tv_sec > INT32_MAX)
return -ENOTSUP;

long new_isec = (long)new_value->it_interval.tv_sec;
long new_vsec = (long)new_value->it_value.tv_sec;

long new_raw[4] = {
new_isec, new_value->it_interval.tv_usec,
new_vsec, new_value->it_value.tv_usec,
};
long old_raw[4];

auto ret = do_syscall(SYS_setitimer, which, new_raw, old_raw);

if (int e = sc_error(ret); e) {
return e;
}


if (old_value) {
old_value->it_interval.tv_sec = old_raw[0];
old_value->it_interval.tv_usec = old_raw[1];
old_value->it_value.tv_sec = old_raw[2];
old_value->it_value.tv_usec = old_raw[3];
}

#endif
return 0;
}

Expand Down Expand Up @@ -811,7 +861,11 @@ int sys_timer_create(clockid_t clk, struct sigevent *__restrict evp, timer_t *__
}

int sys_timer_settime(timer_t t, int flags, const struct itimerspec *__restrict val, struct itimerspec *__restrict old) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_syscall(SYS_timer_settime, t, flags, val, old);
#else
auto ret = do_syscall(SYS_timer_settime64, t, flags, val, old);
#endif
if (int e = sc_error(ret); e) {
return e;
}
Expand Down Expand Up @@ -1019,7 +1073,11 @@ int sys_timerfd_create(int clockid, int flags, int *fd) {
}

int sys_timerfd_settime(int fd, int flags, const struct itimerspec *value, struct itimerspec *oldvalue) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_syscall(SYS_timerfd_settime, fd, flags, value, oldvalue);
#else
auto ret = do_syscall(SYS_timerfd_settime64, fd, flags, value, oldvalue);
#endif
if (int e = sc_error(ret); e)
return e;
return 0;
Expand Down Expand Up @@ -1579,9 +1637,15 @@ int sys_futex_tid() {
}

int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
#if UINTPTR_MAX == UINT64_MAX
auto ret = do_cp_syscall(SYS_futex, pointer, FUTEX_WAIT, expected, time);
#else
auto ret = do_cp_syscall(SYS_futex_time64, pointer, FUTEX_WAIT, expected, time);
#endif

if (int e = sc_error(ret); e)
return e;

return 0;
}

Expand Down
7 changes: 4 additions & 3 deletions tests/ansi/timegm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>

int main() {
struct tm soon = {};
Expand All @@ -14,7 +15,7 @@ int main() {
time_t expected_result = 0;
// This should be epoch.
result = timegm(&soon);
printf("epoch: %ld\n", result);
printf("epoch: %" PRId64 "\n", result);
assert(result == expected_result);

soon.tm_sec = 12;
Expand All @@ -26,7 +27,7 @@ int main() {
expected_result = 1652803692;
result = timegm(&soon);
// On my host, this returned 1652803692, verify this.
printf("epoch: %ld\n", result);
printf("epoch: %" PRId64 "\n", result);
assert(result == expected_result);

soon.tm_sec = 45;
Expand All @@ -38,7 +39,7 @@ int main() {
expected_result = -9181035;
result = timegm(&soon);
// On my host, this returned -9181035, verify this.
printf("epoch: %ld\n", result);
printf("epoch: %" PRId64 "\n", result);
assert(result == expected_result);

return 0;
Expand Down
Loading