nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
syscall.c
Go to the documentation of this file.
1 #include <sys/syscall.h>
2 #include <stddef.h>
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <errno.h>
7 
8 static inline int syscall0(int fn)
9 {
10  int ret;
11 
12  asm volatile (
13  "movl %1, %%eax\n"
14  "movl $0, %%ecx\n"
15  "int $0x31\n"
16  "movl %%eax, %0\n"
17  : "=m"(ret)
18  : "m"(fn)
19  : "memory", "%eax", "%ecx"
20  );
21 
22  return ret;
23 }
24 
25 static inline int syscall1(int fn, int arg)
26 {
27  int ret;
28 
29  asm volatile (
30  "pushl %1\n"
31  "movl %2, %%eax\n"
32  "movl $1, %%ecx\n"
33  "int $0x31\n"
34  "addl $4, %%esp\n"
35  "movl %%eax, %0\n"
36  : "=m"(ret)
37  : "m"(arg), "m"(fn)
38  : "memory", "%eax", "%ecx", "%esp"
39  );
40 
41  return ret;
42 }
43 
44 static inline int syscall2(int fn, int arg[2])
45 {
46  int ret;
47 
48  asm volatile (
49  "pushl %1\n"
50  "pushl %2\n"
51  "movl %3, %%eax\n"
52  "movl $2, %%ecx\n"
53  "int $0x31\n"
54  "addl $8, %%esp\n"
55  "movl %%eax, %0\n"
56  : "=m"(ret)
57  : "m"(arg[0]), "m"(arg[1])
58  : "memory", "%eax", "%ecx", "%esp"
59  );
60 
61  return ret;
62 }
63 
64 static inline int syscall3(int fn, int arg[3])
65 {
66  int ret;
67 
68  asm volatile (
69  "pushl %1\n"
70  "pushl %2\n"
71  "pushl %3\n"
72  "movl %4, %%eax\n"
73  "movl $3, %%ecx\n"
74  "int $0x31\n"
75  "addl $12, %%esp\n"
76  "movl %%eax, %0\n"
77  : "=m"(ret)
78  : "m"(arg[0]), "m"(arg[1]), "m"(arg[2]), "m"(fn)
79  : "memory", "%eax", "%ecx", "%esp"
80  );
81 
82  return ret;
83 }
84 
85 int syscall(int fn, ...)
86 {
87  va_list ap;
88  int arg[3];
89  int ret;
90 
91  switch (fn) {
92  case SYS_NR_OPEN: /* 3 */
93  case SYS_NR_READ: /* 3 */
94  case SYS_NR_WRITE: /* 3 */
95  case SYS_NR_SEEK: /* 3 */
96  va_start(ap, fn);
97  arg[0] = (int)va_arg(ap, int);
98  arg[1] = (int)va_arg(ap, int);
99  arg[2] = (int)va_arg(ap, int);
100  ret = syscall3(fn, arg);
101  va_end(ap);
102  break;
103  case SYS_NR_CLOSE: /* 1 */
104  case SYS_NR_OPENDIR: /* 1 */
105  case SYS_NR_CLOSEDIR: /* 1 */
106  case SYS_NR_READDIR: /* 1 */
107  va_start(ap, fn);
108  arg[0] = (int)va_arg(ap, int);
109  ret = syscall1(fn, arg[0]);
110  va_end(ap);
111  break;
112  case SYS_NR_YIELD: /* 0 */
113  ret = syscall0(fn);
114  break;
115  default:
116  ret = -ENOSYS;
117  break;
118  }
119 
120  return ret;
121 }
122 
123 /* End of a file */