Use linux includes whenever possible

This commit is contained in:
flysand7 2023-08-01 12:45:45 +11:00
parent 3a965154ed
commit e41df0dd4d
5 changed files with 42 additions and 125 deletions

View File

@ -33,7 +33,6 @@ platforms: [
path: "linux", path: "linux",
includes: [ includes: [
"syscall.c", "syscall.c",
"errno.c",
"entry.c" "entry.c"
], ],
tinyrt: [ tinyrt: [

View File

@ -6,7 +6,7 @@ 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(_SYS_STDERR_FILENO, stack_chk_fail_msg, sizeof stack_chk_fail_msg); _syscall_write(STDERR_FILENO, stack_chk_fail_msg, sizeof stack_chk_fail_msg);
_syscall_exit(1); _syscall_exit(1);
} }

View File

@ -1,76 +0,0 @@
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* Input/output error */
#define ENXIO 6 /* Device not configured */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file descriptor */
#define ECHILD 10 /* No child processes */
#define EDEADLK 11 /* Resource deadlock avoided */
#define ENOMEM 12 /* Cannot allocate memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define EBUSY 16 /* Device busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* Operation not supported by device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* Too many open files in system */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Inappropriate ioctl for device */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Numerical argument out of domain */
#define ERANGE 34 /* Result too large */
#define EAGAIN 35 /* Resource temporarily unavailable */
#define EWOULDBLOCK 35 /* Operation would block */
#define EINPROGRESS 36 /* Operation now in progress */
#define EALREADY 37 /* Operation already in progress */
#define ENOTSOCK 38 /* Socket operation on non-socket */
#define EDESTADDRREQ 39 /* Destination address required */
#define EMSGSIZE 40 /* Message too long */
#define EPROTOTYPE 41 /* Protocol wrong type for socket */
#define ENOPROTOOPT 42 /* Protocol not available */
#define EPROTONOSUPPORT 43 /* Protocol not supported */
#define EOPNOTSUPP 45 /* Operation not supported */
#define EAFNOSUPPORT 47 /* Address family not supported by protocol family */
#define EADDRINUSE 48 /* Address already in use */
#define EADDRNOTAVAIL 49 /* Can't assign requested address */
#define ENETDOWN 50 /* Network is down */
#define ENETUNREACH 51 /* Network is unreachable */
#define ENETRESET 52 /* Network dropped connection on reset */
#define ECONNABORTED 53 /* Software caused connection abort */
#define ECONNRESET 54 /* Connection reset by peer */
#define ENOBUFS 55 /* No buffer space available */
#define EISCONN 56 /* Socket is already connected */
#define ENOTCONN 57 /* Socket is not connected */
#define ETIMEDOUT 60 /* Operation timed out */
#define ECONNREFUSED 61 /* Connection refused */
#define ELOOP 62 /* Too many levels of symbolic links */
#define ENAMETOOLONG 63 /* File name too long */
#define EHOSTUNREACH 65 /* No route to host */
#define ENOTEMPTY 66 /* Directory not empty */
#define EDQUOT 69 /* Disk quota exceeded */
#define ENOLCK 77 /* No locks available */
#define ENOSYS 78 /* Function not implemented */
#define EILSEQ 84 /* Illegal byte sequence */
#define EOVERFLOW 87 /* Value too large to be stored in data type */
#define ECANCELED 88 /* Operation canceled */
#define EIDRM 89 /* Identifier removed */
#define ENOMSG 90 /* No message of desired type */
#define ENOTSUP 91 /* Not supported */
#define EBADMSG 92 /* Bad message */
#define ENOTRECOVERABLE 93 /* State not recoverable */
#define EOWNERDEAD 94 /* Previous owner died */
#define EPROTO 95 /* Protocol error */

View File

@ -1,48 +1,42 @@
// Standard handles file descriptors #include <asm/unistd.h>
#define _SYS_STDIN_FILENO 0 #include <asm/signal.h>
#define _SYS_STDOUT_FILENO 1 #include <asm/ioctls.h>
#define _SYS_STDERR_FILENO 2 #include <asm/mman.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <linux/time.h>
#include <errno.h>
// arch_prctl() syscall codes #if !defined(STDIN_FILENO)
#define _SYS_ARCH_SET_GS 0x1001 #define STDIN_FILENO 0
#define _SYS_ARCH_SET_FS 0x1002 #define STDOUT_FILENO 1
#define _SYS_ARCH_GET_FS 0x1003 #define STDERR_FILENO 2
#define _SYS_ARCH_GET_GS 0x1004 #endif
#define _SYS_ARCH_GET_CPUID 0x1011
#define _SYS_ARCH_SET_CPUID 0x1012
// open() syscall modes #if !defined(MAP_SHARED)
#define _SYS_O_ACCMODE 0003 #define MAP_SHARED 0x01
#define _SYS_O_RDONLY 00 #define MAP_PRIVATE 0x02
#define _SYS_O_WRONLY 01 #define MAP_SHARED_VALIDATE 0x03
#define _SYS_O_RDWR 02 #endif
#define _SYS_O_CREAT 0100 /* not fcntl */
#define _SYS_O_EXCL 0200 /* not fcntl */
#define _SYS_O_NOCTTY 0400 /* not fcntl */
#define _SYS_O_TRUNC 01000 /* not fcntl */
#define _SYS_O_APPEND 02000
#define _SYS_O_NONBLOCK 04000
#define _SYS_O_NDELAY O_NONBLOCK
#define _SYS_O_SYNC 010000
#define _SYS_O_FSYNC O_SYNC
#define _SYS_O_ASYNC 020000
// mmap() protection modes, flags and constants #if !defined(MAP_FAILED)
#define _SYS_PROT_READ 0x1 #define MAP_FAILED ((void *)-1)
#define _SYS_PROT_WRITE 0x2 #endif
#define _SYS_PROT_EXEC 0x4
#define _SYS_PROT_NONE 0x0
#define _SYS_PROT_GROWSDOWN 0x01000000
#define _SYS_PROT_GROWSUP 0x02000000
#define _SYS_MAP_SHARED 0x01
#define _SYS_MAP_PRIVATE 0x02
#define _SYS_MAP_SHARED_VALIDATE 0x03
#define _SYS_MAP_FILE 0
#define _SYS_MAP_ANONYMOUS 0x20
#define _SYS_MAP_32BIT 0x40
#define _SYS_MAP_FAILED (void *)()
// NOTE(bumbread): These are architecture-specific
#if !defined(O_RDONLY)
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0x40
#define O_EXCL 0x80
#define O_NOCTTY 0x100
#define O_TRUNC 0x200
#define O_APPEND 0x400
#define O_NONBLOCK 0x800
#define O_DIRECTORY 0x10000
#endif
#define _SYSCALL_read 0 #define _SYSCALL_read 0
#define _SYSCALL_write 1 #define _SYSCALL_write 1

View File

@ -17,12 +17,12 @@ 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 = _SYS_O_RDWR; if((_rt_flags & 0x03) == 0x03) mode = O_RDWR;
else if(_rt_flags & _RT_FILE_READ) mode = _SYS_O_RDONLY; else if(_rt_flags & _RT_FILE_READ) mode = O_RDONLY;
else if(_rt_flags & _RT_FILE_WRITE) mode = _SYS_O_RDWR; else if(_rt_flags & _RT_FILE_WRITE) mode = O_RDWR;
if(_rt_flags & _RT_FILE_CREATE) flags |= _SYS_O_CREAT; if(_rt_flags & _RT_FILE_CREATE) flags |= O_CREAT;
if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= _SYS_O_EXCL; if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= O_EXCL;
if(_rt_flags & _RT_FILE_TRUNCATE) flags |= _SYS_O_TRUNC; if(_rt_flags & _RT_FILE_TRUNCATE) flags |= 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;
@ -70,7 +70,7 @@ _Noreturn static void _rt_program_exit(int 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, _SYS_PROT_READ|_SYS_PROT_WRITE, _SYS_MAP_PRIVATE|_SYS_MAP_ANONYMOUS, -1, 0); void *addr = _syscall_mmap((u64)optional_desired_addr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if(addr == NULL) { if(addr == NULL) {
return _RT_ERROR_GENERIC; return _RT_ERROR_GENERIC;
} }