sysdep.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #define RETINSTR(cond, reg) \
  42. BXC(cond, reg)
  43. #define DO_RET(_reg) \
  44. BX(_reg)
  45. #else /* APCS-26 */
  46. #define LOADREGS(cond, base, reglist...) \
  47. ldm##cond base,reglist^
  48. #define RETINSTR(cond, reg) \
  49. mov##cond##s pc, reg
  50. #define DO_RET(_reg) \
  51. movs pc, _reg
  52. #endif
  53. /* Define an entry point visible from C. */
  54. #define ENTRY(name) \
  55. .globl C_SYMBOL_NAME(name); \
  56. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),function) \
  57. .align ALIGNARG(4); \
  58. name##: \
  59. CALL_MCOUNT
  60. #undef END
  61. #define END(name) \
  62. ASM_SIZE_DIRECTIVE(name)
  63. /* If compiled for profiling, call `mcount' at the start of each function. */
  64. #ifdef PROF
  65. #define CALL_MCOUNT \
  66. str lr,[sp, #-4]! ; \
  67. bl PLTJMP(mcount) ; \
  68. ldr lr, [sp], #4 ;
  69. #else
  70. #define CALL_MCOUNT /* Do nothing. */
  71. #endif
  72. #ifdef NO_UNDERSCORES
  73. /* Since C identifiers are not normally prefixed with an underscore
  74. on this system, the asm identifier `syscall_error' intrudes on the
  75. C name space. Make sure we use an innocuous name. */
  76. #define syscall_error __syscall_error
  77. #define mcount _mcount
  78. #endif
  79. /* Linux uses a negative return value to indicate syscall errors,
  80. unlike most Unices, which use the condition codes' carry flag.
  81. Since version 2.1 the return value of a system call might be
  82. negative even if the call succeeded. E.g., the `lseek' system call
  83. might return a large offset. Therefore we must not anymore test
  84. for < 0, but test for a real error by making sure the value in R0
  85. is a real error number. Linus said he will make sure the no syscall
  86. returns a value in -1 .. -4095 as a valid result so we can safely
  87. test with -4095. */
  88. #undef PSEUDO
  89. #define PSEUDO(name, syscall_name, args) \
  90. .text; \
  91. ENTRY (name); \
  92. DO_CALL (syscall_name, args); \
  93. cmn r0, $4096;
  94. #define PSEUDO_RET \
  95. RETINSTR(cc, lr); \
  96. b PLTJMP(SYSCALL_ERROR)
  97. #undef ret
  98. #define ret PSEUDO_RET
  99. #undef PSEUDO_END
  100. #define PSEUDO_END(name) \
  101. SYSCALL_ERROR_HANDLER \
  102. END (name)
  103. #undef PSEUDO_NOERRNO
  104. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  105. .text; \
  106. ENTRY (name); \
  107. DO_CALL (syscall_name, args);
  108. #define PSEUDO_RET_NOERRNO \
  109. DO_RET (lr);
  110. #undef ret_NOERRNO
  111. #define ret_NOERRNO PSEUDO_RET_NOERRNO
  112. #undef PSEUDO_END_NOERRNO
  113. #define PSEUDO_END_NOERRNO(name) \
  114. END (name)
  115. /* The function has to return the error code. */
  116. #undef PSEUDO_ERRVAL
  117. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  118. .text; \
  119. ENTRY (name) \
  120. DO_CALL (syscall_name, args); \
  121. rsb r0, r0, #0
  122. #undef PSEUDO_END_ERRVAL
  123. #define PSEUDO_END_ERRVAL(name) \
  124. END (name)
  125. #undef ret_ERRVAL
  126. #define ret_ERRVAL PSEUDO_RET_NOERRNO
  127. #if defined NOT_IN_libc
  128. # define SYSCALL_ERROR __local_syscall_error
  129. # ifdef RTLD_PRIVATE_ERRNO
  130. # define SYSCALL_ERROR_HANDLER \
  131. __local_syscall_error: \
  132. ldr r1, 1f; \
  133. rsb r0, r0, #0; \
  134. 0: str r0, [pc, r1]; \
  135. mvn r0, #0; \
  136. DO_RET(lr); \
  137. 1: .word C_SYMBOL_NAME(rtld_errno) - 0b - 8;
  138. # else
  139. # define SYSCALL_ERROR_HANDLER \
  140. __local_syscall_error: \
  141. str lr, [sp, #-4]!; \
  142. str r0, [sp, #-4]!; \
  143. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  144. ldr r1, [sp], #4; \
  145. rsb r1, r1, #0; \
  146. str r1, [r0]; \
  147. mvn r0, #0; \
  148. ldr pc, [sp], #4;
  149. # endif
  150. #else
  151. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  152. # define SYSCALL_ERROR __syscall_error
  153. #endif
  154. /* Linux takes system call args in registers:
  155. syscall number in the SWI instruction
  156. arg 1 r0
  157. arg 2 r1
  158. arg 3 r2
  159. arg 4 r3
  160. arg 5 r4 (this is different from the APCS convention)
  161. arg 6 r5
  162. arg 7 r6
  163. The compiler is going to form a call by coming here, through PSEUDO, with
  164. arguments
  165. syscall number in the DO_CALL macro
  166. arg 1 r0
  167. arg 2 r1
  168. arg 3 r2
  169. arg 4 r3
  170. arg 5 [sp]
  171. arg 6 [sp+4]
  172. arg 7 [sp+8]
  173. We need to shuffle values between R4..R6 and the stack so that the
  174. caller's v1..v3 and stack frame are not corrupted, and the kernel
  175. sees the right arguments.
  176. */
  177. #if __ARM_ARCH > 6 || defined (__ARM_ARCH_6K__) || defined (__ARM_ARCH_6ZK__)
  178. # define ARCH_HAS_HARD_TP
  179. #endif
  180. # ifdef __thumb2__
  181. # define NEGOFF_ADJ_BASE(R, OFF) add R, R, $OFF
  182. # define NEGOFF_ADJ_BASE2(D, S, OFF) add D, S, $OFF
  183. # define NEGOFF_OFF1(R, OFF) [R]
  184. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $((OFFA) - (OFFB))]
  185. # else
  186. # define NEGOFF_ADJ_BASE(R, OFF)
  187. # define NEGOFF_ADJ_BASE2(D, S, OFF) mov D, S
  188. # define NEGOFF_OFF1(R, OFF) [R, $OFF]
  189. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $OFFA]
  190. # endif
  191. # ifdef ARCH_HAS_HARD_TP
  192. /* If the cpu has cp15 available, use it. */
  193. # define GET_TLS(TMP) mrc p15, 0, r0, c13, c0, 3
  194. # else
  195. /* At this generic level we have no tricks to pull. Call the ABI routine. */
  196. # define GET_TLS(TMP) \
  197. push { r1, r2, r3, lr }; \
  198. cfi_remember_state; \
  199. cfi_adjust_cfa_offset (16); \
  200. cfi_rel_offset (r1, 0); \
  201. cfi_rel_offset (r2, 4); \
  202. cfi_rel_offset (r3, 8); \
  203. cfi_rel_offset (lr, 12); \
  204. bl __aeabi_read_tp; \
  205. pop { r1, r2, r3, lr }; \
  206. cfi_restore_state
  207. # endif /* ARCH_HAS_HARD_TP */
  208. #undef DO_CALL
  209. #if defined(__ARM_EABI__)
  210. #define DO_CALL(syscall_name, args) \
  211. DOARGS_##args \
  212. mov ip, r7; \
  213. ldr r7, =SYS_ify (syscall_name); \
  214. swi 0x0; \
  215. mov r7, ip; \
  216. UNDOARGS_##args
  217. #else
  218. #define DO_CALL(syscall_name, args) \
  219. DOARGS_##args \
  220. swi SYS_ify (syscall_name); \
  221. UNDOARGS_##args
  222. #endif
  223. #define DOARGS_0 /* nothing */
  224. #define DOARGS_1 /* nothing */
  225. #define DOARGS_2 /* nothing */
  226. #define DOARGS_3 /* nothing */
  227. #define DOARGS_4 /* nothing */
  228. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  229. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  230. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  231. #define UNDOARGS_0 /* nothing */
  232. #define UNDOARGS_1 /* nothing */
  233. #define UNDOARGS_2 /* nothing */
  234. #define UNDOARGS_3 /* nothing */
  235. #define UNDOARGS_4 /* nothing */
  236. #define UNDOARGS_5 ldr r4, [sp], $4;
  237. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  238. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  239. #else /* not __ASSEMBLER__ */
  240. /* Define a macro which expands into the inline wrapper code for a system
  241. call. */
  242. #undef INLINE_SYSCALL
  243. #define INLINE_SYSCALL(name, nr, args...) \
  244. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  245. if (unlikely (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ))) \
  246. { \
  247. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  248. _inline_sys_result = (unsigned int) -1; \
  249. } \
  250. (int) _inline_sys_result; })
  251. #undef INTERNAL_SYSCALL_DECL
  252. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  253. #undef INTERNAL_SYSCALL_RAW
  254. #if defined(__thumb__)
  255. /* Hide the use of r7 from the compiler, this would be a lot
  256. * easier but for the fact that the syscalls can exceed 255.
  257. * For the moment the LOAD_ARG_7 is sacrificed.
  258. * We can't use push/pop inside the asm because that breaks
  259. * unwinding (ie. thread cancellation).
  260. */
  261. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  262. ({ unsigned int _internal_sys_result; \
  263. { \
  264. int _sys_buf[2]; \
  265. register int __a1 __asm__ ("a1"); \
  266. register int *_v3 __asm__ ("v3") = _sys_buf; \
  267. LOAD_ARGS_##nr (args) \
  268. *_v3 = (int) (name); \
  269. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  270. "\tldr r7, [v3]\n" \
  271. "\tswi 0 @ syscall " #name "\n" \
  272. "\tldr r7, [v3, #4]" \
  273. : "=r" (__a1) \
  274. : "r" (_v3) ASM_ARGS_##nr \
  275. : "memory"); \
  276. _internal_sys_result = __a1; \
  277. } \
  278. (int) _internal_sys_result; })
  279. #elif defined(__ARM_EABI__)
  280. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  281. ({unsigned int _internal_sys_result; \
  282. { \
  283. register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  284. LOAD_ARGS_##nr (args) \
  285. _nr = name; \
  286. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  287. : "=r" (__a1) \
  288. : "r" (_nr) ASM_ARGS_##nr \
  289. : "memory"); \
  290. _internal_sys_result = __a1; \
  291. } \
  292. (int) _internal_sys_result; })
  293. #else /* !defined(__ARM_EABI__) */
  294. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  295. ({ unsigned int _internal_sys_result; \
  296. { \
  297. register int __a1 __asm__ ("a1"); \
  298. LOAD_ARGS_##nr (args) \
  299. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  300. : "=r" (__a1) \
  301. : "i" (name) ASM_ARGS_##nr \
  302. : "memory"); \
  303. _internal_sys_result = __a1; \
  304. } \
  305. (int) _internal_sys_result; })
  306. #endif
  307. #undef INTERNAL_SYSCALL
  308. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  309. INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
  310. #undef INTERNAL_SYSCALL_ARM
  311. #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
  312. INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
  313. #undef INTERNAL_SYSCALL_ERROR_P
  314. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  315. ((unsigned int) (val) >= 0xfffff001u)
  316. #undef INTERNAL_SYSCALL_ERRNO
  317. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  318. #if defined(__ARM_EABI__)
  319. #undef INTERNAL_SYSCALL_NCS
  320. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  321. INTERNAL_SYSCALL_RAW(number, err, nr, args)
  322. #else
  323. /* We can't implement non-constant syscalls directly since the syscall
  324. number is normally encoded in the instruction. So use SYS_syscall. */
  325. #undef INTERNAL_SYSCALL_NCS
  326. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  327. INTERNAL_SYSCALL_NCS_##nr (number, err, args)
  328. #define INTERNAL_SYSCALL_NCS_0(number, err, args...) \
  329. INTERNAL_SYSCALL (syscall, err, 1, number, args)
  330. #define INTERNAL_SYSCALL_NCS_1(number, err, args...) \
  331. INTERNAL_SYSCALL (syscall, err, 2, number, args)
  332. #define INTERNAL_SYSCALL_NCS_2(number, err, args...) \
  333. INTERNAL_SYSCALL (syscall, err, 3, number, args)
  334. #define INTERNAL_SYSCALL_NCS_3(number, err, args...) \
  335. INTERNAL_SYSCALL (syscall, err, 4, number, args)
  336. #define INTERNAL_SYSCALL_NCS_4(number, err, args...) \
  337. INTERNAL_SYSCALL (syscall, err, 5, number, args)
  338. #define INTERNAL_SYSCALL_NCS_5(number, err, args...) \
  339. INTERNAL_SYSCALL (syscall, err, 6, number, args)
  340. #endif
  341. #endif /* __ASSEMBLER__ */
  342. /* Pointer mangling is not yet supported for ARM. */
  343. #define PTR_MANGLE(var) (void) (var)
  344. #define PTR_DEMANGLE(var) (void) (var)
  345. #endif /* linux/arm/sysdep.h */