Skip to content

Commit 4a98ecd

Browse files
committed
Saved state of timer handle working
1 parent 341d837 commit 4a98ecd

File tree

27 files changed

+639
-78
lines changed

27 files changed

+639
-78
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,21 @@ run:
167167
qemu-system-x86_64 -smp cpus=1,maxcpus=2,sockets=1,dies=1,clusters=1,cores=2 -vga std -no-reboot --no-shutdown -M q35 -m size=2G,maxmem=2G -hda menios.hdd -serial file:com1.log -monitor stdio -d int -M hpet=on -usb -machine q35 -rtc base=utc,clock=host
168168

169169
.PHONY: test
170-
test:
170+
test: docker
171171
@set -eux
172172

173173
ifeq ($(OS_NAME),linux)
174174
@echo "Testing inside Linux"
175175

176176
for file in $(shell find -L test -type f -name 'test_*.c'); do \
177-
gcc -DMENIOS_NO_DEBUG -I./include $$file test/unity.c src/kernel/mem/kmalloc.c -o "$$file".bin ; \
177+
gcc -DMENIOS_NO_DEBUG -I./include \
178+
$$file \
179+
test/unity.c \
180+
src/kernel/console/vprintk.c \
181+
src/kernel/mem/kmalloc.c \
182+
src/libc/itoa.c \
183+
src/libc/string.c \
184+
-o "$$file".bin ; \
178185
echo "Testing $(.c:.bin=$$file)" ; \
179186
"$$file".bin ; \
180187
rm "$$file".bin ; \

include/kernel/acpi.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
extern "C" {
88
#endif
99

10-
typedef struct {
10+
typedef struct acpi_rsdp_t {
1111
char signature[8];
1212
uint8_t checksum;
1313
char oemid[6];
1414
uint8_t revision;
1515
uint32_t rsdt_address;
1616
} acpi_rsdp_t;
1717

18-
typedef struct {
18+
typedef struct acpi_xsdp_t {
1919
char signature[8];
2020
uint8_t checksum;
2121
char oemid[6];
@@ -27,7 +27,7 @@ typedef struct {
2727
uint8_t reserved[3];
2828
} acpi_xsdp_t;
2929

30-
typedef struct {
30+
typedef struct acpi_sdt_header_t {
3131
char signature[4];
3232
uint32_t length;
3333
uint8_t revision;
@@ -37,18 +37,37 @@ typedef struct {
3737
uint32_t oem_revision;
3838
uint32_t creator_id;
3939
uint32_t creator_revision;
40-
} acpi_sdt_header_t;
40+
} __attribute__((packed)) acpi_sdt_header_t;
4141

42-
typedef struct {
42+
typedef struct acpi_rsdt_t {
4343
acpi_sdt_header_t header;
4444
uint32_t* sdt;
4545
} acpi_rsdt_t;
4646

47-
typedef struct {
47+
typedef struct acpi_xsdt_t {
4848
acpi_sdt_header_t header;
4949
uint64_t* sdt;
5050
} acpi_xsdt_t;
5151

52+
typedef uint8_t acpi_address_space_t;
53+
54+
#define ACPI_SYSTEM_MEMORY 0
55+
#define ACPI_SYSTEM_IO 1
56+
57+
#define ACPI_ACCESS_SIZE_UNDEFINED 0
58+
#define ACPI_ACCESS_SIZE_BYTE 1
59+
#define ACPI_ACCESS_SIZE_WORD 2
60+
#define ACPI_ACCESS_SIZE_DWORD 3
61+
#define ACPI_ACCESS_SIZE_QWORD 4
62+
63+
typedef struct acpi_address_t {
64+
uint8_t space_id;
65+
uint8_t register_bit_width;
66+
uint8_t register_bit_offset;
67+
uint8_t access_size;
68+
phys_addr_t address;
69+
} acpi_address_t;
70+
5271
int acpi_init();
5372
int acpi_shutdown();
5473

include/kernel/console.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define MENIOS_INCLUDE_KERNEL_CONSOLE_H
33

44
#include <stdarg.h>
5-
#include <stdint.h>
5+
#include <types.h>
66

77
#define FLAG_LEFT 0x01 // The '-' flag
88
#define FLAG_SIGN 0x02 // The '+' flag
@@ -22,4 +22,8 @@ uint16_t get_cursor_position();
2222
void gotoxy(uint32_t x, uint32_t y);
2323
void get_cursor_pos(screen_pos_t* pos);
2424

25+
int vprintk(char *str, const char *format, ...);
26+
int vsprintk(char* str, const char* format, va_list args);
27+
int vsnprintk(char *str, size_t, const char* format, va_list args);
28+
2529
#endif

include/kernel/hpet.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ extern "C" {
77

88
#include <types.h>
99

10+
#include <kernel/acpi.h>
11+
1012
#define HPET_BASE_ADDRESS 0xF1000000
1113
#define HPET_REG_CAPABILITIES 0x00
1214
#define HPET_REG_CONFIGURATION 0x10
@@ -31,10 +33,22 @@ extern "C" {
3133
#define HPET_REG_TIMER5_COMPARATOR 0x1A8
3234
#define HPET_REG_TIMER5_COUNTER 0x1B0
3335

34-
#define HPET_OK 0
36+
#define HPET_OK 0
3537
#define HPET_ERROR -1
38+
39+
#define HPET_GCR_OFFSET 0x10
40+
3641
typedef int hpet_status_t;
3742

43+
typedef struct hpet_table_t {
44+
acpi_sdt_header_t header; // Standard ACPI header
45+
uint32_t event_timer_block_id;
46+
acpi_address_t address; // Contains the base address of HPET
47+
uint8_t hpet_number;
48+
uint16_t minimum_tick;
49+
uint8_t page_protection;
50+
} __attribute__((packed)) hpet_table_t;
51+
3852
hpet_status_t hpet_timer_init();
3953

4054
#ifdef __cplusplus

include/kernel/idt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct __attribute__((packed)) {
3838
uint64_t offset; // Base address of the IDT
3939
} idt_pointer_t;
4040

41-
typedef struct {
41+
typedef struct idt_exception_t {
4242
uint64_t r15;
4343
uint64_t r14;
4444
uint64_t r13;

include/kernel/pmm.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ typedef struct pml4_map_t {
117117

118118
typedef pml4_map_t* pml4_map_p;
119119

120-
uintptr_t read_cr2();
121-
uintptr_t read_cr3();
120+
uintptr_t read_cr2();
121+
phys_addr_t read_cr3();
122122

123123
void debug_heap();
124124
void pmm_init();
@@ -135,4 +135,6 @@ phys_addr_t virtual_to_physical(virt_addr_t virtual_address);
135135

136136
void set_page_used(phys_addr_t physical_address);
137137

138+
virt_addr_t get_kernel_offset();
139+
138140
#endif

include/kernel/proc.h

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,94 @@ extern "C" {
1010
#define PROC_KERNEL 0
1111
#define PROC_MAX 16
1212

13-
#define PROC_STATE_IDLE 0
14-
#define PROC_STATE_RUNNING 1
13+
/*
14+
DISPATCH
15+
+------------+
16+
| |
17+
+------------+ +----------+-+ +--v---------+EXIT +------------+
18+
| NEW +-----> READY | | RUNNING +------> TERMINATED |
19+
+------------+ +--^-------^-+ +--+-------+-+ +------------+
20+
| | | |
21+
| | | |
22+
| +------------+ |
23+
| INTERRUPT |
24+
| |
25+
I/O COMPLETED| +------------+ |I/O WAIT
26+
+-------+ WAITING <-------+
27+
+------------+
28+
*/
29+
30+
#define PROC_STATE_NEW 0
31+
#define PROC_STATE_READY 1
32+
#define PROC_STATE_RUNNING 2
33+
#define PROC_STATE_WAITING 3
34+
#define PROC_STATE_TERMINATED 7
1535

1636
#define PROC_PRIO_NORMAL 3
1737

1838
#define PROC_PARENT_NONE 0
1939

2040
#define RLIMIT_DATA (4 * 1024 * 1024)
2141

22-
typedef struct {
23-
uintptr_t brk;
24-
uintptr_t heap;
25-
char* name;
26-
int parent_pid;
27-
int pid;
28-
uint8_t priority;
29-
uint8_t state;
42+
#define PROC_STACK_SIZE 1024
43+
44+
typedef struct cpu_state_t {
45+
uint64_t rax;
46+
uint64_t rbx;
47+
uint64_t rcx;
48+
uint64_t rdx;
49+
uint64_t rsi;
50+
uint64_t rdi;
51+
uint64_t rbp;
52+
uint64_t rsp;
53+
uint64_t r8;
54+
uint64_t r9;
55+
uint64_t r10;
56+
uint64_t r11;
57+
uint64_t r12;
58+
uint64_t r13;
59+
uint64_t r14;
60+
uint64_t r15;
61+
// phys_addr_t cr3;
62+
uint64_t rip;
63+
uint64_t cs;
64+
uint64_t rflags;
65+
uint64_t ss;
66+
} __attribute__((packed)) cpu_state_t;
67+
68+
typedef cpu_state_t* cpu_state_p;
69+
70+
typedef uint8_t proc_state_t;
71+
72+
typedef struct proc_info_t proc_info_t;
73+
typedef proc_info_t* proc_info_p;
74+
75+
typedef struct proc_info_t {
76+
proc_info_p parent;
77+
proc_info_p* children;
78+
uint32_t children_count;
79+
uint32_t pid;
80+
char name[32];
81+
uintptr_t brk;
82+
uintptr_t heap;
83+
uintptr_t* stack;
84+
proc_state_t state;
85+
uint8_t priority;
86+
cpu_state_t* cpu_state;
87+
void(*entrypoint)(void*);
88+
void* arguments;
89+
proc_info_p next;
3090
} proc_info_t;
3191

3292
typedef proc_info_t* proc_info_p;
3393

3494
extern proc_info_p procs[PROC_MAX];
3595
extern proc_info_p current;
3696

97+
void init_scheduler();
98+
void proc_create(proc_info_p proc, void (*entrypoint)(void *), void* arg);
99+
void proc_execute(proc_info_p proc);
100+
37101
#ifdef __cplusplus
38102
}
39103
#endif

include/kernel/thread.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef MENIOS_INCLUDE_KERNEL_THREAD_H
2+
#define MENIOS_INCLUDE_KERNEL_THREAD_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef struct kthread_t {
9+
10+
} kthread_t;
11+
12+
// typedef void *(*entrypoint)(void *) kthread_handler_t;
13+
14+
int kthread_create(kthread_t* thread, void (*entrypoint)(void *), void* arg);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif //MENIOS_INCLUDE_KERNEL_THREAD_H

include/kernel/timer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ void timer_eoi();
1212

1313
uint64_t unix_time();
1414

15+
void show_clock(void*);
16+
17+
void register_timer_callback(void (*cb)(void*));
18+
1519
#ifdef __cplusplus
1620
}
1721
#endif
1822

19-
#endif // MENIOS_INCLUDE_KERNEL_TIMER_H
23+
#endif // MENIOS_INCLUDE_KERNEL_TIMER_H

include/kernel/tsc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef MENIOS_INCLUDE_KERNEL_TSC_H
2+
#define MENIOS_INCLUDE_KERNEL_TSC_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <types.h>
9+
10+
void init_tsc();
11+
12+
uint64_t read_tsc(void);
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
18+
#endif // MENIOS_INCLUDE_KERNEL_TSC_H

0 commit comments

Comments
 (0)