shithub: mc

ref: 606bce2a5601d6a124da33d007e071ed9d43ff45
dir: /lib/sys/sys+linux-x64.myr/

View raw version
use "systypes"

pkg sys =
	type scno	= int64	/* syscall */

	/* processes/threads */
	type pid	= int	/* process id */
	type tid	= int	/* thread id */
	type cloneopt	= int64	/* options for clone(2) */

	/* file descriptor manipulation */
	type fdopt	= int64	/* fd options */
	type fd		= int32	/* fd */
	type whence	= uint64	/* seek from whence */
	type filemode	= uint32	/* file open mode */

	type mprot	= int64	/* memory protection */
	type mopt	= int64	/* memory mapping options */
	type socktype	= int64	/* socket type */
	type sockproto	= int64	/* socket protocol */
	type sockfam	= uint16	/* socket family */
	type sockopt	= int64

	type epollflags	= uint32
	type epollop	= uint32
	type epollevttype	= uint32

	type pollevt	= uint16

	type futexop	= uint32

	type clock = union
		`Clockrealtime
		`Clockmonotonic
		`Clockproccpu
		`Clockthreadcpu
		`Clockmonotonicraw
		`Clockrealtimecoarse
		`Clockmonotoniccoarse
		`Clockboottime
		`Clockrealtimealarm
		`Clockboottimealarm
	;;

	type waitstatus = union
		`Waitexit int32
		`Waitsig  int32
		`Waitstop int32
		`Waitfail int32
	;;

	type timespec = struct
		sec	: uint64
		nsec	: uint64
	;;

	type timeval = struct
		sec	: uint64
		usec	: uint64
	;;

	type rusage = struct
		utime	: timeval	/* user time */
		stime	: timeval	/* system time */
		_opaque	: uint64[14]	/* padding (darwin-specific data) */
	;;

	type statbuf = struct
		 dev	: uint64
		 ino	: uint64
		 nlink	: uint64
		 mode	: filemode
		 uid	: uint32
		 gid	: uint32
		 __pad0	: uint32
		 rdev	: uint64
		 size	: uint64
		 blksz	: uint32
		 blocks	: uint64
		 atime	: timespec
		 mtime	: timespec
		 ctime	: timespec
		 __pad1	: uint64[3]
	;;

	type dirent64 = struct
		ino	: uint64
		off	: uint64
		reclen	: uint16
		etype	: byte
		name	: byte[...]	/* special case; zero length => unchecked indexing */
	;;

	type utsname = struct
		system	: byte[65]
		node	: byte[65]
		release	: byte[65]
		version	: byte[65]
		machine	: byte[65]
		domain	: byte[65]
	;;

	type sockaddr = struct
		fam	: sockfam
		data	: byte[14]
	;;

	type sockaddr_in = struct
		fam	: sockfam
		port	: uint16
		addr	: byte[4]
		zero	: byte[8]
	;;

	type sockaddr_in6 = struct
		fam	: sockfam
		port	: uint16
		addr	: byte[16]
		scope	: uint32
	;;

        type sockaddr_un = struct
		fam	: sockfam
		path	: byte[108]
	;;

	type sockaddr_storage = struct
		fam	: sockfam
		__align	: uint32
		__pad	: byte[112]
	;;

	type epollevt = struct
		events	: epollevttype
		data	: byte[8]
	;;

	type pollfd = struct
		fd	: fd
		events	: pollevt
		revents	: pollevt
	;;

	/* clone options */
	const Clonesignal	: cloneopt = 0xff
	const Clonevm		: cloneopt = 0x100
	const Clonefs		: cloneopt = 0x200
	const Clonefiles	: cloneopt = 0x400
	const Clonesighand	: cloneopt = 0x800
	const Cloneptrace	: cloneopt = 0x2000
	const Clonevfork	: cloneopt = 0x4000
	const Cloneparent	: cloneopt = 0x8000
	const Clonethread	: cloneopt = 0x10000
	const Clonenewns	: cloneopt = 0x20000
	const Clonesysvsem	: cloneopt = 0x40000
	const Clonesettls	: cloneopt = 0x80000
	const Cloneparentsettid	: cloneopt = 0x100000
	const Clonechildcleartid: cloneopt = 0x200000
	const Clonedetached	: cloneopt = 0x400000
	const Cloneuntraced	: cloneopt = 0x800000
	const Clonechildsettid	: cloneopt = 0x1000000
	const Clonenewuts	: cloneopt = 0x4000000
	const Clonenewipc	: cloneopt = 0x8000000
	const Clonenewuser	: cloneopt = 0x10000000
	const Clonenewpid	: cloneopt = 0x20000000
	const Clonenewnet	: cloneopt = 0x40000000
	const Cloneio		: cloneopt = 0x80000000

	type ptregs = struct
	;;

	/* open options */
	const Ordonly  	: fdopt = 0x0
	const Owronly  	: fdopt = 0x1
	const Ordwr    	: fdopt = 0x2
	const Ocreat   	: fdopt = 0x40
	const Oexcl  	: fdopt = 0x80
	const Otrunc   	: fdopt = 0x200
	const Oappend  	: fdopt = 0x400
	const Ondelay  	: fdopt = 0x800
	const Odirect	: fdopt = 0x4000
	const Odir	: fdopt = 0x10000
	const Onofollow	: fdopt = 0x20000

	/* stat modes */
	const Sifmt	: filemode = 0xf000
	const Sififo	: filemode = 0x1000
	const Sifchr	: filemode = 0x2000
	const Sifdir	: filemode = 0x4000
	const Sifblk	: filemode = 0x6000
	const Sifreg	: filemode = 0x8000
	const Siflnk	: filemode = 0xa000
	const Sifsock	: filemode = 0xc000

	/* mmap protection */
	const Mprotnone	: mprot = 0x0
	const Mprotrd	: mprot = 0x1
	const Mprotwr	: mprot = 0x2
	const Mprotexec	: mprot = 0x4
	const Mprotrw	: mprot = 0x3 /* convenience */
	
	/* mmap options */
	const Mshared	: mopt = 0x1
	const Mpriv	: mopt = 0x2
	const Mfixed	: mopt = 0x10
	const Mfile	: mopt = 0x0
	const Manon	: mopt = 0x20
	const M32bit	: mopt = 0x40

	/* socket families. INCOMPLETE. */
	const Afunspec	: sockfam = 0
	const Afunix	: sockfam = 1
	const Afinet	: sockfam = 2
	const Afinet6	: sockfam = 10

	/* socket types. */
	const Sockstream	: socktype = 1	/* sequenced, reliable byte stream */
	const Sockdgram		: socktype = 2	/* datagrams */
	const Sockraw		: socktype = 3	/* raw proto */
	const Sockrdm		: socktype = 4	/* reliably delivered messages */
	const Sockseqpacket	: socktype = 5	/* sequenced, reliable packets */
	const Sockdccp		: socktype = 6	/* data congestion control protocol */
	const Sockpack		: socktype = 10	/* linux specific packet */
	const Solsock	: socktype =	1

	/* socket options */
	const Sodebug		: sockopt = 1
	const Soreuseaddr	: sockopt = 2
	const Sotype		: sockopt = 3
	const Soerror		: sockopt = 4
	const Sodontroute	: sockopt = 5
	const Sobroadcast	: sockopt = 6
	const Sosndbuf		: sockopt = 7
	const Sorcvbuf		: sockopt = 8
	const Sosndbufforce	: sockopt = 32
	const Sorcvbufforce	: sockopt = 33
	const Sokeepalive	: sockopt = 9
	const Sooobinline	: sockopt = 10
	const Sono_check	: sockopt = 11
	const Sopriority	: sockopt = 12
	const Solinger		: sockopt = 13
	const Sobsdcompat	: sockopt = 14
	const Soreuseport	: sockopt = 15
	const Sopasscred	: sockopt = 16
	const Sopeercred	: sockopt = 17
	const Sorcvlowat	: sockopt = 18
	const Sosndlowat	: sockopt = 19
	const Sorcvtimeo	: sockopt = 20
	const Sosndtimeo	: sockopt = 21

	/* network protocols */
	const Ipproto_ip	: sockproto = 0
	const Ipproto_icmp	: sockproto = 1
	const Ipproto_tcp	: sockproto = 6
	const Ipproto_udp	: sockproto = 17
	const Ipproto_raw	: sockproto = 255

	/* epoll flags */
	const Epollcloexec	: epollflags	= 0o2000000

	/* epoll ops */
	const Epollctladd	: epollop	= 1
	const Epollctlmod	: epollop	= 2
	const Epollctldel	: epollop	= 3

	/* epoll events */
	const Epollin	: epollevttype = 0x001
	const Epollpri	: epollevttype = 0x002
	const Epollout	: epollevttype = 0x004
	const Epollerr	: epollevttype = 0x008
	const Epollhup	: epollevttype = 0x010
	const Epollrdnorm	: epollevttype = 0x040
	const Epollrdband	: epollevttype = 0x080
	const Epollwrnorm	: epollevttype = 0x100
	const Epollwrband	: epollevttype = 0x200
	const Epollmsg		: epollevttype = 0x400
	const Epollrdhup	: epollevttype = 0x2000
	const Epollwakeup	: epollevttype = 1 << 29
	const Epolloneshot	: epollevttype = 1 << 30
	const Epolledge	: epollevttype = 1 << 31
	
	/* futex ops */
	const Futexwait	: futexop = 0
	const Futexwake	: futexop = 1
	/* Futexfd: removed */
	const Futexrequeue	: futexop = 3
	const Futexcmprequeue	: futexop = 4
	const Futexwakeop	: futexop = 5
	const Futexlockpi	: futexop = 6
	const Futexunlockpi	: futexop = 7
	const Futextrylockpi	: futexop = 8
	const Futexwaitbitset	: futexop = 9
	const Futexwakebitset	: futexop = 10
	const Futexwaitrequeuepi	: futexop = 11
	const Futexcmprequeuepi	: futexop = 12

	const Futexpriv	: futexop = 128
	const Futexclockrt	: futexop = 256

	/* poll events : posix */
	const Pollin	: pollevt = 0x001	/* There is data to read.  */
	const Pollpri	: pollevt = 0x002	/* There is urgent data to read.  */
	const Pollout	: pollevt = 0x004	/* Writing now will not block.  */

	/* poll events: xopen */
	const Pollrdnorm	: pollevt = 0x040	/* Normal data may be read.  */
	const Pollrdband	: pollevt = 0x080	/* Priority data may be read.  */
	const Pollwrnorm	: pollevt = 0x100	/* Writing now will not block.  */
	const Pollwrband	: pollevt = 0x200	/* Priority data may be written.  */

	/* poll events: linux */
	const Pollmsg		: pollevt = 0x400
	const Pollremove	: pollevt = 0x1000
	const Pollrdhup		: pollevt = 0x2000

	const Seekset	: whence = 0
	const Seekcur	: whence = 1
	const Seekend	: whence = 2

	/* return value for a failed mapping */
	const Mapbad	: byte# = (-1 : byte#)

	/* syscalls */
	const Sysread			: scno = 0
	const Syswrite			: scno = 1
	const Sysopen			: scno = 2
	const Sysclose			: scno = 3
	const Sysstat			: scno = 4
	const Sysfstat			: scno = 5
	const Syslstat			: scno = 6
	const Syspoll			: scno = 7
	const Syslseek			: scno = 8
	const Sysmmap			: scno = 9
	const Sysmprotect		: scno = 10
	const Sysmunmap			: scno = 11
	const Sysbrk			: scno = 12
	const Sysrt_sigaction		: scno = 13
	const Sysrt_sigprocmask		: scno = 14
	const Sysrt_sigreturn		: scno = 15
	const Sysioctl			: scno = 16
	const Syspread64		: scno = 17
	const Syspwrite64		: scno = 18
	const Sysreadv			: scno = 19
	const Syswritev			: scno = 20
	const Sysaccess			: scno = 21
	const Syspipe			: scno = 22
	const Sysselect			: scno = 23
	const Syssched_yield		: scno = 24
	const Sysmremap			: scno = 25
	const Sysmsync			: scno = 26
	const Sysmincore		: scno = 27
	const Sysmadvise		: scno = 28
	const Sysshmget			: scno = 29
	const Sysshmat			: scno = 30
	const Sysshmctl			: scno = 31
	const Sysdup			: scno = 32
	const Sysdup2			: scno = 33
	const Syspause			: scno = 34
	const Sysnanosleep		: scno = 35
	const Sysgetitimer		: scno = 36
	const Sysalarm			: scno = 37
	const Syssetitimer		: scno = 38
	const Sysgetpid			: scno = 39
	const Syssendfile		: scno = 40
	const Syssocket			: scno = 41
	const Sysconnect		: scno = 42
	const Sysaccept			: scno = 43
	const Syssendto			: scno = 44
	const Sysrecvfrom		: scno = 45
	const Syssendmsg		: scno = 46
	const Sysrecvmsg		: scno = 47
	const Sysshutdown		: scno = 48
	const Sysbind			: scno = 49
	const Syslisten			: scno = 50
	const Sysgetsockname		: scno = 51
	const Sysgetpeername		: scno = 52
	const Syssocketpair		: scno = 53
	const Syssetsockopt		: scno = 54
	const Sysgetsockopt		: scno = 55
	const Sysclone			: scno = 56
	const Sysfork			: scno = 57
	const Sysvfork			: scno = 58
	const Sysexecve			: scno = 59
	const Sysexit			: scno = 60
	const Syswait4			: scno = 61
	const Syskill			: scno = 62
	const Sysuname			: scno = 63
	const Syssemget			: scno = 64
	const Syssemop			: scno = 65
	const Syssemctl			: scno = 66
	const Sysshmdt			: scno = 67
	const Sysmsgget			: scno = 68
	const Sysmsgsnd			: scno = 69
	const Sysmsgrcv			: scno = 70
	const Sysmsgctl			: scno = 71
	const Sysfcntl			: scno = 72
	const Sysflock			: scno = 73
	const Sysfsync			: scno = 74
	const Sysfdatasync		: scno = 75
	const Systruncate		: scno = 76
	const Sysftruncate		: scno = 77
	const Sysgetdents		: scno = 78
	const Sysgetcwd			: scno = 79
	const Syschdir			: scno = 80
	const Sysfchdir			: scno = 81
	const Sysrename			: scno = 82
	const Sysmkdir			: scno = 83
	const Sysrmdir			: scno = 84
	const Syscreat			: scno = 85
	const Syslink			: scno = 86
	const Sysunlink			: scno = 87
	const Syssymlink		: scno = 88
	const Sysreadlink		: scno = 89
	const Syschmod			: scno = 90
	const Sysfchmod			: scno = 91
	const Syschown			: scno = 92
	const Sysfchown			: scno = 93
	const Syslchown			: scno = 94
	const Sysumask			: scno = 95
	const Sysgettimeofday		: scno = 96
	const Sysgetrlimit		: scno = 97
	const Sysgetrusage		: scno = 98
	const Syssysinfo		: scno = 99
	const Systimes			: scno = 100
	const Sysptrace			: scno = 101
	const Sysgetuid			: scno = 102
	const Syssyslog			: scno = 103
	const Sysgetgid			: scno = 104
	const Syssetuid			: scno = 105
	const Syssetgid			: scno = 106
	const Sysgeteuid		: scno = 107
	const Sysgetegid		: scno = 108
	const Syssetpgid		: scno = 109
	const Sysgetppid		: scno = 110
	const Sysgetpgrp		: scno = 111
	const Syssetsid			: scno = 112
	const Syssetreuid		: scno = 113
	const Syssetregid		: scno = 114
	const Sysgetgroups		: scno = 115
	const Syssetgroups		: scno = 116
	const Syssetresuid		: scno = 117
	const Sysgetresuid		: scno = 118
	const Syssetresgid		: scno = 119
	const Sysgetresgid		: scno = 120
	const Sysgetpgid		: scno = 121
	const Syssetfsuid		: scno = 122
	const Syssetfsgid		: scno = 123
	const Sysgetsid			: scno = 124
	const Syscapget			: scno = 125
	const Syscapset			: scno = 126
	const Sysrt_sigpending		: scno = 127
	const Sysrt_sigtimedwait	: scno = 128
	const Sysrt_sigqueueinfo	: scno = 129
	const Sysrt_sigsuspend		: scno = 130
	const Syssigaltstack		: scno = 131
	const Sysutime			: scno = 132
	const Sysmknod			: scno = 133
	const Sysuselib			: scno = 134
	const Syspersonality		: scno = 135
	const Sysustat			: scno = 136
	const Sysstatfs			: scno = 137
	const Sysfstatfs		: scno = 138
	const Syssysfs			: scno = 139
	const Sysgetpriority		: scno = 140
	const Syssetpriority		: scno = 141
	const Syssched_setparam		: scno = 142
	const Syssched_getparam		: scno = 143
	const Syssched_setscheduler	: scno = 144
	const Syssched_getscheduler	: scno = 145
	const Syssched_get_priority_max	: scno = 146
	const Syssched_get_priority_min	: scno = 147
	const Syssched_rr_get_interval	: scno = 148
	const Sysmlock			: scno = 149
	const Sysmunlock		: scno = 150
	const Sysmlockall		: scno = 151
	const Sysmunlockall		: scno = 152
	const Sysvhangup		: scno = 153
	const Sysmodify_ldt		: scno = 154
	const Syspivot_root		: scno = 155
	const Sys_sysctl		: scno = 156
	const Sysprctl			: scno = 157
	const Sysarch_prctl		: scno = 158
	const Sysadjtimex		: scno = 159
	const Syssetrlimit		: scno = 160
	const Syschroot			: scno = 161
	const Syssync			: scno = 162
	const Sysacct			: scno = 163
	const Syssettimeofday		: scno = 164
	const Sysmount			: scno = 165
	const Sysumount2		: scno = 166
	const Sysswapon			: scno = 167
	const Sysswapoff		: scno = 168
	const Sysreboot			: scno = 169
	const Syssethostname		: scno = 170
	const Syssetdomainname		: scno = 171
	const Sysiopl			: scno = 172
	const Sysioperm			: scno = 173
	const Syscreate_module		: scno = 174
	const Sysinit_module		: scno = 175
	const Sysdelete_module		: scno = 176
	const Sysget_kernel_syms	: scno = 177
	const Sysquery_module		: scno = 178
	const Sysquotactl		: scno = 179
	const Sysnfsservctl		: scno = 180
	const Sysgetpmsg		: scno = 181
	const Sysputpmsg		: scno = 182
	const Sysafs_syscall		: scno = 183
	const Systuxcall		: scno = 184
	const Syssecurity		: scno = 185
	const Sysgettid			: scno = 186
	const Sysreadahead		: scno = 187
	const Syssetxattr		: scno = 188
	const Syslsetxattr		: scno = 189
	const Sysfsetxattr		: scno = 190
	const Sysgetxattr		: scno = 191
	const Syslgetxattr		: scno = 192
	const Sysfgetxattr		: scno = 193
	const Syslistxattr		: scno = 194
	const Sysllistxattr		: scno = 195
	const Sysflistxattr		: scno = 196
	const Sysremovexattr		: scno = 197
	const Syslremovexattr		: scno = 198
	const Sysfremovexattr		: scno = 199
	const Systkill			: scno = 200
	const Systime			: scno = 201
	const Sysfutex			: scno = 202
	const Syssched_setaffinity	: scno = 203
	const Syssched_getaffinity	: scno = 204
	const Sysset_thread_area	: scno = 205
	const Sysio_setup		: scno = 206
	const Sysio_destroy		: scno = 207
	const Sysio_getevents		: scno = 208
	const Sysio_submit		: scno = 209
	const Sysio_cancel		: scno = 210
	const Sysget_thread_area	: scno = 211
	const Syslookup_dcookie		: scno = 212
	const Sysepoll_create		: scno = 213
	const Sysepoll_ctl_old		: scno = 214
	const Sysepoll_wait_old		: scno = 215
	const Sysremap_file_pages	: scno = 216
	const Sysgetdents64		: scno = 217
	const Sysset_tid_address	: scno = 218
	const Sysrestart_syscall	: scno = 219
	const Syssemtimedop		: scno = 220
	const Sysfadvise64		: scno = 221
	const Systimer_create		: scno = 222
	const Systimer_settime		: scno = 223
	const Systimer_gettime		: scno = 224
	const Systimer_getoverrun	: scno = 225
	const Systimer_delete		: scno = 226
	const Sysclock_settime		: scno = 227
	const Sysclock_gettime		: scno = 228
	const Sysclock_getres		: scno = 229
	const Sysclock_nanosleep	: scno = 230
	const Sysexit_group		: scno = 231
	const Sysepoll_wait		: scno = 232
	const Sysepoll_ctl		: scno = 233
	const Systgkill			: scno = 234
	const Sysutimes			: scno = 235
	const Sysvserver		: scno = 236
	const Sysmbind			: scno = 237
	const Sysset_mempolicy		: scno = 238
	const Sysget_mempolicy		: scno = 239
	const Sysmq_open		: scno = 240
	const Sysmq_unlink		: scno = 241
	const Sysmq_timedsend		: scno = 242
	const Sysmq_timedreceive	: scno = 243
	const Sysmq_notify		: scno = 244
	const Sysmq_getsetattr		: scno = 245
	const Syskexec_load		: scno = 246
	const Syswaitid			: scno = 247
	const Sysadd_key		: scno = 248
	const Sysrequest_key		: scno = 249
	const Syskeyctl			: scno = 250
	const Sysioprio_set		: scno = 251
	const Sysioprio_get		: scno = 252
	const Sysinotify_init		: scno = 253
	const Sysinotify_add_watch	: scno = 254
	const Sysinotify_rm_watch	: scno = 255
	const Sysmigrate_pages		: scno = 256
	const Sysopenat			: scno = 257
	const Sysmkdirat		: scno = 258
	const Sysmknodat		: scno = 259
	const Sysfchownat		: scno = 260
	const Sysfutimesat		: scno = 261
	const Sysnewfstatat		: scno = 262
	const Sysunlinkat		: scno = 263
	const Sysrenameat		: scno = 264
	const Syslinkat			: scno = 265
	const Syssymlinkat		: scno = 266
	const Sysreadlinkat		: scno = 267
	const Sysfchmodat		: scno = 268
	const Sysfaccessat		: scno = 269
	const Syspselect6		: scno = 270
	const Sysppoll			: scno = 271
	const Sysunshare		: scno = 272
	const Sysset_robust_list	: scno = 273
	const Sysget_robust_list	: scno = 274
	const Syssplice			: scno = 275
	const Systee			: scno = 276
	const Syssync_file_range	: scno = 277
	const Sysvmsplice		: scno = 278
	const Sysmove_pages		: scno = 279
	const Sysutimensat		: scno = 280
	const Sysepoll_pwait		: scno = 281
	const Syssignalfd		: scno = 282
	const Systimerfd_create		: scno = 283
	const Syseventfd		: scno = 284
	const Sysfallocate		: scno = 285
	const Systimerfd_settime	: scno = 286
	const Systimerfd_gettime	: scno = 287
	const Sysaccept4		: scno = 288
	const Syssignalfd4		: scno = 289
	const Syseventfd2		: scno = 290
	const Sysepoll_create1		: scno = 291
	const Sysdup3			: scno = 292
	const Syspipe2			: scno = 293
	const Sysinotify_init1		: scno = 294
	const Syspreadv			: scno = 295
	const Syspwritev		: scno = 296
	const Sysrt_tgsigqueueinfo	: scno = 297
	const Sysperf_event_open	: scno = 298
	const Sysrecvmmsg		: scno = 299
	const Sysfanotify_init		: scno = 300
	const Sysfanotify_mark		: scno = 301
	const Sysprlimit64		: scno = 302
	const Sysname_to_handle_at	: scno = 303
	const Sysopen_by_handle_at	: scno = 304
	const Sysclock_adjtime		: scno = 305
	const Syssyncfs			: scno = 306
	const Syssendmmsg		: scno = 307
	const Syssetns			: scno = 308
	const Sysgetcpu			: scno = 309
	const Sysprocess_vm_readv	: scno = 310
	const Sysprocess_vm_writev	: scno = 311

	/* getting to the os */
	extern const syscall	: (sc:scno, args:... -> int64)

	/* process management */
	const exit	: (status:int -> void)
	const exit_group	: (status:int -> void)
	const getpid	: ( -> pid)
	const kill	: (pid:pid, sig:int64 -> int64)
	const fork	: (-> pid)
	/* FIXME: where the fuck is 'struct pt_reg' defined?? */
	const clone	: (flags : cloneopt, stk : byte#, ptid : pid#, ctid : pid#, ptreg : byte# -> pid)
	extern const fnclone	: ( flags : cloneopt, \
				stk : byte#, \
				ptid : pid#, \
				tls : byte#, \
				ctid : pid#, \
				ptreg : byte#, \
				fn : void# \	/* we need a raw pointer */ 
				-> pid)
	const wait4	: (pid:pid, loc:int32#, opt : int64, usage:rusage#	-> int64)
	const waitpid	: (pid:pid, loc:int32#, opt : int64	-> int64)
	const execv	: (cmd : byte[:], args : byte[:][:] -> int64)
	const execve	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
	/* wrappers to extract wait status */
	const waitstatus	: (st : int32 -> waitstatus)

	/* file manipulation */
	const open	: (path:byte[:], opts:fdopt -> fd)
	const openmode	: (path:byte[:], opts:fdopt, mode:int64 -> fd)
	const close	: (fd:fd -> int64)
	const rename	: (from : byte[:], to : byte[:] -> int64)
	const creat	: (path:byte[:], mode:int64 -> fd)
	const unlink	: (path:byte[:] -> int)
	const read	: (fd:fd, buf:byte[:] -> size)
	const write	: (fd:fd, buf:byte[:] -> size)
	const lseek	: (fd:fd, off:off, whence:whence -> int64)
	const stat	: (path:byte[:], sb:statbuf# -> int64)
	const lstat	: (path:byte[:], sb:statbuf# -> int64)
	const fstat	: (fd:fd, sb:statbuf# -> int64)
	const mkdir	: (path : byte[:], mode : int64	-> int64)
	generic ioctl	: (fd:fd, req : int64, arg:@a# -> int64)
	const getdents64	: (fd:fd, buf : byte[:] -> int64)
	const chdir	: (p : byte[:] -> int64)
	const getcwd	: (buf : byte[:] -> int64)

	/* fd stuff */
	const pipe	: (fds : fd[2]# -> int64)
	const dup	: (fd : fd -> fd)
	const dup2	: (src : fd, dst : fd -> fd)

	/* threading */
	const futex	: (uaddr : int32#, op : futexop, val : int32, \
		timeout : timespec#, uaddr2 : int32#, val3 : int32 -> int64)

	/* polling */
	const epollcreate	: (flg : epollflags	-> fd)	/* actually epoll_create1 */
	const epollctl	: (epfd : fd, op : int, fd : fd, evt : epollevt# -> int)
	const epollwait	: (epfd : fd, evts : epollevt[:], timeout : int -> int)
	const poll	: (pfd	: pollfd[:], timeout : int	-> int)

	/* networking */
	const socket	: (dom : sockfam, stype : socktype, proto : sockproto	-> fd)
	const connect	: (sock	: fd, addr : sockaddr#, len : size -> int)
	const accept	: (sock : fd, addr : sockaddr#, len : size# -> fd)
	const listen	: (sock : fd, backlog : int	-> int)
	const bind	: (sock : fd, addr : sockaddr#, len : size -> int)
	const setsockopt	: (sock : fd, lev : socktype, opt : sockopt, val : void#, len : size -> int)
	const getsockopt	: (sock : fd, lev : socktype, opt : sockopt, val : void#, len : size# -> int)

	/* memory mapping */
	const munmap	: (addr:byte#, len:size -> int64)
	const mmap	: (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#)

	/* time */
	const clock_getres	: (clk : clock, ts : timespec# -> int32)
	const clock_gettime	: (clk : clock, ts : timespec# -> int32)
	const clock_settime	: (clk : clock, ts : timespec# -> int32)
	const nanosleep	: (req : timespec#, rem : timespec# -> int32)

	/* system information */
	const uname 	: (buf : utsname# -> int)

	/* exported values: initialized by start code */
	extern const __environment : byte[:][:]
	extern const __cenvp : byte##
;;

/* 
wraps a syscall argument, converting it to 64 bits for the syscall function.
This is the same as casting, but more concise than writing a cast to int64.
*/
generic a = {x : @t; -> (x : uint64)}

/* asm stubs from util.s */
extern const cstring	: (str : byte[:] -> byte#)
extern const alloca	: (sz : size	-> byte#)

/* process management */
const exit	= {status;		syscall(Sysexit, a(status))}
const exit_group	= {status;	syscall(Sysexit_group, a(status))}
const getpid	= {;			-> (syscall(Sysgetpid) : pid)}
const kill	= {pid, sig;		-> syscall(Syskill, a(pid), a(sig))}
const fork	= {;			-> (syscall(Sysfork) : pid)}
const clone	= {flags, stk, ptid, ctid, ptreg;	-> (syscall(Sysclone, a(flags), a(stk), a(ptid), a(ctid), a(ptreg)) : pid)}
const wait4	= {pid, loc, opt, usage;	-> syscall(Syswait4, a(pid), a(loc), a(opt), a(usage))}
const waitpid	= {pid, loc, opt;
	var rusage
	-> wait4(pid, loc, opt, &rusage)
}

const execv	= {cmd, args
	var p, cargs, i

	/* of course we fucking have to duplicate this code everywhere,
	* since we want to stack allocate... */
	p = alloca((args.len + 1)*sizeof(byte#))
	cargs = (p : byte##)[:args.len + 1]
	for i = 0; i < args.len; i++
		cargs[i] = cstring(args[i])
	;;
	cargs[args.len] = (0 : byte#)
	-> syscall(Sysexecve, cstring(cmd), a(p), a(__cenvp))
}

const execve	= {cmd, args, env
	var cargs, cenv, i
	var ap, ep

	/* copy the args */
	ap = alloca((args.len + 1)*sizeof(byte#))
	cargs = (ap : byte##)[:args.len + 1]
	for i = 0; i < args.len; i++
		cargs[i] = cstring(args[i])
	;;
	cargs[args.len] = (0 : byte#)

	/*
	 copy the env.
	 of course we fucking have to duplicate this code everywhere,
	 since we want to stack allocate... 
	*/
	ep = alloca((env.len + 1)*sizeof(byte#))
	cenv = (ep : byte##)[:env.len]
	for i = 0; i < env.len; i++
		cenv[i] = cstring(env[i])
	;;
	cenv[env.len] = (0 : byte#)

	-> syscall(Sysexecve, cstring(cmd), a(ap), a(ep))
}

/* file manipulation */
const open	= {path, opts;		-> (syscall(Sysopen, cstring(path), a(opts), a(0o777)) : fd)}
const openmode	= {path, opts, mode;	-> (syscall(Sysopen, cstring(path), a(opts), a(mode)) : fd)}
const close	= {fd;			-> syscall(Sysclose, a(fd))}
const creat	= {path, mode;		-> (syscall(Syscreat, cstring(path), a(mode)) : fd)}
const rename	= {from, to;		-> syscall(Sysrename, cstring(from), cstring(to))}
const unlink	= {path;		-> (syscall(Sysunlink, cstring(path)) : int)}
const read	= {fd, buf;		-> (syscall(Sysread, a(fd), (buf : byte#), a(buf.len)) : size)}
const write	= {fd, buf;		-> (syscall(Syswrite, a(fd), (buf : byte#), a(buf.len)) : size)}
const lseek	= {fd, off, whence;	-> syscall(Syslseek, a(fd), a(off), a(whence))}
const stat	= {path, sb;		-> syscall(Sysstat, cstring(path), a(sb))}
const lstat	= {path, sb;		-> syscall(Syslstat, cstring(path), a(sb))}
const fstat	= {fd, sb;		-> syscall(Sysfstat, a(fd), a(sb))}
const mkdir	= {path, mode;		-> (syscall(Sysmkdir, cstring(path), a(mode)) : int64)}
generic ioctl	= {fd, req, arg;	-> (syscall(Sysioctl, a(fd), a(req), a(arg)) : int64)}
const getdents64	= {fd, buf;	-> syscall(Sysgetdents64, a(fd), (buf : byte#), a(buf.len))}
const chdir	= {dir;	-> syscall(Syschdir, cstring(dir))}
const getcwd	= {buf;	-> syscall(Sysgetcwd, a(buf), a(buf.len))}

/* file stuff */
const pipe	= {fds;	-> syscall(Syspipe, a(fds))}
const dup 	= {fd;	-> (syscall(Sysdup, a(fd)) : fd)}
const dup2 	= {src, dst;	-> (syscall(Sysdup2, a(src), a(dst)) : fd)}

/* threading */
const futex	= {uaddr, op, val, timeout, uaddr2, val3
	-> syscall(Sysfutex, a(uaddr), a(op), a(val), a(timeout), a(uaddr2), a(val3))}

/* poll */
const poll	= {pfd, timeout;	-> (syscall(Syspoll, (pfd : pollfd#), a(pfd.len), a(timeout)) : int)}
const epollctl	= {epfd, op, fd, evt;
	-> (syscall(Sysepoll_ctl, a(epfd), a(op), a(fd), a(evt)) : int)}
const epollwait	= {epfd, evts, timeout;
	-> (syscall(Sysepoll_wait, a(epfd), (evts : epollevt#), a(evts.len), a(timeout)) : int)}
const epollcreate	= {flg;	-> (syscall(Sysepoll_create1, a(flg)) : fd)}

/* networking */
const socket	= {dom, stype, proto;	-> (syscall(Syssocket, a(dom), a(stype), a(proto)) : fd)}
const connect	= {sock, addr, len;	-> (syscall(Sysconnect, a(sock), a(addr), a(len)) : int)}
const bind	= {sock, addr, len;	-> (syscall(Sysbind, a(sock), a(addr), a(len)) : int)}
const listen	= {sock, backlog;	-> (syscall(Syslisten, a(sock), a(backlog)) : int)}
const accept	= {sock, addr, lenp;	-> (syscall(Sysaccept, a(sock), a(addr), a(lenp)) : fd)}
const setsockopt	= {sock, lev, opt, val, len;	-> (syscall(Syssetsockopt, a(sock), a(lev), a(opt), a(val), a(len)) : int)}
const getsockopt	= {sock, lev, opt, val, len;	-> (syscall(Syssetsockopt, a(sock), a(lev), a(opt), a(val), a(len)) : int)}

/* memory mapping */
const munmap	= {addr, len;		-> syscall(Sysmunmap, a(addr), a(len))}
const mmap	= {addr, len, prot, flags, fd, off;
	-> (syscall(Sysmmap, a(addr), a(len), a(prot), a(flags), a(fd), a(off)) : byte#)
}

/* time */
const clock_getres = {clk, ts;	-> (syscall(Sysclock_getres, clockid(clk), a(ts)) : int32)}
const clock_gettime = {clk, ts;	-> (syscall(Sysclock_gettime, clockid(clk), a(ts)) : int32)}
const clock_settime = {clk, ts;	-> (syscall(Sysclock_settime, clockid(clk), a(ts)) : int32)}
const nanosleep	= {req, rem;	-> (syscall(Sysnanosleep, a(req), a(rem)) : int32)}

/* system information */
const uname	= {buf;	-> (syscall(Sysuname, buf) : int)}

const clockid = {clk
	match clk
	| `Clockrealtime:	-> 0
	| `Clockmonotonic:	-> 1
	| `Clockproccpu:	-> 2
	| `Clockthreadcpu:	-> 3
	| `Clockmonotonicraw:	-> 4
	| `Clockrealtimecoarse:	-> 5
	| `Clockmonotoniccoarse:-> 6
	| `Clockboottime:	-> 7
	| `Clockrealtimealarm:	-> 8
	| `Clockboottimealarm:	-> 9
	;;
	-> -1
}


const waitstatus = {st
	if st & 0x7f == 0 /* if exited */
		-> `Waitexit ((st & 0xff00) >> 8)
	elif ((st & 0xffff)-1) < 0xff /* if signaled */
		-> `Waitsig ((st) & 0x7f)
	elif (((st & 0xffff)*0x10001)>>8) > 0x7f00
		-> `Waitstop ((st & 0xff00) >> 8)
	;;
	-> `Waitfail st	/* wait failed to give a result */
}