Move all files into ports/ subdirectory in preparation for merge with glibc
[glibc.git] / ports / sysdeps / unix / sysv / linux / m68k / sysdep.h
blobb288712541ab91ec6d71682ba49e868f02c80be5
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>,
5 December 1995.
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/>. */
21 #include <tls.h>
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. */
30 #undef SYS_ify
31 #define SYS_ify(syscall_name) __NR_##syscall_name
33 #ifdef __ASSEMBLER__
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. */
47 #ifdef PIC
48 #define SYSCALL_ERROR_LABEL .Lsyscall_error
49 #else
50 #define SYSCALL_ERROR_LABEL __syscall_error
51 #endif
53 #undef PSEUDO
54 #define PSEUDO(name, syscall_name, args) \
55 .text; \
56 ENTRY (name) \
57 DO_CALL (syscall_name, args); \
58 cmp.l &-4095, %d0; \
59 jcc SYSCALL_ERROR_LABEL
61 #undef PSEUDO_END
62 #define PSEUDO_END(name) \
63 SYSCALL_ERROR_HANDLER; \
64 END (name)
66 #undef PSEUDO_NOERRNO
67 #define PSEUDO_NOERRNO(name, syscall_name, args) \
68 .text; \
69 ENTRY (name) \
70 DO_CALL (syscall_name, args)
72 #undef PSEUDO_END_NOERRNO
73 #define PSEUDO_END_NOERRNO(name) \
74 END (name)
76 #define ret_NOERRNO rts
78 /* The function has to return the error code. */
79 #undef PSEUDO_ERRVAL
80 #define PSEUDO_ERRVAL(name, syscall_name, args) \
81 .text; \
82 ENTRY (name) \
83 DO_CALL (syscall_name, args); \
84 negl %d0
86 #undef PSEUDO_END_ERRVAL
87 #define PSEUDO_END_ERRVAL(name) \
88 END (name)
90 #define ret_ERRVAL rts
92 #ifdef PIC
93 # if RTLD_PRIVATE_ERRNO
94 # define SYSCALL_ERROR_HANDLER \
95 SYSCALL_ERROR_LABEL: \
96 PCREL_OP (lea, rtld_errno, %a0, %a0); \
97 neg.l %d0; \
98 move.l %d0, (%a0); \
99 move.l &-1, %d0; \
100 /* Copy return value to %a0 for syscalls that are declared to return \
101 a pointer (e.g., mmap). */ \
102 move.l %d0, %a0; \
103 rts;
104 # elif defined _LIBC_REENTRANT
105 # ifndef NOT_IN_libc
106 # define SYSCALL_ERROR_ERRNO __libc_errno
107 # else
108 # define SYSCALL_ERROR_ERRNO errno
109 # endif
110 # define SYSCALL_ERROR_HANDLER \
111 SYSCALL_ERROR_LABEL: \
112 neg.l %d0; \
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); \
120 move.l &-1, %d0; \
121 /* Copy return value to %a0 for syscalls that are declared to return \
122 a pointer (e.g., mmap). */ \
123 move.l %d0, %a0; \
124 rts;
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; \
130 neg.l %d0; \
131 move.l %d0, (%a0); \
132 move.l &-1, %d0; \
133 /* Copy return value to %a0 for syscalls that are declared to return \
134 a pointer (e.g., mmap). */ \
135 move.l %d0, %a0; \
136 rts;
137 # endif /* _LIBC_REENTRANT */
138 #else
139 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
140 #endif /* PIC */
142 /* Linux takes system call arguments in registers:
144 syscall number %d0 call-clobbered
145 arg 1 %d1 call-clobbered
146 arg 2 %d2 call-saved
147 arg 3 %d3 call-saved
148 arg 4 %d4 call-saved
149 arg 5 %d5 call-saved
150 arg 6 %a0 call-clobbered
152 The stack layout upon entering the function is:
154 24(%sp) Arg# 6
155 20(%sp) Arg# 5
156 16(%sp) Arg# 4
157 12(%sp) Arg# 3
158 8(%sp) Arg# 2
159 4(%sp) Arg# 1
160 (%sp) Return address
162 (Of course a function with say 3 arguments does not have entries for
163 arguments 4 and 5.)
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; \
171 DOARGS_##args \
172 trap &0; \
173 UNDOARGS_##args
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); \
212 UNDOARGS_5
215 #define ret rts
216 #if 0 /* Not used by Linux */
217 #define r0 %d0
218 #define r1 %d1
219 #define MOVE(x,y) movel x , y
220 #endif
222 #else /* not __ASSEMBLER__ */
224 /* Define a macro which expands into the inline wrapper code for a system
225 call. */
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
242 gave back. */
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) \
251 LOAD_REGS_##nr \
252 register int _d0 asm ("%d0") = name; \
253 asm volatile ("trap #0" \
254 : "=d" (_d0) \
255 : "0" (_d0) ASM_ARGS_##nr \
256 : "memory"); \
257 _sys_result = _d0; \
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()
271 #define LOAD_REGS_0
272 #define ASM_ARGS_0
273 #define LOAD_ARGS_1(a1) \
274 LOAD_ARGS_0 () \
275 int __arg1 = (int) (a1);
276 #define LOAD_REGS_1 \
277 register int _d1 asm ("d1") = __arg1; \
278 LOAD_REGS_0
279 #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
280 #define LOAD_ARGS_2(a1, a2) \
281 LOAD_ARGS_1 (a1) \
282 int __arg2 = (int) (a2);
283 #define LOAD_REGS_2 \
284 register int _d2 asm ("d2") = __arg2; \
285 LOAD_REGS_1
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; \
292 LOAD_REGS_2
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; \
299 LOAD_REGS_3
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; \
306 LOAD_REGS_4
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; \
313 LOAD_REGS_5
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
326 #endif