hazel/kernel/task.c
2024-07-07 23:42:44 -04:00

55 lines
1.7 KiB
C

#include <kernel/task.h>
#include <kernel/mem.h>
extern uint32_t boot_page_tab[1024];
extern kernel_ctx_t ctx;
int task_init(task_t *task, elf_t *elf) {
section_t *text = elf_find_section(elf, ".text");
if (!text) return 0;
//task->eip = elf->e_entry + text->sh_offset;
// Allocate a page for a stack
task->esp = (uint32_t)mmap_find_first_free_block() + 0x1000;
uint32_t *page_dir = mmap_find_first_free_block();
task->cr3 = (uint32_t)page_dir - KERNEL_VMA;
uint32_t *page_tab = mmap_find_first_free_block();
for (int i = 0; i < 1024; i++) {
// Map kernel to 3GiB
if (i == 768) page_dir[i] = ((uint32_t)boot_page_tab - KERNEL_VMA) | 3;
// Map 3.4GiB-4GiB as non-present kernel entries
else if (i > 768) page_dir[i] = 4;
// Map remaining entries as non-present u/s
else page_dir[i] = 6;
}
// Map the ELF image to its specified entry
// This will cause problems if the ELF image is larger than 4 MiB so I need to fix this
page_dir[PAGE_DIR_INDEX(elf->e_entry)] = ((uint32_t)page_tab - KERNEL_VMA) | 7;
uint32_t elf_phys = (uint32_t)elf - KERNEL_VMA;
for (int i = 0; i < 1024; i++) {
page_tab[i] = (elf_phys + 0x1000*i) | 7;
}
return 1;
}
void task_sort() {
if (!ctx.current_task || !ctx.current_task->next) return; // If the list is empty or has only one node
task_t* new_head = (task_t *)ctx.current_task->next;
task_t* temp = new_head;
while (temp->next) {
temp = (task_t *)temp->next;
}
temp->next = (uint32_t *)ctx.current_task;
ctx.current_task->next = 0;
ctx.current_task = new_head;
}
void schedule(void) {
// If there's only one task running
if (!ctx.current_task->next) return;
}