Clean up some code

This commit is contained in:
Justine Tunney 2023-10-11 11:45:31 -07:00
parent ec3275179f
commit 285c565051
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
33 changed files with 122 additions and 382 deletions

View file

@ -16,12 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/clock.h"
#ifdef __x86_64__
#define CTL_KERN 1
#define KERN_BOOTTIME 21
int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
long ax, dx;
if (clock == CLOCK_REALTIME) {
@ -41,18 +47,28 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
// 2. old xnu returns *ts in rax:rdx regs
//
// we assume this system call always succeeds
asm volatile("syscall"
: "=a"(ax), "=d"(dx)
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
: "rcx", "r8", "r9", "r10", "r11", "memory");
if (ax) {
ts->tv_sec = ax;
ts->tv_nsec = dx;
if (ts) {
asm volatile("syscall"
: "=a"(ax), "=d"(dx)
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
: "rcx", "r8", "r9", "r10", "r11", "memory");
if (ax) {
ts->tv_sec = ax;
ts->tv_nsec = dx;
}
ts->tv_nsec *= 1000;
}
ts->tv_nsec *= 1000;
return 0;
} else if (clock == CLOCK_MONOTONIC) {
if (!ts) return 0;
return sys_clock_gettime_mono(ts);
} else if (clock == CLOCK_BOOTTIME) {
struct timeval x;
size_t n = sizeof(x);
int mib[] = {CTL_KERN, KERN_BOOTTIME};
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return -1;
if (ts) *ts = timeval_totimespec(timeval_sub(timeval_real(), x));
return 0;
} else {
return -EINVAL;
}