diff options
author | Marvin Borner | 2020-04-25 23:53:19 +0200 |
---|---|---|
committer | Marvin Borner | 2020-04-25 23:53:19 +0200 |
commit | 69d08dcb4580f8e544464bf54432ab07a7f58fc6 (patch) | |
tree | 878d35b5d3e3fac8f1c8e2a043519658d440f350 /src/kernel/tasks | |
parent | f2175ab40a977d15526e7ebc3ad327e3ef825069 (diff) |
Small cleanup and formatting
Diffstat (limited to 'src/kernel/tasks')
-rw-r--r-- | src/kernel/tasks/process.asm | 48 | ||||
-rw-r--r-- | src/kernel/tasks/task.c | 177 | ||||
-rw-r--r-- | src/kernel/tasks/task.h | 30 |
3 files changed, 0 insertions, 255 deletions
diff --git a/src/kernel/tasks/process.asm b/src/kernel/tasks/process.asm deleted file mode 100644 index a46ac3d..0000000 --- a/src/kernel/tasks/process.asm +++ /dev/null @@ -1,48 +0,0 @@ -[GLOBAL read_eip] -read_eip: - pop eax - jmp eax - -[GLOBAL copy_page_physical] -copy_page_physical: - push ebx - pushf - - cli - - mov ebx, [esp+12] - mov ecx, [esp+16] - - mov edx, cr0 - and edx, 0x7fffffff - mov cr0, edx - - mov edx, 1024 - -.loop: - mov eax, [ebx] - mov [ecx], eax - add ebx, 4 - add ecx, 4 - dec edx - jnz .loop - - mov edx, cr0 - or edx, 0x80000000 - mov cr0, edx - - popf - pop ebx - ret - -[GLOBAL perform_task_switch] -perform_task_switch: - cli - mov ecx, [esp+4] - mov eax, [esp+8] - mov ebp, [esp+12] - mov esp, [esp+16] - mov cr3, eax - mov eax, 0x12345 - sti - jmp ecx
\ No newline at end of file diff --git a/src/kernel/tasks/task.c b/src/kernel/tasks/task.c deleted file mode 100644 index 036c5d4..0000000 --- a/src/kernel/tasks/task.c +++ /dev/null @@ -1,177 +0,0 @@ -#include <kernel/memory/paging.h> -#include <kernel/tasks/task.h> -#include <kernel/memory/alloc.h> -#include <kernel/lib/lib.h> -#include <kernel/io/io.h> -#include <kernel/gdt/gdt.h> -#include <kernel/system.h> - -task_t *current_task; -task_t *ready_queue; - -extern uint32_t read_eip(); - -uint32_t next_pid = 1; - -void tasking_install() -{ - cli(); - move_stack((void *)0xE0000000, 0x2000); - - current_task = ready_queue = (task_t *)kmalloc(sizeof(task_t)); - current_task->id = (int)next_pid++; - current_task->esp = 0; - current_task->ebp = 0; - current_task->eip = 0; - current_task->page_directory = current_page_directory; - current_task->next = 0; - current_task->kernel_stack = kmalloc(KERNEL_STACK_SIZE); - - vga_log("Installed Tasking"); - sti(); -} - -void move_stack(void *new_stack_start, uint32_t size) -{ - for (uint32_t i = (uint32_t)new_stack_start; i >= ((uint32_t)new_stack_start - size); - i -= 0x1000) { - // paging_alloc_frame(paging_get_page(i, 1, current_page_directory), 0, 1); - } - - uint32_t pd_addr; - asm volatile("mov %%cr3, %0" : "=r"(pd_addr)); - asm volatile("mov %0, %%cr3" : : "r"(pd_addr)); - - uint32_t old_stack_pointer; - asm volatile("mov %%esp, %0" : "=r"(old_stack_pointer)); - uint32_t old_base_pointer; - asm volatile("mov %%ebp, %0" : "=r"(old_base_pointer)); - - uint32_t offset = (uint32_t)new_stack_start - initial_esp; - - uint32_t new_stack_pointer = old_stack_pointer + offset; - uint32_t new_base_pointer = old_base_pointer + offset; - - memcpy((void *)new_stack_pointer, (void *)old_stack_pointer, - initial_esp - old_stack_pointer); - - for (uint32_t i = (uint32_t)new_stack_start; i > (uint32_t)new_stack_start - size; i -= 4) { - uint32_t tmp = *(uint32_t *)i; - if ((old_stack_pointer < tmp) && (tmp < initial_esp)) { - tmp = tmp + offset; - uint32_t *tmp2 = (uint32_t *)i; - *tmp2 = tmp; - } - } - - asm volatile("mov %0, %%esp" : : "r"(new_stack_pointer)); - asm volatile("mov %0, %%ebp" : : "r"(new_base_pointer)); -} - -extern void perform_task_switch(uint32_t, uint32_t, uint32_t, uint32_t); - -void switch_task() -{ - if (!current_task) - return; - - uint32_t esp, ebp, eip; - asm volatile("mov %%esp, %0" : "=r"(esp)); - asm volatile("mov %%ebp, %0" : "=r"(ebp)); - - eip = read_eip(); - - if (eip == 0x12345) - return; - - current_task->eip = eip; - current_task->esp = esp; - current_task->ebp = ebp; - - current_task = current_task->next; - if (!current_task) - current_task = ready_queue; - - eip = current_task->eip; - esp = current_task->esp; - ebp = current_task->ebp; - - current_page_directory = current_task->page_directory; - - set_kernel_stack((uintptr_t)(current_task->kernel_stack + KERNEL_STACK_SIZE)); - - paging_switch_directory((int)current_page_directory); - perform_task_switch(eip, (uint32_t)current_page_directory, ebp, esp); -} - -int fork() -{ - cli(); - - task_t *parent_task = (task_t *)current_task; - - uint32_t *directory = 0; //paging_clone_directory(current_page_directory); - - task_t *new_task = (task_t *)kmalloc(sizeof(task_t)); - new_task->id = (int)next_pid++; - new_task->esp = new_task->ebp = 0; - new_task->eip = 0; - new_task->page_directory = directory; - current_task->kernel_stack = kmalloc(KERNEL_STACK_SIZE); - new_task->next = 0; - - task_t *tmp_task = (task_t *)ready_queue; - while (tmp_task->next) - tmp_task = tmp_task->next; - tmp_task->next = new_task; - - uint32_t eip = read_eip(); - - if (current_task == parent_task) { - uint32_t esp; - asm volatile("mov %%esp, %0" : "=r"(esp)); - uint32_t ebp; - asm volatile("mov %%ebp, %0" : "=r"(ebp)); - new_task->esp = esp; - new_task->ebp = ebp; - new_task->eip = eip; - sti(); - - return new_task->id; - } else { - return 0; - } -} - -int getpid() -{ - return current_task->id; -} - -void exec(uint32_t binary) -{ - set_kernel_stack((uintptr_t)(current_task->kernel_stack + KERNEL_STACK_SIZE)); - - info("Switching to user mode..."); - - asm volatile("\ - cli; \ - mov $0x23, %%ax; \ - mov %%ax, %%ds; \ - mov %%ax, %%es; \ - mov %%ax, %%fs; \ - mov %%ax, %%gs; \ - mov %%esp, %%eax; \ - pushl $0x23; \ - pushl %%esp; \ - pushf; \ - pushl $0x1B; \ - push $1f; \ - iret; \ - 1: \ - " - : - : "r"(binary)); - - // syscall_write("test"); -}
\ No newline at end of file diff --git a/src/kernel/tasks/task.h b/src/kernel/tasks/task.h deleted file mode 100644 index 8652e44..0000000 --- a/src/kernel/tasks/task.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef MELVIX_TASK_H -#define MELVIX_TASK_H - -#include <stdint.h> -#include <kernel/memory/paging.h> - -#define KERNEL_STACK_SIZE 2048 - -typedef struct task { - int id; - uint32_t esp, ebp; - uint32_t eip; - uint32_t *page_directory; - uint32_t *kernel_stack; - struct task *next; -} task_t; - -void tasking_install(); - -void switch_task(); - -int fork(); - -void move_stack(void *new_stack_start, uint32_t size); - -int getpid(); - -void exec(uint32_t binary); - -#endif
\ No newline at end of file |