Skip to content

Commit

Permalink
Improve error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Twometer committed Feb 4, 2021
1 parent a621971 commit ae33500
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion kernel/include/kernel/syscallhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Kernel
{
typedef uint32_t (*syscall_t)(void *);
typedef int (*syscall_t)(void *);

class SyscallHandler : public InterruptHandler
{
Expand Down
36 changes: 18 additions & 18 deletions kernel/include/kernel/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ extern "C"
}
#endif

uint32_t sys$$texit(void *param);
uint32_t sys$$print(void *param);
uint32_t sys$$exit(void *param);
uint32_t sys$$putchar(void *param);
uint32_t sys$$fopen(void *param);
uint32_t sys$$fread(void *param);
uint32_t sys$$fwrite(void *param);
uint32_t sys$$fclose(void *param);
uint32_t sys$$pagealloc(void *param);
uint32_t sys$$sleep(void *param);
uint32_t sys$$spawnp(void *param);
uint32_t sys$$waitp(void *param);
uint32_t sys$$readln(void *param);
uint32_t sys$$fb_acquire(void *param);
uint32_t sys$$fb_flush(void *param);
uint32_t sys$$fb_release(void *param);
uint32_t sys$$chdir(void *param);
uint32_t sys$$getcwd(void *param);
int sys$$texit(void *param);
int sys$$print(void *param);
int sys$$exit(void *param);
int sys$$putchar(void *param);
int sys$$fopen(void *param);
int sys$$fread(void *param);
int sys$$fwrite(void *param);
int sys$$fclose(void *param);
int sys$$pagealloc(void *param);
int sys$$sleep(void *param);
int sys$$spawnp(void *param);
int sys$$waitp(void *param);
int sys$$readln(void *param);
int sys$$fb_acquire(void *param);
int sys$$fb_flush(void *param);
int sys$$fb_release(void *param);
int sys$$chdir(void *param);
int sys$$getcwd(void *param);

#endif
3 changes: 2 additions & 1 deletion kernel/syscallhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <kernel/tasks/thread.h>
#include <kernel/kdebug.h>
#include <kernel/video/tty.h>
#include <errno.h>

using namespace Memory;

Expand Down Expand Up @@ -60,7 +61,7 @@ namespace Kernel
auto syscallHandler = syscalls->At(syscall_num);
if (syscallHandler == nullptr)
{
*retval = -1;
*retval = -ENOSYS;
kdbg("warn: Thread %d attempted invalid syscall %x\n", Thread::Current()->GetId(), syscall_num);
return;
}
Expand Down
38 changes: 19 additions & 19 deletions kernel/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ nk::String resolve_file(const nk::Path path)
return file;
}

uint32_t sys$$texit(void *param)
int sys$$texit(void *param)
{
uint32_t exit_code = *(uint32_t *)(param);
kdbg("User thread %d exited with exit code %d\n", Thread::Current()->GetId(), exit_code);
Thread::Current()->Kill();
return 0;
}

uint32_t sys$$print(void *param)
int sys$$print(void *param)
{
printf("%s", param);
return 0;
}

uint32_t sys$$exit(void *param)
int sys$$exit(void *param)
{
uint32_t exit_code = *(uint32_t *)(param);
auto process = Process::Current();
if (process == nullptr)
return 1;
return -ENOSYS;

kdbg("User process %d exited with exit code %d\n", process->GetId(), exit_code);
process->Kill();
return 0;
}

uint32_t sys$$putchar(void *param)
int sys$$putchar(void *param)
{
char c = *(char *)param;
VideoManager::GetInstance()->GetTTY()->Write(&c, sizeof(c));
return 0;
}

uint32_t sys$$fopen(void *param)
int sys$$fopen(void *param)
{
auto params = (sys$$fopen_param *)param;

Expand All @@ -74,27 +74,27 @@ uint32_t sys$$fopen(void *param)
return 0;
}

uint32_t sys$$fread(void *param)
int sys$$fread(void *param)
{
auto params = (sys$$fread_param *)param;
FS::VirtualFileSystem::GetInstance()->Read(params->fd, params->offset, params->length, (uint8_t *)params->dst);
return 0;
}

uint32_t sys$$fwrite(void *param)
int sys$$fwrite(void *param)
{
auto params = (sys$$fwrite_param *)param;
return 0;
}

uint32_t sys$$fclose(void *param)
int sys$$fclose(void *param)
{
auto params = (sys$$fclose_param *)param;
FS::VirtualFileSystem::GetInstance()->Close(params->fd);
return 0;
}

uint32_t sys$$pagealloc(void *param)
int sys$$pagealloc(void *param)
{
int num = *(int *)param;
auto ptr = (uint32_t)Process::Current()->MapNewPages(num);
Expand All @@ -104,15 +104,15 @@ uint32_t sys$$pagealloc(void *param)
return ptr;
}

uint32_t sys$$sleep(void *param)
int sys$$sleep(void *param)
{
uint32_t timeout = *(uint32_t *)(param);
auto thread = Thread::Current();
thread->Sleep(timeout * 1000);
return 0;
}

uint32_t sys$$spawnp(void *param)
int sys$$spawnp(void *param)
{
auto params = (sys$$spawnp_param *)param;
auto resolved = resolve_file(params->path);
Expand All @@ -128,7 +128,7 @@ uint32_t sys$$spawnp(void *param)
return 0;
}

uint32_t sys$$waitp(void *param)
int sys$$waitp(void *param)
{
pid_t pid = PARAM_VALUE(param, pid_t);
auto proc = ProcessDir::GetInstance()->GetProcess(pid);
Expand All @@ -138,14 +138,14 @@ uint32_t sys$$waitp(void *param)
return 0;
}

uint32_t sys$$readln(void *param)
int sys$$readln(void *param)
{
auto params = (sys$$readln_param *)param;
Thread::Current()->Block(new KeyboardBlocker('\n'));
return Device::DeviceManager::keyboard->ReadUntil(params->dst, params->maxSize, '\n');
}

uint32_t sys$$fb_acquire(void *param)
int sys$$fb_acquire(void *param)
{
auto *buf = (FRAMEBUF *)param;
auto vm = VideoManager::GetInstance();
Expand All @@ -164,7 +164,7 @@ uint32_t sys$$fb_acquire(void *param)
return 0;
}

uint32_t sys$$fb_flush(void *param)
int sys$$fb_flush(void *param)
{
auto params = (sys$$fb_flush_param *)param;

Expand All @@ -180,7 +180,7 @@ uint32_t sys$$fb_flush(void *param)
return 0;
}

uint32_t sys$$fb_release(void *)
int sys$$fb_release(void *)
{
auto vm = VideoManager::GetInstance();
if (vm->GetFramebufferController() != Process::Current()->GetId())
Expand All @@ -192,7 +192,7 @@ uint32_t sys$$fb_release(void *)
return 0;
}

uint32_t sys$$chdir(void *param)
int sys$$chdir(void *param)
{
char *params = (char *)param;
auto newCwd = nk::Path::Resolve(Process::Current()->GetCwd(), params);
Expand All @@ -206,7 +206,7 @@ uint32_t sys$$chdir(void *param)
return 0;
}

uint32_t sys$$getcwd(void *param)
int sys$$getcwd(void *param)
{
sys$$getcwd_param *params = (sys$$getcwd_param *)param;

Expand Down
3 changes: 2 additions & 1 deletion libc/stdio/file.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/syscall.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

FILE *fopen(const char *path, const char *mode)
{
Expand Down Expand Up @@ -37,7 +38,7 @@ size_t fread(void *ptr, size_t size, size_t count, FILE *stream)
int fseek(FILE *stream, long offset, int origin)
{
if (offset < 0 || offset >= stream->fsize)
return 1;
return -ERANGE;

if (origin == SEEK_SET)
stream->offset = offset;
Expand Down

0 comments on commit ae33500

Please sign in to comment.