sysdep.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #if defined NOT_IN_libc
  116. # define SYSCALL_ERROR __local_syscall_error
  117. # define SYSCALL_ERROR_HANDLER \
  118. __local_syscall_error: \
  119. str lr, [sp, #-4]!; \
  120. str r0, [sp, #-4]!; \
  121. bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
  122. ldr r1, [sp], #4; \
  123. rsb r1, r1, #0; \
  124. str r1, [r0]; \
  125. mvn r0, #0; \
  126. ldr pc, [sp], #4;
  127. #else
  128. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  129. # define SYSCALL_ERROR __syscall_error
  130. #endif
  131. /* Linux takes system call args in registers:
  132. syscall number in the SWI instruction
  133. arg 1 r0
  134. arg 2 r1
  135. arg 3 r2
  136. arg 4 r3
  137. arg 5 r4 (this is different from the APCS convention)
  138. arg 6 r5
  139. arg 7 r6
  140. The compiler is going to form a call by coming here, through PSEUDO, with
  141. arguments
  142. syscall number in the DO_CALL macro
  143. arg 1 r0
  144. arg 2 r1
  145. arg 3 r2
  146. arg 4 r3
  147. arg 5 [sp]
  148. arg 6 [sp+4]
  149. arg 7 [sp+8]
  150. We need to shuffle values between R4..R6 and the stack so that the
  151. caller's v1..v3 and stack frame are not corrupted, and the kernel
  152. sees the right arguments.
  153. */
  154. #if __ARM_ARCH > 6 || defined (__ARM_ARCH_6K__) || defined (__ARM_ARCH_6ZK__)
  155. # define ARCH_HAS_HARD_TP
  156. #endif
  157. # ifdef __thumb2__
  158. # define NEGOFF_ADJ_BASE(R, OFF) add R, R, $OFF
  159. # define NEGOFF_ADJ_BASE2(D, S, OFF) add D, S, $OFF
  160. # define NEGOFF_OFF1(R, OFF) [R]
  161. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $((OFFA) - (OFFB))]
  162. # else
  163. # define NEGOFF_ADJ_BASE(R, OFF)
  164. # define NEGOFF_ADJ_BASE2(D, S, OFF) mov D, S
  165. # define NEGOFF_OFF1(R, OFF) [R, $OFF]
  166. # define NEGOFF_OFF2(R, OFFA, OFFB) [R, $OFFA]
  167. # endif
  168. # ifdef ARCH_HAS_HARD_TP
  169. /* If the cpu has cp15 available, use it. */
  170. # define GET_TLS(TMP) mrc p15, 0, r0, c13, c0, 3
  171. # else
  172. /* At this generic level we have no tricks to pull. Call the ABI routine. */
  173. # define GET_TLS(TMP) \
  174. push { r1, r2, r3, lr }; \
  175. cfi_remember_state; \
  176. cfi_adjust_cfa_offset (16); \
  177. cfi_rel_offset (r1, 0); \
  178. cfi_rel_offset (r2, 4); \
  179. cfi_rel_offset (r3, 8); \
  180. cfi_rel_offset (lr, 12); \
  181. bl __aeabi_read_tp; \
  182. pop { r1, r2, r3, lr }; \
  183. cfi_restore_state
  184. # endif /* ARCH_HAS_HARD_TP */
  185. #undef DO_CALL
  186. #if defined(__ARM_EABI__)
  187. #define DO_CALL(syscall_name, args) \
  188. DOARGS_##args \
  189. mov ip, r7; \
  190. ldr r7, =SYS_ify (syscall_name); \
  191. swi 0x0; \
  192. mov r7, ip; \
  193. UNDOARGS_##args
  194. #else
  195. #define DO_CALL(syscall_name, args) \
  196. DOARGS_##args \
  197. swi SYS_ify (syscall_name); \
  198. UNDOARGS_##args
  199. #endif
  200. #define DOARGS_0 /* nothing */
  201. #define DOARGS_1 /* nothing */
  202. #define DOARGS_2 /* nothing */
  203. #define DOARGS_3 /* nothing */
  204. #define DOARGS_4 /* nothing */
  205. #define DOARGS_5 str r4, [sp, $-4]!; ldr r4, [sp, $4];
  206. #define DOARGS_6 mov ip, sp; stmfd sp!, {r4, r5}; ldmia ip, {r4, r5};
  207. #define DOARGS_7 mov ip, sp; stmfd sp!, {r4, r5, r6}; ldmia ip, {r4, r5, r6};
  208. #define UNDOARGS_0 /* nothing */
  209. #define UNDOARGS_1 /* nothing */
  210. #define UNDOARGS_2 /* nothing */
  211. #define UNDOARGS_3 /* nothing */
  212. #define UNDOARGS_4 /* nothing */
  213. #define UNDOARGS_5 ldr r4, [sp], $4;
  214. #define UNDOARGS_6 ldmfd sp!, {r4, r5};
  215. #define UNDOARGS_7 ldmfd sp!, {r4, r5, r6};
  216. #endif /* __ASSEMBLER__ */
  217. #endif /* linux/arm/sysdep.h */