syscalls.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL_NCS (name, , nr, args); \
  29. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ), 0)) \
  30. { \
  31. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  32. _inline_sys_result = (unsigned int) -1; \
  33. } \
  34. (int) _inline_sys_result; })
  35. #if !defined(__thumb__)
  36. #if defined(__ARM_EABI__)
  37. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  38. ({unsigned int __internal_sys_result; \
  39. { \
  40. register int __a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  41. LOAD_ARGS_##nr (args) \
  42. _nr = (name); \
  43. __asm__ __volatile__ ("swi 0x0 @ syscall " #name \
  44. : "=r" (__a1) \
  45. : "r" (_nr) ASM_ARGS_##nr \
  46. : "memory"); \
  47. __internal_sys_result = __a1; \
  48. } \
  49. (int) __internal_sys_result; })
  50. #else /* defined(__ARM_EABI__) */
  51. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  52. ({ unsigned int __internal_sys_result; \
  53. { \
  54. register int __a1 __asm__ ("a1"); \
  55. LOAD_ARGS_##nr (args) \
  56. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  57. : "=r" (__a1) \
  58. : "i" (name) ASM_ARGS_##nr \
  59. : "memory"); \
  60. __internal_sys_result = __a1; \
  61. } \
  62. (int) __internal_sys_result; })
  63. #endif
  64. #else /* !defined(__thumb__) */
  65. /* We can't use push/pop inside the asm because that breaks
  66. unwinding (ie. thread cancellation).
  67. */
  68. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  69. ({ unsigned int __internal_sys_result; \
  70. { \
  71. int _sys_buf[2]; \
  72. register int __a1 __asm__ ("a1"); \
  73. register int *_v3 __asm__ ("v3") = _sys_buf; \
  74. *_v3 = (int) (name); \
  75. LOAD_ARGS_##nr (args) \
  76. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  77. "\tldr r7, [v3]\n" \
  78. "\tswi 0 @ syscall " #name "\n" \
  79. "\tldr r7, [v3, #4]" \
  80. : "=r" (__a1) \
  81. : "r" (_v3) ASM_ARGS_##nr \
  82. : "memory"); \
  83. __internal_sys_result = __a1; \
  84. } \
  85. (int) __internal_sys_result; })
  86. #endif /*!defined(__thumb__)*/
  87. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  88. ((unsigned int) (val) >= 0xfffff001u)
  89. #define LOAD_ARGS_0()
  90. #define ASM_ARGS_0
  91. #define LOAD_ARGS_1(a1) \
  92. int __a1tmp = (int) (a1); \
  93. LOAD_ARGS_0 () \
  94. __a1 = __a1tmp;
  95. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__a1)
  96. #define LOAD_ARGS_2(a1, a2) \
  97. int __a2tmp = (int) (a2); \
  98. LOAD_ARGS_1 (a1) \
  99. register int __a2 __asm__ ("a2") = __a2tmp;
  100. #define ASM_ARGS_2 ASM_ARGS_1, "r" (__a2)
  101. #define LOAD_ARGS_3(a1, a2, a3) \
  102. int __a3tmp = (int) (a3); \
  103. LOAD_ARGS_2 (a1, a2) \
  104. register int __a3 __asm__ ("a3") = __a3tmp;
  105. #define ASM_ARGS_3 ASM_ARGS_2, "r" (__a3)
  106. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  107. int __a4tmp = (int) (a4); \
  108. LOAD_ARGS_3 (a1, a2, a3) \
  109. register int __a4 __asm__ ("a4") = __a4tmp;
  110. #define ASM_ARGS_4 ASM_ARGS_3, "r" (__a4)
  111. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  112. int _v1tmp = (int) (a5); \
  113. LOAD_ARGS_4 (a1, a2, a3, a4) \
  114. register int _v1 __asm__ ("v1") = _v1tmp;
  115. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  116. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  117. int _v2tmp = (int) (a6); \
  118. LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
  119. register int _v2 __asm__ ("v2") = _v2tmp;
  120. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  121. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  122. int _v3tmp = (int) (a7); \
  123. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6) \
  124. register int _v3 __asm__ ("v3") = _v3tmp;
  125. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  126. #endif /* __ASSEMBLER__ */
  127. #endif /* _BITS_SYSCALLS_H */