@@ -6,6 +6,8 @@
|
|||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
#include "mmgr.h"
|
#include "mmgr.h"
|
||||||
#include "syscalls.h"
|
#include "syscalls.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
|
||||||
@@ -33,8 +35,9 @@ struct boot_info_t
|
|||||||
{
|
{
|
||||||
char *bootloader;
|
char *bootloader;
|
||||||
char *parameters;
|
char *parameters;
|
||||||
size_t module_count;
|
size_t memory_size;
|
||||||
struct memory_map_t map;
|
struct memory_map_t map;
|
||||||
|
size_t module_count;
|
||||||
struct module_t modules[module_limit];
|
struct module_t modules[module_limit];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -82,29 +85,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);
|
||||||
|
|
||||||
int terminate_process(size_t process_id);
|
unsigned long 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(unsigned long recipient, struct message_t *message);
|
||||||
|
|
||||||
|
enum error_t kernel_queue_sender(unsigned long recipient);
|
||||||
|
|
||||||
|
enum error_t kernel_queue_message(unsigned long recipient, struct message_t *message);
|
||||||
|
|
||||||
int receive_message(struct message_t *buffer, int flags);
|
int receive_message(struct message_t *buffer, int flags);
|
||||||
|
|
||||||
|
|||||||
8
include/math.h
Normal file
8
include/math.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _QUARK_UTIL_H
|
||||||
|
#define _QUARK_UTIL_H
|
||||||
|
|
||||||
|
int ilog2(unsigned int x);
|
||||||
|
|
||||||
|
int llog2(unsigned long x);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,10 +8,24 @@
|
|||||||
extern const size_t page_size;
|
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
|
* @param size
|
||||||
* can be identified by testing the least signifigant bits of the return value.
|
* @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
|
* @param stack
|
||||||
* @return physaddr_t
|
* @return physaddr_t
|
||||||
@@ -19,9 +33,7 @@ extern const size_t page_size;
|
|||||||
physaddr_t reserve_page();
|
physaddr_t reserve_page();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pushes `location` onto the stack.
|
* @brief Marks the page at the given location as free.
|
||||||
*
|
|
||||||
* If there is no room on the stack, the stack will be unaffected.
|
|
||||||
*
|
*
|
||||||
* @param stack
|
* @param stack
|
||||||
* @param location
|
* @param location
|
||||||
@@ -41,22 +53,25 @@ size_t free_page_count();
|
|||||||
*
|
*
|
||||||
* @return void*
|
* @return void*
|
||||||
*/
|
*/
|
||||||
void *page_stack_bottom();
|
void *page_map_base();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the location of the top of the page stack.
|
* @brief Get the location of the top of the page stack.
|
||||||
*
|
*
|
||||||
* @return void*
|
* @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 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.
|
* @brief Create a new top-level page table and map the kernel in it.
|
||||||
|
|||||||
@@ -1,19 +1,35 @@
|
|||||||
#pragma once
|
#ifndef _QUARK_CONTEXT_H
|
||||||
|
#define _QUARK_CONTEXT_H
|
||||||
|
|
||||||
struct process_context_t;
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#if defined __i386__ || defined __x86_64__
|
||||||
|
#define DEFAULT_FLAGS 0x3200
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Allocates a new process context and initializes it with the given
|
* @brief Stores the context of a particular process. The contents of this
|
||||||
* program counter.
|
* 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);
|
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_flags(struct process_context_t *context, unsigned long flags);
|
||||||
|
|
||||||
void set_context_return(struct process_context_t *context, unsigned long value);
|
void set_context_return(struct process_context_t *context, unsigned long value);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "types/status.h"
|
||||||
|
|
||||||
struct queue_node_t;
|
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.
|
* @brief Inserts a new item at the end of the queue.
|
||||||
*
|
*
|
||||||
* @param 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.
|
* @brief Removes the next item from the queue and returns it.
|
||||||
*
|
*
|
||||||
* @param queue
|
* @param queue
|
||||||
* @return void*
|
* @return void* The pointer stored at the front of the queue.
|
||||||
*/
|
*/
|
||||||
void *queue_get_next(struct queue_t *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.
|
* @brief Returns the next item on the queue without removing it.
|
||||||
*
|
*
|
||||||
* @param queue
|
* @param queue
|
||||||
* @return void*
|
* @return void* The pointer stored at the front of the queue.
|
||||||
*/
|
*/
|
||||||
void *queue_peek(struct queue_t *queue);
|
void *queue_peek(struct queue_t *queue);
|
||||||
|
|
||||||
|
|||||||
@@ -3,19 +3,6 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
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
|
typedef union
|
||||||
{
|
{
|
||||||
long signed_int;
|
long signed_int;
|
||||||
@@ -57,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);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#ifndef _QUARK_PHYSADDR_H
|
||||||
|
#define _QUARK_PHYSADDR_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -9,3 +10,5 @@ typedef uint64_t physaddr_t;
|
|||||||
#else
|
#else
|
||||||
typedef uint64_t physaddr_t;
|
typedef uint64_t physaddr_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,14 +1,20 @@
|
|||||||
#pragma once
|
#ifndef _QUARK_ERROR_H
|
||||||
|
#define _QUARK_ERROR_H
|
||||||
|
|
||||||
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,
|
ENOTPRESENT,
|
||||||
S_EXISTS,
|
EDOESNTEXIST,
|
||||||
S_BAD_SYSCALL,
|
EEXISTS,
|
||||||
S_BUSY
|
ENOSYSCALL,
|
||||||
|
EPERM,
|
||||||
|
EBUSY,
|
||||||
|
EEXITED
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
17
include/types/syscallid.h
Normal file
17
include/types/syscallid.h
Normal file
@@ -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
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#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;
|
|
||||||
};
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
noinst_PROGRAMS = quark-kernel
|
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_LDADD = -lgcc
|
||||||
quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
|
quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
|
||||||
quark_kernel_LDFLAGS = -nostdlib
|
quark_kernel_LDFLAGS = -nostdlib
|
||||||
|
|||||||
12
src/elf.c
12
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)
|
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;
|
||||||
}
|
}
|
||||||
26
src/heap.c
26
src/heap.c
@@ -1,6 +1,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "heap.h"
|
#include "heap.h"
|
||||||
#include "mmgr.h"
|
#include "mmgr.h"
|
||||||
|
#include "math.h"
|
||||||
#include "types/status.h"
|
#include "types/status.h"
|
||||||
|
|
||||||
#define AVAIL 0
|
#define AVAIL 0
|
||||||
@@ -23,23 +24,6 @@ struct heap_node_t
|
|||||||
size_t state : 2;
|
size_t state : 2;
|
||||||
} __attribute__ ((packed));
|
} __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)
|
size_t find_free_block(struct heap_t *heap, size_t height)
|
||||||
{
|
{
|
||||||
if(height > heap->tree_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->header = (struct heap_node_t*) start;
|
||||||
heap->heap_size = heap_size;
|
heap->heap_size = heap_size;
|
||||||
heap->block_size = block_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;
|
size_t header_size = (heap_size / block_size) << 1;
|
||||||
for(size_t i = 1; i <= (heap_size / block_size) * 2; i++)
|
for(size_t i = 1; i <= (heap_size / block_size) * 2; i++)
|
||||||
{
|
{
|
||||||
@@ -103,7 +87,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,14 +113,14 @@ 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)
|
||||||
{
|
{
|
||||||
size += heap->block_size - 1;
|
size += heap->block_size - 1;
|
||||||
size -= size % heap->block_size;
|
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);
|
size_t index = find_free_block(heap, height);
|
||||||
if(index)
|
if(index)
|
||||||
{
|
{
|
||||||
|
|||||||
219
src/kernel.c
219
src/kernel.c
@@ -11,16 +11,18 @@
|
|||||||
#include "platform/context.h"
|
#include "platform/context.h"
|
||||||
#include "platform/putc.h"
|
#include "platform/putc.h"
|
||||||
#include "types/status.h"
|
#include "types/status.h"
|
||||||
|
#include "types/syscallid.h"
|
||||||
|
|
||||||
struct kernel_t kernel;
|
struct kernel_t kernel;
|
||||||
|
|
||||||
void kernel_initialize(struct boot_info_t *boot_info)
|
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);
|
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);
|
initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size);
|
||||||
kminit(&_kernel_start, page_stack_top(), 0xFFC00000 - (size_t)&_kernel_start, 64);
|
kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64);
|
||||||
initialize_screen();
|
initialize_screen();
|
||||||
printf("***%s***\n", PACKAGE_STRING);
|
printf("***%s***\n", PACKAGE_STRING);
|
||||||
|
printf("Total memory: %08x\n", boot_info->memory_size);
|
||||||
printf("Type\t\tLocation\t\tSize\n");
|
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++)
|
for (size_t i = 0; i < boot_info->map.size && boot_info->map.array[i].size > 0; i++)
|
||||||
{
|
{
|
||||||
@@ -31,7 +33,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.");
|
||||||
}
|
}
|
||||||
@@ -41,63 +43,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)
|
||||||
@@ -106,7 +108,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;
|
||||||
@@ -137,10 +139,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);
|
||||||
@@ -151,9 +153,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;
|
||||||
@@ -162,9 +164,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;
|
||||||
@@ -175,16 +177,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
|
||||||
{
|
{
|
||||||
@@ -192,7 +194,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)
|
||||||
{
|
{
|
||||||
@@ -204,14 +206,40 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
struct process_t *new_process = (struct process_t*) kmalloc(sizeof(struct process_t));
|
||||||
if(new_process == NULL)
|
if(new_process == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
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, &_kernel_start);
|
||||||
new_process->priority = priority;
|
new_process->priority = priority;
|
||||||
new_process->pid = kernel.next_pid;
|
new_process->pid = kernel.next_pid;
|
||||||
new_process->page_table = address_space;
|
new_process->page_table = address_space;
|
||||||
@@ -226,7 +254,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)
|
||||||
{
|
{
|
||||||
@@ -242,12 +270,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)
|
||||||
{
|
{
|
||||||
@@ -255,81 +283,84 @@ int terminate_process(size_t process_id)
|
|||||||
}
|
}
|
||||||
kernel.process_table = avl_remove(kernel.process_table, process_id);
|
kernel.process_table = avl_remove(kernel.process_table, process_id);
|
||||||
priorityqueue_remove(&kernel.priority_queue, process);
|
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);
|
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)
|
||||||
{
|
{
|
||||||
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, sizeof(*context));
|
||||||
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)
|
|
||||||
{
|
|
||||||
struct port_t *port = avl_get(kernel.port_table, recipient);
|
|
||||||
if(port != NULL)
|
|
||||||
{
|
|
||||||
recipient = port->owner_pid;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Port %i does not exist\n", recipient);
|
return port->owner_pid;
|
||||||
return S_DOESNT_EXIST;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum error_t kernel_send_message(unsigned long 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)
|
||||||
{
|
{
|
||||||
@@ -342,29 +373,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(unsigned long 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(unsigned long 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,27 +433,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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
66
src/math.c
Normal file
66
src/math.c
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
int ilog2(unsigned int x)
|
||||||
|
{
|
||||||
|
#if defined __GNUC__
|
||||||
|
if(x <= 1)
|
||||||
|
return 0;
|
||||||
|
return 32 - __builtin_clz(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
|
||||||
|
}
|
||||||
273
src/mmgr.c
273
src/mmgr.c
@@ -1,5 +1,6 @@
|
|||||||
#include "mmgr.h"
|
#include "mmgr.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "math.h"
|
||||||
#include "platform/paging.h"
|
#include "platform/paging.h"
|
||||||
#include "types/status.h"
|
#include "types/status.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -12,11 +13,12 @@
|
|||||||
*/
|
*/
|
||||||
struct page_stack_t
|
struct page_stack_t
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The total number of physical pages managed by the system.
|
* @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.
|
* @brief Points to the topmost physical address on the stack.
|
||||||
@@ -39,90 +41,239 @@ struct page_stack_t
|
|||||||
|
|
||||||
} page_stack;
|
} 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;
|
* @brief The underlying bitmap representing the availability of chunks of
|
||||||
page_stack.stack_pointer = stack_base;
|
* physical memory.
|
||||||
page_stack.total_pages = 0;
|
*
|
||||||
|
*/
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
if(page_map.bitmap[index] != 0)
|
||||||
|
{
|
||||||
|
return bitmap_word_size * index + __builtin_ctz(page_map.bitmap[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
return reserve_region(page_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int free_page(physaddr_t location)
|
||||||
|
{
|
||||||
|
return free_region(location, page_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t free_page_count()
|
||||||
|
{
|
||||||
|
return page_map.free_block_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *page_map_base()
|
||||||
|
{
|
||||||
|
return (void*)page_map.bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *page_map_end()
|
||||||
|
{
|
||||||
|
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++)
|
for(int i = 0; i < map->size; i++)
|
||||||
{
|
{
|
||||||
if(map->array[i].type != M_AVAILABLE)
|
if(map->array[i].type != M_AVAILABLE)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
size_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1);
|
physaddr_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1);
|
||||||
while(location + page_size <= map->array[i].location + map->array[i].size)
|
physaddr_t region_end = map->array[i].location + map->array[i].size;
|
||||||
|
while(location + block_size <= region_end)
|
||||||
{
|
{
|
||||||
if(free_page(location) != S_OK)
|
if(pages_mapped < page_map.bitmap_size / page_size)
|
||||||
{
|
{
|
||||||
return S_OUT_OF_MEMORY;
|
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++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
page_stack.total_pages++;
|
|
||||||
location += page_size;
|
location += page_size;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
int bit_offset = (location / block_size) % bitmap_word_size;
|
||||||
return S_OK;
|
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)
|
||||||
physaddr_t reserve_page()
|
|
||||||
{
|
{
|
||||||
if(page_stack.stack_pointer > page_stack.base_pointer)
|
// Set all bits in the word
|
||||||
{
|
page_map.bitmap[bitmap_index] = ~0;
|
||||||
page_stack.stack_pointer--;
|
|
||||||
physaddr_t frame = *page_stack.stack_pointer;
|
|
||||||
*page_stack.stack_pointer = (physaddr_t) 0;
|
|
||||||
return frame;
|
|
||||||
}
|
}
|
||||||
return S_OUT_OF_MEMORY;
|
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)
|
||||||
int free_page(physaddr_t location)
|
|
||||||
{
|
{
|
||||||
if(page_stack.stack_pointer < page_stack.limit_pointer)
|
// Set all bits starting at 'bit_offset'
|
||||||
{
|
page_map.bitmap[bitmap_index] |= ~((1 << bit_offset) - 1);
|
||||||
*page_stack.stack_pointer = location;
|
|
||||||
page_stack.stack_pointer++;
|
|
||||||
return S_OK;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch(map_page(page_stack.limit_pointer, location, PAGE_RW))
|
// Set all bits starting at 'bit_offset' up to 'count'
|
||||||
{
|
int count = (region_end - location) >> block_log;
|
||||||
case S_OUT_OF_MEMORY:
|
page_map.bitmap[bitmap_index] |= ((1 << count) - 1) & ~((1 << bit_offset) - 1);
|
||||||
return S_OUT_OF_MEMORY;
|
|
||||||
case S_OUT_OF_BOUNDS:
|
|
||||||
return S_OUT_OF_BOUNDS;
|
|
||||||
case S_OK:
|
|
||||||
page_stack.limit_pointer += page_size / sizeof(*page_stack.limit_pointer);
|
|
||||||
return S_OK;
|
|
||||||
}
|
}
|
||||||
return S_OUT_OF_MEMORY;
|
location += chunk_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ENONE;
|
||||||
size_t free_page_count()
|
|
||||||
{
|
|
||||||
return page_stack.base_pointer - page_stack.stack_pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *page_stack_bottom()
|
|
||||||
{
|
|
||||||
return (void*)page_stack.base_pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *page_stack_top()
|
|
||||||
{
|
|
||||||
return (void*)page_stack.limit_pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
physaddr_t create_address_space()
|
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 +294,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 +302,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 +319,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);
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
11
src/queue.c
11
src/queue.c
@@ -23,9 +23,13 @@ void queue_construct(struct queue_t *queue)
|
|||||||
queue->count = 0;
|
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));
|
struct queue_node_t *node = kmalloc(sizeof(struct queue_node_t));
|
||||||
|
if(node == NULL)
|
||||||
|
{
|
||||||
|
return ENOMEM;
|
||||||
|
}
|
||||||
node->ptr = ptr;
|
node->ptr = ptr;
|
||||||
node->next = NULL;
|
node->next = NULL;
|
||||||
if(queue->last == NULL)
|
if(queue->last == NULL)
|
||||||
@@ -38,11 +42,16 @@ void queue_insert(struct queue_t *queue, void *ptr)
|
|||||||
queue->last = node;
|
queue->last = node;
|
||||||
}
|
}
|
||||||
queue->count++;
|
queue->count++;
|
||||||
|
return ENONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *queue_get_next(struct queue_t *queue)
|
void *queue_get_next(struct queue_t *queue)
|
||||||
{
|
{
|
||||||
struct queue_node_t *node = queue->first;
|
struct queue_node_t *node = queue->first;
|
||||||
|
if(node == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
queue->first = node->next;
|
queue->first = node->next;
|
||||||
if(queue->first == NULL)
|
if(queue->first == NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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 == (unsigned long)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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,54 +4,17 @@
|
|||||||
#include "heap.h"
|
#include "heap.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "system.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)
|
|
||||||
{
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
context->eip = pc;
|
context->eip = pc;
|
||||||
|
context->cs = 0x1B;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_context_stack(struct process_context_t *context, void *stack)
|
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)
|
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)
|
void set_context_return(struct process_context_t *context, unsigned long value)
|
||||||
{
|
{
|
||||||
context->eax = value;
|
context->gp_registers[0] = value;
|
||||||
}
|
}
|
||||||
@@ -7,8 +7,8 @@
|
|||||||
.type load_context, @function
|
.type load_context, @function
|
||||||
load_context:
|
load_context:
|
||||||
mov 4(%esp), %eax
|
mov 4(%esp), %eax
|
||||||
push 0x1C(%eax)
|
|
||||||
push 0x20(%eax)
|
push 0x20(%eax)
|
||||||
|
push 0x1C(%eax)
|
||||||
push 0x2C(%eax)
|
push 0x2C(%eax)
|
||||||
push 0x24(%eax)
|
push 0x24(%eax)
|
||||||
push 0x28(%eax)
|
push 0x28(%eax)
|
||||||
@@ -19,10 +19,11 @@ load_context:
|
|||||||
mov 0x10(%eax), %edi
|
mov 0x10(%eax), %edi
|
||||||
mov 0x14(%eax), %esi
|
mov 0x14(%eax), %esi
|
||||||
mov 0x18(%eax), %ebp
|
mov 0x18(%eax), %ebp
|
||||||
mov 0x1C(%eax), %ax
|
mov 0x20(%eax), %ax
|
||||||
mov %ax, %ds
|
mov %ax, %ds
|
||||||
mov %ax, %es
|
mov %ax, %es
|
||||||
mov %ax, %fs
|
mov %ax, %fs
|
||||||
mov %ax, %gs
|
mov %ax, %gs
|
||||||
pop %eax
|
pop %eax
|
||||||
iret
|
iret
|
||||||
|
.size load_context, . - load_context
|
||||||
@@ -68,6 +68,14 @@ struct multiboot2_memory_map_t
|
|||||||
struct multiboot2_map_entry_t entries;
|
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)
|
void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
|
||||||
{
|
{
|
||||||
uint32_t *int_table = (uint32_t *)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:
|
case MB_END_TAG:
|
||||||
return NULL;
|
return NULL;
|
||||||
|
case MB_MEMORY_INFO:
|
||||||
|
boot_info->memory_size = ((struct multiboot2_memory_info_t*) table)->high_memory * 1024;
|
||||||
|
break;
|
||||||
case MB_MEMORY_MAP: ;
|
case MB_MEMORY_MAP: ;
|
||||||
unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16;
|
unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16;
|
||||||
unsigned int entry_size = ((struct multiboot2_memory_map_t*) table)->entry_size;
|
unsigned int entry_size = ((struct multiboot2_memory_map_t*) table)->entry_size;
|
||||||
|
|||||||
@@ -58,14 +58,14 @@ isr_preempt:
|
|||||||
mov 4(%ebp), %eax
|
mov 4(%ebp), %eax
|
||||||
push %eax
|
push %eax
|
||||||
|
|
||||||
// Load ESP, then push it onto stack
|
|
||||||
mov 12(%ebp), %eax
|
|
||||||
push %eax
|
|
||||||
|
|
||||||
// Load SS, then push it onto stack
|
// Load SS, then push it onto stack
|
||||||
mov 16(%ebp), %eax
|
mov 16(%ebp), %eax
|
||||||
push %eax
|
push %eax
|
||||||
|
|
||||||
|
// Load ESP, then push it onto stack
|
||||||
|
mov 12(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
// Load EBP, then push it onto stack
|
// Load EBP, then push it onto stack
|
||||||
mov -4(%ebp), %eax
|
mov -4(%ebp), %eax
|
||||||
push %eax
|
push %eax
|
||||||
@@ -84,10 +84,10 @@ isr_preempt:
|
|||||||
// Push pointer to the process context saved on the stack
|
// Push pointer to the process context saved on the stack
|
||||||
push %esp
|
push %esp
|
||||||
|
|
||||||
call save_context
|
call kernel_store_active_context
|
||||||
mov %ebp, %esp
|
mov %ebp, %esp
|
||||||
|
|
||||||
call next_process
|
call kernel_advance_scheduler
|
||||||
|
|
||||||
push %eax
|
push %eax
|
||||||
call load_context
|
call load_context
|
||||||
|
|||||||
Reference in New Issue
Block a user