OSDN Git Service

d31304430c775f87718dcd07e9ce024abfbcae13
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / x86_64 / bits / syscalls.h
1 #ifndef _BITS_SYSCALLS_H
2 #define _BITS_SYSCALLS_H
3 #ifndef _SYSCALL_H
4 # error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
5 #endif
6
7 /*
8    Some of the sneaky macros in the code were taken from 
9    glibc-2.2.5/sysdeps/unix/sysv/linux/x86_64/sysdep.h
10 */
11
12 #ifndef __ASSEMBLER__
13
14 #include <errno.h>
15
16 #define SYS_ify(syscall_name)  (__NR_##syscall_name)
17
18 #undef _syscall0
19 #define _syscall0(type,name) \
20 type name(void) \
21 { \
22 return (type) (INLINE_SYSCALL(name, 0)); \
23 }
24
25 #undef _syscall1
26 #define _syscall1(type,name,type1,arg1) \
27 type name(type1 arg1) \
28 { \
29 return (type) (INLINE_SYSCALL(name, 1, arg1)); \
30 }
31
32 #undef _syscall2
33 #define _syscall2(type,name,type1,arg1,type2,arg2) \
34 type name(type1 arg1,type2 arg2) \
35 { \
36 return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
37 }
38
39 #undef _syscall3
40 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
41 type name(type1 arg1,type2 arg2,type3 arg3) \
42 { \
43 return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
44 }
45
46 #undef _syscall4
47 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
48 type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
49 { \
50 return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
51
52
53 #undef _syscall5
54 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
55           type5,arg5) \
56 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
57 { \
58 return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
59 }
60
61 #undef _syscall6
62 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
63           type5,arg5,type6,arg6) \
64 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
65 { \
66 return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
67 }
68
69 /* The Linux/x86-64 kernel expects the system call parameters in
70    registers according to the following table:
71
72     syscall number      rax
73     arg 1               rdi
74     arg 2               rsi
75     arg 3               rdx
76     arg 4               r10
77     arg 5               r8
78     arg 6               r9
79
80     The Linux kernel uses and destroys internally these registers:
81     return address from
82     syscall             rcx
83     additionally clobered: r12-r15,rbx,rbp
84     eflags from syscall r11
85
86     Normal function call, including calls to the system call stub
87     functions in the libc, get the first six parameters passed in
88     registers and the seventh parameter and later on the stack.  The
89     register use is as follows:
90
91      system call number in the DO_CALL macro
92      arg 1              rdi
93      arg 2              rsi
94      arg 3              rdx
95      arg 4              rcx
96      arg 5              r8
97      arg 6              r9
98
99     We have to take care that the stack is aligned to 16 bytes.  When
100     called the stack is not aligned since the return address has just
101     been pushed.
102
103
104     Syscalls of more than 6 arguments are not supported.  */
105
106 #undef SYS_ify
107 #define SYS_ify(syscall_name)   __NR_##syscall_name
108
109 #undef  DO_CALL
110 #define DO_CALL(syscall_name, args)             \
111     DOARGS_##args                               \
112     movq $SYS_ify (syscall_name), %rax;         \
113     syscall;
114
115 #define DOARGS_0 /* nothing */
116 #define DOARGS_1 /* nothing */
117 #define DOARGS_2 /* nothing */
118 #define DOARGS_3 /* nothing */
119 #define DOARGS_4 movq %rcx, %r10;
120 #define DOARGS_5 DOARGS_4
121 #define DOARGS_6 DOARGS_5
122
123 /* Define a macro which expands inline into the wrapper code for a system
124    call.  */
125 #undef INLINE_SYSCALL
126 #define INLINE_SYSCALL(name, nr, args...) \
127   ({                                                                          \
128     unsigned long _resultvar = INTERNAL_SYSCALL (name, , nr, args);           \
129     if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_resultvar, ), 0))        \
130       {                                                                       \
131         __set_errno (INTERNAL_SYSCALL_ERRNO (_resultvar, ));                  \
132         _resultvar = (unsigned long) -1;                                              \
133       }                                                                       \
134     (long) _resultvar; })
135
136 #undef INTERNAL_SYSCALL_DECL
137 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
138
139 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
140   ({                                                                          \
141     unsigned long resultvar;                                                  \
142     LOAD_ARGS_##nr (args)                                                     \
143     LOAD_REGS_##nr                                                            \
144     asm volatile (                                                            \
145     "syscall\n\t"                                                             \
146     : "=a" (resultvar)                                                        \
147     : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx");                \
148     (long) resultvar; })
149 #undef INTERNAL_SYSCALL
150 #define INTERNAL_SYSCALL(name, err, nr, args...) \
151   INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
152
153 #undef INTERNAL_SYSCALL_ERROR_P
154 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
155   ((unsigned long) (val) >= -4095L)
156
157 #undef INTERNAL_SYSCALL_ERRNO
158 #define INTERNAL_SYSCALL_ERRNO(val, err)        (-(val))
159
160 #define LOAD_ARGS_0()
161 #define LOAD_REGS_0
162 #define ASM_ARGS_0
163
164 #define LOAD_ARGS_1(a1)                                 \
165   long int __arg1 = (long) (a1);                        \
166   LOAD_ARGS_0 ()
167 #define LOAD_REGS_1                                     \
168   register long int _a1 asm ("rdi") = __arg1;           \
169   LOAD_REGS_0
170 #define ASM_ARGS_1      ASM_ARGS_0, "r" (_a1)
171
172 #define LOAD_ARGS_2(a1, a2)                             \
173   long int __arg2 = (long) (a2);                        \
174   LOAD_ARGS_1 (a1)
175 #define LOAD_REGS_2                                     \
176   register long int _a2 asm ("rsi") = __arg2;           \
177   LOAD_REGS_1
178 #define ASM_ARGS_2      ASM_ARGS_1, "r" (_a2)
179
180 #define LOAD_ARGS_3(a1, a2, a3)                         \
181   long int __arg3 = (long) (a3);                        \
182   LOAD_ARGS_2 (a1, a2)
183 #define LOAD_REGS_3                                     \
184   register long int _a3 asm ("rdx") = __arg3;           \
185   LOAD_REGS_2
186 #define ASM_ARGS_3      ASM_ARGS_2, "r" (_a3)
187
188 #define LOAD_ARGS_4(a1, a2, a3, a4)                     \
189   long int __arg4 = (long) (a4);                        \
190   LOAD_ARGS_3 (a1, a2, a3)
191 #define LOAD_REGS_4                                     \
192   register long int _a4 asm ("r10") = __arg4;           \
193   LOAD_REGS_3
194 #define ASM_ARGS_4      ASM_ARGS_3, "r" (_a4)
195
196 #define LOAD_ARGS_5(a1, a2, a3, a4, a5)                 \
197   long int __arg5 = (long) (a5);                        \
198   LOAD_ARGS_4 (a1, a2, a3, a4)
199 #define LOAD_REGS_5                                     \
200   register long int _a5 asm ("r8") = __arg5;            \
201   LOAD_REGS_4
202 #define ASM_ARGS_5      ASM_ARGS_4, "r" (_a5)
203
204 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)             \
205   long int __arg6 = (long) (a6);                        \
206   LOAD_ARGS_5 (a1, a2, a3, a4, a5)
207 #define LOAD_REGS_6                                     \
208   register long int _a6 asm ("r9") = __arg6;            \
209   LOAD_REGS_5
210 #define ASM_ARGS_6      ASM_ARGS_5, "r" (_a6)
211
212 #endif /* __ASSEMBLER__ */
213 #endif /* _BITS_SYSCALLS_H */