-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyscall.h
348 lines (280 loc) · 11.6 KB
/
syscall.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#ifndef minilib_syscall_h
#define minilib_syscall_h
#ifdef mini_vsyscalls
extern int __mini_vsys;
#endif
//extern int sysret;
#ifdef mini_errno
#ifndef mini_globalregister
extern int errno;
#endif
#endif
#ifdef OSX
#define NCONST 0x2000000
#define SCALL(call) SYS_##call
#define __SYSCALL(call) SYS##call
#include <sys/syscall.h>
#else
#define NCONST 0
#define SCALL(call) __NR_##call
#define __SYSCALL(call) __NR##call
#ifdef X64
#include <sys/syscall.h>
#else
#include "i386syscalls.h"
#endif
#endif //OSX
//void opt_fence(void*p,...);
#ifndef OPTFENCE
//+doc prevent gcc to optimize away registers and variables
// the macro OPTFENCE(...) can be invoked with any parameter.
// The parameters will get calculated, even if gcc doesn't recognize
// the use of the parameters, e.g. cause they are needed for an inlined asm syscall.
//
// The macro translates to an asm jmp and a function call to the function
// opt_fence, which is defined with the attribute "noipa" -
// (the compiler "forgets" the function body, so gcc is forced
// to generate all arguments for the function)
// The generated asm jump hops over the call to the function,
// but this gcc doesn't recognize.
//
// This generates some overhead,
// (a few (never reached) bytes for setting up the function call, and the jmp)
// but I didn't find any other solution,
// which gcc wouldn't cut for optimizations from time to time.
// (volatile, volatile asm, optimize attributes,
// andsoon have all shown up to be unreliable - sometimes(!)).
//
// Had some fun debugging these bugs, which naturally showed up only sometimes.
// (Many syscalls also work with scrambled arguments..)
// And, I believe it IS a compiler bug.
// Volatile should be volatile for sure, not only sometimes.
// I mean, why the heck do I write volatile??
//+def OPTFENCE
#ifndef __clang__
static void __attribute__((noipa,cold,naked)) opt_fence(void*p,...){}
#define _optjmp(a,b) asm( a "OPTFENCE_"#b )
#define _optlabel(a) asm( "OPTFENCE_" #a ":" )
#define __optfence(a,...) _optjmp("jmp ", a ); opt_fence(__VA_ARGS__); _optlabel(a)
#define OPTFENCE(...) __optfence(__COUNTER__,__VA_ARGS__)
#else
#define OPTFENCE(...)
#endif
#endif //OPTFENCE
// syscall table at: /usr/src/linux/include/linux/syscalls.h.
// table, ordered: /usr/src/linux/arch/x86/syscalls/syscall_32.tbl
// defining sycalls as static inline has these advantages:
// -not compiled, if not used
// -optimization can take place, gcc (4.4.5, at least) doesn't use the stack at all.
// since the syscall parameters have to be placed into registers,
// using functions would mean push and pop every parameter.
// even with -O (lowest Optimization) gcc handles putting the parameters into the right registers fine.
// so static inline even results often in smaller codesize than not inlining.
//
// when minilib is compiled without errno,
// the syscalls return the negative value of errno on error.
//
// Seems linux x86_64 has same convention as osx darwin
#ifdef X64
// memory clobber is needed, gcc optimizes syscalls very likely away without
#define __callend : "memory","rcx", "r11" )
#define __callend0 __callend
#define __callend1 __callend
#define __callend2 __callend
#define __callend3 __callend
#define __callend4 __callend; OPTFENCE((void*)r10)
#define __callend5 __callend; OPTFENCE((void*)r10,(void*)r8)
#define __callend6 __callend; OPTFENCE((void*)r10,(void*)r8,(void*)r9)
//(also osx)
#define __SYSCALL_ASM(ret,call) asm volatile ("syscall" : "=a" (ret) : "a" ( (call | NCONST ) )
#else
#ifdef mini_vsyscalls
#define __SYSCALL_ASM(ret,call) asm volatile ("call *__mini_vsys" : "=a" (ret) : "a" (call)
#else
//linux32bit
// memory clobber is needed, gcc optimizes syscalls very likely away without
#define __callend : "memory" )
#define __SYSCALL_ASM(ret,call) asm volatile ("int $0x80" : "=a" (ret) : "a" (call)
#endif
#endif
#ifdef X64
// also osx
#define syscall1(ret,call,a1) __SYSCALL_ASM(ret,call) , "D" (a1) __callend
#define syscall2(ret,call,a1,a2) __SYSCALL_ASM(ret,call) , "D" (a1), "S" (a2) __callend
#define syscall3(ret,call,a1,a2,a3) __SYSCALL_ASM(ret,call) , "D" (a1), "S" (a2), "d" (a3) __callend
#define syscall4(ret,call,a1,a2,a3,a4) register long int r10 asm ("r10") = a4 ; __SYSCALL_ASM(ret,call) , "D" (a1), "S" (a2), "d" (a3), "r" (r10) __callend
#define syscall5(ret,call,a1,a2,a3,a4,a5) register long int r10 asm ("r10") = a4 ; register long int r8 asm ("r8") = a5 ; __SYSCALL_ASM(ret,call) , "D" (a1), "S" (a2), "d" (a3), "r" (r10), "r" (r8) __callend
#define syscall6(ret,call,a1,a2,a3,a4,a5,a6) register long int r10 asm ("r10") = a4 ; register long int r8 asm ("r8") = a5 ; register long int r9 asm ("r9") = a6; __SYSCALL_ASM(ret,call) , "D" (a1), "S" (a2), "d" (a3), "r" (r10), "r" (r8), "r" (r9) __callend
// save value in (temporary) var sysret.
// return -1 if an error occured, set errno.
#define syscall0_ret(call) __SYSCALL_ASM(sysret,call)
#define syscall1_ret(call) syscall0_ret(call) , "D" (a1)
#define syscall2_ret(call) syscall1_ret(call) , "S" (a2)
#define syscall3_ret(call) syscall2_ret(call) , "d" (a3)
#define syscall4_ret(call) register long int r10 asm("r10")= a4; syscall3_ret(call) , "r" (r10)
#define syscall5_ret(call) register long int r10 asm("r10")= a4; register long int r8 asm("r8")= a5 ; syscall3_ret(call) , "r" (r8)
#define syscall6_ret(call) register long int r10 asm("r10")= a4; register long int r8 asm("r8")= a5 ; register long int r9 asm("r9")=a6; syscall3_ret(call) , "r" (r8)
// r9: 6th param..
#else
//linux
#ifndef X64
//linux 32bit
#define syscall1(ret,call,a1) __SYSCALL_ASM(ret,call) , "b" (a1) __callend
#define syscall2(ret,call,a1,a2) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2) __callend
#define syscall3(ret,call,a1,a2,a3) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3) __callend
#define syscall4(ret,call,a1,a2,a3,a4) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3), "S" (a4) __callend
#define syscall5(ret,call,a1,a2,a3,a4,a5) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3), "S" (a4), "D" (a5) __callend
// save value in (temporary) var sysret.
// return -1 if an error occured, set errno.
#define syscall0_ret(call) __SYSCALL_ASM(sysret,call)
#define syscall1_ret(call) syscall0_ret(call) , "b" (a1)
#define syscall2_ret(call) syscall1_ret(call) , "c" (a2)
#define syscall3_ret(call) syscall2_ret(call) , "d" (a3)
#define syscall4_ret(call) syscall3_ret(call) , "S" (a4)
#define syscall5_ret(call) syscall4_ret(call) , "D" (a5)
#else
// linux 86_X64
#define syscall1(ret,call,a1) __SYSCALL_ASM(ret,call) , "b" (a1) __callend
#define syscall2(ret,call,a1,a2) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2) __callend
#define syscall3(ret,call,a1,a2,a3) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3) __callend
#define syscall4(ret,call,a1,a2,a3,a4) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3), "S" (a4) __callend
#define syscall5(ret,call,a1,a2,a3,a4,a5) __SYSCALL_ASM(ret,call) , "b" (a1), "c" (a2), "d" (a3), "S" (a4), "D" (a5) __callend
// save value in (temporary) var sysret.
// return -1 if an error occured, set errno.
#define syscall0_ret(call) __SYSCALL_ASM(sysret,call)
#define syscall1_ret(call) syscall0_ret(call) , "b" (a1)
#define syscall2_ret(call) syscall1_ret(call) , "c" (a2)
#define syscall3_ret(call) syscall2_ret(call) , "d" (a3)
#define syscall4_ret(call) syscall3_ret(call) , "S" (a4)
#define syscall5_ret(call) syscall4_ret(call) , "D" (a5)
#define syscall6_ret(call) syscall5_ret(call) , "r10" (a6)
#endif
#endif
// args: count of parameters, syscall number, [parameters...]
#define __DO_syscall(n,...) syscall##n##_ret( __VA_ARGS__ ) __callend##n
// args: name (e.g. getpid), count of args, arguments (e.g. int* a1, char *a2).
// arguments must be named a1,a2,...
#ifndef GENSYNTAXCHECK
#ifdef mini_errno
#define REAL_define_syscall( name, argcount, ... ) inline \
int volatile __attribute__((always_inline)) name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, (SCALL(name) | NCONST ) );\
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}
// syscalls with more than 4 args may not be optimized nor inlined.
// register assignment gets optimized out otherways.
#define REAL_define_syscall_noopt( name, argcount, ... ) \
int volatile name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, (SCALL(name) | NCONST ) );\
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}
#else //errno
#define REAL_define_syscall( name, argcount, ... ) inline \
int volatile __attribute__((always_inline)) name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, ( SCALL(name) | NCONST ) );\
return( sysret );\
}
#define REAL_define_syscall_noopt( name, argcount, ... ) \
int volatile name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, ( SCALL(name) | NCONST ) );\
return( sysret );\
}
// return( (sysret<0) ? -1 : sysret );
#endif
#ifdef mini_errno
#define SYSREAL_define_syscall( name, argcount, ... ) inline \
int volatile __attribute__((always_inline)) sys##name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, (__SYSCALL(name) | NCONST ) );\
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}
#define SYSREAL_define_syscall_noopt( name, argcount, ... ) \
int volatile sys##name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, (__SYSCALL(name) | NCONST ) );\
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}
#else
#define SYSREAL_define_syscall( name, argcount, ... ) inline \
int volatile __attribute__((always_inline)) sys##name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, ( __SYSCALL(name) | NCONST ) );\
return( sysret );\
}
//return( (sysret<0) ? -1 : sysret );
#define SYSREAL_define_syscall_noopt( name, argcount, ... ) \
int volatile sys##name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, ( __SYSCALL(name) | NCONST ) );\
return( sysret );\
}
#endif
//return( (sysret<0) ? -1 : sysret ); (no errno) : ok. not added anything to the final size
/*
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}
*/
// global sysret: +34 bytes (??)
// errno = (sysret<0) ? 0 : sysret;\ + 42 bytes..
/* (+64bytes) /+38 bytes with local sysret
if ( sysret<0){\
errno = -sysret;\
return(-1);}\
return(sysret);\
}\
*/
// args: name (e.g. getpid), argument to return, count of args, arguments (e.g. int* a1, char *a2).
// arguments must be named a1,a2,...
#ifdef mini_errno
#define REAL_define_syscallret( name, ret, argcount, ... ) inline \
int volatile __attribute__((always_inline)) name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, SCALL(name));\
if ( sysret<0 ){\
errno = -sysret;\
return(-1);}\
return(ret);\
}
#else
#define REAL_define_syscallret( name, ret, argcount, ... ) inline \
int volatile __attribute__((always_inline)) name( __VA_ARGS__ ){\
int sysret;\
__DO_syscall( argcount, SCALL(name));\
if ( sysret<0 ){\
return(sysret);}\
return(ret);\
}
#endif
#define DEF_syscall(...)
#define DEF_syscallret(...)
#else //ifndef gensyntaxcheck
// Boilerplates, to get the syntaxchecking right (syntaxcheck.h)
#define DEF_syscall( name, argcount, ... ) int volatile name( __VA_ARGS__ );
#define DEF_syscallret( name, ret, argcount, ... ) int volatile name( __VA_ARGS__ );
#define SYSDEF_syscall( name, argcount, ... ) int volatile sys##name( __VA_ARGS__ );
#define REAL_define_syscall(...)
#define REAL_define_syscall_noopt(...)
#define REAL_define_syscallret(...)
#define SYSREAL_define_syscall(...)
#define SYSREAL_define_syscall_noopt(...)
#endif
#endif