2022-10-21 19:26:19 +00:00
|
|
|
#define MODULE "sched"
|
|
|
|
|
2022-09-20 17:58:04 +00:00
|
|
|
#include "thread/Task.h"
|
2022-10-21 19:26:19 +00:00
|
|
|
#include "log/Log.h"
|
2022-10-17 15:07:25 +00:00
|
|
|
#include "memory/VMM.h"
|
2022-10-25 16:35:17 +00:00
|
|
|
#include "std/errno.h"
|
2022-09-20 17:58:04 +00:00
|
|
|
#include "std/string.h"
|
|
|
|
|
2022-10-17 15:00:07 +00:00
|
|
|
void Task::restore_context(Context* context)
|
2022-09-20 17:58:04 +00:00
|
|
|
{
|
2022-10-17 15:00:07 +00:00
|
|
|
memcpy(context, ®s, sizeof(Context));
|
2022-09-20 17:58:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:00:07 +00:00
|
|
|
void Task::save_context(Context* context)
|
2022-09-20 17:58:04 +00:00
|
|
|
{
|
2022-10-17 15:00:07 +00:00
|
|
|
memcpy(®s, context, sizeof(Context));
|
2022-10-02 16:53:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:00:07 +00:00
|
|
|
void Task::save_floating()
|
2022-10-02 16:53:54 +00:00
|
|
|
{
|
2022-10-17 15:00:07 +00:00
|
|
|
floating_saved = true;
|
|
|
|
asm volatile("fxsave (%0)" : : "r"((char*)floating_region));
|
2022-10-02 16:53:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 15:00:07 +00:00
|
|
|
void Task::restore_floating()
|
2022-10-02 16:53:54 +00:00
|
|
|
{
|
2022-10-17 15:00:07 +00:00
|
|
|
if (!floating_saved) return;
|
|
|
|
asm volatile("fxrstor (%0)" : : "r"((char*)floating_region));
|
2022-10-02 16:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::is_user_task()
|
|
|
|
{
|
2022-10-12 15:07:39 +00:00
|
|
|
return user_task;
|
2022-10-15 08:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Task::alloc_fd()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
for (fd = 0; fd < TASK_MAX_FDS; fd++)
|
|
|
|
{
|
|
|
|
if (!files[fd].is_open()) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == TASK_MAX_FDS) { return -1; }
|
|
|
|
|
2022-10-17 15:00:07 +00:00
|
|
|
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; }
|
|
|
|
|
2022-10-15 08:45:12 +00:00
|
|
|
return fd;
|
2022-10-17 15:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Task::switch_to_address_space()
|
|
|
|
{
|
|
|
|
VMM::switch_to_user_address_space(address_space);
|
|
|
|
VMM::apply_address_space();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::has_died()
|
|
|
|
{
|
|
|
|
return state == Exited;
|
2022-10-21 19:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Task::resume_read()
|
|
|
|
{
|
|
|
|
VMM::switch_back_to_kernel_address_space();
|
|
|
|
VMM::apply_address_space();
|
|
|
|
VMM::switch_to_previous_user_address_space();
|
|
|
|
regs.rax = files[blocking_read_info.fd].read(blocking_read_info.size, blocking_read_info.buf);
|
|
|
|
VMM::apply_address_space();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Task::is_still_blocking()
|
|
|
|
{
|
|
|
|
return VFS::would_block(files[blocking_read_info.fd].node());
|
2022-10-25 16:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Descriptor* Task::descriptor_from_fd(int fd, int& error)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= TASK_MAX_FDS)
|
|
|
|
{
|
|
|
|
error = EBADF;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!files[fd].is_open())
|
|
|
|
{
|
|
|
|
error = EBADF;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &files[fd];
|
2022-09-20 17:58:04 +00:00
|
|
|
}
|