Moved PCB for each process to kernel heap
This commit is contained in:
@@ -40,6 +40,12 @@ struct message_t
|
|||||||
uint32_t param1, param2, param3;
|
uint32_t param1, param2, param3;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct address_space_t
|
||||||
|
{
|
||||||
|
physaddr_t top_table;
|
||||||
|
int counter;
|
||||||
|
};
|
||||||
|
|
||||||
struct process_t
|
struct process_t
|
||||||
{
|
{
|
||||||
size_t priority;
|
size_t priority;
|
||||||
@@ -70,10 +76,14 @@ int active_process();
|
|||||||
|
|
||||||
int add_process(void *program_entry, int priority, physaddr_t address_space);
|
int add_process(void *program_entry, int priority, physaddr_t address_space);
|
||||||
|
|
||||||
struct process_context_t *next_process(struct process_context_t *prev_state);
|
struct process_context_t *next_process();
|
||||||
|
|
||||||
int terminate_process(size_t process_id);
|
int terminate_process(size_t process_id);
|
||||||
|
|
||||||
|
int store_active_context(struct process_context_t *context, size_t size);
|
||||||
|
|
||||||
|
struct process_context_t *get_active_context();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
int accept_message(size_t process_id, struct message_t *message);
|
int accept_message(size_t process_id, struct message_t *message);
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
struct process_context_t;
|
||||||
|
|
||||||
void *initialize_context(void *task_entry);
|
void *initialize_context(void *task_entry);
|
||||||
|
|
||||||
void destroy_context(void *ctx);
|
void destroy_context(void *ctx);
|
||||||
|
|
||||||
void save_context(struct process_context_t *context, void *ptr);
|
void save_context(struct process_context_t *context);
|
||||||
|
|
||||||
void load_context(struct process_context_t *context);
|
void load_context(struct process_context_t *context);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ quark_kernel_SOURCES += x86/paging.c \
|
|||||||
x86/isr.c \
|
x86/isr.c \
|
||||||
x86/msr.c \
|
x86/msr.c \
|
||||||
x86/context.c \
|
x86/context.c \
|
||||||
x86/contextswitch.S \
|
x86/load_context.S \
|
||||||
x86/preempt.S \
|
x86/preempt.S \
|
||||||
x86/quark_x86.c \
|
x86/quark_x86.c \
|
||||||
x86/entry.S
|
x86/entry.S
|
||||||
|
|||||||
22
src/kernel.c
22
src/kernel.c
@@ -57,7 +57,7 @@ void kernel_initialize(struct boot_info_t *boot_info)
|
|||||||
asm("hlt");*/
|
asm("hlt");*/
|
||||||
|
|
||||||
irq_enable();
|
irq_enable();
|
||||||
load_context(next_process(NULL));
|
load_context(next_process());
|
||||||
}
|
}
|
||||||
|
|
||||||
int set_syscall(int id, int arg_count, int pid, void *func_ptr)
|
int set_syscall(int id, int arg_count, int pid, void *func_ptr)
|
||||||
@@ -219,13 +219,8 @@ int add_process(void *program_entry, int priority, physaddr_t address_space)
|
|||||||
return new_process->resource_id;
|
return new_process->resource_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct process_context_t *next_process(struct process_context_t *prev_state)
|
struct process_context_t *next_process()
|
||||||
{
|
{
|
||||||
if(prev_state != NULL)
|
|
||||||
{
|
|
||||||
kernel.active_process->state = prev_state;
|
|
||||||
queue_insert(&kernel.priority_queue, kernel.active_process, kernel.active_process->priority);
|
|
||||||
}
|
|
||||||
kernel.active_process = extract_min(&kernel.priority_queue);
|
kernel.active_process = extract_min(&kernel.priority_queue);
|
||||||
if(kernel.active_process != NULL)
|
if(kernel.active_process != NULL)
|
||||||
{
|
{
|
||||||
@@ -254,6 +249,19 @@ int terminate_process(size_t process_id)
|
|||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int store_active_context(struct process_context_t *context, size_t size)
|
||||||
|
{
|
||||||
|
if(kernel.active_process != NULL && kernel.active_process->state != NULL)
|
||||||
|
{
|
||||||
|
memcpy(kernel.active_process->state, context, size);
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return S_DOESNT_EXIST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
int accept_message(size_t process_id, struct message_t *message)
|
int accept_message(size_t process_id, struct message_t *message)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,35 +1,45 @@
|
|||||||
#include "platform/context.h"
|
#include "platform/context.h"
|
||||||
|
#include "kernel.h"
|
||||||
#include "mmgr.h"
|
#include "mmgr.h"
|
||||||
|
#include "heap.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "system.h"
|
||||||
#include "x86/processstate.h"
|
#include "x86/processstate.h"
|
||||||
|
|
||||||
void *initialize_context(void *task_entry)
|
void *initialize_context(void *task_entry)
|
||||||
{
|
{
|
||||||
/*
|
physaddr_t stack_frame = reserve_page();
|
||||||
* TODO: this implementation is a goddamn mess.
|
if(stack_frame % page_size != 0)
|
||||||
* Stack pointer is hardcoded, and the stack isn't resizable.
|
{
|
||||||
* PCB pointer is just a constant.
|
return NULL;
|
||||||
*/
|
}
|
||||||
map_page(NULL, reserve_page(), PAGE_RW);
|
map_page((void*)&_kernel_start - page_size, stack_frame, PAGE_RW | PAGE_USERMODE);
|
||||||
map_page((void*)0xFF3FF000, reserve_page(), PAGE_RW | PAGE_USERMODE);
|
unmap_page((void*)&_kernel_start - (2 * page_size));
|
||||||
unmap_page((void*)0xFF3FE000);
|
struct process_context_t *context = kmalloc(sizeof(struct process_context_t));
|
||||||
uint32_t flags;
|
if(context != NULL)
|
||||||
asm("pushf; "
|
{
|
||||||
"mov (%%esp), %0; "
|
memset(context, 0, sizeof(struct process_context_t));
|
||||||
"popf; "
|
uint32_t flags;
|
||||||
: "=r"(flags));
|
asm("pushf; "
|
||||||
struct process_context_t *state = (struct process_context_t*)PCB_LOCATION;
|
"mov (%%esp), %0; "
|
||||||
memset(NULL, 0, page_size);
|
"popf; "
|
||||||
state->cs = 0x1B;
|
: "=r"(flags));
|
||||||
state->eip = (uint32_t)task_entry;
|
context->cs = 0x1B;
|
||||||
state->flags = (flags & ~0xFD) | 0x200;
|
context->eip = (uint32_t)task_entry;
|
||||||
state->ss = 0x23;
|
context->flags = (flags & ~0xFD) | 0x200;
|
||||||
state->esp = 0xFF400000;
|
context->ss = 0x23;
|
||||||
state->ebp = 0xFF400000;
|
context->esp = &_kernel_start;
|
||||||
return (void*)state;
|
context->ebp = &_kernel_start;
|
||||||
|
}
|
||||||
|
return (void*)context;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_context(void *ctx)
|
void destroy_context(void *ctx)
|
||||||
{
|
{
|
||||||
// Nothing to do...
|
kfree(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void save_context(struct process_context_t *context)
|
||||||
|
{
|
||||||
|
store_active_context(context, sizeof(*context));
|
||||||
}
|
}
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
.section .text
|
|
||||||
|
|
||||||
/*
|
|
||||||
* save_context(struct process_state_t *context, struct interrupt_frame_t *frame)
|
|
||||||
*/
|
|
||||||
.global save_context
|
|
||||||
.type save_context, @function
|
|
||||||
save_context:
|
|
||||||
push %edi
|
|
||||||
push %esi
|
|
||||||
push %eax
|
|
||||||
mov $0x10, %ax
|
|
||||||
mov %ax, %ds
|
|
||||||
mov %ax, %es
|
|
||||||
mov %ax, %fs
|
|
||||||
mov %ax, %gs
|
|
||||||
mov 16(%esp), %eax
|
|
||||||
mov %ebx, 0x04(%eax)
|
|
||||||
mov %ecx, 0x08(%eax)
|
|
||||||
mov %edx, 0x0C(%eax)
|
|
||||||
mov %edi, 0x10(%eax)
|
|
||||||
mov %esi, 0x14(%eax)
|
|
||||||
mov %ebp, 0x18(%eax)
|
|
||||||
mov %eax, %edi
|
|
||||||
pop %eax
|
|
||||||
mov %eax, 0x00(%edi)
|
|
||||||
mov 16(%esp), %esi
|
|
||||||
mov 0x10(%esi), %eax
|
|
||||||
mov %eax, 0x1C(%edi)
|
|
||||||
mov 0x0C(%esi), %eax
|
|
||||||
mov %eax, 0x20(%edi)
|
|
||||||
mov 0x08(%esi), %eax
|
|
||||||
mov %eax, 0x2C(%edi)
|
|
||||||
mov 0x04(%esi), %eax
|
|
||||||
mov %eax, 0x24(%edi)
|
|
||||||
mov 0x00(%esi), %eax
|
|
||||||
mov %eax, 0x28(%edi)
|
|
||||||
pop %esi
|
|
||||||
pop %edi
|
|
||||||
ret
|
|
||||||
|
|
||||||
/*
|
|
||||||
* load_context(struct process_state_t *context)
|
|
||||||
*/
|
|
||||||
.global load_context
|
|
||||||
.type load_context, @function
|
|
||||||
load_context:
|
|
||||||
mov 4(%esp), %eax
|
|
||||||
push 0x1C(%eax)
|
|
||||||
push 0x20(%eax)
|
|
||||||
push 0x2C(%eax)
|
|
||||||
push 0x24(%eax)
|
|
||||||
push 0x28(%eax)
|
|
||||||
push 0x00(%eax)
|
|
||||||
mov 0x04(%eax), %ebx
|
|
||||||
mov 0x08(%eax), %ecx
|
|
||||||
mov 0x0C(%eax), %edx
|
|
||||||
mov 0x10(%eax), %edi
|
|
||||||
mov 0x14(%eax), %esi
|
|
||||||
mov 0x18(%eax), %ebp
|
|
||||||
mov 0x1C(%eax), %ax
|
|
||||||
mov %ax, %ds
|
|
||||||
mov %ax, %es
|
|
||||||
mov %ax, %fs
|
|
||||||
mov %ax, %gs
|
|
||||||
pop %eax
|
|
||||||
iret
|
|
||||||
28
src/x86/load_context.S
Normal file
28
src/x86/load_context.S
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
.section .text
|
||||||
|
|
||||||
|
/*
|
||||||
|
* load_context(struct process_state_t *context)
|
||||||
|
*/
|
||||||
|
.global load_context
|
||||||
|
.type load_context, @function
|
||||||
|
load_context:
|
||||||
|
mov 4(%esp), %eax
|
||||||
|
push 0x1C(%eax)
|
||||||
|
push 0x20(%eax)
|
||||||
|
push 0x2C(%eax)
|
||||||
|
push 0x24(%eax)
|
||||||
|
push 0x28(%eax)
|
||||||
|
push 0x00(%eax)
|
||||||
|
mov 0x04(%eax), %ebx
|
||||||
|
mov 0x08(%eax), %ecx
|
||||||
|
mov 0x0C(%eax), %edx
|
||||||
|
mov 0x10(%eax), %edi
|
||||||
|
mov 0x14(%eax), %esi
|
||||||
|
mov 0x18(%eax), %ebp
|
||||||
|
mov 0x1C(%eax), %ax
|
||||||
|
mov %ax, %ds
|
||||||
|
mov %ax, %es
|
||||||
|
mov %ax, %fs
|
||||||
|
mov %ax, %gs
|
||||||
|
pop %eax
|
||||||
|
iret
|
||||||
@@ -25,14 +25,63 @@ isr_syscall:
|
|||||||
.global isr_preempt
|
.global isr_preempt
|
||||||
.type isr_preempt, @function
|
.type isr_preempt, @function
|
||||||
isr_preempt:
|
isr_preempt:
|
||||||
|
|
||||||
|
// Disable interrupts for now
|
||||||
cli
|
cli
|
||||||
|
|
||||||
|
// Save process's base pointer
|
||||||
|
push %ebp
|
||||||
|
|
||||||
|
// Save base pointer; it points to the interrupt stack frame
|
||||||
|
mov %esp, %ebp
|
||||||
|
sub $4, %ebp
|
||||||
|
|
||||||
|
// Save EAX on stack so it can be used as a buffer
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load EFLAGS, then push it onto stack
|
||||||
|
mov 8(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load EIP, then push it onto stack
|
||||||
|
mov 16(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load CS, then push it onto stack
|
||||||
|
mov 12(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load ESP, then push it onto stack
|
||||||
|
mov 4(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load SS, then push it onto stack
|
||||||
|
mov (%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Load EBP, then push it onto stack
|
||||||
|
mov -4(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Push GP registers onto stack
|
||||||
|
push %esi
|
||||||
|
push %edi
|
||||||
|
push %edx
|
||||||
|
push %ecx
|
||||||
|
push %ebx
|
||||||
|
|
||||||
|
// Restore saved value of EAX, then push it onto stack
|
||||||
|
mov -8(%ebp), %eax
|
||||||
|
push %eax
|
||||||
|
|
||||||
|
// Push pointer to the process context saved on the stack
|
||||||
push %esp
|
push %esp
|
||||||
push $0x800
|
|
||||||
call save_context
|
call save_context
|
||||||
add $8, %esp
|
mov %ebp, %esp
|
||||||
push $0x800
|
|
||||||
call next_process
|
call next_process
|
||||||
add $8, %esp
|
|
||||||
push %eax
|
push %eax
|
||||||
call load_context
|
call load_context
|
||||||
|
|
||||||
Reference in New Issue
Block a user