sysdep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Copyright (C) 1992, 93, 1995-2000, 2002, 2003, 2005, 2006
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>, August 1995.
  5. ARM changes by Philip Blundell, <pjb27@cam.ac.uk>, May 1997.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. 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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #ifndef _LINUX_ARM_SYSDEP_H
  19. #define _LINUX_ARM_SYSDEP_H 1
  20. #include <sys/syscall.h>
  21. /* There is some commonality. */
  22. #include <sysdeps/arm/sysdep.h>
  23. /* For Linux we can use the system call table in the header file
  24. /usr/include/asm/unistd.h
  25. of the kernel. But these symbols do not follow the SYS_* syntax
  26. so we have to redefine the `SYS_ify' macro here. */
  27. #undef SYS_ify
  28. #define SWI_BASE (0x900000)
  29. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  30. #ifdef __ASSEMBLER__
  31. /* Linux uses a negative return value to indicate syscall errors,
  32. unlike most Unices, which use the condition codes' carry flag.
  33. Since version 2.1 the return value of a system call might be
  34. negative even if the call succeeded. E.g., the `lseek' system call
  35. might return a large offset. Therefore we must not anymore test
  36. for < 0, but test for a real error by making sure the value in R0
  37. is a real error number. Linus said he will make sure the no syscall
  38. returns a value in -1 .. -4095 as a valid result so we can safely
  39. test with -4095. */
  40. #undef PSEUDO
  41. #define PSEUDO(name, syscall_name, args) \
  42. .text; \
  43. ENTRY (name); \
  44. DO_CALL (syscall_name, args); \
  45. cmn r0, $4096;
  46. #define PSEUDO_RET \
  47. RETINSTR(cc, lr); \
  48. b PLTJMP(SYSCALL_ERROR)
  49. #undef ret
  50. #define ret PSEUDO_RET
  51. #undef PSEUDO_END
  52. #define PSEUDO_END(name) \
  53. SYSCALL_ERROR_HANDLER \
  54. END (name)
  55. #undef PSEUDO_NOERRNO
  56. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  57. .text; \
  58. ENTRY (name); \
  59. DO_CALL (syscall_name, args);
  60. #define PSEUDO_RET_NOERRNO \
  61. DO_RET (lr);
  62. #undef ret_NOERRNO
  63. #define ret_NOERRNO PSEUDO_RET_NOERRNO
  64. #undef PSEUDO_END_NOERRNO
  65. #define PSEUDO_END_NOERRNO(name) \
  66. END (name)
  67. /* The function has to return the error code. */
  68. #undef PSEUDO_ERRVAL
  69. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  70. .text; \
  71. ENTRY (name) \
  72. DO_CALL (syscall_name, args); \
  73. rsb r0, r0, #0
  74. #undef PSEUDO_END_ERRVAL
  75. #define PSEUDO_END_ERRVAL(name) \
  76. END (name)
  77. #define ret_ERRVAL PSEUDO_RET_NOERRNO
  78. #if NOT_IN_libc
  79. # define SYSCALL_ERROR __local_syscall_error
  80. # ifdef RTLD_PRIVATE_ERRNO
  81. # define SYSCALL_ERROR_HANDLER \
  82. __local_syscall_error: \
  83. ldr r1, 1f; \
  84. rsb r0, r0, #0; \
  85. 0: str r0, [pc, r1]; \
  86. mvn r0, #0; \
  87. DO_RET(lr); \
  88. 1: .word C_SYMBOL_NAME(rtld_errno) - 0b - 8;
  89. # else
  90. # define SYSCALL_ERROR_HANDLER \
  91. __local_syscall_error: \
  92. str lr, [sp, #-4]!; \
  93. str r0, [sp, #-4]!; \
  94. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  95. ldr r1, [sp], #4; \
  96. rsb r1, r1, #0; \
  97. str r1, [r0]; \
  98. mvn r0, #0; \
  99. ldr pc, [sp], #4;
  100. # endif
  101. #else
  102. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  103. # define SYSCALL_ERROR __syscall_error
  104. #endif
  105. /* Linux takes system call args in registers:
  106. syscall number in the SWI instruction
  107. arg 1 r0
  108. arg 2 r1
  109. arg 3 r2
  110. arg 4 r3
  111. arg 5 r4 (this is different from the APCS convention)
  112. arg 6 r5
  113. arg 7 r6
  114. The compiler is going to form a call by coming here, through PSEUDO, with
  115. arguments
  116. syscall number in the DO_CALL macro
  117. arg 1 r0
  118. arg 2 r1
  119. arg 3 r2
  120. arg 4 r3
  121. arg 5 [sp]
  122. arg 6 [sp+4]
  123. arg 7 [sp+8]
  124. We need to shuffle values between R4..R6 and the stack so that the
  125. caller's v1..v3 and stack frame are not corrupted, and the kernel
  126. sees the right arguments.
  127. */
  128. #undef DO_CALL
  129. #if defined(__ARM_EABI__)
  130. #define DO_CALL(syscall_name, args) \
  131. DOARGS_##args \
  132. mov ip, r7; \
  133. ldr r7, =SYS_ify (syscall_name); \
  134. swi 0x0; \
  135. mov r7, ip; \
  136. UNDOARGS_##args
  137. #else
  138. #define DO_CALL(syscall_name, args) \
  139. DOARGS_##args \
  140. swi SYS_ify (syscall_name); \
  141. UNDOARGS_##args
  142. #endif
  143. #define DOARGS_0 /* nothing */
  144. #define DOARGS_1 /* nothing */
  145. #define DOARGS_2 /* nothing */
  146. #define DOARGS_3 /* nothing */
  147. #define DOARGS_4 /* nothing */
  148. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  149. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  150. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  151. #define UNDOARGS_0 /* nothing */
  152. #define UNDOARGS_1 /* nothing */
  153. #define UNDOARGS_2 /* nothing */
  154. #define UNDOARGS_3 /* nothing */
  155. #define UNDOARGS_4 /* nothing */
  156. #define UNDOARGS_5 ldr r4, [sp], $4;
  157. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  158. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  159. #else /* not __ASSEMBLER__ */
  160. /* Define a macro which expands into the inline wrapper code for a system
  161. call. */
  162. #undef INLINE_SYSCALL
  163. #define INLINE_SYSCALL(name, nr, args...) \
  164. ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  165. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0)) \
  166. { \
  167. __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
  168. _sys_result = (unsigned int) -1; \
  169. } \
  170. (int) _sys_result; })
  171. #undef INTERNAL_SYSCALL_DECL
  172. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  173. #undef INTERNAL_SYSCALL_RAW
  174. #if defined(__thumb__)
  175. /* Hide the use of r7 from the compiler, this would be a lot
  176. * easier but for the fact that the syscalls can exceed 255.
  177. * For the moment the LOAD_ARG_7 is sacrificed.
  178. * We can't use push/pop inside the asm because that breaks
  179. * unwinding (ie. thread cancellation).
  180. */
  181. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  182. ({ unsigned int _sys_result; \
  183. { \
  184. int _sys_buf[2]; \
  185. register int _a1 __asm__ ("a1"); \
  186. register int *_v3 __asm__ ("v3") = _sys_buf; \
  187. LOAD_ARGS_##nr (args) \
  188. *_v3 = (int) (name); \
  189. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  190. "\tldr r7, [v3]\n" \
  191. "\tswi 0 @ syscall " #name "\n" \
  192. "\tldr r7, [v3, #4]" \
  193. : "=r" (_a1) \
  194. : "r" (_v3) ASM_ARGS_##nr \
  195. : "memory"); \
  196. _sys_result = _a1; \
  197. } \
  198. (int) _sys_result; })
  199. #elif defined(__ARM_EABI__)
  200. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  201. ({unsigned int _sys_result; \
  202. { \
  203. register int _a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  204. LOAD_ARGS_##nr (args) \
  205. _nr = name; \
  206. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  207. : "=r" (_a1) \
  208. : "r" (_nr) ASM_ARGS_##nr \
  209. : "memory"); \
  210. _sys_result = _a1; \
  211. } \
  212. (int) _sys_result; })
  213. #else /* !defined(__ARM_EABI__) */
  214. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  215. ({ unsigned int _sys_result; \
  216. { \
  217. register int _a1 __asm__ ("a1"); \
  218. LOAD_ARGS_##nr (args) \
  219. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  220. : "=r" (_a1) \
  221. : "i" (name) ASM_ARGS_##nr \
  222. : "memory"); \
  223. _sys_result = _a1; \
  224. } \
  225. (int) _sys_result; })
  226. #endif
  227. #undef INTERNAL_SYSCALL
  228. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  229. INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
  230. #undef INTERNAL_SYSCALL_ARM
  231. #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
  232. INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
  233. #undef INTERNAL_SYSCALL_ERROR_P
  234. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  235. ((unsigned int) (val) >= 0xfffff001u)
  236. #undef INTERNAL_SYSCALL_ERRNO
  237. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  238. #define LOAD_ARGS_0()
  239. #define ASM_ARGS_0
  240. #define LOAD_ARGS_1(a1) \
  241. _a1 = (int) (a1); \
  242. LOAD_ARGS_0 ()
  243. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  244. #define LOAD_ARGS_2(a1, a2) \
  245. register int _a2 __asm__ ("a2") = (int) (a2); \
  246. LOAD_ARGS_1 (a1)
  247. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  248. #define LOAD_ARGS_3(a1, a2, a3) \
  249. register int _a3 __asm__ ("a3") = (int) (a3); \
  250. LOAD_ARGS_2 (a1, a2)
  251. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  252. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  253. register int _a4 __asm__ ("a4") = (int) (a4); \
  254. LOAD_ARGS_3 (a1, a2, a3)
  255. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  256. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  257. register int _v1 __asm__ ("v1") = (int) (a5); \
  258. LOAD_ARGS_4 (a1, a2, a3, a4)
  259. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  260. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  261. register int _v2 __asm__ ("v2") = (int) (a6); \
  262. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  263. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  264. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  265. register int _v3 __asm__ ("v3") = (int) (a7); \
  266. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  267. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  268. #if defined(__ARM_EABI__)
  269. #undef INTERNAL_SYSCALL_NCS
  270. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  271. INTERNAL_SYSCALL_RAW(number, err, nr, args)
  272. #else
  273. /* We can't implement non-constant syscalls directly since the syscall
  274. number is normally encoded in the instruction. So use SYS_syscall. */
  275. #undef INTERNAL_SYSCALL_NCS
  276. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  277. INTERNAL_SYSCALL_NCS_##nr (number, err, args)
  278. #define INTERNAL_SYSCALL_NCS_0(number, err, args...) \
  279. INTERNAL_SYSCALL (syscall, err, 1, number, args)
  280. #define INTERNAL_SYSCALL_NCS_1(number, err, args...) \
  281. INTERNAL_SYSCALL (syscall, err, 2, number, args)
  282. #define INTERNAL_SYSCALL_NCS_2(number, err, args...) \
  283. INTERNAL_SYSCALL (syscall, err, 3, number, args)
  284. #define INTERNAL_SYSCALL_NCS_3(number, err, args...) \
  285. INTERNAL_SYSCALL (syscall, err, 4, number, args)
  286. #define INTERNAL_SYSCALL_NCS_4(number, err, args...) \
  287. INTERNAL_SYSCALL (syscall, err, 5, number, args)
  288. #define INTERNAL_SYSCALL_NCS_5(number, err, args...) \
  289. INTERNAL_SYSCALL (syscall, err, 6, number, args)
  290. #endif
  291. #endif /* __ASSEMBLER__ */
  292. #endif /* linux/arm/sysdep.h */