From 0337fca7495eb38467e225314dbb765a4a6c472f Mon Sep 17 00:00:00 2001 From: ngiddings Date: Fri, 19 Aug 2022 06:06:48 -0500 Subject: [PATCH 01/11] Moved syscall IDs to separate header --- include/kernel.h | 1 + include/syscalls.h | 13 ------------- include/types/syscallid.h | 17 +++++++++++++++++ src/kernel.c | 1 + 4 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 include/types/syscallid.h diff --git a/include/kernel.h b/include/kernel.h index 0efc5f5..1a3ad1e 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -6,6 +6,7 @@ #include "queue.h" #include "mmgr.h" #include "syscalls.h" +#include "types/syscallid.h" #include #define MAX_SYSCALL_ID 256 diff --git a/include/syscalls.h b/include/syscalls.h index 3efd400..d3372d5 100644 --- a/include/syscalls.h +++ b/include/syscalls.h @@ -3,19 +3,6 @@ #include #include -enum syscall_id_t -{ - SYSCALL_TEST = 1, - SYSCALL_YIELD, - SYSCALL_MMAP, - SYSCALL_MUNMAP, - SYSCALL_TERMINATE_SELF, - SYSCALL_SEND, - SYSCALL_RECEIVE, - SYSCALL_OPEN_PORT, - SYSCALL_CLOSE_PORT -}; - typedef union { long signed_int; diff --git a/include/types/syscallid.h b/include/types/syscallid.h new file mode 100644 index 0000000..fe95a5b --- /dev/null +++ b/include/types/syscallid.h @@ -0,0 +1,17 @@ +#ifndef QUARK_SYSCALLID_H +#define QUARK_SYSCALLID_H + +enum syscall_id_t +{ + SYSCALL_TEST = 1, + SYSCALL_MMAP, + SYSCALL_MUNMAP, + SYSCALL_YIELD, + SYSCALL_EXIT, + SYSCALL_SEND, + SYSCALL_RECEIVE, + SYSCALL_OPEN_PORT, + SYSCALL_CLOSE_PORT +}; + +#endif \ No newline at end of file diff --git a/src/kernel.c b/src/kernel.c index f9ebb83..6bcf553 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -11,6 +11,7 @@ #include "platform/context.h" #include "platform/putc.h" #include "types/status.h" +#include "types/syscallid.h" struct kernel_t kernel; From c36f65ce97ff01ab9455e25d57079e31a3644f5e Mon Sep 17 00:00:00 2001 From: ngiddings Date: Fri, 19 Aug 2022 22:34:45 -0500 Subject: [PATCH 02/11] Various renaming and refactoring --- include/kernel.h | 29 ++++--- include/syscalls.h | 4 +- include/types/status.h | 21 ++--- src/elf.c | 12 +-- src/heap.c | 4 +- src/kernel.c | 187 +++++++++++++++++++++++------------------ src/mmgr.c | 38 ++++----- src/priorityqueue.c | 12 +-- src/syscalls.c | 72 ++++++++++++---- src/x86/context.c | 2 +- src/x86/preempt.S | 2 +- 11 files changed, 227 insertions(+), 156 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index 1a3ad1e..493aea7 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -7,6 +7,7 @@ #include "mmgr.h" #include "syscalls.h" #include "types/syscallid.h" +#include "types/status.h" #include #define MAX_SYSCALL_ID 256 @@ -83,29 +84,35 @@ struct kernel_t void kernel_initialize(struct boot_info_t *boot_info); -int set_syscall(int id, int arg_count, int pid, void *func_ptr); +enum error_t set_syscall(int id, int arg_count, int pid, void *func_ptr); size_t do_syscall(enum syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags); -int load_module(struct module_t *module); +enum error_t kernel_load_module(struct module_t *module); -int active_process(); +unsigned long kernel_current_pid(); -int add_process(void *program_entry, int priority, physaddr_t address_space); +struct process_context_t *kernel_current_context(); -struct process_context_t *next_process(); +enum error_t kernel_store_active_context(struct process_context_t *context, size_t size); -int terminate_process(size_t process_id); +enum error_t kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space); -int store_active_context(struct process_context_t *context, size_t size); +struct process_context_t *kernel_advance_scheduler(); -struct process_context_t *get_active_context(); +enum error_t kernel_terminate_process(size_t process_id); -int open_port(unsigned long id); +enum error_t kernel_create_port(unsigned long id); -int close_port(unsigned long id); +enum error_t kernel_remove_port(unsigned long id); -int send_message(int recipient, struct message_t *message, int flags); +unsigned long kernel_get_port_owner(unsigned long id); + +enum error_t kernel_send_message(int recipient, struct message_t *message); + +enum error_t kernel_queue_sender(int recipient); + +enum error_t kernel_queue_message(int recipient, struct message_t *message); int receive_message(struct message_t *buffer, int flags); diff --git a/include/syscalls.h b/include/syscalls.h index d3372d5..7787fce 100644 --- a/include/syscalls.h +++ b/include/syscalls.h @@ -44,6 +44,6 @@ size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags) size_t receive(syscall_arg_t buffer, syscall_arg_t flags); -size_t openport(syscall_arg_t id); +size_t open_port(syscall_arg_t id); -size_t closeport(syscall_arg_t id); +size_t close_port(syscall_arg_t id); diff --git a/include/types/status.h b/include/types/status.h index d80bfef..c371b01 100644 --- a/include/types/status.h +++ b/include/types/status.h @@ -1,14 +1,15 @@ #pragma once -enum status_t +enum error_t { - S_OK = 0, - S_NULL_POINTER, - S_OUT_OF_BOUNDS, - S_INVALID_ARGUMENT, - S_OUT_OF_MEMORY, - S_DOESNT_EXIST, - S_EXISTS, - S_BAD_SYSCALL, - S_BUSY + ENONE = 0, + ENULLPTR, + EOUTOFBOUNDS, + EINVALIDARG, + ENOMEM, + EDOESNTEXIST, + EEXISTS, + ENOSYSCALL, + EPERM, + EBUSY }; \ No newline at end of file diff --git a/src/elf.c b/src/elf.c index e04110b..ace95da 100644 --- a/src/elf.c +++ b/src/elf.c @@ -17,18 +17,18 @@ int load_program(struct elf_file_header_t *elf) for(size_t n = 0; n < program_header->memsize; n += page_size) { physaddr_t page = reserve_page(); - if(page == S_OUT_OF_MEMORY) + if(page == ENOMEM) { - return S_OUT_OF_MEMORY; + return ENOMEM; } int status = map_page(d + n, page, PAGE_RW | PAGE_USERMODE | PAGE_EXECUTABLE); switch(status) { - case S_OUT_OF_MEMORY: + case ENOMEM: return status; - case S_OUT_OF_BOUNDS: + case EOUTOFBOUNDS: return status; - case S_OK: + case ENONE: memcpy(d + n, s + n, n + page_size < program_header->memsize ? page_size : program_header->memsize - n); } } @@ -36,5 +36,5 @@ int load_program(struct elf_file_header_t *elf) count--; program_header = (struct elf_program_header_t*)((void*)program_header + elf->phsize); } - return S_OK; + return ENONE; } \ No newline at end of file diff --git a/src/heap.c b/src/heap.c index 96de07e..9eec40e 100644 --- a/src/heap.c +++ b/src/heap.c @@ -103,7 +103,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size if((flags & PAGE_PRESENT) == 0) { int status = map_page((void*)heap->header + i, reserve_page(), PAGE_RW); - if(status != S_OK) + if(status != ENONE) { return status; } @@ -129,7 +129,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size heap->header[i + (1 << heap->tree_height)].state = UNAVAIL; } } - return S_OK; + return ENONE; } void *heap_allocate(struct heap_t *heap, size_t size) diff --git a/src/kernel.c b/src/kernel.c index 6bcf553..93189db 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -32,7 +32,7 @@ void kernel_initialize(struct boot_info_t *boot_info) kernel.next_pid = 1; kernel.process_table = NULL; kernel.port_table = NULL; - if(construct_priority_queue(&kernel.priority_queue, 512) != S_OK) + if(construct_priority_queue(&kernel.priority_queue, 512) != ENONE) { panic("Failed to construct priority queue."); } @@ -42,63 +42,63 @@ void kernel_initialize(struct boot_info_t *boot_info) set_syscall(SYSCALL_MUNMAP, 2, 0, munmap); set_syscall(SYSCALL_SEND, 3, 0, send); set_syscall(SYSCALL_RECEIVE, 2, 0, receive); - set_syscall(SYSCALL_OPEN_PORT, 1, 0, openport); - set_syscall(SYSCALL_CLOSE_PORT, 1, 0, closeport); + set_syscall(SYSCALL_OPEN_PORT, 1, 0, open_port); + set_syscall(SYSCALL_CLOSE_PORT, 1, 0, close_port); for(int i = 0; i < boot_info->module_count; i++) { - if(load_module(&boot_info->modules[i]) != S_OK) + if(kernel_load_module(&boot_info->modules[i]) != ENONE) { panic("Failed to load modules."); } } - if(initialize_interrupts() != S_OK) + if(initialize_interrupts() != ENONE) { panic("Failed to initialize interrupts."); } irq_enable(); - load_context(next_process()); + load_context(kernel_advance_scheduler()); } -int set_syscall(int id, int arg_count, int pid, void *func_ptr) +enum error_t set_syscall(int id, int arg_count, int pid, void *func_ptr) { if(id < 0 || id > MAX_SYSCALL_ID) { - return S_OUT_OF_BOUNDS; + return EOUTOFBOUNDS; } else if(kernel.syscall_table[id].defined) { - return S_INVALID_ARGUMENT; + return EINVALIDARG; } else if(arg_count < 0 || arg_count > 3) { - return S_INVALID_ARGUMENT; + return EINVALIDARG; } else if(pid != 0 && avl_get(kernel.process_table, pid) == NULL) { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } else if(func_ptr == NULL) { - return S_NULL_POINTER; + return ENULLPTR; } kernel.syscall_table[id].defined = true; kernel.syscall_table[id].arg_count = arg_count; kernel.syscall_table[id].process_id = pid; kernel.syscall_table[id].func_ptr_0 = func_ptr; - return S_OK; + return ENONE; } size_t do_syscall(enum syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, void *pc, void *stack, unsigned long flags) { if(id < 0 || id > MAX_SYSCALL_ID) { - return S_BAD_SYSCALL; + return ENOSYSCALL; } else if(!kernel.syscall_table[id].defined) { - return S_BAD_SYSCALL; + return ENOSYSCALL; } bool switched_address_space = false; if(kernel.syscall_table[id].process_id > 0) @@ -107,7 +107,7 @@ size_t do_syscall(enum syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, if(callee == NULL) { kernel.syscall_table[id].defined = false; - return S_BAD_SYSCALL; + return ENOSYSCALL; } paging_load_address_space(callee->page_table); switched_address_space = true; @@ -138,10 +138,10 @@ size_t do_syscall(enum syscall_id_t id, syscall_arg_t arg1, syscall_arg_t arg2, return result; } -int load_module(struct module_t *module) +enum error_t kernel_load_module(struct module_t *module) { physaddr_t module_address_space = create_address_space(); - if(module_address_space == S_OUT_OF_MEMORY) { + if(module_address_space == ENOMEM) { panic("failed to create address space for module: out of memory"); } paging_load_address_space(module_address_space); @@ -152,9 +152,9 @@ int load_module(struct module_t *module) int status = map_page(load_base + load_offset, p, PAGE_RW); switch(status) { - case S_OUT_OF_MEMORY: + case ENOMEM: panic("ran out of memory while mapping module"); - case S_OUT_OF_BOUNDS: + case EOUTOFBOUNDS: panic("got out-of-bounds error while mapping module"); } load_offset += page_size; @@ -163,9 +163,9 @@ int load_module(struct module_t *module) int status = load_program(load_base); switch(status) { - case S_OUT_OF_MEMORY: + case ENOMEM: panic("ran out of memory while reading ELF file"); - case S_OUT_OF_BOUNDS: + case EOUTOFBOUNDS: panic("got out-of-bounds error while reading ELF file"); } void *module_entry = ((struct elf_file_header_t*)load_base)->entry; @@ -176,16 +176,16 @@ int load_module(struct module_t *module) int status = unmap_page(load_base + load_offset); switch(status) { - case S_OUT_OF_MEMORY: + case ENOMEM: panic("ran out of memory while unmapping module"); - case S_OUT_OF_BOUNDS: + case EOUTOFBOUNDS: panic("got out-of-bounds error while unmapping module"); } load_offset += page_size; } - if(add_process(module_entry, 1, current_address_space()) > 0) + if(kernel_spawn_process(module_entry, 1, current_address_space()) > 0) { - return S_OK; + return ENONE; } else { @@ -193,7 +193,7 @@ int load_module(struct module_t *module) } } -int active_process() +unsigned long kernel_current_pid() { if(kernel.active_process == NULL) { @@ -205,7 +205,19 @@ int active_process() } } -int add_process(void *program_entry, int priority, physaddr_t address_space) +struct process_context_t *kernel_current_context() +{ + if(kernel.active_process == NULL) + { + return NULL; + } + else + { + return kernel.active_process->ctx; + } +} + +enum error_t kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space) { struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t)); if(new_process == NULL) @@ -227,7 +239,7 @@ int add_process(void *program_entry, int priority, physaddr_t address_space) return new_process->pid; } -struct process_context_t *next_process() +struct process_context_t *kernel_advance_scheduler() { if(kernel.active_process != NULL) { @@ -243,12 +255,12 @@ struct process_context_t *next_process() panic("no processes available to enter!"); } -int terminate_process(size_t process_id) +enum error_t kernel_terminate_process(size_t process_id) { struct process_t *process = avl_get(kernel.process_table, process_id); if(process == NULL) { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } if(kernel.active_process == process) { @@ -258,79 +270,72 @@ int terminate_process(size_t process_id) priorityqueue_remove(&kernel.priority_queue, process); destroy_context(process->ctx); kfree(process); - return S_OK; + return ENONE; } -int store_active_context(struct process_context_t *context, size_t size) +enum error_t kernel_store_active_context(struct process_context_t *context, size_t size) { if(kernel.active_process != NULL && kernel.active_process->ctx != NULL) { memcpy(kernel.active_process->ctx, context, size); - return S_OK; + return ENONE; } else { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } } -int open_port(unsigned long id) +enum error_t kernel_create_port(unsigned long id) { if(avl_get(kernel.port_table, id) != NULL) { - return S_EXISTS; + return EEXISTS; } printf("opening port %i -> %i\n", id, kernel.active_process->pid); struct port_t *port = kmalloc(sizeof(struct port_t)); port->id = id; port->owner_pid = kernel.active_process->pid; kernel.port_table = avl_insert(kernel.port_table, id, port); - return S_OK; + return ENONE; } -int close_port(unsigned long id) +enum error_t kernel_remove_port(unsigned long id) { struct port_t *port = avl_get(kernel.port_table, id); if(port == NULL) { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } else if(port->owner_pid != kernel.active_process->pid) { - return S_INVALID_ARGUMENT; + return EPERM; } printf("closing port %i attached to %i\n", id, kernel.active_process->pid); kernel.port_table = avl_remove(kernel.port_table, id); kfree(port); - return S_OK; + return ENONE; } -int send_message(int recipient, struct message_t *message, int flags) +unsigned long kernel_get_port_owner(unsigned long id) { - int op_type = flags & IO_OP; - int dest_type = flags & IO_RECIPIENT_TYPE; - if((flags & ~(IO_OP | IO_RECIPIENT_TYPE)) != 0 || dest_type >= IO_MAILBOX) + struct port_t *port = avl_get(kernel.port_table, id); + if(port == NULL) { - printf("Invalid flags on send_message\n"); - return S_INVALID_ARGUMENT; + return 0; } - if(dest_type == IO_PORT) + else { - struct port_t *port = avl_get(kernel.port_table, recipient); - if(port != NULL) - { - recipient = port->owner_pid; - } - else - { - printf("Port %i does not exist\n", recipient); - return S_DOESNT_EXIST; - } + return port->owner_pid; } +} + +enum error_t kernel_send_message(int recipient, struct message_t *message) +{ struct process_t *dest = avl_get(kernel.process_table, recipient); if(dest == NULL) { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } else if(dest->message_buffer != NULL) { @@ -343,29 +348,51 @@ int send_message(int recipient, struct message_t *message, int flags) paging_load_address_space(kernel.active_process->page_table); dest->message_buffer = NULL; dest->state = PROCESS_ACTIVE; - set_context_return(dest->ctx, S_OK); + set_context_return(dest->ctx, ENONE); priorityqueue_insert(&kernel.priority_queue, dest, dest->priority); - return S_OK; - } - else if(op_type == IO_ASYNC) - { - printf("Queueing message from %i to %i\n", kernel.active_process->pid, dest->pid); - struct message_t *queued_msg = kmalloc(sizeof(struct message_t)); - if(queued_msg == NULL) - { - return S_OUT_OF_MEMORY; - } - memcpy(queued_msg, message, sizeof(struct message_t)); - queue_insert(&dest->message_queue, queued_msg); - return S_OK; + return ENONE; } else + { + return EBUSY; + } +} + +enum error_t kernel_queue_sender(int recipient) +{ + struct process_t *dest = avl_get(kernel.process_table, recipient); + if(dest != NULL) { printf("Queueing process %i to %i\n", kernel.active_process->pid, dest->pid); queue_insert(&dest->sending_queue, kernel.active_process); kernel.active_process->state = PROCESS_SENDING; kernel.active_process = NULL; - load_context(next_process()); + return ENONE; + } + else + { + return EDOESNTEXIST; + } +} + +enum error_t kernel_queue_message(int recipient, struct message_t *message) +{ + struct process_t *dest = avl_get(kernel.process_table, recipient); + if(dest != NULL) + { + printf("Queueing message from %i to %i\n", kernel.active_process->pid, dest->pid); + struct message_t *queued_msg = kmalloc(sizeof(struct message_t)); + if(queued_msg == NULL) + { + return ENOMEM; + } + memcpy(queued_msg, message, sizeof(struct message_t)); + queue_insert(&dest->message_queue, queued_msg); + return ENONE; + } + else + { + return EDOESNTEXIST; } } @@ -381,27 +408,27 @@ int receive_message(struct message_t *buffer, int flags) paging_load_address_space(kernel.active_process->page_table); memcpy(buffer, &kernel_buffer, sizeof(struct message_t)); sender->state = PROCESS_ACTIVE; - set_context_return(sender->ctx, S_OK); + set_context_return(sender->ctx, ENONE); priorityqueue_insert(&kernel.priority_queue, sender, sender->priority); - return S_OK; + return ENONE; } else if(kernel.active_process->message_queue.count > 0) { struct message_t *queued_msg = queue_get_next(&kernel.active_process->message_queue); memcpy(buffer, queued_msg, sizeof(struct message_t)); kfree(queued_msg); - return S_OK; + return ENONE; } else if((flags & IO_OP) == IO_ASYNC) { - return S_DOESNT_EXIST; + return EDOESNTEXIST; } else { kernel.active_process->message_buffer = buffer; kernel.active_process->state = PROCESS_REQUESTING; kernel.active_process = NULL; - load_context(next_process()); + load_context(kernel_advance_scheduler()); } } diff --git a/src/mmgr.c b/src/mmgr.c index 0525633..6a6113c 100644 --- a/src/mmgr.c +++ b/src/mmgr.c @@ -54,15 +54,15 @@ int initialize_page_stack(struct memory_map_t *map, physaddr_t *stack_base) size_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1); while(location + page_size <= map->array[i].location + map->array[i].size) { - if(free_page(location) != S_OK) + if(free_page(location) != ENONE) { - return S_OUT_OF_MEMORY; + return ENOMEM; } page_stack.total_pages++; location += page_size; } } - return S_OK; + return ENONE; } physaddr_t reserve_page() @@ -74,7 +74,7 @@ physaddr_t reserve_page() *page_stack.stack_pointer = (physaddr_t) 0; return frame; } - return S_OUT_OF_MEMORY; + return ENOMEM; } int free_page(physaddr_t location) @@ -83,21 +83,21 @@ int free_page(physaddr_t location) { *page_stack.stack_pointer = location; page_stack.stack_pointer++; - return S_OK; + return ENONE; } else { switch(map_page(page_stack.limit_pointer, location, PAGE_RW)) { - case S_OUT_OF_MEMORY: - return S_OUT_OF_MEMORY; - case S_OUT_OF_BOUNDS: - return S_OUT_OF_BOUNDS; - case S_OK: + case ENOMEM: + return ENOMEM; + case EOUTOFBOUNDS: + return EOUTOFBOUNDS; + case ENONE: page_stack.limit_pointer += page_size / sizeof(*page_stack.limit_pointer); - return S_OK; + return ENONE; } - return S_OUT_OF_MEMORY; + return ENOMEM; } } @@ -120,9 +120,9 @@ physaddr_t create_address_space() { physaddr_t table = reserve_page(); int result; - if (table == S_OUT_OF_MEMORY) + if (table == ENOMEM) { - return S_OUT_OF_MEMORY; + return ENOMEM; } else if((result = paging_init_top_table(table))) { @@ -143,7 +143,7 @@ int map_page(void *page, physaddr_t frame, int flags) { if (frame % page_size != 0) { - return S_INVALID_ARGUMENT; + return EINVALIDARG; } for(int level = 0; level < page_table_levels - 1; level++) { @@ -151,16 +151,16 @@ int map_page(void *page, physaddr_t frame, int flags) if(present == 0) { physaddr_t new_table = reserve_page(); - if(new_table == S_OUT_OF_MEMORY) + if(new_table == ENOMEM) { - return S_OUT_OF_MEMORY; + return ENOMEM; } set_pte(page, level, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table); wipe_page_table(page, level + 1); } } set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame); - return S_OK; + return ENONE; } physaddr_t unmap_page(void *page) @@ -168,7 +168,7 @@ physaddr_t unmap_page(void *page) for(int level = 0; level < page_table_levels; level++) { if((get_pte_type(page, level) & PAGE_PRESENT) == 0) - return S_OUT_OF_BOUNDS; + return EOUTOFBOUNDS; } physaddr_t frame = get_pte_address(page, page_table_levels - 1); set_pte(page, page_table_levels - 1, 0, 0); diff --git a/src/priorityqueue.c b/src/priorityqueue.c index 1396d0b..d3ffae0 100644 --- a/src/priorityqueue.c +++ b/src/priorityqueue.c @@ -31,11 +31,11 @@ int construct_priority_queue(struct priority_queue_t *queue, int capacity) queue->heap = kmalloc(sizeof(struct priority_queue_node_t) * capacity); if(queue->heap == NULL) { - return S_OUT_OF_MEMORY; + return ENOMEM; } queue->capacity = capacity; queue->size = 0; - return S_OK; + return ENONE; } void *priorityqueue_extract_min(struct priority_queue_t *queue) @@ -52,7 +52,7 @@ void *priorityqueue_extract_min(struct priority_queue_t *queue) int priorityqueue_insert(struct priority_queue_t *queue, void *value, int priority) { if(queue->size == queue->capacity) - return S_OUT_OF_MEMORY; + return ENOMEM; size_t i = queue->size; queue->size++; while(i > 0 && queue->heap[(i - 1) / 2].priority > priority) @@ -62,7 +62,7 @@ int priorityqueue_insert(struct priority_queue_t *queue, void *value, int priori } queue->heap[i].priority = priority; queue->heap[i].value = value; - return S_OK; + return ENONE; } int priorityqueue_remove(struct priority_queue_t *queue, void *value) @@ -74,8 +74,8 @@ int priorityqueue_remove(struct priority_queue_t *queue, void *value) queue->size--; queue->heap[i] = queue->heap[queue->size]; heapify(queue, i); - return S_OK; + return ENONE; } } - return S_OUT_OF_BOUNDS; + return EOUTOFBOUNDS; } \ No newline at end of file diff --git a/src/syscalls.c b/src/syscalls.c index eae5696..4153084 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -2,6 +2,7 @@ #include "kernel.h" #include "mmgr.h" #include "stdio.h" +#include "platform/context.h" #include "types/status.h" size_t test_syscall(syscall_arg_t str) @@ -17,29 +18,29 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t unsigned long flags = arg_flags.unsigned_int; if(location % page_size != 0 || length % page_size != 0) { - return S_INVALID_ARGUMENT; + return EINVALIDARG; } else if(location == NULL) { - return S_NULL_POINTER; + return ENULLPTR; } for(size_t i = 0; i < length; i += page_size) { if(page_type((void*)(location + i)) & PAGE_PRESENT) { - return S_EXISTS; + return EEXISTS; } } size_t n = 0; - int status = S_OK; - while(n < length && status == S_OK) + int status = ENONE; + while(n < length && status == ENONE) { physaddr_t frame = reserve_page(); status = frame % page_size; - if(status == S_OK) + if(status == ENONE) { status = map_page((void*)(location + n), frame, PAGE_USERMODE | PAGE_RW); - if(status != S_OK && free_page(frame) != S_OK) + if(status != ENONE && free_page(frame) != ENONE) { panic("critical error reached during mmap."); } @@ -50,7 +51,7 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t break; } } - if(status != S_OK && munmap(arg_location, arg_length) != S_OK) + if(status != ENONE && munmap(arg_location, arg_length) != ENONE) { panic("critical error reached during mmap."); } @@ -63,15 +64,15 @@ size_t munmap(syscall_arg_t arg_location, syscall_arg_t arg_length) unsigned long length = arg_length.unsigned_int; if(location % page_size != 0 || length % page_size != 0) { - return S_INVALID_ARGUMENT; + return EINVALIDARG; } else if(location == 0) { - return S_NULL_POINTER; + return ENULLPTR; } size_t n = 0; - int status = S_OK; - while(n < length && status == S_OK) + int status = ENONE; + while(n < length && status == ENONE) { int type = page_type((void*)(location + n)); physaddr_t frame; @@ -89,12 +90,47 @@ size_t munmap(syscall_arg_t arg_location, syscall_arg_t arg_length) size_t terminate_self() { - return terminate_process(active_process()); + return kernel_terminate_process(kernel_current_pid()); } size_t send(syscall_arg_t recipient, syscall_arg_t message, syscall_arg_t flags) { - return send_message(recipient.unsigned_int, message.ptr, flags.unsigned_int); + unsigned long op_type = flags.unsigned_int & IO_OP; + unsigned long dest_type = flags.unsigned_int & IO_RECIPIENT_TYPE; + if((flags.unsigned_int & ~(IO_OP | IO_RECIPIENT_TYPE)) != 0 || dest_type >= IO_MAILBOX) + { + printf("Invalid flags on send()\n"); + return EINVALIDARG; + } + if(dest_type == IO_PORT) + { + recipient.unsigned_int = kernel_get_port_owner(recipient.unsigned_int); + if(recipient.unsigned_int == 0) + { + return EDOESNTEXIST; + } + } + enum error_t status = kernel_send_message(recipient.unsigned_int, message.ptr); + if(status == EBUSY && op_type == IO_SYNC) + { + status = kernel_queue_sender(recipient.unsigned_int); + if(status) + { + return status; + } + else + { + load_context(kernel_advance_scheduler()); + } + } + else if(status == EBUSY && op_type == IO_ASYNC) + { + return kernel_queue_message(recipient.unsigned_int, message.ptr); + } + else + { + return status; + } } size_t receive(syscall_arg_t buffer, syscall_arg_t flags) @@ -102,12 +138,12 @@ size_t receive(syscall_arg_t buffer, syscall_arg_t flags) return receive_message(buffer.ptr, flags.unsigned_int); } -size_t openport(syscall_arg_t id) +size_t open_port(syscall_arg_t id) { - return open_port(id.unsigned_int); + return kernel_create_port(id.unsigned_int); } -size_t closeport(syscall_arg_t id) +size_t close_port(syscall_arg_t id) { - return close_port(id.unsigned_int); + return kernel_remove_port(id.unsigned_int); } diff --git a/src/x86/context.c b/src/x86/context.c index 0f37597..6760ccd 100644 --- a/src/x86/context.c +++ b/src/x86/context.c @@ -41,7 +41,7 @@ void destroy_context(void *ctx) void save_context(struct process_context_t *context) { - store_active_context(context, sizeof(*context)); + kernel_store_active_context(context, sizeof(*context)); } void set_context_pc(struct process_context_t *context, void *pc) diff --git a/src/x86/preempt.S b/src/x86/preempt.S index 5814fe7..e5d6598 100644 --- a/src/x86/preempt.S +++ b/src/x86/preempt.S @@ -87,7 +87,7 @@ isr_preempt: call save_context mov %ebp, %esp - call next_process + call kernel_advance_scheduler push %eax call load_context From 21509be9bc0a758ad39b21f38689a9d4e6a85d2b Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 08:41:53 -0500 Subject: [PATCH 03/11] Added 'math.c' for optimized math and bit operations --- include/math.h | 8 ++++++ src/heap.c | 22 +++-------------- src/math.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 include/math.h create mode 100644 src/math.c diff --git a/include/math.h b/include/math.h new file mode 100644 index 0000000..d86cf0a --- /dev/null +++ b/include/math.h @@ -0,0 +1,8 @@ +#ifndef _QUARK_UTIL_H +#define _QUARK_UTIL_H + +int ilog2(unsigned int x); + +int llog2(unsigned long x); + +#endif \ No newline at end of file diff --git a/src/heap.c b/src/heap.c index 9eec40e..488fa5a 100644 --- a/src/heap.c +++ b/src/heap.c @@ -1,6 +1,7 @@ #include #include "heap.h" #include "mmgr.h" +#include "math.h" #include "types/status.h" #define AVAIL 0 @@ -23,23 +24,6 @@ struct heap_node_t size_t state : 2; } __attribute__ ((packed)); -size_t ilog2(size_t n) -{ - size_t m = n; - size_t count = 0; - bool isPowerOfTwo = true; - while(m) - { - if((m & 1) == 1 && m > 1) - { - isPowerOfTwo = false; - } - count++; - m >>= 1; - } - return count - (isPowerOfTwo ? 1 : 0); -} - size_t find_free_block(struct heap_t *heap, size_t height) { if(height > heap->tree_height) @@ -95,7 +79,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size heap->header = (struct heap_node_t*) start; heap->heap_size = heap_size; heap->block_size = block_size; - heap->tree_height = ilog2(heap_size / block_size); + heap->tree_height = llog2(heap_size / block_size); size_t header_size = (heap_size / block_size) << 1; for(size_t i = 1; i <= (heap_size / block_size) * 2; i++) { @@ -136,7 +120,7 @@ void *heap_allocate(struct heap_t *heap, size_t size) { size += heap->block_size - 1; size -= size % heap->block_size; - size_t height = ilog2(size / heap->block_size); + size_t height = llog2(size / heap->block_size); size_t index = find_free_block(heap, height); if(index) { diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..b8ec08e --- /dev/null +++ b/src/math.c @@ -0,0 +1,66 @@ +#include "math.h" + +int ilog2(unsigned int x) +{ +#if defined __GNUC__ + if(x <= 1) + return 0; + return 32 - __builtin_clzl(x - 1); +#else + static const int table[32] = { + 0, 9, 1, 10, 13, 21, 2, 29, + 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, + 19, 27, 23, 6, 26, 5, 4, 31}; + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return table[(x * 0x07C4ACDD) >> 27]; +#endif +} + +int llog2(unsigned long x) +{ +#if (defined __GNUC__) && (__SIZEOF_LONG__ == 4) + if(x <= 1) + return 0; + return 32 - __builtin_clzl(x - 1); +#elif (defined __GNUC__) && (__SIZEOF_LONG__ == 8) + if(x <= 1) + return 0; + return 64 - __buildin_clzl(x - 1); +#elif __SIZEOF_LONG__ == 8 + static const int table[64] = { + 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, + 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, + 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, + 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5, 63}; + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x |= x >> 32; + + return table[(x * 0x03f6eaf2cd271461) >> 58]; +#else + static const int table[32] = { + 0, 9, 1, 10, 13, 21, 2, 29, + 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, + 19, 27, 23, 6, 26, 5, 4, 31}; + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return table[(x * 0x07C4ACDD) >> 27]; +#endif +} \ No newline at end of file From b8cec93721217ea779b2d7c2c8d75c734559fd1d Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 08:42:59 -0500 Subject: [PATCH 04/11] Put 'math.c' into Makefile.am --- src/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index db5a2ae..9bd5478 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ noinst_PROGRAMS = quark-kernel -quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c memmap.c avltree.c queue.c +quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c memmap.c avltree.c queue.c math.c quark_kernel_LDADD = -lgcc quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb quark_kernel_LDFLAGS = -nostdlib From 2cc9c6942e390a6426105c90d4ff83eabbf240c9 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 08:46:34 -0500 Subject: [PATCH 05/11] Fixed call to __builtin_clz() ilog2() erroneously called __builtin_clzl() instead of __builtin_clz() --- src/math.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math.c b/src/math.c index b8ec08e..5275c83 100644 --- a/src/math.c +++ b/src/math.c @@ -5,7 +5,7 @@ int ilog2(unsigned int x) #if defined __GNUC__ if(x <= 1) return 0; - return 32 - __builtin_clzl(x - 1); + return 32 - __builtin_clz(x - 1); #else static const int table[32] = { 0, 9, 1, 10, 13, 21, 2, 29, From f76b8cab4327f9ee58f4a06d17133ea9edd3d082 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 12:55:07 -0500 Subject: [PATCH 06/11] Changed #include guards in physaddr.h --- include/types/physaddr.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/types/physaddr.h b/include/types/physaddr.h index fb00318..6631873 100644 --- a/include/types/physaddr.h +++ b/include/types/physaddr.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef _QUARK_PHYSADDR_H +#define _QUARK_PHYSADDR_H #include @@ -8,4 +9,6 @@ typedef uint32_t physaddr_t; typedef uint64_t physaddr_t; #else typedef uint64_t physaddr_t; +#endif + #endif \ No newline at end of file From 9c37167c5ecd14d729e32b13c51b786b2824b3ff Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 12:55:39 -0500 Subject: [PATCH 07/11] Changed #include guards in physaddr.h, added now error codes --- include/types/status.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/types/status.h b/include/types/status.h index c371b01..60eec73 100644 --- a/include/types/status.h +++ b/include/types/status.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef _QUARK_ERROR_H +#define _QUARK_ERROR_H enum error_t { @@ -7,9 +8,13 @@ enum error_t EOUTOFBOUNDS, EINVALIDARG, ENOMEM, + ENOTPRESENT, EDOESNTEXIST, EEXISTS, ENOSYSCALL, EPERM, - EBUSY -}; \ No newline at end of file + EBUSY, + EEXITED +}; + +#endif \ No newline at end of file From 2f7d39e679d1c6b6ae9e28028a9c590819593cf7 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 12:56:45 -0500 Subject: [PATCH 08/11] Improved error handling in queue.c --- include/queue.h | 11 +++++++---- src/queue.c | 11 ++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/queue.h b/include/queue.h index 97ac60a..6b8368a 100644 --- a/include/queue.h +++ b/include/queue.h @@ -1,5 +1,7 @@ #pragma once +#include "types/status.h" + struct queue_node_t; /** @@ -38,15 +40,16 @@ void queue_construct(struct queue_t *queue); * @brief Inserts a new item at the end of the queue. * * @param queue - * @param ptr + * @param ptr A pointer to some user-defined data to store on the queue. + * @return enum error_t */ -void queue_insert(struct queue_t *queue, void *ptr); +enum error_t queue_insert(struct queue_t *queue, void *ptr); /** * @brief Removes the next item from the queue and returns it. * * @param queue - * @return void* + * @return void* The pointer stored at the front of the queue. */ void *queue_get_next(struct queue_t *queue); @@ -54,7 +57,7 @@ void *queue_get_next(struct queue_t *queue); * @brief Returns the next item on the queue without removing it. * * @param queue - * @return void* + * @return void* The pointer stored at the front of the queue. */ void *queue_peek(struct queue_t *queue); diff --git a/src/queue.c b/src/queue.c index dc51873..492f257 100644 --- a/src/queue.c +++ b/src/queue.c @@ -23,9 +23,13 @@ void queue_construct(struct queue_t *queue) queue->count = 0; } -void queue_insert(struct queue_t *queue, void *ptr) +enum error_t queue_insert(struct queue_t *queue, void *ptr) { struct queue_node_t *node = kmalloc(sizeof(struct queue_node_t)); + if(node == NULL) + { + return ENOMEM; + } node->ptr = ptr; node->next = NULL; if(queue->last == NULL) @@ -38,11 +42,16 @@ void queue_insert(struct queue_t *queue, void *ptr) queue->last = node; } queue->count++; + return ENONE; } void *queue_get_next(struct queue_t *queue) { struct queue_node_t *node = queue->first; + if(node == NULL) + { + return NULL; + } queue->first = node->next; if(queue->first == NULL) { From 54e2beefc1dafea15516e505e9e30eb0f432aeb1 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 12:58:28 -0500 Subject: [PATCH 09/11] Fixed warning in syscall.c Cast NULL to 'unsigned long' to get rid of compiler warning. --- src/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syscalls.c b/src/syscalls.c index 4153084..c36954c 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -20,7 +20,7 @@ size_t mmap(syscall_arg_t arg_location, syscall_arg_t arg_length, syscall_arg_t { return EINVALIDARG; } - else if(location == NULL) + else if(location == (unsigned long)NULL) { return ENULLPTR; } From bacedbea868f0940c9c8651c171db0ffd5198614 Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 13:00:26 -0500 Subject: [PATCH 10/11] Rewrote physical memory allocator The physical memory allocator now uses a buddy allocator instead of a stack. Also moved some of the platform-independent context code to kernel.c. --- include/kernel.h | 13 +- include/mmgr.h | 37 ++++-- include/platform/context.h | 36 ++++-- include/x86/processstate.h | 21 ---- src/kernel.c | 39 ++++-- src/mmgr.c | 251 +++++++++++++++++++++++++++++-------- src/x86/context.c | 45 +------ src/x86/load_context.S | 7 +- src/x86/multiboot2.c | 11 ++ src/x86/preempt.S | 10 +- 10 files changed, 314 insertions(+), 156 deletions(-) delete mode 100644 include/x86/processstate.h diff --git a/include/kernel.h b/include/kernel.h index 493aea7..6908394 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -35,8 +35,9 @@ struct boot_info_t { char *bootloader; char *parameters; - size_t module_count; + size_t memory_size; struct memory_map_t map; + size_t module_count; struct module_t modules[module_limit]; }; @@ -94,9 +95,9 @@ unsigned long kernel_current_pid(); struct process_context_t *kernel_current_context(); -enum error_t kernel_store_active_context(struct process_context_t *context, size_t size); +enum error_t kernel_store_active_context(struct process_context_t *context); -enum error_t kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space); +unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space); struct process_context_t *kernel_advance_scheduler(); @@ -108,11 +109,11 @@ enum error_t kernel_remove_port(unsigned long id); unsigned long kernel_get_port_owner(unsigned long id); -enum error_t kernel_send_message(int recipient, struct message_t *message); +enum error_t kernel_send_message(unsigned long recipient, struct message_t *message); -enum error_t kernel_queue_sender(int recipient); +enum error_t kernel_queue_sender(unsigned long recipient); -enum error_t kernel_queue_message(int recipient, struct message_t *message); +enum error_t kernel_queue_message(unsigned long recipient, struct message_t *message); int receive_message(struct message_t *buffer, int flags); diff --git a/include/mmgr.h b/include/mmgr.h index 7df2fb5..731afb9 100644 --- a/include/mmgr.h +++ b/include/mmgr.h @@ -8,10 +8,24 @@ extern const size_t page_size; /** - * @brief Pop the topmost address from the stack and returns that value. + * @brief * - * If the stack is empty, this function will instead return a status code. This - * can be identified by testing the least signifigant bits of the return value. + * @param size + * @return physaddr_t + */ +physaddr_t reserve_region(size_t size); + +/** + * @brief + * + * @param location + * @param size + * @return int + */ +int free_region(physaddr_t location, size_t size); + +/** + * @brief Reserves a single page and returns its physical address. * * @param stack * @return physaddr_t @@ -19,9 +33,7 @@ extern const size_t page_size; physaddr_t reserve_page(); /** - * @brief Pushes `location` onto the stack. - * - * If there is no room on the stack, the stack will be unaffected. + * @brief Marks the page at the given location as free. * * @param stack * @param location @@ -41,22 +53,25 @@ size_t free_page_count(); * * @return void* */ -void *page_stack_bottom(); +void *page_map_base(); /** * @brief Get the location of the top of the page stack. * * @return void* */ -void *page_stack_top(); +void *page_map_end(); /** - * @brief Push all available pages in `map` onto the stack + * @brief Constructs the bitmaps used by the physical memory allocator. * - * @param stack * @param map + * @param base + * @param memory_size + * @param block_size + * @return enum error_t */ -int initialize_page_stack(struct memory_map_t *map, physaddr_t *stack_base); +enum error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_size, unsigned long block_size); /** * @brief Create a new top-level page table and map the kernel in it. diff --git a/include/platform/context.h b/include/platform/context.h index dc6b27f..2152655 100644 --- a/include/platform/context.h +++ b/include/platform/context.h @@ -1,19 +1,35 @@ -#pragma once +#ifndef _QUARK_CONTEXT_H +#define _QUARK_CONTEXT_H -struct process_context_t; +#include + +#if defined __i386__ || defined __x86_64__ +#define DEFAULT_FLAGS 0x3200 +#endif /** - * @brief Allocates a new process context and initializes it with the given - * program counter. + * @brief Stores the context of a particular process. The contents of this + * struct are platform-specific. * - * @param task_entry - * @return void* */ -void *initialize_context(void *pc); +struct process_context_t +{ -void destroy_context(void *ctx); +#if defined __i386__ + uint32_t gp_registers[8]; + uint32_t ss; + uint32_t cs; + uint32_t eip; + uint32_t flags; +#elif defined __x86_64__ + uint64_t gp_registers[16]; + uint64_t ss; + uint64_t cs; + uint64_t rip; + uint64_t flags; +#endif -void save_context(struct process_context_t *context); +}; void load_context(struct process_context_t *context); @@ -24,3 +40,5 @@ void set_context_stack(struct process_context_t *context, void *stack); void set_context_flags(struct process_context_t *context, unsigned long flags); void set_context_return(struct process_context_t *context, unsigned long value); + +#endif diff --git a/include/x86/processstate.h b/include/x86/processstate.h deleted file mode 100644 index e8c45cf..0000000 --- a/include/x86/processstate.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include - -#define PCB_LOCATION 0x800 - -struct process_context_t -{ - uint32_t eax; - uint32_t ebx; - uint32_t ecx; - uint32_t edx; - uint32_t edi; - uint32_t esi; - uint32_t ebp; - uint32_t ss; - uint32_t esp; - uint32_t cs; - uint32_t eip; - uint32_t flags; -}; diff --git a/src/kernel.c b/src/kernel.c index 93189db..414128b 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -18,10 +18,11 @@ struct kernel_t kernel; void kernel_initialize(struct boot_info_t *boot_info) { insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE); - initialize_page_stack(&boot_info->map, (physaddr_t*)&_kernel_end); - kminit(&_kernel_start, page_stack_top(), 0xFFC00000 - (size_t)&_kernel_start, 64); + initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size); + kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64); initialize_screen(); printf("***%s***\n", PACKAGE_STRING); + printf("Total memory: %08x\n", boot_info->memory_size); printf("Type\t\tLocation\t\tSize\n"); for (size_t i = 0; i < boot_info->map.size && boot_info->map.array[i].size > 0; i++) { @@ -217,14 +218,22 @@ struct process_context_t *kernel_current_context() } } -enum error_t kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space) +unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t address_space) { struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t)); if(new_process == NULL) { return 0; } - struct process_context_t *initial_context = initialize_context(program_entry); + struct process_context_t *initial_context = kmalloc(sizeof(struct process_context_t)); + if(initial_context == NULL) + { + return 0; + } + memset(initial_context, 0, sizeof(struct process_context_t)); + set_context_pc(initial_context, program_entry); + set_context_flags(initial_context, DEFAULT_FLAGS); + set_context_stack(initial_context, NULL); new_process->priority = priority; new_process->pid = kernel.next_pid; new_process->page_table = address_space; @@ -268,16 +277,26 @@ enum error_t kernel_terminate_process(size_t process_id) } kernel.process_table = avl_remove(kernel.process_table, process_id); priorityqueue_remove(&kernel.priority_queue, process); - destroy_context(process->ctx); + for(struct message_t *msg = queue_get_next(&process->message_queue); msg != NULL; msg = queue_get_next(&process->message_queue)) + { + kfree(msg); + } + for(struct process_t *sender = queue_get_next(&process->sending_queue); sender != NULL; sender = queue_get_next(&process->sending_queue)) + { + sender->state = PROCESS_ACTIVE; + set_context_return(sender->ctx, EEXITED); + priorityqueue_insert(&kernel.priority_queue, sender, sender->priority); + } + kfree(process->ctx); kfree(process); return ENONE; } -enum error_t kernel_store_active_context(struct process_context_t *context, size_t size) +enum error_t kernel_store_active_context(struct process_context_t *context) { if(kernel.active_process != NULL && kernel.active_process->ctx != NULL) { - memcpy(kernel.active_process->ctx, context, size); + memcpy(kernel.active_process->ctx, context, sizeof(*context)); return ENONE; } else @@ -330,7 +349,7 @@ unsigned long kernel_get_port_owner(unsigned long id) } } -enum error_t kernel_send_message(int recipient, struct message_t *message) +enum error_t kernel_send_message(unsigned long recipient, struct message_t *message) { struct process_t *dest = avl_get(kernel.process_table, recipient); if(dest == NULL) @@ -358,7 +377,7 @@ enum error_t kernel_send_message(int recipient, struct message_t *message) } } -enum error_t kernel_queue_sender(int recipient) +enum error_t kernel_queue_sender(unsigned long recipient) { struct process_t *dest = avl_get(kernel.process_table, recipient); if(dest != NULL) @@ -375,7 +394,7 @@ enum error_t kernel_queue_sender(int recipient) } } -enum error_t kernel_queue_message(int recipient, struct message_t *message) +enum error_t kernel_queue_message(unsigned long recipient, struct message_t *message) { struct process_t *dest = avl_get(kernel.process_table, recipient); if(dest != NULL) diff --git a/src/mmgr.c b/src/mmgr.c index 6a6113c..0caede0 100644 --- a/src/mmgr.c +++ b/src/mmgr.c @@ -1,5 +1,6 @@ #include "mmgr.h" #include "string.h" +#include "math.h" #include "platform/paging.h" #include "types/status.h" #include @@ -12,11 +13,12 @@ */ struct page_stack_t { + /** * @brief The total number of physical pages managed by the system. * */ - size_t total_pages; + unsigned long total_pages; /** * @brief Points to the topmost physical address on the stack. @@ -39,81 +41,230 @@ struct page_stack_t } page_stack; -int initialize_page_stack(struct memory_map_t *map, physaddr_t *stack_base) +struct page_map_t { - page_stack.base_pointer = stack_base; - page_stack.limit_pointer = stack_base; - page_stack.stack_pointer = stack_base; - page_stack.total_pages = 0; - for(int i = 0; i < map->size; i++) + /** + * @brief The underlying bitmap representing the availability of chunks of + * physical memory. + * + */ + unsigned long *bitmap; + + /** + * @brief The size of the bitmap in bytes. + * + */ + unsigned long bitmap_size; + + /** + * @brief The size in bytes of the smallest unit of allocation. + * + * This value should either be the size of a page on the host system, or + * possibly some number of pages. + * + */ + unsigned long block_size; + + /** + * @brief + * + */ + unsigned long height; + + /** + * @brief The number of available blocks of memory. + * + * Due to memory fragmentation, it may not be possible to allocate all + * available memory at once. + * + */ + unsigned long free_block_count; +} page_map; + +const int bitmap_word_size = 8 * sizeof(*page_map.bitmap); + +int split_block(int index) +{ + if(index) { - if(map->array[i].type != M_AVAILABLE) + int bitmap_index = index / bitmap_word_size; + int bitmap_offset = index % bitmap_word_size; + page_map.bitmap[bitmap_index] &= ~(1 << bitmap_offset); + index *= 2; + bitmap_index = index / bitmap_word_size; + bitmap_offset = index / bitmap_word_size; + page_map.bitmap[bitmap_index] |= 1 << bitmap_offset; + page_map.bitmap[bitmap_index] |= 1 << (bitmap_offset ^ 1); + } + return index; +} + +int find_free_region(int height) +{ + if(height > page_map.height || height < 0) + { + return 0; + } + else if(height <= page_map.height - ilog2(bitmap_word_size)) + { + for(int index = (1 << (page_map.height - height)) / bitmap_word_size; + index < (1 << (page_map.height - height + 1)) / bitmap_word_size; + index++) { - continue; - } - size_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1); - while(location + page_size <= map->array[i].location + map->array[i].size) - { - if(free_page(location) != ENONE) + if(page_map.bitmap[index] != 0) { - return ENOMEM; + return bitmap_word_size * index + __builtin_ctz(page_map.bitmap[index]); } - page_stack.total_pages++; - location += page_size; } } + else + { + static const int bitmasks[] = {0x00000002, 0x0000000C, 0x000000F0, 0x0000FF00, 0xFFFF0000}; + int depth = page_map.height - height; + if(page_map.bitmap[0] & bitmasks[depth]) + { + return __builtin_ctz(page_map.bitmap[0] & bitmasks[depth]); + } + } + return split_block(find_free_region(height + 1)); +} + +physaddr_t reserve_region(size_t size) +{ + int height = llog2(size / page_map.block_size); + int index = find_free_region(height); + if(index) + { + int bitmap_index = index / bitmap_word_size; + int bitmap_offset = index % bitmap_word_size; + page_map.bitmap[bitmap_index] &= ~(1 << bitmap_offset); + return (page_map.block_size << height) * (index - (1 << (page_map.height - height))); + } + else + { + return ENOMEM; + } +} + +int free_region(physaddr_t location, size_t size) +{ + int height = llog2(size / page_map.block_size); + int index = (location / (page_map.block_size * (1 << height))) + (1 << (page_map.height - height)); + int bitmap_index = index / bitmap_word_size; + int bitmap_offset = index % bitmap_word_size; + page_map.bitmap[bitmap_index] |= 1 << bitmap_offset; + while(page_map.bitmap[bitmap_index] & (1 << (bitmap_offset ^ 1))) + { + page_map.bitmap[bitmap_index] &= ~(1 << bitmap_offset); + page_map.bitmap[bitmap_index] &= ~(1 << (bitmap_offset ^ 1)); + index /= 2; + bitmap_index = index / bitmap_word_size; + bitmap_offset = index % bitmap_word_size; + page_map.bitmap[bitmap_index] |= 1 << bitmap_offset; + } return ENONE; } physaddr_t reserve_page() { - if(page_stack.stack_pointer > page_stack.base_pointer) - { - page_stack.stack_pointer--; - physaddr_t frame = *page_stack.stack_pointer; - *page_stack.stack_pointer = (physaddr_t) 0; - return frame; - } - return ENOMEM; + return reserve_region(page_size); } int free_page(physaddr_t location) { - if(page_stack.stack_pointer < page_stack.limit_pointer) - { - *page_stack.stack_pointer = location; - page_stack.stack_pointer++; - return ENONE; - } - else - { - switch(map_page(page_stack.limit_pointer, location, PAGE_RW)) - { - case ENOMEM: - return ENOMEM; - case EOUTOFBOUNDS: - return EOUTOFBOUNDS; - case ENONE: - page_stack.limit_pointer += page_size / sizeof(*page_stack.limit_pointer); - return ENONE; - } - return ENOMEM; - } + return free_region(location, page_size); } size_t free_page_count() { - return page_stack.base_pointer - page_stack.stack_pointer; + return page_map.free_block_count; } -void *page_stack_bottom() +void *page_map_base() { - return (void*)page_stack.base_pointer; + return (void*)page_map.bitmap; } -void *page_stack_top() +void *page_map_end() { - return (void*)page_stack.limit_pointer; + return (void*)page_map.bitmap + page_map.bitmap_size; +} + +enum error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_size, unsigned long block_size) +{ + // Round memory_size up to nearest power of 2 + memory_size = 1 << llog2(memory_size); + page_map.bitmap = (unsigned long*) base; + page_map.bitmap_size = (memory_size / page_size) / 4; + page_map.block_size = block_size; + page_map.height = llog2(memory_size / block_size); + page_map.free_block_count = 0; + int block_log = llog2(block_size); + int pages_mapped = 0; + for(int i = 0; i < map->size; i++) + { + if(map->array[i].type != M_AVAILABLE) + { + continue; + } + physaddr_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1); + physaddr_t region_end = map->array[i].location + map->array[i].size; + while(location + block_size <= region_end) + { + if(pages_mapped < page_map.bitmap_size / page_size) + { + void *page = (void*)page_map.bitmap + pages_mapped * page_size; + for(int level = 0; level < page_table_levels; level++) + { + if(!(get_pte_type(page, level) & PAGE_PRESENT)) + { + if(set_pte(page, level, PAGE_PRESENT | PAGE_RW, location)) + { + return ENOMEM; + } + else if(level == page_table_levels - 1) + { + pages_mapped++; + } + break; + } + else if(level == page_table_levels - 1) + { + pages_mapped++; + } + } + location += page_size; + continue; + } + int bit_offset = (location / block_size) % bitmap_word_size; + int bitmap_index = (location / block_size) / bitmap_word_size; + size_t chunk_size = (bitmap_word_size - bit_offset) * block_size; + if(bit_offset == 0 && (region_end - location) >= chunk_size) + { + // Set all bits in the word + page_map.bitmap[bitmap_index] = ~0; + } + else if(bit_offset == 0) + { + // Set the first 'count' bits + int count = (region_end - location) >> block_log; + page_map.bitmap[bitmap_index] |= (1 << count) - 1; + } + else if((region_end - location) >= chunk_size) + { + // Set all bits starting at 'bit_offset' + page_map.bitmap[bitmap_index] |= ~((1 << bit_offset) - 1); + } + else + { + // Set all bits starting at 'bit_offset' up to 'count' + int count = (region_end - location) >> block_log; + page_map.bitmap[bitmap_index] |= ((1 << count) - 1) & ~((1 << bit_offset) - 1); + } + location += chunk_size; + } + } + return ENONE; } physaddr_t create_address_space() diff --git a/src/x86/context.c b/src/x86/context.c index 6760ccd..9e6b04c 100644 --- a/src/x86/context.c +++ b/src/x86/context.c @@ -4,54 +4,17 @@ #include "heap.h" #include "string.h" #include "system.h" -#include "x86/processstate.h" - -void *initialize_context(void *task_entry) -{ - physaddr_t stack_frame = reserve_page(); - if(stack_frame % page_size != 0) - { - return NULL; - } - map_page((void*)&_kernel_start - page_size, stack_frame, PAGE_RW | PAGE_USERMODE); - unmap_page((void*)&_kernel_start - (2 * page_size)); - struct process_context_t *context = kmalloc(sizeof(struct process_context_t)); - if(context != NULL) - { - memset(context, 0, sizeof(struct process_context_t)); - uint32_t flags; - asm("pushf; " - "mov (%%esp), %0; " - "popf; " - : "=r"(flags)); - context->cs = 0x1B; - context->eip = (uint32_t)task_entry; - context->flags = (flags & ~0xFD) | 0x200; - context->ss = 0x23; - context->esp = &_kernel_start; - context->ebp = &_kernel_start; - } - return (void*)context; -} - -void destroy_context(void *ctx) -{ - kfree(ctx); -} - -void save_context(struct process_context_t *context) -{ - kernel_store_active_context(context, sizeof(*context)); -} void set_context_pc(struct process_context_t *context, void *pc) { context->eip = pc; + context->cs = 0x1B; } void set_context_stack(struct process_context_t *context, void *stack) { - context->esp = stack; + context->gp_registers[7] = stack; + context->ss = 0x23; } void set_context_flags(struct process_context_t *context, unsigned long flags) @@ -61,5 +24,5 @@ void set_context_flags(struct process_context_t *context, unsigned long flags) void set_context_return(struct process_context_t *context, unsigned long value) { - context->eax = value; + context->gp_registers[0] = value; } \ No newline at end of file diff --git a/src/x86/load_context.S b/src/x86/load_context.S index 4550674..cdb5620 100644 --- a/src/x86/load_context.S +++ b/src/x86/load_context.S @@ -7,8 +7,8 @@ .type load_context, @function load_context: mov 4(%esp), %eax - push 0x1C(%eax) push 0x20(%eax) + push 0x1C(%eax) push 0x2C(%eax) push 0x24(%eax) push 0x28(%eax) @@ -19,10 +19,11 @@ load_context: mov 0x10(%eax), %edi mov 0x14(%eax), %esi mov 0x18(%eax), %ebp - mov 0x1C(%eax), %ax + mov 0x20(%eax), %ax mov %ax, %ds mov %ax, %es mov %ax, %fs mov %ax, %gs pop %eax - iret \ No newline at end of file + iret +.size load_context, . - load_context \ No newline at end of file diff --git a/src/x86/multiboot2.c b/src/x86/multiboot2.c index fe83633..ed13e55 100644 --- a/src/x86/multiboot2.c +++ b/src/x86/multiboot2.c @@ -68,6 +68,14 @@ struct multiboot2_memory_map_t struct multiboot2_map_entry_t entries; }; +struct multiboot2_memory_info_t +{ + uint32_t type; + uint32_t size; + uint32_t low_memory; + uint32_t high_memory; +}; + void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table) { uint32_t *int_table = (uint32_t *)table; @@ -75,6 +83,9 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table) { case MB_END_TAG: return NULL; + case MB_MEMORY_INFO: + boot_info->memory_size = ((struct multiboot2_memory_info_t*) table)->high_memory * 1024; + break; case MB_MEMORY_MAP: ; unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16; unsigned int entry_size = ((struct multiboot2_memory_map_t*) table)->entry_size; diff --git a/src/x86/preempt.S b/src/x86/preempt.S index e5d6598..2fc45da 100644 --- a/src/x86/preempt.S +++ b/src/x86/preempt.S @@ -58,14 +58,14 @@ isr_preempt: mov 4(%ebp), %eax push %eax - // Load ESP, then push it onto stack - mov 12(%ebp), %eax - push %eax - // Load SS, then push it onto stack mov 16(%ebp), %eax push %eax + // Load ESP, then push it onto stack + mov 12(%ebp), %eax + push %eax + // Load EBP, then push it onto stack mov -4(%ebp), %eax push %eax @@ -84,7 +84,7 @@ isr_preempt: // Push pointer to the process context saved on the stack push %esp - call save_context + call kernel_store_active_context mov %ebp, %esp call kernel_advance_scheduler From 8b1d7cb085a84b2f69c41b53c96880fe1175413b Mon Sep 17 00:00:00 2001 From: ngiddings Date: Tue, 23 Aug 2022 13:13:46 -0500 Subject: [PATCH 11/11] Kernel initializes a stack for new processes --- src/kernel.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/kernel.c b/src/kernel.c index 414128b..dad6537 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -230,10 +230,16 @@ unsigned long kernel_spawn_process(void *program_entry, int priority, physaddr_t { return 0; } + physaddr_t stack_page = reserve_page(); + if(stack_page % page_size) + { + return 0; + } + map_page((void*)&_kernel_start - page_size, stack_page, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW); memset(initial_context, 0, sizeof(struct process_context_t)); set_context_pc(initial_context, program_entry); set_context_flags(initial_context, DEFAULT_FLAGS); - set_context_stack(initial_context, NULL); + set_context_stack(initial_context, &_kernel_start); new_process->priority = priority; new_process->pid = kernel.next_pid; new_process->page_table = address_space;