2020-06-16 13:38:43 +00:00
|
|
|
|
/*-*- 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"
|
2021-02-01 11:33:13 +00:00
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
|
#include "libc/sysv/consts/nr.h"
|
|
|
|
|
#include "libc/sysv/consts/map.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
|
#include "libc/macros.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
|
#include "libc/nexgen32e/macros.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
|
|
/* ▄▄▄
|
|
|
|
|
▄▄▄ ▀▓▓▒▄
|
|
|
|
|
▄▓▒▒░ ▀▓▒▒▒▄
|
|
|
|
|
▄▓▓▓▒▀ ▄▄▄▄ ▒▓▒▒░▒▄
|
|
|
|
|
▄▓▓▓▒▓ ▄▄▓██▓▓▓▓▒▒▒▒▓▓▄▄▓▓▒▒▒░░▒
|
|
|
|
|
▓▓▓▓▒▒▒▄▄ ░▒█▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓▒░░▒░
|
|
|
|
|
██▓▓▓▒▒░░▒▒▒▒▓▓▓▓▓▓▒▓▒░▒▒░▀▒▒▒▒░▀░▒▒▒░▒
|
|
|
|
|
▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▒▓▓▒▒▒░▒▒░░ ░▒▒░ ░▒▒▒▒
|
|
|
|
|
▀▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒░░▒░░ ░▒▒ ░ ▀▒▒
|
|
|
|
|
▀▓▓█▓▓▓▓▓▓▓▓▓▓▒▒░░▒▒░░ ░░░▓░ ▓░░░▒
|
|
|
|
|
▀▀█▓███▓▓▓▓▓▒▒░░░▒░░ ░█▓░█▓░█▓▓▄▒░
|
|
|
|
|
░▓██▓▓▓▓▓▒▒░░░▒░░ ░████▓▒▓█▓▀░▀▄
|
|
|
|
|
░▓██▓▓▓▓▓▒▒▒░░░▒░░ ▒██▓▒▒▒▒▒▒░░░▒
|
|
|
|
|
████▓▓▓▓▓▒▒▒▒▒▒▒▒▒░░▒▓▓▒░░░░▒░░░▒░ ░░░░░
|
|
|
|
|
░▓███▓▓▓▓▓▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒░░░ ░░░░░ ░
|
|
|
|
|
▓███▓▓▓▓▓▒▓▒▒▒▒░░░░░░░░░▒▓▒▒░▀ ░░░ ░░░░░
|
|
|
|
|
▀▒██▓▓▓▓▒▒▒▓▓▓▓▒▒▒▒▒▒▒▓▀▀░ ░░░░░░░░░ ░
|
|
|
|
|
▓▓▓▓▓▓▓▒▓▒▒▒▒▓▓▓▒▀░ ░░░░░▄░░░ ░░░ ░░░░░░
|
|
|
|
|
▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓ █▓▒░░▒░░░░ ░░░░░░░░
|
|
|
|
|
▄▓▓▓▒▒▒▒▒░░░░░░░▒▄▄▄░▒▓▓▒▒░▀░
|
|
|
|
|
░▓█▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒░░░▒ besiyata
|
|
|
|
|
▓▓█▓▓▒▓▓▓▒▒▒░░░░░░▒▓▓▓▓▒▒▒▒▒░ dishmaya
|
|
|
|
|
▓▓█▓▓▓▓▓▓▒▒▒░░░░░░░▒▓▓▒▀▀▀
|
|
|
|
|
▓▓██▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▀
|
|
|
|
|
█▓▓█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▀
|
|
|
|
|
▒▓▓▓▓▀░░▒▓▓▓▓▓▓▓▓▒▒░░▒
|
|
|
|
|
▄▓▓▀░░░▄▓▓▓▓▒▒▒▒▒░░░░▄░
|
|
|
|
|
▄███▄▄▓▓▓▓▓▓▓▒▒▒▒▒░░▒▒░
|
|
|
|
|
▄▓▓▓█▓█▓▓███▓▓▓▓▓▓▓▓▓▓▓░
|
|
|
|
|
▄░▓▓▓▓▓▓▀▒▓▓▓▒▒▓▒░░░▒▓▒░░░▓
|
|
|
|
|
▄▄▄░▒▓▓▓▓▓▓░▀▀ ▓▓▒░▓▒▒▒▒▒▒▒▒▒▒▄░░▀▀░░ ▄▄▄▄
|
|
|
|
|
▄▄▄▒▒▓▓█▓▓▓▓▓▀▀▀▀▀ ▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▀░░▀░░▒▒▒░░░ ░░░░░
|
|
|
|
|
▄▓▓▓▒▀▀ ▓▒▓▓▓▓▓▒▒▒▒▒▒▒▒▓░░░ ▒▒▒░░░░░░░░▒
|
|
|
|
|
█▓▓▒ ▄▄▄ ▀▓▒▓▒▒▒▓▓▓▓▓▓▒▒▒░░░░░░░░░▒▒░░░░░░░
|
|
|
|
|
▀▓▓▓▓▒▄▄▒▒▒▒▒▒▄▄ ▀▀▀▀░░▒▒▒▒░░░░░░
|
|
|
|
|
▀▀▀▓▓▓▓▒▒▒▒▒▓▓▄▄
|
|
|
|
|
╔────────────────────────────────────────────────────────────────────────────│─╗
|
|
|
|
|
│ cosmopolitan § bell system five » system call support ─╬─│┼
|
|
|
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
|
|
2020-12-27 15:02:35 +00:00
|
|
|
|
.initbss 300,_init_systemfive
|
|
|
|
|
__hostos:
|
|
|
|
|
.quad 0
|
|
|
|
|
.endobj __hostos,globl,hidden
|
|
|
|
|
|
2021-02-08 17:19:00 +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
|
|
|
|
|
// 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
|
2020-11-25 16:19:00 +00:00
|
|
|
|
__systemfive:
|
|
|
|
|
.quad 0
|
|
|
|
|
.endobj __systemfive,globl,hidden
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.previous
|
2021-02-03 04:21:06 +00:00
|
|
|
|
|
2021-02-06 11:17:41 +00:00
|
|
|
|
.privileged
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.Lanchorpoint:
|
2021-02-24 04:23:19 +00:00
|
|
|
|
#if SupportsLinux() || SupportsMetal()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_linux:
|
2021-02-05 14:16:20 +00:00
|
|
|
|
and $0xfff,%eax
|
|
|
|
|
cmp $0xfff,%eax
|
2021-05-14 12:36:58 +00:00
|
|
|
|
je systemfive_enosys # never taken branches cost nothing
|
2021-01-25 21:08:05 +00:00
|
|
|
|
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
|
2021-05-14 12:36:58 +00:00
|
|
|
|
cmp $-4095,%rax # system five nexgen32e abi § a.2.1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jae systemfive_error # encodes errno as neg return value
|
2020-06-15 14:18:57 +00:00
|
|
|
|
ret
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.endfn systemfive_linux,globl,hidden
|
|
|
|
|
systemfive_error:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
neg %eax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
|
|
|
|
.endfn systemfive_error,globl,hidden
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_errno:
|
2021-01-25 21:08:05 +00:00
|
|
|
|
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
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.endfn systemfive_errno,globl,hidden
|
|
|
|
|
systemfive_enosys:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
mov ENOSYS(%rip),%eax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp systemfive_errno
|
|
|
|
|
.endfn systemfive_enosys,globl,hidden
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#if SupportsNetbsd()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_netbsd:
|
2021-02-05 14:16:20 +00:00
|
|
|
|
shr $4*13,%rax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp systemfive_bsdscrub
|
2021-02-11 16:37:18 +00:00
|
|
|
|
.endfn systemfive_netbsd,globl,hidden
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsOpenbsd()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_openbsd:
|
2021-02-05 14:16:20 +00:00
|
|
|
|
shr $4*10,%rax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp systemfive_bsdscrub
|
|
|
|
|
.endfn systemfive_openbsd,globl,hidden
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsFreebsd()
|
2021-02-08 17:19:00 +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
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
|
|
|
|
.endfn systemfive_freebsd,globl,hidden
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsBsd()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_bsdscrub:
|
2021-02-05 14:16:20 +00:00
|
|
|
|
and $0xfff,%eax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
|
|
|
|
.endfn systemfive_bsdscrub,globl,hidden
|
|
|
|
|
systemfive_bsd:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
cmp $0xfff,%ax
|
2021-02-08 17:19:00 +00:00
|
|
|
|
je systemfive_enosys
|
2021-01-25 21:08:05 +00:00
|
|
|
|
mov %rcx,%r10 # note: we do not create a stack frame
|
|
|
|
|
syscall # bsd will need arg on stack sometimes
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jc systemfive_errno # bsd sets carry flag if %rax is errno
|
2020-06-15 14:18:57 +00:00
|
|
|
|
ret
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.endfn systemfive_bsd
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsXnu()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
systemfive_xnu:
|
|
|
|
|
// 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
|
2021-02-08 17:19:00 +00:00
|
|
|
|
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
|
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 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
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.init.start 300,_init_systemfive
|
|
|
|
|
push %rbx
|
|
|
|
|
push %rsi
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov (%rdi),%eax
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#if SupportsOpenbsd()
|
2021-02-24 12:00:38 +00:00
|
|
|
|
cmpq $0,(%r15) # OpenBSD has no auxv
|
|
|
|
|
jnz 0f
|
|
|
|
|
mov $OPENBSD,%al
|
|
|
|
|
0:
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsNetbsd()
|
2021-02-24 12:00:38 +00:00
|
|
|
|
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
|
2021-02-05 14:16:20 +00:00
|
|
|
|
jnz 0b
|
2021-02-24 12:00:38 +00:00
|
|
|
|
2:
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
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
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_linux:
|
2021-02-24 12:00:38 +00:00
|
|
|
|
#if SupportsLinux()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
pushb systemfive_linux-.Lanchorpoint
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov $syscon_linux,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_linux
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_metal:
|
2021-02-24 12:00:38 +00:00
|
|
|
|
#if SupportsMetal()
|
2021-09-28 05:58:51 +00:00
|
|
|
|
pushb systemfive_enosys-.Lanchorpoint
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov $syscon_linux,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_metal
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_windows:
|
2021-02-24 12:00:38 +00:00
|
|
|
|
#if SupportsWindows()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
pushb systemfive_enosys-.Lanchorpoint
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov $syscon_windows,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_windows
|
|
|
|
|
_init_systemfive_xnu:
|
|
|
|
|
#if SupportsXnu()
|
|
|
|
|
pushb systemfive_xnu-.Lanchorpoint
|
|
|
|
|
mov $syscon_xnu,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_xnu
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_openbsd:
|
2021-02-24 12:00:38 +00:00
|
|
|
|
#if SupportsOpenbsd()
|
2021-02-08 17:19:00 +00:00
|
|
|
|
pushb systemfive_openbsd-.Lanchorpoint
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov $syscon_openbsd,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_openbsd
|
|
|
|
|
_init_systemfive_freebsd:
|
|
|
|
|
#if SupportsFreebsd()
|
|
|
|
|
pushb systemfive_freebsd-.Lanchorpoint
|
|
|
|
|
mov $syscon_freebsd,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jmp _init_systemfive_os
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_freebsd
|
|
|
|
|
_init_systemfive_netbsd:
|
|
|
|
|
#if SupportsNetbsd()
|
|
|
|
|
pushb systemfive_netbsd-.Lanchorpoint
|
|
|
|
|
mov $syscon_netbsd,%esi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_netbsd
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_os:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
pop %rax
|
2021-02-24 12:00:38 +00:00
|
|
|
|
add $.Lanchorpoint,%eax
|
2020-11-25 16:19:00 +00:00
|
|
|
|
stosq #→ __systemfive
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_os
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_magnums:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
push %rdi
|
2021-02-24 12:00:38 +00:00
|
|
|
|
mov $syscon_start,%edi
|
|
|
|
|
2: cmp $syscon_end,%edi
|
2020-06-15 14:18:57 +00:00
|
|
|
|
jnb 5f
|
2021-02-24 12:00:38 +00:00
|
|
|
|
xor %ebx,%ebx
|
2020-06-15 14:18:57 +00:00
|
|
|
|
xor %ecx,%ecx
|
|
|
|
|
xor %edx,%edx
|
2021-02-24 12:00:38 +00:00
|
|
|
|
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
|
2020-06-15 14:18:57 +00:00
|
|
|
|
stosq
|
|
|
|
|
jmp 2b
|
2021-02-01 11:33:13 +00:00
|
|
|
|
5: pop %rdi
|
|
|
|
|
pop %rsi
|
|
|
|
|
pop %rbx
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
2021-02-24 12:00:38 +00:00
|
|
|
|
.endfn _init_systemfive_magnums
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#if SupportsSystemv() && !defined(TINY)
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_stack: # determinism ftw!
|
2021-02-24 04:23:19 +00:00
|
|
|
|
#if SupportsWindows() || SupportsMetal()
|
|
|
|
|
testb $WINDOWS|METAL,__hostos(%rip)
|
2021-02-08 17:19:00 +00:00
|
|
|
|
jnz _init_systemfive_done
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-01 11:33:13 +00:00
|
|
|
|
push %rdi
|
|
|
|
|
push %rsi
|
|
|
|
|
mov __NR_mmap,%eax
|
2021-02-24 04:23:19 +00:00
|
|
|
|
movabs $ape_stack_vaddr,%rdi
|
|
|
|
|
mov $ape_stack_memsz,%esi
|
2021-02-01 11:33:13 +00:00
|
|
|
|
mov $PROT_READ|PROT_WRITE,%edx
|
|
|
|
|
mov $MAP_PRIVATE|MAP_FIXED,%r10d
|
|
|
|
|
or MAP_ANONYMOUS,%r10d
|
2021-02-03 04:21:06 +00:00
|
|
|
|
or $-1,%r8d
|
2021-02-01 11:33:13 +00:00
|
|
|
|
xor %r9d,%r9d
|
2021-02-24 04:23:19 +00:00
|
|
|
|
push %rdi # vaddr of stack
|
|
|
|
|
push %rsi # size of stack
|
2021-02-01 11:33:13 +00:00
|
|
|
|
push %r9 # openbsd:pad
|
2021-02-03 04:21:06 +00:00
|
|
|
|
push %r9 # openbsd:align
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#if SupportsOpenbsd()
|
2021-02-03 04:21:06 +00:00
|
|
|
|
testb IsOpenbsd()
|
|
|
|
|
jz 0f
|
|
|
|
|
syscall # openbsd:dubstack
|
|
|
|
|
jc 1f
|
|
|
|
|
mov __NR_mmap,%eax
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-03 04:21:06 +00:00
|
|
|
|
0: or MAP_GROWSDOWN,%r10d # openbsd:mapstack
|
|
|
|
|
clc
|
2021-02-01 11:33:13 +00:00
|
|
|
|
syscall
|
|
|
|
|
pop %r9
|
2021-02-03 04:21:06 +00:00
|
|
|
|
pop %r9
|
2021-02-24 04:23:19 +00:00
|
|
|
|
pop %r9 # size of stack
|
|
|
|
|
pop %r11 # vaddr of stack
|
2021-02-01 11:33:13 +00:00
|
|
|
|
jnc 2f
|
|
|
|
|
1: mov %eax,%edi
|
|
|
|
|
mov __NR_exit_group,%eax
|
|
|
|
|
syscall
|
|
|
|
|
2: test %rax,%rax
|
|
|
|
|
js 1b
|
2021-10-14 00:27:13 +00:00
|
|
|
|
|
|
|
|
|
// prevent operating system from auto-mapping stack
|
|
|
|
|
// we guarantee stack overflows are always detected
|
|
|
|
|
// so long as you never use -DSTACK_FRAME_UNLIMITED
|
|
|
|
|
// TODO: Why does this fail sometimes with FreeBSD?
|
|
|
|
|
testb IsFreebsd()
|
|
|
|
|
jnz 9f
|
|
|
|
|
push %rax
|
|
|
|
|
push %rdx
|
|
|
|
|
push %r11
|
|
|
|
|
mov __NR_mprotect,%eax
|
|
|
|
|
mov $PAGESIZE,%esi
|
|
|
|
|
xor %edx,%edx # PROT_NONE
|
|
|
|
|
syscall
|
|
|
|
|
pop %r11
|
|
|
|
|
pop %rdx
|
|
|
|
|
pop %rax
|
|
|
|
|
9:
|
|
|
|
|
|
|
|
|
|
// update the memory intervals
|
|
|
|
|
// m.i 0 4
|
|
|
|
|
// m.n 8 4
|
|
|
|
|
// m.p 16 8
|
|
|
|
|
// m.p[0].x 24 4
|
|
|
|
|
// m.p[0].y 28 4
|
|
|
|
|
// m.p[0].h 32 8
|
|
|
|
|
// m.p[0].prot 40 4
|
|
|
|
|
// m.p[0].flags 44 4
|
2021-02-01 11:33:13 +00:00
|
|
|
|
.weak _mmi
|
|
|
|
|
ezlea _mmi,cx
|
|
|
|
|
test %rcx,%rcx
|
2021-02-24 04:23:19 +00:00
|
|
|
|
push %r9 # save the stack size
|
2021-02-01 11:33:13 +00:00
|
|
|
|
jz 3f
|
2021-02-24 04:23:19 +00:00
|
|
|
|
lea -1(%r11,%r9),%r9 # need incl. interval
|
|
|
|
|
shr $16,%r11 # for the stack range
|
|
|
|
|
shr $16,%r9
|
2021-02-01 11:33:13 +00:00
|
|
|
|
movb $1,(%rcx) # _mmi.i
|
2021-09-04 20:20:47 +00:00
|
|
|
|
mov %r11d,24(%rcx) # _mmi.s[0].x
|
|
|
|
|
mov %r9d,28(%rcx) # _mmi.s[0].y
|
2021-10-14 00:27:13 +00:00
|
|
|
|
orq $-1,32(%rcx) # _mmi.s[0].h
|
|
|
|
|
mov %edx,40(%rcx) # _mmi.s[0].prot
|
|
|
|
|
mov %r10d,44(%rcx) # _mmi.s[0].flags
|
2021-02-24 04:23:19 +00:00
|
|
|
|
3: pop %r9 # restore stack size
|
|
|
|
|
pop %rsi
|
2021-02-01 11:33:13 +00:00
|
|
|
|
pop %rdi
|
|
|
|
|
leave
|
2021-10-14 00:27:13 +00:00
|
|
|
|
// switch stacks
|
2021-02-01 11:33:13 +00:00
|
|
|
|
pop %rcx
|
2021-02-24 04:23:19 +00:00
|
|
|
|
lea (%rax,%r9),%rsp
|
|
|
|
|
sub $ape_stack_align,%rsp # openbsd:stackbound
|
2021-02-08 12:04:42 +00:00
|
|
|
|
mov %rbp,(%rsp)
|
2021-02-01 11:33:13 +00:00
|
|
|
|
push %rcx
|
|
|
|
|
push %rbp
|
|
|
|
|
mov %rsp,%rbp
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
|
|
|
|
_init_systemfive_syscall:
|
2021-05-14 12:36:58 +00:00
|
|
|
|
mov __NR_msyscall,%eax # syscall origin protect
|
2021-02-24 12:00:38 +00:00
|
|
|
|
cmp $0xfff,%ax # openbsd is pretty cool
|
|
|
|
|
jae _init_systemfive_done
|
2021-02-03 04:21:06 +00:00
|
|
|
|
push %rdi
|
|
|
|
|
push %rsi
|
2021-02-06 11:17:41 +00:00
|
|
|
|
.weak __privileged_addr
|
|
|
|
|
.weak __privileged_size
|
|
|
|
|
mov $__privileged_addr,%edi
|
|
|
|
|
mov $__privileged_size,%esi
|
2021-02-03 04:21:06 +00:00
|
|
|
|
syscall
|
|
|
|
|
pop %rsi
|
2021-05-14 12:36:58 +00:00
|
|
|
|
pop %rdi
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 𝑠𝑙𝑖𝑑𝑒
|
2021-02-01 11:33:13 +00:00
|
|
|
|
#endif /* TINY */
|
2021-02-08 17:19:00 +00:00
|
|
|
|
_init_systemfive_done:
|
2021-02-01 11:33:13 +00:00
|
|
|
|
nop
|
|
|
|
|
.init.end 300,_init_systemfive,globl,hidden
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
|
// 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
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .piro.bss.sort.syscon.1,"aw",@nobits
|
|
|
|
|
.align 8
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_start:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized quadwords...
|
|
|
|
|
*/.previous
|
|
|
|
|
.section .piro.bss.sort.syscon.3,"aw",@nobits
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_end:
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_start,@object
|
|
|
|
|
.type syscon_end,@object
|
|
|
|
|
.globl syscon_start
|
|
|
|
|
.globl syscon_end
|
2021-02-24 04:23:19 +00:00
|
|
|
|
#if SupportsLinux() || SupportsMetal()
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .sort.rodata.syscon.linux.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_linux:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_linux,@object
|
|
|
|
|
.globl syscon_linux
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsXnu()
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .sort.rodata.syscon.xnu.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_xnu:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_xnu,@object
|
|
|
|
|
.globl syscon_xnu
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsFreebsd()
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .sort.rodata.syscon.freebsd.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_freebsd:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_freebsd,@object
|
|
|
|
|
.globl syscon_freebsd
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsOpenbsd()
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .sort.rodata.syscon.openbsd.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_openbsd:/*
|
2021-02-05 14:16:20 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_openbsd,@object
|
|
|
|
|
.globl syscon_openbsd
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsNetbsd()
|
2021-02-05 14:16:20 +00:00
|
|
|
|
.section .sort.rodata.syscon.netbsd.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_netbsd:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_netbsd,@object
|
|
|
|
|
.globl syscon_netbsd
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if SupportsWindows()
|
2020-06-15 14:18:57 +00:00
|
|
|
|
.section .sort.rodata.syscon.windows.1,"a",@progbits
|
|
|
|
|
.align 1
|
2021-02-08 17:19:00 +00:00
|
|
|
|
syscon_windows:/*
|
2020-06-15 14:18:57 +00:00
|
|
|
|
...decentralized leb128...
|
|
|
|
|
*/.previous
|
2021-02-08 17:19:00 +00:00
|
|
|
|
.type syscon_windows,@object
|
|
|
|
|
.globl syscon_windows
|
2021-02-08 12:04:42 +00:00
|
|
|
|
#endif
|
2021-02-24 04:23:19 +00:00
|
|
|
|
|
|
|
|
|
.weak ape_stack_vaddr
|
|
|
|
|
.weak ape_stack_memsz
|
|
|
|
|
.weak ape_stack_align
|