syscalls-common.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Common syscall type defines
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #ifndef _SYSCALLS_COMMON_H
  7. #define _SYSCALLS_COMMON_H 1
  8. #ifndef _SYSCALL_H
  9. # error "Never use <bits/syscalls-common.h> directly; include <sys/syscall.h> instead."
  10. #endif
  11. #ifndef SYS_ify
  12. # define SYS_ify(syscall_name) (__NR_##syscall_name)
  13. #endif
  14. #ifndef __ASSEMBLER__
  15. #include <errno.h>
  16. #ifndef INTERNAL_SYSCALL_DECL
  17. # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  18. #endif
  19. #ifndef INTERNAL_SYSCALL_ERROR_P
  20. # define INTERNAL_SYSCALL_ERROR_P(val, err) ((unsigned long)val >= (unsigned long)(-4095))
  21. #endif
  22. #ifndef INTERNAL_SYSCALL_ERRNO
  23. # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  24. #endif
  25. /* Define a macro which expands into the inline wrapper code for a system call */
  26. #ifndef INLINE_SYSCALL
  27. # define INLINE_SYSCALL(name, nr, args...) INLINE_SYSCALL_NCS(__NR_##name, nr, args)
  28. #endif
  29. /* Just like INLINE_SYSCALL(), but take a non-constant syscall (NCS) argument */
  30. #ifndef INLINE_SYSCALL_NCS
  31. # define INLINE_SYSCALL_NCS(name, nr, args...) \
  32. ({ \
  33. INTERNAL_SYSCALL_DECL(__err); \
  34. long __res = INTERNAL_SYSCALL_NCS(name, __err, nr, args); \
  35. if (unlikely(INTERNAL_SYSCALL_ERROR_P(__res, __err))) { \
  36. __set_errno(INTERNAL_SYSCALL_ERRNO(__res, __err)); \
  37. __res = -1L; \
  38. } \
  39. __res; \
  40. })
  41. #endif
  42. /* No point in forcing people to implement both when they only need one */
  43. #ifndef INTERNAL_SYSCALL
  44. # define INTERNAL_SYSCALL(name, err, nr, args...) INTERNAL_SYSCALL_NCS(__NR_##name, err, nr, args)
  45. #endif
  46. #ifndef INTERNAL_SYSCALL_NCS
  47. # error your port needs to define INTERNAL_SYSCALL_NCS in bits/syscalls.h
  48. #endif
  49. #ifndef _syscall0
  50. #define C_DECL_ARGS_0() void
  51. #define C_DECL_ARGS_1(t, v) t v
  52. #define C_DECL_ARGS_2(t, v, args...) t v, C_DECL_ARGS_1(args)
  53. #define C_DECL_ARGS_3(t, v, args...) t v, C_DECL_ARGS_2(args)
  54. #define C_DECL_ARGS_4(t, v, args...) t v, C_DECL_ARGS_3(args)
  55. #define C_DECL_ARGS_5(t, v, args...) t v, C_DECL_ARGS_4(args)
  56. #define C_DECL_ARGS_6(t, v, args...) t v, C_DECL_ARGS_5(args)
  57. #define C_ARGS_0()
  58. #define C_ARGS_1(t, v) v
  59. #define C_ARGS_2(t, v, args...) v, C_ARGS_1(args)
  60. #define C_ARGS_3(t, v, args...) v, C_ARGS_2(args)
  61. #define C_ARGS_4(t, v, args...) v, C_ARGS_3(args)
  62. #define C_ARGS_5(t, v, args...) v, C_ARGS_4(args)
  63. #define C_ARGS_6(t, v, args...) v, C_ARGS_5(args)
  64. #define SYSCALL_FUNC(nargs, type, name, args...) \
  65. type name(C_DECL_ARGS_##nargs(args)) { \
  66. return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args)); \
  67. }
  68. #define _syscall0(args...) SYSCALL_FUNC(0, args)
  69. #define _syscall1(args...) SYSCALL_FUNC(1, args)
  70. #define _syscall2(args...) SYSCALL_FUNC(2, args)
  71. #define _syscall3(args...) SYSCALL_FUNC(3, args)
  72. #define _syscall4(args...) SYSCALL_FUNC(4, args)
  73. #define _syscall5(args...) SYSCALL_FUNC(5, args)
  74. #define _syscall6(args...) SYSCALL_FUNC(6, args)
  75. #endif /* _syscall0 */
  76. #endif /* __ASSEMBLER__ */
  77. #endif