syscalls.h 4.1 KB

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