1 /* Copyright (C) 1996-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library. If not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <sysdeps/unix/sysv/linux/sysdep.h>
21 /* Defines RTLD_PRIVATE_ERRNO. */
22 #include <dl-sysdep.h>
24 /* For Linux we can use the system call table in the header file
25 /usr/include/asm/unistd.h
26 of the kernel. But these symbols do not follow the SYS_* syntax
27 so we have to redefine the `SYS_ify' macro here. */
29 #define SYS_ify(syscall_name) __NR_##syscall_name
33 /* Linux uses a negative return value to indicate syscall errors, unlike
34 most Unices, which use the condition codes' carry flag.
36 Since version 2.1 the return value of a system call might be negative
37 even if the call succeeded. E.g., the `lseek' system call might return
38 a large offset. Therefore we must not anymore test for < 0, but test
39 for a real error by making sure the value in %d0 is a real error
40 number. Linus said he will make sure the no syscall returns a value
41 in -1 .. -4095 as a valid result so we can safely test with -4095. */
43 /* We don't want the label for the error handler to be visible in the symbol
44 table when we define it here. */
45 #undef SYSCALL_ERROR_LABEL
47 #define SYSCALL_ERROR_LABEL .Lsyscall_error
49 #define SYSCALL_ERROR_LABEL __syscall_error
53 #define PSEUDO(name, syscall_name, args) \
56 DO_CALL (syscall_name, args); \
58 jcc SYSCALL_ERROR_LABEL
61 #define PSEUDO_END(name) \
62 SYSCALL_ERROR_HANDLER; \
66 #define PSEUDO_NOERRNO(name, syscall_name, args) \
69 DO_CALL (syscall_name, args)
71 #undef PSEUDO_END_NOERRNO
72 #define PSEUDO_END_NOERRNO(name) \
75 #define ret_NOERRNO rts
77 /* The function has to return the error code. */
79 #define PSEUDO_ERRVAL(name, syscall_name, args) \
82 DO_CALL (syscall_name, args); \
85 #undef PSEUDO_END_ERRVAL
86 #define PSEUDO_END_ERRVAL(name) \
89 #define ret_ERRVAL rts
92 # if RTLD_PRIVATE_ERRNO
93 # define SYSCALL_ERROR_HANDLER \
94 SYSCALL_ERROR_LABEL: \
95 PCREL_OP (lea, rtld_errno, %a0, %a0); \
99 /* Copy return value to %a0 for syscalls that are declared to return \
100 a pointer (e.g., mmap). */ \
103 # elif defined _LIBC_REENTRANT
105 # define SYSCALL_ERROR_ERRNO __libc_errno
107 # define SYSCALL_ERROR_ERRNO errno
109 # define SYSCALL_ERROR_HANDLER \
110 SYSCALL_ERROR_LABEL: \
112 move.l %d0, -(%sp); \
113 cfi_adjust_cfa_offset (4); \
114 jbsr __m68k_read_tp@PLTPC; \
115 SYSCALL_ERROR_LOAD_GOT (%a1); \
116 add.l (SYSCALL_ERROR_ERRNO@TLSIE, %a1), %a0; \
117 move.l (%sp)+, (%a0); \
118 cfi_adjust_cfa_offset (-4); \
120 /* Copy return value to %a0 for syscalls that are declared to return \
121 a pointer (e.g., mmap). */ \
124 # else /* !_LIBC_REENTRANT */
125 /* Store (- %d0) into errno through the GOT. */
126 # define SYSCALL_ERROR_HANDLER \
127 SYSCALL_ERROR_LABEL: \
128 move.l (errno@GOTPC, %pc), %a0; \
132 /* Copy return value to %a0 for syscalls that are declared to return \
133 a pointer (e.g., mmap). */ \
136 # endif /* _LIBC_REENTRANT */
138 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
141 /* Linux takes system call arguments in registers:
143 syscall number %d0 call-clobbered
144 arg 1 %d1 call-clobbered
149 arg 6 %a0 call-clobbered
151 The stack layout upon entering the function is:
161 (Of course a function with say 3 arguments does not have entries for
164 Separate move's are faster than movem, but need more space. Since
165 speed is more important, we don't use movem. Since %a0 and %a1 are
166 scratch registers, we can use them for saving as well. */
168 #define DO_CALL(syscall_name, args) \
169 move.l &SYS_ify(syscall_name), %d0; \
174 #define DOARGS_0 /* No arguments to frob. */
175 #define UNDOARGS_0 /* No arguments to unfrob. */
176 #define _DOARGS_0(n) /* No arguments to frob. */
178 #define DOARGS_1 _DOARGS_1 (4)
179 #define _DOARGS_1(n) move.l n(%sp), %d1; _DOARGS_0 (n)
180 #define UNDOARGS_1 UNDOARGS_0
182 #define DOARGS_2 _DOARGS_2 (8)
183 #define _DOARGS_2(n) move.l %d2, %a0; cfi_register (%d2, %a0); \
184 move.l n(%sp), %d2; _DOARGS_1 (n-4)
185 #define UNDOARGS_2 UNDOARGS_1; move.l %a0, %d2; cfi_restore (%d2)
187 #define DOARGS_3 _DOARGS_3 (12)
188 #define _DOARGS_3(n) move.l %d3, %a1; cfi_register (%d3, %a1); \
189 move.l n(%sp), %d3; _DOARGS_2 (n-4)
190 #define UNDOARGS_3 UNDOARGS_2; move.l %a1, %d3; cfi_restore (%d3)
192 #define DOARGS_4 _DOARGS_4 (16)
193 #define _DOARGS_4(n) move.l %d4, -(%sp); \
194 cfi_adjust_cfa_offset (4); cfi_rel_offset (%d4, 0); \
195 move.l n+4(%sp), %d4; _DOARGS_3 (n)
196 #define UNDOARGS_4 UNDOARGS_3; move.l (%sp)+, %d4; \
197 cfi_adjust_cfa_offset (-4); cfi_restore (%d4)
199 #define DOARGS_5 _DOARGS_5 (20)
200 #define _DOARGS_5(n) move.l %d5, -(%sp); \
201 cfi_adjust_cfa_offset (4); cfi_rel_offset (%d5, 0); \
202 move.l n+4(%sp), %d5; _DOARGS_4 (n)
203 #define UNDOARGS_5 UNDOARGS_4; move.l (%sp)+, %d5; \
204 cfi_adjust_cfa_offset (-4); cfi_restore (%d5)
206 #define DOARGS_6 _DOARGS_6 (24)
207 #define _DOARGS_6(n) _DOARGS_5 (n-4); move.l %a0, -(%sp); \
208 cfi_adjust_cfa_offset (4); \
209 move.l n+12(%sp), %a0;
210 #define UNDOARGS_6 move.l (%sp)+, %a0; cfi_adjust_cfa_offset (-4); \
215 #if 0 /* Not used by Linux */
218 #define MOVE(x,y) movel x , y
221 #else /* not __ASSEMBLER__ */
223 /* Define a macro which expands inline into the wrapper code for a system
224 call. This use is for internal calls that do not need to handle errors
225 normally. It will never touch errno. This returns just what the kernel
227 #undef INTERNAL_SYSCALL
228 #define INTERNAL_SYSCALL_NCS(name, nr, args...) \
229 ({ unsigned int _sys_result; \
231 /* Load argument values in temporary variables
232 to perform side effects like function calls
233 before the call used registers are set. */ \
234 LOAD_ARGS_##nr (args) \
236 register int _d0 asm ("%d0") = name; \
237 asm volatile ("trap #0" \
239 : "0" (_d0) ASM_ARGS_##nr \
243 (int) _sys_result; })
244 #define INTERNAL_SYSCALL(name, nr, args...) \
245 INTERNAL_SYSCALL_NCS (__NR_##name, nr, ##args)
247 #define LOAD_ARGS_0()
250 #define LOAD_ARGS_1(a1) \
252 int __arg1 = (int) (a1);
253 #define LOAD_REGS_1 \
254 register int _d1 asm ("d1") = __arg1; \
256 #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
257 #define LOAD_ARGS_2(a1, a2) \
259 int __arg2 = (int) (a2);
260 #define LOAD_REGS_2 \
261 register int _d2 asm ("d2") = __arg2; \
263 #define ASM_ARGS_2 ASM_ARGS_1, "d" (_d2)
264 #define LOAD_ARGS_3(a1, a2, a3) \
265 LOAD_ARGS_2 (a1, a2) \
266 int __arg3 = (int) (a3);
267 #define LOAD_REGS_3 \
268 register int _d3 asm ("d3") = __arg3; \
270 #define ASM_ARGS_3 ASM_ARGS_2, "d" (_d3)
271 #define LOAD_ARGS_4(a1, a2, a3, a4) \
272 LOAD_ARGS_3 (a1, a2, a3) \
273 int __arg4 = (int) (a4);
274 #define LOAD_REGS_4 \
275 register int _d4 asm ("d4") = __arg4; \
277 #define ASM_ARGS_4 ASM_ARGS_3, "d" (_d4)
278 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
279 LOAD_ARGS_4 (a1, a2, a3, a4) \
280 int __arg5 = (int) (a5);
281 #define LOAD_REGS_5 \
282 register int _d5 asm ("d5") = __arg5; \
284 #define ASM_ARGS_5 ASM_ARGS_4, "d" (_d5)
285 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
286 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
287 int __arg6 = (int) (a6);
288 #define LOAD_REGS_6 \
289 register int _a0 asm ("a0") = __arg6; \
291 #define ASM_ARGS_6 ASM_ARGS_5, "a" (_a0)
293 #undef HAVE_INTERNAL_BRK_ADDR_SYMBOL
294 #define HAVE_INTERNAL_BRK_ADDR_SYMBOL 1
296 #endif /* not __ASSEMBLER__ */
298 /* M68K needs system-supplied DSO to access TLS helpers
299 even when statically linked. */
300 #define NEED_STATIC_SYSINFO_DSO 1