Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ disk:

run: iso disk
@if [ -n "$$DISPLAY" ] || [ -n "$$WAYLAND_DISPLAY" ]; then \
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk; \
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk -m 512M; \
else \
echo "No GUI display detected, falling back to nographic mode."; \
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk -nographic -serial mon:stdio; \
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk -nographic -serial mon:stdio -m 512M; \
fi

run-nographic: iso disk
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk -nographic -serial mon:stdio
qemu-system-i386 -cdrom $(ISO) -drive file=$(DATA_DISK),format=raw,if=ide,index=0,media=disk -nographic -serial mon:stdio -m 512M

clean:
rm -rf build
Expand Down
7,010 changes: 7,010 additions & 0 deletions qemu.log

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/arch/gdt.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gdt.h"

static struct gdt_entry gdt[3];
static struct gdt_entry gdt[6];
static struct gdt_ptr gdtp;

static void gdt_set_gate(int n, uint32_t base, uint32_t limit,
Expand All @@ -20,6 +20,8 @@ void gdt_init() {
gdt_set_gate(0, 0, 0, 0x00, 0x00);
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);

__asm__ volatile(
"lgdt %0\n\t"
Expand Down
90 changes: 79 additions & 11 deletions src/arch/idt.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <stdint.h>
#include "../kernel.h"
#include "idt.h"

/* =========================
IDT STRUCTURES
Expand Down Expand Up @@ -29,14 +31,6 @@ extern void irq1_handler();
extern void timer_stub();
extern void irq12_mouse_stub();

/* =========================
PORT I/O (PIC)
========================= */

static inline void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}

/* =========================
IDT SETUP
========================= */
Expand All @@ -54,9 +48,72 @@ void idt_set_gate(int num, uint32_t base, uint16_t sel, uint8_t flags) {
DEFAULT INTERRUPT HANDLER
========================= */

void isr_handler() {
typedef struct {
uint32_t ds;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t int_no, err_code;
uint32_t eip, cs, eflags, useresp, ss;
} __attribute__((packed)) registers_t;

static const char *exception_messages[] = {
"Division By Zero", // 0
"Debug", // 1
"Non Maskable Interrupt", // 2
"Breakpoint", // 3
"Into Detected Overflow", // 4
"Out of Bounds", // 5
"Invalid Opcode", // 6
"No Coprocessor", // 7
"Double Fault", // 8
"Coprocessor Segment Overrun", // 9
"Bad TSS", // 10
"Segment Not Present", // 11
"Stack Fault", // 12
"General Protection Fault (#GP)", // 13
"Page Fault (#PF)", // 14
"Unknown Interrupt", // 15
"Coprocessor Fault", // 16
"Alignment Check", // 17
"Machine Check", // 18
// 19-31 are reserved
};

void kernel_panic(registers_t *regs) {
const char *reason;

if (regs->int_no < 19) {
reason = exception_messages[regs->int_no];
} else if (regs->int_no < 32) {
reason = "Reserved Exception";
} else {
reason = "User/Hardware Interrupt";
}

set_console_bg_color(0xFF0000);
set_console_fg_color(0x000000);
console_clear();
console_writefln("\n================ KERNEL PANIC ================");
console_writefln("Reason: %s (INT %d / 0x%x)", reason, regs->int_no, regs->int_no);
console_writefln("Err Code: 0x%x", regs->err_code);
console_writefln("EIP: 0x%x", regs->eip);

if (regs->int_no == 14) {
uint32_t cr2;
asm volatile("mov %%cr2, %0" : "=r"(cr2));
console_writefln("Fault Address (CR2): 0x%x", cr2);
}
console_writefln("ESP: %x EBP: %x ESI: %x EDI: %x", regs->esp, regs->ebp, regs->esi, regs->edi);
console_writefln("EAX: %x EBX: %x EDX: %x ECX: %x", regs->eax, regs->ebx, regs->edx, regs->ecx);
console_writefln("==============================================");

while (1) {
__asm__("hlt");
asm volatile("cli; hlt");
}
}

void isr_handler(registers_t *regs) {
if (regs->int_no < 32) {
kernel_panic(regs);
}
}

Expand All @@ -81,14 +138,25 @@ void pic_remap() {
IDT INIT
========================= */

void* isr_stub_table[32] = {
isr0, isr1, isr2, isr3, isr4, isr5, isr6, isr7,
isr8, isr9, isr10, isr11, isr12, isr13, isr14, isr15,
isr16, isr17, isr18, isr19, isr20, isr21, isr22, isr23,
isr24, isr25, isr26, isr27, isr28, isr29, isr30, isr31
};

extern void timer_stub(void);

void idt_init() {
idtp.limit = sizeof(struct idt_entry) * 256 - 1;
idtp.base = (uint32_t)&idt;
pic_remap();

for (int i = 0; i < 256; i++) {
for (int i = 0; i < 32; i++) {
idt_set_gate(i, (uint32_t)isr_stub_table[i], 0x08, 0x8E);
}

for (int i = 32; i < 256; i++) {
idt_set_gate(i, (uint32_t)isr_handler, 0x08, 0x8E);
}

Expand Down
34 changes: 34 additions & 0 deletions src/arch/idt.h
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
//isr stubs
extern void isr0(void);
extern void isr1(void);
extern void isr2(void);
extern void isr3(void);
extern void isr4(void);
extern void isr5(void);
extern void isr6(void);
extern void isr7(void);
extern void isr8(void);
extern void isr9(void);
extern void isr10(void);
extern void isr11(void);
extern void isr12(void);
extern void isr13(void);
extern void isr14(void);
extern void isr15(void);
extern void isr16(void);
extern void isr17(void);
extern void isr18(void);
extern void isr19(void);
extern void isr20(void);
extern void isr21(void);
extern void isr22(void);
extern void isr23(void);
extern void isr24(void);
extern void isr25(void);
extern void isr26(void);
extern void isr27(void);
extern void isr28(void);
extern void isr29(void);
extern void isr30(void);
extern void isr31(void);

void idt_init();
73 changes: 73 additions & 0 deletions src/arch/idt_isr_stub.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[EXTERN isr_handler]

%macro ISR_NOERRCODE 1
global isr%1
isr%1:
cli
push dword 0
push dword %1
jmp isr_common_stub
%endmacro

%macro ISR_ERRCODE 1
global isr%1
isr%1:
cli
push dword %1
jmp isr_common_stub
%endmacro

isr_common_stub:
pusha
mov ax, ds
push eax
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
push esp
call isr_handler
add esp, 4
pop eax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa
add esp, 8
sti
iret

ISR_NOERRCODE 0
ISR_NOERRCODE 1
ISR_NOERRCODE 2
ISR_NOERRCODE 3
ISR_NOERRCODE 4
ISR_NOERRCODE 5
ISR_NOERRCODE 6
ISR_NOERRCODE 7
ISR_ERRCODE 8 ; Double Fault has an error code
ISR_NOERRCODE 9
ISR_ERRCODE 10 ; Invalid TSS
ISR_ERRCODE 11 ; Segment Not Present
ISR_ERRCODE 12 ; Stack Fault
ISR_ERRCODE 13 ; General Protection Fault (#GP)
ISR_ERRCODE 14 ; Page Fault (#PF)
ISR_NOERRCODE 15
ISR_NOERRCODE 16
ISR_ERRCODE 17 ; Alignment Check
ISR_NOERRCODE 18
ISR_NOERRCODE 19
ISR_NOERRCODE 20
ISR_NOERRCODE 21
ISR_NOERRCODE 22
ISR_NOERRCODE 23
ISR_NOERRCODE 24
ISR_NOERRCODE 25
ISR_NOERRCODE 26
ISR_NOERRCODE 27
ISR_NOERRCODE 28
ISR_NOERRCODE 29
ISR_ERRCODE 30 ; Security Exception
ISR_NOERRCODE 31
43 changes: 43 additions & 0 deletions src/arch/logging/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdint.h>
#include "../../libc/main.h"
#include "../../kernel.h"
#include "../../multitasking/task.h"

#define ENTRIES 100

typedef struct {
char log[128];
int task;
char name[16]; //that's the [XXX] thing so it can be like [SHELL] or [PCI] or smth
}log_t;

log_t logs[100];

int tail = 0;
int head = 0;
int first_log = 1;

void log_init() {
memset(logs, 0, sizeof(logs));
}

void log(char* string, char* name) {
if (first_log == 0) {
head = (head + 1) % (ENTRIES - 1);
}
strncpy(logs[head].log, string, strlen(string));
logs[head].task = get_current_task();
strncpy(logs[head].name, name, strlen(name));
if (!first_log && head == tail) {
tail++;
}
first_log = 0;
}

void log_print() {
int i = head + 1;
while (i != tail) {
console_writefln("[%s][%d] %s", logs[i - 1].name, logs[i - 1].task, logs[i - 1].log);
i = (i - 1) % (ENTRIES - 1);
}
}
Loading