sysdep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.
  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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _LINUX_ARM_SYSDEP_H
  17. #define _LINUX_ARM_SYSDEP_H 1
  18. #include <common/sysdep.h>
  19. #include <sys/syscall.h>
  20. /* For Linux we can use the system call table in the header file
  21. /usr/include/asm/unistd.h
  22. of the kernel. But these symbols do not follow the SYS_* syntax
  23. so we have to redefine the `SYS_ify' macro here. */
  24. #undef SYS_ify
  25. #define SWI_BASE (0x900000)
  26. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  27. #ifdef __ASSEMBLER__
  28. /* Syntactic details of assembler. */
  29. #define ALIGNARG(log2) log2
  30. /* For ELF we need the `.type' directive to make shared libs work right. */
  31. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,%##typearg;
  32. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name
  33. /* In ELF C symbols are asm symbols. */
  34. #undef NO_UNDERSCORES
  35. #define NO_UNDERSCORES
  36. #define PLTJMP(_x) _x##(PLT)
  37. /* APCS-32 doesn't preserve the condition codes across function call. */
  38. #ifdef __APCS_32__
  39. #define LOADREGS(cond, base, reglist...)\
  40. ldm##cond base,reglist
  41. #ifdef __USE_BX__
  42. #define RETINSTR(cond, reg) \
  43. bx##cond reg
  44. #define DO_RET(_reg) \
  45. bx _reg
  46. #else
  47. #define RETINSTR(cond, reg) \
  48. mov##cond pc, reg
  49. #define DO_RET(_reg) \
  50. mov pc, _reg
  51. #endif
  52. #else /* APCS-26 */
  53. #define LOADREGS(cond, base, reglist...) \
  54. ldm##cond base,reglist^
  55. #define RETINSTR(cond, reg) \
  56. mov##cond##s pc, reg
  57. #define DO_RET(_reg) \
  58. movs pc, _reg
  59. #endif
  60. /* Define an entry point visible from C. */
  61. #define ENTRY(name) \
  62. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  63. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),function) \
  64. .align ALIGNARG(4); \
  65. name##: \
  66. CALL_MCOUNT
  67. #undef END
  68. #define END(name) \
  69. ASM_SIZE_DIRECTIVE(name)
  70. /* If compiled for profiling, call `mcount' at the start of each function. */
  71. #ifdef PROF
  72. #define CALL_MCOUNT \
  73. str lr,[sp, #-4]! ; \
  74. bl PLTJMP(mcount) ; \
  75. ldr lr, [sp], #4 ;
  76. #else
  77. #define CALL_MCOUNT /* Do nothing. */
  78. #endif
  79. #ifdef NO_UNDERSCORES
  80. /* Since C identifiers are not normally prefixed with an underscore
  81. on this system, the asm identifier `syscall_error' intrudes on the
  82. C name space. Make sure we use an innocuous name. */
  83. #define syscall_error __syscall_error
  84. #define mcount _mcount
  85. #endif
  86. /* Linux uses a negative return value to indicate syscall errors,
  87. unlike most Unices, which use the condition codes' carry flag.
  88. Since version 2.1 the return value of a system call might be
  89. negative even if the call succeeded. E.g., the `lseek' system call
  90. might return a large offset. Therefore we must not anymore test
  91. for < 0, but test for a real error by making sure the value in R0
  92. is a real error number. Linus said he will make sure the no syscall
  93. returns a value in -1 .. -4095 as a valid result so we can safely
  94. test with -4095. */
  95. #undef PSEUDO
  96. #define PSEUDO(name, syscall_name, args) \
  97. .text; \
  98. ENTRY (name); \
  99. DO_CALL (syscall_name, args); \
  100. cmn r0, $4096;
  101. #define PSEUDO_RET \
  102. RETINSTR(cc, lr); \
  103. b PLTJMP(SYSCALL_ERROR)
  104. #undef ret
  105. #define ret PSEUDO_RET
  106. #undef PSEUDO_END
  107. #define PSEUDO_END(name) \
  108. SYSCALL_ERROR_HANDLER \
  109. END (name)
  110. #undef PSEUDO_NOERRNO
  111. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  112. .text; \
  113. ENTRY (name); \
  114. DO_CALL (syscall_name, args);
  115. #define PSEUDO_RET_NOERRNO \
  116. DO_RET (lr);
  117. #undef ret_NOERRNO
  118. #define ret_NOERRNO PSEUDO_RET_NOERRNO
  119. #undef PSEUDO_END_NOERRNO
  120. #define PSEUDO_END_NOERRNO(name) \
  121. END (name)
  122. /* The function has to return the error code. */
  123. #undef PSEUDO_ERRVAL
  124. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  125. .text; \
  126. ENTRY (name) \
  127. DO_CALL (syscall_name, args); \
  128. rsb r0, r0, #0
  129. #undef PSEUDO_END_ERRVAL
  130. #define PSEUDO_END_ERRVAL(name) \
  131. END (name)
  132. #define ret_ERRVAL PSEUDO_RET_NOERRNO
  133. #if defined NOT_IN_libc
  134. # define SYSCALL_ERROR __local_syscall_error
  135. # ifdef RTLD_PRIVATE_ERRNO
  136. # define SYSCALL_ERROR_HANDLER \
  137. __local_syscall_error: \
  138. ldr r1, 1f; \
  139. rsb r0, r0, #0; \
  140. 0: str r0, [pc, r1]; \
  141. mvn r0, #0; \
  142. DO_RET(lr); \
  143. 1: .word C_SYMBOL_NAME(rtld_errno) - 0b - 8;
  144. # else
  145. # define SYSCALL_ERROR_HANDLER \
  146. __local_syscall_error: \
  147. str lr, [sp, #-4]!; \
  148. str r0, [sp, #-4]!; \
  149. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  150. ldr r1, [sp], #4; \
  151. rsb r1, r1, #0; \
  152. str r1, [r0]; \
  153. mvn r0, #0; \
  154. ldr pc, [sp], #4;
  155. # endif
  156. #else
  157. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  158. # define SYSCALL_ERROR __syscall_error
  159. #endif
  160. /* Linux takes system call args in registers:
  161. syscall number in the SWI instruction
  162. arg 1 r0
  163. arg 2 r1
  164. arg 3 r2
  165. arg 4 r3
  166. arg 5 r4 (this is different from the APCS convention)
  167. arg 6 r5
  168. arg 7 r6
  169. The compiler is going to form a call by coming here, through PSEUDO, with
  170. arguments
  171. syscall number in the DO_CALL macro
  172. arg 1 r0
  173. arg 2 r1
  174. arg 3 r2
  175. arg 4 r3
  176. arg 5 [sp]
  177. arg 6 [sp+4]
  178. arg 7 [sp+8]
  179. We need to shuffle values between R4..R6 and the stack so that the
  180. caller's v1..v3 and stack frame are not corrupted, and the kernel
  181. sees the right arguments.
  182. */
  183. #undef DO_CALL
  184. #if defined(__ARM_EABI__)
  185. #define DO_CALL(syscall_name, args) \
  186. DOARGS_##args \
  187. mov ip, r7; \
  188. ldr r7, =SYS_ify (syscall_name); \
  189. swi 0x0; \
  190. mov r7, ip; \
  191. UNDOARGS_##args
  192. #else
  193. #define DO_CALL(syscall_name, args) \
  194. DOARGS_##args \
  195. swi SYS_ify (syscall_name); \
  196. UNDOARGS_##args
  197. #endif
  198. #define DOARGS_0 /* nothing */
  199. #define DOARGS_1 /* nothing */
  200. #define DOARGS_2 /* nothing */
  201. #define DOARGS_3 /* nothing */
  202. #define DOARGS_4 /* nothing */
  203. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  204. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  205. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  206. #define UNDOARGS_0 /* nothing */
  207. #define UNDOARGS_1 /* nothing */
  208. #define UNDOARGS_2 /* nothing */
  209. #define UNDOARGS_3 /* nothing */
  210. #define UNDOARGS_4 /* nothing */
  211. #define UNDOARGS_5 ldr r4, [sp], $4;
  212. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  213. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  214. #else /* not __ASSEMBLER__ */
  215. /* Define a macro which expands into the inline wrapper code for a system
  216. call. */
  217. #undef INLINE_SYSCALL
  218. #define INLINE_SYSCALL(name, nr, args...) \
  219. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  220. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ), 0)) \
  221. { \
  222. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  223. _inline_sys_result = (unsigned int) -1; \
  224. } \
  225. (int) _inline_sys_result; })
  226. #undef INTERNAL_SYSCALL_DECL
  227. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  228. #undef INTERNAL_SYSCALL_RAW
  229. #if defined(__thumb__)
  230. /* Hide the use of r7 from the compiler, this would be a lot
  231. * easier but for the fact that the syscalls can exceed 255.
  232. * For the moment the LOAD_ARG_7 is sacrificed.
  233. * We can't use push/pop inside the asm because that breaks
  234. * unwinding (ie. thread cancellation).
  235. */
  236. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  237. ({ unsigned int _internal_sys_result; \
  238. { \
  239. int _sys_buf[2]; \
  240. register int __a1 __asm__ ("a1"); \
  241. register int *_v3 __asm__ ("v3") = _sys_buf; \
  242. LOAD_ARGS_##nr (args) \
  243. *_v3 = (int) (name); \
  244. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  245. "\tldr r7, [v3]\n" \
  246. "\tswi 0 @ syscall " #name "\n" \
  247. "\tldr r7, [v3, #4]" \
  248. : "=r" (__a1) \
  249. : "r" (_v3) ASM_ARGS_##nr \
  250. : "memory"); \
  251. _internal_sys_result = __a1; \
  252. } \
  253. (int) _internal_sys_result; })
  254. #elif defined(__ARM_EABI__)
  255. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  256. ({unsigned int _internal_sys_result; \
  257. { \
  258. register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  259. LOAD_ARGS_##nr (args) \
  260. _nr = name; \
  261. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  262. : "=r" (__a1) \
  263. : "r" (_nr) ASM_ARGS_##nr \
  264. : "memory"); \
  265. _internal_sys_result = __a1; \
  266. } \
  267. (int) _internal_sys_result; })
  268. #else /* !defined(__ARM_EABI__) */
  269. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  270. ({ unsigned int _internal_sys_result; \
  271. { \
  272. register int __a1 __asm__ ("a1"); \
  273. LOAD_ARGS_##nr (args) \
  274. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  275. : "=r" (__a1) \
  276. : "i" (name) ASM_ARGS_##nr \
  277. : "memory"); \
  278. _internal_sys_result = __a1; \
  279. } \
  280. (int) _internal_sys_result; })
  281. #endif
  282. #undef INTERNAL_SYSCALL
  283. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  284. INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
  285. #undef INTERNAL_SYSCALL_ARM
  286. #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
  287. INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
  288. #undef INTERNAL_SYSCALL_ERROR_P
  289. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  290. ((unsigned int) (val) >= 0xfffff001u)
  291. #undef INTERNAL_SYSCALL_ERRNO
  292. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  293. #if defined(__ARM_EABI__)
  294. #undef INTERNAL_SYSCALL_NCS
  295. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  296. INTERNAL_SYSCALL_RAW(number, err, nr, args)
  297. #else
  298. /* We can't implement non-constant syscalls directly since the syscall
  299. number is normally encoded in the instruction. So use SYS_syscall. */
  300. #undef INTERNAL_SYSCALL_NCS
  301. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  302. INTERNAL_SYSCALL_NCS_##nr (number, err, args)
  303. #define INTERNAL_SYSCALL_NCS_0(number, err, args...) \
  304. INTERNAL_SYSCALL (syscall, err, 1, number, args)
  305. #define INTERNAL_SYSCALL_NCS_1(number, err, args...) \
  306. INTERNAL_SYSCALL (syscall, err, 2, number, args)
  307. #define INTERNAL_SYSCALL_NCS_2(number, err, args...) \
  308. INTERNAL_SYSCALL (syscall, err, 3, number, args)
  309. #define INTERNAL_SYSCALL_NCS_3(number, err, args...) \
  310. INTERNAL_SYSCALL (syscall, err, 4, number, args)
  311. #define INTERNAL_SYSCALL_NCS_4(number, err, args...) \
  312. INTERNAL_SYSCALL (syscall, err, 5, number, args)
  313. #define INTERNAL_SYSCALL_NCS_5(number, err, args...) \
  314. INTERNAL_SYSCALL (syscall, err, 6, number, args)
  315. #endif
  316. #endif /* __ASSEMBLER__ */
  317. /* Pointer mangling is not yet supported for ARM. */
  318. #define PTR_MANGLE(var) (void) (var)
  319. #define PTR_DEMANGLE(var) (void) (var)
  320. #endif /* linux/arm/sysdep.h */