syscalls.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef _BITS_SYSCALLS_H
  2. #define _BITS_SYSCALLS_H
  3. #ifndef _SYSCALL_H
  4. # error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
  5. #endif
  6. /*
  7. Some of the sneaky macros in the code were taken from
  8. glibc-2.3.2/sysdeps/unix/sysv/linux/arm/sysdep.h
  9. */
  10. #ifdef __ASSEMBLER__
  11. /* Call a given syscall, with arguments loaded. For EABI, we must
  12. save and restore r7 for the syscall number. Unlike the DO_CALL
  13. macro in glibc, this macro does not load syscall arguments. */
  14. #undef DO_CALL
  15. #if defined(__ARM_EABI__)
  16. #define DO_CALL(syscall_name) \
  17. mov ip, r7; \
  18. ldr r7, =SYS_ify (syscall_name); \
  19. swi 0x0; \
  20. mov r7, ip;
  21. #else
  22. #define DO_CALL(syscall_name) \
  23. swi SYS_ify (syscall_name);
  24. #endif
  25. #else
  26. #include <errno.h>
  27. #define INLINE_SYSCALL_NCS(name, nr, args...) \
  28. (__extension__ \
  29. ({ \
  30. unsigned int _inline_sys_result = INTERNAL_SYSCALL_NCS (name, , nr, args);\
  31. if (unlikely (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ))) \
  32. { \
  33. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  34. _inline_sys_result = (unsigned int) -1; \
  35. } \
  36. (int) _inline_sys_result; \
  37. }) \
  38. )
  39. #if !defined(__thumb__)
  40. #if defined(__ARM_EABI__)
  41. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  42. (__extension__ \
  43. ({unsigned int __internal_sys_result; \
  44. { \
  45. register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  46. LOAD_ARGS_##nr (args) \
  47. _nr = (name); \
  48. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  49. : "=r" (__a1) \
  50. : "r" (_nr) ASM_ARGS_##nr \
  51. : "memory"); \
  52. __internal_sys_result = __a1; \
  53. } \
  54. (int) __internal_sys_result; }) \
  55. )
  56. #else /* defined(__ARM_EABI__) */
  57. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  58. (__extension__ \
  59. ({ unsigned int __internal_sys_result; \
  60. { \
  61. register int __a1 __asm__ ("a1"); \
  62. LOAD_ARGS_##nr (args) \
  63. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  64. : "=r" (__a1) \
  65. : "i" (name) ASM_ARGS_##nr \
  66. : "memory"); \
  67. __internal_sys_result = __a1; \
  68. } \
  69. (int) __internal_sys_result; }) \
  70. )
  71. #endif
  72. #else /* !defined(__thumb__) */
  73. /* We can't use push/pop inside the asm because that breaks
  74. unwinding (ie. thread cancellation).
  75. */
  76. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  77. (__extension__ \
  78. ({ unsigned int __internal_sys_result; \
  79. { \
  80. int _sys_buf[2]; \
  81. register int __a1 __asm__ ("a1"); \
  82. register int *_v3 __asm__ ("v3") = _sys_buf; \
  83. *_v3 = (int) (name); \
  84. LOAD_ARGS_##nr (args) \
  85. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  86. "\tldr r7, [v3]\n" \
  87. "\tswi 0 @ syscall " #name "\n" \
  88. "\tldr r7, [v3, #4]" \
  89. : "=r" (__a1) \
  90. : "r" (_v3) ASM_ARGS_##nr \
  91. : "memory"); \
  92. __internal_sys_result = __a1; \
  93. } \
  94. (int) __internal_sys_result; }) \
  95. )
  96. #endif /*!defined(__thumb__)*/
  97. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  98. ((unsigned int) (val) >= 0xfffff001u)
  99. #define LOAD_ARGS_0()
  100. #define ASM_ARGS_0
  101. #define LOAD_ARGS_1(a1) \
  102. int __a1tmp = (int) (a1); \
  103. LOAD_ARGS_0 () \
  104. __a1 = __a1tmp;
  105. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__a1)
  106. #define LOAD_ARGS_2(a1, a2) \
  107. int __a2tmp = (int) (a2); \
  108. LOAD_ARGS_1 (a1) \
  109. register int __a2 __asm__ ("a2") = __a2tmp;
  110. #define ASM_ARGS_2 ASM_ARGS_1, "r" (__a2)
  111. #define LOAD_ARGS_3(a1, a2, a3) \
  112. int __a3tmp = (int) (a3); \
  113. LOAD_ARGS_2 (a1, a2) \
  114. register int __a3 __asm__ ("a3") = __a3tmp;
  115. #define ASM_ARGS_3 ASM_ARGS_2, "r" (__a3)
  116. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  117. int __a4tmp = (int) (a4); \
  118. LOAD_ARGS_3 (a1, a2, a3) \
  119. register int __a4 __asm__ ("a4") = __a4tmp;
  120. #define ASM_ARGS_4 ASM_ARGS_3, "r" (__a4)
  121. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  122. int _v1tmp = (int) (a5); \
  123. LOAD_ARGS_4 (a1, a2, a3, a4) \
  124. register int _v1 __asm__ ("v1") = _v1tmp;
  125. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  126. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  127. int _v2tmp = (int) (a6); \
  128. LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
  129. register int _v2 __asm__ ("v2") = _v2tmp;
  130. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  131. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  132. int _v3tmp = (int) (a7); \
  133. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6) \
  134. register int _v3 __asm__ ("v3") = _v3tmp;
  135. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  136. #endif /* __ASSEMBLER__ */
  137. #endif /* _BITS_SYSCALLS_H */