harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

syscalls.ha (4118B)


      1 fn syscall0(_: u64) u64;
      2 fn syscall1(_: u64, _: u64) u64;
      3 fn syscall2(_: u64, _: u64, _: u64) u64;
      4 fn syscall3(_: u64, _: u64, _: u64, _: u64) u64;
      5 fn syscall4(_: u64, _: u64, _: u64, _: u64, _: u64) u64;
      6 fn syscall5(_: u64, _: u64, _: u64, _: u64, _: u64, _: u64) u64;
      7 fn syscall6(_: u64, _: u64, _: u64, _: u64, _: u64, _: u64, _: u64) u64;
      8 
      9 export fn write(fd: int, buf: *const void, count: size) size =
     10 	syscall3(SYS_write, fd: u64, buf: uintptr: u64, count: u64): size;
     11 
     12 export fn close(fd: int) int = syscall1(SYS_close, fd: u64): int;
     13 
     14 export fn dup3(old: int, new: int, flags: int) int =
     15 	syscall3(SYS_dup3, old: u64, new: u64, flags: u64): int;
     16 
     17 export fn dup2(old: int, new: int) int =
     18 	syscall3(SYS_dup3, old: u64, new: u64, 0): int;
     19 
     20 export fn getpid() int = syscall0(SYS_getpid): int;
     21 
     22 export def EXIT_SUCCESS: int = 0;
     23 export def EXIT_FAILURE: int = 1;
     24 
     25 export @noreturn fn exit(status: int) void = { syscall1(SYS_exit, status: u64); };
     26 
     27 export fn fork() int = syscall2(SYS_clone, SIGCHLD: u64, 0u64): int;
     28 
     29 export fn execve(
     30 	path: *const char,
     31 	argv: *[*]nullable *const char,
     32 	envp: *[*]nullable *const char,
     33 ) int = syscall3(SYS_execve,
     34 	path: uintptr: u64,
     35 	argv: uintptr: u64,
     36 	envp: uintptr: u64): int;
     37 
     38 export fn wait4(pid: int, status: *int, options: int, rusage: nullable *void) void = {
     39 	syscall4(SYS_wait4, pid: u64, status: uintptr: u64,
     40 		options: u64, rusage: uintptr: u64);
     41 };
     42 
     43 export fn wifexited(status: int) bool = wtermsig(status) == 0;
     44 export fn wexitstatus(status: int) int = (status & 0xff00) >> 8;
     45 
     46 export fn wtermsig(status: int) int = status & 0x7f;
     47 export fn wifsignaled(status: int) bool = (status & 0xffff) - 1 < 0xff;
     48 
     49 export fn kill(pid: int, signal: int) int =
     50 	syscall2(SYS_kill, pid: u64, signal: u64): int;
     51 
     52 export fn pipe2(pipefd: *[2]int, flags: int) int =
     53 	syscall2(SYS_pipe2, pipefd: uintptr: u64, flags: u64): int;
     54 
     55 export def MAP_SHARED: uint		= 0x01;
     56 export def MAP_PRIVATE: uint		= 0x02;
     57 export def MAP_SHARED_VALIDATE: uint	= 0x03;
     58 export def MAP_FIXED: uint		= 0x10;
     59 export def MAP_ANON: uint		= 0x20;
     60 export def MAP_NORESERVE: uint		= 0x4000;
     61 export def MAP_GROWSDOWN: uint		= 0x0100;
     62 export def MAP_DENYWRITE: uint		= 0x0800;
     63 export def MAP_EXECUTABLE: uint		= 0x1000;
     64 export def MAP_LOCKED: uint		= 0x2000;
     65 export def MAP_POPULATE: uint		= 0x8000;
     66 export def MAP_NONBLOCK: uint		= 0x10000;
     67 export def MAP_STACK: uint		= 0x20000;
     68 export def MAP_HUGETLB: uint		= 0x40000;
     69 export def MAP_SYNC: uint		= 0x80000;
     70 export def MAP_FIXED_NOREPLACE: uint	= 0x100000;
     71 export def MAP_FILE: uint		= 0;
     72 export def MAP_HUGE_SHIFT: uint		= 26;
     73 export def MAP_HUGE_MASK: uint		= 0x3f;
     74 export def MAP_HUGE_64KB: uint		= 16 << 26;
     75 export def MAP_HUGE_512KB: uint		= 19 << 26;
     76 export def MAP_HUGE_1MB: uint		= 20 << 26;
     77 export def MAP_HUGE_2MB: uint		= 21 << 26;
     78 export def MAP_HUGE_8MB: uint		= 23 << 26;
     79 export def MAP_HUGE_16MB: uint		= 24 << 26;
     80 export def MAP_HUGE_32MB: uint		= 25 << 26;
     81 export def MAP_HUGE_256MB: uint		= 28 << 26;
     82 export def MAP_HUGE_512MB: uint		= 29 << 26;
     83 export def MAP_HUGE_1GB: uint		= 30 << 26;
     84 export def MAP_HUGE_2GB: uint		= 31 << 26;
     85 export def MAP_HUGE_16GB: uint		= 34 << 26;
     86 
     87 export def PROT_NONE: uint	= 0;
     88 export def PROT_READ: uint 	= 1;
     89 export def PROT_WRITE: uint	= 2;
     90 export def PROT_EXEC: uint	= 4;
     91 export def PROT_GROWSDOWN: uint	= 0x01000000;
     92 export def PROT_GROWSUP: uint	= 0x02000000;
     93 
     94 export fn mmap(
     95 	addr: nullable *void,
     96 	length: size,
     97 	prot: uint,
     98 	flags: uint,
     99 	fd: int,
    100 	offs: size
    101 ) *void = {
    102 	let r = syscall6(SYS_mmap, addr: uintptr: u64, length: u64, prot: u64,
    103 		flags: u64, fd: u64, offs: u64): u64;
    104 	return if (r: int == -EPERM && addr == null
    105 			&& flags & MAP_ANON > 0 && flags & MAP_FIXED == 0) {
    106 		yield -ENOMEM: uintptr: *void; // Fix up incorrect EPERM from kernel
    107 	} else r: uintptr: *void;
    108 };
    109 
    110 export fn munmap(addr: *void, length: size) int =
    111 	syscall2(SYS_munmap, addr: uintptr: u64, length: u64): int;
    112 
    113 export fn mprotect(addr: *void, length: size, prot: uint) int =
    114 	syscall3(SYS_mprotect, addr: uintptr: u64, length: u64, prot: u64): int;
    115 
    116 export def SIGABRT: int	= 6;
    117 export def SIGCHLD: int	= 17;