2023-04-12 17:53:02 +03:00
|
|
|
#include <BAN/Assert.h>
|
2023-05-26 22:21:07 +03:00
|
|
|
#include <kernel/Syscall.h>
|
2023-05-07 01:51:39 +03:00
|
|
|
#include <errno.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2023-04-25 12:38:08 +03:00
|
|
|
#include <stdio.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <sys/syscall.h>
|
2023-04-05 23:58:40 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
void _exit(int status)
|
|
|
|
{
|
|
|
|
syscall(SYS_EXIT, status);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
long syscall(long syscall, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, syscall);
|
|
|
|
|
2023-04-25 12:38:08 +03:00
|
|
|
long ret = -1;
|
2023-04-12 17:53:02 +03:00
|
|
|
|
|
|
|
switch (syscall)
|
|
|
|
{
|
2023-04-22 15:29:15 +03:00
|
|
|
case SYS_EXIT:
|
|
|
|
{
|
|
|
|
int exit_code = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_EXIT, exit_code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_READ:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
void* buffer = va_arg(args, void*);
|
2023-05-09 20:31:22 +03:00
|
|
|
size_t offset = va_arg(args, size_t);
|
2023-04-22 15:29:15 +03:00
|
|
|
size_t bytes = va_arg(args, size_t);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_READ, fd, (uintptr_t)buffer, offset, bytes);
|
2023-04-22 15:29:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_WRITE:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
const char* string = va_arg(args, const char*);
|
2023-05-09 20:31:22 +03:00
|
|
|
size_t offset = va_arg(args, size_t);
|
2023-04-22 15:29:15 +03:00
|
|
|
size_t bytes = va_arg(args, size_t);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_WRITE, fd, (uintptr_t)string, offset, bytes);
|
2023-04-22 15:29:15 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-04-25 12:38:08 +03:00
|
|
|
case SYS_TERMID:
|
|
|
|
{
|
|
|
|
char* buffer = va_arg(args, char*);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_TERMID, (uintptr_t)buffer);
|
2023-04-25 12:38:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_CLOSE:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_CLOSE, fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_OPEN:
|
|
|
|
{
|
|
|
|
const char* path = va_arg(args, const char*);
|
|
|
|
int oflags = va_arg(args, int);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_OPEN, (uintptr_t)path, oflags);
|
2023-04-25 12:38:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-05-06 18:10:38 +03:00
|
|
|
case SYS_ALLOC:
|
|
|
|
{
|
|
|
|
size_t bytes = va_arg(args, size_t);
|
|
|
|
ret = Kernel::syscall(SYS_ALLOC, bytes);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-07 01:21:50 +03:00
|
|
|
case SYS_FREE:
|
|
|
|
{
|
|
|
|
void* ptr = va_arg(args, void*);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_FREE, (uintptr_t)ptr);
|
2023-05-07 01:21:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-05-16 14:14:47 +03:00
|
|
|
case SYS_SEEK:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
off_t offset = va_arg(args, off_t);
|
|
|
|
int whence = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_SEEK, fd, offset, whence);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_TELL:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_TELL, fd);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-16 19:22:10 +03:00
|
|
|
case SYS_GET_TERMIOS:
|
|
|
|
{
|
|
|
|
struct termios* termios = va_arg(args, struct termios*);
|
|
|
|
ret = Kernel::syscall(SYS_GET_TERMIOS, (uintptr_t)termios);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_TERMIOS:
|
|
|
|
{
|
|
|
|
const struct termios* termios = va_arg(args, const struct termios*);
|
|
|
|
ret = Kernel::syscall(SYS_SET_TERMIOS, (uintptr_t)termios);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-28 18:08:26 +03:00
|
|
|
case SYS_FORK:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_FORK);
|
|
|
|
break;
|
|
|
|
}
|
2023-04-25 12:38:08 +03:00
|
|
|
default:
|
|
|
|
puts("LibC: Unhandeled syscall");
|
2023-05-07 01:51:39 +03:00
|
|
|
ret = -ENOSYS;
|
|
|
|
break;
|
2023-04-12 17:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2023-05-07 01:51:39 +03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:58:40 +03:00
|
|
|
pid_t fork(void)
|
|
|
|
{
|
2023-05-28 18:08:26 +03:00
|
|
|
return syscall(SYS_FORK);
|
2023-04-05 23:58:40 +03:00
|
|
|
}
|