From 0066af13e84c9c85e6cc357ca64930868ecd1b25 Mon Sep 17 00:00:00 2001 From: ssimnb <250598700+ssimnb@users.noreply.github.com> Date: Sat, 24 Jan 2026 08:15:08 +0100 Subject: [PATCH] Rename & scheduler stuff --- Makefile | 2 +- README.md | 6 ++--- limine.conf | 4 +-- src/drivers/ahci.c | 2 +- src/drivers/pmt.c | 2 +- src/hal/apic.c | 2 +- src/hal/idt.c | 2 +- src/hal/ioapic.c | 2 +- src/hal/smp.c | 2 +- src/hal/timer.c | 2 +- src/include/kprint.h | 2 +- src/include/{SFB25.h => neobbo.h} | 0 src/lib/kprint.c | 3 ++- src/main.c | 44 +++---------------------------- src/mm/kmalloc.c | 2 +- src/mm/page.c | 2 +- src/mm/pmm.c | 2 +- src/mm/slab.c | 6 ++++- src/mm/slab.h | 2 ++ src/mm/vmm.c | 2 +- src/scheduler/sched.asm | 10 ++++++- src/scheduler/sched.c | 44 ++++++++++++++----------------- src/sys/acpi.c | 2 +- src/sys/pci.c | 2 +- src/uacpi_kernel_api.c | 2 +- 25 files changed, 63 insertions(+), 88 deletions(-) rename src/include/{SFB25.h => neobbo.h} (100%) diff --git a/Makefile b/Makefile index 17add98..333f92f 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ LDFLAGS += -m elf_x86_64 \ -z text \ -z max-page-size=0x1000 \ -T linker.ld -NASMFLAGS = -f elf64 +NASMFLAGS = -f elf64 -g -F dwarf dependencies: # build limine rm -rf limine diff --git a/README.md b/README.md index 3455fe1..1fb50a3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SFB/25 +# Neobbo Hobby operating system for the x86_64 architecture written in C. Licensed under GPLv3 @@ -10,12 +10,12 @@ Then run `make all` - make sure to adjust the `CC`, `AS` and `LD` flags to match in the `build` folder you should have a `SFB25.iso` file. -To try out SFB/25 you can use QEMU: +To try out Neobbo you can use QEMU: `qemu-system-x86_64 build/SFB25.iso -machine q35 -m 512M` ## External projects - [Limine bootloader](https://github.com/limine-bootloader/limine) for the bootloader -- [Flanterm](https://github.com/mintsuki/flanterm) for the terminal +- [Flanterm](https://codeberg.org/mintsuki/flanterm) for the terminal - [uACPI](https://github.com/uacpi/uacpi) for the AML interpreter and other ACPI stuff \ No newline at end of file diff --git a/limine.conf b/limine.conf index 3381228..aeac6c8 100644 --- a/limine.conf +++ b/limine.conf @@ -2,7 +2,7 @@ timeout: 0 # The entry name that will be displayed in the boot menu. -/SFB/25 (KASLR on) +/Neobbo (KASLR on) # We use the Limine boot protocol. protocol: limine @@ -10,7 +10,7 @@ timeout: 0 kernel_path: boot():/SFB25.elf # Same thing, but without KASLR. -/SFB/25 (KASLR off) +/Neobbo (KASLR off) # We use the Limine boot protocol. protocol: limine diff --git a/src/drivers/ahci.c b/src/drivers/ahci.c index 33bbc20..1723420 100644 --- a/src/drivers/ahci.c +++ b/src/drivers/ahci.c @@ -1,5 +1,5 @@ -#include +#include #include #include "../hal/apic.h" #include "../sys/pci.h" diff --git a/src/drivers/pmt.c b/src/drivers/pmt.c index 18f93aa..72d277b 100644 --- a/src/drivers/pmt.c +++ b/src/drivers/pmt.c @@ -1,6 +1,6 @@ #include "../sys/acpi.h" #include -#include +#include #include diff --git a/src/hal/apic.c b/src/hal/apic.c index d6e9eeb..df8f28c 100644 --- a/src/hal/apic.c +++ b/src/hal/apic.c @@ -5,7 +5,7 @@ #include "ioapic.h" #include #include -#include +#include #include // GCC specific #define LAPIC_ID_REG 0x020 diff --git a/src/hal/idt.c b/src/hal/idt.c index bc4d015..f8957c0 100644 --- a/src/hal/idt.c +++ b/src/hal/idt.c @@ -3,7 +3,7 @@ #include "timer.h" #include #include -#include +#include idt_descriptor idt[256] = {0}; idt_register idtr = {sizeof(idt)-1, (uint64_t)(&idt)}; diff --git a/src/hal/ioapic.c b/src/hal/ioapic.c index 8553ff6..7a55922 100644 --- a/src/hal/ioapic.c +++ b/src/hal/ioapic.c @@ -1,7 +1,7 @@ #include #include -#include +#include #include "../sys/acpi.h" #include "error.h" diff --git a/src/hal/smp.c b/src/hal/smp.c index a1247f7..b884385 100644 --- a/src/hal/smp.c +++ b/src/hal/smp.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include "gdt.h" #include "smp.h" #include "apic.h" diff --git a/src/hal/timer.c b/src/hal/timer.c index 7c67cb3..48f6fe6 100644 --- a/src/hal/timer.c +++ b/src/hal/timer.c @@ -4,7 +4,7 @@ #include "../drivers/pmt.h" #include "timer.h" #include -#include +#include /* Determines which timer will be used for calibration */ int calibration_timer = -1; diff --git a/src/include/kprint.h b/src/include/kprint.h index 0c2e07b..bdabb72 100644 --- a/src/include/kprint.h +++ b/src/include/kprint.h @@ -8,7 +8,7 @@ enum { LOG_SUCCESS, }; -void klog(const char *func, const char *msg); +void klog(const char *func, const char *msg, ...); int kprintf(const char *format_string, ...); diff --git a/src/include/SFB25.h b/src/include/neobbo.h similarity index 100% rename from src/include/SFB25.h rename to src/include/neobbo.h diff --git a/src/lib/kprint.c b/src/lib/kprint.c index 12cd964..a2dc4f6 100644 --- a/src/lib/kprint.c +++ b/src/lib/kprint.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include "../include/kprint.h" @@ -22,7 +23,7 @@ extern bool serial_enabled; -void klog(const char *func, const char *msg){ +void klog(const char *func, const char *msg, ...){ kprintf("{ksk}: {s}\n", ANSI_COLOR_MAGENTA, func, ANSI_COLOR_RESET, msg); serial_kprintf("{ksk}: {s}\n", ANSI_COLOR_MAGENTA, func, ANSI_COLOR_RESET, msg); diff --git a/src/main.c b/src/main.c index 5e0fdf7..2407086 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include "error.h" @@ -78,7 +78,7 @@ void _start(void){ 0 ); - kprintf("Welcome to SFB/25{n}"); + kprintf("Welcome to Neobbo{n}"); extern link_symbol_ptr text_start_addr, text_end_addr; @@ -90,59 +90,22 @@ void _start(void){ klog("acpi", "Reading ACPI tables"); acpi_init(); - klog("apic", "Initalizing APIC"); apic_init(); - tsc_init(); - klog("pmm", "Setting up the PMM"); pmm_init(); klog("vmm", "Setting up the page tables"); vmm_init(); + init_page_array(); - - //struct ma_kcache *cache = ma_cache_create("bird", 1048576, 0, 0, 0); - - //uint64_t *d = ma_cache_alloc(cache, 0); - - //cache_info(cache); - - //memset(d, 0, 1048576); - - //for(;;); - - //cache_info(cache); - _kmalloc_init(); - int *my_int; - - for(int i = 0;i < 3000;i++){ - my_int = kmalloc(512); - *my_int = rand(); - kprintf("my int: {d} on iter: {d}\n", *my_int, i); - kstatus status = kfree(my_int); - if(status != KERNEL_STATUS_SUCCESS){ - kprintf("status: {d}\n", status); - } - - } - - - - kprintf("my int: {d}\n", *my_int); - - - - - //for(;;){} - klog("smp", "Starting APs"); smp_init(); @@ -163,6 +126,7 @@ void _start(void){ bool kernel_killed = false; void kkill(void){ kernel_killed = true; + kprintf("The kernel has been killed.\n"); asm volatile("cli; hlt"); for(;;); } diff --git a/src/mm/kmalloc.c b/src/mm/kmalloc.c index 5bee45a..52a65d5 100644 --- a/src/mm/kmalloc.c +++ b/src/mm/kmalloc.c @@ -26,7 +26,7 @@ size_t sizes[14] = {16, 32, 64, 128, 256, 512, 1024, 4096, 8192, 32768, 65536, 1 void *kmalloc(size_t size){ if(size > 1048576){ - klog(__func__, "Attempted to allocate more than max size (1M)"); + klog(__func__, "Attempted to allocate {d} bytes, more than max size (1M)"); return NULL; } diff --git a/src/mm/page.c b/src/mm/page.c index b3724be..89a9f2b 100644 --- a/src/mm/page.c +++ b/src/mm/page.c @@ -1,7 +1,7 @@ #include "page.h" #include #include "vmm.h" -#include +#include #include #include #include diff --git a/src/mm/pmm.c b/src/mm/pmm.c index 9fb7acd..7518823 100644 --- a/src/mm/pmm.c +++ b/src/mm/pmm.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include "pmm.h" #include "kmalloc.h" diff --git a/src/mm/slab.c b/src/mm/slab.c index d77aa83..719d8f8 100644 --- a/src/mm/slab.c +++ b/src/mm/slab.c @@ -7,7 +7,7 @@ #include "page.h" #include "slab.h" #include -#include +#include #include struct ma_kcache *caches = NULL; @@ -414,6 +414,8 @@ kstatus ma_cache_dealloc(void *object){ return KERNEL_STATUS_ERROR; } + acquire_spinlock(&slab->lock); + struct ma_bufctl *buf = addr_to_bufctl(object); if(buf == NULL){ @@ -432,6 +434,8 @@ kstatus ma_cache_dealloc(void *object){ _ma_move_slab(slab, FREE); } + free_spinlock(&slab->lock); + return KERNEL_STATUS_SUCCESS; } diff --git a/src/mm/slab.h b/src/mm/slab.h index b42c558..5526d33 100644 --- a/src/mm/slab.h +++ b/src/mm/slab.h @@ -21,6 +21,8 @@ struct ma_slab { uint32_t refcount; // The amount of active (not free) objects in the slabs + atomic_flag lock; + struct ma_bufctl *free; // Linked list of free buffers in the slab. Is equal to NULL once there are no more free objects }; diff --git a/src/mm/vmm.c b/src/mm/vmm.c index 9c9fc2d..9a026fa 100644 --- a/src/mm/vmm.c +++ b/src/mm/vmm.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include #include "pmm.h" diff --git a/src/scheduler/sched.asm b/src/scheduler/sched.asm index 6436533..a874b8d 100644 --- a/src/scheduler/sched.asm +++ b/src/scheduler/sched.asm @@ -1,6 +1,7 @@ global switch_context global get_context +global restore_stack ; switch_context(struct context *old, struct context *new) ; ^RDI ^RSI @@ -31,6 +32,8 @@ switch_context: jmp [rsi + 0x38] + + ret @@ -50,4 +53,9 @@ get_context: push r8 - ret \ No newline at end of file + ret +; restore_stack(uint64_t rsp, uint64_t rbp) +restore_stack: + mov rsp, rdi + mov rbp, rsi + ret diff --git a/src/scheduler/sched.c b/src/scheduler/sched.c index c32d992..3125564 100644 --- a/src/scheduler/sched.c +++ b/src/scheduler/sched.c @@ -1,21 +1,24 @@ #include +#include #include -#include +#include #include "../hal/smp.h" #include #include "../mm/kmalloc.h" #include "sched.h" -extern context *save_context(); +extern void get_context(struct context *ctx); extern void switch_context(context *old, context *new); +extern void restore_stack(uint64_t rsp, uint64_t rbp); + #define QUANTUM_US 10000 int next_pid = 1; void idle_task(){ - kprintf("Hello world from bruhd task!\n"); + kprintf("Hello world from idle task!\n"); } void test_task(){ @@ -32,32 +35,22 @@ proc *alloc_process(void){ proc *p = &proc_list[i]; p->state = READY; - kprintf("pstate = 0x{x}\n", READY); - kprintf("actual: 0x{x}\n", p->state); + p->kstack = kmalloc(INITIAL_STACK_SIZE); - kprintf("actua1l: 0x{x}\n", p->state); p->pid = next_pid++; - kprintf("actua2l: 0x{x}\n", p->state); - - memset(&p->context, 0, sizeof(context)); - - kprintf("actua3l: 0x{x}\n", p->state); - p->context.rbp = (uint64_t)p->kstack; p->context.rsp = (uint64_t)p->context.rbp + INITIAL_STACK_SIZE; - kprintf("actua4l: 0x{x}\n", p->state); - return p; } } - kprintf("Couldn't find free process!"); + klog(__func__, "Couldnt find free process!!!!!!\n"); } -kstatus add_task(uint64_t *entry){ +proc *add_task(uint64_t *entry){ proc *proc = alloc_process(); if (proc == NULL) { @@ -67,7 +60,7 @@ kstatus add_task(uint64_t *entry){ proc->context.rip = (uint64_t)entry; - return KERNEL_STATUS_SUCCESS; + return proc; } void scheduler_init(){ @@ -83,27 +76,32 @@ void scheduler_init(){ kkill(); } - kprintf("hi1\n"); proc *proc_list = state->process_list; memset(proc_list, 0, sizeof(proc) * 512); - int pid = add_task((uint64_t*)idle_task); + proc *idle = add_task((uint64_t*)idle_task); + + if(idle == NULL){ + klog(__func__, "Failed to allocate idle task"); + kkill(); + } + + state->current_process = idle; + add_task((uint64_t*)test_task); for(;;){ for(int i = 0; i < PROC_MAX; i++){ if(proc_list[i].state == READY){ - - kprintf("Hell yeah"); context old_state = state->current_process->context; state->current_process = &proc_list[i]; state->current_process->state = RUNNING; + get_context(&state->scheduler_context); switch_context(&old_state, &state->current_process->context); - } } } @@ -112,8 +110,6 @@ void scheduler_init(){ void scheduler_tick(){ cpu_state *state = get_cpu_struct(); proc *proc_list = state->process_list; - switch_context(&state->current_process->context, &state->scheduler_context); - } \ No newline at end of file diff --git a/src/sys/acpi.c b/src/sys/acpi.c index f9ca211..7e8c504 100644 --- a/src/sys/acpi.c +++ b/src/sys/acpi.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include "acpi.h" diff --git a/src/sys/pci.c b/src/sys/pci.c index 508abd9..28b81b3 100644 --- a/src/sys/pci.c +++ b/src/sys/pci.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/uacpi_kernel_api.c b/src/uacpi_kernel_api.c index 9fde5ee..b9849eb 100644 --- a/src/uacpi_kernel_api.c +++ b/src/uacpi_kernel_api.c @@ -4,7 +4,7 @@ #include "mm/kmalloc.h" #include #include -#include +#include #include uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *out_rsdp_address){