structure project

This commit is contained in:
nub31
2025-09-03 11:18:17 +02:00
parent ad30071e9f
commit c338e05648
15 changed files with 46 additions and 49 deletions

View File

@@ -2,16 +2,15 @@ CC = x86_64-elf-gcc
LD = x86_64-elf-ld LD = x86_64-elf-ld
AS = nasm AS = nasm
TARGET_PATH = src/arch/x86_64
CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c11 -g CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c11 -g
LDFLAGS = -g LDFLAGS = -g
ASFLAGS = -f elf64 -g -F dwarf ASFLAGS = -f elf64 -g -F dwarf
SRC_C := src/kernel.c src/mem.c src/pmm.c src/vmm.c src/string.c src/vga.c src/interrupts.c src/keyboard.c SRC_C := $(shell find src -name '*.c')
SRC_ASM := src/start.asm SRC_ASM := $(shell find src -name '*.asm')
OBJ_C := $(SRC_C:src/%.c=.build/%.o) OBJS := $(patsubst src/%.c, .build/%.o, $(SRC_C)) $(patsubst src/%.asm, .build/%.o, $(SRC_ASM))
OBJ_ASM := $(SRC_ASM:src/%.asm=.build/%.o)
OBJS := $(OBJ_C) $(OBJ_ASM)
iso: .build/nub-os.iso iso: .build/nub-os.iso
@echo "ISO created at '.build/nub-os.iso'" @echo "ISO created at '.build/nub-os.iso'"
@@ -19,9 +18,6 @@ iso: .build/nub-os.iso
clean: clean:
@rm -r .build 2>/dev/null || true @rm -r .build 2>/dev/null || true
build-dir:
mkdir .build 2>/dev/null || true
.build/nub-os.iso: .build/kernel grub.cfg .build/nub-os.iso: .build/kernel grub.cfg
mkdir -p .build/nub-os/boot/grub mkdir -p .build/nub-os/boot/grub
cp grub.cfg .build/nub-os/boot/grub cp grub.cfg .build/nub-os/boot/grub
@@ -31,8 +27,10 @@ build-dir:
.build/kernel: $(OBJS) .build/kernel: $(OBJS)
$(LD) $(LDFLAGS) -T linker.ld -o $@ $^ $(LD) $(LDFLAGS) -T linker.ld -o $@ $^
.build/%.o: src/%.c | build-dir .build/%.o: src/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
.build/%.o: src/%.asm | build-dir .build/%.o: src/%.asm
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $< $(AS) $(ASFLAGS) -o $@ $<

View File

@@ -1,5 +1,5 @@
#include "interrupts.h" #include "interrupts.h"
#include "util.h" #include "../../util.h"
#define PIC1_COMMAND 0x20 #define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21 #define PIC1_DATA 0x21
@@ -55,7 +55,7 @@ void enable_apic()
uint64_t apic_base = rdmsr(0x1B); uint64_t apic_base = rdmsr(0x1B);
apic_base |= (1 << 11); apic_base |= (1 << 11);
wrmsr(0x1B, apic_base); wrmsr(0x1B, apic_base);
kprintf("APIC enabled\n"); printf("APIC enabled\n");
} }
void remap_pic() void remap_pic()
@@ -74,32 +74,32 @@ void remap_pic()
outb(PIC1_DATA, 0); outb(PIC1_DATA, 0);
outb(PIC2_DATA, 0); outb(PIC2_DATA, 0);
kprintf("PIC remapped\n"); printf("PIC remapped\n");
} }
void disable_pic() void disable_pic()
{ {
outb(PIC1_DATA, 0xFF); outb(PIC1_DATA, 0xFF);
outb(PIC2_DATA, 0xFF); outb(PIC2_DATA, 0xFF);
kprintf("PIC disabled\n"); printf("PIC disabled\n");
} }
void handle_exception(const isr_frame_t* frame) void handle_exception(const isr_frame_t* frame)
{ {
kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code); printf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code);
kpanic(); panic();
} }
void register_irq_handler(uint8_t irq, irq_handler_t handler) void register_irq_handler(uint8_t irq, irq_handler_t handler)
{ {
if (irq >= 16) if (irq >= 16)
{ {
kprintf("Cannot register irq %d is out of bounds\n", irq); printf("Cannot register irq %d is out of bounds\n", irq);
} }
else else
{ {
irq_handlers[irq] = handler; irq_handlers[irq] = handler;
kprintf("Registered irq handler at vector %d\n", irq); printf("Registered irq handler at vector %d\n", irq);
} }
} }
@@ -132,6 +132,6 @@ void handle_isr(const isr_frame_t* frame)
} }
else else
{ {
kprintf("interrupt[%d]: not implemented\n", frame->int_no); printf("interrupt[%d]: not implemented\n", frame->int_no);
} }
} }

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "kernel.h" #include "../../kernel.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@@ -26,11 +26,11 @@ void register_irq_handler(uint8_t irq, irq_handler_t handler);
static inline void enable_interrupts() static inline void enable_interrupts()
{ {
__asm__ volatile("sti"); __asm__ volatile("sti");
kprintf("Interrupts enabled\n"); printf("Interrupts enabled\n");
} }
static inline void disable_interrupts() static inline void disable_interrupts()
{ {
__asm__ volatile("cli"); __asm__ volatile("cli");
kprintf("Interrupts disabled\n"); printf("Interrupts disabled\n");
} }

View File

@@ -1,7 +1,7 @@
#include "keyboard.h" #include "keyboard.h"
#include "../../kernel.h"
#include "../../util.h"
#include "interrupts.h" #include "interrupts.h"
#include "kernel.h"
#include "util.h"
#include <stddef.h> #include <stddef.h>
#define SCANCODE_LEFT_SHIFT 42 #define SCANCODE_LEFT_SHIFT 42
@@ -96,8 +96,8 @@ void register_keypress_handler(const keyboard_handler_t handler)
// keyboard_handlers is a dynamic list // keyboard_handlers is a dynamic list
if (handler_index >= KEYBOARD_HANDLERS_LENGTH) if (handler_index >= KEYBOARD_HANDLERS_LENGTH)
{ {
kprintf("Maximum keyboard handlers reached\n"); printf("Maximum keyboard handlers reached\n");
kpanic(); panic();
} }
keyboard_handlers[handler_index] = handler; keyboard_handlers[handler_index] = handler;

View File

@@ -1,6 +1,6 @@
#include "pmm.h" #include "pmm.h"
#include "kernel.h" #include "../../kernel.h"
#include "mem.h" #include "../../mem.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@@ -24,8 +24,8 @@ void init_pmm(multiboot_info_t* mbd)
if (!(mbd->flags & (1 << 6))) if (!(mbd->flags & (1 << 6)))
{ {
kprintf("Invalid memory map given by bootloader\n"); printf("Invalid memory map given by bootloader\n");
kpanic(); panic();
} }
uint64_t offset = 0; uint64_t offset = 0;
@@ -42,13 +42,13 @@ void init_pmm(multiboot_info_t* mbd)
num_regions++; num_regions++;
kprintf("Available memory: 0x%x - 0x%x (%u KB)\n", mmmt->addr, mmmt->addr + mmmt->len, mmmt->len / 1024); printf("Available memory: 0x%x - 0x%x (%u KB)\n", mmmt->addr, mmmt->addr + mmmt->len, mmmt->len / 1024);
} }
offset += mmmt->size + sizeof(mmmt->size); offset += mmmt->size + sizeof(mmmt->size);
} }
kmemset(page_bitmap, 0xFF, BITMAP_SIZE); memset(page_bitmap, 0xFF, BITMAP_SIZE);
for (size_t i = 0; i < num_regions; i++) for (size_t i = 0; i < num_regions; i++)
{ {
@@ -67,7 +67,7 @@ void init_pmm(multiboot_info_t* mbd)
} }
else else
{ {
kprintf("System has more ram than the bitmap allows!\n"); printf("System has more ram than the bitmap allows!\n");
break; break;
} }
} }
@@ -86,7 +86,7 @@ void init_pmm(multiboot_info_t* mbd)
} }
} }
kprintf("PMM initialized\n"); printf("PMM initialized\n");
} }
uint64_t pmm_alloc_page() uint64_t pmm_alloc_page()

View File

@@ -1,7 +1,7 @@
#include "kernel.h" #include "kernel.h"
#include "interrupts.h" #include "arch/x86_64/interrupts.h"
#include "multiboot.h" #include "arch/x86_64/multiboot.h"
#include "pmm.h" #include "arch/x86_64/pmm.h"
#include "string.h" #include "string.h"
#include "vga.h" #include "vga.h"
#include <stdarg.h> #include <stdarg.h>
@@ -15,18 +15,18 @@ void kmain(multiboot_info_t* mbd)
remap_pic(); remap_pic();
enable_interrupts(); enable_interrupts();
kprintf("Welcome to nub OS :)\n"); printf("Welcome to nub OS :)\n");
khalt(); halt();
} }
void kpanic() void panic()
{ {
kprintf("Kernel panic!\n"); printf("Kernel panic!\n");
disable_interrupts(); disable_interrupts();
khalt(); halt();
} }
void kprintf(const char* fmt, ...) void printf(const char* fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);

View File

@@ -2,11 +2,11 @@
#include <stdbool.h> #include <stdbool.h>
void kpanic(); void panic();
void kprintf(const char* fmt, ...); void printf(const char* fmt, ...);
static inline void khalt() static inline void halt()
{ {
while (true) while (true)
{ {

View File

@@ -1,7 +1,7 @@
#include "mem.h" #include "mem.h"
#include <stdint.h> #include <stdint.h>
void kmemset(void* destination, uint8_t value, size_t length) void memset(void* destination, uint8_t value, size_t length)
{ {
for (size_t i = 0; i < length; i++) for (size_t i = 0; i < length; i++)
{ {

View File

@@ -3,4 +3,4 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
void kmemset(void* destination, uint8_t value, size_t length); void memset(void* destination, uint8_t value, size_t length);

View File

View File

@@ -1 +0,0 @@
#pragma once