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