getting there...
This commit is contained in:
@@ -67,19 +67,25 @@ uint32_t switch_context(uint32_t current_esp) {
|
||||
|
||||
current_process = next_process;
|
||||
|
||||
if (process_table[current_process].flags & PROCESS_FLAG_USER ) {
|
||||
tss.esp0 = process_table[current_process].kstack_top;
|
||||
}
|
||||
|
||||
if (fetch_cr3() != process_table[current_process].cr3) {
|
||||
uint32_t current_cr3 = fetch_cr3();
|
||||
if (current_cr3 != process_table[current_process].cr3) {
|
||||
kprintf("switching cr3 | old: 0x%x | new: 0x%x\n", current_cr3, process_table[current_process].cr3);
|
||||
load_pd((void*)process_table[current_process].cr3);
|
||||
}
|
||||
|
||||
if (process_table[current_process].flags & PROCESS_FLAG_USER) {
|
||||
tss.esp0 = process_table[current_process].kstack_top;
|
||||
}
|
||||
|
||||
if (found_ready) {
|
||||
kprintf("found ready task %d | cr3: 0x%x\n", current_process, process_table[current_process].cr3);
|
||||
}
|
||||
return process_table[current_process].esp;
|
||||
}
|
||||
|
||||
void init_scheduler() {
|
||||
process_table = kalloc(16 * sizeof(process_t));
|
||||
memset((void*)process_table, 0, 16 * sizeof(process_t));
|
||||
total_processes = 0;
|
||||
|
||||
process_t* process = &process_table[total_processes];
|
||||
@@ -99,25 +105,15 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
lock_scheduler();
|
||||
|
||||
// default to a new task, but if we find a dead one, re use it
|
||||
int reuse = 0;
|
||||
process_t* process = &process_table[total_processes];
|
||||
uint32_t pid = total_processes;
|
||||
for (uint32_t i = 0; i < total_processes; i++) {
|
||||
if (process_table[i].state == STATE_DEAD) {
|
||||
process = &process_table[i];
|
||||
pid = i;
|
||||
reuse = 1;
|
||||
}
|
||||
}
|
||||
|
||||
process->pid = pid;
|
||||
process->cr3 = cr3;
|
||||
process->state = STATE_READY;
|
||||
process->sleep = 0;
|
||||
process->flags = user ? PROCESS_FLAG_USER : PROCESS_FLAG_KERNEL;
|
||||
total_processes++;
|
||||
|
||||
void* kstack_bottom = kalloc(PAGE_SIZE);
|
||||
uint32_t kstack_top = (uint32_t)kstack_bottom + PAGE_SIZE;
|
||||
@@ -130,13 +126,11 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
|
||||
*(--esp) = 0x0202; // EFLAGS (Interrupts enabled)
|
||||
*(--esp) = 0x1B; // User Code Segment (CS) with RPL 3 (0x18 | 3)
|
||||
*(--esp) = entry_point; // EIP
|
||||
*(--esp) = 0x23;
|
||||
} else {
|
||||
// IRET values for Ring 0
|
||||
*(--esp) = 0x0202; // EFLAGS
|
||||
*(--esp) = 0x08; // Kernel Code Segment (CS)
|
||||
*(--esp) = entry_point; // EIP
|
||||
*(--esp) = 0x10;
|
||||
}
|
||||
|
||||
*(--esp) = 0; // EAX
|
||||
@@ -149,17 +143,11 @@ int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
|
||||
*(--esp) = 0; // EDI
|
||||
|
||||
process->esp = (uint32_t)esp;
|
||||
if (!reuse) {
|
||||
total_processes++;
|
||||
}
|
||||
unlock_scheduler();
|
||||
return 1;
|
||||
}
|
||||
|
||||
process_t* current_task() {
|
||||
lock_scheduler();
|
||||
process_t* rtn = &process_table[current_process];
|
||||
unlock_scheduler();
|
||||
return rtn;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user