Skip to content

Commit c3c37cc

Browse files
tidy up kernel.c
1 parent 2ba3599 commit c3c37cc

17 files changed

+163
-132
lines changed

asm/loader.S

+79-54
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ exception_gate_loop:
4646
dec %rcx
4747
jnz exception_gate_loop
4848

49-
mov $(51-32), %rcx
49+
mov $(64-32), %rcx
5050
interrupt_gate_loop:
5151
mov (%r10), %rax // Get the gate jump-table address in r10 again
5252
add $8, %r10
@@ -139,58 +139,71 @@ interrupt_gate_loop:
139139
pop %r15
140140
.endm
141141

142-
143-
ISR_NOERR 0
144-
ISR_NOERR 1
145-
ISR_NOERR 2
146-
ISR_NOERR 3
147-
ISR_NOERR 4
148-
ISR_NOERR 5
149-
ISR_NOERR 6
150-
ISR_NOERR 7
151-
ISR_ERR 8
152-
ISR_NOERR 9
153-
ISR_ERR 10
154-
ISR_ERR 11
155-
ISR_ERR 12
156-
ISR_ERR 13
157-
ISR_ERR 14
158-
ISR_NOERR 15
159-
ISR_NOERR 16
160-
ISR_NOERR 17
161-
ISR_NOERR 18
162-
ISR_NOERR 19
163-
ISR_NOERR 20
164-
ISR_NOERR 21
165-
ISR_NOERR 22
166-
ISR_NOERR 23
167-
ISR_NOERR 24
168-
ISR_NOERR 25
169-
ISR_NOERR 26
170-
ISR_NOERR 27
171-
ISR_NOERR 28
172-
ISR_NOERR 29
173-
ISR_NOERR 30
174-
ISR_NOERR 31
175-
IRQ 0, 32
176-
IRQ 1, 33
177-
IRQ 2, 34
178-
IRQ 3, 35
179-
IRQ 4, 36
180-
IRQ 5, 37
181-
IRQ 6, 38
182-
IRQ 7, 39
183-
IRQ 8, 40
184-
IRQ 9, 41
185-
IRQ 10, 42
186-
IRQ 11, 43
187-
IRQ 12, 44
188-
IRQ 13, 45
189-
IRQ 14, 46
190-
IRQ 15, 47
191-
ISR_NOERR 48
192-
ISR_NOERR 49
193-
IRQ 16, 50
142+
isrs:
143+
ISR_NOERR 0
144+
ISR_NOERR 1
145+
ISR_NOERR 2
146+
ISR_NOERR 3
147+
ISR_NOERR 4
148+
ISR_NOERR 5
149+
ISR_NOERR 6
150+
ISR_NOERR 7
151+
ISR_ERR 8
152+
ISR_NOERR 9
153+
ISR_ERR 10
154+
ISR_ERR 11
155+
ISR_ERR 12
156+
ISR_ERR 13
157+
ISR_ERR 14
158+
ISR_NOERR 15
159+
ISR_NOERR 16
160+
ISR_NOERR 17
161+
ISR_NOERR 18
162+
ISR_NOERR 19
163+
ISR_NOERR 20
164+
ISR_NOERR 21
165+
ISR_NOERR 22
166+
ISR_NOERR 23
167+
ISR_NOERR 24
168+
ISR_NOERR 25
169+
ISR_NOERR 26
170+
ISR_NOERR 27
171+
ISR_NOERR 28
172+
ISR_NOERR 29
173+
ISR_NOERR 30
174+
ISR_NOERR 31
175+
IRQ 0, 32
176+
IRQ 1, 33
177+
IRQ 2, 34
178+
IRQ 3, 35
179+
IRQ 4, 36
180+
IRQ 5, 37
181+
IRQ 6, 38
182+
IRQ 7, 39
183+
IRQ 8, 40
184+
IRQ 9, 41
185+
IRQ 10, 42
186+
IRQ 11, 43
187+
IRQ 12, 44
188+
IRQ 13, 45
189+
IRQ 14, 46
190+
IRQ 15, 47
191+
ISR_NOERR 48
192+
ISR_NOERR 49
193+
IRQ 16, 50
194+
IRQ 17, 51
195+
IRQ 18, 52
196+
IRQ 19, 53
197+
IRQ 20, 54
198+
IRQ 21, 55
199+
IRQ 22, 56
200+
IRQ 23, 57
201+
IRQ 24, 58
202+
IRQ 25, 59
203+
IRQ 26, 60
204+
IRQ 27, 61
205+
IRQ 28, 62
206+
IRQ 29, 63
194207

195208
irq_stub:
196209
call IRQ
@@ -254,7 +267,19 @@ exception_handlers:
254267
.quad isr48
255268
.quad isr49
256269
.quad irq16
257-
270+
.quad irq17
271+
.quad irq18
272+
.quad irq19
273+
.quad irq20
274+
.quad irq21
275+
.quad irq22
276+
.quad irq23
277+
.quad irq24
278+
.quad irq25
279+
.quad irq26
280+
.quad irq27
281+
.quad irq28
282+
.quad irq29
258283

259284
.data
260285

asm/sse.asm

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@ bits 64
33
section .text
44

55
global enable_sse
6+
global enable_fpu
7+
8+
9+
enable_fpu:
10+
mov rax, cr0
11+
bts rax, 1
12+
btr rax, 2
13+
bts rax, 5
14+
btr rax, 3
15+
mov cr0, rax
16+
fninit
17+
ret
18+
19+
620

721
enable_sse:
822
mov eax, 0x1
923
cpuid
1024
test edx, 1<<25
1125
jz no_sse
1226
;SSE is available
13-
mov rax, cr0
14-
and ax, 0xFFFB ;clear coprocessor emulation CR0.EM
15-
or ax, 0x2 ;set coprocessor monitoring CR0.MP
16-
mov cr0, rax
1727
mov rax, cr4
18-
or ax, 3 << 9 ;set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time
28+
bts rax, 9
29+
bts rax, 10
1930
mov cr4, rax
2031
no_sse:
2132
ret

include/fpu.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
/**
66
* @brief Enable floating point operations
77
*/
8-
static inline void fninit()
9-
{
10-
asm volatile("fninit");
11-
}
8+
extern void enable_fpu();
129

1310
/**
1411
* @brief Enable SSE

include/limine-requests.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <limine.h>
3+
4+
volatile struct limine_stack_size_request stack_size_request = {
5+
.id = LIMINE_STACK_SIZE_REQUEST,
6+
.revision = 0,
7+
.stack_size = (1024 * 1024 * 32),
8+
};
9+
10+
volatile struct limine_hhdm_request hhdm_request = {
11+
.id = LIMINE_HHDM_REQUEST,
12+
.revision = 0,
13+
};
14+

include/net.h

+4
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,7 @@ uint16_t ntohs(uint16_t netshort);
106106
* @return uint32_t host order
107107
*/
108108
uint32_t ntohl(uint32_t netlong);
109+
110+
void network_up();
111+
112+
void network_down();

include/taskswitch.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef __TASKSWITCH_H__
2-
#define __TASKSWITCH_H__
1+
#pragma once
32

43
#define PROC_RUNNING 0
54
#define PROC_IDLE 1
@@ -70,4 +69,5 @@ uint32_t proc_id(int64_t index);
7069
*/
7170
void proc_register_idle(proc_idle_timer_t handler, idle_type_t type);
7271

73-
#endif
72+
void proc_init();
73+

include/video.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void dputstring(char* message);
5959
void put(console* c, const char n);
6060
void dput(const char n);
6161

62-
void initconsole(console* c);
62+
void initconsole();
6363

6464
void setbackground(console* c, unsigned char background);
6565

src/ap.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <kernel.h>
2+
3+
void kmain_ap()
4+
{
5+
wait_forever();
6+
}

src/clock.c

+1
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@ const char* get_datetime_str() {
120120

121121
void clock_init() {
122122
rtc_read_datetime();
123+
kprintf("System boot time: %s\n", get_datetime_str());
123124
}

src/errorhandler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void error_handler(uint8_t int_no, uint64_t errorcode, uint64_t irq_no)
3333
"Alignment check exception",
3434
"Machine check exception",
3535
};
36-
asm volatile("cli");
36+
interrupts_off();
3737
PANIC_BANNER;
3838
setforeground(current_console, COLOUR_LIGHTRED);
3939
kprintf("Fatal exception %02X: %s\n", int_no, error_table[int_no]);

src/idt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ void idt_setup()
2222
}
2323

2424
/* Now we are safe to enable interrupts */
25-
asm volatile("sti");
25+
interrupts_on();
2626
}

src/kernel.c

+4-56
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,17 @@
11
#include <kernel.h>
2-
3-
console first_console;
4-
console* current_console = NULL;
5-
uint8_t cpunum = 1;
6-
7-
volatile struct limine_stack_size_request stack_size_request = {
8-
.id = LIMINE_STACK_SIZE_REQUEST,
9-
.revision = 0,
10-
.stack_size = (1024 * 1024 * 32),
11-
};
12-
13-
volatile struct limine_hhdm_request hhdm_request = {
14-
.id = LIMINE_HHDM_REQUEST,
15-
.revision = 0,
16-
};
17-
18-
void kmain_ap()
19-
{
20-
while (1) asm volatile("hlt");
21-
}
22-
23-
void network_up()
24-
{
25-
arp_init();
26-
ip_init();
27-
tcp_init();
28-
dhcp_discover();
29-
init_dns();
30-
}
31-
32-
void network_down()
33-
{
34-
}
2+
#include <limine-requests.h>
353

364
void kmain()
375
{
38-
current_console = &first_console;
39-
initconsole(current_console);
40-
41-
/* NB: Can't use kmalloc/kfree until heap_init is called.
42-
* This depends upon paging.
43-
*/
6+
enable_fpu();
7+
initconsole();
448
heap_init();
459
detect_cores();
4610
idt_setup();
4711
init_error_handler();
48-
fninit();
4912
init_pci();
50-
5113
clock_init();
5214
init_lapic_timer(50);
53-
54-
5515
init_devicenames();
5616
init_basic_keyboard();
5717
ide_initialise();
@@ -69,19 +29,7 @@ void kmain()
6929

7030
init_debug();
7131

72-
//////
73-
7432
rtl8139_init();
7533

76-
kprintf("System boot time: %s\n", get_datetime_str());
77-
kprintf("Loading initial process...\n");
78-
79-
struct process* init = proc_load("/programs/init", (struct console*)current_console);
80-
if (!init) {
81-
kprintf("/programs/init missing!\n");
82-
}
83-
84-
kprintf("Launching /programs/init...\n");
85-
86-
proc_loop();
34+
proc_init();
8735
}

src/net.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ uint32_t ntohl(uint32_t netlong) {
8989
return netlong;
9090
}
9191

92-
#endif
92+
#endif
93+
94+
void network_up()
95+
{
96+
arp_init();
97+
ip_init();
98+
tcp_init();
99+
dhcp_discover();
100+
init_dns();
101+
}
102+
103+
void network_down()
104+
{
105+
}

src/pci.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ bool pci_enable_msi(pci_dev_t device, uint32_t vector, bool edgetrigger, bool de
445445
uint32_t next_capability = (config_space & 0xFF00) >> 8;
446446
if (id == PCI_CAPABILITY_MSI) {
447447
/* MSI capability */
448-
asm volatile("cli");
448+
interrupts_off();
449449
uint32_t new_message_data = (vector & 0xFF) | (edgetrigger ? 0 : PCI_MSI_EDGETRIGGER) | (deassert ? 0 : PCI_MSI_DEASSERT);
450450
uint32_t new_message_address = (0xFEE00000 | (cpu_id() << 12));
451451
bool bits64cap = (config_space & PCI_MSI_64BIT);
@@ -463,7 +463,7 @@ bool pci_enable_msi(pci_dev_t device, uint32_t vector, bool edgetrigger, bool de
463463
pci_write(device, current + 0x08, new_message_data);
464464
}
465465
pci_write(device, current + 0x00, config_space | PCI_MSI_ENABLE); // Mask in enable bit
466-
asm volatile("sti");
466+
interrupts_on();
467467
return true;
468468
}
469469
current = next_capability;

0 commit comments

Comments
 (0)