Underscore syscall stuff

This commit is contained in:
flysand7 2023-07-30 19:47:59 +11:00
parent 9ac97f1e9d
commit 9f44678472
3 changed files with 149 additions and 149 deletions

View File

@ -6,8 +6,8 @@ static char stack_chk_fail_msg[] =
"Sorry these guys didn't tell me where\n"; "Sorry these guys didn't tell me where\n";
void __stack_chk_fail(void) { void __stack_chk_fail(void) {
syscall_write(STDERR_FILENO, stack_chk_fail_msg, sizeof stack_chk_fail_msg); _syscall_write(_SYS_STDERR_FILENO, stack_chk_fail_msg, sizeof stack_chk_fail_msg);
syscall_exit(1); _syscall_exit(1);
} }
static void _fileapi_init(); static void _fileapi_init();
@ -28,5 +28,5 @@ void __libc_start_main(
fini(); fini();
// glibc bug // glibc bug
// dl_fini(); // dl_fini();
syscall_exit(0); _syscall_exit(0);
} }

View File

@ -1,199 +1,199 @@
// Standard handles file descriptors // Standard handles file descriptors
#define STDIN_FILENO 0 #define _SYS_STDIN_FILENO 0
#define STDOUT_FILENO 1 #define _SYS_STDOUT_FILENO 1
#define STDERR_FILENO 2 #define _SYS_STDERR_FILENO 2
// arch_prctl() syscall codes // arch_prctl() syscall codes
#define ARCH_SET_GS 0x1001 #define _SYS_ARCH_SET_GS 0x1001
#define ARCH_SET_FS 0x1002 #define _SYS_ARCH_SET_FS 0x1002
#define ARCH_GET_FS 0x1003 #define _SYS_ARCH_GET_FS 0x1003
#define ARCH_GET_GS 0x1004 #define _SYS_ARCH_GET_GS 0x1004
#define ARCH_GET_CPUID 0x1011 #define _SYS_ARCH_GET_CPUID 0x1011
#define ARCH_SET_CPUID 0x1012 #define _SYS_ARCH_SET_CPUID 0x1012
// open() syscall modes // open() syscall modes
#define O_ACCMODE 0003 #define _SYS_O_ACCMODE 0003
#define O_RDONLY 00 #define _SYS_O_RDONLY 00
#define O_WRONLY 01 #define _SYS_O_WRONLY 01
#define O_RDWR 02 #define _SYS_O_RDWR 02
#define O_CREAT 0100 /* not fcntl */ #define _SYS_O_CREAT 0100 /* not fcntl */
#define O_EXCL 0200 /* not fcntl */ #define _SYS_O_EXCL 0200 /* not fcntl */
#define O_NOCTTY 0400 /* not fcntl */ #define _SYS_O_NOCTTY 0400 /* not fcntl */
#define O_TRUNC 01000 /* not fcntl */ #define _SYS_O_TRUNC 01000 /* not fcntl */
#define O_APPEND 02000 #define _SYS_O_APPEND 02000
#define O_NONBLOCK 04000 #define _SYS_O_NONBLOCK 04000
#define O_NDELAY O_NONBLOCK #define _SYS_O_NDELAY O_NONBLOCK
#define O_SYNC 010000 #define _SYS_O_SYNC 010000
#define O_FSYNC O_SYNC #define _SYS_O_FSYNC O_SYNC
#define O_ASYNC 020000 #define _SYS_O_ASYNC 020000
// mmap() protection modes, flags and constants // mmap() protection modes, flags and constants
#define PROT_READ 0x1 #define _SYS_PROT_READ 0x1
#define PROT_WRITE 0x2 #define _SYS_PROT_WRITE 0x2
#define PROT_EXEC 0x4 #define _SYS_PROT_EXEC 0x4
#define PROT_NONE 0x0 #define _SYS_PROT_NONE 0x0
#define PROT_GROWSDOWN 0x01000000 #define _SYS_PROT_GROWSDOWN 0x01000000
#define PROT_GROWSUP 0x02000000 #define _SYS_PROT_GROWSUP 0x02000000
#define MAP_FILE 0 #define _SYS_MAP_FILE 0
#define MAP_ANONYMOUS 0x20 #define _SYS_MAP_ANONYMOUS 0x20
#define MAP_32BIT 0x40 #define _SYS_MAP_32BIT 0x40
#define MAP_FAILED (void *)() #define _SYS_MAP_FAILED (void *)()
#define SYS_read 0 #define _SYSCALL_read 0
#define SYS_write 1 #define _SYSCALL_write 1
#define SYS_open 2 #define _SYSCALL_open 2
#define SYS_close 3 #define _SYSCALL_close 3
#define SYS_stat 4 #define _SYSCALL_stat 4
#define SYS_fstat 5 #define _SYSCALL_fstat 5
#define SYS_lstat 6 #define _SYSCALL_lstat 6
#define SYS_poll 7 #define _SYSCALL_poll 7
#define SYS_lseek 8 #define _SYSCALL_lseek 8
#define SYS_mmap 9 #define _SYSCALL_mmap 9
#define SYS_mprotect 10 #define _SYSCALL_mprotect 10
#define SYS_munmap 11 #define _SYSCALL_munmap 11
#define SYS_brk 12 #define _SYSCALL_brk 12
#define SYS_rt_sigaction 13 #define _SYSCALL_rt_sigaction 13
#define SYS_rt_sigprocmask 14 #define _SYSCALL_rt_sigprocmask 14
#define SYS_rt_sigreturn 15 #define _SYSCALL_rt_sigreturn 15
#define SYS_ioctl 16 #define _SYSCALL_ioctl 16
#define SYS_pread64 17 #define _SYSCALL_pread64 17
#define SYS_pwrite64 18 #define _SYSCALL_pwrite64 18
#define SYS_readv 19 #define _SYSCALL_readv 19
#define SYS_writev 20 #define _SYSCALL_writev 20
#define SYS_access 21 #define _SYSCALL_access 21
#define SYS_pipe 22 #define _SYSCALL_pipe 22
#define SYS_select 23 #define _SYSCALL_select 23
#define SYS_sched_yield 24 #define _SYSCALL_sched_yield 24
#define SYS_mremap 25 #define _SYSCALL_mremap 25
#define SYS_msync 26 #define _SYSCALL_msync 26
#define SYS_mincore 27 #define _SYSCALL_mincore 27
#define SYS_madvise 28 #define _SYSCALL_madvise 28
#define SYS_shmget 29 #define _SYSCALL_shmget 29
#define SYS_shmat 30 #define _SYSCALL_shmat 30
#define SYS_shmctl 31 #define _SYSCALL_shmctl 31
#define SYS_dup 32 #define _SYSCALL_dup 32
#define SYS_dup2 33 #define _SYSCALL_dup2 33
#define SYS_pause 34 #define _SYSCALL_pause 34
#define SYS_nanosleep 35 #define _SYSCALL_nanosleep 35
#define SYS_getitimer 36 #define _SYSCALL_getitimer 36
#define SYS_alarm 37 #define _SYSCALL_alarm 37
#define SYS_setitimer 38 #define _SYSCALL_setitimer 38
#define SYS_getpid 39 #define _SYSCALL_getpid 39
#define SYS_sendfile 40 #define _SYSCALL_sendfile 40
#define SYS_socket 41 #define _SYSCALL_socket 41
#define SYS_connect 42 #define _SYSCALL_connect 42
#define SYS_accept 43 #define _SYSCALL_accept 43
#define SYS_sendto 44 #define _SYSCALL_sendto 44
#define SYS_recvfrom 45 #define _SYSCALL_recvfrom 45
#define SYS_sendmsg 46 #define _SYSCALL_sendmsg 46
#define SYS_recvmsg 47 #define _SYSCALL_recvmsg 47
#define SYS_shutdown 48 #define _SYSCALL_shutdown 48
#define SYS_bind 49 #define _SYSCALL_bind 49
#define SYS_listen 50 #define _SYSCALL_listen 50
#define SYS_getsockname 51 #define _SYSCALL_getsockname 51
#define SYS_getpeername 52 #define _SYSCALL_getpeername 52
#define SYS_socketpair 53 #define _SYSCALL_socketpair 53
#define SYS_setsockopt 54 #define _SYSCALL_setsockopt 54
#define SYS_getsockopt 55 #define _SYSCALL_getsockopt 55
#define SYS_clone 56 #define _SYSCALL_clone 56
#define SYS_fork 57 #define _SYSCALL_fork 57
#define SYS_vfork 58 #define _SYSCALL_vfork 58
#define SYS_execve 59 #define _SYSCALL_execve 59
#define SYS_exit 60 #define _SYSCALL_exit 60
#define SYS_arch_prctl 158 #define _SYSCALL_arch_prctl 158
// Syscall stubs // Syscall stubs
static __inline i64 __syscall0(i64 n) { static inline i64 _syscall0(i64 n) {
i64 ret; i64 ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n) : "rcx", "r11", "memory"); asm volatile("syscall" : "=a"(ret) : "a"(n) : "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall1(i64 n, i64 a1) { static inline i64 _syscall1(i64 n, i64 a1) {
i64 ret; i64 ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory"); asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall2(i64 n, i64 a1, i64 a2) { static inline i64 _syscall2(i64 n, i64 a1, i64 a2) {
i64 ret; i64 ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2) asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2)
: "rcx", "r11", "memory"); : "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall3(i64 n, i64 a1, i64 a2, i64 a3) { static inline i64 _syscall3(i64 n, i64 a1, i64 a2, i64 a3) {
i64 ret; i64 ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3) : "rcx", "r11", "memory"); "d"(a3) : "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall4(i64 n, i64 a1, i64 a2, i64 a3, i64 a4) { static inline i64 _syscall4(i64 n, i64 a1, i64 a2, i64 a3, i64 a4) {
i64 ret; i64 ret;
register i64 r10 __asm__("r10") = a4; register i64 r10 asm("r10") = a4;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10): "rcx", "r11", "memory"); "d"(a3), "r"(r10): "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall5(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) { static inline i64 _syscall5(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) {
i64 ret; i64 ret;
register i64 r10 __asm__("r10") = a4; register i64 r10 asm("r10") = a4;
register i64 r8 __asm__("r8") = a5; register i64 r8 asm("r8") = a5;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory"); "d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory");
return ret; return ret;
} }
static __inline i64 __syscall6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6) { static inline i64 _syscall6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6) {
i64 ret; i64 ret;
register i64 r10 __asm__("r10") = a4; register i64 r10 asm("r10") = a4;
register i64 r8 __asm__("r8") = a5; register i64 r8 asm("r8") = a5;
register i64 r9 __asm__("r9") = a6; register i64 r9 asm("r9") = a6;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), asm volatile("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory"); "d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory");
return ret; return ret;
} }
// Syscall wrappers // Syscall wrappers
static inline i64 syscall_read(u32 fd, char *buf, u64 count) { static inline i64 _syscall_read(u32 fd, char *buf, u64 count) {
return __syscall3(SYS_read, (i64)fd, (i64)buf, (i64)count); return _syscall3(_SYSCALL_read, (i64)fd, (i64)buf, (i64)count);
} }
static inline i64 syscall_write(u32 fd, char const *buf, u64 count) { static inline i64 _syscall_write(u32 fd, char const *buf, u64 count) {
return __syscall3(SYS_write, (i64)fd, (i64)buf, (u64)count); return _syscall3(_SYSCALL_write, (i64)fd, (i64)buf, (u64)count);
} }
static inline i64 syscall_open(char const *filename, int flags, int mode) { static inline i64 _syscall_open(char const *filename, int flags, int mode) {
return __syscall3(SYS_open, (i64)filename, (i64)flags, (i64)mode); return _syscall3(_SYSCALL_open, (i64)filename, (i64)flags, (i64)mode);
} }
static inline i64 syscall_close(u32 fd) { static inline i64 _syscall_close(u32 fd) {
return __syscall1(SYS_close, fd); return _syscall1(_SYSCALL_close, fd);
} }
static inline void *syscall_mmap(u64 addr, u64 len, u64 prot, u64 flags, u64 fd, u64 offset) { static inline void *_syscall_mmap(u64 addr, u64 len, u64 prot, u64 flags, u64 fd, u64 offset) {
return (void *)__syscall6(SYS_mmap, addr, len, prot, flags, fd, offset); return (void *)_syscall6(_SYSCALL_mmap, addr, len, prot, flags, fd, offset);
} }
static inline i64 syscall_munmap(void *addr, u64 len) { static inline i64 _syscall_munmap(void *addr, u64 len) {
return __syscall2(SYS_munmap, (u64)addr, len); return _syscall2(_SYSCALL_munmap, (u64)addr, len);
} }
_Noreturn static inline void syscall_exit(int code) { _Noreturn static inline void _syscall_exit(int code) {
__syscall1(SYS_exit, (i64)code); _syscall1(_SYSCALL_exit, (i64)code);
__builtin_unreachable(); __builtin_unreachable();
} }
static inline i64 syscall_arch_prctl_set(int code, u64 value) { static inline i64 _syscall_arch_prctl_set(int code, u64 value) {
return __syscall2(SYS_arch_prctl, code, (i64)value); return _syscall2(_SYSCALL_arch_prctl, code, (i64)value);
} }
static inline i64 syscall_arch_prctl_get(int code, u64 *value) { static inline i64 _syscall_arch_prctl_get(int code, u64 *value) {
return __syscall2(SYS_arch_prctl, code, (i64)value); return _syscall2(_SYSCALL_arch_prctl, code, (i64)value);
} }

View File

@ -17,13 +17,13 @@ static _RT_Status _rt_file_open(_RT_File *file, char const *name, int _rt_flags)
} }
int mode = 0; int mode = 0;
int flags = 0; int flags = 0;
if((_rt_flags & 0x03) == 0x03) mode = O_RDWR; if((_rt_flags & 0x03) == 0x03) mode = _SYS_O_RDWR;
else if(_rt_flags & _RT_FILE_READ) mode = O_RDONLY; else if(_rt_flags & _RT_FILE_READ) mode = _SYS_O_RDONLY;
else if(_rt_flags & _RT_FILE_WRITE) mode = O_RDWR; else if(_rt_flags & _RT_FILE_WRITE) mode = _SYS_O_RDWR;
if(_rt_flags & _RT_FILE_CREATE) flags |= O_CREAT; if(_rt_flags & _RT_FILE_CREATE) flags |= _SYS_O_CREAT;
if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= O_EXCL; if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= _SYS_O_EXCL;
if(_rt_flags & _RT_FILE_TRUNCATE) flags |= O_TRUNC; if(_rt_flags & _RT_FILE_TRUNCATE) flags |= _SYS_O_TRUNC;
i64 fd = syscall_open(name, flags, mode); i64 fd = _syscall_open(name, flags, mode);
if(-fd == EACCES) return _RT_STATUS_FILE_ACCESS; if(-fd == EACCES) return _RT_STATUS_FILE_ACCESS;
if(-fd == EEXIST) return _RT_STATUS_FILE_EXISTS; if(-fd == EEXIST) return _RT_STATUS_FILE_EXISTS;
if(-fd == ENOENT) return _RT_STATUS_FILE_NOT_EXISTS; if(-fd == ENOENT) return _RT_STATUS_FILE_NOT_EXISTS;
@ -37,7 +37,7 @@ static _RT_Status _rt_file_open(_RT_File *file, char const *name, int _rt_flags)
} }
static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out_bytes_read) { static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out_bytes_read) {
i64 bytes_read = syscall_read(from->fd, buffer, size); i64 bytes_read = _syscall_read(from->fd, buffer, size);
if(bytes_read == 0) { if(bytes_read == 0) {
return _RT_STATUS_FILE_EOF; return _RT_STATUS_FILE_EOF;
} }
@ -49,7 +49,7 @@ static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out
} }
static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written) { static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written) {
i64 status = syscall_write(to->fd, buffer, size); i64 status = _syscall_write(to->fd, buffer, size);
if(-status == EBADF) return _RT_ERROR_BAD_PARAM; if(-status == EBADF) return _RT_ERROR_BAD_PARAM;
if(-status == EIO) return _RT_STATUS_FILE_IO_ERROR; if(-status == EIO) return _RT_STATUS_FILE_IO_ERROR;
if(-status > 0) return _RT_STATUS_FILE_IO_ERROR; if(-status > 0) return _RT_STATUS_FILE_IO_ERROR;
@ -58,7 +58,7 @@ static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_
} }
static _RT_Status _rt_file_close(_RT_File *file) { static _RT_Status _rt_file_close(_RT_File *file) {
i64 result = syscall_close(file->fd); i64 result = _syscall_close(file->fd);
if(result < 0) { if(result < 0) {
return _RT_STATUS_FILE_IO_ERROR; return _RT_STATUS_FILE_IO_ERROR;
} }
@ -66,11 +66,11 @@ static _RT_Status _rt_file_close(_RT_File *file) {
} }
_Noreturn static void _rt_program_exit(int code) { _Noreturn static void _rt_program_exit(int code) {
syscall_exit(code); _syscall_exit(code);
} }
static _RT_Status _rt_mem_alloc(void *optional_desired_addr, u64 size, void **out_addr) { static _RT_Status _rt_mem_alloc(void *optional_desired_addr, u64 size, void **out_addr) {
void *addr = syscall_mmap((u64)optional_desired_addr, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0); void *addr = _syscall_mmap((u64)optional_desired_addr, size, _SYS_PROT_READ|_SYS_PROT_WRITE, _SYS_MAP_ANONYMOUS, -1, 0);
if(addr == NULL) { if(addr == NULL) {
return _RT_ERROR_GENERIC; return _RT_ERROR_GENERIC;
} }
@ -79,7 +79,7 @@ static _RT_Status _rt_mem_alloc(void *optional_desired_addr, u64 size, void **ou
} }
static _RT_Status _rt_mem_free(void *ptr, u64 len) { static _RT_Status _rt_mem_free(void *ptr, u64 len) {
i64 result = syscall_munmap(ptr, len); i64 result = _syscall_munmap(ptr, len);
if(result == -1) { if(result == -1) {
return _RT_ERROR_GENERIC; return _RT_ERROR_GENERIC;
} }