1
0

syscall.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef _SYSCALL_H
  2. #define _SYSCALL_H
  3. #include <sys/syscall.h>
  4. #define SYSCALL_LL(x) \
  5. ((union { long long ll; long l[2]; }){ .ll = x }).l[0], \
  6. ((union { long long ll; long l[2]; }){ .ll = x }).l[1]
  7. #define SYSCALL_SIGSET_SIZE 8
  8. #if defined(SYSCALL_NORETURN)
  9. static inline long __syscall_ret(unsigned long r)
  10. {
  11. for(;;);
  12. return 0;
  13. }
  14. #elif defined(SYSCALL_RETURN_ERRNO)
  15. static inline long __syscall_ret(unsigned long r)
  16. {
  17. return -r;
  18. }
  19. #else
  20. extern long __syscall_ret(unsigned long);
  21. #endif
  22. static inline long syscall0(long n)
  23. {
  24. unsigned long ret;
  25. __asm__ __volatile__ ("int $128" : "=a"(ret) : "a"(n) : "memory");
  26. return __syscall_ret(ret);
  27. }
  28. #ifndef __PIC__
  29. static inline long syscall1(long n, long a1)
  30. {
  31. unsigned long ret;
  32. __asm__ __volatile__ ("int $128" : "=a"(ret) : "a"(n), "b"(a1) : "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__ ("int $128" : "=a"(ret) : "a"(n), "b"(a1), "c"(a2) : "memory");
  39. return __syscall_ret(ret);
  40. }
  41. static inline long syscall3(long n, long a1, long a2, long a3)
  42. {
  43. unsigned long ret;
  44. __asm__ __volatile__ ("int $128" : "=a"(ret) : "a"(n), "b"(a1), "c"(a2), "d"(a3) : "memory");
  45. return __syscall_ret(ret);
  46. }
  47. static inline long syscall4(long n, long a1, long a2, long a3, long a4)
  48. {
  49. unsigned long ret;
  50. __asm__ __volatile__ ("int $128" : "=a"(ret) : "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4) : "memory");
  51. return __syscall_ret(ret);
  52. }
  53. static inline long syscall5(long n, long a1, long a2, long a3, long a4, long a5)
  54. {
  55. unsigned long ret;
  56. __asm__ __volatile__ ("int $128" : "=a"(ret) : "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5) : "memory");
  57. return __syscall_ret(ret);
  58. }
  59. static inline long syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
  60. {
  61. unsigned long ret;
  62. __asm__ __volatile__ ("pushl %%ebp ; mov %%eax,%%ebp ; movl %1,%%eax ; int $128 ; popl %%ebp"
  63. : "=a"(ret) : "i"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "a"(a6) : "memory");
  64. return __syscall_ret(ret);
  65. }
  66. #else
  67. static inline long syscall1(long n, long a1)
  68. {
  69. unsigned long ret;
  70. __asm__ __volatile__ ("xchg %2,%%ebx ; int $128 ; xchg %2,%%ebx"
  71. : "=a"(ret) : "a"(n), "r"(a1) : "memory");
  72. return __syscall_ret(ret);
  73. }
  74. static inline long syscall2(long n, long a1, long a2)
  75. {
  76. unsigned long ret;
  77. __asm__ __volatile__ ("xchg %2,%%ebx ; int $128 ; xchg %2,%%ebx"
  78. : "=a"(ret) : "a"(n), "r"(a1), "c"(a2) : "memory");
  79. return __syscall_ret(ret);
  80. }
  81. static inline long syscall3(long n, long a1, long a2, long a3)
  82. {
  83. unsigned long ret;
  84. __asm__ __volatile__ ("xchg %2,%%ebx ; int $128 ; xchg %2,%%ebx"
  85. : "=a"(ret) : "a"(n), "r"(a1), "c"(a2), "d"(a3) : "memory");
  86. return __syscall_ret(ret);
  87. }
  88. static inline long syscall4(long n, long a1, long a2, long a3, long a4)
  89. {
  90. unsigned long ret;
  91. __asm__ __volatile__ ("xchg %2,%%ebx ; int $128 ; xchg %2,%%ebx"
  92. : "=a"(ret) : "a"(n), "r"(a1), "c"(a2), "d"(a3), "S"(a4) : "memory");
  93. return __syscall_ret(ret);
  94. }
  95. static inline long syscall5(long n, long a1, long a2, long a3, long a4, long a5)
  96. {
  97. unsigned long ret;
  98. __asm__ __volatile__ ("pushl %%ebx ; mov %%eax,%%ebx ; movl %1,%%eax ; int $128 ; popl %%ebx"
  99. : "=a"(ret) : "i"(n), "a"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5) : "memory");
  100. return __syscall_ret(ret);
  101. }
  102. #define syscall6(n,a1,a2,a3,a4,a5,a6) __syscall((n),(a1),(a2),(a3),(a4),(a5),(a6))
  103. #endif
  104. #define __SC_socket 1
  105. #define __SC_bind 2
  106. #define __SC_connect 3
  107. #define __SC_listen 4
  108. #define __SC_accept 5
  109. #define __SC_getsockname 6
  110. #define __SC_getpeername 7
  111. #define __SC_socketpair 8
  112. #define __SC_send 9
  113. #define __SC_recv 10
  114. #define __SC_sendto 11
  115. #define __SC_recvfrom 12
  116. #define __SC_shutdown 13
  117. #define __SC_setsockopt 14
  118. #define __SC_getsockopt 15
  119. #define __SC_sendmsg 16
  120. #define __SC_recvmsg 17
  121. #define socketcall(nm, a, b, c, d, e, f) syscall2(__NR_socketcall, __SC_##nm, \
  122. (long)(long [6]){ (long)a, (long)b, (long)c, (long)d, (long)e, (long)f })
  123. #undef O_LARGEFILE
  124. #define O_LARGEFILE 0100000
  125. /* the following are needed for iso c functions to use */
  126. #define __syscall_open(filename, flags, mode) syscall3(__NR_open, (long)(filename), (flags)|O_LARGEFILE, (mode))
  127. #define __syscall_read(fd, buf, len) syscall3(__NR_read, (fd), (long)(buf), (len))
  128. #define __syscall_write(fd, buf, len) syscall3(__NR_write, (fd), (long)(buf), (len))
  129. #define __syscall_close(fd) syscall1(__NR_close, (fd))
  130. #define __syscall_fcntl(fd, cmd, arg) syscall3(__NR_fcntl64, (fd), (cmd), (long)(arg))
  131. #define __syscall_dup2(old, new) syscall2(__NR_dup2, (old), (new))
  132. #define __syscall_unlink(path) syscall1(__NR_unlink, (long)(path))
  133. #define __syscall_getpid() syscall0(__NR_getpid)
  134. #define __syscall_kill(pid,sig) syscall2(__NR_kill, (pid), (sig))
  135. #define __syscall_sigaction(sig,new,old) syscall4(__NR_rt_sigaction, (sig), (long)(new), (long)(old), SYSCALL_SIGSET_SIZE)
  136. #define __syscall_ioctl(fd,ioc,arg) syscall3(__NR_ioctl, (fd), (ioc), (long)(arg))
  137. #define __syscall_exit(code) syscall1(__NR_exit, code)
  138. long __syscall(long, ...);
  139. #endif