Add a proper syscall() function to unistd.h

This commit is contained in:
apio 2022-10-02 17:02:15 +02:00
parent 83d23ce8fe
commit 3c6c94adda
7 changed files with 120 additions and 18 deletions

View File

@ -1,17 +1,32 @@
#ifndef _SYSCALL_H
#define _SYSCALL_H
#ifndef _LUNA_SYSCALL_H
#define _LUNA_SYSCALL_H
#define SYS_exit 0
#define SYS_yield 1
#define SYS_sleep 2
#define SYS_write 3
#define SYS_paint 4
#define SYS_rand 5
#define SYS_version 6
#define SYS_gettid 7
#ifndef __want_syscalls
#ifdef __cplusplus
extern "C"
{
#endif
long int __syscall0(int sys_num);
long int __luna_syscall0(int sys_num);
long int __luna_syscall1(int sys_num, unsigned long int arg0);
long int __luna_syscall2(int sys_num, unsigned long int arg0, unsigned long int arg1);
long int __luna_syscall3(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2);
long int __luna_syscall4(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2,
unsigned long int arg3);
long int __luna_syscall5(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2,
unsigned long int arg3, unsigned long int arg4);
#ifdef __cplusplus
}
#endif
#endif
#endif

View File

@ -0,0 +1,9 @@
#ifndef _SYS_SYSCALL_H
#define _SYS_SYSCALL_H
#define __want_syscalls
#include <luna/syscall.h>
#undef __want_syscalls
#undef _LUNA_SYSCALL_H
#endif

View File

@ -9,6 +9,8 @@ extern "C"
int execve(const char*, char* const[], char* const[]);
int execvp(const char*, char* const[]);
pid_t fork(void);
long syscall(long, ...);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,48 @@
#include <luna/syscall.h>
long int __luna_syscall0(int sys_num)
{
long int result;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num));
return result;
}
long int __luna_syscall1(int sys_num, unsigned long int arg0)
{
long int result;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num), "D"(arg0));
return result;
}
long int __luna_syscall2(int sys_num, unsigned long int arg0, unsigned long int arg1)
{
long int result;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num), "D"(arg0), "S"(arg1));
return result;
}
long int __luna_syscall3(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2)
{
long int result;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num), "D"(arg0), "S"(arg1), "d"(arg2));
return result;
}
long int __luna_syscall4(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2,
unsigned long int arg3)
{
long int result;
register unsigned long int value0 asm("r10") = arg3;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num), "D"(arg0), "S"(arg1), "d"(arg2), "r"(value0));
return result;
}
long int __luna_syscall5(int sys_num, unsigned long int arg0, unsigned long int arg1, unsigned long int arg2,
unsigned long int arg3, unsigned long int arg4)
{
long int result;
register unsigned long int value0 asm("r10") = arg3;
register unsigned long int value1 asm("r10") = arg4;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num), "D"(arg0), "S"(arg1), "d"(arg2), "r"(value0), "r"(value1));
return result;
}

View File

@ -1,12 +0,0 @@
#include <luna/syscall.h>
#include <stdint.h>
extern "C"
{
long int __syscall0(int sys_num)
{
long int result;
asm volatile("int $0x42" : "=a"(result) : "a"(sys_num));
return result;
}
}

View File

@ -1,5 +1,8 @@
#include <luna/syscall.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#define noreturn __attribute__((noreturn))
#define maybe_unused __attribute__((maybe_unused))
#define unused __attribute__((unused))
@ -13,7 +16,7 @@ extern "C"
noreturn void exit(int)
{
__syscall0(SYS_exit);
syscall(SYS_exit);
__builtin_unreachable();
}

View File

@ -1,3 +1,5 @@
#include <luna/syscall.h>
#include <stdarg.h>
#include <unistd.h>
extern "C"
@ -18,4 +20,39 @@ extern "C"
{
return -1;
}
long syscall(long number, ...)
{
typedef unsigned long int arg;
long result;
va_list ap;
va_start(ap, number);
switch (number)
{
case SYS_exit:
case SYS_yield:
case SYS_gettid:
case SYS_rand: result = __luna_syscall0(number); break;
case SYS_sleep: result = __luna_syscall1(number, va_arg(ap, arg)); break;
case SYS_write:
case SYS_version: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
result = __luna_syscall2(number, arg0, arg1);
break;
}
case SYS_paint: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
arg arg3 = va_arg(ap, arg);
arg arg4 = va_arg(ap, arg);
result = __luna_syscall5(number, arg0, arg1, arg2, arg3, arg4);
break;
}
default: result = -1; break;
}
va_end(ap);
return result;
}
}