From cb3d25f49066aff4e14394fdb45234d6bb44ab62 Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Tue, 1 Apr 2025 16:28:27 +0300 Subject: [PATCH] tasking: make tasks run with arguments --- include/task.h | 3 ++- src/task.c | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/task.h b/include/task.h index e450c9e..5c5cf90 100644 --- a/include/task.h +++ b/include/task.h @@ -24,7 +24,8 @@ typedef struct Process struct Process* next; // Pointer to the next task in the list } Process; -Process* task_create(EntryPoint func); +//Process* task_create(EntryPoint func); +Process* task_create(EntryPoint func, void** args, uint32_t arg_count); void scheduler_init(); void schedule(); void scheduler_lock(); diff --git a/src/task.c b/src/task.c index 994455d..b577a3e 100644 --- a/src/task.c +++ b/src/task.c @@ -2,6 +2,8 @@ #include "../include/stdio.h" #include "../include/paging.h" +#include + Process* current = 0; Process* queue = 0; @@ -19,7 +21,7 @@ static void processStartup() scheduler_unlock(); } -Process* task_create(EntryPoint func) +Process* task_create(void(*func)(), void** args, uint32_t arg_count) { Process* p = heap_alloc(sizeof(Process)); p->pid = ++pid_counter; @@ -28,12 +30,19 @@ Process* task_create(EntryPoint func) p->stackBase = heap_alloc(STACK_SIZE); p->stackPointer = (uintptr_t*)((uint32_t)p->stackBase + STACK_SIZE); - *(--p->stackPointer) = (uintptr_t)func;//TODO: check this shit + //put the arguments in stack + for(int i = arg_count-1; i >=0; i--) + *(--p->stackPointer) = (uintptr_t)args[i]; + + // Добавляем адрес возврата (фиктивный, для выравнивания) + *(--p->stackPointer) = 0; // Фиктивный адрес возврата (не используется напрямую) + + *(--p->stackPointer) = (uintptr_t)func; *(--p->stackPointer) = (uintptr_t)processStartup; - *(--p->stackPointer) = 0; - *(--p->stackPointer) = 0; - *(--p->stackPointer) = 0; - *(--p->stackPointer) = 0; + *(--p->stackPointer) = 0;//ebp + *(--p->stackPointer) = 0;//edi + *(--p->stackPointer) = 0;//esi + *(--p->stackPointer) = 0;//ebx p->next = 0; @@ -83,19 +92,27 @@ void idle() while(1){} } -void task1() +void task1(int arg1, char* arg2) { - while(1){printf("task1\n");} + while(1){ + if(arg1 == 42) + printf("task1 0x%x %s\n", arg1, arg2); + else + printf("ZHOPA %s\n", arg2); + } } -void task2() +void task2(int arg1, int arg2) { - while(1){printf("task2\n");} + while(1){printf("task2 %X %X\n", arg1, arg2);} } void scheduler_init() { - task_create(&idle); - task_create(&task1); - task_create(&task2); + task_create(&idle, NULL, 0); + + void* args1[] = {(void*)42, (void*)"ebalo"}; + task_create((void (*)())&task1, args1, 2); + void* args2[] = {(void*)69, (void*)420}; + task_create((void (*)())&task2, args2, 2); }