From 4cf4fb0dda35616eb00b58d4920baa7e6459e053 Mon Sep 17 00:00:00 2001 From: xamidev Date: Fri, 20 Feb 2026 16:01:34 +0100 Subject: [PATCH] Task switching fix? but doesnt exit process gracefully --- .gitignore | 3 ++- Makefile | 4 ++++ src/idt/idt.c | 12 ++++++------ src/kmain.c | 6 +++++- src/sched/process.c | 2 ++ src/sched/scheduler.c | 6 +++--- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 3e041d8..a3f06dd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ iso_root *.iso *.gch */*.gch -*/*/*.gch \ No newline at end of file +*/*/*.gch +.gdb_history \ No newline at end of file diff --git a/Makefile b/Makefile index 13cd618..761f074 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,10 @@ debug: /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot -no-shutdown & gdb pepperk --command=debug.gdb +debug2: + /usr/bin/qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot -no-shutdown & + pwndbg pepperk --command=debug.gdb + run: build-iso /usr/bin/qemu-system-x86_64 -cdrom pepper.iso -serial stdio diff --git a/src/idt/idt.c b/src/idt/idt.c index 61e402c..7e559f4 100644 --- a/src/idt/idt.c +++ b/src/idt/idt.c @@ -197,17 +197,17 @@ struct cpu_status_t* interrupt_dispatch(struct cpu_status_t* context) case 32: // Timer Interrupt ticks++; + // Send an EOI so that we can continue having interrupts + outb(0x20, 0x20); if (ticks % SCHEDULER_QUANTUM == 0) { - CLEAR_INTERRUPTS; - struct cpu_status_t* current_ctx = scheduler_schedule(context); - process_switch(current_ctx->iret_rsp, current_ctx->iret_rip); - SET_INTERRUPTS; + return scheduler_schedule(context); + //struct cpu_status_t* current_ctx = scheduler_schedule(context); + //process_switch(current_ctx->iret_rsp, current_ctx->iret_rip); + //SET_INTERRUPTS; } - // Send an EOI so that we can continue having interrupts - outb(0x20, 0x20); break; case 33: diff --git a/src/kmain.c b/src/kmain.c index 856d336..d9aac09 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -35,7 +35,7 @@ void hcf() } // Doing nothing (can be interrupted) -void idle() {for(;;)asm("hlt");} +void idle() {SET_INTERRUPTS; for(;;)asm("hlt");} uint8_t kernel_stack[KERNEL_STACK_SIZE] __attribute__((aligned(16))); @@ -110,12 +110,16 @@ void kmain() vmm_init(); + struct process_t* idle_proc = process_create("idle", (void*)idle_main, 0); struct process_t* pedicel = process_create("pedicel", (void*)pedicel_main, 0); process_display_list(processes_list); scheduler_init(); + current_process = idle_proc; + current_process->status = RUNNING; + SET_INTERRUPTS; keyboard_init(FR); diff --git a/src/sched/process.c b/src/sched/process.c index 7566bbf..28945ad 100644 --- a/src/sched/process.c +++ b/src/sched/process.c @@ -160,6 +160,7 @@ void process_switch(uint64_t stack_addr, uint64_t code_addr) // Just mark as DEAD then idle. Scheduler will delete process at next timer interrupt % quantum. void process_exit() { + DEBUG("Exiting from process '%s'", current_process->name); CLEAR_INTERRUPTS; if (current_process) { @@ -167,6 +168,7 @@ void process_exit() } SET_INTERRUPTS; + outb(0x20, 0x20); for (;;) { asm("hlt"); diff --git a/src/sched/scheduler.c b/src/sched/scheduler.c index 645d845..1e2e98d 100644 --- a/src/sched/scheduler.c +++ b/src/sched/scheduler.c @@ -8,6 +8,7 @@ #include "process.h" #include "mem/paging/paging.h" #include +#include "io/serial/serial.h" extern struct process_t* processes_list; extern struct process_t* current_process; @@ -20,7 +21,7 @@ void scheduler_init() struct cpu_status_t* scheduler_schedule(struct cpu_status_t* context) { - //current_process->context = context; + current_process->context = context; current_process->status = READY; for (;;) { @@ -49,7 +50,6 @@ struct cpu_status_t* scheduler_schedule(struct cpu_status_t* context) load_cr3(VIRT_TO_PHYS((uint64_t)current_process->root_page_table)); DEBUG("loaded process pml4 into cr3"); - /* process_switch(current_process->context->rbp, current_process->context->iret_rip); - DEBUG("switched to process rip!"); */ + return current_process->context; } \ No newline at end of file