syscalls.h 5.4 KB

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