xref: /linux/tools/include/nolibc/arch-x86_64.h (revision 41e0d49104dbff888ef6446ea46842fde66c0a76)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * x86_64 specific definitions for NOLIBC
4  * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
5  */
6 
7 #ifndef _NOLIBC_ARCH_X86_64_H
8 #define _NOLIBC_ARCH_X86_64_H
9 
10 /* O_* macros for fcntl/open are architecture-specific */
11 #define O_RDONLY            0
12 #define O_WRONLY            1
13 #define O_RDWR              2
14 #define O_CREAT          0x40
15 #define O_EXCL           0x80
16 #define O_NOCTTY        0x100
17 #define O_TRUNC         0x200
18 #define O_APPEND        0x400
19 #define O_NONBLOCK      0x800
20 #define O_DIRECTORY   0x10000
21 
22 /* The struct returned by the stat() syscall, equivalent to stat64(). The
23  * syscall returns 116 bytes and stops in the middle of __unused.
24  */
25 struct sys_stat_struct {
26 	unsigned long st_dev;
27 	unsigned long st_ino;
28 	unsigned long st_nlink;
29 	unsigned int  st_mode;
30 	unsigned int  st_uid;
31 
32 	unsigned int  st_gid;
33 	unsigned int  __pad0;
34 	unsigned long st_rdev;
35 	long          st_size;
36 	long          st_blksize;
37 
38 	long          st_blocks;
39 	unsigned long st_atime;
40 	unsigned long st_atime_nsec;
41 	unsigned long st_mtime;
42 
43 	unsigned long st_mtime_nsec;
44 	unsigned long st_ctime;
45 	unsigned long st_ctime_nsec;
46 	long          __unused[3];
47 };
48 
49 /* Syscalls for x86_64 :
50  *   - registers are 64-bit
51  *   - syscall number is passed in rax
52  *   - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively
53  *   - the system call is performed by calling the syscall instruction
54  *   - syscall return comes in rax
55  *   - rcx and r11 are clobbered, others are preserved.
56  *   - the arguments are cast to long and assigned into the target registers
57  *     which are then simply passed as registers to the asm code, so that we
58  *     don't have to experience issues with register constraints.
59  *   - the syscall number is always specified last in order to allow to force
60  *     some registers before (gcc refuses a %-register at the last position).
61  *   - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2.1
62  *     Calling Conventions.
63  *
64  * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home
65  *
66  */
67 
68 #define my_syscall0(num)                                                      \
69 ({                                                                            \
70 	long _ret;                                                            \
71 	register long _num  __asm__ ("rax") = (num);                          \
72 	                                                                      \
73 	__asm__  volatile (                                                   \
74 		"syscall\n"                                                   \
75 		: "=a"(_ret)                                                  \
76 		: "0"(_num)                                                   \
77 		: "rcx", "r11", "memory", "cc"                                \
78 	);                                                                    \
79 	_ret;                                                                 \
80 })
81 
82 #define my_syscall1(num, arg1)                                                \
83 ({                                                                            \
84 	long _ret;                                                            \
85 	register long _num  __asm__ ("rax") = (num);                          \
86 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
87 	                                                                      \
88 	__asm__  volatile (                                                   \
89 		"syscall\n"                                                   \
90 		: "=a"(_ret)                                                  \
91 		: "r"(_arg1),                                                 \
92 		  "0"(_num)                                                   \
93 		: "rcx", "r11", "memory", "cc"                                \
94 	);                                                                    \
95 	_ret;                                                                 \
96 })
97 
98 #define my_syscall2(num, arg1, arg2)                                          \
99 ({                                                                            \
100 	long _ret;                                                            \
101 	register long _num  __asm__ ("rax") = (num);                          \
102 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
103 	register long _arg2 __asm__ ("rsi") = (long)(arg2);                   \
104 	                                                                      \
105 	__asm__  volatile (                                                   \
106 		"syscall\n"                                                   \
107 		: "=a"(_ret)                                                  \
108 		: "r"(_arg1), "r"(_arg2),                                     \
109 		  "0"(_num)                                                   \
110 		: "rcx", "r11", "memory", "cc"                                \
111 	);                                                                    \
112 	_ret;                                                                 \
113 })
114 
115 #define my_syscall3(num, arg1, arg2, arg3)                                    \
116 ({                                                                            \
117 	long _ret;                                                            \
118 	register long _num  __asm__ ("rax") = (num);                          \
119 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
120 	register long _arg2 __asm__ ("rsi") = (long)(arg2);                   \
121 	register long _arg3 __asm__ ("rdx") = (long)(arg3);                   \
122 	                                                                      \
123 	__asm__  volatile (                                                   \
124 		"syscall\n"                                                   \
125 		: "=a"(_ret)                                                  \
126 		: "r"(_arg1), "r"(_arg2), "r"(_arg3),                         \
127 		  "0"(_num)                                                   \
128 		: "rcx", "r11", "memory", "cc"                                \
129 	);                                                                    \
130 	_ret;                                                                 \
131 })
132 
133 #define my_syscall4(num, arg1, arg2, arg3, arg4)                              \
134 ({                                                                            \
135 	long _ret;                                                            \
136 	register long _num  __asm__ ("rax") = (num);                          \
137 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
138 	register long _arg2 __asm__ ("rsi") = (long)(arg2);                   \
139 	register long _arg3 __asm__ ("rdx") = (long)(arg3);                   \
140 	register long _arg4 __asm__ ("r10") = (long)(arg4);                   \
141 	                                                                      \
142 	__asm__  volatile (                                                   \
143 		"syscall\n"                                                   \
144 		: "=a"(_ret)                                                  \
145 		: "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4),             \
146 		  "0"(_num)                                                   \
147 		: "rcx", "r11", "memory", "cc"                                \
148 	);                                                                    \
149 	_ret;                                                                 \
150 })
151 
152 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5)                        \
153 ({                                                                            \
154 	long _ret;                                                            \
155 	register long _num  __asm__ ("rax") = (num);                          \
156 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
157 	register long _arg2 __asm__ ("rsi") = (long)(arg2);                   \
158 	register long _arg3 __asm__ ("rdx") = (long)(arg3);                   \
159 	register long _arg4 __asm__ ("r10") = (long)(arg4);                   \
160 	register long _arg5 __asm__ ("r8")  = (long)(arg5);                   \
161 	                                                                      \
162 	__asm__  volatile (                                                   \
163 		"syscall\n"                                                   \
164 		: "=a"(_ret)                                                  \
165 		: "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
166 		  "0"(_num)                                                   \
167 		: "rcx", "r11", "memory", "cc"                                \
168 	);                                                                    \
169 	_ret;                                                                 \
170 })
171 
172 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6)                  \
173 ({                                                                            \
174 	long _ret;                                                            \
175 	register long _num  __asm__ ("rax") = (num);                          \
176 	register long _arg1 __asm__ ("rdi") = (long)(arg1);                   \
177 	register long _arg2 __asm__ ("rsi") = (long)(arg2);                   \
178 	register long _arg3 __asm__ ("rdx") = (long)(arg3);                   \
179 	register long _arg4 __asm__ ("r10") = (long)(arg4);                   \
180 	register long _arg5 __asm__ ("r8")  = (long)(arg5);                   \
181 	register long _arg6 __asm__ ("r9")  = (long)(arg6);                   \
182 	                                                                      \
183 	__asm__  volatile (                                                   \
184 		"syscall\n"                                                   \
185 		: "=a"(_ret)                                                  \
186 		: "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
187 		  "r"(_arg6), "0"(_num)                                       \
188 		: "rcx", "r11", "memory", "cc"                                \
189 	);                                                                    \
190 	_ret;                                                                 \
191 })
192 
193 /* startup code */
194 /*
195  * x86-64 System V ABI mandates:
196  * 1) %rsp must be 16-byte aligned right before the function call.
197  * 2) The deepest stack frame should be zero (the %rbp).
198  *
199  */
200 __asm__ (".section .text\n"
201     ".weak _start\n"
202     "_start:\n"
203     "pop %rdi\n"                // argc   (first arg, %rdi)
204     "mov %rsp, %rsi\n"          // argv[] (second arg, %rsi)
205     "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx)
206     "xor %ebp, %ebp\n"          // zero the stack frame
207     "and $-16, %rsp\n"          // x86 ABI : esp must be 16-byte aligned before call
208     "call main\n"               // main() returns the status code, we'll exit with it.
209     "mov %eax, %edi\n"          // retrieve exit code (32 bit)
210     "mov $60, %eax\n"           // NR_exit == 60
211     "syscall\n"                 // really exit
212     "hlt\n"                     // ensure it does not return
213     "");
214 
215 #endif // _NOLIBC_ARCH_X86_64_H
216