cosmopolitan/libc/sysv/systemfive.S

460 lines
16 KiB
ArmAsm
Raw Normal View History

/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
2020-06-15 14:18:57 +00:00
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
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.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
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.
2020-06-15 14:18:57 +00:00
*/
#include "libc/dce.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/map.h"
2020-06-15 14:18:57 +00:00
#include "libc/macros.h"
/*
besiyata
dishmaya
cosmopolitan § bell system five » system call support
*/
.initbss 300,_init_systemfive
__hostos:
.quad 0
.endobj __hostos,globl,hidden
2020-06-15 14:18:57 +00:00
/ 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
2020-06-15 14:18:57 +00:00
/ 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
2020-06-15 14:18:57 +00:00
/ 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
2020-06-15 14:18:57 +00:00
/ 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
2020-06-15 14:18:57 +00:00
/
/ @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
2020-06-15 14:18:57 +00:00
.previous
.privileged
2020-06-15 14:18:57 +00:00
.Lanchorpoint:
2021-02-08 12:04:42 +00:00
#if SupportsLinux()
2020-06-15 14:18:57 +00:00
systemfive.linux:
2021-02-05 14:16:20 +00:00
and $0xfff,%eax
cmp $0xfff,%eax
je systemfive.enosys
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
2020-06-15 14:18:57 +00:00
ret
.endfn systemfive.linux,globl,hidden
2020-06-15 14:18:57 +00:00
systemfive.error:
neg %eax
/ 𝑠𝑙𝑖𝑑𝑒
2020-10-27 10:39:46 +00:00
.endfn systemfive.error,globl,hidden
2021-02-08 12:04:42 +00:00
#endif
2020-06-15 14:18:57 +00:00
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
2020-06-15 14:18:57 +00:00
ret
2020-10-27 10:39:46 +00:00
.endfn systemfive.errno,globl,hidden
2020-06-15 14:18:57 +00:00
systemfive.enosys:
mov ENOSYS(%rip),%eax
jmp systemfive.errno
2020-10-27 10:39:46 +00:00
.endfn systemfive.enosys,globl,hidden
2021-02-08 12:04:42 +00:00
#if SupportsNetbsd()
2021-02-05 14:16:20 +00:00
systemfive.netbsd:
shr $4*13,%rax
jmp systemfive.bsdscrub
.endfn systemfive.openbsd,globl,hidden
2021-02-08 12:04:42 +00:00
#endif
#if SupportsOpenbsd()
2020-06-15 14:18:57 +00:00
systemfive.openbsd:
2021-02-05 14:16:20 +00:00
shr $4*10,%rax
jmp systemfive.bsdscrub
.endfn systemfive.openbsd,globl,hidden
2021-02-08 12:04:42 +00:00
#endif
#if SupportsFreebsd()
2020-06-15 14:18:57 +00:00
systemfive.freebsd:
2021-02-05 14:16:20 +00:00
shr $4*7,%rax
2020-06-15 14:18:57 +00:00
movzwl %ax,%eax
/ 𝑠𝑙𝑖𝑑𝑒
.endfn systemfive.freebsd,globl,hidden
2021-02-08 12:04:42 +00:00
#endif
#if SupportsBsd()
2021-02-05 14:16:20 +00:00
systemfive.bsdscrub:
and $0xfff,%eax
/ 𝑠𝑙𝑖𝑑𝑒
.endfn systemfive.bsdscrub,globl,hidden
2020-06-15 14:18:57 +00:00
systemfive.bsd:
cmp $0xfff,%ax
2021-02-05 14:16:20 +00:00
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
2020-06-15 14:18:57 +00:00
ret
.endfn systemfive.bsd
2021-02-08 12:04:42 +00:00
#endif
#if SupportsXnu()
2020-06-15 14:18:57 +00:00
systemfive.xnu:
2021-02-05 14:16:20 +00:00
/ 0x?????????2153??? # how syscalls.sh encodes xnu ordinals
/
/
/ 0x0000000002000153 # how xnu wants ordinals to be encoded
2020-06-15 14:18:57 +00:00
mov %eax,%r11d
2021-02-05 14:16:20 +00:00
and $0x0f000000,%r11d
shl $8,%eax
shr $20,%eax
2020-06-15 14:18:57 +00:00
or %r11d,%eax
jmp systemfive.bsd
.endfn systemfive.xnu,globl,hidden
2021-02-08 12:04:42 +00:00
#endif
2020-06-15 14:18:57 +00:00
.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
2020-06-15 14:18:57 +00:00
/
/ @param %r15 is auxv
/ @note OpenBSD devs: let us know if you start using auxv
.init.start 300,_init_systemfive
push %rbx
push %rsi
2021-02-08 12:04:42 +00:00
#if SupportsMetal()
testb $METAL,(%rdi) # @see ape/ape.S
jnz systemfive.init.metal
2021-02-08 12:04:42 +00:00
#endif
#if SupportsXnu()
2020-06-15 14:18:57 +00:00
testb $XNU,(%rdi) # @see libc/crt/crt.S
jnz systemfive.init.xnu
2021-02-08 12:04:42 +00:00
#endif
#if SupportsFreebsd()
2020-06-15 14:18:57 +00:00
testb $FREEBSD,(%rdi) # @see libc/crt/crt.S
jnz systemfive.init.freebsd
2021-02-08 12:04:42 +00:00
#endif
#if SupportsWindows()
2020-06-15 14:18:57 +00:00
testb $WINDOWS,(%rdi) # @see libc/runtime/winmain.c
jnz systemfive.init.windows
2021-02-08 12:04:42 +00:00
#endif
#if SupportsOpenbsd()
2020-06-15 14:18:57 +00:00
cmpq $0,(%r15) # OpenBSD doesn't have auxv
je systemfive.init.openbsd
2021-02-08 12:04:42 +00:00
#endif
#if SupportsNetbsd()
2021-02-05 14:16:20 +00:00
xor %eax,%eax
0: cmpq $2014,(%r15,%rax,8) # NetBSD's distinctive AT_EXECFN
je systemfive.init.netbsd
cmpq $0,(%r15,%rax,8)
lea 2(%eax),%eax
jnz 0b
2021-02-08 12:04:42 +00:00
#endif
#if SupportsLinux()
systemfive.init.linux:
2020-06-15 14:18:57 +00:00
pushb systemfive.linux-.Lanchorpoint
push $LINUX
ezlea syscon.linux,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsMetal()
systemfive.init.metal:
pushb systemfive.linux-.Lanchorpoint
push $METAL
ezlea syscon.linux,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsWindows()
systemfive.init.windows:
2020-06-15 14:18:57 +00:00
pushb systemfive.enosys-.Lanchorpoint
push $WINDOWS
ezlea syscon.windows,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsFreebsd()
systemfive.init.freebsd:
2020-06-15 14:18:57 +00:00
pushb systemfive.freebsd-.Lanchorpoint
push $FREEBSD
ezlea syscon.freebsd,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsOpenbsd()
systemfive.init.openbsd:
2020-06-15 14:18:57 +00:00
pushb systemfive.openbsd-.Lanchorpoint
push $OPENBSD
ezlea syscon.openbsd,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsNetbsd()
2021-02-05 14:16:20 +00:00
systemfive.init.netbsd:
pushb systemfive.netbsd-.Lanchorpoint
push $NETBSD
ezlea syscon.netbsd,si
jmp systemfive.init.os
2021-02-08 12:04:42 +00:00
#endif
#if SupportsXnu()
systemfive.init.xnu:
2020-06-15 14:18:57 +00:00
pushb systemfive.xnu-.Lanchorpoint
push $XNU
ezlea syscon.xnu,si
/ 𝑠𝑙𝑖𝑑𝑒
2021-02-08 12:04:42 +00:00
#endif
systemfive.init.os:
2020-06-15 14:18:57 +00:00
ezlea .Lanchorpoint,cx
pop %rax
stosq # __hostos
2020-06-15 14:18:57 +00:00
pop %rax
add %rcx,%rax
stosq # __systemfive
/ 𝑠𝑙𝑖𝑑𝑒
systemfive.init.magnums:
2020-06-15 14:18:57 +00:00
push %rdi
ezlea syscon.start,di
ezlea syscon.end,bx
or $-1,%r9
2020-06-15 14:18:57 +00:00
2: cmp %rbx,%rdi
jnb 5f
xor %ecx,%ecx
xor %edx,%edx
3: lodsb # decodes sleb128
2020-06-15 14:18:57 +00:00
mov %rax,%r8
and $127,%r8d
sal %cl,%r8
add $7,%ecx
or %r8,%rdx
test %al,%al
js 3b
test $64,%al
je 4f
mov %r9,%rax
sal %cl,%rax
or %rax,%rdx
4: mov %rdx,%rax
cmpq $0,(%rdi) # dont change if set
cmovne (%rdi),%rax # @see WinMain()
2020-06-15 14:18:57 +00:00
stosq
jmp 2b
5: pop %rdi
pop %rsi
pop %rbx
/ 𝑠𝑙𝑖𝑑𝑒
2021-02-08 12:04:42 +00:00
#if SupportsSystemv() && !defined(TINY)
systemfive.init.stack: # determinism ftw!
2021-02-08 12:04:42 +00:00
#if SupportsWindows()
testb IsWindows() # already did this
jnz systemfive.init.done
2021-02-08 12:04:42 +00:00
#endif
push %rdi
push %rsi
mov __NR_mmap,%eax
mov $0x700000000000-STACKSIZE,%rdi
mov $STACKSIZE,%esi
mov $PROT_READ|PROT_WRITE,%edx
mov $MAP_PRIVATE|MAP_FIXED,%r10d
or MAP_ANONYMOUS,%r10d
or $-1,%r8d
xor %r9d,%r9d
push %r9 # openbsd:pad
push %r9 # openbsd:align
2021-02-08 12:04:42 +00:00
#if SupportsOpenbsd()
testb IsOpenbsd()
jz 0f
syscall # openbsd:dubstack
jc 1f
mov __NR_mmap,%eax
2021-02-08 12:04:42 +00:00
#endif
0: or MAP_GROWSDOWN,%r10d # openbsd:mapstack
clc
syscall
pop %r9
pop %r9
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
jz 3f
movb $1,(%rcx) # _mmi.i
movl $(0x700000000000-STACKSIZE)>>16,8(%rcx) # _mmi.p[0].x
movl $(0x700000000000-1)>>16,12(%rcx) # _mmi.p[0].y
mov %edx,20(%rcx) # _mmi.p[0].prot
mov %r10d,24(%rcx) # _mmi.p[0].flags
3: pop %rsi
pop %rdi
leave
pop %rcx
lea STACKSIZE-16(%rax),%rsp # openbsd:stackbound
2021-02-08 12:04:42 +00:00
mov %rbp,(%rsp)
push %rcx
push %rbp
mov %rsp,%rbp
/ 𝑠𝑙𝑖𝑑𝑒
systemfive.init.syscall:
mov __NR_msyscall,%eax # syscall origin protect
test %eax,%eax # openbsd is pretty cool
js systemfive.init.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 */
systemfive.init.done:
nop
.init.end 300,_init_systemfive,globl,hidden
2020-06-15 14:18:57 +00:00
/ Sections for varint encoded magic numbers.
2020-06-15 14:18:57 +00:00
/
/ 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:/*
2020-06-15 14:18:57 +00:00
...decentralized quadwords...
*/.previous
.section .piro.bss.sort.syscon.3,"aw",@nobits
syscon.end:
2020-06-15 14:18:57 +00:00
.previous
2021-02-08 12:04:42 +00:00
.type syscon.start,@object
.type syscon.end,@object
.globl syscon.start
.globl syscon.end
#if SupportsLinux()
2020-06-15 14:18:57 +00:00
.section .sort.rodata.syscon.linux.1,"a",@progbits
.align 1
syscon.linux:/*
...decentralized leb128...
*/.previous
2021-02-08 12:04:42 +00:00
.type syscon.linux,@object
.globl syscon.linux
#endif
#if SupportsXnu()
2020-06-15 14:18:57 +00:00
.section .sort.rodata.syscon.xnu.1,"a",@progbits
.align 1
syscon.xnu:/*
...decentralized leb128...
*/.previous
2021-02-08 12:04:42 +00:00
.type syscon.xnu,@object
.globl syscon.xnu
#endif
#if SupportsFreebsd()
2020-06-15 14:18:57 +00:00
.section .sort.rodata.syscon.freebsd.1,"a",@progbits
.align 1
syscon.freebsd:/*
...decentralized leb128...
*/.previous
2021-02-08 12:04:42 +00:00
.type syscon.freebsd,@object
.globl syscon.freebsd
#endif
#if SupportsOpenbsd()
2020-06-15 14:18:57 +00:00
.section .sort.rodata.syscon.openbsd.1,"a",@progbits
.align 1
syscon.openbsd:/*
2021-02-05 14:16:20 +00:00
...decentralized leb128...
*/.previous
2021-02-08 12:04:42 +00:00
.type syscon.openbsd,@object
.globl syscon.openbsd
#endif
#if SupportsNetbsd()
2021-02-05 14:16:20 +00:00
.section .sort.rodata.syscon.netbsd.1,"a",@progbits
.align 1
syscon.netbsd:/*
2020-06-15 14:18:57 +00:00
...decentralized leb128...
*/.previous
2021-02-08 12:04:42 +00:00
.type syscon.netbsd,@object
.globl syscon.netbsd
#endif
#if SupportsWindows()
2020-06-15 14:18:57 +00:00
.section .sort.rodata.syscon.windows.1,"a",@progbits
.align 1
syscon.windows:/*
...decentralized leb128...
*/.previous
.type syscon.windows,@object
.globl syscon.windows
2021-02-08 12:04:42 +00:00
#endif