Compare commits
No commits in common. "5c61252061e53a9b3772dc3bdfdd4f434d8ceff4" and "c77e752a821fa49d383543f6ce92515ff1dc14cc" have entirely different histories.
5c61252061
...
c77e752a82
@ -16,7 +16,6 @@
|
||||
#define SYS_close 11
|
||||
#define SYS_seek 12
|
||||
#define SYS_exec 13
|
||||
#define SYS_fcntl 14
|
||||
|
||||
namespace Syscall
|
||||
{
|
||||
@ -39,4 +38,3 @@ void sys_read(Context* context, int fd, size_t size, char* buffer);
|
||||
void sys_close(Context* context, int fd);
|
||||
void sys_seek(Context* context, int fd, long offset, int whence);
|
||||
void sys_exec(Context* context, const char* pathname);
|
||||
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg);
|
@ -46,8 +46,6 @@ struct Task
|
||||
Descriptor files[TASK_MAX_FDS];
|
||||
|
||||
AddressSpace address_space;
|
||||
|
||||
int alloc_fd();
|
||||
};
|
||||
|
||||
void set_context_from_task(Task& task, Context* ctx);
|
||||
|
@ -25,13 +25,12 @@ void Syscall::entry(Context* context)
|
||||
case SYS_close: sys_close(context, (int)context->rdi); break;
|
||||
case SYS_seek: sys_seek(context, (int)context->rdi, (long)context->rsi, (int)context->rdx); break;
|
||||
case SYS_exec: sys_exec(context, (const char*)context->rdi); break;
|
||||
case SYS_fcntl: sys_fcntl(context, (int)context->rdi, (int)context->rsi, context->rdx); break;
|
||||
default: context->rax = -ENOSYS; break;
|
||||
}
|
||||
VMM::exit_syscall_context();
|
||||
}
|
||||
|
||||
char* Syscall::strdup_from_user(const char* user_string) // FIXME: This function is a little hacky.
|
||||
char* Syscall::strdup_from_user(const char* user_string)
|
||||
{
|
||||
uint64_t phys = VMM::get_physical((uint64_t)user_string);
|
||||
if (phys == (uint64_t)-1) { return nullptr; }
|
||||
|
@ -18,42 +18,6 @@
|
||||
#define SEEK_CUR 1
|
||||
#define SEEK_END 2
|
||||
|
||||
#define FNCTL_DUPFD 0
|
||||
|
||||
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg)
|
||||
{
|
||||
if (fd >= TASK_MAX_FDS || fd < 0)
|
||||
{
|
||||
context->rax = -EBADF;
|
||||
return;
|
||||
}
|
||||
Task* current_task = Scheduler::current_task();
|
||||
if (!current_task->files[fd].is_open())
|
||||
{
|
||||
context->rax = -EBADF;
|
||||
return;
|
||||
}
|
||||
Descriptor& file = current_task->files[fd];
|
||||
if (command == FNCTL_DUPFD)
|
||||
{
|
||||
int dupfd = current_task->alloc_fd();
|
||||
if (dupfd < 0)
|
||||
{
|
||||
context->rax = -EMFILE;
|
||||
return;
|
||||
}
|
||||
current_task->files[dupfd] = file;
|
||||
context->rax = dupfd;
|
||||
kdbgln("fcntl(F_DUPFD): duplicated fd %d, result is %d", fd, dupfd);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
context->rax = -EINVAL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void sys_seek(Context* context, int fd, long offset, int whence)
|
||||
{
|
||||
if (whence < SEEK_SET || whence > SEEK_END)
|
||||
@ -72,13 +36,12 @@ void sys_seek(Context* context, int fd, long offset, int whence)
|
||||
context->rax = -EBADF;
|
||||
return;
|
||||
}
|
||||
Descriptor& file = current_task->files[fd];
|
||||
long new_offset;
|
||||
if (whence == SEEK_SET) new_offset = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
new_offset = offset + file.offset();
|
||||
new_offset = offset + current_task->files[fd].offset();
|
||||
else if (whence == SEEK_END)
|
||||
new_offset = file.length() + offset;
|
||||
new_offset = current_task->files[fd].length() + offset;
|
||||
else
|
||||
__builtin_unreachable();
|
||||
if (new_offset < 0)
|
||||
@ -86,12 +49,7 @@ void sys_seek(Context* context, int fd, long offset, int whence)
|
||||
context->rax = -EINVAL; // FIXME: Is this the right error?
|
||||
return;
|
||||
}
|
||||
if (new_offset == file.offset())
|
||||
{
|
||||
context->rax = new_offset;
|
||||
return;
|
||||
}
|
||||
int result = file.seek(new_offset);
|
||||
int result = current_task->files[fd].seek(new_offset);
|
||||
if (result < 0)
|
||||
{
|
||||
context->rax = result;
|
||||
@ -119,13 +77,12 @@ void sys_write(Context* context, int fd, size_t size, const char* addr)
|
||||
context->rax = -EBADF;
|
||||
return;
|
||||
}
|
||||
Descriptor& file = current_task->files[fd];
|
||||
if (!file.can_write())
|
||||
if (!current_task->files[fd].can_write())
|
||||
{
|
||||
context->rax = -EBADF;
|
||||
return;
|
||||
}
|
||||
ssize_t result = file.write(size, (const char*)VMM::get_physical((uint64_t)addr));
|
||||
ssize_t result = current_task->files[fd].write(size, (const char*)VMM::get_physical((uint64_t)addr));
|
||||
context->rax = (size_t)result;
|
||||
return;
|
||||
}
|
||||
@ -133,8 +90,13 @@ void sys_write(Context* context, int fd, size_t size, const char* addr)
|
||||
void sys_open(Context* context, const char* filename, int flags)
|
||||
{
|
||||
Task* current_task = Scheduler::current_task();
|
||||
int fd = current_task->alloc_fd();
|
||||
if (fd < 0)
|
||||
int fd;
|
||||
for (fd = 0; fd < TASK_MAX_FDS; fd++)
|
||||
{
|
||||
if (!current_task->files[fd].is_open()) break;
|
||||
}
|
||||
|
||||
if (fd == TASK_MAX_FDS)
|
||||
{
|
||||
context->rax = -EMFILE;
|
||||
return;
|
||||
|
@ -27,16 +27,3 @@ bool Task::is_user_task()
|
||||
{
|
||||
return user_task;
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
return fd;
|
||||
}
|
Loading…
Reference in New Issue
Block a user