From baf97840e99e4cd65dbbf263db44a0f6c34a9a65 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 30 Oct 2022 09:07:03 +0100 Subject: [PATCH] Kernel: Keep track of boot time --- kernel/include/utils/Time.h | 8 +++++++ kernel/src/init/Init.cpp | 4 ++++ kernel/src/sys/clock.cpp | 16 ++++++++++---- kernel/src/utils/Time.cpp | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 kernel/include/utils/Time.h create mode 100644 kernel/src/utils/Time.cpp diff --git a/kernel/include/utils/Time.h b/kernel/include/utils/Time.h new file mode 100644 index 00000000..3e06716f --- /dev/null +++ b/kernel/include/utils/Time.h @@ -0,0 +1,8 @@ +#pragma once +#include + +int make_yday(int year, int month); + +uint64_t broken_down_to_unix(uint64_t year, uint64_t yday, uint64_t hour, uint64_t min, uint64_t sec); + +uint64_t unix_boottime(uint8_t boottime[8]); \ No newline at end of file diff --git a/kernel/src/init/Init.cpp b/kernel/src/init/Init.cpp index 2ad47635..59146d9b 100644 --- a/kernel/src/init/Init.cpp +++ b/kernel/src/init/Init.cpp @@ -17,6 +17,7 @@ #include "render/TextRenderer.h" #include "std/assert.h" #include "std/string.h" +#include "utils/Time.h" extern BOOTBOOT bootboot; extern "C" char environment[4096]; @@ -36,6 +37,7 @@ void Init::disable_smp() } extern "C" void asm_enable_sse(); +extern void clock_init(); void Init::early_init() { @@ -55,6 +57,8 @@ void Init::early_init() } else if (!strstr(environment, "verbose=1")) { KernelLog::toggle_log_level(LogLevel::DEBUG); } + clock_init(); + InitRD::init(); Mersenne::init(); diff --git a/kernel/src/sys/clock.cpp b/kernel/src/sys/clock.cpp index 81123aae..364fd494 100644 --- a/kernel/src/sys/clock.cpp +++ b/kernel/src/sys/clock.cpp @@ -1,9 +1,17 @@ +#include "bootboot.h" #include "interrupts/Context.h" +#include "std/errno.h" +#include "sys/UserMemory.h" +#include "thread/PIT.h" #include "thread/Scheduler.h" +#include "utils/Time.h" +#include -void sys_clock(Context* context) +static uint64_t unix_boot_time; + +extern BOOTBOOT bootboot; + +void clock_init() { - Task* current_task = Scheduler::current_task(); - context->rax = (long)current_task->cpu_time; - return; + unix_boot_time = unix_boottime(bootboot.datetime); } \ No newline at end of file diff --git a/kernel/src/utils/Time.cpp b/kernel/src/utils/Time.cpp new file mode 100644 index 00000000..0476a6b2 --- /dev/null +++ b/kernel/src/utils/Time.cpp @@ -0,0 +1,42 @@ +#define MODULE "time" + +#include "utils/Time.h" +#include "log/Log.h" + +int isleap(int year) +{ + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); +} + +int make_yday(int year, int month) +{ + static const short int upto[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + int yd; + + yd = upto[month - 1]; + if (month > 2 && isleap(year)) yd++; + return yd; +} + +uint64_t broken_down_to_unix(uint64_t year, uint64_t yday, uint64_t hour, uint64_t min, uint64_t sec) +{ + return sec + min * 60 + hour * 3600 + yday * 86400 + (year - 70) * 31536000 + ((year - 69) / 4) * 86400 - + ((year - 1) / 100) * 86400 + ((year + 299) / 400) * 86400; +} + +static int bcd_number_to_decimal(int num) +{ + return ((num >> 4) * 10) + (num & 0xf); +} + +uint64_t unix_boottime(uint8_t boottime[8]) +{ + int year = bcd_number_to_decimal(boottime[0]) * 100 + bcd_number_to_decimal(boottime[1]); + int month = bcd_number_to_decimal(boottime[2]); + int day = bcd_number_to_decimal(boottime[3]); + int hour = bcd_number_to_decimal(boottime[4]); + int minute = bcd_number_to_decimal(boottime[5]); + int second = bcd_number_to_decimal(boottime[6]); + kinfoln("UTC boot time: %d-%d-%d %d:%d:%d", year, month, day, hour, minute, second); + return broken_down_to_unix(year - 1900, make_yday(year, month) + (day - 1), hour, minute, second); +} \ No newline at end of file