Rewrote physical memory allocator

The physical memory allocator now uses a buddy allocator instead of a stack.

Also moved some of the platform-independent context code to kernel.c.
This commit is contained in:
2022-08-23 13:00:26 -05:00
parent 54e2beefc1
commit bacedbea86
10 changed files with 314 additions and 156 deletions

View File

@@ -4,54 +4,17 @@
#include "heap.h"
#include "string.h"
#include "system.h"
#include "x86/processstate.h"
void *initialize_context(void *task_entry)
{
physaddr_t stack_frame = reserve_page();
if(stack_frame % page_size != 0)
{
return NULL;
}
map_page((void*)&_kernel_start - page_size, stack_frame, PAGE_RW | PAGE_USERMODE);
unmap_page((void*)&_kernel_start - (2 * page_size));
struct process_context_t *context = kmalloc(sizeof(struct process_context_t));
if(context != NULL)
{
memset(context, 0, sizeof(struct process_context_t));
uint32_t flags;
asm("pushf; "
"mov (%%esp), %0; "
"popf; "
: "=r"(flags));
context->cs = 0x1B;
context->eip = (uint32_t)task_entry;
context->flags = (flags & ~0xFD) | 0x200;
context->ss = 0x23;
context->esp = &_kernel_start;
context->ebp = &_kernel_start;
}
return (void*)context;
}
void destroy_context(void *ctx)
{
kfree(ctx);
}
void save_context(struct process_context_t *context)
{
kernel_store_active_context(context, sizeof(*context));
}
void set_context_pc(struct process_context_t *context, void *pc)
{
context->eip = pc;
context->cs = 0x1B;
}
void set_context_stack(struct process_context_t *context, void *stack)
{
context->esp = stack;
context->gp_registers[7] = stack;
context->ss = 0x23;
}
void set_context_flags(struct process_context_t *context, unsigned long flags)
@@ -61,5 +24,5 @@ void set_context_flags(struct process_context_t *context, unsigned long flags)
void set_context_return(struct process_context_t *context, unsigned long value)
{
context->eax = value;
context->gp_registers[0] = value;
}

View File

@@ -7,8 +7,8 @@
.type load_context, @function
load_context:
mov 4(%esp), %eax
push 0x1C(%eax)
push 0x20(%eax)
push 0x1C(%eax)
push 0x2C(%eax)
push 0x24(%eax)
push 0x28(%eax)
@@ -19,10 +19,11 @@ load_context:
mov 0x10(%eax), %edi
mov 0x14(%eax), %esi
mov 0x18(%eax), %ebp
mov 0x1C(%eax), %ax
mov 0x20(%eax), %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
pop %eax
iret
iret
.size load_context, . - load_context

View File

@@ -68,6 +68,14 @@ struct multiboot2_memory_map_t
struct multiboot2_map_entry_t entries;
};
struct multiboot2_memory_info_t
{
uint32_t type;
uint32_t size;
uint32_t low_memory;
uint32_t high_memory;
};
void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
{
uint32_t *int_table = (uint32_t *)table;
@@ -75,6 +83,9 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table)
{
case MB_END_TAG:
return NULL;
case MB_MEMORY_INFO:
boot_info->memory_size = ((struct multiboot2_memory_info_t*) table)->high_memory * 1024;
break;
case MB_MEMORY_MAP: ;
unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16;
unsigned int entry_size = ((struct multiboot2_memory_map_t*) table)->entry_size;

View File

@@ -58,14 +58,14 @@ isr_preempt:
mov 4(%ebp), %eax
push %eax
// Load ESP, then push it onto stack
mov 12(%ebp), %eax
push %eax
// Load SS, then push it onto stack
mov 16(%ebp), %eax
push %eax
// Load ESP, then push it onto stack
mov 12(%ebp), %eax
push %eax
// Load EBP, then push it onto stack
mov -4(%ebp), %eax
push %eax
@@ -84,7 +84,7 @@ isr_preempt:
// Push pointer to the process context saved on the stack
push %esp
call save_context
call kernel_store_active_context
mov %ebp, %esp
call kernel_advance_scheduler