From 17ccde355672be85baf7efe2f77c468b75ef842d Mon Sep 17 00:00:00 2001 From: slinky55 Date: Sat, 4 Jul 2026 01:59:19 -0500 Subject: [PATCH] f'd myself over of course, scheduler can now run user programs.... --- build_and_run.sh | 23 ++++++++++++++++ deploy_programs.sh => clean.sh | 8 ++---- kernel/interrupts.S | 29 +++++++++++++++++--- kernel/interrupts.c | 29 +++++++++++--------- kernel/scheduler.c | 16 +++++++----- kernel/syscall.S | 48 +++++++++++++++++----------------- kernel/syscall.c | 1 + programs/crt0.S | 7 ++--- programs/main.c | 2 +- programs/makefile | 2 +- rocklibc/src/rockos/syscall.c | 4 +-- 11 files changed, 109 insertions(+), 60 deletions(-) create mode 100755 build_and_run.sh rename deploy_programs.sh => clean.sh (73%) diff --git a/build_and_run.sh b/build_and_run.sh new file mode 100755 index 0000000..f1ac468 --- /dev/null +++ b/build_and_run.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# cd rocklibc +# make clean +# make +# cd .. + +cd programs +make clean +make +cd .. + +make clean +make + +cd initrd +./make_img.sh +cd .. + +make -B + +make run + diff --git a/deploy_programs.sh b/clean.sh similarity index 73% rename from deploy_programs.sh rename to clean.sh index f970e8e..7895d11 100755 --- a/deploy_programs.sh +++ b/clean.sh @@ -2,20 +2,16 @@ cd rocklibc make clean -make cd .. cd programs make clean -make cd .. make clean -make cd initrd -./make_img.sh +rm -rf output +rm *.elf cd .. -make -B - diff --git a/kernel/interrupts.S b/kernel/interrupts.S index e8336e8..3ca959f 100644 --- a/kernel/interrupts.S +++ b/kernel/interrupts.S @@ -18,10 +18,22 @@ isr\num: isr_common_stub: pushal + xor %eax, %eax + mov %ds, %ax + pushl %eax + + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es + pushl %esp call common_interrupt_handler addl $4, %esp + popl %eax + mov %ax, %ds + mov %ax, %es + popal addl $8, %esp iret @@ -44,17 +56,28 @@ ISR_ERRCODE 14 # Page Fault (Has Error Code!) .extern send_eoi_master # drivers/pic/pic.S isr32: cli - pushal + pushal + call send_eoi_master + + xor %eax, %eax + mov %ds, %ax + pushl %eax + + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es pushl %esp call switch_context movl %eax, %esp - call send_eoi_master + popl %eax + mov %ax, %ds + mov %ax, %es popal sti - iret + iret ISR_NOERRCODE 33 # IRQ1 - Keyboard diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 57e4805..d695670 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -29,15 +29,6 @@ typedef struct __attribute__((packed)) { uint32_t base; } idt_ptr; -typedef struct __attribute__((packed)) { - uint32_t edi, esi, ebp, dummy, ebx, edx, ecx, eax; - - uint32_t int_no; - uint32_t error_code; - - uint32_t eip, cs, eflags, u_esp, u_ss; -} intr_regs_t; - // (interrupts.S) extern void load_idt(void); extern void isr0(); @@ -89,7 +80,7 @@ void init_idt() { if (isr_stub_table[i] != 0) { set_idtr(i, isr_stub_table[i], SEGMENT_KERN, ATTR_KERN_32); } else { - set_idtr(i, 0, SEGMENT_KERN, 0); + set_idtr(i, 0, SEGMENT_KERN, ATTR_KERN_32); } } @@ -98,6 +89,15 @@ void init_idt() { load_idt(); } +typedef struct __attribute__((packed)) { + uint32_t ds; + uint32_t edi, esi, ebp, dummy, ebx, edx, ecx, eax; + + uint32_t int_no; + uint32_t error_code; + + uint32_t eip, cs, eflags, u_esp, u_ss; +} intr_regs_t; void common_interrupt_handler(intr_regs_t *args) { switch (args->int_no) { case 0: @@ -116,7 +116,6 @@ void common_interrupt_handler(intr_regs_t *args) { case 13: case 15: case 16: - asm volatile("cli"); kprintf("\n--- CPU Exception ---\n"); kprintf("Error: [%d] %s\n", args->int_no, err_messages[args->int_no]); kprintf("Error Code: 0x%x\n", args->error_code); @@ -125,10 +124,10 @@ void common_interrupt_handler(intr_regs_t *args) { kprintf("EAX: 0x%x\n", args->eax); kprintf("EFLAGS: 0x%x\n", args->eflags); kprintf("Code Segment: 0x%x\n", args->cs); + kprintf("Calling Data Segment: 0x%x\n", args->ds); kprintf("Current cr3: 0x%x\n", fetch_cr3()); kprintf("tss.esp0: 0x%x\n", tss.esp0); - process_t* task = current_task(); - kprintf("Task kstack_top: 0x%x\n", task->kstack_top); + kprintf("instruction: 0x%x\n", *(uint32_t*)args->eip); asm volatile("hlt"); break; case 14:{ @@ -154,6 +153,10 @@ void common_interrupt_handler(intr_regs_t *args) { asm volatile("hlt"); break; } + case 32: { + + break; + } case 33: { kprintf("ps2 keyboard interrupt\n"); uint8_t data = ps2_read_data(); diff --git a/kernel/scheduler.c b/kernel/scheduler.c index 4e36e49..8d16410 100644 --- a/kernel/scheduler.c +++ b/kernel/scheduler.c @@ -69,17 +69,13 @@ uint32_t switch_context(uint32_t current_esp) { uint32_t current_cr3 = fetch_cr3(); if (current_cr3 != process_table[current_process].cr3) { - kprintf("switching cr3 | old: 0x%x | new: 0x%x\n", current_cr3, process_table[current_process].cr3); load_pd((void*)process_table[current_process].cr3); } if (process_table[current_process].flags & PROCESS_FLAG_USER) { tss.esp0 = process_table[current_process].kstack_top; } - - if (found_ready) { - kprintf("found ready task %d | cr3: 0x%x\n", current_process, process_table[current_process].cr3); - } + return process_table[current_process].esp; } @@ -123,12 +119,12 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) { if (user) { *(--esp) = 0x23; // User Data Segment (SS) with RPL 3 (0x20 | 3) *(--esp) = u_esp; // User Stack Pointer (ESP) - location mapped in user space - *(--esp) = 0x0202; // EFLAGS (Interrupts enabled) + *(--esp) = 0x202; // EFLAGS (Interrupts enabled) *(--esp) = 0x1B; // User Code Segment (CS) with RPL 3 (0x18 | 3) *(--esp) = entry_point; // EIP } else { // IRET values for Ring 0 - *(--esp) = 0x0202; // EFLAGS + *(--esp) = 0x202; // EFLAGS *(--esp) = 0x08; // Kernel Code Segment (CS) *(--esp) = entry_point; // EIP } @@ -142,6 +138,12 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) { *(--esp) = 0; // ESI *(--esp) = 0; // EDI + if (user) { + *(--esp) = 0x23; // User Data Segment Selector (RPL 3) + } else { + *(--esp) = 0x10; // Kernel Data Segment Selector (RPL 0) + } + process->esp = (uint32_t)esp; return 1; } diff --git a/kernel/syscall.S b/kernel/syscall.S index bd7e539..931e612 100644 --- a/kernel/syscall.S +++ b/kernel/syscall.S @@ -4,33 +4,33 @@ syscall_handler: cli - pushfl - pushl %cs + push $0 + push $0x80 - movl 8(%esp), %eax - pushl %eax - movl 4(%esp), %ebx # Get CS - movl %ebx, 8(%esp) # Move CS over Old EIP - movl 0(%esp), %ebx # Get Return EIP - movl %ebx, 4(%esp) # Move Return EIP over CS - addl $4, %esp # Adjust stack pointer + pusha - movw %ds, %ax - pushl %eax + push %ds + push %es + push %fs + push %gs - pushal - - pushl %esp - call switch_context - movl %eax, %esp - - popal + mov $0x10, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %fs + mov %ax, %gs - popl %eax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs + push %esp + call syscall + add $4, %esp + + mov 28(%esp), %eax - sti + pop %gs + pop %fs + pop %es + pop %ds + + popa + add $8, %esp iret \ No newline at end of file diff --git a/kernel/syscall.c b/kernel/syscall.c index c3fb359..6da6104 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -18,6 +18,7 @@ typedef struct registers { static int32_t sys_exit(int status) { + asm volatile("cli"); asm volatile("hlt"); return 0; } diff --git a/programs/crt0.S b/programs/crt0.S index b2f3d65..de913e1 100644 --- a/programs/crt0.S +++ b/programs/crt0.S @@ -1,7 +1,7 @@ .global _start .section .text -_start: +_start: xor %ebp, %ebp push %ebp mov %esp, %ebp @@ -13,9 +13,10 @@ _start: push %ecx push %ebx push %eax -1: jmp 1b + call main push %eax +1: jmp 1b call exit -1: jmp 1b + diff --git a/programs/main.c b/programs/main.c index a3d10c6..abe7ab7 100644 --- a/programs/main.c +++ b/programs/main.c @@ -1,6 +1,6 @@ #include -int main() { +int main(int argc, char *argv[]) { printf("RockOS booting...\n"); return 0; } \ No newline at end of file diff --git a/programs/makefile b/programs/makefile index 8bf6c96..6fee48f 100644 --- a/programs/makefile +++ b/programs/makefile @@ -21,4 +21,4 @@ $(TARGET): crt0.o main.o ../rocklibc/rlibc.a clean: rm -f *.o $(TARGET) -.PHONY: all clean \ No newline at end of file +.PHONY: all clean test \ No newline at end of file diff --git a/rocklibc/src/rockos/syscall.c b/rocklibc/src/rockos/syscall.c index f318af0..4afe9d4 100644 --- a/rocklibc/src/rockos/syscall.c +++ b/rocklibc/src/rockos/syscall.c @@ -96,7 +96,7 @@ void * sbrk(intptr_t increment) { if (increment == 0) { return (void *)current_break; - } + } uint32_t new_break = (uint32_t)((intptr_t)current_break + increment); @@ -109,6 +109,6 @@ void * sbrk(intptr_t increment) { return (void *)-1; } -void exit(int status) { +void _exit(int status) { _PDCLIB_exit(status); } \ No newline at end of file