Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions api/kernel/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <hw/cpu.hpp>
#include <hertz>
#include <vector>
#include <kernel/rtc.hpp>

namespace hw{ class Serial; }

Expand All @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions api/kernel/rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/kernel/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/kernel/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down