summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2023-09-18 16:20:52 +0200
committerdzwdz2023-09-18 16:20:52 +0200
commit14fd2aecd074fb93bb509df0c1cedd1f1055a4a6 (patch)
tree10ecb45967748a6b65558074f22c8cdf52ee74ae /src
parent730a929fffbaaaeb20529962654049fe26dd5dde (diff)
kernel: implement _sys_time()
After some consideration this seems like the most fitting way to handle timekeeping. Directly, the syscall is only useful for keeping time within a single process, but it is meant to be used for e.g. NTP clients, which will provide the real time through the VFS.
Diffstat (limited to 'src')
-rw-r--r--src/cmd/tests/kernel/miscsyscall.c18
-rw-r--r--src/kernel/proc.h3
-rw-r--r--src/kernel/syscalls.c13
-rw-r--r--src/libc/syscall.c4
-rw-r--r--src/libk/include/camellia/syscalls.h4
5 files changed, 42 insertions, 0 deletions
diff --git a/src/cmd/tests/kernel/miscsyscall.c b/src/cmd/tests/kernel/miscsyscall.c
index 7c61392..3cf3f2e 100644
--- a/src/cmd/tests/kernel/miscsyscall.c
+++ b/src/cmd/tests/kernel/miscsyscall.c
@@ -287,6 +287,23 @@ static void test_badopen(void) {
test(_sys_open(TMPFILEPATH, strlen(TMPFILEPATH), OPEN_CREATE) == -EINVAL);
}
+static void test_timer(void) {
+ uint64_t start = _sys_time(0);
+ test(start == 0);
+ _sys_sleep(10);
+ uint64_t delay = _sys_time(0) - start;
+ test(delay >= 10 * 1000000);
+ if (!fork()) {
+ uint64_t start = _sys_time(0);
+ test(start == 0);
+ _sys_sleep(10);
+ uint64_t delay = _sys_time(0) - start;
+ test(delay >= 10 * 1000000);
+ } else {
+ _sys_await();
+ }
+}
+
void r_k_miscsyscall(void) {
run_test(test_await);
run_test(test_await2);
@@ -297,4 +314,5 @@ void r_k_miscsyscall(void) {
run_test(test_execbuf);
run_test(test_sleep);
run_test(test_badopen);
+ run_test(test_timer);
}
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index bfe5cfc..5d95b52 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -98,6 +98,9 @@ struct Proc {
size_t len;
size_t pos;
} execbuf;
+
+ /* Time of the first _sys_time() call. 0 if not called yet. */
+ uint64_t basetime;
};
extern Proc *proc_cur;
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index a2e0e27..3350796 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -418,6 +418,18 @@ hid_t _sys_getprocfs(int flags) {
SYSCALL_RETURN(hid);
}
+uint64_t _sys_time(int flags) {
+ if (flags != 0) {
+ SYSCALL_RETURN(-ENOSYS);
+ }
+
+ uint64_t now = uptime_ns();
+ if (proc_cur->basetime == 0) {
+ proc_cur->basetime = now;
+ }
+ SYSCALL_RETURN(now - proc_cur->basetime);
+}
+
long _sys_execbuf(void __user *ubuf, size_t len) {
if (len == 0) SYSCALL_RETURN(0);
if (len > EXECBUF_MAX_LEN)
@@ -473,6 +485,7 @@ long _syscall(long num, long a, long b, long c, long d, long e) {
break; case _SYS_GETPPID: _sys_getppid();
break; case _SYS_WAIT2: _sys_wait2(a, b, (userptr_t)c);
break; case _SYS_GETPROCFS: _sys_getprocfs(a);
+ break; case _SYS_TIME: _sys_time(a);
break; case _SYS_EXECBUF: _sys_execbuf((userptr_t)a, b);
break; case _SYS_DEBUG_KLOG: _sys_debug_klog((userptr_t)a, b);
break;
diff --git a/src/libc/syscall.c b/src/libc/syscall.c
index 706aec6..4cdbe6c 100644
--- a/src/libc/syscall.c
+++ b/src/libc/syscall.c
@@ -94,6 +94,10 @@ hid_t _sys_getprocfs(int flags) {
return (hid_t)_syscall(_SYS_GETPROCFS, (long)flags, 0, 0, 0, 0);
}
+uint64_t _sys_time(int flags) {
+ return (uint64_t)_syscall(_SYS_TIME, (long)flags, 0, 0, 0, 0);
+}
+
long _sys_execbuf(void __user *buf, size_t len) {
return _syscall(_SYS_EXECBUF, (long)buf, (long)len, 0, 0, 0);
}
diff --git a/src/libk/include/camellia/syscalls.h b/src/libk/include/camellia/syscalls.h
index 58898d4..d27ae0c 100644
--- a/src/libk/include/camellia/syscalls.h
+++ b/src/libk/include/camellia/syscalls.h
@@ -22,6 +22,7 @@
#define _SYS_GETPPID 20
#define _SYS_WAIT2 21
#define _SYS_GETPROCFS 22
+#define _SYS_TIME 23
#define _SYS_EXECBUF 100
#define _SYS_DEBUG_KLOG 101
@@ -84,6 +85,9 @@ int _sys_wait2(int pid, int flags, struct sys_wait2 __user *out);
hid_t _sys_getprocfs(int flags);
+/** Returns the time in nanoseconds since the first _sys_time call in the process */
+uint64_t _sys_time(int flags);
+
/* see shared/execbuf.h */
long _sys_execbuf(void __user *buf, size_t len);