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.
101 lines
3.4 KiB
101 lines
3.4 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/pipe.h"
|
|
#include "src/kernel/process.h"
|
|
#include "src/kernel/syscall.h"
|
|
#include "src/kernel/timer.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"
|
|
#include "src/lib/syscall.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);
|
|
|
|
gdt_init();
|
|
syscall_init();
|
|
|
|
process_t *kernel = memory_allocate(sizeof(process_t));
|
|
kernel->pml4 = (uint64_t *)KERNEL_VIRTUAL_PML4;
|
|
kernel->pid = 0;
|
|
kernel->state = PROCESS_RUNNING;
|
|
kernel->kernel_stack = kernel->kernel_rsp = (uint8_t *)memory_allocate(PAGE_SIZE) + PAGE_SIZE;
|
|
kernel->user_stack = kernel->user_rsp = (uint8_t *)memory_allocate(PAGE_SIZE) + PAGE_SIZE;
|
|
kernel->cwd = memory_allocate(sizeof(path_t));
|
|
kernel->fds[kernel->free_fd++] = keyboard_init();
|
|
kernel->fds[kernel->free_fd++] = vga_init();
|
|
kernel->code = EXIT_CODE_OK;
|
|
|
|
current_process = kernel;
|
|
tss.rsp0 = (uint64_t)kernel->kernel_stack;
|
|
|
|
fs_node_t *terminal_node = path_open_node(kernel->cwd, "bin/terminal", OPEN_FILE);
|
|
fs_node_t *shell_node = path_open_node(kernel->cwd, "bin/shell", OPEN_FILE);
|
|
|
|
ASSERT(terminal_node, "kernel: terminal not found");
|
|
ASSERT(shell_node, "kernel: shell not found");
|
|
|
|
uint8_t *terminal_code = memory_allocate(terminal_node->size);
|
|
fs_read(terminal_node, 0, terminal_node->size, terminal_code);
|
|
fs_close(terminal_node);
|
|
|
|
uint8_t *shell_code = memory_allocate(shell_node->size);
|
|
fs_read(shell_node, 0, shell_node->size, shell_code);
|
|
fs_close(shell_node);
|
|
|
|
process_t *terminal = process_create(kernel, terminal_code, terminal_node->size, 0, 1);
|
|
memory_free(terminal_code);
|
|
|
|
process_t *shell = process_create(kernel, shell_code, shell_node->size, 0, 1);
|
|
memory_free(shell_code);
|
|
|
|
uint64_t *terminal_stack = (uint64_t *)terminal->user_stack;
|
|
*(--terminal_stack) = 0;
|
|
terminal->user_rsp = (void *)(USER_VIRTUAL_STACK_TOP - ((uint64_t)terminal->user_stack - (uint64_t)terminal_stack));
|
|
|
|
uint64_t *shell_stack = (uint64_t *)shell->user_stack;
|
|
*(--shell_stack) = 0;
|
|
shell->user_rsp = (void *)(USER_VIRTUAL_STACK_TOP - ((uint64_t)shell->user_stack - (uint64_t)shell_stack));
|
|
|
|
pipe_init(&(terminal->fds[terminal->free_fd++]), &(shell->fds[0]));
|
|
|
|
timer_init();
|
|
|
|
__asm__ volatile("sti; hlt");
|
|
}
|
|
|