diff options
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/cmos.c | 34 | ||||
-rw-r--r-- | src/kernel/cmos.h | 18 | ||||
-rw-r--r-- | src/kernel/idt.c | 19 | ||||
-rw-r--r-- | src/kernel/isrs.asm | 2 | ||||
-rw-r--r-- | src/kernel/main.c | 11 |
5 files changed, 77 insertions, 7 deletions
diff --git a/src/kernel/cmos.c b/src/kernel/cmos.c new file mode 100644 index 0000000..39f7c4f --- /dev/null +++ b/src/kernel/cmos.c @@ -0,0 +1,34 @@ +#include "cmos.h" +#include "util.h" + +static inline uint8_t read_cmos(uint8_t reg) { + outb(0x0070, reg); + return inb(0x0071); +} + +//The idea of reading until you get the same value twice, and checking the status register +// is from the OSDev Wiki page at <https://wiki.osdev.org/CMOS#The_Real-Time_Clock>. +struct rtc_time get_rtc_time() { + uint8_t prev_sec = -1; + struct rtc_time ret; +get_sec: + while (read_cmos(0x0a) & 0x80) + ;//spin while rtc is being updated + ret.seconds = read_cmos(0x00); + if (ret.seconds != prev_sec) { + prev_sec = ret.seconds; + goto get_sec; + } + ret.minutes = read_cmos(0x02); + ret.hours = read_cmos(0x04); + if (ret.hours & 0x80) { + ret.hours -= 0x80 - 12; + if (ret.hours == 24) + ret.hours = 0; + } + ret.day_of_week = read_cmos(0x06); + ret.day_of_month = read_cmos(0x07); + ret.month = read_cmos(0x08); + ret.year = read_cmos(0x09); + return ret; +}
\ No newline at end of file diff --git a/src/kernel/cmos.h b/src/kernel/cmos.h new file mode 100644 index 0000000..53ec398 --- /dev/null +++ b/src/kernel/cmos.h @@ -0,0 +1,18 @@ +#ifndef CMOS_H +#define CMOS_H + +#include <stdint.h> + +struct rtc_time { + uint8_t seconds; + uint8_t minutes; + uint8_t hours; + uint8_t day_of_week; + uint8_t day_of_month; + uint8_t month; + uint8_t year; +}; + +struct rtc_time get_rtc_time(); + +#endif
\ No newline at end of file diff --git a/src/kernel/idt.c b/src/kernel/idt.c index 9b3ba96..99284ac 100644 --- a/src/kernel/idt.c +++ b/src/kernel/idt.c @@ -2,6 +2,7 @@ #include "window.h" #include "drive.h" #include "panic.h" +#include "cmos.h" #include "pmap.h" #include "task.h" #include "util.h" @@ -141,6 +142,21 @@ uint32_t sc_is_task_running(uint32_t handle) { return tasks[handle - 1].page_directory ? 1 : 0; } +static const uint16_t days_into_four_years_per_month[] = { + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, + 366, 366+31, 366+59, 366+90, 366+120, 366+151, 366+181, 366+212, 366+243, 366+273, 366+304, 366+334, + 731, 731+31, 731+59, 731+90, 731+120, 731+151, 731+181, 731+212, 731+243, 731+273, 731+304, 731+334, + 1096, 1096+31, 1096+59, 1096+90, 1096+120, 1096+151, 1096+181, 1096+212, 1096+243, 1096+273, 1096+304, 1096+334 +}; + +//gregorian, assumes year is in 2001 to 2099 +uint32_t sc_get_timestamp() { + const struct rtc_time time = get_rtc_time(); + const uint32_t secs_into_month = time.seconds + time.minutes * 60 + time.hours * 3600 + (time.day_of_month - 1) * 86400; + const uint32_t days_to_month_into_four_years = days_into_four_years_per_month[time.month + (time.year % 4) * 12 - 1]; + return secs_into_month + days_to_month_into_four_years * 86400 + (time.year / 4) * (365 * 4 + 1) * 86400; +} + void const *syscall_table[] = { &sc_open_file, &sc_close_file, @@ -166,7 +182,8 @@ void const *syscall_table[] = { &sc_wait_any_ipc_sent, &find_unread_ipc, &sc_wait_ipc_read, - &sc_is_task_running + &sc_is_task_running, + &sc_get_timestamp }; //these aren't really void ()'s, but gcc complains if we take an address of a void, so we give it a type diff --git a/src/kernel/isrs.asm b/src/kernel/isrs.asm index a4eb988..f547bfb 100644 --- a/src/kernel/isrs.asm +++ b/src/kernel/isrs.asm @@ -25,7 +25,7 @@ extern exception_halt extern pf_check_stack extern dump -n_syscalls equ 0x19 +n_syscalls equ 0x1a ;section .bss ;_debug_is_start_task resb 1 diff --git a/src/kernel/main.c b/src/kernel/main.c index 26cc5e4..1fd3182 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -4,16 +4,17 @@ #include "window.h" #include "panic.h" #include "boot.h" +#include "cmos.h" +#include "pmap.h" +#include "task.h" #include "util.h" +#include "elf.h" #include "fat.h" #include "ide.h" #include "idt.h" -#include "pmap.h" -#include "task.h" -#include "pci.h" -#include "elf.h" -#include "log.h" #include "kbd.h" +#include "log.h" +#include "pci.h" void _start_user_mode() __attribute__ ((noreturn)); |