syscalls.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 .../sysdeps/unix/sysv/linux/xtensa/sysdep.h
  9. */
  10. #define SYS_ify(syscall_name) __NR_##syscall_name
  11. #ifdef __ASSEMBLER__
  12. /* The register layout upon entering the function is:
  13. return addr stack ptr arg0, arg1, arg2, arg3, arg4, arg5
  14. ----------- --------- ----------------------------------
  15. a0 a1 a2, a3, a4, a5, a6, a7
  16. (Of course a function with say 3 arguments does not have entries for
  17. arguments 4, 5, and 6.)
  18. Linux takes system-call arguments in registers. The ABI and Xtensa
  19. software conventions require the system-call number in a2. We move any
  20. argument that was in a2 to a7, and a7 to a8 if we have all 6 arguments.
  21. Note that for improved efficiency, we do NOT shift all parameters down
  22. one register to maintain the original order.
  23. syscall number arg0, arg1, arg2, arg3, arg4, arg5
  24. -------------- ----------------------------------
  25. a2 a6, a3, a4, a5, a8, a9
  26. Upon return, a2 and a3 are clobbered; all other registers are preserved. */
  27. #undef DO_CALL
  28. #define DO_CALL(syscall_name, nargs) \
  29. DO_ARGS_##nargs \
  30. movi a2, SYS_ify (syscall_name); \
  31. syscall
  32. #define DO_ARGS_0
  33. #define DO_ARGS_1 mov a6, a2;
  34. #define DO_ARGS_2 mov a6, a2;
  35. #define DO_ARGS_3 mov a6, a2;
  36. #define DO_ARGS_4 mov a6, a2;
  37. #define DO_ARGS_5 mov a8, a6; mov a6, a2;
  38. #define DO_ARGS_6 mov a9, a7; mov a8, a6; mov a6, a2;
  39. #else /* not __ASSEMBLER__ */
  40. #include <errno.h>
  41. #define STR(s) #s
  42. #define LD_ARG(n,ar) register int _a##n __asm__ (STR(a##n)) = (int) (ar)
  43. #define LD_ARGS_0()
  44. #define LD_ARGS_1(a0) LD_ARG(6,a0)
  45. #define LD_ARGS_2(a0,a1) LD_ARGS_1(a0); LD_ARG(3,a1)
  46. #define LD_ARGS_3(a0,a1,a2) LD_ARGS_2(a0,a1); LD_ARG(4,a2)
  47. #define LD_ARGS_4(a0,a1,a2,a3) LD_ARGS_3(a0,a1,a2); LD_ARG(5,a3)
  48. #define LD_ARGS_5(a0,a1,a2,a3,a4) LD_ARGS_4(a0,a1,a2,a3); LD_ARG(8,a4)
  49. #define LD_ARGS_6(a0,a1,a2,a3,a4,a5) LD_ARGS_5(a0,a1,a2,a3,a4); LD_ARG(9,a5)
  50. #define ASM_ARGS_0 "r"(_a2)
  51. #define ASM_ARGS_1 ASM_ARGS_0, "r"(_a6)
  52. #define ASM_ARGS_2 ASM_ARGS_1, "r"(_a3)
  53. #define ASM_ARGS_3 ASM_ARGS_2, "r"(_a4)
  54. #define ASM_ARGS_4 ASM_ARGS_3, "r"(_a5)
  55. #define ASM_ARGS_5 ASM_ARGS_4, "r"(_a8)
  56. #define ASM_ARGS_6 ASM_ARGS_5, "r"(_a9)
  57. /* Define a macro which expands into the inline wrapper code for a system
  58. call. */
  59. #undef INLINE_SYSCALL
  60. #define INLINE_SYSCALL(name, nr, args...) \
  61. ({ unsigned long resultvar = INTERNAL_SYSCALL (name, , nr, args); \
  62. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
  63. { \
  64. __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
  65. resultvar = (unsigned long) -1; \
  66. } \
  67. (long) resultvar; })
  68. #undef INTERNAL_SYSCALL_DECL
  69. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  70. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  71. ({ LD_ARG(2, name); \
  72. LD_ARGS_##nr(args); \
  73. __asm__ __volatile__ ("syscall\n" \
  74. : "=a" (_a2) \
  75. : ASM_ARGS_##nr \
  76. : "memory"); \
  77. (long) _a2; })
  78. #undef INTERNAL_SYSCALL
  79. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  80. INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
  81. #undef INTERNAL_SYSCALL_ERROR_P
  82. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  83. ((unsigned long) (val) >= -4095L)
  84. #undef INTERNAL_SYSCALL_ERRNO
  85. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  86. #define _syscall0(args...) SYSCALL_FUNC (0, args)
  87. #define _syscall1(args...) SYSCALL_FUNC (1, args)
  88. #define _syscall2(args...) SYSCALL_FUNC (2, args)
  89. #define _syscall3(args...) SYSCALL_FUNC (3, args)
  90. #define _syscall4(args...) SYSCALL_FUNC (4, args)
  91. #define _syscall5(args...) SYSCALL_FUNC (5, args)
  92. #define _syscall6(args...) SYSCALL_FUNC (6, args)
  93. #define C_DECL_ARGS_0() void
  94. #define C_DECL_ARGS_1(t, v) t v
  95. #define C_DECL_ARGS_2(t, v, args...) t v, C_DECL_ARGS_1(args)
  96. #define C_DECL_ARGS_3(t, v, args...) t v, C_DECL_ARGS_2(args)
  97. #define C_DECL_ARGS_4(t, v, args...) t v, C_DECL_ARGS_3(args)
  98. #define C_DECL_ARGS_5(t, v, args...) t v, C_DECL_ARGS_4(args)
  99. #define C_DECL_ARGS_6(t, v, args...) t v, C_DECL_ARGS_5(args)
  100. #define C_ARGS_0()
  101. #define C_ARGS_1(t, v) v
  102. #define C_ARGS_2(t, v, args...) v, C_ARGS_1 (args)
  103. #define C_ARGS_3(t, v, args...) v, C_ARGS_2 (args)
  104. #define C_ARGS_4(t, v, args...) v, C_ARGS_3 (args)
  105. #define C_ARGS_5(t, v, args...) v, C_ARGS_4 (args)
  106. #define C_ARGS_6(t, v, args...) v, C_ARGS_5 (args)
  107. #define SYSCALL_FUNC(nargs, type, name, args...) \
  108. type name (C_DECL_ARGS_##nargs (args)) { \
  109. return (type) INLINE_SYSCALL (name, nargs, C_ARGS_##nargs (args)); \
  110. }
  111. #endif /* not __ASSEMBLER__ */
  112. #endif /* _BITS_SYSCALLS_H */