A toy operating system written in C.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
freywaros/src/kernel/kernel.c

88 lines
2.7 KiB

#include "src/kernel/fs.h"
#include "src/kernel/gdt.h"
#include "src/kernel/idt.h"
#include "src/kernel/keyboard.h"
#include "src/kernel/panic.h"
#include "src/kernel/path.h"
#include "src/kernel/pic.h"
#include "src/kernel/process.h"
#include "src/kernel/syscall.h"
#include "src/kernel/tss.h"
#include "src/kernel/util.h"
#include "src/kernel/vga.h"
#include "src/lib/layout.h"
#include "src/lib/memory.h"
__attribute__((interrupt)) void isr_divide_by_zero(__attribute__((unused)) struct interrupt_frame *frame) {
vga_set_string(VGA_HEIGHT - 1, 0, "EXCEPTION: divide by zero", 0x4F);
while (1)
;
}
__attribute__((interrupt)) void isr_page_fault(__attribute__((unused)) struct interrupt_frame *frame) {
vga_set_string(VGA_HEIGHT - 1, 0, "EXCEPTION: page fault", 0x4F);
while (1)
;
}
__attribute__((interrupt)) void isr_general_violation(__attribute__((unused)) struct interrupt_frame *frame) {
vga_set_string(VGA_HEIGHT - 1, 0, "EXCEPTION: general violation", 0x4F);
while (1)
;
}
__attribute__((interrupt)) void isr_ata_primary(__attribute__((unused)) struct interrupt_frame *frame) {
outb(0x20, 0x20);
outb(0xA0, 0x20);
}
void kernel_main() {
pic_init();
idt_init();
outb(0x21, inb(0x21) | 0x01); // mask out timer interrupt
idt_set_entry(0, isr_divide_by_zero, 0x8E);
idt_set_entry(0x0E, isr_page_fault, 0x8E);
idt_set_entry(0x0D, isr_general_violation, 0x8E);
idt_set_entry(46, isr_ata_primary, 0x8E);
__asm__ volatile("sti");
gdt_init();
syscall_init();
process_t *kernel = memory_allocate(sizeof(process_t));
kernel->pml4 = (void *)KERNEL_VIRTUAL_PML4;
kernel->root = fs_mount();
kernel->cwd = memory_allocate(sizeof(path_t));
kernel->stdin = keyboard_init();
kernel->stdout = vga_init();
fs_node_t *bin = path_open_file(kernel->root, kernel->cwd, "bin/terminal");
ASSERT(bin, "kernel: terminal not found");
uint8_t *code = memory_allocate(bin->size);
fs_read(bin, 0, bin->size, code);
process_t *terminal = process_create(kernel, code, bin->size);
memory_free(code);
fs_close(bin);
__asm__ volatile("mov %0, %%cr3" : : "r"((uint64_t)VIRT_TO_PHYS((void *)KERNEL_VIRTUAL_PML4)) : "memory");
uint8_t *stack = (uint8_t *)terminal->user_stack;
stack -= sizeof(uint64_t);
*(uint64_t *)stack = 0;
terminal->user_rsp = (void *)(USER_VIRTUAL_STACK_TOP - ((uint64_t)terminal->user_stack - (uint64_t)stack));
current_process = terminal;
tss.rsp0 = (uint64_t)current_process->kernel_stack;
process_switch_to(&kernel->kernel_rsp, terminal->kernel_rsp, VIRT_TO_PHYS(terminal->pml4));
fs_unmount((fs_node_t *)kernel->root);
outw(0x604, 0x2000); // TODO: parse ACPI tables for real hardware
__asm__ volatile("cli; hlt");
}