Add first C user program

This commit is contained in:
2026-06-11 20:19:24 +03:00
parent b32c12b311
commit 1c935cf154
55 changed files with 365 additions and 276 deletions
+77
View File
@@ -0,0 +1,77 @@
#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/util.h"
#include "src/kernel/vga.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 *kp = memory_allocate(sizeof(process_t));
kp->root = fs_mount();
kp->cwd = memory_allocate(sizeof(path_t));
kp->stdin = keyboard_init();
kp->stdout = vga_init();
fs_node_t *hello = fs_open_by(kp->root, "HELLO.BIN");
ASSERT(hello, "kernel: hello.bin not found");
uint8_t *hello_code = memory_allocate(hello->size);
fs_read(hello, 0, hello->size, hello_code);
process_t *proc = process_create(kp, hello_code, hello->size);
memory_free(hello_code);
fs_close(hello);
process_run(proc);
while (1)
;
fs_unmount(kp->root);
outw(0x604, 0x2000); // TODO: parse ACPI tables for real hardware
__asm__ volatile("cli; hlt");
}