Skip to content

Commit 341d837

Browse files
committed
Organized timer code. Started to work on HPET.
1 parent b67a682 commit 341d837

File tree

11 files changed

+146
-47
lines changed

11 files changed

+146
-47
lines changed

include/kernel/apic.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,22 @@
2121
#define LAPIC_TIMER_CURR 0x390
2222
#define LAPIC_TIMER_LVT 0x320
2323

24+
#define DEFAULT_LAPIC_ADDRESS 0xfee00000
25+
26+
#define DIV_BY_2 0x00
27+
#define DIV_BY_4 0x01
28+
#define DIV_BY_8 0x02
29+
#define DIV_BY_16 0x03
30+
#define DIV_BY_32 0x08
31+
#define DIV_BY_64 0x09
32+
#define DIV_BY_128 0x0a
33+
#define DIV_BY_1 0x0b
34+
2435
void apic_init();
36+
void lapic_timer_init();
37+
void timer_frequency(uint32_t freq);
2538
void write_lapic(uintptr_t reg, uint32_t value);
39+
2640
uint32_t read_lapic(uintptr_t reg);
2741

2842
#endif

include/kernel/hpet.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef MENIOS_INCLUDE_KERNEL_HPET_H
2+
#define MENIOS_INCLUDE_KERNEL_HPET_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <types.h>
9+
10+
#define HPET_BASE_ADDRESS 0xF1000000
11+
#define HPET_REG_CAPABILITIES 0x00
12+
#define HPET_REG_CONFIGURATION 0x10
13+
#define HPET_REG_INTERRUPT_STATUS 0x20
14+
#define HPET_REG_MAIN_COUNTER 0xF0
15+
#define HPET_REG_TIMER0_CONFIG 0x100
16+
#define HPET_REG_TIMER0_COMPARATOR 0x108
17+
#define HPET_REG_TIMER0_COUNTER 0x110
18+
#define HPET_REG_TIMER1_CONFIG 0x120
19+
#define HPET_REG_TIMER1_COMPARATOR 0x128
20+
#define HPET_REG_TIMER1_COUNTER 0x130
21+
#define HPET_REG_TIMER2_CONFIG 0x140
22+
#define HPET_REG_TIMER2_COMPARATOR 0x148
23+
#define HPET_REG_TIMER2_COUNTER 0x150
24+
#define HPET_REG_TIMER3_CONFIG 0x160
25+
#define HPET_REG_TIMER3_COMPARATOR 0x168
26+
#define HPET_REG_TIMER3_COUNTER 0x170
27+
#define HPET_REG_TIMER4_CONFIG 0x180
28+
#define HPET_REG_TIMER4_COMPARATOR 0x188
29+
#define HPET_REG_TIMER4_COUNTER 0x190
30+
#define HPET_REG_TIMER5_CONFIG 0x1A0
31+
#define HPET_REG_TIMER5_COMPARATOR 0x1A8
32+
#define HPET_REG_TIMER5_COUNTER 0x1B0
33+
34+
#define HPET_OK 0
35+
#define HPET_ERROR -1
36+
typedef int hpet_status_t;
37+
38+
hpet_status_t hpet_timer_init();
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif //MENIOS_INCLUDE_KERNEL_HPET_H

include/kernel/pmm.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef MENIOS_INCLUDE_KERNEL_PMM_H
22
#define MENIOS_INCLUDE_KERNEL_PMM_H
33

4-
#include <stdint.h>
4+
#include <types.h>
55

66
// good enough for 16GB - it can be increased later
77
#define PAGE_BITMAP_SIZE 0x10000
@@ -16,6 +16,15 @@
1616

1717
#define VADDR_UNUSED (0xffff000000000000)
1818

19+
typedef struct {
20+
uint64_t error_code; // Error code pushed by the CPU
21+
uint64_t rip; // Instruction pointer at the time of the fault
22+
uint64_t cs; // Code segment at the time of the fault
23+
uint64_t rflags; // Flags register at the time of the fault
24+
uint64_t rsp; // Stack pointer at the time of the fault
25+
uint64_t faulting_address; // The address that caused the page fault (from CR2)
26+
} page_fault_info_t;
27+
1928
// Page Map Level 4 (PML4) Entry
2029
typedef struct {
2130
uint64_t present: 1; // Page present in memory
@@ -113,17 +122,17 @@ uintptr_t read_cr3();
113122

114123
void debug_heap();
115124
void pmm_init();
116-
void write_cr3(uint64_t value);
125+
void write_cr3(phys_addr_t value);
117126

118127
uint64_t get_first_free_page();
119128

120-
void pml4_map(uintptr_t vaddr, pml4_map_t* map);
129+
void pml4_map(virt_addr_t vaddr, pml4_map_t* map);
121130

122-
uintptr_t get_first_free_virtual_address(uintptr_t offset);
131+
virt_addr_t get_first_free_virtual_address(virt_addr_t offset);
123132

124-
uintptr_t physical_to_virtual(uintptr_t physical_address);
125-
uintptr_t virtual_to_physical(uintptr_t virtual_to_physical);
133+
virt_addr_t physical_to_virtual(phys_addr_t physical_address);
134+
phys_addr_t virtual_to_physical(virt_addr_t virtual_address);
126135

127-
void set_page_used(uintptr_t physical_address);
136+
void set_page_used(phys_addr_t physical_address);
128137

129138
#endif

include/kernel/rtc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ extern "C" {
1010
#define RTC_INDEX_PORT 0x70
1111
#define RTC_DATA_PORT 0x71
1212

13+
#define RTC_SECONDS 0x00
14+
#define RTC_MINUTES 0x02
15+
#define RTC_HOURS 0x04
16+
#define RTC_DAY 0x07
17+
#define RTC_MONTH 0x08
18+
#define RTC_YEAR 0x09
19+
#define RTC_CENTURY 0x32
20+
#define RTC_REGISTER_B 0x0b
21+
#define RTC_WEEKDAY 0x06
22+
1323
typedef struct rtc_time_t {
1424
uint8_t seconds;
1525
uint8_t minutes;

include/kernel/timer.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ extern "C" {
77

88
#include <types.h>
99

10-
#define DIV_BY_2 0x00
11-
#define DIV_BY_4 0x01
12-
#define DIV_BY_8 0x02
13-
#define DIV_BY_16 0x03
14-
#define DIV_BY_32 0x08
15-
#define DIV_BY_64 0x09
16-
#define DIV_BY_128 0x0a
17-
#define DIV_BY_1 0x0b
18-
1910
void timer_init();
11+
void timer_eoi();
2012

2113
uint64_t unix_time();
2214

include/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ typedef long long off_t;
2323
(typeof(a)) (ROUNDDOWN((uint32_t) (a) + __n - 1, __n)); \
2424
})
2525

26+
typedef uintptr_t phys_addr_t;
27+
typedef uintptr_t virt_addr_t;
28+
2629
#ifdef __cplusplus
2730
}
2831
#endif

src/kernel/mem/mem.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
uint8_t arena[PAGE_SIZE * HEAP_SIZE];
1111

1212
void mem_init() {
13-
serial_puts("\n- Initing memory management:\n");
14-
// init the physical memory management
15-
pmm_init();
13+
serial_puts("\n- Initing memory management:\n");
14+
// init the physical memory management
15+
pmm_init();
1616

17-
init_heap((void*)arena, PAGE_SIZE * HEAP_SIZE);
17+
init_heap((void*)arena, PAGE_SIZE * HEAP_SIZE);
1818
}

src/kernel/timer/hpet.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <kernel/hpet.h>
2+
3+
hpet_status_t hpet_timer_init() {
4+
return HPET_ERROR;
5+
}

src/kernel/timer/lapic.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <kernel/apic.h>
2+
#include <kernel/pmm.h>
3+
#include <kernel/serial.h>
4+
5+
static uintptr_t addr;
6+
7+
static uint64_t timer_freq = 126582;
8+
9+
void lapic_timer_init() {
10+
addr = physical_to_virtual(DEFAULT_LAPIC_ADDRESS);
11+
serial_printf("lapic address: %lx - virt: %lx\n", DEFAULT_LAPIC_ADDRESS, addr);
12+
write_lapic(addr + LAPIC_SVR, read_lapic(addr + LAPIC_SVR) | 0x100);
13+
write_lapic(addr + LAPIC_TIMER_DIV, DIV_BY_128);
14+
timer_frequency(timer_freq);
15+
write_lapic(addr + LAPIC_TIMER_LVT, 0x20020);
16+
}
17+
18+
void timer_frequency(uint32_t freq) {
19+
timer_freq = freq;
20+
serial_printf("timer frequency: %d\n", timer_freq);
21+
write_lapic(addr + LAPIC_TIMER_INIT, timer_freq);
22+
}
23+
24+
void timer_eoi() {
25+
write_lapic(addr + LAPIC_EOI, 0); // End Of Interrupt (EOI) to acknowledge
26+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ uint8_t rtc_read(uint8_t reg) {
1010
}
1111

1212
uint8_t bcd_to_binary(uint8_t bcd) {
13-
return (bcd >> 4) * 10 + (bcd & 0x0F);
13+
return (bcd >> 4) * 10 + (bcd & 0x0f);
1414
}
1515

1616
void rtc_time(rtc_time_t* time) {
1717
time->register_b = rtc_read(0x0b);
1818

1919
bool bcd = !(time->register_b & 0x04);
2020

21-
uint8_t seconds = rtc_read(0x00); // Read seconds
22-
uint8_t minutes = rtc_read(0x02); // Read minutes
23-
uint8_t weekday = rtc_read(0x06);
24-
uint8_t hours = rtc_read(0x04); // Read hours
25-
uint8_t day = rtc_read(0x07); // Read day of the month
26-
uint8_t month = rtc_read(0x08); // Read month
27-
uint8_t year = rtc_read(0x09); // Read year (offset from 1900)
28-
uint8_t century = rtc_read(0x32);
21+
uint8_t seconds = rtc_read(RTC_SECONDS);
22+
uint8_t minutes = rtc_read(RTC_MINUTES);
23+
uint8_t weekday = rtc_read(RTC_WEEKDAY);
24+
uint8_t hours = rtc_read(RTC_HOURS);
25+
uint8_t day = rtc_read(RTC_DAY);
26+
uint8_t month = rtc_read(RTC_MONTH);
27+
uint8_t year = rtc_read(RTC_YEAR);
28+
uint8_t century = rtc_read(RTC_CENTURY);
2929

3030
time->seconds = bcd ? bcd_to_binary(seconds) : seconds;
3131
time->minutes = bcd ? bcd_to_binary(minutes) : minutes;

0 commit comments

Comments
 (0)