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