The Pedigree Project  0.1
x86/syscall-stubs.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef SERVICE
21 #error syscall-stubs.h requires SERVICE to be defined
22 #endif
23 #ifndef SERVICE_ERROR
24 #error syscall-stubs.h requires SERVICE_ERROR to be defined
25 #endif
26 #ifndef SERVICE_INIT
27 #error syscall-stubs.h requires SERVICE_INIT to be defined
28 #endif
29 
30 // If USE_PIC_SYSCALLS is defined, the EBX register will be preserved, as GCC
31 // refuses to accept it as a clobbered register in inline assembly with -fPIC.
32 
33 static int syscall0(int function)
34 {
35  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
36  int ret;
37  SERVICE_INIT;
38 #ifdef USE_PIC_SYSCALLS
39  asm volatile("push %%ebx; \
40  mov %1, %%ebx; \
41  int $255; \
42  mov %%ebx, %1; \
43  pop %%ebx"
44  : "=a"(ret), "=r"(SERVICE_ERROR)
45  : "0"(eax));
46 #else
47  asm volatile("int $255" : "=a"(ret), "=b"(SERVICE_ERROR) : "0"(eax));
48 #endif
49  return ret;
50 }
51 
52 static int syscall1(int function, int p1)
53 {
54  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
55  int ret;
56  SERVICE_INIT;
57 #ifdef USE_PIC_SYSCALLS
58  asm volatile("push %%ebx; \
59  mov %1, %%ebx; \
60  int $255; \
61  mov %%ebx, %1; \
62  pop %%ebx"
63  : "=a"(ret), "=r"(SERVICE_ERROR)
64  : "0"(eax), "1"(p1));
65 #else
66  asm volatile("int $255"
67  : "=a"(ret), "=b"(SERVICE_ERROR)
68  : "0"(eax), "1"(p1));
69 #endif
70  return ret;
71 }
72 
73 static int syscall2(int function, int p1, int p2)
74 {
75  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
76  int ret;
77  SERVICE_INIT;
78 #ifdef USE_PIC_SYSCALLS
79  asm volatile("push %%ebx; \
80  mov %1, %%ebx; \
81  int $255; \
82  mov %%ebx, %1; \
83  pop %%ebx"
84  : "=a"(ret), "=r"(SERVICE_ERROR)
85  : "0"(eax), "1"(p1), "c"(p2));
86 #else
87  asm volatile("int $255"
88  : "=a"(ret), "=b"(SERVICE_ERROR)
89  : "0"(eax), "1"(p1), "c"(p2));
90 #endif
91  return ret;
92 }
93 
94 static int syscall3(int function, int p1, int p2, int p3)
95 {
96  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
97  int ret;
98  SERVICE_INIT;
99 #ifdef USE_PIC_SYSCALLS
100  asm volatile("push %%ebx; \
101  mov %1, %%ebx; \
102  int $255; \
103  mov %%ebx, %1; \
104  pop %%ebx"
105  : "=a"(ret), "=r"(SERVICE_ERROR)
106  : "0"(eax), "1"(p1), "c"(p2), "d"(p3));
107 #else
108  asm volatile("int $255"
109  : "=a"(ret), "=b"(SERVICE_ERROR)
110  : "0"(eax), "1"(p1), "c"(p2), "d"(p3));
111 #endif
112  return ret;
113 }
114 
115 static int syscall4(int function, int p1, int p2, int p3, int p4)
116 {
117  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
118  int ret;
119  SERVICE_INIT;
120 #ifdef USE_PIC_SYSCALLS
121  asm volatile("push %%ebx; \
122  mov %1, %%ebx; \
123  int $255; \
124  mov %%ebx, %1; \
125  pop %%ebx"
126  : "=a"(ret), "=r"(SERVICE_ERROR)
127  : "0"(eax), "1"(p1), "c"(p2), "d"(p3), "S"(p4));
128 #else
129  asm volatile("int $255"
130  : "=a"(ret), "=b"(SERVICE_ERROR)
131  : "0"(eax), "1"(p1), "c"(p2), "d"(p3), "S"(p4));
132 #endif
133  return ret;
134 }
135 
136 static int syscall5(int function, int p1, int p2, int p3, int p4, int p5)
137 {
138  int eax = ((SERVICE & 0xFFFF) << 16) | (function & 0xFFFF);
139  int ret;
140  SERVICE_INIT;
141 #ifdef USE_PIC_SYSCALLS
142  asm volatile("push %%ebx; \
143  mov %1, %%ebx; \
144  int $255; \
145  mov %%ebx, %1; \
146  pop %%ebx"
147  : "=a"(ret), "+m"(p1)
148  : "0"(eax), "c"(p2), "d"(p3), "S"(p4), "D"(p5));
149  SERVICE_ERROR = p1;
150 #else
151  asm volatile("int $255"
152  : "=a"(ret), "=b"(SERVICE_ERROR)
153  : "0"(eax), "1"(p1), "c"(p2), "d"(p3), "S"(p4), "D"(p5));
154 #endif
155  return ret;
156 }