diff --git a/kernel/include/thread/Task.h b/kernel/include/thread/Task.h index e6aebf82..e445b298 100644 --- a/kernel/include/thread/Task.h +++ b/kernel/include/thread/Task.h @@ -48,10 +48,12 @@ struct Task AddressSpace address_space; int alloc_fd(); -}; -void set_context_from_task(Task& task, Context* ctx); -void get_context_to_task(Task& task, Context* ctx); + int alloc_fd_greater_than_or_equal(int base_fd); -void task_save_floating(Task& task); -void task_restore_floating(Task& task); \ No newline at end of file + void save_context(Context* context); + void restore_context(Context* context); + + void save_floating(); + void restore_floating(); +}; \ No newline at end of file diff --git a/kernel/src/thread/Task.cpp b/kernel/src/thread/Task.cpp index 69414e7a..c470d216 100644 --- a/kernel/src/thread/Task.cpp +++ b/kernel/src/thread/Task.cpp @@ -1,26 +1,26 @@ #include "thread/Task.h" #include "std/string.h" -void set_context_from_task(Task& task, Context* ctx) +void Task::restore_context(Context* context) { - memcpy(ctx, &task.regs, sizeof(Context)); + memcpy(context, ®s, sizeof(Context)); } -void get_context_to_task(Task& task, Context* ctx) +void Task::save_context(Context* context) { - memcpy(&task.regs, ctx, sizeof(Context)); + memcpy(®s, context, sizeof(Context)); } -void task_save_floating(Task& task) +void Task::save_floating() { - task.floating_saved = true; - asm volatile("fxsave (%0)" : : "r"(&task.floating_region)); + floating_saved = true; + asm volatile("fxsave (%0)" : : "r"((char*)floating_region)); } -void task_restore_floating(Task& task) +void Task::restore_floating() { - if (!task.floating_saved) return; - asm volatile("fxrstor (%0)" : : "r"(&task.floating_region)); + if (!floating_saved) return; + asm volatile("fxrstor (%0)" : : "r"((char*)floating_region)); } bool Task::is_user_task() @@ -38,5 +38,19 @@ int Task::alloc_fd() if (fd == TASK_MAX_FDS) { return -1; } + return fd; +} + +int Task::alloc_fd_greater_than_or_equal(int base_fd) +{ + int fd; + if (base_fd >= TASK_MAX_FDS) return -1; + for (fd = base_fd; fd < TASK_MAX_FDS; fd++) + { + if (!files[fd].is_open()) break; + } + + if (fd == TASK_MAX_FDS) { return -1; } + return fd; } \ No newline at end of file