diff --git a/api/kernel/os.hpp b/api/kernel/os.hpp index 59b92ab957..78a1fc8d42 100644 --- a/api/kernel/os.hpp +++ b/api/kernel/os.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace hw{ class Serial; } @@ -55,11 +56,18 @@ class OS { return cycles_since_boot() / cpu_mhz_.count(); } - /** Uptime in seconds. */ - static double uptime() { - return cycles_since_boot() / Hz(cpu_mhz_).count(); + /** Timestamp for when OS was booted */ + static RTC::timestamp_t boot_timestamp() + { return booted_at_; } + + /** Uptime in whole seconds. */ + static RTC::timestamp_t uptime() { + return RTC::now() - booted_at_; } + static MHz cpu_freq() + { return cpu_mhz_; } + /** * Shutdown operating system * @@ -159,6 +167,8 @@ class OS { static hw::Serial& com1; + static RTC::timestamp_t booted_at_; + struct Custom_init_struct { Custom_init_struct(Custom_init f, const char* n) diff --git a/api/kernel/rtc.hpp b/api/kernel/rtc.hpp index e6d8b5b42e..ccba22d08c 100644 --- a/api/kernel/rtc.hpp +++ b/api/kernel/rtc.hpp @@ -28,10 +28,7 @@ class RTC using timestamp_t = int64_t; /// returns a 64-bit unix timestamp of the current time - static timestamp_t get(); - - static timestamp_t now() - { return get(); } + static timestamp_t now(); /// start an auto-calibration process static void init(); diff --git a/src/kernel/os.cpp b/src/kernel/os.cpp index 1ae6bc209d..a6bd35bd5d 100644 --- a/src/kernel/os.cpp +++ b/src/kernel/os.cpp @@ -46,6 +46,7 @@ extern uintptr_t _MAX_MEM_MIB_; bool OS::power_ {true}; MHz OS::cpu_mhz_ {1000}; +RTC::timestamp_t OS::booted_at_ {0}; uintptr_t OS::low_memory_size_ {0}; uintptr_t OS::high_memory_size_ {0}; uintptr_t OS::heap_max_ {0xfffffff}; @@ -215,6 +216,7 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) { // Realtime/monotonic clock RTC::init(); + booted_at_ = RTC::now(); // Trying custom initialization functions MYINFO("Calling custom initialization functions"); diff --git a/src/kernel/rtc.cpp b/src/kernel/rtc.cpp index 6938863308..be28595cdd 100644 --- a/src/kernel/rtc.cpp +++ b/src/kernel/rtc.cpp @@ -28,10 +28,10 @@ void RTC::init() }); } -RTC::timestamp_t RTC::get() +RTC::timestamp_t RTC::now() { auto ticks = hw::CPU::rdtsc() - current_ticks; auto diff = ticks / Hz(MHz(_CPUFreq_)).count(); - + return current_time + diff; } diff --git a/src/kernel/syscalls.cpp b/src/kernel/syscalls.cpp index ebec9a7596..19423f22b2 100644 --- a/src/kernel/syscalls.cpp +++ b/src/kernel/syscalls.cpp @@ -149,7 +149,7 @@ int wait(int* UNUSED(status)) { } int gettimeofday(struct timeval* p, void*) { - p->tv_sec = RTC::get(); + p->tv_sec = RTC::now(); p->tv_usec = 0; return 0; } @@ -212,7 +212,7 @@ void abort_ex(const char* why) { // Basic second-resolution implementation - using CMOS directly for now. int clock_gettime(clockid_t clk_id, struct timespec *tp){ if (clk_id == CLOCK_REALTIME) { - tp->tv_sec = RTC::get(); + tp->tv_sec = RTC::now(); tp->tv_nsec = 0; return 0; }