From 46116758f8c74db62d263433eb10c36d94f1b48f Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 10:59:14 -0400 Subject: change start vitaddr --- kernel/memory/virtalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kernel') diff --git a/kernel/memory/virtalloc.c b/kernel/memory/virtalloc.c index a077532..08a0758 100644 --- a/kernel/memory/virtalloc.c +++ b/kernel/memory/virtalloc.c @@ -79,7 +79,7 @@ static void free_node(struct virt_ctx *ctx, struct virt_addr_node *node) void virtaddr_init(struct virt_ctx *ctx) { struct virt_addr_node init = { - .start = 0x40005000, // map after paging pt + .start = 0x50000000, .end = 0x1000000000000, // 48bit memory address max .next = NULL, .prev = NULL, -- cgit v1.2.3-freya From 03ce2cd514ee2c87528cff12591241963fde3ffe Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 10:59:49 -0400 Subject: fix physalloc edge case --- kernel/memory/physalloc.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'kernel') diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 856a627..833c995 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include #include @@ -11,13 +12,13 @@ extern char kernel_end; // between memory_start and kernel_start will be the bitmap static uintptr_t memory_start = 0; -static uint64_t *bitmap; +static uint64_t *bitmap = NULL; static uint64_t total_memory; static uint64_t free_memory; static uint64_t page_count; static uint64_t segment_count; struct memory_map phys_mmap; -struct memory_segment *page_start; +struct memory_segment *page_start = NULL; static const char *segment_type_str[] = { [SEG_TYPE_FREE] = "Free", [SEG_TYPE_RESERVED] = "Reserved", @@ -84,6 +85,13 @@ void *alloc_phys_pages_exact(size_t pages) if (pages < 1) return NULL; + if (bitmap == NULL || page_start == NULL) { + // temporary bump allocator + void *addr = (void*)memory_start; + memory_start += PAGE_SIZE; + return addr; + } + size_t n_contiguous = 0; size_t free_region_start = 0; for (size_t i = 0; i < page_count; i++) { -- cgit v1.2.3-freya From 228366dcf517266097a77b846efe2c2364a10fa3 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 11:20:08 -0400 Subject: syscall_return fn --- kernel/cpu/idt.S | 23 +++++++++++++++-------- kernel/include/comus/cpu.h | 6 ++++++ kernel/procs.c | 2 ++ 3 files changed, 23 insertions(+), 8 deletions(-) (limited to 'kernel') diff --git a/kernel/cpu/idt.S b/kernel/cpu/idt.S index 177d3b1..06c70a0 100644 --- a/kernel/cpu/idt.S +++ b/kernel/cpu/idt.S @@ -1,5 +1,5 @@ .global isr_stub_table - .global isr_restore + .global syscall_return .global awd .extern idt_exception_handler @@ -8,6 +8,7 @@ .extern idt_pic_mouse .extern idt_pic_eoi .extern syscall_handler + .extern current_pcb .macro PUSHALL pushq %rax @@ -88,7 +89,7 @@ isr_stub_\num: cld movq %rsp, %rdi # top of stack callq syscall_handler - jmp isr_restore + jmp syscall_return .endm .macro PICGeneric num @@ -419,12 +420,18 @@ isr_stub_table: .code64 # isr restore -isr_restore: - movw $(0x20 | 3), %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs +syscall_return: + movq current_pcb, %rbx // return user stack + movq 0(%rbx), %rsp // esp + movq 8(%rbx), %rcx // pml4 + movq (%rcx), %rcx + movq %rcx, %cr3 + + movw $(0x20 | 3), %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs POPALL iretq diff --git a/kernel/include/comus/cpu.h b/kernel/include/comus/cpu.h index ffc1782..8f485be 100644 --- a/kernel/include/comus/cpu.h +++ b/kernel/include/comus/cpu.h @@ -89,4 +89,10 @@ void cpu_feats(struct cpu_feat *feats); */ void cpu_print_regs(struct cpu_regs *regs); +/** + * Return from a syscall handler back into userspace + */ +__attribute__((noreturn)) +void syscall_return(void); + #endif /* cpu.h */ diff --git a/kernel/procs.c b/kernel/procs.c index 9cde22f..8cd44a4 100644 --- a/kernel/procs.c +++ b/kernel/procs.c @@ -472,4 +472,6 @@ void dispatch(void) // set the process up for success current_pcb->state = PROC_STATE_RUNNING; current_pcb->ticks = 3; // ticks per process + + syscall_return(); } -- cgit v1.2.3-freya From 5d4601d864c03448e32a5b9e1748ce5010a28c44 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 11:21:20 -0400 Subject: noreturn on dispatch --- kernel/include/comus/procs.h | 1 + kernel/procs.c | 1 + 2 files changed, 2 insertions(+) (limited to 'kernel') diff --git a/kernel/include/comus/procs.h b/kernel/include/comus/procs.h index d105867..de65849 100644 --- a/kernel/include/comus/procs.h +++ b/kernel/include/comus/procs.h @@ -222,6 +222,7 @@ void schedule(struct pcb *pcb); /** * Select the next process to receive the CPU */ +__attribute__((noreturn)) void dispatch(void); #endif /* procs.h */ diff --git a/kernel/procs.c b/kernel/procs.c index 8cd44a4..f114f52 100644 --- a/kernel/procs.c +++ b/kernel/procs.c @@ -461,6 +461,7 @@ void schedule(struct pcb *pcb) panic("schedule insert fail"); } +__attribute__((noreturn)) void dispatch(void) { assert(current_pcb == NULL, "dispatch: current process is not null"); -- cgit v1.2.3-freya From 53c7f83e611d6b249097dcae1f748fc2fcc42173 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 11:22:24 -0400 Subject: fmt --- kernel/include/comus/cpu.h | 3 +-- kernel/include/comus/procs.h | 3 +-- kernel/memory/paging.c | 6 +++--- kernel/memory/physalloc.c | 2 +- kernel/memory/virtalloc.c | 12 ++++++++---- kernel/procs.c | 3 +-- 6 files changed, 15 insertions(+), 14 deletions(-) (limited to 'kernel') diff --git a/kernel/include/comus/cpu.h b/kernel/include/comus/cpu.h index 8f485be..3669000 100644 --- a/kernel/include/comus/cpu.h +++ b/kernel/include/comus/cpu.h @@ -92,7 +92,6 @@ void cpu_print_regs(struct cpu_regs *regs); /** * Return from a syscall handler back into userspace */ -__attribute__((noreturn)) -void syscall_return(void); +__attribute__((noreturn)) void syscall_return(void); #endif /* cpu.h */ diff --git a/kernel/include/comus/procs.h b/kernel/include/comus/procs.h index de65849..0150975 100644 --- a/kernel/include/comus/procs.h +++ b/kernel/include/comus/procs.h @@ -222,7 +222,6 @@ void schedule(struct pcb *pcb); /** * Select the next process to receive the CPU */ -__attribute__((noreturn)) -void dispatch(void); +__attribute__((noreturn)) void dispatch(void); #endif /* procs.h */ diff --git a/kernel/memory/paging.c b/kernel/memory/paging.c index c6e6a82..3453636 100644 --- a/kernel/memory/paging.c +++ b/kernel/memory/paging.c @@ -704,8 +704,8 @@ volatile void *pgdir_alloc(void) volatile void *pgdir_clone(volatile const void *old_pgdir, bool cow) { // TODO: - (void) old_pgdir; - (void) cow; + (void)old_pgdir; + (void)cow; return NULL; } @@ -827,7 +827,7 @@ void *mem_alloc_pages_at(mem_ctx_t ctx, size_t count, void *virt, if (map_pages((volatile struct pml4 *)ctx->pml4, (void *)virtual_address, phys_pages.pagestart, flags, phys_pages.num_pages)) { - assert(phys_start, "expected something allocated"); + assert(phys_start, "expected something allocated"); free_phys_pages(phys_start, count - pages_needed); return NULL; } diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 833c995..9fcbe8f 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -87,7 +87,7 @@ void *alloc_phys_pages_exact(size_t pages) if (bitmap == NULL || page_start == NULL) { // temporary bump allocator - void *addr = (void*)memory_start; + void *addr = (void *)memory_start; memory_start += PAGE_SIZE; return addr; } diff --git a/kernel/memory/virtalloc.c b/kernel/memory/virtalloc.c index 08a0758..cbde9b4 100644 --- a/kernel/memory/virtalloc.c +++ b/kernel/memory/virtalloc.c @@ -102,12 +102,14 @@ int virtaddr_clone(struct virt_ctx *old, struct virt_ctx *new) memcpy(new, old, sizeof(struct virt_ctx)); // allocate new space - new->alloc_nodes = kalloc(sizeof(struct virt_addr_node) * new->alloc_node_count); + new->alloc_nodes = + kalloc(sizeof(struct virt_addr_node) * new->alloc_node_count); if (new->alloc_nodes == NULL) return 1; // update prev/next in new allocation space - update_node_ptrs(old->alloc_nodes, new->alloc_nodes, old->alloc_node_count, new->alloc_node_count); + update_node_ptrs(old->alloc_nodes, new->alloc_nodes, old->alloc_node_count, + new->alloc_node_count); // update bootstrap nodes for (size_t i = 0; i < new->used_node_count; i++) { @@ -117,8 +119,10 @@ int virtaddr_clone(struct virt_ctx *old, struct virt_ctx *new) break; // get prev - prev = i > 0 ? &new->bootstrap_nodes[i-1] : NULL; - next = i < BOOTSTRAP_VIRT_ALLOC_NODES - 1 ? &new->bootstrap_nodes[i+1] : NULL; + prev = i > 0 ? &new->bootstrap_nodes[i - 1] : NULL; + next = i < BOOTSTRAP_VIRT_ALLOC_NODES - 1 ? + &new->bootstrap_nodes[i + 1] : + NULL; new->bootstrap_nodes[i].prev = prev; new->bootstrap_nodes[i].next = next; diff --git a/kernel/procs.c b/kernel/procs.c index f114f52..340739d 100644 --- a/kernel/procs.c +++ b/kernel/procs.c @@ -461,8 +461,7 @@ void schedule(struct pcb *pcb) panic("schedule insert fail"); } -__attribute__((noreturn)) -void dispatch(void) +__attribute__((noreturn)) void dispatch(void) { assert(current_pcb == NULL, "dispatch: current process is not null"); -- cgit v1.2.3-freya From 16f5d0443aa072d977517be45dcf9b0d12f584b4 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 11:36:23 -0400 Subject: load init from ata and run it --- Makefile | 2 +- kernel/main.c | 13 ++++++++++--- kernel/procs.c | 3 ++- kernel/syscall.c | 3 --- kernel/user.c | 4 ++++ 5 files changed, 17 insertions(+), 8 deletions(-) (limited to 'kernel') diff --git a/Makefile b/Makefile index 59b32d1..b40443d 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ UNAME := $(shell uname) QEMU = qemu-system-x86_64 QEMUOPTS = -cdrom $(BIN)/$(ISO) \ -no-reboot \ - -drive format=raw,file=$(BIN)/$(IMAGE)\ + -drive format=raw,file=$(BIN)/user/hello\ -serial mon:stdio \ -m 4G \ -name kern diff --git a/kernel/main.c b/kernel/main.c index 4047a64..c15c38d 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +22,7 @@ void kreport(void) gpu_report(); } -void main(long magic, volatile void *mboot) +__attribute__((noreturn)) void main(long magic, volatile void *mboot) { // initalize idt and pic cpu_init(); @@ -47,6 +48,12 @@ void main(long magic, volatile void *mboot) // report system state kreport(); - // halt - kprintf("halting...\n"); + // load init process + pcb_alloc(&init_pcb); + if (user_load(init_pcb, &fs_disks[0])) + panic("failed to load init"); + + // schedule and dispatch init + schedule(init_pcb); + dispatch(); } diff --git a/kernel/procs.c b/kernel/procs.c index 340739d..c1bcc4f 100644 --- a/kernel/procs.c +++ b/kernel/procs.c @@ -35,7 +35,7 @@ struct pcb *init_pcb = NULL; struct pcb ptable[N_PROCS]; /// next avaliable pid -pid_t next_pid = 0; +pid_t next_pid = 1; static struct pcb *find_prev_wakeup(pcb_queue_t queue, struct pcb *pcb) { @@ -119,6 +119,7 @@ int pcb_alloc(struct pcb **pcb) if (pcb_queue_pop(pcb_freelist, &tmp) != SUCCESS) return E_NO_PCBS; + tmp->pid = next_pid++; *pcb = tmp; return SUCCESS; } diff --git a/kernel/syscall.c b/kernel/syscall.c index 7944f46..12db401 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -91,7 +91,4 @@ void syscall_handler(struct cpu_regs *regs) // save return value current_pcb->regs->rax = ret; - - // switch back to process ctx - mem_ctx_switch(current_pcb->memctx); } diff --git a/kernel/user.c b/kernel/user.c index 592b35b..3c686a0 100644 --- a/kernel/user.c +++ b/kernel/user.c @@ -98,6 +98,10 @@ static int user_setup_stack(struct pcb *pcb) int user_load(struct pcb *pcb, struct disk *disk) { + // check inputs + if (pcb == NULL || disk == NULL) + return 1; + pcb->regs = NULL; // allocate memory context -- cgit v1.2.3-freya From 87e8de4f0050ec154286189783ddbd269fbcc7ee Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 11:45:15 -0400 Subject: poweroff syscall --- kernel/include/comus/syscalls.h | 3 ++- kernel/syscall.c | 23 ++++++++++++++++------- user/include/unistd.h | 7 +++++++ user/lib/syscall.S | 1 + user/poweroff.c | 6 ++++++ 5 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 user/poweroff.c (limited to 'kernel') diff --git a/kernel/include/comus/syscalls.h b/kernel/include/comus/syscalls.h index 3b9ea70..0105104 100644 --- a/kernel/include/comus/syscalls.h +++ b/kernel/include/comus/syscalls.h @@ -27,9 +27,10 @@ #define SYS_sleep 14 #define SYS_brk 15 #define SYS_sbrk 16 +#define SYS_poweroff 17 // UPDATE THIS DEFINITION IF MORE SYSCALLS ARE ADDED! -#define N_SYSCALLS 17 +#define N_SYSCALLS 18 // interrupt vector entry for system calls #define VEC_SYSCALL 0x80 diff --git a/kernel/syscall.c b/kernel/syscall.c index 12db401..00e6afb 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -52,13 +52,22 @@ static int sys_write(void) return nbytes; } +static int sys_poweroff(void) +{ + acpi_shutdown(); + return 1; +} + static int (*syscall_tbl[N_SYSCALLS])(void) = { - [SYS_exit] = sys_exit, [SYS_waitpid] = NULL, [SYS_fork] = NULL, - [SYS_exec] = NULL, [SYS_open] = NULL, [SYS_close] = NULL, - [SYS_read] = NULL, [SYS_write] = sys_write, [SYS_getpid] = NULL, - [SYS_getppid] = NULL, [SYS_gettime] = NULL, [SYS_getprio] = NULL, - [SYS_setprio] = NULL, [SYS_kill] = NULL, [SYS_sleep] = NULL, - [SYS_brk] = NULL, [SYS_sbrk] = NULL, + [SYS_exit] = sys_exit, [SYS_waitpid] = NULL, + [SYS_fork] = NULL, [SYS_exec] = NULL, + [SYS_open] = NULL, [SYS_close] = NULL, + [SYS_read] = NULL, [SYS_write] = sys_write, + [SYS_getpid] = NULL, [SYS_getppid] = NULL, + [SYS_gettime] = NULL, [SYS_getprio] = NULL, + [SYS_setprio] = NULL, [SYS_kill] = NULL, + [SYS_sleep] = NULL, [SYS_brk] = NULL, + [SYS_sbrk] = NULL, [SYS_poweroff] = sys_poweroff, }; void syscall_handler(struct cpu_regs *regs) diff --git a/user/include/unistd.h b/user/include/unistd.h index a815914..bbacdbe 100644 --- a/user/include/unistd.h +++ b/user/include/unistd.h @@ -162,4 +162,11 @@ extern void *brk(const void *addr); */ extern void *sbrk(intptr_t increment); +/** + * Poweroff the system. + * + * @return 1 on failure + */ +extern int poweroff(void); + #endif /* unistd.h */ diff --git a/user/lib/syscall.S b/user/lib/syscall.S index fc1ab93..c5a23b5 100644 --- a/user/lib/syscall.S +++ b/user/lib/syscall.S @@ -24,3 +24,4 @@ SYSCALL kill SYS_kill SYSCALL sleep SYS_sleep SYSCALL brk SYS_brk SYSCALL sbrk SYS_sbrk +SYSCALL poweroff SYS_poweroff diff --git a/user/poweroff.c b/user/poweroff.c new file mode 100644 index 0000000..f51a86f --- /dev/null +++ b/user/poweroff.c @@ -0,0 +1,6 @@ +#include + +int main(void) +{ + return poweroff(); +} -- cgit v1.2.3-freya