syscalls.h 5.2 KB

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