1 /* Assembler macros for ARM.
2 Copyright (C) 1997, 1998, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #ifndef _LINUX_ARM_SYSDEP_H
21 #define _LINUX_ARM_SYSDEP_H 1
23 #include <common/sysdep.h>
24 #include <bits/arm_asm.h>
26 #include <sys/syscall.h>
27 /* For Linux we can use the system call table in the header file
28 /usr/include/asm/unistd.h
29 of the kernel. But these symbols do not follow the SYS_* syntax
30 so we have to redefine the `SYS_ify' macro here. */
32 #define SWI_BASE (0x900000)
33 #define SYS_ify(syscall_name) (__NR_##syscall_name)
37 /* Syntactic details of assembler. */
39 #define ALIGNARG(log2) log2
40 /* For ELF we need the `.type' directive to make shared libs work right. */
41 #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,%##typearg;
42 #define ASM_SIZE_DIRECTIVE(name) .size name,.-name
44 /* In ELF C symbols are asm symbols. */
46 #define NO_UNDERSCORES
48 #define PLTJMP(_x) _x##(PLT)
50 /* APCS-32 doesn't preserve the condition codes across function call. */
52 #define LOADREGS(cond, base, reglist...)\
53 ldm##cond base,reglist
55 #define RETINSTR(cond, reg) \
57 #define DO_RET(_reg) \
60 #define RETINSTR(cond, reg) \
62 #define DO_RET(_reg) \
66 #define LOADREGS(cond, base, reglist...) \
67 ldm##cond base,reglist^
68 #define RETINSTR(cond, reg) \
70 #define DO_RET(_reg) \
74 /* Define an entry point visible from C. */
76 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
77 ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),function) \
84 ASM_SIZE_DIRECTIVE(name)
86 /* If compiled for profiling, call `mcount' at the start of each function. */
93 #define CALL_MCOUNT /* Do nothing. */
97 /* Since C identifiers are not normally prefixed with an underscore
98 on this system, the asm identifier `syscall_error' intrudes on the
99 C name space. Make sure we use an innocuous name. */
100 #define syscall_error __syscall_error
101 #define mcount _mcount
103 /* Linux uses a negative return value to indicate syscall errors,
104 unlike most Unices, which use the condition codes' carry flag.
106 Since version 2.1 the return value of a system call might be
107 negative even if the call succeeded. E.g., the `lseek' system call
108 might return a large offset. Therefore we must not anymore test
109 for < 0, but test for a real error by making sure the value in R0
110 is a real error number. Linus said he will make sure the no syscall
111 returns a value in -1 .. -4095 as a valid result so we can safely
115 #define PSEUDO(name, syscall_name, args) \
118 DO_CALL (syscall_name, args); \
123 b PLTJMP(SYSCALL_ERROR)
125 #define ret PSEUDO_RET
128 #define PSEUDO_END(name) \
129 SYSCALL_ERROR_HANDLER \
132 #undef PSEUDO_NOERRNO
133 #define PSEUDO_NOERRNO(name, syscall_name, args) \
136 DO_CALL (syscall_name, args);
138 #define PSEUDO_RET_NOERRNO \
142 #define ret_NOERRNO PSEUDO_RET_NOERRNO
144 #undef PSEUDO_END_NOERRNO
145 #define PSEUDO_END_NOERRNO(name) \
148 /* The function has to return the error code. */
150 #define PSEUDO_ERRVAL(name, syscall_name, args) \
153 DO_CALL (syscall_name, args); \
156 #undef PSEUDO_END_ERRVAL
157 #define PSEUDO_END_ERRVAL(name) \
160 #define ret_ERRVAL PSEUDO_RET_NOERRNO
162 #if defined NOT_IN_libc
163 # define SYSCALL_ERROR __local_syscall_error
164 # ifdef RTLD_PRIVATE_ERRNO
165 # define SYSCALL_ERROR_HANDLER \
166 __local_syscall_error: \
169 0: str r0, [pc, r1]; \
172 1: .word C_SYMBOL_NAME(rtld_errno) - 0b - 8;
174 # define SYSCALL_ERROR_HANDLER \
175 __local_syscall_error: \
176 str lr, [sp, #-4]!; \
177 str r0, [sp, #-4]!; \
178 bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
186 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
187 # define SYSCALL_ERROR __syscall_error
190 /* Linux takes system call args in registers:
191 syscall number in the SWI instruction
196 arg 5 r4 (this is different from the APCS convention)
200 The compiler is going to form a call by coming here, through PSEUDO, with
202 syscall number in the DO_CALL macro
211 We need to shuffle values between R4..R6 and the stack so that the
212 caller's v1..v3 and stack frame are not corrupted, and the kernel
213 sees the right arguments.
218 #if defined(__ARM_EABI__)
219 #define DO_CALL(syscall_name, args) \
222 ldr r7, =SYS_ify (syscall_name); \
227 #define DO_CALL(syscall_name, args) \
229 swi SYS_ify (syscall_name); \
233 #define DOARGS_0 /* nothing */
234 #define DOARGS_1 /* nothing */
235 #define DOARGS_2 /* nothing */
236 #define DOARGS_3 /* nothing */
237 #define DOARGS_4 /* nothing */
238 #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
239 #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
240 #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
242 #define UNDOARGS_0 /* nothing */
243 #define UNDOARGS_1 /* nothing */
244 #define UNDOARGS_2 /* nothing */
245 #define UNDOARGS_3 /* nothing */
246 #define UNDOARGS_4 /* nothing */
247 #define UNDOARGS_5 ldr r4, [sp], $4;
248 #define UNDOARGS_6 ldmfd sp!, {r4, r5};
249 #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
251 #else /* not __ASSEMBLER__ */
252 /* Define a macro which expands into the inline wrapper code for a system
254 #undef INLINE_SYSCALL
255 #define INLINE_SYSCALL(name, nr, args...) \
256 ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
257 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ), 0)) \
259 __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
260 _inline_sys_result = (unsigned int) -1; \
262 (int) _inline_sys_result; })
264 #undef INTERNAL_SYSCALL_DECL
265 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
267 #undef INTERNAL_SYSCALL_RAW
268 #if defined(__thumb__)
269 /* Hide the use of r7 from the compiler, this would be a lot
270 * easier but for the fact that the syscalls can exceed 255.
271 * For the moment the LOAD_ARG_7 is sacrificed.
272 * We can't use push/pop inside the asm because that breaks
273 * unwinding (ie. thread cancellation).
275 #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
276 ({ unsigned int _internal_sys_result; \
279 register int __a1 __asm__ ("a1"); \
280 register int *_v3 __asm__ ("v3") = _sys_buf; \
281 LOAD_ARGS_##nr (args) \
282 *_v3 = (int) (name); \
283 __asm__ __volatile__ ("str r7, [v3, #4]\n" \
285 "\tswi 0 @ syscall " #name "\n" \
286 "\tldr r7, [v3, #4]" \
288 : "r" (_v3) ASM_ARGS_##nr \
290 _internal_sys_result = __a1; \
292 (int) _internal_sys_result; })
293 #elif defined(__ARM_EABI__)
294 #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
295 ({unsigned int _internal_sys_result; \
297 register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
298 LOAD_ARGS_##nr (args) \
300 __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
302 : "r" (_nr) ASM_ARGS_##nr \
304 _internal_sys_result = __a1; \
306 (int) _internal_sys_result; })
307 #else /* !defined(__ARM_EABI__) */
308 #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
309 ({ unsigned int _internal_sys_result; \
311 register int __a1 __asm__ ("a1"); \
312 LOAD_ARGS_##nr (args) \
313 __asm__ __volatile__ ("swi %1 @ syscall " #name \
315 : "i" (name) ASM_ARGS_##nr \
317 _internal_sys_result = __a1; \
319 (int) _internal_sys_result; })
322 #undef INTERNAL_SYSCALL
323 #define INTERNAL_SYSCALL(name, err, nr, args...) \
324 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
326 #undef INTERNAL_SYSCALL_ARM
327 #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
328 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
330 #undef INTERNAL_SYSCALL_ERROR_P
331 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
332 ((unsigned int) (val) >= 0xfffff001u)
334 #undef INTERNAL_SYSCALL_ERRNO
335 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
337 #if defined(__ARM_EABI__)
338 #undef INTERNAL_SYSCALL_NCS
339 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
340 INTERNAL_SYSCALL_RAW(number, err, nr, args)
342 /* We can't implement non-constant syscalls directly since the syscall
343 number is normally encoded in the instruction. So use SYS_syscall. */
344 #undef INTERNAL_SYSCALL_NCS
345 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
346 INTERNAL_SYSCALL_NCS_##nr (number, err, args)
348 #define INTERNAL_SYSCALL_NCS_0(number, err, args...) \
349 INTERNAL_SYSCALL (syscall, err, 1, number, args)
350 #define INTERNAL_SYSCALL_NCS_1(number, err, args...) \
351 INTERNAL_SYSCALL (syscall, err, 2, number, args)
352 #define INTERNAL_SYSCALL_NCS_2(number, err, args...) \
353 INTERNAL_SYSCALL (syscall, err, 3, number, args)
354 #define INTERNAL_SYSCALL_NCS_3(number, err, args...) \
355 INTERNAL_SYSCALL (syscall, err, 4, number, args)
356 #define INTERNAL_SYSCALL_NCS_4(number, err, args...) \
357 INTERNAL_SYSCALL (syscall, err, 5, number, args)
358 #define INTERNAL_SYSCALL_NCS_5(number, err, args...) \
359 INTERNAL_SYSCALL (syscall, err, 6, number, args)
362 #endif /* __ASSEMBLER__ */
364 /* Pointer mangling is not yet supported for ARM. */
365 #define PTR_MANGLE(var) (void) (var)
366 #define PTR_DEMANGLE(var) (void) (var)
368 #endif /* linux/arm/sysdep.h */