d2a6a58453d6357bd1f6ba131674f75a8da1f897
[util-vserver.git] / lib / syscall-alternative.h
1  // from http://vserver.13thfloor.at/Experimental/SYSCALL/syscall_shiny10.h
2
3 #ifndef __SYSCALL_NEW_H
4 #define __SYSCALL_NEW_H
5
6 /*      Copyright (C) 2005-2006 Herbert Pƶtzl
7
8                 global config options
9
10         __sysc_setret   ... set return value (default none)
11         __sysc_seterr   ... set error value (default errno)
12
13
14                 arch specific config
15
16         __sysc_reg_cid  ... the callid (if define), immediate otherwise
17
18         __sysc_reg(n)   ... syscall argument registers
19
20         __sysc_reg_res  ... simple result register (either that or)
21         __sysc_reg_ret  ... complex result register (and)
22         __sysc_reg_err  ... complex error register
23
24         __sysc_cmd_pre  ... preparation command(s)      __sysc_pre(n)
25         __sysc_cmd_sys  ... the syscall                 __sysc_cmd(n)
26         __sysc_cmd_fin  ... syscall finalization        __sysc_fin(n)
27
28         __sysc_clobber  ... clobbered registers (default memory)
29         __sysc_max_err  ... maximum error number (for simple result)
30
31         __sysc_errc(r,e)        ... error condition (e cmplx)
32         __sysc_retv(t,r,e)      ... syscall return value (e cmplx)
33
34                 if gcc cannot be trusted
35
36         __sysc_load(r,a)        ... asm code for register load
37         __sysc_save(r,a)        ... asm code to save result(s)
38         __sysc_limm(r,i)        ... asm code to load immediate
39         __sysc_rcon(n)          ... syscall register constraint
40         __sysc_regs             ... list of input regs for clobber
41         __sysc_type             ... register type
42         __sysc_aout             ... asm code output constraint
43
44                 if all else fails
45
46         __sc_asmload(n,N,...)   ... asm code to prepare arguments
47         __sc_asmsysc(n,N)       ... asm code to execute syscall
48         __sc_asmsave(n)         ... asm code to store results
49
50 */
51
52
53
54
55 /*      *****************************************
56         ALPHA   ALPHA   ALPHA   ALPHA           *
57         alpha kernel interface                  */
58
59 #if     defined(__alpha__)
60
61 /*      The Alpha calling convention doesn't use the stack until
62         after the first six arguments have been passed in registers.
63
64         scnr:   v0($0)
65         args:   a1($16), a2($17), a3($18), a4($19), a5($20), a6($21)
66         sret:   r0($0)
67         serr:   e0($19) (!=0, err=sret)
68         call:   callsys
69         clob:   memory
70 */
71
72 #define __sysc_reg_cid  "$0"
73 #define __sysc_reg_ret  "$0"
74 #define __sysc_reg_err  "$19"
75 #define __sysc_cmd_sys  "callsys"
76
77 #define __sysc_reg(n)   __arg_##n\
78         ("$16", "$17", "$18", "$19", "$20", "$21")
79
80 #define __sysc_clobber  __sysc_regs,                                    \
81         "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8",                 \
82         "$22", "$23", "$24", "$25", "$27", "$28", "memory"              \
83
84
85
86 /*      *****************************************
87         ARM     ARM     ARM     ARM             *
88         arm kernel interface                    */
89
90 #elif   defined(__arm__)
91
92 /*      The Arm calling convention uses stack args after four arguments
93         but the Linux kernel gets up to seven arguments in registers.
94         
95         scnr:   imm
96         args:   a1(r0), a2(r1), a3(r2), a4(r3), a5(r4), a6(r5),
97         sret:   r0(r0)
98         serr:   (sret >= (unsigned)-EMAXERRNO)
99         call:   swi
100         clob:   memory
101 */
102
103 #define __sysc_max_err  125
104 #define __sysc_reg_res  "r0"
105 #define __sysc_cmd_sys  "swi    %0"
106
107 #define __sysc_reg(n)   __arg_##n\
108         ("r0", "r1", "r2", "r3", "r4", "r5")
109
110 #define __sysc_clobber  __sysc_regs, "memory"
111
112 #warning syscall arch arm not tested yet
113
114
115
116 /*      *****************************************
117         CRIS    CRIS    CRIS    CRIS            *
118         cris v10 kernel interface               */
119
120 #elif   defined(__cris__)
121
122 /*      The Cris calling convention uses stack args after four arguments
123         but the Linux kernel gets up to six arguments in registers.
124
125         scnr:   id(r9)
126         args:   a1(r10), a2(r11), a3(r12), a4(r13), a5(mof), a6(srp),
127         sret:   r0(r10)
128         serr:   (sret >= (unsigned)-EMAXERRNO)
129         call:   break 13
130         clob:   memory
131 */
132
133 #define __sysc_max_err  125
134 #define __sysc_reg_cid  "r9"
135 #define __sysc_reg_res  "r0"
136 #define __sysc_cmd_sys  "break 13"
137
138 #define __sysc_regs     "r10", "r11", "r12", "r13"
139 #define __sysc_reg(n)   __arg_##n\
140         ("r10", "r11", "r12", "r13", "r0", "srp")
141
142 #define __sysc_pre(n)   __casm(n,5,0,"move r0,mof",)
143
144 #define __sysc_clobber  __sysc_regs, "memory"
145
146 #warning syscall arch cris not tested yet
147
148
149
150 /*      *****************************************
151         FRV     FRV     FRV     FRV             *
152         frv kernel interface            */
153
154 #elif   defined(__frv__)
155
156 /*      The C calling convention on FR-V uses the gr8-gr13 registers
157         for the first six arguments, the remainder is spilled onto the
158         stack. the linux kernel syscall interface does so too.
159         
160         scnr:   id(gr7)
161         args:   a1(gr8), a2(gr9), a3(gr10), a4(gr11), a5(gr12), a6(gr13)
162         sret:   r0(gr8)
163         serr:   (sret >= (unsigned)-EMAXERRNO)
164         call:   tra gr0,gr0
165         clob:   memory
166 */
167
168 #define __sysc_max_err  125
169 #define __sysc_reg_cid  "gr7"
170 #define __sysc_reg_res  "gr8"
171 #define __sysc_cmd_sys  "tira   gr0,#0"
172
173 #define __sysc_reg(n)   __arg_##n\
174         ("gr8", "gr9", "gr10", "gr11", "gr12", "gr13")
175
176 #define __sysc_clobber  __sysc_regs, "memory"
177
178 #warning syscall arch frv not tested yet
179
180
181
182 /*      *****************************************
183         H8300   H8300   H8300   H8300           *
184         h8/300 kernel interface                 */
185
186 #elif   defined(__H8300__)
187
188 /*      The H8/300 C calling convention passes the first three
189         arguments in registers. However the linux kernel calling
190         convention passes the first six arguments in registers
191         er1-er6
192
193         scnr:   id(er0)
194         args:   a1(er1), a2(er2), a3(er3), a4(er4), a5(er5), a6(er6)
195         sret:   r0(er0)
196         serr:   (sret >= (unsigned)-EMAXERRNO)
197         call:   trapa #0
198         clob:   memory
199 */
200
201 #define __sysc_max_err  125
202 #define __sysc_reg_res  "er0"
203
204 #define __sysc_reg(n)   __arg_##n\
205         ("er1", "er2", "er3", "er4", "er5", "er6")
206
207 #define __sysc_clobber  "memory"
208
209 #define __sc_asmload(n,N,...)   __sc_asm        (                       \
210         __casm(n,1,1,   "mov.l  %0,er1"         ,                       )\
211         __casm(n,2,1,   "mov.l  %1,er2"         ,                       )\
212         __casm(n,3,1,   "mov.l  %2,er3"         ,                       )\
213         __casm(n,4,1,   "mov.l  %3,er4"         ,                       )\
214         __casm(n,5,1,   "mov.l  %4,er5"         ,                       )\
215         __casm(n,6,1,   "mov.l  er6,@-sp"       ,                       )\
216         __casm(n,6,1,   "mov.l  %5,er6"         ,                       )\
217         ""::__sc_iregs(n,__VA_ARGS__):__sysc_regs)
218
219 #define __sysc_cmd_pre  "mov.l  %0,er0"
220 #define __sysc_cmd_sys  "trapa  #0"
221 #define __sysc_fin(n)   __casm(n,6,0,"mov.l     @sp+,er6",)
222
223 #warning syscall arch h8300 not tested yet
224
225
226
227 /*      *****************************************
228         HPPA    HPPA    HPPA    HPPA            *
229         hppa/64 kernel interface                */
230
231 #elif   defined(__hppa__)
232
233 /*      The hppa calling convention uses r26-r23 for the first 4
234         arguments, the rest is spilled onto the stack. However the
235         Linux kernel passes the first six arguments in the registers
236         r26-r21.
237
238         The system call number MUST ALWAYS be loaded in the delay
239         slot of the ble instruction, or restarting system calls
240         WILL NOT WORK.
241
242         scnr:   id(r20)
243         args:   a1(r26), a2(r25), a3(r24), a4(r23), a5(r22), a6(r21)
244         sret:   r0(r28)
245         serr:   (sret >= (unsigned)-EMAXERRNO)
246         call:   ble  0x100(%%sr2, %%r0)
247         clob:   r1, r2, (r4), r20, r29, r31, memory
248         picr:   pr(r19)
249 */
250
251 #define __sysc_max_err  4095
252 #define __sysc_reg_res  "r28"
253
254 #define __sysc_reg(n)   __arg_##n\
255         ("r26", "r25", "r24", "r23", "r22", "r21")
256
257 #define __sysc_cmd_sys  "ble 0x100(%%sr2,%%r0)"
258
259 #define __sysc_pre(n)                                                   \
260         __pasm(n,1,1,   "copy %%r19, %%r4"      ,                       )
261
262 #define __sysc_fin(n)                                                   \
263         __casm(n,1,1,   "ldi %0,%%r20"          ,                       )\
264         __pasm(n,1,1,   "copy %%r4, %%r19"      ,                       )
265
266 #ifndef __PIC__
267 #define __sysc_clobber  __sysc_regs,                                    \
268         "r1", "r2", "r20", "r29", "r31", "memory"
269 #else
270 #define __sysc_clobber  __sysc_regs,                                    \
271         "r1", "r2", "r4", "r20", "r29", "r31", "memory"
272 #endif
273
274
275 /*      *****************************************
276         I386    I386    I386    I386            *
277         i386 kernel interface                   */
278
279 #elif   defined(__i386__)
280
281 /*      The x86 calling convention uses stack args for all arguments,
282         but the Linux kernel passes the first six arguments in the
283         following registers: ebx, ecx, edx, esi, edi, ebp.
284         
285         scnr:   id(eax)
286         args:   a1(ebx), a2(ecx), a3(edx), a4(esi), a5(edi), a6(ebp) 
287         sret:   r0(eax)
288         serr:   (sret >= (unsigned)-EMAXERRNO)
289         call:   int 0x80
290         picr:   pr(ebx)
291         clob:   memory
292 */
293
294 #define __sysc_max_err  129
295 #define __sysc_reg_res  "eax"
296 #define __sysc_cmd_sys  "int    $0x80"
297
298 #ifndef __PIC__
299 #define __sysc_regs     "ebx", "ecx", "edx", "esi", "edi"
300 #else
301 #define __sysc_regs     "ecx", "edx", "esi", "edi"
302 #endif
303
304 #define __sc_asmload(n,N,...)   __sc_asm        (                       \
305         __casm(n,6,1,   "movl   %5,%%eax"       ,                       )\
306         __casm(n,5,1,   "movl   %4,%%edi"       ,                       )\
307         __casm(n,4,1,   "movl   %3,%%esi"       ,                       )\
308         __casm(n,3,1,   "movl   %2,%%edx"       ,                       )\
309         __casm(n,2,1,   "movl   %1,%%ecx"       ,                       )\
310         __pasm(n,1,1,   "pushl  %%ebx"          ,                       )\
311         __casm(n,1,1,   "movl   %0,%%ebx"       ,                       )\
312         __casm(n,6,1,   "pushl  %%ebp"          ,                       )\
313         ""::__sc_iregs(n,__VA_ARGS__):__sysc_clobber)
314
315 #define __sc_asmsave(n)
316
317 #define __sysc_pre(n)                                                   \
318         __casm(n,6,1,   "movl   %%eax,%%ebp"    ,                       )\
319         __casm(n,0,1,   "movl   %1,%%eax"       ,                       )\
320
321 #define __sysc_fin(n)                                                   \
322         __casm(n,6,1,   "popl   %%ebp"          ,                       )\
323         __pasm(n,1,1,   "popl   %%ebx"          ,                       )\
324
325 #define __sysc_aout     "=a"(__res)
326 #define __sysc_clobber  __sysc_regs, "memory"
327
328
329 /*      *****************************************
330         IA64    IA64    IA64    IA64            *
331         ia64 kernel interface                   */
332
333 #elif   defined(__ia64__)
334
335 /*      The ia64 calling convention uses out0-out7 to pass the first
336         eight arguments (mapped via register windows).
337
338         scnr:   id(r15)
339         args:   a1(out0), a2(out1), ... a5(out4), a6(out5)
340         sret:   r0(r8)
341         serr:   e0(r10)
342         call:   break 0x100000
343         clob:   out6/7, r2/3/9, r11-r14, r16-r31, p6-p15, f6-f15, b6/7
344 */
345
346
347 #define __sysc_reg_ret  "r8"
348 #define __sysc_reg_err  "r10"
349 #define __sysc_reg_cid  "r15"
350 #define __sysc_cmd_sys  "break.i        0x100000"
351
352 #define __sysc_errc(r,e)        ((e) == -1)
353
354 #define __sysc_reg(n)   __arg_##n\
355         ("out0", "out1", "out2", "out3", "out4", "out5")
356
357 #define __sysc_clobber  __sysc_regs,                                    \
358         "out6", "out7", "r2", "r3", "r9", "r11", "r12", "r13",          \
359         "r14", "r16", "r17", "r18", "r19", "r20", "r21", "r22",         \
360         "r23", "r24", "r25", "r26", "r27", "r28", "r29", "r30",         \
361         "r31", "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13",      \
362         "p14", "p15", "f6", "f7", "f8", "f9", "f10", "f11", "f12",      \
363         "f13", "f14", "f15", "f16", "b6", "b7", "cc", "memory"
364
365 #warning syscall arch ia64 not tested yet
366
367
368
369 /*      *****************************************
370         M32R    M32R    M32R    M32R            *
371         m32r kernel interface                   */
372
373 #elif   defined(__M32R__)
374
375 /*      The m32r calling convention uses r0-r7 to pass the first
376         eight arguments (mapped via register windows).
377
378         scnr:   id(r0)
379         args:   a1(r1), a2(r2), a3(r3), a4(r4), a5(r5), a6(r6)
380         sret:   r0(r0)
381         serr:   (sret >= (unsigned)-EMAXERRNO)
382         call:   trap #2
383         clob:   out6/7, r2/3/9, r11-r14, r16-r31, p6-p15, f6-f15, b6/7
384 */
385
386 #define __sysc_max_err  125
387 #define __sysc_reg_cid  "r7"
388 #define __sysc_reg_res  "r0"
389 #define __sysc_cmd_sys  "trap #2"
390
391 #define __sysc_reg(n)   __arg_##n\
392         ("r0", "r1", "r2", "r3", "r4", "r5")
393
394 #define __sysc_clobber  __sysc_regs, "memory"
395
396 #warning syscall arch m32r not tested yet
397
398
399
400 /*      *****************************************
401         M68K    M68K    M68K    M68K            *
402         m68k kernel interface                   */
403
404 #elif   defined(__m68000__)
405
406 #error syscall arch m68k not implemented yet
407
408
409
410 /*      *****************************************
411         MIPS    MIPS    MIPS    MIPS            *
412         mips kernel interface                   */
413
414 #elif   defined(__mips__)
415
416 /*      The ABIO32 calling convention uses a0-a3  to pass the first
417         four arguments, the rest is passed on the userspace stack.  The 5th arg
418         starts at 16($sp).
419
420         ABIN32 and ABI64 pass 6 args in a0-a3, t0-t1.
421
422         scnr:   id(v0)
423         args:   a1(a0), a2(a1), a3(a2), a4(a3), a5(16($sp)), a6(20($sp))
424         sret:   r0(v0)
425         serr:   e0(a3)
426         call:   syscall
427         clob:   at, v0, t0-t7, t8-t9
428 */
429
430 #define __sysc_reg_cid  "v0"
431 #define __sysc_reg_ret  "v0"
432 #define __sysc_reg_err  "a3"
433 #define __sysc_cmd_sys  "syscall"
434
435 #define __sysc_reg(n) __arg_##n\
436         ("a0","a1","a2","a3", "t0", "t1")
437
438 #define __sysc_clobber "$1", "$3", "$8", "$9", "$10", "$11", "$12",     \
439         "$13", "$14", "$15", "$24", "$25", "memory"
440
441 #if _MIPS_SIM == _ABIO32
442 #define __sysc_pre(n)                                                   \
443         __casm(n,5,1,"addiu $sp,$sp,-32",)                              \
444         __casm(n,6,1,"sw $9,20($sp)",)                                  \
445         __casm(n,5,1,"sw $8, 16($sp)",)
446 #define __sysc_fin(n)                                                   \
447         __casm(n,5,1,"addiu $sp,$sp,32",)
448 #elif (_MIPS_SIM == _ABIN32) || (_MIPS_SIM == _ABI64)
449 #warning syscall arch mips with ABI N32 and 64 not tested yet
450 #else
451 #error unknown mips ABI version
452 #endif
453
454
455 /*      *****************************************
456         PPC     PPC     PPC     PPC             *
457         ppc/64 kernel interface                 */
458
459 #elif   defined(__powerpc__)
460
461 /*      The powerpc calling convention uses r3-r10 to pass the first
462         eight arguments, the remainder is spilled onto the stack.
463         
464         scnr:   id(r0)
465         args:   a1(r3), a2(r4), a3(r5), a4(r6), a5(r7), a6(r8)
466         sret:   r0(r3)
467         serr:   (carry)
468         call:   sc
469         clob:   cr0, ctr
470 */
471
472
473 #define __sysc_reg_cid  "r0"
474 #define __sysc_reg_ret  "r3"
475 #define __sysc_reg_err  "r0"
476
477 #define __sysc_errc(r,e)        ((e) & 0x10000000)
478
479 #define __sysc_reg(n)   __arg_##n\
480         ("r3", "r4", "r5", "r6", "r7", "r8")
481
482 #define __sysc_cmd_sys  "sc"
483 #define __sysc_cmd_fin  "mfcr 0"
484
485 #define __sysc_clobber  __sysc_regs,                                    \
486         "r9", "r10", "r11", "r12", "cr0", "ctr", "memory"
487
488
489
490 /*      *****************************************
491         S390    S390    S390    S390            *
492         s390/x kernel interface                 */
493
494 #elif   defined(__s390__)
495
496 /*      The s390x calling convention passes the first five arguments
497         in r2-r6, the remainder is spilled onto the stack. However
498         the Linux kernel passes the first six arguments in r2-r7.
499         
500         scnr:   imm, id(r1)
501         args:   a1(r2), a2(r3), a3(r4), a4(r5), a5(r6), a6(r7)
502         sret:   r0(r2)
503         serr:   (sret >= (unsigned)-EMAXERRNO)
504         call:   svc
505         clob:   memory
506 */
507
508 #define __sysc_max_err  4095
509 #define __sysc_reg_cid  "r1"
510 #define __sysc_reg_res  "r2"
511 #define __sysc_cmd_sys  "svc    0"
512
513 #define __sysc_regtyp   unsigned long
514
515 #define __sysc_reg(n)   __arg_##n\
516         ("r2", "r3", "r4", "r5", "r6", "r7")
517
518 #define __sysc_clobber  __sysc_regs, "memory"
519
520
521
522 /*      *****************************************
523         SH      SH      SH      SH              *
524         sh kernel interface                     */
525
526 #elif   defined(__sh__) && !defined(__SH5__)
527
528 /*      The SuperH calling convention passes the first four arguments
529         in r4-r7, the remainder is spilled onto the stack. However
530         the Linux kernel passes the remainder in r0-r2.
531
532         scnr:   id(r3)
533         args:   a1(r4), a2(r5), a3(r6), a4(r7), a5(r0), a6(r1)
534         sret:   r0(r0)
535         serr:   (sret >= (unsigned)-EMAXERRNO)
536         call:   trapa #0x1x (x=#args)
537         clob:   memory
538 */
539
540 #define __sysc_max_err  4095
541 #define __sysc_reg_cid  "r3"
542 #define __sysc_reg_res  "r0"
543
544 #define __sysc_reg(n)   __arg_##n\
545         ("r4", "r5", "r6", "r7", "r0", "r1")
546
547 #define __sysc_cmd(n)   "trapa  #0x1" __stringify(n)
548
549 #define __rep_6(x)      x x x x x x
550 #define __sysc_cmd_fin  __rep_6("or     r0,r0\n\t")
551
552 #define __sysc_clobber  __sysc_regs, "memory"
553
554 #warning syscall arch sh not tested yet
555
556
557
558 /*      *****************************************
559         SH64    SH64    SH64    SH64            *
560         sh64 kernel interface                   */
561
562 #elif defined(__sh__) && defined(__SH5__)
563
564 /*      The SuperH-5 calling convention passes the first eight
565         arguments in r2-r9
566
567         scnr:   id(r9)
568         args:   a1(r2), a2(r3), a3(r4), a4(r5), a5(r6), a6(r7)
569         sret:   r0(r9)
570         serr:   (sret >= (unsigned)-EMAXERRNO)
571         call:   trapa #0x1x (x=#args)
572         clob:   memory
573 */
574
575 #define __sysc_max_err  4095
576 #define __sysc_reg_res  "r9"
577 #define __sysc_cmd_sys  "trapa  r9"
578
579 #define __sysc_reg(n)   __arg_##n\
580         ("r2", "r3", "r4", "r5", "r6", "r7")
581
582 #define __sc_asmsysc(n,N)       __sc_asm_vol    (                       \
583         __casm(n,0,1,   "movi %0,r9"            ,                       )\
584         __casm(n,0,1,   __sc_cmds(n,N)          ,                       )\
585         ""::"i"(__sc_id(N) | 0x1##n << 16) : __sysc_clobber)
586
587 #define __sysc_clobber  __sysc_regs, "memory"
588
589 #warning syscall arch sh64 not tested yet
590
591
592
593 /*      *****************************************
594         SPARC64 SPARC64 SPARC64 SPARC64         *
595         sparc64 kernel interface                */
596
597 #elif   defined(__sparc__)
598
599 /*      The sparc/64 calling convention uses o0-o5 to pass the first
600         six arguments (mapped via register windows).
601
602         scnr:   id(g1)
603         args:   a1(o0), a2(o1), a3(o2), a4(o3), a5(o4), a6(o5)
604         sret:   r0(o0)
605         serr:   (carry)
606         call:   ta 0x6d, t 0x10
607         clob:   g1-g6, g7?, o7?, f0-f31, cc
608 */
609
610 #define __sysc_max_err  515
611 #define __sysc_reg_cid  "g1"
612 #define __sysc_reg_ret  "o0"
613 #define __sysc_reg_err  "l1"
614
615 #define __sysc_reg(n)   __arg_##n\
616         ("o0", "o1", "o2", "o3", "o4", "o5")
617
618 #ifdef  __arch64__
619 #define __sysc_cmd_sys  "ta     0x6d"
620 #else
621 #define __sysc_cmd_sys  "t      0x10"
622 #endif
623
624 #define __sysc_cmd_fin  "addx   %%g0,%%g0,%%l1"
625
626
627 #define __sysc_clobber  __sysc_regs,                                    \
628         "g2", "g3", "g4", "g5", "g6",                                   \
629         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8",           \
630         "f9", "f10", "f11", "f12", "f13", "f14", "f15", "f16",          \
631         "f17", "f18", "f19", "f20", "f21", "f22", "f23", "f24",         \
632         "f25", "f26", "f27", "f28", "f29", "f30", "f31", "f32",         \
633         "f34", "f36", "f38", "f40", "f42", "f44", "f46", "f48",         \
634         "f50", "f52", "f54", "f56", "f58", "f60", "f62",                \
635         "cc", "memory"
636
637 #warning syscall arch sparc not tested yet
638
639
640
641 /*      *****************************************
642         V850    V850    V850    V850            *
643         v850 kernel interface                   */
644
645 #elif   defined(__v850__)
646
647 /*      The V850 calling convention passes the first four arguments
648         in registers r6-r9, the rest is spilled onto the stack.
649         but the Linux kernel interface uses r6-r9 and r13/14.
650
651         scnr:   id(r12)
652         args:   a1(r6), a2(r7), a3(r8), a4(r9), a5(r13), a6(r14)
653         sret:   r0(r10)
654         serr:   (sret >= (unsigned)-EMAXERRNO)
655         call:   trap 0, trap 1
656         clob:   r1, r5, r11, r15-r19
657 */
658
659 #define __sysc_max_err  515
660 #define __sysc_reg_cid  "r12"
661 #define __sysc_reg_res  "r10"
662
663 #define __sysc_reg(n)   __arg_##n\
664         ("r6", "r7", "r8", "r9", "r13", "r14")
665
666 #define __sysc_cmd(n)   __casm(n,4,0,"trap 1","trap 0")
667
668 #define __sysc_clobber  __sysc_regs,                                    \
669         "r1", "r5", "r11", "r15", "r16", "r17", "r18", "r19", "memory"
670
671 #warning syscall arch v850 not tested yet
672
673
674
675 /*      *****************************************
676         X86_64  X86_64  X86_64  X86_64          *
677         x86_64 kernel interface                 */
678
679 #elif   defined(__x86_64__)
680
681 /*      The x86_64 calling convention uses rdi, rsi, rdx, rcx, r8, r9
682         but the Linux kernel interface uses rdi, rsi, rdx, r10, r8, r9.
683         
684         scnr:   id(rax)
685         args:   a1(rdi), a2(rsi), a3(rdx), a4(r10), a5(r8), a6(r9)
686         sret:   r0(rax)
687         serr:   (err= sret > (unsigned)-EMAXERRNO)
688         call:   syscall
689         clob:   rcx, r11
690 */
691
692 #define __sysc_max_err  4095
693 #define __sysc_reg_cid  "rax"
694 #define __sysc_reg_res  "rax"
695 #define __sysc_cmd_sys  "syscall"
696
697 #define __sysc_reg(n)   __arg_##n\
698         ("rdi", "rsi", "rdx", "r10", "r8", "r9")
699
700 #define __sysc_clobber  __sysc_regs,                                    \
701         "cc", "r11", "rcx", "memory"
702
703 #define __sysc_aout     "=a"(__res)
704
705 #else
706 #error unknown kernel arch
707 #endif
708
709
710
711
712
713         /* argument list */
714
715 #define __lst_6(x,a1,a2,a3,a4,a5,a6)    __lst_5(x,a1,a2,a3,a4,a5),x(6,a6)
716 #define __lst_5(x,a1,a2,a3,a4,a5)       __lst_4(x,a1,a2,a3,a4),x(5,a5)
717 #define __lst_4(x,a1,a2,a3,a4)          __lst_3(x,a1,a2,a3),x(4,a4)
718 #define __lst_3(x,a1,a2,a3)             __lst_2(x,a1,a2),x(3,a3)
719 #define __lst_2(x,a1,a2)                __lst_1(x,a1),x(2,a2)
720 #define __lst_1(x,a1)                   __lst_0(x,*)x(1,a1)
721 #define __lst_0(x,a0)
722
723         /* argument concat */
724
725 #define __con_6(x,a1,a2,a3,a4,a5,a6)    __con_5(x,a1,a2,a3,a4,a5)x(6,a6)
726 #define __con_5(x,a1,a2,a3,a4,a5)       __con_4(x,a1,a2,a3,a4)x(5,a5)
727 #define __con_4(x,a1,a2,a3,a4)          __con_3(x,a1,a2,a3)x(4,a4)
728 #define __con_3(x,a1,a2,a3)             __con_2(x,a1,a2)x(3,a3)
729 #define __con_2(x,a1,a2)                __con_1(x,a1)x(2,a2)
730 #define __con_1(x,a1)                   __con_0(x,*)x(1,a1)
731 #define __con_0(x,a0)
732
733         /* argument selection */
734
735 #define __arg_0(...)
736 #define __arg_1(a1,...)                 a1
737 #define __arg_2(a1,a2,...)              a2
738 #define __arg_3(a1,a2,a3,...)           a3
739 #define __arg_4(a1,a2,a3,a4,...)        a4
740 #define __arg_5(a1,a2,a3,a4,a5,...)     a5
741 #define __arg_6(a1,a2,a3,a4,a5,a6)      a6
742
743
744
745 #ifdef  __PIC__
746 #define __pic(v)                v
747 #define __nopic(v)
748 #else
749 #define __pic(v)
750 #define __nopic(v)              v
751 #endif
752
753 #define __casm_nl(v)            v "\n\t"
754
755 #define __casm(n,a,r,v,w)       __casm_##n##a(v,w,r)
756
757 #define __pasm(n,a,r,v,w)       __pic(__casm(n,a,r,v,w))
758 #define __Pasm(n,a,r,v,w)       __nopic(__casm(n,a,r,v,w))
759
760 #define __casm_use(q,r,v)       v __casm_use_##q##r(__casm_nl(""))
761
762 #define __casm_use_10(v)
763 #define __casm_use_11(v)        v
764 #define __casm_use_12(v)
765 #define __casm_use_13(v)        v
766
767 #define __casm_use_20(v)
768 #define __casm_use_21(v)
769 #define __casm_use_22(v)        v
770 #define __casm_use_23(v)        v
771
772
773 #define __casm_00(v,w,r)        __casm_use(1,r,v)
774 #define __casm_01(v,w,r)        __casm_use(2,r,w)
775 #define __casm_02(v,w,r)        __casm_use(2,r,w)
776 #define __casm_03(v,w,r)        __casm_use(2,r,w)
777 #define __casm_04(v,w,r)        __casm_use(2,r,w)
778 #define __casm_05(v,w,r)        __casm_use(2,r,w)
779 #define __casm_06(v,w,r)        __casm_use(2,r,w)
780
781 #define __casm_10(v,w,r)        __casm_use(1,r,v)
782 #define __casm_11(v,w,r)        __casm_use(1,r,v)
783 #define __casm_12(v,w,r)        __casm_use(2,r,w)
784 #define __casm_13(v,w,r)        __casm_use(2,r,w)
785 #define __casm_14(v,w,r)        __casm_use(2,r,w)
786 #define __casm_15(v,w,r)        __casm_use(2,r,w)
787 #define __casm_16(v,w,r)        __casm_use(2,r,w)
788
789 #define __casm_20(v,w,r)        __casm_use(1,r,v)
790 #define __casm_21(v,w,r)        __casm_use(1,r,v)
791 #define __casm_22(v,w,r)        __casm_use(1,r,v)
792 #define __casm_23(v,w,r)        __casm_use(2,r,w)
793 #define __casm_24(v,w,r)        __casm_use(2,r,w)
794 #define __casm_25(v,w,r)        __casm_use(2,r,w)
795 #define __casm_26(v,w,r)        __casm_use(2,r,w)
796
797 #define __casm_30(v,w,r)        __casm_use(1,r,v)
798 #define __casm_31(v,w,r)        __casm_use(1,r,v)
799 #define __casm_32(v,w,r)        __casm_use(1,r,v)
800 #define __casm_33(v,w,r)        __casm_use(1,r,v)
801 #define __casm_34(v,w,r)        __casm_use(2,r,w)
802 #define __casm_35(v,w,r)        __casm_use(2,r,w)
803 #define __casm_36(v,w,r)        __casm_use(2,r,w)
804
805 #define __casm_40(v,w,r)        __casm_use(1,r,v)
806 #define __casm_41(v,w,r)        __casm_use(1,r,v)
807 #define __casm_42(v,w,r)        __casm_use(1,r,v)
808 #define __casm_43(v,w,r)        __casm_use(1,r,v)
809 #define __casm_44(v,w,r)        __casm_use(1,r,v)
810 #define __casm_45(v,w,r)        __casm_use(2,r,w)
811 #define __casm_46(v,w,r)        __casm_use(2,r,w)
812
813 #define __casm_50(v,w,r)        __casm_use(1,r,v)
814 #define __casm_51(v,w,r)        __casm_use(1,r,v)
815 #define __casm_52(v,w,r)        __casm_use(1,r,v)
816 #define __casm_53(v,w,r)        __casm_use(1,r,v)
817 #define __casm_54(v,w,r)        __casm_use(1,r,v)
818 #define __casm_55(v,w,r)        __casm_use(1,r,v)
819 #define __casm_56(v,w,r)        __casm_use(2,r,w)
820
821 #define __casm_60(v,w,r)        __casm_use(1,r,v)
822 #define __casm_61(v,w,r)        __casm_use(1,r,v)
823 #define __casm_62(v,w,r)        __casm_use(1,r,v)
824 #define __casm_63(v,w,r)        __casm_use(1,r,v)
825 #define __casm_64(v,w,r)        __casm_use(1,r,v)
826 #define __casm_65(v,w,r)        __casm_use(1,r,v)
827 #define __casm_66(v,w,r)        __casm_use(1,r,v)
828
829 #define __casm_cn_0
830 #define __casm_cn_1             ,
831 #define __casm_cn_2             ,
832 #define __casm_cn_3             ,
833 #define __casm_cn_4             ,
834 #define __casm_cn_5             ,
835 #define __casm_cn_6             ,
836
837
838
839 #define __sc_asm                __asm__
840 #define __sc_asm_vol            __asm__ __volatile__
841
842 #ifndef __sysc_setret
843 #define __sysc_setret(v)        do { } while(0)
844 #endif
845
846 #ifndef __sysc_seterr
847 #define __sysc_seterr(e)        do { errno = (e); } while(0)
848 #endif
849
850 #ifndef __stringify0
851 #define __stringify0(val)       #val
852 #endif
853
854 #ifndef __stringify
855 #define __stringify(val)        __stringify0(val)
856 #endif
857
858
859 #if     !defined(__sysc_load) && !defined(__sysc_save)
860 #if     !defined(__sysc_limm) && !defined(__sc_asmload)
861 #define __sc_trust
862 #endif
863 #endif
864
865 #if     defined(__sysc_reg_ret) && defined(__sysc_reg_err)
866 #define __sc_complex
867 #endif
868
869
870 #ifndef __sysc_type
871 #define __sysc_type             long
872 #endif
873
874 #define __sc_cast(v)            (__sysc_type)(v)
875
876
877 #define __sc_reg(n)             register __sysc_type n
878 #define __sc_asm_reg(n,r)       register __sysc_type n __sc_asm (r)
879 #define __sc_asm_val(n,r,v)     __sc_asm_reg(n,r) = __sc_cast(v)
880
881 #ifndef __sysc_load
882 #define __sc_inp_def(n,v)       __sc_asm_val(__sc_a##n, __sysc_reg(n), v);
883 #else
884 #define __sc_inp_def(n,value)
885 #endif
886
887 #if     !defined(__sysc_save) && !defined(__sysc_aout)
888 #define __sc_res_def(n,r)       __sc_asm_reg(n, r);
889 #else
890 #define __sc_res_def(n,r)       __sc_reg(n);
891 #endif
892
893
894 #define __sc_rreg(n,v)          "r"(__sc_a##n)
895 #define __sc_creg(n,v)          __sysc_rcon(n)(__sc_cast(v))
896
897 #ifdef  __sc_trust
898 #define __sc_iregs(n,...)       __lst_##n(__sc_rreg,__VA_ARGS__)
899 #define __sc_input(n,...)       __con_##n(__sc_inp_def,__VA_ARGS__)
900 #else
901 #define __sc_iregs(n,...)       __lst_##n(__sc_creg,__VA_ARGS__)
902 #define __sc_input(n,...)
903 #endif
904
905
906
907 #define __sc_list(x)            x(1), x(2), x(3), x(4), x(5), x(6)
908
909 #ifndef __sysc_regs
910 #define __sysc_regs             __sc_list(__sysc_reg)
911 #endif
912
913 #ifndef __sysc_rcon
914 #define __sysc_rcon(n)          "g"
915 #endif
916
917
918 #ifdef  __sc_complex    /* complex result */
919
920 #ifndef __sysc_errc
921 #define __sysc_errc(ret, err) (err)
922 #endif
923
924 #ifndef __sysc_retv
925 #define __sysc_retv(type, ret, err)                                     \
926         __sysc_setret(ret);                                             \
927         if (__sysc_errc(ret, err)) {                                    \
928                 int __err = (ret);                                      \
929                 __sysc_seterr(__err);                                   \
930                 ret = -1;                                               \
931         }                                                               \
932         return (type)(ret)
933 #endif
934
935 #define __sc_results                                                    \
936         __sc_res_def(__err, __sysc_reg_err)                             \
937         __sc_res_def(__ret, __sysc_reg_ret)
938
939 #define __sc_oregs      "=r"(__ret), "=r"(__err)
940
941 #if     defined(__sc_trust) || !defined(__sysc_save)
942 #define __sc_saveres    __sc_dummy_save(1)
943 #else
944 #define __sc_saveres                                                    \
945         __casm_nl(__sysc_save(__sysc_reg_ret,"%0"))                     \
946         __casm_nl(__sysc_save(__sysc_reg_err,"%1"))
947 #endif
948
949 #define __sc_return(t)  __sysc_retv(t, __ret, __err)
950
951 #else                   /* simple result  */
952
953 #ifndef __sysc_errc
954 #define __sysc_errc(res)                                                \
955         ((unsigned __sysc_type)(res) >=                                 \
956                 (unsigned __sysc_type)(-(__sysc_max_err)))
957 #endif
958
959 #ifndef __sysc_retv
960 #define __sysc_retv(type, res)                                          \
961         __sysc_setret(res);                                             \
962         if (__sysc_errc(res)) {                                         \
963                 int __err = -(res);                                     \
964                 __sysc_seterr(__err);                                   \
965                 res = -1;                                               \
966         }                                                               \
967         return (type)(res)
968 #endif
969
970
971 #define __sc_results                                                    \
972         __sc_res_def(__res, __sysc_reg_res)
973
974 #define __sc_oregs      "=r"(__res)
975
976 #if     defined(__sc_trust) || !defined(__sysc_save)
977 #define __sc_saveres    __sc_dummy_save(0)
978 #else
979 #define __sc_saveres    __casm_nl(__sysc_save(__sysc_reg_res,"%0"))
980 #endif
981
982 #define __sc_return(t)  __sysc_retv(t, __res)
983
984 #endif                  /* simple/complex */
985
986
987 #define __sc_dummy_load(n)      "/* gcc dummy load "                    \
988         __casm(n,0,0,"%0 ",) __casm(n,1,0,"%1 ",) __casm(n,2,0,"%2 ",)  \
989         __casm(n,3,0,"%3 ",) __casm(n,4,0,"%4 ",) __casm(n,5,0,"%5 ",)  \
990         __casm(n,6,0,"%6 ",) "*/"
991
992 #ifdef  __sysc_aout
993 #define __sc_dummy_save(n)
994 #define __sc_asmsave(n)
995 #else
996 #define __sc_dummy_save(n)      "/* gcc dummy save "                    \
997         __casm(n,0,0,"%0 ",) __casm(n,1,0,"%1 ",) "*/"
998 #endif
999
1000 #define __comment(name)         "\t/* kernel sys_"                      \
1001         #name "[" __stringify(__sc_id(name)) "] */"
1002
1003
1004 #define __sc_id(N)              __NR_##N
1005
1006 #ifndef __sysc_reg_cid
1007 #define __sc_cid(N)             "i"(__sc_id(N))
1008 #define __sc_load_cid           ""
1009 #define __sc_callid(N)
1010 #else
1011 #define __sc_cid(N)             "r"(__cid)
1012 #define __sc_load_cid           __sysc_limm(__sysc_reg_cid,"%0")
1013 #define __sc_callid(N)                                                  \
1014         __sc_asm_val(__cid, __sysc_reg_cid, __sc_id(N));
1015 #endif
1016
1017 #ifndef __sysc_cmd_pre
1018 #define __sc_cmd_pre            ""
1019 #else
1020 #define __sc_cmd_pre            __casm_nl(__sysc_cmd_pre)
1021 #endif
1022
1023 #ifndef __sysc_cmd_fin
1024 #define __sc_cmd_fin            ""
1025 #else
1026 #define __sc_cmd_fin            __sysc_cmd_fin
1027 #endif
1028
1029 #ifndef __sysc_pre
1030 #define __sysc_pre(n)           __sc_cmd_pre
1031 #endif
1032
1033 #ifndef __sysc_cmd
1034 #define __sysc_cmd(n)           __sysc_cmd_sys
1035 #endif
1036
1037 #ifndef __sysc_fin
1038 #define __sysc_fin(n)           __sc_cmd_fin
1039 #endif
1040
1041 #define __sc_cmds(n,name)                                               \
1042         __sysc_pre(n)                                                   \
1043         __casm_nl(__sysc_cmd(n) __comment(name))                        \
1044         __sysc_fin(n)
1045
1046 #ifndef __sc_asmload
1047 #ifdef  __sc_trust
1048 #define __sc_asmload(n,N,...)   __sc_asm(                               \
1049         __sc_dummy_load(n)                                              \
1050         ::__sc_cid(N) __casm_cn_##n __sc_iregs(n,__VA_ARGS__))
1051 #else
1052 #define __sc_asmload(n,N,...)   __sc_asm(                               \
1053         __casm(n,1,1,   __sysc_load(__sysc_reg(1),"%1"),                )\
1054         __casm(n,2,1,   __sysc_load(__sysc_reg(2),"%2"),                )\
1055         __casm(n,3,1,   __sysc_load(__sysc_reg(3),"%3"),                )\
1056         __casm(n,4,1,   __sysc_load(__sysc_reg(4),"%4"),                )\
1057         __casm(n,5,1,   __sysc_load(__sysc_reg(5),"%5"),                )\
1058         __casm(n,6,1,   __sysc_load(__sysc_reg(6),"%6"),                )\
1059         __sc_load_cid   ::__sc_cid(N) __casm_cn_##n                     \
1060         __sc_iregs(n,__VA_ARGS__):__sysc_regs)
1061 #endif
1062 #endif
1063
1064 #ifndef __sysc_aout
1065 #define __sysc_aout
1066 #endif
1067
1068 #ifndef __sc_asmsysc
1069 #define __sc_asmsysc(n,N)       __sc_asm_vol(                           \
1070         __casm(n,0,0,   __sc_cmds(n,N)          ,                       )\
1071         :__sysc_aout:"i"(__sc_id(N)) : __sysc_clobber)
1072 #endif
1073
1074 #ifndef __sc_asmsave
1075 #define __sc_asmsave(n)         __sc_asm(                               \
1076         __sc_saveres            :__sc_oregs)
1077 #endif
1078
1079
1080
1081
1082 #define __sc_body(n, type, name, ...)                                   \
1083 {                                                                       \
1084         __sc_results __sc_callid(name) __sc_input(n, __VA_ARGS__)       \
1085         __sc_asmload(n, name, __VA_ARGS__);                             \
1086         __sc_asmsysc(n, name);                                          \
1087         __sc_asmsave(n);                                                \
1088         __sc_return(type);                                              \
1089 }
1090
1091
1092
1093 #define _syscall0(type, name)                                           \
1094 type name(void)                                                         \
1095 __sc_body(0, type, name, *)
1096
1097 #define _syscall1(type, name, type1, arg1)                              \
1098 type name(type1 arg1)                                                   \
1099 __sc_body(1, type, name, arg1)
1100
1101 #define _syscall2(type, name, type1, arg1, type2, arg2)                 \
1102 type name(type1 arg1, type2 arg2)                                       \
1103 __sc_body(2, type, name, arg1, arg2)
1104
1105 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3)    \
1106 type name(type1 arg1, type2 arg2, type3 arg3)                           \
1107 __sc_body(3, type, name, arg1, arg2, arg3)
1108
1109 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3,    \
1110                               type4, arg4)                              \
1111 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)               \
1112 __sc_body(4, type, name, arg1, arg2, arg3, arg4)
1113
1114 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3,    \
1115                               type4, arg4, type5, arg5)                 \
1116 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)   \
1117 __sc_body(5, type, name, arg1, arg2, arg3, arg4, arg5)
1118
1119 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3,    \
1120                         type4, arg4, type5, arg5, type6, arg6)          \
1121 type name(type1 arg1, type2 arg2, type3 arg3,                           \
1122           type4 arg4, type5 arg5, type6 arg6)                           \
1123 __sc_body(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6)
1124
1125
1126 #endif  /* __SYSCALL_NEW_H */