diff options
author | Anton Kling <anton@kling.gg> | 2024-12-09 23:27:58 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-09 23:27:58 +0100 |
commit | 0a2e1f076786ff59bcc1fb6fdb066c0db2a5d77f (patch) | |
tree | efb761545a810b798f60fa3cc800f440fc75f20c | |
parent | 4bd8a81cca44285402af2a5e26db267c44abe4f4 (diff) |
timer: Use strings instead of direct int64_t to communicate with userland
This makes it easier to write and read from /dev/clock using shell
scripts. The actual performance decrease will be minimal and the
interface is not that perfect for proper time keeping anyways.
-rw-r--r-- | kernel/drivers/cmos.c | 2 | ||||
-rw-r--r-- | kernel/timer.c | 30 |
2 files changed, 23 insertions, 9 deletions
diff --git a/kernel/drivers/cmos.c b/kernel/drivers/cmos.c index bdb13cc..4223093 100644 --- a/kernel/drivers/cmos.c +++ b/kernel/drivers/cmos.c @@ -185,7 +185,7 @@ static i64 cmos_get_time(void) { } static void cmos_set_time(i64 time) { - assert(time > 0); // TODO: Add support for time travelers + assert(time >= 0); // TODO: Add support for time travelers u8 reg_b = cmos_get_register(0x0B); u8 seconds = 0; diff --git a/kernel/timer.c b/kernel/timer.c index b00a24b..dd92d3d 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -46,21 +46,35 @@ u64 timer_get_ms(void) { } int clock_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) { - (void)offset; + if (0 != offset) { + return 0; + } + // Currently don't have snprintf without NULL termination... + char tmp[4096]; u64 r = timer_get_ms(); - u64 l = min(len, sizeof(u64)); - memcpy(buffer, &r, l); + + u64 l = ksnprintf(tmp, 4096, "%llu", r); + l = min(len, l); + memcpy(buffer, tmp, l); return l; } int clock_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) { - (void)offset; - if (len != sizeof(i64)) { + if (0 != offset) { + return 0; + } + // TODO: Move sv to kernel or something to just avoid this. I hate null + // termination + char tmp[4096]; + memcpy(tmp, buffer, len); + tmp[len] = '\0'; + + int err; + u64 new_value_ms = parse_u64(tmp, NULL, 10, &err); + if (err) { return 0; } - i64 new_value_ms; - memcpy(&new_value_ms, buffer, sizeof(i64)); i64 new_value_seconds = new_value_ms / 1000; u64 offset_tsc = tsc_get() - start_tsc_time; @@ -82,6 +96,6 @@ int always_can_write(vfs_inode_t *inode); int timer_add_clock(void) { devfs_add_file("/clock", clock_read, clock_write, NULL, always_has_data, - always_can_write, FS_TYPE_CHAR_DEVICE); + always_can_write, FS_TYPE_BLOCK_DEVICE); return 1; } |