Skip to content

Commit c64fadb

Browse files
committed
Fix many bugé
1 parent 95a1159 commit c64fadb

File tree

20 files changed

+213
-173
lines changed

20 files changed

+213
-173
lines changed

kernel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ add_executable(${target}
6969
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/log.c
7070
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/mutex.c
7171
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/num_to_str.c
72+
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/panic.c
7273
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/rawprint.c
7374
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/semaphore.c
7475
${CMAKE_CURRENT_LIST_DIR}/src/badgelib/spinlock.c

kernel/cpu/amd64/src/isr.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ void amd64_trap_handler(size_t trapno, size_t error_code) {
9898
return;
9999
}
100100

101+
// Unhandled trap.
102+
claim_panic();
101103
rawprint("\033[0m");
102104
if (fault3) {
103105
rawprint("**** TRIPLE FAULT ****\n");
@@ -163,15 +165,7 @@ void amd64_trap_handler(size_t trapno, size_t error_code) {
163165
backtrace_from_ptr(kctx->frameptr);
164166

165167
isr_ctx_dump(kctx);
166-
167-
if ((kctx->regs.cs & 3) == 0 || fault2) {
168-
// When the kernel traps it's a bad time.
169-
panic_poweroff();
170-
} else {
171-
// When the user traps just stop the process.
172-
sched_raise_from_isr(kctx->thread, false, kill_proc_on_trap);
173-
}
174-
isr_ctx_swap(kctx);
168+
panic_poweroff_unchecked();
175169
}
176170

177171
// Return a value from the syscall handler.

kernel/cpu/amd64/src/panic.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,7 @@
33

44
#include "panic.h"
55

6-
#include "backtrace.h"
7-
#include "interrupt.h"
8-
#include "isr_ctx.h"
9-
#include "log.h"
10-
#include "rawprint.h"
11-
12-
void abort() {
13-
panic_abort();
14-
}
15-
16-
// Call this function when and only when the kernel has encountered a fatal error.
17-
// Prints register dump for current kernel context and jumps to `panic_poweroff`.
18-
void panic_abort() {
19-
irq_disable();
20-
logkf_from_isr(LOG_FATAL, "`panic_abort()` called!");
21-
backtrace();
22-
kernel_cur_regs_dump();
23-
panic_poweroff();
24-
}
25-
26-
// Call this function when and only when the kernel has encountered a fatal error.
27-
// Immediately power off or reset the system.
28-
void panic_poweroff() {
29-
rawprint("**** KERNEL PANIC ****\nhalted\n");
30-
// TODO: Disable interrupts.
6+
void cpu_panic_poweroff() {
7+
asm volatile("cli");
318
while (1) asm volatile("hlt");
329
}

kernel/cpu/amd64/src/scheduler.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ bool sched_signal_exit() {
155155

156156
// Return to exit the thread.
157157
static void sched_exit_self(int code) {
158-
#ifndef NDEBUG
159-
sched_thread_t *const thread = sched_current_thread();
160-
logkf(LOG_DEBUG, "Kernel thread '%{cs}' returned %{d}", thread->name, code);
161-
#endif
162158
thread_exit(code);
163159
}
164160

kernel/cpu/riscv/src/interrupt/riscv_plic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void plic_dtb_init(dtb_handle_t *dtb, dtb_node_t *node, uint32_t addr_cel
135135
for (uint16_t i = 0; i < plic_ctx_count; i++) {
136136
if (plic_ctx[i].irq == RISCV_INT_SUPERVISOR_EXT) {
137137
plic_smp_ctx[smp_get_cpu(plic_ctx[i].hartid)] = i;
138-
logkf(LOG_DEBUG, "CPU%{d} PLIC ctx is %{d}", smp_get_cpu(plic_ctx[i].hartid), i);
138+
// logkf(LOG_DEBUG, "CPU%{d} PLIC ctx is %{d}", smp_get_cpu(plic_ctx[i].hartid), i);
139139
}
140140
}
141141

kernel/cpu/riscv/src/isr.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ void riscv_trap_handler() {
132132
}
133133

134134
// Unhandled trap.
135+
claim_panic();
135136
rawprint("\033[0m");
136137
if (fault3) {
137138
rawprint("**** TRIPLE FAULT ****\n");
@@ -208,15 +209,7 @@ void riscv_trap_handler() {
208209
backtrace_from_ptr(kctx->frameptr);
209210

210211
isr_ctx_dump(kctx);
211-
212-
if ((status & (CSR_STATUS_PP_MASK << CSR_STATUS_PP_BASE_BIT)) || fault2) {
213-
// When the kernel traps it's a bad time.
214-
panic_poweroff();
215-
} else {
216-
// When the user traps just stop the process.
217-
sched_raise_from_isr(kctx->thread, false, kill_proc_on_trap);
218-
}
219-
isr_ctx_swap(kctx);
212+
panic_poweroff_unchecked();
220213
}
221214

222215
// Return a value from the syscall handler.

kernel/cpu/riscv/src/panic.c

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,9 @@
33

44
#include "panic.h"
55

6-
#include "backtrace.h"
7-
#include "interrupt.h"
8-
#include "isr_ctx.h"
9-
#include "log.h"
10-
#include "rawprint.h"
6+
#include "cpu/regs.h"
117

12-
void abort() {
13-
panic_abort();
14-
}
15-
16-
static void kekw() {
17-
// clang-format off
18-
char const *const msg =
19-
"======+++++++***************####**++++++========" "\n"
20-
"=--:::----:-==++*****+++++==++++*+====---=======" "\n"
21-
"-::........::-==++++++===--:::.:::::::-=========" "\n"
22-
":::----=---:::-====++===--:::...::-=============" "\n"
23-
"--==+++++=+++=::--==+++=----==+++++***+++=======" "\n"
24-
":. :----======+#*++===-===---:.:::::--=====" "\n"
25-
"=----===+++++======+**++++=====--::-===---------" "\n"
26-
"==----:-==========++++++++++++====++++**++====++" "\n"
27-
"========+++========+++++++++++++++=======+++=+++" "\n"
28-
"=====++++++========++++====+++***++++++**#*+++++" "\n"
29-
"=====++++++=======++====-=====+*##******##*+++++" "\n"
30-
"===+++++=======+++**+==-=========*#######*++++++" "\n"
31-
"=========-===---========+++=--=*+==+****++++++++" "\n"
32-
"---====--==:...:----::. .::::=========+++======" "\n"
33-
"-------:--:..........:::::::::::::-=--==========" "\n"
34-
"--------:. .. ....:-:. .::...:::..::----========" "\n"
35-
"-------:...........--....::...::...::::---======" "\n"
36-
"------:. .........-===:.:::...:::......:---=====" "\n"
37-
"-----=-. .... .. ..........:::::::. :--====" "\n"
38-
"------=-::-...+##= ::-:-=====" "\n"
39-
"::::--====-=+: :::......:--=----:.-----====-" "\n"
40-
".::----==--=+=--=+++**********++==---===--===---" "\n"
41-
".:-:-=--===-==--=+****++++++++++=--=*===-====---" "\n"
42-
"..:-:==-======---=++++++++====---===+========---" "\n"
43-
"..:---==-=====---==========+#*=--==+++=======--=" "\n"
44-
"...--:=+===---============++++=====++=======---=" "\n";
45-
// clang-format on
46-
// c9 8d 74
47-
rawprint("\033[38;2;201;141;116m\n\n");
48-
rawprint(msg);
49-
rawprint("\033[0m\n\n");
50-
}
51-
52-
// Call this function when and only when the kernel has encountered a fatal error.
53-
// Prints register dump for current kernel context and jumps to `panic_poweroff`.
54-
void panic_abort() {
55-
irq_disable();
56-
logkf_from_isr(LOG_FATAL, "`panic_abort()` called!");
57-
backtrace();
58-
kernel_cur_regs_dump();
59-
panic_poweroff();
60-
}
61-
62-
// Call this function when and only when the kernel has encountered a fatal error.
63-
// Immediately power off or reset the system.
64-
void panic_poweroff() {
65-
rawprint("**** KERNEL PANIC ****\nhalted\n");
66-
kekw();
8+
void cpu_panic_poweroff() {
679
asm volatile("csrci " CSR_STATUS_STR ", %0" ::"ri"(1 << CSR_STATUS_IE_BIT));
6810
while (1) asm volatile("wfi");
6911
}

kernel/cpu/riscv/src/scheduler.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,6 @@ bool sched_signal_exit() {
195195

196196
// Return to exit the thread.
197197
static void sched_exit_self(int code) {
198-
#ifndef NDEBUG
199-
sched_thread_t *const thread = sched_current_thread();
200-
logkf(LOG_DEBUG, "Kernel thread '%{cs}' returned %{d}", thread->name, code);
201-
#endif
202198
thread_exit(code);
203199
}
204200

kernel/include/badgelib/panic.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// SPDX-License-Identifier: MIT
3+
4+
#pragma once
5+
6+
#include "attributes.h"
7+
8+
// Try to atomically claim the panic flag.
9+
// Only ever call this if a subsequent call to `panic_abort_unchecked` or `panic_poweroff_unchecked` is imminent.
10+
void claim_panic();
11+
12+
// Like `panic_abort`, but does not check the panic flag.
13+
void panic_abort_unchecked() NORETURN;
14+
// Like `panic_poweroff`, but does not check the panic flag.
15+
void panic_poweroff_unchecked() NORETURN;
16+
17+
// Call this function when and only when the kernel has encountered a fatal error.
18+
// Prints register dump for current kernel context and jumps to `panic_poweroff`.
19+
void panic_abort() NORETURN;
20+
// Call this function when and only when the kernel has encountered a fatal error.
21+
// Immediately power off or reset the system.
22+
void panic_poweroff() NORETURN;
23+
// Check for a panic and immediately halt if it has happened.
24+
void check_for_panic();

kernel/include/panic.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

kernel/port/generic/gdbinit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ end
1111
mon system_reset
1212
maintenance flush register-cache
1313
thb basic_runtime_init
14-
b panic_poweroff
14+
b panic_poweroff_unchecked
1515
c

kernel/port/generic/include/port/port.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
void port_early_init();
1212
// Post-heap hardware initialization.
1313
void port_postheap_init();
14+
// Reclaim bootloader memory.
15+
void port_reclaim_mem();
1416
// Full hardware initialization.
1517
void port_init();
1618
// Power off.

kernel/port/generic/port_riscv64.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ clean-image:
6868
.PHONY: qemu
6969
qemu: $(BUILDDIR)/cache/OVMF.fd image
7070
$(QEMU) -s \
71-
-M virt,acpi=off -cpu rv64,sv48=false -smp 2 -m 4G \
71+
-M virt,acpi=off -cpu rv64,sv48=false -smp 4 -m 4G \
7272
-device pcie-root-port,bus=pcie.0,id=pcisw0 \
7373
-device qemu-xhci,bus=pcisw0 -device usb-kbd \
7474
-drive if=pflash,unit=0,format=raw,file=$(BUILDDIR)/cache/OVMF.fd \

kernel/port/generic/src/port.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void port_early_init() {
121121
[LIMINE_MEMMAP_KERNEL_AND_MODULES] = "Kernel",
122122
[LIMINE_MEMMAP_FRAMEBUFFER] = "Framebuffer",
123123
};
124-
logkf_from_isr(LOG_DEBUG, "Memory map:");
124+
// logkf_from_isr(LOG_DEBUG, "Memory map:");
125125
size_t kernel_len = 0;
126126
size_t biggest_pool_size = 0;
127127
size_t biggest_pool_index = 0;
@@ -130,13 +130,13 @@ void port_early_init() {
130130
size_t reclaim_len = 0;
131131
for (uint64_t i = 0; i < mem->entry_count; i++) {
132132
struct limine_memmap_entry *entry = mem->entries[i];
133-
logkf_from_isr(
134-
LOG_DEBUG,
135-
"%{u64;x}-%{u64;x} %{cs}",
136-
entry->base,
137-
entry->base + entry->length - 1,
138-
types[entry->type]
139-
);
133+
// logkf_from_isr(
134+
// LOG_DEBUG,
135+
// "%{u64;x}-%{u64;x} %{cs}",
136+
// entry->base,
137+
// entry->base + entry->length - 1,
138+
// types[entry->type]
139+
// );
140140
if (entry->type == LIMINE_MEMMAP_USABLE) {
141141
usable_len += entry->length;
142142
} else if (entry->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE ||
@@ -208,7 +208,7 @@ void port_init() {
208208
#ifdef PORT_ENABLE_DTB
209209
if (dtb_req.response) {
210210
// Parse and process DTB.
211-
dtdump(dtb_req.response->dtb_ptr);
211+
// dtdump(dtb_req.response->dtb_ptr);
212212
dtparse(dtb_req.response->dtb_ptr);
213213
} else
214214
#endif
@@ -225,6 +225,12 @@ void port_init() {
225225
#endif
226226
}
227227

228+
// Enumerate PCIe devices.
229+
// pcie_ecam_detect();
230+
}
231+
232+
// Reclaim bootloader memory.
233+
void port_reclaim_mem() {
228234
// Reclaim all reclaimable memory.
229235
size_t base = 0;
230236
size_t len = 0;
@@ -237,7 +243,7 @@ void port_init() {
237243
}
238244
if (entry->base != base + len) {
239245
if (len > 16 * MEMMAP_PAGE_SIZE) {
240-
logkf_from_isr(LOG_DEBUG, "Adding memory at 0x%{size;x}-0x%{size;x}", base, base + len - 1);
246+
// logkf_from_isr(LOG_DEBUG, "Adding memory at 0x%{size;x}-0x%{size;x}", base, base + len - 1);
241247
init_pool((void *)(base + mmu_hhdm_vaddr), (void *)(base + len + mmu_hhdm_vaddr), 0);
242248
}
243249
base = entry->base;
@@ -247,16 +253,19 @@ void port_init() {
247253
}
248254
}
249255
if (len >= 16 * MEMMAP_PAGE_SIZE) {
250-
logkf_from_isr(LOG_DEBUG, "Adding memory at 0x%{size;x}-0x%{size;x}", base, base + len - 1);
256+
// logkf_from_isr(LOG_DEBUG, "Adding memory at 0x%{size;x}-0x%{size;x}", base, base + len - 1);
251257
init_pool((void *)(base + mmu_hhdm_vaddr), (void *)(base + len + mmu_hhdm_vaddr), 0);
252258
}
253-
254-
// Enumerate PCIe devices.
255-
pcie_ecam_detect();
256259
}
257260

258261
// Send a single character to the log output.
259262
void port_putc(char msg) {
263+
// Artificial delay.
264+
timestamp_us_t lim = time_us();
265+
if (lim) {
266+
lim += 100;
267+
while (time_us() < lim);
268+
}
260269
// TODO: More proper way to do this.
261270
#ifdef __x86_64__
262271
outb(0x3f8, msg);

0 commit comments

Comments
 (0)