syscalls.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef __ASSEMBLER__
  14. #define SYS_ify(syscall_name) __NR_##syscall_name
  15. /* Assume all syscalls are done from PIC code just to be
  16. * safe. The worst case scenario is that you lose a register
  17. * and save/restore r19 across the syscall. */
  18. #define PIC
  19. #ifndef ASM_LINE_SEP
  20. # define ASM_LINE_SEP ;
  21. #endif
  22. /* Definition taken from glibc 2.3.3
  23. * sysdeps/unix/sysv/linux/hppa/sysdep.h
  24. */
  25. #ifdef PIC
  26. /* WARNING: CANNOT BE USED IN A NOP! */
  27. # define K_STW_ASM_PIC " copy %%r19, %%r4\n"
  28. # define K_LDW_ASM_PIC " copy %%r4, %%r19\n"
  29. # define K_USING_GR4 "%r4",
  30. #else
  31. # define K_STW_ASM_PIC " \n"
  32. # define K_LDW_ASM_PIC " \n"
  33. # define K_USING_GR4
  34. #endif
  35. /* GCC has to be warned that a syscall may clobber all the ABI
  36. registers listed as "caller-saves", see page 8, Table 2
  37. in section 2.2.6 of the PA-RISC RUN-TIME architecture
  38. document. However! r28 is the result and will conflict with
  39. the clobber list so it is left out. Also the input arguments
  40. registers r20 -> r26 will conflict with the list so they
  41. are treated specially. Although r19 is clobbered by the syscall
  42. we cannot say this because it would violate ABI, thus we say
  43. r4 is clobbered and use that register to save/restore r19
  44. across the syscall. */
  45. #define K_CALL_CLOB_REGS "%r1", "%r2", K_USING_GR4 \
  46. "%r20", "%r29", "%r31"
  47. #undef K_INLINE_SYSCALL
  48. #define K_INLINE_SYSCALL(name, nr, args...) ({ \
  49. long __sys_res; \
  50. { \
  51. register unsigned long __res asm("r28"); \
  52. K_LOAD_ARGS_##nr(args) \
  53. /* FIXME: HACK stw/ldw r19 around syscall */ \
  54. asm volatile( \
  55. K_STW_ASM_PIC \
  56. " ble 0x100(%%sr2, %%r0)\n" \
  57. " ldi %1, %%r20\n" \
  58. K_LDW_ASM_PIC \
  59. : "=r" (__res) \
  60. : "i" (SYS_ify(name)) K_ASM_ARGS_##nr \
  61. : "memory", K_CALL_CLOB_REGS K_CLOB_ARGS_##nr \
  62. ); \
  63. __sys_res = (long)__res; \
  64. } \
  65. if ( (unsigned long)__sys_res >= (unsigned long)-4095 ){ \
  66. errno = -__sys_res; \
  67. __sys_res = -1; \
  68. } \
  69. __sys_res; \
  70. })
  71. #define K_LOAD_ARGS_0()
  72. #define K_LOAD_ARGS_1(r26) \
  73. register unsigned long __r26 __asm__("r26") = (unsigned long)(r26); \
  74. K_LOAD_ARGS_0()
  75. #define K_LOAD_ARGS_2(r26,r25) \
  76. register unsigned long __r25 __asm__("r25") = (unsigned long)(r25); \
  77. K_LOAD_ARGS_1(r26)
  78. #define K_LOAD_ARGS_3(r26,r25,r24) \
  79. register unsigned long __r24 __asm__("r24") = (unsigned long)(r24); \
  80. K_LOAD_ARGS_2(r26,r25)
  81. #define K_LOAD_ARGS_4(r26,r25,r24,r23) \
  82. register unsigned long __r23 __asm__("r23") = (unsigned long)(r23); \
  83. K_LOAD_ARGS_3(r26,r25,r24)
  84. #define K_LOAD_ARGS_5(r26,r25,r24,r23,r22) \
  85. register unsigned long __r22 __asm__("r22") = (unsigned long)(r22); \
  86. K_LOAD_ARGS_4(r26,r25,r24,r23)
  87. #define K_LOAD_ARGS_6(r26,r25,r24,r23,r22,r21) \
  88. register unsigned long __r21 __asm__("r21") = (unsigned long)(r21); \
  89. K_LOAD_ARGS_5(r26,r25,r24,r23,r22)
  90. /* Even with zero args we use r20 for the syscall number */
  91. #define K_ASM_ARGS_0
  92. #define K_ASM_ARGS_1 K_ASM_ARGS_0, "r" (__r26)
  93. #define K_ASM_ARGS_2 K_ASM_ARGS_1, "r" (__r25)
  94. #define K_ASM_ARGS_3 K_ASM_ARGS_2, "r" (__r24)
  95. #define K_ASM_ARGS_4 K_ASM_ARGS_3, "r" (__r23)
  96. #define K_ASM_ARGS_5 K_ASM_ARGS_4, "r" (__r22)
  97. #define K_ASM_ARGS_6 K_ASM_ARGS_5, "r" (__r21)
  98. /* The registers not listed as inputs but clobbered */
  99. #define K_CLOB_ARGS_6
  100. #define K_CLOB_ARGS_5 K_CLOB_ARGS_6, "%r21"
  101. #define K_CLOB_ARGS_4 K_CLOB_ARGS_5, "%r22"
  102. #define K_CLOB_ARGS_3 K_CLOB_ARGS_4, "%r23"
  103. #define K_CLOB_ARGS_2 K_CLOB_ARGS_3, "%r24"
  104. #define K_CLOB_ARGS_1 K_CLOB_ARGS_2, "%r25"
  105. #define K_CLOB_ARGS_0 K_CLOB_ARGS_1, "%r26"
  106. #define _syscall0(type,name) \
  107. type name(void) \
  108. { \
  109. return (type) K_INLINE_SYSCALL(name, 0); \
  110. }
  111. #define _syscall1(type,name,type1,arg1) \
  112. type name(type1 arg1) \
  113. { \
  114. return (type) K_INLINE_SYSCALL(name, 1, arg1); \
  115. }
  116. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  117. type name(type1 arg1, type2 arg2) \
  118. { \
  119. return (type) K_INLINE_SYSCALL(name, 2, arg1, arg2); \
  120. }
  121. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  122. type name(type1 arg1, type2 arg2, type3 arg3) \
  123. { \
  124. return (type) K_INLINE_SYSCALL(name, 3, arg1, arg2, arg3); \
  125. }
  126. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  127. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  128. { \
  129. return (type) K_INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4); \
  130. }
  131. /* select takes 5 arguments */
  132. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
  133. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
  134. { \
  135. return (type) K_INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5); \
  136. }
  137. /* mmap & mmap2 take 6 arguments */
  138. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6) \
  139. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6) \
  140. { \
  141. return (type) K_INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6); \
  142. }
  143. #endif /* __ASSEMBLER__ */
  144. #endif /* _BITS_SYSCALLS_H */