Various renaming and refactoring

This commit is contained in:
2022-08-19 22:34:45 -05:00
parent 0337fca749
commit c36f65ce97
11 changed files with 227 additions and 156 deletions

View File

@@ -7,6 +7,7 @@
#include "mmgr.h" #include "mmgr.h"
#include "syscalls.h" #include "syscalls.h"
#include "types/syscallid.h" #include "types/syscallid.h"
#include "types/status.h"
#include <stddef.h> #include <stddef.h>
#define MAX_SYSCALL_ID 256 #define MAX_SYSCALL_ID 256
@@ -83,29 +84,35 @@ struct kernel_t
void kernel_initialize(struct boot_info_t *boot_info); 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); 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); int receive_message(struct message_t *buffer, int flags);

View File

@@ -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 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);

View File

@@ -1,14 +1,15 @@
#pragma once #pragma once
enum status_t enum error_t
{ {
S_OK = 0, ENONE = 0,
S_NULL_POINTER, ENULLPTR,
S_OUT_OF_BOUNDS, EOUTOFBOUNDS,
S_INVALID_ARGUMENT, EINVALIDARG,
S_OUT_OF_MEMORY, ENOMEM,
S_DOESNT_EXIST, EDOESNTEXIST,
S_EXISTS, EEXISTS,
S_BAD_SYSCALL, ENOSYSCALL,
S_BUSY EPERM,
EBUSY
}; };

View File

@@ -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) for(size_t n = 0; n < program_header->memsize; n += page_size)
{ {
physaddr_t page = reserve_page(); 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); int status = map_page(d + n, page, PAGE_RW | PAGE_USERMODE | PAGE_EXECUTABLE);
switch(status) switch(status)
{ {
case S_OUT_OF_MEMORY: case ENOMEM:
return status; return status;
case S_OUT_OF_BOUNDS: case EOUTOFBOUNDS:
return status; return status;
case S_OK: case ENONE:
memcpy(d + n, s + n, n + page_size < program_header->memsize ? page_size : program_header->memsize - n); 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--; count--;
program_header = (struct elf_program_header_t*)((void*)program_header + elf->phsize); program_header = (struct elf_program_header_t*)((void*)program_header + elf->phsize);
} }
return S_OK; return ENONE;
} }

View File

@@ -103,7 +103,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size
if((flags & PAGE_PRESENT) == 0) if((flags & PAGE_PRESENT) == 0)
{ {
int status = map_page((void*)heap->header + i, reserve_page(), PAGE_RW); int status = map_page((void*)heap->header + i, reserve_page(), PAGE_RW);
if(status != S_OK) if(status != ENONE)
{ {
return status; 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; heap->header[i + (1 << heap->tree_height)].state = UNAVAIL;
} }
} }
return S_OK; return ENONE;
} }
void *heap_allocate(struct heap_t *heap, size_t size) void *heap_allocate(struct heap_t *heap, size_t size)

View File

@@ -32,7 +32,7 @@ void kernel_initialize(struct boot_info_t *boot_info)
kernel.next_pid = 1; kernel.next_pid = 1;
kernel.process_table = NULL; kernel.process_table = NULL;
kernel.port_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."); 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_MUNMAP, 2, 0, munmap);
set_syscall(SYSCALL_SEND, 3, 0, send); set_syscall(SYSCALL_SEND, 3, 0, send);
set_syscall(SYSCALL_RECEIVE, 2, 0, receive); set_syscall(SYSCALL_RECEIVE, 2, 0, receive);
set_syscall(SYSCALL_OPEN_PORT, 1, 0, openport); set_syscall(SYSCALL_OPEN_PORT, 1, 0, open_port);
set_syscall(SYSCALL_CLOSE_PORT, 1, 0, closeport); set_syscall(SYSCALL_CLOSE_PORT, 1, 0, close_port);
for(int i = 0; i < boot_info->module_count; i++) 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."); panic("Failed to load modules.");
} }
} }
if(initialize_interrupts() != S_OK) if(initialize_interrupts() != ENONE)
{ {
panic("Failed to initialize interrupts."); panic("Failed to initialize interrupts.");
} }
irq_enable(); 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) if(id < 0 || id > MAX_SYSCALL_ID)
{ {
return S_OUT_OF_BOUNDS; return EOUTOFBOUNDS;
} }
else if(kernel.syscall_table[id].defined) else if(kernel.syscall_table[id].defined)
{ {
return S_INVALID_ARGUMENT; return EINVALIDARG;
} }
else if(arg_count < 0 || arg_count > 3) 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) else if(pid != 0 && avl_get(kernel.process_table, pid) == NULL)
{ {
return S_DOESNT_EXIST; return EDOESNTEXIST;
} }
else if(func_ptr == NULL) else if(func_ptr == NULL)
{ {
return S_NULL_POINTER; return ENULLPTR;
} }
kernel.syscall_table[id].defined = true; kernel.syscall_table[id].defined = true;
kernel.syscall_table[id].arg_count = arg_count; kernel.syscall_table[id].arg_count = arg_count;
kernel.syscall_table[id].process_id = pid; kernel.syscall_table[id].process_id = pid;
kernel.syscall_table[id].func_ptr_0 = func_ptr; 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) 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) if(id < 0 || id > MAX_SYSCALL_ID)
{ {
return S_BAD_SYSCALL; return ENOSYSCALL;
} }
else if(!kernel.syscall_table[id].defined) else if(!kernel.syscall_table[id].defined)
{ {
return S_BAD_SYSCALL; return ENOSYSCALL;
} }
bool switched_address_space = false; bool switched_address_space = false;
if(kernel.syscall_table[id].process_id > 0) 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) if(callee == NULL)
{ {
kernel.syscall_table[id].defined = false; kernel.syscall_table[id].defined = false;
return S_BAD_SYSCALL; return ENOSYSCALL;
} }
paging_load_address_space(callee->page_table); paging_load_address_space(callee->page_table);
switched_address_space = true; 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; 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(); 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"); panic("failed to create address space for module: out of memory");
} }
paging_load_address_space(module_address_space); 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); int status = map_page(load_base + load_offset, p, PAGE_RW);
switch(status) switch(status)
{ {
case S_OUT_OF_MEMORY: case ENOMEM:
panic("ran out of memory while mapping module"); panic("ran out of memory while mapping module");
case S_OUT_OF_BOUNDS: case EOUTOFBOUNDS:
panic("got out-of-bounds error while mapping module"); panic("got out-of-bounds error while mapping module");
} }
load_offset += page_size; load_offset += page_size;
@@ -163,9 +163,9 @@ int load_module(struct module_t *module)
int status = load_program(load_base); int status = load_program(load_base);
switch(status) switch(status)
{ {
case S_OUT_OF_MEMORY: case ENOMEM:
panic("ran out of memory while reading ELF file"); 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"); panic("got out-of-bounds error while reading ELF file");
} }
void *module_entry = ((struct elf_file_header_t*)load_base)->entry; 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); int status = unmap_page(load_base + load_offset);
switch(status) switch(status)
{ {
case S_OUT_OF_MEMORY: case ENOMEM:
panic("ran out of memory while unmapping module"); panic("ran out of memory while unmapping module");
case S_OUT_OF_BOUNDS: case EOUTOFBOUNDS:
panic("got out-of-bounds error while unmapping module"); panic("got out-of-bounds error while unmapping module");
} }
load_offset += page_size; 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 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) 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)); struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t));
if(new_process == NULL) if(new_process == NULL)
@@ -227,7 +239,7 @@ int add_process(void *program_entry, int priority, physaddr_t address_space)
return new_process->pid; return new_process->pid;
} }
struct process_context_t *next_process() struct process_context_t *kernel_advance_scheduler()
{ {
if(kernel.active_process != NULL) if(kernel.active_process != NULL)
{ {
@@ -243,12 +255,12 @@ struct process_context_t *next_process()
panic("no processes available to enter!"); 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); struct process_t *process = avl_get(kernel.process_table, process_id);
if(process == NULL) if(process == NULL)
{ {
return S_DOESNT_EXIST; return EDOESNTEXIST;
} }
if(kernel.active_process == process) if(kernel.active_process == process)
{ {
@@ -258,79 +270,72 @@ int terminate_process(size_t process_id)
priorityqueue_remove(&kernel.priority_queue, process); priorityqueue_remove(&kernel.priority_queue, process);
destroy_context(process->ctx); destroy_context(process->ctx);
kfree(process); 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) if(kernel.active_process != NULL && kernel.active_process->ctx != NULL)
{ {
memcpy(kernel.active_process->ctx, context, size); memcpy(kernel.active_process->ctx, context, size);
return S_OK; return ENONE;
} }
else 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) if(avl_get(kernel.port_table, id) != NULL)
{ {
return S_EXISTS; return EEXISTS;
} }
printf("opening port %i -> %i\n", id, kernel.active_process->pid); printf("opening port %i -> %i\n", id, kernel.active_process->pid);
struct port_t *port = kmalloc(sizeof(struct port_t)); struct port_t *port = kmalloc(sizeof(struct port_t));
port->id = id; port->id = id;
port->owner_pid = kernel.active_process->pid; port->owner_pid = kernel.active_process->pid;
kernel.port_table = avl_insert(kernel.port_table, id, port); 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); struct port_t *port = avl_get(kernel.port_table, id);
if(port == NULL) if(port == NULL)
{ {
return S_DOESNT_EXIST; return EDOESNTEXIST;
} }
else if(port->owner_pid != kernel.active_process->pid) 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); printf("closing port %i attached to %i\n", id, kernel.active_process->pid);
kernel.port_table = avl_remove(kernel.port_table, id); kernel.port_table = avl_remove(kernel.port_table, id);
kfree(port); 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; struct port_t *port = avl_get(kernel.port_table, id);
int dest_type = flags & IO_RECIPIENT_TYPE; if(port == NULL)
if((flags & ~(IO_OP | IO_RECIPIENT_TYPE)) != 0 || dest_type >= IO_MAILBOX)
{ {
printf("Invalid flags on send_message\n"); return 0;
return S_INVALID_ARGUMENT;
} }
if(dest_type == IO_PORT) else
{ {
struct port_t *port = avl_get(kernel.port_table, recipient); return port->owner_pid;
if(port != NULL)
{
recipient = port->owner_pid;
}
else
{
printf("Port %i does not exist\n", recipient);
return S_DOESNT_EXIST;
}
} }
}
enum error_t kernel_send_message(int recipient, struct message_t *message)
{
struct process_t *dest = avl_get(kernel.process_table, recipient); struct process_t *dest = avl_get(kernel.process_table, recipient);
if(dest == NULL) if(dest == NULL)
{ {
return S_DOESNT_EXIST; return EDOESNTEXIST;
} }
else if(dest->message_buffer != NULL) 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); paging_load_address_space(kernel.active_process->page_table);
dest->message_buffer = NULL; dest->message_buffer = NULL;
dest->state = PROCESS_ACTIVE; 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); priorityqueue_insert(&kernel.priority_queue, dest, dest->priority);
return S_OK; return ENONE;
}
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;
} }
else 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); printf("Queueing process %i to %i\n", kernel.active_process->pid, dest->pid);
queue_insert(&dest->sending_queue, kernel.active_process); queue_insert(&dest->sending_queue, kernel.active_process);
kernel.active_process->state = PROCESS_SENDING; kernel.active_process->state = PROCESS_SENDING;
kernel.active_process = NULL; 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); paging_load_address_space(kernel.active_process->page_table);
memcpy(buffer, &kernel_buffer, sizeof(struct message_t)); memcpy(buffer, &kernel_buffer, sizeof(struct message_t));
sender->state = PROCESS_ACTIVE; 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); priorityqueue_insert(&kernel.priority_queue, sender, sender->priority);
return S_OK; return ENONE;
} }
else if(kernel.active_process->message_queue.count > 0) else if(kernel.active_process->message_queue.count > 0)
{ {
struct message_t *queued_msg = queue_get_next(&kernel.active_process->message_queue); struct message_t *queued_msg = queue_get_next(&kernel.active_process->message_queue);
memcpy(buffer, queued_msg, sizeof(struct message_t)); memcpy(buffer, queued_msg, sizeof(struct message_t));
kfree(queued_msg); kfree(queued_msg);
return S_OK; return ENONE;
} }
else if((flags & IO_OP) == IO_ASYNC) else if((flags & IO_OP) == IO_ASYNC)
{ {
return S_DOESNT_EXIST; return EDOESNTEXIST;
} }
else else
{ {
kernel.active_process->message_buffer = buffer; kernel.active_process->message_buffer = buffer;
kernel.active_process->state = PROCESS_REQUESTING; kernel.active_process->state = PROCESS_REQUESTING;
kernel.active_process = NULL; kernel.active_process = NULL;
load_context(next_process()); load_context(kernel_advance_scheduler());
} }
} }

View File

@@ -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); 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) 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++; page_stack.total_pages++;
location += page_size; location += page_size;
} }
} }
return S_OK; return ENONE;
} }
physaddr_t reserve_page() physaddr_t reserve_page()
@@ -74,7 +74,7 @@ physaddr_t reserve_page()
*page_stack.stack_pointer = (physaddr_t) 0; *page_stack.stack_pointer = (physaddr_t) 0;
return frame; return frame;
} }
return S_OUT_OF_MEMORY; return ENOMEM;
} }
int free_page(physaddr_t location) 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 = location;
page_stack.stack_pointer++; page_stack.stack_pointer++;
return S_OK; return ENONE;
} }
else else
{ {
switch(map_page(page_stack.limit_pointer, location, PAGE_RW)) switch(map_page(page_stack.limit_pointer, location, PAGE_RW))
{ {
case S_OUT_OF_MEMORY: case ENOMEM:
return S_OUT_OF_MEMORY; return ENOMEM;
case S_OUT_OF_BOUNDS: case EOUTOFBOUNDS:
return S_OUT_OF_BOUNDS; return EOUTOFBOUNDS;
case S_OK: case ENONE:
page_stack.limit_pointer += page_size / sizeof(*page_stack.limit_pointer); 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(); physaddr_t table = reserve_page();
int result; 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))) 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) if (frame % page_size != 0)
{ {
return S_INVALID_ARGUMENT; return EINVALIDARG;
} }
for(int level = 0; level < page_table_levels - 1; level++) 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) if(present == 0)
{ {
physaddr_t new_table = reserve_page(); 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); set_pte(page, level, PAGE_PRESENT | PAGE_USERMODE | PAGE_RW, new_table);
wipe_page_table(page, level + 1); wipe_page_table(page, level + 1);
} }
} }
set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame); set_pte(page, page_table_levels - 1, PAGE_PRESENT | flags, frame);
return S_OK; return ENONE;
} }
physaddr_t unmap_page(void *page) 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++) for(int level = 0; level < page_table_levels; level++)
{ {
if((get_pte_type(page, level) & PAGE_PRESENT) == 0) 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); physaddr_t frame = get_pte_address(page, page_table_levels - 1);
set_pte(page, page_table_levels - 1, 0, 0); set_pte(page, page_table_levels - 1, 0, 0);

View File

@@ -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); queue->heap = kmalloc(sizeof(struct priority_queue_node_t) * capacity);
if(queue->heap == NULL) if(queue->heap == NULL)
{ {
return S_OUT_OF_MEMORY; return ENOMEM;
} }
queue->capacity = capacity; queue->capacity = capacity;
queue->size = 0; queue->size = 0;
return S_OK; return ENONE;
} }
void *priorityqueue_extract_min(struct priority_queue_t *queue) 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) int priorityqueue_insert(struct priority_queue_t *queue, void *value, int priority)
{ {
if(queue->size == queue->capacity) if(queue->size == queue->capacity)
return S_OUT_OF_MEMORY; return ENOMEM;
size_t i = queue->size; size_t i = queue->size;
queue->size++; queue->size++;
while(i > 0 && queue->heap[(i - 1) / 2].priority > priority) 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].priority = priority;
queue->heap[i].value = value; queue->heap[i].value = value;
return S_OK; return ENONE;
} }
int priorityqueue_remove(struct priority_queue_t *queue, void *value) 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->size--;
queue->heap[i] = queue->heap[queue->size]; queue->heap[i] = queue->heap[queue->size];
heapify(queue, i); heapify(queue, i);
return S_OK; return ENONE;
} }
} }
return S_OUT_OF_BOUNDS; return EOUTOFBOUNDS;
} }

View File

@@ -2,6 +2,7 @@
#include "kernel.h" #include "kernel.h"
#include "mmgr.h" #include "mmgr.h"
#include "stdio.h" #include "stdio.h"
#include "platform/context.h"
#include "types/status.h" #include "types/status.h"
size_t test_syscall(syscall_arg_t str) 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; unsigned long flags = arg_flags.unsigned_int;
if(location % page_size != 0 || length % page_size != 0) if(location % page_size != 0 || length % page_size != 0)
{ {
return S_INVALID_ARGUMENT; return EINVALIDARG;
} }
else if(location == NULL) else if(location == NULL)
{ {
return S_NULL_POINTER; return ENULLPTR;
} }
for(size_t i = 0; i < length; i += page_size) for(size_t i = 0; i < length; i += page_size)
{ {
if(page_type((void*)(location + i)) & PAGE_PRESENT) if(page_type((void*)(location + i)) & PAGE_PRESENT)
{ {
return S_EXISTS; return EEXISTS;
} }
} }
size_t n = 0; size_t n = 0;
int status = S_OK; int status = ENONE;
while(n < length && status == S_OK) while(n < length && status == ENONE)
{ {
physaddr_t frame = reserve_page(); physaddr_t frame = reserve_page();
status = frame % page_size; status = frame % page_size;
if(status == S_OK) if(status == ENONE)
{ {
status = map_page((void*)(location + n), frame, PAGE_USERMODE | PAGE_RW); 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."); 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; 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."); 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; unsigned long length = arg_length.unsigned_int;
if(location % page_size != 0 || length % page_size != 0) if(location % page_size != 0 || length % page_size != 0)
{ {
return S_INVALID_ARGUMENT; return EINVALIDARG;
} }
else if(location == 0) else if(location == 0)
{ {
return S_NULL_POINTER; return ENULLPTR;
} }
size_t n = 0; size_t n = 0;
int status = S_OK; int status = ENONE;
while(n < length && status == S_OK) while(n < length && status == ENONE)
{ {
int type = page_type((void*)(location + n)); int type = page_type((void*)(location + n));
physaddr_t frame; physaddr_t frame;
@@ -89,12 +90,47 @@ size_t munmap(syscall_arg_t arg_location, syscall_arg_t arg_length)
size_t terminate_self() 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) 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) 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); 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);
} }

View File

@@ -41,7 +41,7 @@ void destroy_context(void *ctx)
void save_context(struct process_context_t *context) 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) void set_context_pc(struct process_context_t *context, void *pc)

View File

@@ -87,7 +87,7 @@ isr_preempt:
call save_context call save_context
mov %ebp, %esp mov %ebp, %esp
call next_process call kernel_advance_scheduler
push %eax push %eax
call load_context call load_context