2024-07-28 10:11:15 -04:00
|
|
|
#include "kernel/kernel.h"
|
2024-07-06 03:51:46 -04:00
|
|
|
#include <kernel/task.h>
|
|
|
|
#include <kernel/mem.h>
|
|
|
|
|
|
|
|
extern uint32_t boot_page_tab[1024];
|
|
|
|
extern kernel_ctx_t ctx;
|
|
|
|
|
2024-07-28 10:11:15 -04:00
|
|
|
//int task_init(task_t *task, uint32_t *img) {
|
2024-07-06 03:51:46 -04:00
|
|
|
int task_init(task_t *task, elf_t *elf) {
|
2024-07-28 10:11:15 -04:00
|
|
|
if (!elf) return 0;
|
2024-10-03 22:13:13 -04:00
|
|
|
if (!elf->e_phoff) return 0;
|
2024-07-28 10:11:15 -04:00
|
|
|
|
|
|
|
task->eip = elf->e_entry;
|
2024-07-06 03:51:46 -04:00
|
|
|
// Allocate a page for a stack
|
|
|
|
task->esp = (uint32_t)mmap_find_first_free_block() + 0x1000;
|
2024-07-28 10:11:15 -04:00
|
|
|
// Allocate a page for stdin
|
|
|
|
// LATER: Allow for allocating more on demand
|
|
|
|
task->stdin = (uint8_t *)mmap_find_first_free_block();
|
|
|
|
task->stdin_len = 0;
|
2024-07-06 03:51:46 -04:00
|
|
|
|
|
|
|
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++) {
|
2024-07-28 10:18:59 -04:00
|
|
|
// Map kernel to 3GiB
|
|
|
|
if (i == 768) page_dir[i] = ((uint32_t)boot_page_tab - KERNEL_VMA) | 7;
|
|
|
|
// 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;
|
2024-07-06 03:51:46 -04:00
|
|
|
}
|
2024-10-03 22:13:13 -04:00
|
|
|
|
|
|
|
phdr_t *phdr = (phdr_t *)((uint32_t)elf + elf->e_phoff);
|
|
|
|
for (int i = 0; i < elf->e_phnum; i++) {
|
|
|
|
if (phdr[i].p_type == PT_LOAD) {
|
|
|
|
uint8_t *s = (uint8_t *)mmap_find_first_free_block();
|
|
|
|
memcpy(s, (uint8_t *)elf + phdr[i].p_offset, phdr[i].p_filesz);
|
|
|
|
// Map the ELF image to its specified entry
|
|
|
|
page_dir[PAGE_DIR_INDEX(phdr[i].p_vaddr)] = ((uint32_t)page_tab - KERNEL_VMA) | 7;
|
|
|
|
page_tab[PAGE_TAB_INDEX(phdr[i].p_vaddr)] = ((uint32_t)s - KERNEL_VMA) | 7;
|
|
|
|
}
|
|
|
|
}
|
2024-07-06 03:51:46 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-07-07 23:42:44 -04:00
|
|
|
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) {
|
2024-07-28 10:18:59 -04:00
|
|
|
temp = (task_t *)temp->next;
|
2024-07-07 23:42:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
temp->next = (uint32_t *)ctx.current_task;
|
|
|
|
ctx.current_task->next = 0;
|
|
|
|
ctx.current_task = new_head;
|
|
|
|
}
|
|
|
|
|
2024-07-06 03:51:46 -04:00
|
|
|
void schedule(void) {
|
|
|
|
// If there's only one task running
|
|
|
|
if (!ctx.current_task->next) return;
|
|
|
|
}
|