syscall.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef _SYSCALL_H
  2. #define _SYSCALL_H
  3. #include <sys/syscall.h>
  4. #define SYSCALL_LL(x) x, 0
  5. #define SYSCALL_SIGSET_SIZE 8
  6. #if defined(SYSCALL_NORETURN)
  7. static inline long __syscall_ret(unsigned long r)
  8. {
  9. for(;;);
  10. return 0;
  11. }
  12. #elif defined(SYSCALL_RETURN_ERRNO)
  13. static inline long __syscall_ret(unsigned long r)
  14. {
  15. return -r;
  16. }
  17. #else
  18. extern long __syscall_ret(unsigned long);
  19. #endif
  20. // 64: di, si, dx, r10, r8, r9
  21. // 32: ebx, ecx, edx, esi, edi, ebp
  22. #define SYSCALL "syscall"
  23. static inline long syscall0(long n)
  24. {
  25. unsigned long ret;
  26. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n) : "rcx", "r11", "memory");
  27. return __syscall_ret(ret);
  28. }
  29. static inline long syscall1(long n, long a1)
  30. {
  31. unsigned long ret;
  32. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory");
  33. return __syscall_ret(ret);
  34. }
  35. static inline long syscall2(long n, long a1, long a2)
  36. {
  37. unsigned long ret;
  38. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2)
  39. : "rcx", "r11", "memory");
  40. return __syscall_ret(ret);
  41. }
  42. static inline long syscall3(long n, long a1, long a2, long a3)
  43. {
  44. unsigned long ret;
  45. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
  46. "d"(a3) : "rcx", "r11", "memory");
  47. return __syscall_ret(ret);
  48. }
  49. static inline long syscall4(long n, long a1, long a2, long a3, long a4)
  50. {
  51. unsigned long ret;
  52. register long r10 __asm__("r10") = a4;
  53. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
  54. "d"(a3), "r"(r10): "rcx", "r11", "memory");
  55. return __syscall_ret(ret);
  56. }
  57. static inline long syscall5(long n, long a1, long a2, long a3, long a4,
  58. long a5)
  59. {
  60. unsigned long ret;
  61. register long r10 __asm__("r10") = a4;
  62. register long r8 __asm__("r8") = a5;
  63. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
  64. "d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory");
  65. return __syscall_ret(ret);
  66. }
  67. static inline long syscall6(long n, long a1, long a2, long a3, long a4,
  68. long a5, long a6)
  69. {
  70. unsigned long ret;
  71. register long r10 __asm__("r10") = a4;
  72. register long r8 __asm__("r8") = a5;
  73. register long r9 __asm__("r9") = a6;
  74. __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
  75. "d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory");
  76. return __syscall_ret(ret);
  77. }
  78. #undef O_LARGEFILE
  79. #define O_LARGEFILE 0100000
  80. #define socketcall(nm, a, b, c, d, e, f) syscall6(__NR_##nm, \
  81. (long)a, (long)b, (long)c, (long)d, (long)e, (long)f)
  82. /* the following are needed for iso c functions to use */
  83. #define __syscall_open(filename, flags, mode) syscall3(__NR_open, (long)(filename), (flags)|O_LARGEFILE, (mode))
  84. #define __syscall_read(fd, buf, len) syscall3(__NR_read, (fd), (long)(buf), (len))
  85. #define __syscall_write(fd, buf, len) syscall3(__NR_write, (fd), (long)(buf), (len))
  86. #define __syscall_close(fd) syscall1(__NR_close, (fd))
  87. #define __syscall_fcntl(fd, cmd, arg) syscall3(__NR_fcntl, (fd), (cmd), (long)(arg))
  88. #define __syscall_dup2(old, new) syscall2(__NR_dup2, (old), (new))
  89. #define __syscall_unlink(path) syscall1(__NR_unlink, (long)(path))
  90. #define __syscall_getpid() syscall0(__NR_getpid)
  91. #define __syscall_kill(pid,sig) syscall2(__NR_kill, (pid), (sig))
  92. #define __syscall_sigaction(sig,new,old) syscall4(__NR_rt_sigaction, (sig), (long)(new), (long)(old), SYSCALL_SIGSET_SIZE)
  93. #define __syscall_ioctl(fd,ioc,arg) syscall3(__NR_ioctl, (fd), (ioc), (long)(arg))
  94. #define __syscall_exit(code) syscall1(__NR_exit, code)
  95. long __syscall(long, ...);
  96. #endif