1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * x86_64 specific definitions for NOLIBC
4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_ARCH_X86_64_H
8 #define _NOLIBC_ARCH_X86_64_H
13 /* Syscalls for x86_64 :
14 * - registers are 64-bit
15 * - syscall number is passed in rax
16 * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively
17 * - the system call is performed by calling the syscall instruction
18 * - syscall return comes in rax
19 * - rcx and r11 are clobbered, others are preserved.
20 * - the arguments are cast to long and assigned into the target registers
21 * which are then simply passed as registers to the asm code, so that we
22 * don't have to experience issues with register constraints.
23 * - the syscall number is always specified last in order to allow to force
24 * some registers before (gcc refuses a %-register at the last position).
25 * - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2.1
26 * Calling Conventions.
28 * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home
32 #define my_syscall0(num) \
35 register long _num __asm__ ("rax") = (num); \
41 : "rcx", "r11", "memory", "cc" \
46 #define my_syscall1(num, arg1) \
49 register long _num __asm__ ("rax") = (num); \
50 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
57 : "rcx", "r11", "memory", "cc" \
62 #define my_syscall2(num, arg1, arg2) \
65 register long _num __asm__ ("rax") = (num); \
66 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
67 register long _arg2 __asm__ ("rsi") = (long)(arg2); \
72 : "r"(_arg1), "r"(_arg2), \
74 : "rcx", "r11", "memory", "cc" \
79 #define my_syscall3(num, arg1, arg2, arg3) \
82 register long _num __asm__ ("rax") = (num); \
83 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
84 register long _arg2 __asm__ ("rsi") = (long)(arg2); \
85 register long _arg3 __asm__ ("rdx") = (long)(arg3); \
90 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
92 : "rcx", "r11", "memory", "cc" \
97 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
100 register long _num __asm__ ("rax") = (num); \
101 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
102 register long _arg2 __asm__ ("rsi") = (long)(arg2); \
103 register long _arg3 __asm__ ("rdx") = (long)(arg3); \
104 register long _arg4 __asm__ ("r10") = (long)(arg4); \
109 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
111 : "rcx", "r11", "memory", "cc" \
116 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
119 register long _num __asm__ ("rax") = (num); \
120 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
121 register long _arg2 __asm__ ("rsi") = (long)(arg2); \
122 register long _arg3 __asm__ ("rdx") = (long)(arg3); \
123 register long _arg4 __asm__ ("r10") = (long)(arg4); \
124 register long _arg5 __asm__ ("r8") = (long)(arg5); \
129 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
131 : "rcx", "r11", "memory", "cc" \
136 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
139 register long _num __asm__ ("rax") = (num); \
140 register long _arg1 __asm__ ("rdi") = (long)(arg1); \
141 register long _arg2 __asm__ ("rsi") = (long)(arg2); \
142 register long _arg3 __asm__ ("rdx") = (long)(arg3); \
143 register long _arg4 __asm__ ("r10") = (long)(arg4); \
144 register long _arg5 __asm__ ("r8") = (long)(arg5); \
145 register long _arg6 __asm__ ("r9") = (long)(arg6); \
150 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
151 "r"(_arg6), "0"(_num) \
152 : "rcx", "r11", "memory", "cc" \
159 * x86-64 System V ABI mandates:
160 * 1) %rsp must be 16-byte aligned right before the function call.
161 * 2) The deepest stack frame should be zero (the %rbp).
164 void __attribute__((weak
, noreturn
)) __nolibc_entrypoint __no_stack_protector
_start(void)
167 "xor %ebp, %ebp\n" /* zero the stack frame */
168 "mov %rsp, %rdi\n" /* save stack pointer to %rdi, as arg1 of _start_c */
169 "and $-16, %rsp\n" /* %rsp must be 16-byte aligned before call */
170 "call _start_c\n" /* transfer to c runtime */
171 "hlt\n" /* ensure it does not return */
173 __nolibc_entrypoint_epilogue();
176 #define NOLIBC_ARCH_HAS_MEMMOVE
177 void *memmove(void *dst
, const void *src
, size_t len
);
179 #define NOLIBC_ARCH_HAS_MEMCPY
180 void *memcpy(void *dst
, const void *src
, size_t len
);
182 #define NOLIBC_ARCH_HAS_MEMSET
183 void *memset(void *dst
, int c
, size_t len
);
186 ".section .text.nolibc_memmove_memcpy\n"
191 "movq %rdx, %rcx\n\t"
192 "movq %rdi, %rax\n\t"
193 "movq %rdi, %rdx\n\t"
194 "subq %rsi, %rdx\n\t"
195 "cmpq %rcx, %rdx\n\t"
199 "1:" /* backward copy */
200 "leaq -1(%rdi, %rcx, 1), %rdi\n\t"
201 "leaq -1(%rsi, %rcx, 1), %rsi\n\t"
207 ".section .text.nolibc_memset\n"
210 "xchgl %eax, %esi\n\t"
211 "movq %rdx, %rcx\n\t"
218 #endif /* _NOLIBC_ARCH_X86_64_H */