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