syscalls.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /* This includes the `__NR_<name>' syscall numbers taken from the Linux kernel
  7. * header files. It also defines the traditional `SYS_<name>' macros for older
  8. * programs. */
  9. #include <bits/sysnum.h>
  10. #ifndef __set_errno
  11. # define __set_errno(val) (*__errno_location ()) = (val)
  12. #endif
  13. /*
  14. Some of the sneaky macros in the code were taken from
  15. glibc-2.2.5/sysdeps/unix/sysv/linux/m68k/sysdep.h
  16. */
  17. #ifndef __ASSEMBLER__
  18. #undef _syscall0
  19. #define _syscall0(type,name) \
  20. type name(void) \
  21. { \
  22. return (type) (INLINE_SYSCALL(name, 0)); \
  23. }
  24. #undef _syscall1
  25. #define _syscall1(type,name,type1,arg1) \
  26. type name(type1 arg1) \
  27. { \
  28. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  29. }
  30. #undef _syscall2
  31. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  32. type name(type1 arg1,type2 arg2) \
  33. { \
  34. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  35. }
  36. #undef _syscall3
  37. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  38. type name(type1 arg1,type2 arg2,type3 arg3) \
  39. { \
  40. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  41. }
  42. #undef _syscall4
  43. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  44. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  45. { \
  46. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  47. }
  48. #undef _syscall5
  49. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  50. type5,arg5) \
  51. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  52. { \
  53. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  54. }
  55. #undef _syscall6
  56. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  57. type5,arg5,type6,arg6) \
  58. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
  59. { \
  60. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  61. }
  62. /* Linux takes system call arguments in registers:
  63. syscall number %d0 call-clobbered
  64. arg 1 %d1 call-clobbered
  65. arg 2 %d2 call-saved
  66. arg 3 %d3 call-saved
  67. arg 4 %d4 call-saved
  68. arg 5 %d5 call-saved
  69. The stack layout upon entering the function is:
  70. 20(%sp) Arg# 5
  71. 16(%sp) Arg# 4
  72. 12(%sp) Arg# 3
  73. 8(%sp) Arg# 2
  74. 4(%sp) Arg# 1
  75. (%sp) Return address
  76. (Of course a function with say 3 arguments does not have entries for
  77. arguments 4 and 5.)
  78. Separate move's are faster than movem, but need more space. Since
  79. speed is more important, we don't use movem. Since %a0 and %a1 are
  80. scratch registers, we can use them for saving as well. */
  81. #undef INLINE_SYSCALL
  82. #define INLINE_SYSCALL(name, nr, args...) \
  83. ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  84. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
  85. { \
  86. __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
  87. _sys_result = (unsigned int) -1; \
  88. } \
  89. (int) _sys_result; })
  90. #undef INTERNAL_SYSCALL
  91. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  92. ({ unsigned int _sys_result; \
  93. { \
  94. /* Load argument values in temporary variables
  95. to perform side effects like function calls
  96. before the call used registers are set. */ \
  97. LOAD_ARGS_##nr (args) \
  98. LOAD_REGS_##nr \
  99. register int _d0 asm ("%d0") = __NR_##name; \
  100. asm volatile ("trap #0" \
  101. : "=d" (_d0) \
  102. : "0" (_d0) ASM_ARGS_##nr \
  103. : "memory"); \
  104. _sys_result = _d0; \
  105. } \
  106. (int) _sys_result; })
  107. #undef INTERNAL_SYSCALL_ERROR_P
  108. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  109. ((unsigned int) (val) >= -4095U)
  110. #undef INTERNAL_SYSCALL_ERRNO
  111. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  112. #define LOAD_ARGS_0()
  113. #define LOAD_REGS_0
  114. #define ASM_ARGS_0
  115. #define LOAD_ARGS_1(a1) \
  116. LOAD_ARGS_0 () \
  117. int __arg1 = (int) (a1);
  118. #define LOAD_REGS_1 \
  119. register int _d1 asm ("d1") = __arg1; \
  120. LOAD_REGS_0
  121. #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
  122. #define LOAD_ARGS_2(a1, a2) \
  123. LOAD_ARGS_1 (a1) \
  124. int __arg2 = (int) (a2);
  125. #define LOAD_REGS_2 \
  126. register int _d2 asm ("d2") = __arg2; \
  127. LOAD_REGS_1
  128. #define ASM_ARGS_2 ASM_ARGS_1, "d" (_d2)
  129. #define LOAD_ARGS_3(a1, a2, a3) \
  130. LOAD_ARGS_2 (a1, a2) \
  131. int __arg3 = (int) (a3);
  132. #define LOAD_REGS_3 \
  133. register int _d3 asm ("d3") = __arg3; \
  134. LOAD_REGS_2
  135. #define ASM_ARGS_3 ASM_ARGS_2, "d" (_d3)
  136. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  137. LOAD_ARGS_3 (a1, a2, a3) \
  138. int __arg4 = (int) (a4);
  139. #define LOAD_REGS_4 \
  140. register int _d4 asm ("d4") = __arg4; \
  141. LOAD_REGS_3
  142. #define ASM_ARGS_4 ASM_ARGS_3, "d" (_d4)
  143. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  144. LOAD_ARGS_4 (a1, a2, a3, a4) \
  145. int __arg5 = (int) (a5);
  146. #define LOAD_REGS_5 \
  147. register int _d5 asm ("d5") = __arg5; \
  148. LOAD_REGS_4
  149. #define ASM_ARGS_5 ASM_ARGS_4, "d" (_d5)
  150. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  151. LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
  152. int __arg6 = (int) (a6);
  153. #define LOAD_REGS_6 \
  154. register int _a0 asm ("a0") = __arg6; \
  155. LOAD_REGS_5
  156. #define ASM_ARGS_6 ASM_ARGS_5, "a" (_a0)
  157. #endif /* __ASSEMBLER__ */
  158. #endif /* _BITS_SYSCALLS_H */