sysdep.h 11 KB

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