summaryrefslogtreecommitdiff
path: root/lib/std/sys+linux-x64.myr
blob: 48601208a5e6237f17ab848b12e9d2a2eb4962f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
use "systypes.use"

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 epollflags	= uint32
	type epollop	= uint32
	type epollevttype	= uint32

	type pollevt	= uint16

	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_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 */

	/* 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	= 0
	const Epollctlmod	: epollop	= 1
	const Epollctldel	: epollop	= 1

	/* 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

	/* 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 castto(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)
	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 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 : int32, val : int32, \
		ts : timespec#, uaddr2 : int#, val3 : int# -> 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)

	/* 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 sleep	: (time : uint64 -> 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 castto(int64).
*/
generic a = {x : @t; -> x castto(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) castto(pid)}
const kill	= {pid, sig;		-> syscall(Syskill, a(pid), a(sig))}
const fork	= {;			-> syscall(Sysfork) castto(pid)}
const clone	= {flags, stk, ptid, ctid, ptreg;	-> syscall(Sysclone, a(flags), a(stk), a(ptid), a(ctid), a(ptreg)) castto(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 castto(byte##))[:args.len + 1]
	for i = 0; i < args.len; i++
		cargs[i] = cstring(args[i])
	;;
	cargs[args.len] = 0 castto(byte#)
	-> syscall(Sysexecve, cstring(cmd), a(p), a(__cenvp))
}

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

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

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

	-> syscall(Sysexecve, cstring(cmd), a(p), a(cenv))
}

/* file manipulation */
const open	= {path, opts;		-> syscall(Sysopen, cstring(path), a(opts), a(0o777)) castto(fd)}
const openmode	= {path, opts, mode;	-> syscall(Sysopen, cstring(path), a(opts), a(mode)) castto(fd)}
const close	= {fd;			-> syscall(Sysclose, a(fd))}
const creat	= {path, mode;		-> syscall(Syscreat, cstring(path), a(mode)) castto(fd)}
const unlink	= {path;		-> syscall(Sysunlink, cstring(path)) castto(int)}
const read	= {fd, buf;		-> syscall(Sysread, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
const write	= {fd, buf;		-> syscall(Syswrite, a(fd), buf castto(byte#), a(buf.len)) castto(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)) castto(int64)}
generic ioctl	= {fd, req, arg;	-> syscall(Sysioctl, a(fd), a(req), a(arg)) castto(int64)}
const getdents64	= {fd, buf;	-> syscall(Sysgetdents64, a(fd), buf castto(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)) castto(fd)}
const dup2 	= {src, dst;	-> syscall(Sysdup2, a(src), a(dst)) castto(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 castto(pollfd#), a(pfd.len), a(timeout)) castto(int)}
const epollctl	= {epfd, op, fd, evt;
	-> syscall(Sysepoll_ctl, a(epfd), a(op), a(fd), a(evt)) castto(int)}
const epollwait	= {epfd, evts, timeout;
	-> syscall(Sysepoll_wait, a(epfd), evts castto(epollevt#), a(evts.len), a(timeout)) castto(int)}
const epollcreate	= {flg;	-> syscall(Sysepoll_create1, a(flg)) castto(fd)}

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

/* 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)) castto(byte#)
}

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

const sleep = {time
	var req, rem
	req = [.sec = time, .nsec = 0]
	-> nanosleep(&req, &rem)
}

const nanosleep	= {req, rem;	-> syscall(Sysnanosleep, a(req), a(rem)) castto(int32)}

/* system information */
const uname	= {buf;	-> syscall(Sysuname, buf) castto(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 */
}