1 /* Copyright (C) 1996, 1997, 1998, 2000, 2003, 2004, 2006, 2010, 2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>,
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library. If not, see
19 <http://www.gnu.org/licenses/>. */
23 /* Defines RTLD_PRIVATE_ERRNO. */
24 #include <dl-sysdep.h>
26 /* For Linux we can use the system call table in the header file
27 /usr/include/asm/unistd.h
28 of the kernel. But these symbols do not follow the SYS_* syntax
29 so we have to redefine the `SYS_ify' macro here. */
31 #define SYS_ify(syscall_name) __NR_##syscall_name
35 /* Linux uses a negative return value to indicate syscall errors, unlike
36 most Unices, which use the condition codes' carry flag.
38 Since version 2.1 the return value of a system call might be negative
39 even if the call succeeded. E.g., the `lseek' system call might return
40 a large offset. Therefore we must not anymore test for < 0, but test
41 for a real error by making sure the value in %d0 is a real error
42 number. Linus said he will make sure the no syscall returns a value
43 in -1 .. -4095 as a valid result so we can savely test with -4095. */
45 /* We don't want the label for the error handler to be visible in the symbol
46 table when we define it here. */
48 #define SYSCALL_ERROR_LABEL .Lsyscall_error
50 #define SYSCALL_ERROR_LABEL __syscall_error
54 #define PSEUDO(name, syscall_name, args) \
57 DO_CALL (syscall_name, args); \
59 jcc SYSCALL_ERROR_LABEL
62 #define PSEUDO_END(name) \
63 SYSCALL_ERROR_HANDLER; \
67 #define PSEUDO_NOERRNO(name, syscall_name, args) \
70 DO_CALL (syscall_name, args)
72 #undef PSEUDO_END_NOERRNO
73 #define PSEUDO_END_NOERRNO(name) \
76 #define ret_NOERRNO rts
78 /* The function has to return the error code. */
80 #define PSEUDO_ERRVAL(name, syscall_name, args) \
83 DO_CALL (syscall_name, args); \
86 #undef PSEUDO_END_ERRVAL
87 #define PSEUDO_END_ERRVAL(name) \
90 #define ret_ERRVAL rts
93 # if RTLD_PRIVATE_ERRNO
94 # define SYSCALL_ERROR_HANDLER \
95 SYSCALL_ERROR_LABEL: \
96 PCREL_OP (lea, rtld_errno, %a0, %a0); \
100 /* Copy return value to %a0 for syscalls that are declared to return \
101 a pointer (e.g., mmap). */ \
104 # elif defined _LIBC_REENTRANT
106 # define SYSCALL_ERROR_ERRNO __libc_errno
108 # define SYSCALL_ERROR_ERRNO errno
110 # define SYSCALL_ERROR_HANDLER \
111 SYSCALL_ERROR_LABEL: \
113 move.l %d0, -(%sp); \
114 cfi_adjust_cfa_offset (4); \
115 jbsr __m68k_read_tp@PLTPC; \
116 SYSCALL_ERROR_LOAD_GOT (%a1); \
117 add.l (SYSCALL_ERROR_ERRNO@TLSIE, %a1), %a0; \
118 move.l (%sp)+, (%a0); \
119 cfi_adjust_cfa_offset (-4); \
121 /* Copy return value to %a0 for syscalls that are declared to return \
122 a pointer (e.g., mmap). */ \
125 # else /* !_LIBC_REENTRANT */
126 /* Store (- %d0) into errno through the GOT. */
127 # define SYSCALL_ERROR_HANDLER \
128 SYSCALL_ERROR_LABEL: \
129 move.l (errno@GOTPC, %pc), %a0; \
133 /* Copy return value to %a0 for syscalls that are declared to return \
134 a pointer (e.g., mmap). */ \
137 # endif /* _LIBC_REENTRANT */
139 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
142 /* Linux takes system call arguments in registers:
144 syscall number %d0 call-clobbered
145 arg 1 %d1 call-clobbered
150 arg 6 %a0 call-clobbered
152 The stack layout upon entering the function is:
162 (Of course a function with say 3 arguments does not have entries for
165 Separate move's are faster than movem, but need more space. Since
166 speed is more important, we don't use movem. Since %a0 and %a1 are
167 scratch registers, we can use them for saving as well. */
169 #define DO_CALL(syscall_name, args) \
170 move.l &SYS_ify(syscall_name), %d0; \
175 #define DOARGS_0 /* No arguments to frob. */
176 #define UNDOARGS_0 /* No arguments to unfrob. */
177 #define _DOARGS_0(n) /* No arguments to frob. */
179 #define DOARGS_1 _DOARGS_1 (4)
180 #define _DOARGS_1(n) move.l n(%sp), %d1; _DOARGS_0 (n)
181 #define UNDOARGS_1 UNDOARGS_0
183 #define DOARGS_2 _DOARGS_2 (8)
184 #define _DOARGS_2(n) move.l %d2, %a0; cfi_register (%d2, %a0); \
185 move.l n(%sp), %d2; _DOARGS_1 (n-4)
186 #define UNDOARGS_2 UNDOARGS_1; move.l %a0, %d2; cfi_restore (%d2)
188 #define DOARGS_3 _DOARGS_3 (12)
189 #define _DOARGS_3(n) move.l %d3, %a1; cfi_register (%d3, %a1); \
190 move.l n(%sp), %d3; _DOARGS_2 (n-4)
191 #define UNDOARGS_3 UNDOARGS_2; move.l %a1, %d3; cfi_restore (%d3)
193 #define DOARGS_4 _DOARGS_4 (16)
194 #define _DOARGS_4(n) move.l %d4, -(%sp); \
195 cfi_adjust_cfa_offset (4); cfi_rel_offset (%d4, 0); \
196 move.l n+4(%sp), %d4; _DOARGS_3 (n)
197 #define UNDOARGS_4 UNDOARGS_3; move.l (%sp)+, %d4; \
198 cfi_adjust_cfa_offset (-4); cfi_restore (%d4)
200 #define DOARGS_5 _DOARGS_5 (20)
201 #define _DOARGS_5(n) move.l %d5, -(%sp); \
202 cfi_adjust_cfa_offset (4); cfi_rel_offset (%d5, 0); \
203 move.l n+4(%sp), %d5; _DOARGS_4 (n)
204 #define UNDOARGS_5 UNDOARGS_4; move.l (%sp)+, %d5; \
205 cfi_adjust_cfa_offset (-4); cfi_restore (%d5)
207 #define DOARGS_6 _DOARGS_6 (24)
208 #define _DOARGS_6(n) _DOARGS_5 (n-4); move.l %a0, -(%sp); \
209 cfi_adjust_cfa_offset (4); \
210 move.l n+12(%sp), %a0;
211 #define UNDOARGS_6 move.l (%sp)+, %a0; cfi_adjust_cfa_offset (-4); \
216 #if 0 /* Not used by Linux */
219 #define MOVE(x,y) movel x , y
222 #else /* not __ASSEMBLER__ */
224 /* Define a macro which expands into the inline wrapper code for a system
226 #undef INLINE_SYSCALL
227 #define INLINE_SYSCALL(name, nr, args...) \
228 ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
229 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
231 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
232 _sys_result = (unsigned int) -1; \
234 (int) _sys_result; })
236 #undef INTERNAL_SYSCALL_DECL
237 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
239 /* Define a macro which expands inline into the wrapper code for a system
240 call. This use is for internal calls that do not need to handle errors
241 normally. It will never touch errno. This returns just what the kernel
243 #undef INTERNAL_SYSCALL
244 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
245 ({ unsigned int _sys_result; \
247 /* Load argument values in temporary variables
248 to perform side effects like function calls
249 before the call used registers are set. */ \
250 LOAD_ARGS_##nr (args) \
252 register int _d0 asm ("%d0") = name; \
253 asm volatile ("trap #0" \
255 : "0" (_d0) ASM_ARGS_##nr \
259 (int) _sys_result; })
260 #define INTERNAL_SYSCALL(name, err, nr, args...) \
261 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
263 #undef INTERNAL_SYSCALL_ERROR_P
264 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
265 ((unsigned int) (val) >= -4095U)
267 #undef INTERNAL_SYSCALL_ERRNO
268 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
270 #define LOAD_ARGS_0()
273 #define LOAD_ARGS_1(a1) \
275 int __arg1 = (int) (a1);
276 #define LOAD_REGS_1 \
277 register int _d1 asm ("d1") = __arg1; \
279 #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
280 #define LOAD_ARGS_2(a1, a2) \
282 int __arg2 = (int) (a2);
283 #define LOAD_REGS_2 \
284 register int _d2 asm ("d2") = __arg2; \
286 #define ASM_ARGS_2 ASM_ARGS_1, "d" (_d2)
287 #define LOAD_ARGS_3(a1, a2, a3) \
288 LOAD_ARGS_2 (a1, a2) \
289 int __arg3 = (int) (a3);
290 #define LOAD_REGS_3 \
291 register int _d3 asm ("d3") = __arg3; \
293 #define ASM_ARGS_3 ASM_ARGS_2, "d" (_d3)
294 #define LOAD_ARGS_4(a1, a2, a3, a4) \
295 LOAD_ARGS_3 (a1, a2, a3) \
296 int __arg4 = (int) (a4);
297 #define LOAD_REGS_4 \
298 register int _d4 asm ("d4") = __arg4; \
300 #define ASM_ARGS_4 ASM_ARGS_3, "d" (_d4)
301 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
302 LOAD_ARGS_4 (a1, a2, a3, a4) \
303 int __arg5 = (int) (a5);
304 #define LOAD_REGS_5 \
305 register int _d5 asm ("d5") = __arg5; \
307 #define ASM_ARGS_5 ASM_ARGS_4, "d" (_d5)
308 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
309 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
310 int __arg6 = (int) (a6);
311 #define LOAD_REGS_6 \
312 register int _a0 asm ("a0") = __arg6; \
314 #define ASM_ARGS_6 ASM_ARGS_5, "a" (_a0)
316 #endif /* not __ASSEMBLER__ */
318 /* Pointer mangling is not yet supported for M68K. */
319 #define PTR_MANGLE(var) (void) (var)
320 #define PTR_DEMANGLE(var) (void) (var)
322 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
323 /* M68K needs system-supplied DSO to access TLS helpers
324 even when statically linked. */
325 # define NEED_STATIC_SYSINFO_DSO 1