Kernel: Keep track of boot time

This commit is contained in:
apio 2022-10-30 09:07:03 +01:00
parent a9da58421f
commit baf97840e9
4 changed files with 66 additions and 4 deletions

View File

@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>
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]);

View File

@ -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();

View File

@ -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 <sys/types.h>
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);
}

42
kernel/src/utils/Time.cpp Normal file
View File

@ -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);
}