Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / m68k / sysdep.h
blob3a91e867f39a5a0d002fe799442a58100063fb78
1 /* Copyright (C) 1996-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>,
4 December 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <tls.h>
22 /* Defines RTLD_PRIVATE_ERRNO. */
23 #include <dl-sysdep.h>
25 /* For Linux we can use the system call table in the header file
26 /usr/include/asm/unistd.h
27 of the kernel. But these symbols do not follow the SYS_* syntax
28 so we have to redefine the `SYS_ify' macro here. */
29 #undef SYS_ify
30 #define SYS_ify(syscall_name) __NR_##syscall_name
32 #ifdef __ASSEMBLER__
34 /* Linux uses a negative return value to indicate syscall errors, unlike
35 most Unices, which use the condition codes' carry flag.
37 Since version 2.1 the return value of a system call might be negative
38 even if the call succeeded. E.g., the `lseek' system call might return
39 a large offset. Therefore we must not anymore test for < 0, but test
40 for a real error by making sure the value in %d0 is a real error
41 number. Linus said he will make sure the no syscall returns a value
42 in -1 .. -4095 as a valid result so we can savely test with -4095. */
44 /* We don't want the label for the error handler to be visible in the symbol
45 table when we define it here. */
46 #ifdef PIC
47 #define SYSCALL_ERROR_LABEL .Lsyscall_error
48 #else
49 #define SYSCALL_ERROR_LABEL __syscall_error
50 #endif
52 #undef PSEUDO
53 #define PSEUDO(name, syscall_name, args) \
54 .text; \
55 ENTRY (name) \
56 DO_CALL (syscall_name, args); \
57 cmp.l &-4095, %d0; \
58 jcc SYSCALL_ERROR_LABEL
60 #undef PSEUDO_END
61 #define PSEUDO_END(name) \
62 SYSCALL_ERROR_HANDLER; \
63 END (name)
65 #undef PSEUDO_NOERRNO
66 #define PSEUDO_NOERRNO(name, syscall_name, args) \
67 .text; \
68 ENTRY (name) \
69 DO_CALL (syscall_name, args)
71 #undef PSEUDO_END_NOERRNO
72 #define PSEUDO_END_NOERRNO(name) \
73 END (name)
75 #define ret_NOERRNO rts
77 /* The function has to return the error code. */
78 #undef PSEUDO_ERRVAL
79 #define PSEUDO_ERRVAL(name, syscall_name, args) \
80 .text; \
81 ENTRY (name) \
82 DO_CALL (syscall_name, args); \
83 negl %d0
85 #undef PSEUDO_END_ERRVAL
86 #define PSEUDO_END_ERRVAL(name) \
87 END (name)
89 #define ret_ERRVAL rts
91 #ifdef PIC
92 # if RTLD_PRIVATE_ERRNO
93 # define SYSCALL_ERROR_HANDLER \
94 SYSCALL_ERROR_LABEL: \
95 PCREL_OP (lea, rtld_errno, %a0, %a0); \
96 neg.l %d0; \
97 move.l %d0, (%a0); \
98 move.l &-1, %d0; \
99 /* Copy return value to %a0 for syscalls that are declared to return \
100 a pointer (e.g., mmap). */ \
101 move.l %d0, %a0; \
102 rts;
103 # elif defined _LIBC_REENTRANT
104 # ifndef NOT_IN_libc
105 # define SYSCALL_ERROR_ERRNO __libc_errno
106 # else
107 # define SYSCALL_ERROR_ERRNO errno
108 # endif
109 # define SYSCALL_ERROR_HANDLER \
110 SYSCALL_ERROR_LABEL: \
111 neg.l %d0; \
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); \
119 move.l &-1, %d0; \
120 /* Copy return value to %a0 for syscalls that are declared to return \
121 a pointer (e.g., mmap). */ \
122 move.l %d0, %a0; \
123 rts;
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; \
129 neg.l %d0; \
130 move.l %d0, (%a0); \
131 move.l &-1, %d0; \
132 /* Copy return value to %a0 for syscalls that are declared to return \
133 a pointer (e.g., mmap). */ \
134 move.l %d0, %a0; \
135 rts;
136 # endif /* _LIBC_REENTRANT */
137 #else
138 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
139 #endif /* PIC */
141 /* Linux takes system call arguments in registers:
143 syscall number %d0 call-clobbered
144 arg 1 %d1 call-clobbered
145 arg 2 %d2 call-saved
146 arg 3 %d3 call-saved
147 arg 4 %d4 call-saved
148 arg 5 %d5 call-saved
149 arg 6 %a0 call-clobbered
151 The stack layout upon entering the function is:
153 24(%sp) Arg# 6
154 20(%sp) Arg# 5
155 16(%sp) Arg# 4
156 12(%sp) Arg# 3
157 8(%sp) Arg# 2
158 4(%sp) Arg# 1
159 (%sp) Return address
161 (Of course a function with say 3 arguments does not have entries for
162 arguments 4 and 5.)
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; \
170 DOARGS_##args \
171 trap &0; \
172 UNDOARGS_##args
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); \
211 UNDOARGS_5
214 #define ret rts
215 #if 0 /* Not used by Linux */
216 #define r0 %d0
217 #define r1 %d1
218 #define MOVE(x,y) movel x , y
219 #endif
221 #else /* not __ASSEMBLER__ */
223 /* Define a macro which expands into the inline wrapper code for a system
224 call. */
225 #undef INLINE_SYSCALL
226 #define INLINE_SYSCALL(name, nr, args...) \
227 ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
228 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
230 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
231 _sys_result = (unsigned int) -1; \
233 (int) _sys_result; })
235 #undef INTERNAL_SYSCALL_DECL
236 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
238 /* Define a macro which expands inline into the wrapper code for a system
239 call. This use is for internal calls that do not need to handle errors
240 normally. It will never touch errno. This returns just what the kernel
241 gave back. */
242 #undef INTERNAL_SYSCALL
243 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
244 ({ unsigned int _sys_result; \
246 /* Load argument values in temporary variables
247 to perform side effects like function calls
248 before the call used registers are set. */ \
249 LOAD_ARGS_##nr (args) \
250 LOAD_REGS_##nr \
251 register int _d0 asm ("%d0") = name; \
252 asm volatile ("trap #0" \
253 : "=d" (_d0) \
254 : "0" (_d0) ASM_ARGS_##nr \
255 : "memory"); \
256 _sys_result = _d0; \
258 (int) _sys_result; })
259 #define INTERNAL_SYSCALL(name, err, nr, args...) \
260 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
262 #undef INTERNAL_SYSCALL_ERROR_P
263 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
264 ((unsigned int) (val) >= -4095U)
266 #undef INTERNAL_SYSCALL_ERRNO
267 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
269 #define LOAD_ARGS_0()
270 #define LOAD_REGS_0
271 #define ASM_ARGS_0
272 #define LOAD_ARGS_1(a1) \
273 LOAD_ARGS_0 () \
274 int __arg1 = (int) (a1);
275 #define LOAD_REGS_1 \
276 register int _d1 asm ("d1") = __arg1; \
277 LOAD_REGS_0
278 #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
279 #define LOAD_ARGS_2(a1, a2) \
280 LOAD_ARGS_1 (a1) \
281 int __arg2 = (int) (a2);
282 #define LOAD_REGS_2 \
283 register int _d2 asm ("d2") = __arg2; \
284 LOAD_REGS_1
285 #define ASM_ARGS_2 ASM_ARGS_1, "d" (_d2)
286 #define LOAD_ARGS_3(a1, a2, a3) \
287 LOAD_ARGS_2 (a1, a2) \
288 int __arg3 = (int) (a3);
289 #define LOAD_REGS_3 \
290 register int _d3 asm ("d3") = __arg3; \
291 LOAD_REGS_2
292 #define ASM_ARGS_3 ASM_ARGS_2, "d" (_d3)
293 #define LOAD_ARGS_4(a1, a2, a3, a4) \
294 LOAD_ARGS_3 (a1, a2, a3) \
295 int __arg4 = (int) (a4);
296 #define LOAD_REGS_4 \
297 register int _d4 asm ("d4") = __arg4; \
298 LOAD_REGS_3
299 #define ASM_ARGS_4 ASM_ARGS_3, "d" (_d4)
300 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
301 LOAD_ARGS_4 (a1, a2, a3, a4) \
302 int __arg5 = (int) (a5);
303 #define LOAD_REGS_5 \
304 register int _d5 asm ("d5") = __arg5; \
305 LOAD_REGS_4
306 #define ASM_ARGS_5 ASM_ARGS_4, "d" (_d5)
307 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
308 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
309 int __arg6 = (int) (a6);
310 #define LOAD_REGS_6 \
311 register int _a0 asm ("a0") = __arg6; \
312 LOAD_REGS_5
313 #define ASM_ARGS_6 ASM_ARGS_5, "a" (_a0)
315 #endif /* not __ASSEMBLER__ */
317 /* Pointer mangling is not yet supported for M68K. */
318 #define PTR_MANGLE(var) (void) (var)
319 #define PTR_DEMANGLE(var) (void) (var)
321 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
322 /* M68K needs system-supplied DSO to access TLS helpers
323 even when statically linked. */
324 # define NEED_STATIC_SYSINFO_DSO 1
325 #endif