wired
libc-free QUIC/HTTP3 SDK in C for x86_64-linux
Loading...
Searching...
No Matches
x8664.h File Reference

x86-64 Linux architecture adapter: every ISA-specific construct the SDK needs, behind the arch-neutral names the rest of src/ consumes (via common/arch/arch.h). More...

Go to the source code of this file.

Macros

#define SYS_read   0
 read(2) syscall number
#define SYS_write   1
 write(2) syscall number
#define SYS_close   3
 close(2) syscall number
#define SYS_newfstatat   262
 newfstatat(2) syscall number
#define SYS_pread64   17
 pread64(2) syscall number
#define SYS_mmap   9
 mmap(2) syscall number
#define SYS_mprotect   10
 mprotect(2) syscall number
#define SYS_munmap   11
 munmap(2) syscall number
#define SYS_rt_sigaction   13
 rt_sigaction(2) syscall number
#define SYS_rt_sigprocmask   14
 rt_sigprocmask(2) syscall number
#define SYS_rt_sigreturn   15
 rt_sigreturn(2) syscall number
#define SYS_rt_sigtimedwait   128
 rt_sigtimedwait(2) syscall number
#define SYS_socket   41
 socket(2) syscall number
#define SYS_sendmsg   46
 sendmsg(2) syscall number
#define SYS_sendto   44
 sendto(2) syscall number
#define SYS_recvfrom   45
 recvfrom(2) syscall number
#define SYS_bind   49
 bind(2) syscall number
#define SYS_recvmmsg   299
 recvmmsg(2) syscall number
#define SYS_setsockopt   54
 setsockopt(2) syscall number
#define SYS_clone   56
 clone(2) syscall number
#define SYS_exit   60
 exit(2) syscall number
#define SYS_gettid   186
 gettid(2) syscall number
#define SYS_futex   202
 futex(2) syscall number
#define SYS_sched_setaffinity   203
 sched_setaffinity(2) syscall number
#define SYS_sched_getaffinity   204
 sched_getaffinity(2) syscall number
#define SYS_clock_gettime   228
 clock_gettime(2) syscall number
#define SYS_openat   257
 openat(2) syscall number
#define SYS_mkdirat   258
 mkdirat(2) syscall number
#define SYS_getrandom   318
 getrandom(2) syscall number
#define SYS_fork   57
 fork(2) syscall number
#define SYS_wait4   61
 wait4(2) syscall number
#define SYS_kill   62
 kill(2) syscall number
#define SYS_getpid   39
 getpid(2) syscall number
#define SYS_getppid   110
 getppid(2) syscall number
#define SYS_prctl   157
 prctl(2) syscall number
#define SYS_exit_group   231
 exit_group(2) syscall number
#define SYS_poll   7
 poll(2) syscall number
#define SYS_getsockopt   55
 getsockopt(2) syscall number
#define SYS_fcntl   72
 fcntl(2) syscall number
#define SYS_bpf   321
 bpf(2) syscall number
#define SA_RESTORER   0x04000000u
 x86_64 Linux rt_sigaction(2) requires SA_RESTORER plus a userspace trampoline that issues rt_sigreturn(2); the kernel refuses a bare handler with no restorer.
#define syscall2(n, a, b)
 Two-argument syscall: syscall6() with the trailing arguments zeroed.
#define syscall3(n, a, b, c)
 Three-argument syscall: syscall6() with the trailing arguments zeroed.
#define syscall4(n, a, b, c, d)
 Four-argument syscall: syscall6() with the trailing arguments zeroed.
#define syscall5(n, a, b, c, d, e)
 Five-argument syscall: syscall6() with the trailing argument zeroed.
#define syscall1(n, a)
 One-argument syscall: syscall6() with the trailing arguments zeroed.
#define WIRED_ARCH_START_ASM(entry)
 Body of a freestanding _start (to be used inside a naked function): Linux enters with RSP%16==0 and no return address on the stack; this recovers argc (rdi) and argv (rsi) from the kernel-built initial stack, 16-byte-aligns RSP for the SysV ABI's post-call state, calls entry (int entry(int argc, char** argv)), and exits with its return value.

Functions

static i64 syscall6 (i64 n, i64 a, i64 b, i64 c, i64 d, i64 e, i64 f)
 Six-argument raw x86_64 Linux syscall.
static void wired_arch_pause (void)
 CPU spin-wait hint for busy-poll loops (x86 pause): tells the core a spin is in progress, saving power and easing hyperthread contention.
i64 wired_arch_clone_raw (i64 flags, u8 *child_stack, i32 *parent_tid, i32 *child_tid, i64 tls)
 Raw clone(2) whose child starts on a new stack (assembly trampoline in clone.c; a C wrapper cannot be used because the compiler's spills would read the parent's frame from the child).
void wired_arch_sigreturn_restorer (void)
 Signal-return trampoline for rt_sigaction(2)'s SA_RESTORER slot: the kernel jumps here when a handler returns, and the stub issues rt_sigreturn(2) (assembly in sigret.c; there is no libc restore_rt to fall back on).

Detailed Description

x86-64 Linux architecture adapter: every ISA-specific construct the SDK needs, behind the arch-neutral names the rest of src/ consumes (via common/arch/arch.h).

Nothing outside src/common/arch/ writes inline assembly or x86 builtins.

Macro Definition Documentation

◆ syscall1

#define syscall1 ( n,
a )
Value:
syscall6((n), (i64)(a), 0, 0, 0, 0, 0)
long i64
signed 64-bit integer
Definition types.h:14
static i64 syscall6(i64 n, i64 a, i64 b, i64 c, i64 d, i64 e, i64 f)
Six-argument raw x86_64 Linux syscall.
Definition x8664.h:77

One-argument syscall: syscall6() with the trailing arguments zeroed.

◆ syscall2

#define syscall2 ( n,
a,
b )
Value:
syscall6((n), (i64)(a), (i64)(b), 0, 0, 0, 0)

Two-argument syscall: syscall6() with the trailing arguments zeroed.

◆ syscall3

#define syscall3 ( n,
a,
b,
c )
Value:
syscall6((n), (i64)(a), (i64)(b), (i64)(c), 0, 0, 0)

Three-argument syscall: syscall6() with the trailing arguments zeroed.

◆ syscall4

#define syscall4 ( n,
a,
b,
c,
d )
Value:
syscall6((n), (i64)(a), (i64)(b), (i64)(c), (i64)(d), 0, 0)

Four-argument syscall: syscall6() with the trailing arguments zeroed.

◆ syscall5

#define syscall5 ( n,
a,
b,
c,
d,
e )
Value:
syscall6((n), (i64)(a), (i64)(b), (i64)(c), (i64)(d), (i64)(e), 0)

Five-argument syscall: syscall6() with the trailing argument zeroed.

◆ WIRED_ARCH_START_ASM

#define WIRED_ARCH_START_ASM ( entry)
Value:
__asm__ volatile( \
"mov (%rsp), %rdi\n" \
"lea 8(%rsp), %rsi\n" \
"and $-16, %rsp\n" \
"call " #entry \
"\n" \
"mov %eax, %edi\n" \
"mov $60, %eax\n" /* SYS_exit */ \
"syscall\n")

Body of a freestanding _start (to be used inside a naked function): Linux enters with RSP%16==0 and no return address on the stack; this recovers argc (rdi) and argv (rsi) from the kernel-built initial stack, 16-byte-aligns RSP for the SysV ABI's post-call state, calls entry (int entry(int argc, char** argv)), and exits with its return value.

Function Documentation

◆ syscall6()

i64 syscall6 ( i64 n,
i64 a,
i64 b,
i64 c,
i64 d,
i64 e,
i64 f )
inlinestatic

Six-argument raw x86_64 Linux syscall.

Arguments follow the x86_64 syscall ABI register order: number in rax, then rdi, rsi, rdx, r10, r8, r9. rcx/r11 are clobbered by the syscall instruction. The syscall1..syscall5 wrappers pass 0 for their unused trailing arguments.

Parameters
nsyscall number (one of the SYS_* constants)
a1st argument (rdi)
b2nd argument (rsi)
c3rd argument (rdx)
d4th argument (r10)
e5th argument (r8)
f6th argument (r9)
Returns
the kernel's return value; negative values are -errno

◆ wired_arch_clone_raw()

i64 wired_arch_clone_raw ( i64 flags,
u8 * child_stack,
i32 * parent_tid,
i32 * child_tid,
i64 tls )

Raw clone(2) whose child starts on a new stack (assembly trampoline in clone.c; a C wrapper cannot be used because the compiler's spills would read the parent's frame from the child).

The caller must have pushed the child's entry function and its argument as the two topmost 8-byte slots of child_stack; the child pops them, calls fn(arg), then exits(0).

Parameters
flagsCLONE_* flag mask passed straight to the kernel
child_stacktop of the child's stack, seeded with fn and arg
parent_tidkernel writes the child tid here (CLONE_PARENT_SETTID)
child_tidcleared+futex-woken on child exit (CLONE_CHILD_CLEARTID)
tlsnew TLS descriptor (0 when unused)
Returns
the child tid in the parent, or a negative -errno

◆ wired_arch_sigreturn_restorer()

void wired_arch_sigreturn_restorer ( void )

Signal-return trampoline for rt_sigaction(2)'s SA_RESTORER slot: the kernel jumps here when a handler returns, and the stub issues rt_sigreturn(2) (assembly in sigret.c; there is no libc restore_rt to fall back on).

Take its address only – it is not a callable C function.