From c4b8ec6787bda192693817d8437c2ff0cf009173 Mon Sep 17 00:00:00 2001 From: Aren Elchinyan Date: Sun, 21 Jan 2024 21:40:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=D0=BF=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/arch.h | 5 +++-- kernel/cpu/task.c | 46 ++++++++++++++++++++-------------------- kernel/cpu/task_switch.s | 7 +++--- kernel/start.c | 14 +++++++----- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/arch.h b/include/arch.h index 7d39688..8678a14 100644 --- a/include/arch.h +++ b/include/arch.h @@ -12,14 +12,14 @@ #include -#define STACK_SIZE 8192 // 1MB +#define STACK_SIZE 8192 // 8 килобайт на стек typedef struct task { uint64_t rax, rbx, rcx, rdx; uint64_t rsi, rdi, rsp, rbp; + uint64_t cr3; uint64_t id; - uint64_t ret; void *stack; struct task *last; @@ -56,6 +56,7 @@ typedef void (*int_entry_t)(struct frame *state); void arch_init( ); void task_init( ); void task_switch(struct frame *state); +uint64_t task_new_thread(void (*func)(void *)); void cpu_init( ); void gdt_init( ); void pic_init( ); diff --git a/kernel/cpu/task.c b/kernel/cpu/task.c index 98e7502..1683485 100644 --- a/kernel/cpu/task.c +++ b/kernel/cpu/task.c @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -14,7 +15,7 @@ static volatile uint64_t next_thread_id = 0; static task_t *last_task = NULL; static task_t *kernel_task = NULL; task_t *current_task = NULL; - +uint32_t *test_buf = NULL; extern uint64_t full_init; void task_switch_asm(task_t *, task_t *); @@ -22,41 +23,39 @@ void task_switch_asm(task_t *, task_t *); void task_switch(struct frame *state) { UNUSED(state); - // LOG("Смена потоков\n"); asm volatile("cli"); - task_t *next = current_task->next; task_t *last = current_task; current_task = next; - // LOG("Смена потоков 2\n"); + // LOG("Смена потоков %u->%u\n", last->id, next->id); + outb(0x20, 0x20); task_switch_asm(last, next); - asm volatile("sti"); } -uint64_t task_new_thread(void (*func)(void *), void *arg) { - asm volatile("cli"); - +uint64_t task_new_thread(void (*func)(void *)) { LOG("Выделение потока\n"); - uint64_t cr3; - asm volatile("mov %%cr3, %0" : "=r"(cr3)); + uint64_t cr3; uint64_t *stack = mem_alloc(STACK_SIZE); task_t *new_task = mem_alloc(sizeof(task_t)); + asm volatile("mov %%cr3, %0" : "=r"(cr3)); + tool_memset(stack, 0, STACK_SIZE); tool_memset(new_task, 0, sizeof(task_t)); - uint64_t *stack_top = (uint64_t *)((uint64_t)stack + STACK_SIZE); - - *(--stack_top) = (uint64_t)arg; - *(--stack_top) = (uint64_t)func; - - new_task->rsp = (uint64_t)new_task->stack + STACK_SIZE - sizeof(uint64_t) * 2; - new_task->stack = stack; + + uint64_t stack_top = STACK_SIZE; + stack[--stack_top] = (uint64_t)stack; + stack[--stack_top] = (uint64_t)func; + stack[--stack_top] = (uint64_t)0; + + new_task->rsp = (uint64_t)new_task->stack + sizeof(uint64_t) * stack_top; new_task->id = next_thread_id++; + new_task->cr3 = cr3; new_task->last = current_task; new_task->next = current_task->next; @@ -65,14 +64,11 @@ uint64_t task_new_thread(void (*func)(void *), void *arg) { LOG("Создан новый поток с ID: %u\n", new_task->id); - if (full_init) { - asm volatile("sti"); // Включаем прерывания - } - return new_task->id; } -void dummy(uint64_t n) { +void dummy( ) { + LOG("\t\tПривет! Я поток: %u\n", current_task->id); for (;;) { asm volatile("hlt"); } } @@ -91,6 +87,7 @@ void task_init( ) { kernel_task->id = next_thread_id++; kernel_task->rsp = rsp; + kernel_task->cr3 = cr3; current_task = kernel_task; @@ -99,7 +96,10 @@ void task_init( ) { last_task = kernel_task; - // task_new_thread(dummy, 2 + 2); + task_new_thread(dummy); + task_new_thread(dummy); + + test_buf = mem_alloc(8 * 8 * sizeof(uint32_t)); LOG("Потоки инициализированы\n"); } diff --git a/kernel/cpu/task_switch.s b/kernel/cpu/task_switch.s index 1606d0f..aab15d3 100644 --- a/kernel/cpu/task_switch.s +++ b/kernel/cpu/task_switch.s @@ -9,10 +9,11 @@ task_switch_asm: movq %rdi, 40(%rdi) movq %rsp, 48(%rdi) movq %rbp, 56(%rdi) + movq %cr3, %rax movq %rax, 64(%rdi) movq 64(%rsi), %rax - movq %rax, 64(%rdi) + movq %rax, %cr3 movq 56(%rsi), %rbp movq 48(%rsi), %rsp movq 40(%rsi), %rdi @@ -22,5 +23,5 @@ task_switch_asm: movq (%rsi), %rax movq 32(%rsi), %rsi popfq - - retq \ No newline at end of file + sti + retq diff --git a/kernel/start.c b/kernel/start.c index 37053b3..d902699 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -17,6 +17,11 @@ uint64_t full_init = 0; +void finally( ) { + LOG("Готово! Для выхода из симуляции удерживайте: ESCAPE\n"); + for (;;) { asm volatile("hlt"); } +} + // Точка входа void _start( ) { asm volatile("cli"); @@ -33,17 +38,16 @@ void _start( ) { LOG("\t\t\t\t *** Дата сборки: %s %s ***\n", __DATE__, __TIME__); - task_init( ); - pit_init( ); mod_init( ); + pit_init( ); + task_init( ); - LOG("Готово! Для выхода из симуляции удерживайте: ESCAPE\n"); + task_new_thread(finally); full_init = 1; asm volatile("sti"); - mod_after_init( ); - + // mod_after_init( ); for (;;) { asm volatile("hlt"); } } \ No newline at end of file