syscalls.h 4.0 KB

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