Repository navigation
Expand file tree
/
Copy pathsystemfive.S
More file actions
471 lines (464 loc) Β· 17 KB
/
Copy pathsystemfive.S
File metadata and controls
471 lines (464 loc) Β· 17 KB
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
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-β
βvi: set et ft=asm ts=8 sw=8 fenc=utf-8 :viβ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β Copyright 2020 Justine Alexandra Roberts Tunney β
β β
β Permission to use, copy, modify, and/or distribute this software for β
β any purpose with or without fee is hereby granted, provided that the β
β above copyright notice and this permission notice appear in all copies. β
β β
β THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL β
β WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED β
β WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE β
β AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL β
β DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR β
β PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER β
β TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR β
β PERFORMANCE OF THIS SOFTWARE. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
#include "libc/dce.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/map.h"
#include "libc/macros.internal.h"
/* βββ
βββ βββββ
βββββ ββββββ
ββββββ ββββ βββββββ
ββββββ βββββββββββββββββββββββββ
βββββββββ βββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββ ββββ βββββ
ββββββββββββββββββββββββ βββ β βββ
ββββββββββββββββββββββ βββββ βββββ
ββββββββββββββββββββ βββββββββββββ
βββββββββββββββββ ββββββββββββββ
ββββββββββββββββββ ββββββββββββββ
ββββββββββββββββββββββββββββββββββ βββββ
βββββββββββββββββββββββββββββββββ βββββ β
ββββββββββββββββββββββββββββββ βββ βββββ
ββββββββββββββββββββββββββ βββββββββ β
βββββββββββββββββββ βββββββββ βββ ββββββ
βββββββββββββββ ββββββββββ ββββββββ
βββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββ besiyata
βββββββββββββββββββββββββββββ dishmaya
ββββββββββββββββββββββββββ
ββββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββββ
βββββββββββββββββββββββ
βββββββββββββββββββββββ
ββββββββββββββββββββββββ
βββββββββββββββββββββββββββ
ββββββββββββββ ββββββββββββββββββββββ ββββ
ββββββββββββββββββ ββββββββββββββββββββββββββββββ βββββ
βββββββ βββββββββββββββββββ ββββββββββββ
ββββ βββ ββββββββββββββββββββββββββββββββββ
ββββββββββββββββ ββββββββββββββββ
ββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β cosmopolitan Β§ bell system five Β» system call support ββ¬βββΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
.initbss 300,_init_systemfive
__hostos:
.quad 0
.endobj __hostos,globl,hidden
// Performs System Five System Call.
//
// Cosmopolitan is designed to delegate all function calls into the
// Linux, FreeBSD, OpenBSD, and XNU kernels via this function, with
// few exceptions. This function should generally only be called by
// generated thunks in the libc/sysv/syscalls/ directory.
//
// It's safe to call this function on Windows, where it will always
// return -1 with errno == ENOSYS. Further note that -1 is the only
// return value that means error, a common anti-pattern is to check
// for values less than 0 (which is more problematic on 32-bit).
//
// It is important to consider that system calls are one order of a
// magnitude more expensive than normal function calls. For example
// getpid() on Linux usually takes 500ns, and cached i/o calls will
// take 1Β΅s or more. So we don't need to inline them like Chromium.
//
// Another thing to consider is that BSDs only loosely follow the
// System Five ABI for the SYSCALL instruction. For example Linux
// always follows the six argument limit but the FreeBSD sendfile
// system call accepts a seventh argument that is passed on stack
// and OpenBSD modifies functions like mmap so that the sixth arg
// is passed on the stack. There's also the carry flag convention
// that XNU, FreeBSD, and OpenBSD inherited from 386BSD aka Jolix
//
// @param %rax function ordinal supplied by jump slot
// @param %rdi,%rsi,%rdx,%rcx,%r8,%r9 and rest on stack
// @return %rax:%rdx is result, or -1 w/ errno on error
// @clob %rcx,%r10,%r11
// @see syscalls.sh
__systemfive:
.quad 0
.endobj __systemfive,globl,hidden
.previous
.privileged
.Lanchorpoint:
#if SupportsLinux() || SupportsMetal()
systemfive_linux:
and $0xfff,%eax
cmp $0xfff,%eax
je systemfive_enosys # never taken branches cost nothing
mov %rcx,%r10 # syscall instruction clobbers %rcx
push %rbp # linux never reads args from stack
mov %rsp,%rbp # having frame will help backtraces
syscall # this is known as a context switch
pop %rbp # next we check to see if it failed
cmp $-4095,%rax # system five nexgen32e abi Β§ a.2.1
jae systemfive_error # encodes errno as neg return value
ret
.endfn systemfive_linux,globl,hidden
systemfive_error:
neg %eax
// π ππππ
.endfn systemfive_error,globl,hidden
#endif
systemfive_errno:
mov %eax,errno(%rip) # normalize to c library convention
push $-1 # negative one is only error result
pop %rax # the push pop is to save code size
ret
.endfn systemfive_errno,globl,hidden
systemfive_enosys:
mov ENOSYS(%rip),%eax
jmp systemfive_errno
.endfn systemfive_enosys,globl,hidden
#if SupportsNetbsd()
systemfive_netbsd:
shr $4*13,%rax
jmp systemfive_bsdscrub
.endfn systemfive_netbsd,globl,hidden
#endif
#if SupportsOpenbsd()
systemfive_openbsd:
shr $4*10,%rax
jmp systemfive_bsdscrub
.endfn systemfive_openbsd,globl,hidden
#endif
#if SupportsFreebsd()
systemfive_freebsd:
shr $4*7,%rax
movzwl %ax,%eax
// π ππππ
.endfn systemfive_freebsd,globl,hidden
#endif
#if SupportsBsd()
systemfive_bsdscrub:
and $0xfff,%eax
// π ππππ
.endfn systemfive_bsdscrub,globl,hidden
systemfive_bsd:
cmp $0xfff,%ax
je systemfive_enosys
mov %rcx,%r10 # note: we do not create a stack frame
syscall # bsd will need arg on stack sometimes
jc systemfive_errno # bsd sets carry flag if %rax is errno
ret
.endfn systemfive_bsd
#endif
#if SupportsXnu()
systemfive_xnu:
// 0x?????????2153??? # how syscalls.sh encodes xnu ordinals
// βββ΄β΄β
// β ββ¬β
// 0x0000000002000153 # how xnu wants ordinals to be encoded
mov %eax,%r11d
and $0x0f000000,%r11d
shl $8,%eax
shr $20,%eax
or %r11d,%eax
jmp systemfive_bsd
.endfn systemfive_xnu,globl,hidden
#endif
.previous
// Initializes System Five system call support.
//
// (1) Extracts parameters passed by kernel
// (2) Detects OS without issuing system calls
// (3) Unpacks magnums from libc/sysv/consts.sh
// (4) Replaces stack with one we control
//
// @param %r15 is auxv
// @note OpenBSD devs: let us know if you start using auxv
.init.start 300,_init_systemfive
push %rbx
push %rsi
mov (%rdi),%eax
#if SupportsOpenbsd()
cmpq $0,(%r15) # OpenBSD has no auxv
jnz 0f
mov $OPENBSD,%al
0:
#endif
#if SupportsNetbsd()
xor %ecx,%ecx
0: cmpq $2014,(%r15,%rcx,8) # NetBSD AT_EXECFN
jne 1f
mov $NETBSD,%al
1: cmpq $0,(%r15,%rcx,8)
lea 2(%ecx),%ecx
jnz 0b
2:
#endif
test %eax,%eax
jnz 1f
mov $LINUX,%al
1: stosq #β __hostos
bsr %eax,%eax
mov $_init_systemfive_jmptab,%ebx
movzbl (%rbx,%rax),%eax
lea (%rbx,%rax),%eax
jmp *%rax
_init_systemfive_jmptab:
.byte _init_systemfive_linux-_init_systemfive_jmptab
.byte _init_systemfive_metal-_init_systemfive_jmptab
.byte _init_systemfive_windows-_init_systemfive_jmptab
.byte _init_systemfive_xnu-_init_systemfive_jmptab
.byte _init_systemfive_openbsd-_init_systemfive_jmptab
.byte _init_systemfive_freebsd-_init_systemfive_jmptab
.byte _init_systemfive_netbsd-_init_systemfive_jmptab
.endobj _init_systemfive_jmptab
_init_systemfive_linux:
#if SupportsLinux()
pushb systemfive_linux-.Lanchorpoint
mov $syscon_linux,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_linux
_init_systemfive_metal:
#if SupportsMetal()
pushb systemfive_linux-.Lanchorpoint
mov $syscon_linux,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_metal
_init_systemfive_windows:
#if SupportsWindows()
pushb systemfive_enosys-.Lanchorpoint
mov $syscon_windows,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_windows
_init_systemfive_xnu:
#if SupportsXnu()
pushb systemfive_xnu-.Lanchorpoint
mov $syscon_xnu,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_xnu
_init_systemfive_openbsd:
#if SupportsOpenbsd()
pushb systemfive_openbsd-.Lanchorpoint
mov $syscon_openbsd,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_openbsd
_init_systemfive_freebsd:
#if SupportsFreebsd()
pushb systemfive_freebsd-.Lanchorpoint
mov $syscon_freebsd,%esi
jmp _init_systemfive_os
#endif
.endfn _init_systemfive_freebsd
_init_systemfive_netbsd:
#if SupportsNetbsd()
pushb systemfive_netbsd-.Lanchorpoint
mov $syscon_netbsd,%esi
// π ππππ
#endif
.endfn _init_systemfive_netbsd
_init_systemfive_os:
pop %rax
add $.Lanchorpoint,%eax
stosq #β __systemfive
// π ππππ
.endfn _init_systemfive_os
_init_systemfive_magnums:
push %rdi
mov $syscon_start,%edi
2: cmp $syscon_end,%edi
jnb 5f
xor %ebx,%ebx
xor %ecx,%ecx
xor %edx,%edx
3: lodsb # decodes uleb128
movzbl %al,%edx
and $127,%dl
shl %cl,%rdx
or %rdx,%rbx
add $7,%cl
test $128,%al
jnz 3b
xchg %rbx,%rax
stosq
jmp 2b
5: pop %rdi
pop %rsi
pop %rbx
// π ππππ
.endfn _init_systemfive_magnums
#if SupportsSystemv() && !defined(TINY)
_init_systemfive_stack: # determinism ftw!
#if SupportsWindows() || SupportsMetal()
testb $WINDOWS|METAL,__hostos(%rip)
jnz _init_systemfive_done
#endif
push %rdi
push %rsi
mov __NR_mmap,%eax
movabs $ape_stack_vaddr,%rdi
mov $ape_stack_memsz,%esi
mov $PROT_READ|PROT_WRITE,%edx
mov $MAP_PRIVATE|MAP_FIXED,%r10d
or MAP_ANONYMOUS,%r10d
or $-1,%r8d
xor %r9d,%r9d
push %rdi # vaddr of stack
push %rsi # size of stack
push %r9 # openbsd:pad
push %r9 # openbsd:align
#if SupportsOpenbsd()
testb IsOpenbsd()
jz 0f
syscall # openbsd:dubstack
jc 1f
mov __NR_mmap,%eax
#endif
0: or MAP_GROWSDOWN,%r10d # openbsd:mapstack
clc
syscall
pop %r9
pop %r9
pop %r9 # size of stack
pop %r11 # vaddr of stack
jnc 2f
1: mov %eax,%edi
mov __NR_exit_group,%eax
syscall
2: test %rax,%rax
js 1b
.weak _mmi
ezlea _mmi,cx
test %rcx,%rcx
push %r9 # save the stack size
jz 3f
lea -1(%r11,%r9),%r9 # need incl. interval
shr $16,%r11 # for the stack range
shr $16,%r9
movb $1,(%rcx) # _mmi.i
mov %r11d,8(%rcx) # _mmi.p[0].x
mov %r9d,12(%rcx) # _mmi.p[0].y
mov %edx,20(%rcx) # _mmi.p[0].prot
mov %r10d,24(%rcx) # _mmi.p[0].flags
3: pop %r9 # restore stack size
pop %rsi
pop %rdi
leave
pop %rcx
lea (%rax,%r9),%rsp
sub $ape_stack_align,%rsp # openbsd:stackbound
mov %rbp,(%rsp)
push %rcx
push %rbp
mov %rsp,%rbp
// π ππππ
_init_systemfive_syscall:
mov __NR_msyscall,%eax # syscall origin protect
cmp $0xfff,%ax # openbsd is pretty cool
jae _init_systemfive_done
push %rdi
push %rsi
.weak __privileged_addr
.weak __privileged_size
mov $__privileged_addr,%edi
mov $__privileged_size,%esi
syscall
pop %rsi
pop %rdi
// π ππππ
#endif /* TINY */
_init_systemfive_done:
nop
.init.end 300,_init_systemfive,globl,hidden
// Sections for varint encoded magic numbers.
//
// These sections are all ordered by (group_name, constant_name).
// They're populated by modules simply referencing the symbols.
//
// @see libc/sysv/consts.sh
// @see libc/sysv/consts/syscon_h
.section .piro.bss.sort.syscon.1,"aw",@nobits
.align 8
syscon_start:/*
...decentralized quadwords...
*/.previous
.section .piro.bss.sort.syscon.3,"aw",@nobits
syscon_end:
.previous
.type syscon_start,@object
.type syscon_end,@object
.globl syscon_start
.globl syscon_end
#if SupportsLinux() || SupportsMetal()
.section .sort.rodata.syscon.linux.1,"a",@progbits
.align 1
syscon_linux:/*
...decentralized leb128...
*/.previous
.type syscon_linux,@object
.globl syscon_linux
#endif
#if SupportsXnu()
.section .sort.rodata.syscon.xnu.1,"a",@progbits
.align 1
syscon_xnu:/*
...decentralized leb128...
*/.previous
.type syscon_xnu,@object
.globl syscon_xnu
#endif
#if SupportsFreebsd()
.section .sort.rodata.syscon.freebsd.1,"a",@progbits
.align 1
syscon_freebsd:/*
...decentralized leb128...
*/.previous
.type syscon_freebsd,@object
.globl syscon_freebsd
#endif
#if SupportsOpenbsd()
.section .sort.rodata.syscon.openbsd.1,"a",@progbits
.align 1
syscon_openbsd:/*
...decentralized leb128...
*/.previous
.type syscon_openbsd,@object
.globl syscon_openbsd
#endif
#if SupportsNetbsd()
.section .sort.rodata.syscon.netbsd.1,"a",@progbits
.align 1
syscon_netbsd:/*
...decentralized leb128...
*/.previous
.type syscon_netbsd,@object
.globl syscon_netbsd
#endif
#if SupportsWindows()
.section .sort.rodata.syscon.windows.1,"a",@progbits
.align 1
syscon_windows:/*
...decentralized leb128...
*/.previous
.type syscon_windows,@object
.globl syscon_windows
#endif
.weak ape_stack_vaddr
.weak ape_stack_memsz
.weak ape_stack_align