From b8752ee39d14ac075705be86af29a133176c6257 Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Tue, 20 May 2025 00:10:37 +0300 Subject: [PATCH] gonna ask tsoding for help, fuck this shit --- include/task.h | 32 +++++++++++++------------------- src/kernel/isr.c | 4 ++-- src/tasking/switch.asm | 34 +++++++++++++++------------------- src/tasking/task.c | 17 +++++++++-------- 4 files changed, 39 insertions(+), 48 deletions(-) diff --git a/include/task.h b/include/task.h index fdd108e..ff277cb 100644 --- a/include/task.h +++ b/include/task.h @@ -35,29 +35,23 @@ typedef struct __attribute__((packed)) Context { // Структура trap frame для перехода в ring 3 typedef struct __attribute__((packed)) TrapFrame { - uint32_t edi; - uint32_t esi; - uint32_t ebp; - uint32_t ebx; // Убрано oesp - uint32_t edx; - uint32_t ecx; - uint32_t eax; - uint16_t gs; - uint16_t fs; - uint16_t es; - uint16_t ds; - uint32_t trapno; - uint32_t err; - uint32_t eip; - uint32_t cs; - uint32_t eflags; - uint32_t esp; - uint32_t ss; + uint32_t gs,fs,es,ds; + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; + + uint32_t interrupt, error; + + uint32_t eip, cs, eflags, usermode_esp, usermode_ss; + } TrapFrame; typedef struct Process { uint32_t pid; // Process ID + + uint32_t kesp; + uint32_t kesp_bottom; + uint32_t *pagedir; + TaskState state; // Состояние процесса char* kstack; // Стек ядра Context* context; // Контекст для переключения @@ -67,7 +61,7 @@ typedef struct Process uint32_t* page_directory; // Директория страниц } Process; -Process* task_create(EntryPoint func, void** args, uint32_t arg_count, uint32_t ring); +Process* task_create(uint32_t func, void** args, uint32_t arg_count, uint32_t ring); void scheduler_init(); void schedule(); void scheduler_lock(); diff --git a/src/kernel/isr.c b/src/kernel/isr.c index 918473e..2da5d8a 100644 --- a/src/kernel/isr.c +++ b/src/kernel/isr.c @@ -106,9 +106,9 @@ __attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame) pit_init(100); pic_end_int(0x0); - scheduler_lock(); + //scheduler_lock(); schedule(); - scheduler_unlock(); + //scheduler_unlock(); } __attribute__((interrupt)) void isr_custom(struct interrupt_frame *frame) diff --git a/src/tasking/switch.asm b/src/tasking/switch.asm index 9a2b7be..acdfa9c 100644 --- a/src/tasking/switch.asm +++ b/src/tasking/switch.asm @@ -16,39 +16,33 @@ extern current extern trapret switchProcess: - ; Сохраняем регистры текущего процесса + mov eax, [esp + 4] ; eax = old + mov edx, [esp + 8] ; edx = new + + push ebp push ebx push esi push edi - push ebp - ; Сохраняем указатель стека в current->context - mov edi, [current] - mov [edi + 12], esp ; PCB.CONTEXT находится по смещению 12 + mov [eax+4], esp + mov esp, [edx+4] - ; Загружаем следующий процесс - mov esi, [esp + 20] ; next (первый аргумент) - mov [current], esi - mov esp, [esi + 12] ; Загружаем esp из next->context + ;mov eax, [edx+12] + ;sub eax, 0xC0000000 + ;mov cr3, eax - ; Восстанавливаем регистры - pop ebp pop edi pop esi pop ebx + pop ebp + - ; Проверяем ring процесса - mov eax, [esi + 20] ; PCB.RING находится по смещению 20 - cmp eax, 3 - je .ring3 - sti ret ; Для ring 0 просто возвращаемся -.ring3: - jmp trapret ; Для ring 3 переходим через trapret + trapret: ; Восстанавливаем общие регистры из trap frame - popal ; edi, esi, ebp, oesp, ebx, edx, ecx, eax + ;popal ; edi, esi, ebp, oesp, ebx, edx, ecx, eax ; Восстанавливаем сегментные регистры pop gs @@ -56,6 +50,8 @@ trapret: pop es pop ds + popad + ; Пропускаем trapno и err add esp, 8 ; trapno (4 байта) + err (4 байта) diff --git a/src/tasking/task.c b/src/tasking/task.c index d9b792f..a109b90 100644 --- a/src/tasking/task.c +++ b/src/tasking/task.c @@ -30,7 +30,7 @@ static void processStartup() // Возврат из этой функции приведёт к вызову trapret } -Process* task_create(EntryPoint func, void** args, uint32_t arg_count, uint32_t ring) +Process* task_create(uint32_t func, void** args, uint32_t arg_count, uint32_t ring) { Process* p = alloc_page();//heap_alloc(sizeof(Process)); p->pid = ++pid_counter; @@ -53,10 +53,10 @@ Process* task_create(EntryPoint func, void** args, uint32_t arg_count, uint32_t p->tf->es = p->tf->ds; p->tf->fs = p->tf->ds; p->tf->gs = p->tf->ds; - p->tf->ss = p->tf->ds; + p->tf->usermode_ss = p->tf->ds; p->tf->eflags = FL_IF; p->tf->eip = (uint32_t)func; - p->tf->esp = 0x800000; // Предполагаем, что пользовательский стек настроен + p->tf->usermode_esp = 0x800000; // Предполагаем, что пользовательский стек настроен sp -= sizeof(Context); p->context = (Context*)sp; @@ -111,6 +111,7 @@ void schedule() if (next != current) { //loadPageDirectory(next->page_directory); + printf("switching to %X\n", next->pid); switchProcess(next); } } @@ -137,11 +138,11 @@ void jump_usermode2(void) ; void scheduler_init() { - task_create((EntryPoint)&idle, NULL, 0, 0); - //task_create((EntryPoint)&idle, NULL, 0, 0); + task_create((uint32_t)idle, NULL, 0, 0); + task_create((uint32_t)idle, NULL, 0, 0); void* args1[] = {(void*)42, (void*)"ebalo"}; - task_create((EntryPoint)&task1, args1, 2, 0); + //task_create((EntryPoint)task1, args1, 2, 0); void* args2[] = {(void*)69, (void*)420}; //task_create((EntryPoint)&task2, args2, 2, 0); //jump_usermode2(); @@ -209,8 +210,8 @@ void jump_usermode2(void) { (code_pte & 0x2) ? "writable" : "read-only", (code_pte & 0x4) ? "user" : "supervisor"); - task_create(user_code_virt, NULL, 0, 3); - task_create(second_user_code_virt, NULL, 0, 3); + task_create((uint32_t)user_code_virt, NULL, 0, 3); + //task_create(second_user_code_virt, NULL, 0, 3); return; // Switch to user mode with interrupts enabled