From 17c25e48e72b5578fe7cdc916fd1b6f26a6df3cd Mon Sep 17 00:00:00 2001
From: =?utf8?q?Duje=20Mihanovi=C4=87?= <duje.mihanovic@skole.hr>
Date: Tue, 21 Jun 2022 16:04:59 +0200
Subject: [PATCH] Add panic facility

---
 include/arch/x86/panic.h             | 24 ++++++++++++++++++++++++
 include/arch/x86/tty.h               |  2 +-
 kernel/arch/x86/irq/sample_handler.c | 13 ++++---------
 kernel/arch/x86/tty/tty.c            |  4 ++--
 kernel/kernel.c                      |  4 ----
 5 files changed, 31 insertions(+), 16 deletions(-)
 create mode 100644 include/arch/x86/panic.h

diff --git a/include/arch/x86/panic.h b/include/arch/x86/panic.h
new file mode 100644
index 0000000..c66d6c7
--- /dev/null
+++ b/include/arch/x86/panic.h
@@ -0,0 +1,24 @@
+#ifndef PANIC_H
+#define PANIC_H
+
+void halt()
+{
+	asm("cli":);
+loop:
+	asm("hlt":);
+	goto loop;
+}
+
+#define PANIC(msg) \
+	kprint("PANIC (", VGA_COLOR_BRIGHT_RED);\
+	kprint(msg, VGA_COLOR_BRIGHT_RED);\
+	kprint(") in ", VGA_COLOR_BRIGHT_RED);\
+	kprint(__FILE__, VGA_COLOR_BRIGHT_RED);\
+	kprintc(':', VGA_COLOR_BRIGHT_RED);\
+	kprintdec(__LINE__, VGA_COLOR_BRIGHT_RED);\
+	kprintc(':', VGA_COLOR_BRIGHT_RED);\
+	kprint(__func__, VGA_COLOR_BRIGHT_RED);\
+	kprint("()", VGA_COLOR_BRIGHT_RED);\
+	halt();
+
+#endif
diff --git a/include/arch/x86/tty.h b/include/arch/x86/tty.h
index e141069..f2049a2 100644
--- a/include/arch/x86/tty.h
+++ b/include/arch/x86/tty.h
@@ -27,5 +27,5 @@ extern void kprintb(const uint8_t byte);
 extern void kprintw(const uint16_t word);
 extern void kprintd(const uint32_t dword);
 extern void kprintq(const uint64_t qword);
-extern int kprintdec(uint32_t num);
+extern int kprintdec(uint32_t num, uint8_t color);
 #endif
diff --git a/kernel/arch/x86/irq/sample_handler.c b/kernel/arch/x86/irq/sample_handler.c
index 0ba0222..5b0e2d4 100644
--- a/kernel/arch/x86/irq/sample_handler.c
+++ b/kernel/arch/x86/irq/sample_handler.c
@@ -3,6 +3,7 @@
 #include <io.h>
 #include <stdint.h>
 #include <mm/paging.h>
+#include <panic.h>
 
 typedef uint32_t uword_t;
 
@@ -36,7 +37,7 @@ void pf_handler(struct fault_frame *frame)
 	int address;
 	struct pf_errcode errcode = frame->error_code;
 	asm ("mov %%cr2, %0": "=a" (address));
-	kprint("A page fault occurred!\n", VGA_COLOR_DARK_RED);
+	kprint("A page fault occurred!\n", VGA_COLOR_BRIGHT_RED);
 	kprint("Faulting address: ", 0);
 	kprintd(address);
 	kprint("\n", 0);
@@ -51,18 +52,12 @@ void pf_handler(struct fault_frame *frame)
 	if (errcode.id) {
 		kprint("Fault occurred while fetching instruction\n", 0);
 	}
-	asm("cli");
-halt:
-	asm("hlt");
-	goto halt;
+	PANIC("page fault");
 }
 
 __attribute__((interrupt))
 void double_fault(struct abort_frame *frame)
 {
-	*(volatile uint32_t *) (0xb8000) = 0xcf28cf3a;
-halt:
-	asm volatile ("cli; hlt");
-	goto halt;
+	PANIC("double fault");
 }
 
diff --git a/kernel/arch/x86/tty/tty.c b/kernel/arch/x86/tty/tty.c
index 861610d..c55cdd4 100644
--- a/kernel/arch/x86/tty/tty.c
+++ b/kernel/arch/x86/tty/tty.c
@@ -176,7 +176,7 @@ void kprintq(uint64_t qword)
 	kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
 }
 
-int kprintdec(uint32_t num)
+int kprintdec(uint32_t num, uint8_t color)
 {
 	char buffer[11];
 	int digits = 10;
@@ -207,6 +207,6 @@ int kprintdec(uint32_t num)
 		}
 	}
 
-	kprint(buffer, 0);
+	kprint(buffer, color);
 	return digits;
 }
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 16b2b40..9257a0e 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -59,9 +59,5 @@ void kmain(struct e820_map *mmap, int mmap_size)
 	pic_unmask(1);
 	asm volatile ("sti");
 	kprint("All done\n", 0);
-	kprintdec(12345);
-	kprintc('\n');
-	kprintdec(6789);
-	kprintc('\n');
 	while(1);
 }
-- 
2.39.5