sysdep.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _LINUX_ARM_SYSDEP_H
  16. #define _LINUX_ARM_SYSDEP_H 1
  17. #include <common/sysdep.h>
  18. #include <bits/arm_bx.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. #undef ret_ERRVAL
  133. #define ret_ERRVAL PSEUDO_RET_NOERRNO
  134. #if defined NOT_IN_libc
  135. # define SYSCALL_ERROR __local_syscall_error
  136. # ifdef RTLD_PRIVATE_ERRNO
  137. # define SYSCALL_ERROR_HANDLER \
  138. __local_syscall_error: \
  139. ldr r1, 1f; \
  140. rsb r0, r0, #0; \
  141. 0: str r0, [pc, r1]; \
  142. mvn r0, #0; \
  143. DO_RET(lr); \
  144. 1: .word C_SYMBOL_NAME(rtld_errno) - 0b - 8;
  145. # else
  146. # define SYSCALL_ERROR_HANDLER \
  147. __local_syscall_error: \
  148. str lr, [sp, #-4]!; \
  149. str r0, [sp, #-4]!; \
  150. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  151. ldr r1, [sp], #4; \
  152. rsb r1, r1, #0; \
  153. str r1, [r0]; \
  154. mvn r0, #0; \
  155. ldr pc, [sp], #4;
  156. # endif
  157. #else
  158. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  159. # define SYSCALL_ERROR __syscall_error
  160. #endif
  161. /* Linux takes system call args in registers:
  162. syscall number in the SWI instruction
  163. arg 1 r0
  164. arg 2 r1
  165. arg 3 r2
  166. arg 4 r3
  167. arg 5 r4 (this is different from the APCS convention)
  168. arg 6 r5
  169. arg 7 r6
  170. The compiler is going to form a call by coming here, through PSEUDO, with
  171. arguments
  172. syscall number in the DO_CALL macro
  173. arg 1 r0
  174. arg 2 r1
  175. arg 3 r2
  176. arg 4 r3
  177. arg 5 [sp]
  178. arg 6 [sp+4]
  179. arg 7 [sp+8]
  180. We need to shuffle values between R4..R6 and the stack so that the
  181. caller's v1..v3 and stack frame are not corrupted, and the kernel
  182. sees the right arguments.
  183. */
  184. #if __ARM_ARCH > 6 || defined (__ARM_ARCH_6K__) || defined (__ARM_ARCH_6ZK__)
  185. # define ARCH_HAS_HARD_TP
  186. #endif
  187. # ifdef __thumb2__
  188. # define NEGOFF_ADJ_BASE(R, OFF) add R, R, $OFF
  189. # define NEGOFF_ADJ_BASE2(D, S, OFF) add D, S, $OFF
  190. # define NEGOFF_OFF1(R, OFF) [R]
  191. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $((OFFA) - (OFFB))]
  192. # else
  193. # define NEGOFF_ADJ_BASE(R, OFF)
  194. # define NEGOFF_ADJ_BASE2(D, S, OFF) mov D, S
  195. # define NEGOFF_OFF1(R, OFF) [R, $OFF]
  196. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $OFFA]
  197. # endif
  198. # ifdef ARCH_HAS_HARD_TP
  199. /* If the cpu has cp15 available, use it. */
  200. # define GET_TLS(TMP) mrc p15, 0, r0, c13, c0, 3
  201. # else
  202. /* At this generic level we have no tricks to pull. Call the ABI routine. */
  203. # define GET_TLS(TMP) \
  204. push { r1, r2, r3, lr }; \
  205. cfi_remember_state; \
  206. cfi_adjust_cfa_offset (16); \
  207. cfi_rel_offset (r1, 0); \
  208. cfi_rel_offset (r2, 4); \
  209. cfi_rel_offset (r3, 8); \
  210. cfi_rel_offset (lr, 12); \
  211. bl __aeabi_read_tp; \
  212. pop { r1, r2, r3, lr }; \
  213. cfi_restore_state
  214. # endif /* ARCH_HAS_HARD_TP */
  215. #undef DO_CALL
  216. #if defined(__ARM_EABI__)
  217. #define DO_CALL(syscall_name, args) \
  218. DOARGS_##args \
  219. mov ip, r7; \
  220. ldr r7, =SYS_ify (syscall_name); \
  221. swi 0x0; \
  222. mov r7, ip; \
  223. UNDOARGS_##args
  224. #else
  225. #define DO_CALL(syscall_name, args) \
  226. DOARGS_##args \
  227. swi SYS_ify (syscall_name); \
  228. UNDOARGS_##args
  229. #endif
  230. #define DOARGS_0 /* nothing */
  231. #define DOARGS_1 /* nothing */
  232. #define DOARGS_2 /* nothing */
  233. #define DOARGS_3 /* nothing */
  234. #define DOARGS_4 /* nothing */
  235. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  236. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  237. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  238. #define UNDOARGS_0 /* nothing */
  239. #define UNDOARGS_1 /* nothing */
  240. #define UNDOARGS_2 /* nothing */
  241. #define UNDOARGS_3 /* nothing */
  242. #define UNDOARGS_4 /* nothing */
  243. #define UNDOARGS_5 ldr r4, [sp], $4;
  244. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  245. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  246. #else /* not __ASSEMBLER__ */
  247. /* Define a macro which expands into the inline wrapper code for a system
  248. call. */
  249. #undef INLINE_SYSCALL
  250. #define INLINE_SYSCALL(name, nr, args...) \
  251. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  252. if (unlikely (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ))) \
  253. { \
  254. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  255. _inline_sys_result = (unsigned int) -1; \
  256. } \
  257. (int) _inline_sys_result; })
  258. #undef INTERNAL_SYSCALL_DECL
  259. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  260. #undef INTERNAL_SYSCALL_RAW
  261. #if defined(__thumb__)
  262. /* Hide the use of r7 from the compiler, this would be a lot
  263. * easier but for the fact that the syscalls can exceed 255.
  264. * For the moment the LOAD_ARG_7 is sacrificed.
  265. * We can't use push/pop inside the asm because that breaks
  266. * unwinding (ie. thread cancellation).
  267. */
  268. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  269. ({ unsigned int _internal_sys_result; \
  270. { \
  271. int _sys_buf[2]; \
  272. register int __a1 __asm__ ("a1"); \
  273. register int *_v3 __asm__ ("v3") = _sys_buf; \
  274. LOAD_ARGS_##nr (args) \
  275. *_v3 = (int) (name); \
  276. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  277. "\tldr r7, [v3]\n" \
  278. "\tswi 0 @ syscall " #name "\n" \
  279. "\tldr r7, [v3, #4]" \
  280. : "=r" (__a1) \
  281. : "r" (_v3) ASM_ARGS_##nr \
  282. : "memory"); \
  283. _internal_sys_result = __a1; \
  284. } \
  285. (int) _internal_sys_result; })
  286. #elif defined(__ARM_EABI__)
  287. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  288. ({unsigned int _internal_sys_result; \
  289. { \
  290. register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  291. LOAD_ARGS_##nr (args) \
  292. _nr = name; \
  293. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  294. : "=r" (__a1) \
  295. : "r" (_nr) ASM_ARGS_##nr \
  296. : "memory"); \
  297. _internal_sys_result = __a1; \
  298. } \
  299. (int) _internal_sys_result; })
  300. #else /* !defined(__ARM_EABI__) */
  301. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  302. ({ unsigned int _internal_sys_result; \
  303. { \
  304. register int __a1 __asm__ ("a1"); \
  305. LOAD_ARGS_##nr (args) \
  306. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  307. : "=r" (__a1) \
  308. : "i" (name) ASM_ARGS_##nr \
  309. : "memory"); \
  310. _internal_sys_result = __a1; \
  311. } \
  312. (int) _internal_sys_result; })
  313. #endif
  314. #undef INTERNAL_SYSCALL
  315. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  316. INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
  317. #undef INTERNAL_SYSCALL_ARM
  318. #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
  319. INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
  320. #undef INTERNAL_SYSCALL_ERROR_P
  321. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  322. ((unsigned int) (val) >= 0xfffff001u)
  323. #undef INTERNAL_SYSCALL_ERRNO
  324. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  325. #if defined(__ARM_EABI__)
  326. #undef INTERNAL_SYSCALL_NCS
  327. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  328. INTERNAL_SYSCALL_RAW(number, err, nr, args)
  329. #else
  330. /* We can't implement non-constant syscalls directly since the syscall
  331. number is normally encoded in the instruction. So use SYS_syscall. */
  332. #undef INTERNAL_SYSCALL_NCS
  333. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  334. INTERNAL_SYSCALL_NCS_##nr (number, err, args)
  335. #define INTERNAL_SYSCALL_NCS_0(number, err, args...) \
  336. INTERNAL_SYSCALL (syscall, err, 1, number, args)
  337. #define INTERNAL_SYSCALL_NCS_1(number, err, args...) \
  338. INTERNAL_SYSCALL (syscall, err, 2, number, args)
  339. #define INTERNAL_SYSCALL_NCS_2(number, err, args...) \
  340. INTERNAL_SYSCALL (syscall, err, 3, number, args)
  341. #define INTERNAL_SYSCALL_NCS_3(number, err, args...) \
  342. INTERNAL_SYSCALL (syscall, err, 4, number, args)
  343. #define INTERNAL_SYSCALL_NCS_4(number, err, args...) \
  344. INTERNAL_SYSCALL (syscall, err, 5, number, args)
  345. #define INTERNAL_SYSCALL_NCS_5(number, err, args...) \
  346. INTERNAL_SYSCALL (syscall, err, 6, number, args)
  347. #endif
  348. #endif /* __ASSEMBLER__ */
  349. /* Pointer mangling is not yet supported for ARM. */
  350. #define PTR_MANGLE(var) (void) (var)
  351. #define PTR_DEMANGLE(var) (void) (var)
  352. #endif /* linux/arm/sysdep.h */