Add stack frame to panic

This commit is contained in:
ssimnb 2026-02-01 14:55:30 +01:00
parent b337e5193b
commit cfcb806ebf
16 changed files with 169 additions and 103 deletions

View file

@ -7,23 +7,29 @@
#include "../mm/kmalloc.h"
#include "sched.h"
extern void get_context(struct context *ctx);
extern void switch_context(context **old, context *new);
extern void switch_context(context *old, context *new);
extern void restore_stack(uint64_t rsp, uint64_t rbp);
#define QUANTUM_US 10000
int next_pid = 1;
void idle_task(){
kprintf("Hello world from idle task!\n");
for(;;){
kprintf("Idle!!1\n");
}
}
void test_task(){
kprintf("Hello world from scheduled task!\n");
return;
for(;;){
kprintf("Hello world from scheduled task!\n");
}
}
void best_task(){
for(;;){
kprintf("Hello world I am best\n");
}
}
/* Setup a process structure */
@ -36,21 +42,28 @@ proc *alloc_process(void){
p->state = READY;
p->kstack = kmalloc(INITIAL_STACK_SIZE);
p->kstack = kzalloc(8 * 4096);
p->pid = next_pid++;
p->context.rbp = (uint64_t)p->kstack;
p->context.rsp = (uint64_t)p->context.rbp + INITIAL_STACK_SIZE;
uint64_t *sp = (uint64_t*)((uint64_t)p->kstack + 8 * 4096);
// Allocate space for context
sp -= sizeof(context)/sizeof(uint64_t);
p->context = (context*)sp;
p->context->rbp = (uint64_t)p->kstack;
return p;
}
}
klog(__func__, "Couldnt find free process!!!!!!\n");
return NULL;
}
proc *add_task(uint64_t *entry){
proc *add_process(uint64_t *entry){
proc *proc = alloc_process();
if (proc == NULL) {
@ -58,29 +71,31 @@ proc *add_task(uint64_t *entry){
kkill();
}
proc->context.rip = (uint64_t)entry;
proc->context->rip = (uint64_t)entry;
kprintf("entry: 0x{xn}", entry);
return proc;
}
void scheduler_init(){
if(!get_cpu_struct_initialized()){
kprintf("sched: cpu struct not initialized!");
klog(__func__, "CPU struct not initialized!");
kkill();
}
cpu_state *state = get_cpu_struct();
if(state->current_process != NULL){
kprintf("sched: scheduler on CPU {d} already initialized!\n", state->lapic_id);
kkill();
kprintf("scheduler on CPU {d} already initialized!\n", state->lapic_id);
return;
}
proc *proc_list = state->process_list;
memset(proc_list, 0, sizeof(proc) * 512);
proc *idle = add_task((uint64_t*)idle_task);
proc *idle = add_process((uint64_t*)idle_task);
if(idle == NULL){
klog(__func__, "Failed to allocate idle task");
@ -89,27 +104,30 @@ void scheduler_init(){
state->current_process = idle;
add_task((uint64_t*)test_task);
add_process((uint64_t*)test_task);
add_process((uint64_t *)best_task);
state->scheduler_initialized = true;
int i = 0;
for(;;){
for(int i = 0; i < PROC_MAX; i++){
for(; i < PROC_MAX; i++){
if(proc_list[i].state == READY){
context old_state = state->current_process->context;
proc *prev = state->current_process;
prev->state = READY;
state->current_process = &proc_list[i];
state->current_process->state = RUNNING;
switch_context(&(get_cpu_struct()->scheduler_context), get_cpu_struct()->current_process->context);
get_context(&state->scheduler_context);
switch_context(&old_state, &state->current_process->context);
}
}
i = 1;
}
}
void scheduler_tick(){
cpu_state *state = get_cpu_struct();
proc *proc_list = state->process_list;
switch_context(&state->current_process->context, &state->scheduler_context);
void enter_scheduler(){
switch_context(&get_cpu_struct()->current_process->context, (get_cpu_struct()->scheduler_context));
}