sysdep.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #undef END
  60. #define END(name) \
  61. ASM_SIZE_DIRECTIVE(name)
  62. #ifdef NO_UNDERSCORES
  63. /* Since C identifiers are not normally prefixed with an underscore
  64. on this system, the asm identifier `syscall_error' intrudes on the
  65. C name space. Make sure we use an innocuous name. */
  66. #define syscall_error __syscall_error
  67. #define mcount _mcount
  68. #endif
  69. /* Linux uses a negative return value to indicate syscall errors,
  70. unlike most Unices, which use the condition codes' carry flag.
  71. Since version 2.1 the return value of a system call might be
  72. negative even if the call succeeded. E.g., the `lseek' system call
  73. might return a large offset. Therefore we must not anymore test
  74. for < 0, but test for a real error by making sure the value in R0
  75. is a real error number. Linus said he will make sure the no syscall
  76. returns a value in -1 .. -4095 as a valid result so we can safely
  77. test with -4095. */
  78. #undef PSEUDO
  79. #define PSEUDO(name, syscall_name, args) \
  80. .text; \
  81. ENTRY (name); \
  82. DO_CALL (syscall_name, args); \
  83. cmn r0, $4096;
  84. #define PSEUDO_RET \
  85. RETINSTR(cc, lr); \
  86. b PLTJMP(SYSCALL_ERROR)
  87. #undef ret
  88. #define ret PSEUDO_RET
  89. #undef PSEUDO_END
  90. #define PSEUDO_END(name) \
  91. SYSCALL_ERROR_HANDLER \
  92. END (name)
  93. #undef PSEUDO_NOERRNO
  94. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  95. .text; \
  96. ENTRY (name); \
  97. DO_CALL (syscall_name, args);
  98. #define PSEUDO_RET_NOERRNO \
  99. DO_RET (lr);
  100. #undef ret_NOERRNO
  101. #define ret_NOERRNO PSEUDO_RET_NOERRNO
  102. #undef PSEUDO_END_NOERRNO
  103. #define PSEUDO_END_NOERRNO(name) \
  104. END (name)
  105. /* The function has to return the error code. */
  106. #undef PSEUDO_ERRVAL
  107. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  108. .text; \
  109. ENTRY (name) \
  110. DO_CALL (syscall_name, args); \
  111. rsb r0, r0, #0
  112. #undef PSEUDO_END_ERRVAL
  113. #define PSEUDO_END_ERRVAL(name) \
  114. END (name)
  115. #undef ret_ERRVAL
  116. #define ret_ERRVAL PSEUDO_RET_NOERRNO
  117. #if defined NOT_IN_libc
  118. # define SYSCALL_ERROR __local_syscall_error
  119. # define SYSCALL_ERROR_HANDLER \
  120. __local_syscall_error: \
  121. str lr, [sp, #-4]!; \
  122. str r0, [sp, #-4]!; \
  123. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  124. ldr r1, [sp], #4; \
  125. rsb r1, r1, #0; \
  126. str r1, [r0]; \
  127. mvn r0, #0; \
  128. ldr pc, [sp], #4;
  129. #else
  130. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  131. # define SYSCALL_ERROR __syscall_error
  132. #endif
  133. /* Linux takes system call args in registers:
  134. syscall number in the SWI instruction
  135. arg 1 r0
  136. arg 2 r1
  137. arg 3 r2
  138. arg 4 r3
  139. arg 5 r4 (this is different from the APCS convention)
  140. arg 6 r5
  141. arg 7 r6
  142. The compiler is going to form a call by coming here, through PSEUDO, with
  143. arguments
  144. syscall number in the DO_CALL macro
  145. arg 1 r0
  146. arg 2 r1
  147. arg 3 r2
  148. arg 4 r3
  149. arg 5 [sp]
  150. arg 6 [sp+4]
  151. arg 7 [sp+8]
  152. We need to shuffle values between R4..R6 and the stack so that the
  153. caller's v1..v3 and stack frame are not corrupted, and the kernel
  154. sees the right arguments.
  155. */
  156. #if __ARM_ARCH > 6 || defined (__ARM_ARCH_6K__) || defined (__ARM_ARCH_6ZK__)
  157. # define ARCH_HAS_HARD_TP
  158. #endif
  159. # ifdef __thumb2__
  160. # define NEGOFF_ADJ_BASE(R, OFF) add R, R, $OFF
  161. # define NEGOFF_ADJ_BASE2(D, S, OFF) add D, S, $OFF
  162. # define NEGOFF_OFF1(R, OFF) [R]
  163. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $((OFFA) - (OFFB))]
  164. # else
  165. # define NEGOFF_ADJ_BASE(R, OFF)
  166. # define NEGOFF_ADJ_BASE2(D, S, OFF) mov D, S
  167. # define NEGOFF_OFF1(R, OFF) [R, $OFF]
  168. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $OFFA]
  169. # endif
  170. # ifdef ARCH_HAS_HARD_TP
  171. /* If the cpu has cp15 available, use it. */
  172. # define GET_TLS(TMP) mrc p15, 0, r0, c13, c0, 3
  173. # else
  174. /* At this generic level we have no tricks to pull. Call the ABI routine. */
  175. # define GET_TLS(TMP) \
  176. push { r1, r2, r3, lr }; \
  177. cfi_remember_state; \
  178. cfi_adjust_cfa_offset (16); \
  179. cfi_rel_offset (r1, 0); \
  180. cfi_rel_offset (r2, 4); \
  181. cfi_rel_offset (r3, 8); \
  182. cfi_rel_offset (lr, 12); \
  183. bl __aeabi_read_tp; \
  184. pop { r1, r2, r3, lr }; \
  185. cfi_restore_state
  186. # endif /* ARCH_HAS_HARD_TP */
  187. #undef DO_CALL
  188. #if defined(__ARM_EABI__)
  189. #define DO_CALL(syscall_name, args) \
  190. DOARGS_##args \
  191. mov ip, r7; \
  192. ldr r7, =SYS_ify (syscall_name); \
  193. swi 0x0; \
  194. mov r7, ip; \
  195. UNDOARGS_##args
  196. #else
  197. #define DO_CALL(syscall_name, args) \
  198. DOARGS_##args \
  199. swi SYS_ify (syscall_name); \
  200. UNDOARGS_##args
  201. #endif
  202. #define DOARGS_0 /* nothing */
  203. #define DOARGS_1 /* nothing */
  204. #define DOARGS_2 /* nothing */
  205. #define DOARGS_3 /* nothing */
  206. #define DOARGS_4 /* nothing */
  207. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  208. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  209. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  210. #define UNDOARGS_0 /* nothing */
  211. #define UNDOARGS_1 /* nothing */
  212. #define UNDOARGS_2 /* nothing */
  213. #define UNDOARGS_3 /* nothing */
  214. #define UNDOARGS_4 /* nothing */
  215. #define UNDOARGS_5 ldr r4, [sp], $4;
  216. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  217. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  218. #endif /* __ASSEMBLER__ */
  219. #endif /* linux/arm/sysdep.h */