2020-06-15 14:18:57 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_SYSV_MACROS_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_SYSV_MACROS_H_
|
2022-08-18 02:56:54 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#ifdef __ASSEMBLER__
|
|
|
|
/* clang-format off */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview System Five jump slot generator.
|
|
|
|
*
|
|
|
|
* We need to pass a magic number to the SYSCALL instruction in %rax.
|
|
|
|
* That's only possible to do in C using asm() macro wrappers, like in
|
|
|
|
* Chromium's "System Call Support" library. This technique is simpler
|
|
|
|
* to implement, gains us object-level ABI compatibility, hookability,
|
|
|
|
* and the ability to support significantly more functions, without the
|
|
|
|
* risk of slowing down builds too much with complicated headers.
|
|
|
|
*/
|
|
|
|
|
2023-05-19 02:05:08 +00:00
|
|
|
.macro .scall name:req amd:req arm_linux:req arm_xnu:req kw1 kw2
|
2023-05-09 09:35:05 +00:00
|
|
|
.section .text.syscall,"ax",@progbits
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
2020-06-15 14:18:57 +00:00
|
|
|
.ifnb \kw2
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
movabs $\amd,%rax
|
2020-11-25 16:19:00 +00:00
|
|
|
jmp *__systemfive(%rip)
|
2020-06-15 14:18:57 +00:00
|
|
|
.else
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
push %rbp
|
2020-06-15 14:18:57 +00:00
|
|
|
mov %rsp,%rbp
|
2023-05-09 08:56:56 +00:00
|
|
|
movabs $\amd,%rax
|
2020-11-25 16:19:00 +00:00
|
|
|
call *__systemfive(%rip)
|
2020-06-15 14:18:57 +00:00
|
|
|
pop %rbp
|
|
|
|
ret
|
|
|
|
.endif
|
2023-05-19 02:05:08 +00:00
|
|
|
.endfn \name,\kw1,\kw2
|
2023-05-09 08:56:56 +00:00
|
|
|
#elif defined(__aarch64__)
|
2023-05-19 02:05:08 +00:00
|
|
|
.ifc \arm_linux,4095
|
|
|
|
.ifc \arm_xnu,4095
|
|
|
|
// return enosys();
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
b enosys
|
2023-05-19 02:05:08 +00:00
|
|
|
.endfn \name,\kw1,\kw2
|
2023-05-12 12:47:54 +00:00
|
|
|
.else
|
2023-05-19 02:05:08 +00:00
|
|
|
// return IsXnu() ? syscall(x16, ...) : syscall(x8, ...);
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
adrp x9,__hostos
|
2023-05-19 02:05:08 +00:00
|
|
|
ldr w9,[x9,#:lo12:__hostos]
|
|
|
|
tbz x9,#3,1f // !IsXnu()
|
|
|
|
mov x16,#\arm_xnu // apple ordinal
|
2023-07-01 12:10:12 +00:00
|
|
|
mov x9,#0 // clear carry flag
|
|
|
|
adds x9,x9,#0 // clear carry flag
|
2023-05-19 02:05:08 +00:00
|
|
|
svc #0 // issue system call
|
|
|
|
bcs 1f
|
|
|
|
b _sysret
|
|
|
|
1: neg x0,x0
|
|
|
|
b _sysret
|
|
|
|
.hidden _sysret
|
|
|
|
.endfn \name,\kw1,\kw2
|
|
|
|
.endif
|
|
|
|
.else
|
|
|
|
.ifc \arm_xnu,4095
|
|
|
|
// return IsLinux() ? syscall(x8, ...) : enosys();
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
adrp x9,__hostos
|
2023-05-19 02:05:08 +00:00
|
|
|
ldr w9,[x9,#:lo12:__hostos]
|
|
|
|
tbz x9,#0,1f // !IsLinux()
|
|
|
|
mov x8,#\arm_linux // systemd ordinal
|
|
|
|
svc #0 // issue system call
|
|
|
|
mov x1,#\arm_linux
|
2023-05-09 08:56:56 +00:00
|
|
|
b _sysret
|
|
|
|
.hidden _sysret
|
2023-05-19 02:05:08 +00:00
|
|
|
1: b enosys
|
|
|
|
.endfn \name,\kw1,\kw2
|
|
|
|
.else
|
2023-06-06 06:35:31 +00:00
|
|
|
.ftrace1
|
|
|
|
\name: .ftrace2
|
|
|
|
mov x16,#\arm_xnu // apple ordinal
|
2023-05-19 02:05:08 +00:00
|
|
|
mov x8,#\arm_linux // systemd ordinal
|
2023-06-03 15:12:13 +00:00
|
|
|
mov x9,#0 // clear carry flag
|
|
|
|
adds x9,x9,#0 // clear carry flag
|
2023-05-19 02:05:08 +00:00
|
|
|
svc #0 // issue system call
|
|
|
|
bcs 1f
|
|
|
|
b _sysret
|
|
|
|
1: neg x0,x0
|
|
|
|
b _sysret
|
|
|
|
.hidden _sysret
|
|
|
|
.endfn \name,\kw1,\kw2
|
|
|
|
.endif
|
|
|
|
.endif
|
2023-05-09 08:56:56 +00:00
|
|
|
#else
|
|
|
|
#error "architecture unsupported"
|
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
.previous
|
|
|
|
.endm
|
|
|
|
|
|
|
|
/* clang-format on */
|
|
|
|
#endif /* __ASSEMBLER__ */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_SYSV_MACROS_H_ */
|