comparison src/syscall.cbc @ 52:214d21c891c7

rename to cbc __ncode is not handled as interfaces
author kono
date Mon, 03 Jun 2019 18:12:44 +0900
parents src/syscall.c@a7144583914c
children b5ddf6fb0a6d
comparison
equal deleted inserted replaced
51:fadfd62d6b14 52:214d21c891c7
1 #include "types.h"
2 #include "defs.h"
3 #include "param.h"
4 #include "memlayout.h"
5 #include "mmu.h"
6 #include "proc.h"
7 #include "arm.h"
8 #include "syscall.h"
9
10 #define __ncode __code
11 #
12 // User code makes a system call with INT T_SYSCALL. System call number
13 // in r0. Arguments on the stack, from the user call to the C library
14 // system call function. The saved user sp points to the first argument.
15
16 // Fetch the int at addr from the current process.
17 int fetchint(uint addr, int *ip)
18 {
19 if(addr >= proc->sz || addr+4 > proc->sz) {
20 return -1;
21 }
22
23 *ip = *(int*)(addr);
24 return 0;
25 }
26
27 // Fetch the nul-terminated string at addr from the current process.
28 // Doesn't actually copy the string - just sets *pp to point at it.
29 // Returns length of string, not including nul.
30 int fetchstr(uint addr, char **pp)
31 {
32 char *s, *ep;
33
34 if(addr >= proc->sz) {
35 return -1;
36 }
37
38 *pp = (char*)addr;
39 ep = (char*)proc->sz;
40
41 for(s = *pp; s < ep; s++) {
42 if(*s == 0) {
43 return s - *pp;
44 }
45 }
46
47 return -1;
48 }
49
50 // Fetch the nth (starting from 0) 32-bit system call argument.
51 // In our ABI, r0 contains system call index, r1-r4 contain parameters.
52 // now we support system calls with at most 4 parameters.
53 int argint(int n, int *ip)
54 {
55 if (n > 3) {
56 panic ("too many system call parameters\n");
57 }
58
59 *ip = *(&proc->tf->r1 + n);
60
61 return 0;
62 }
63
64 // Fetch the nth word-sized system call argument as a pointer
65 // to a block of memory of size n bytes. Check that the pointer
66 // lies within the process address space.
67 int argptr(int n, char **pp, int size)
68 {
69 int i;
70
71 if(argint(n, &i) < 0) {
72 return -1;
73 }
74
75 if((uint)i >= proc->sz || (uint)i+size > proc->sz) {
76 return -1;
77 }
78
79 *pp = (char*)i;
80 return 0;
81 }
82
83 // Fetch the nth word-sized system call argument as a string pointer.
84 // Check that the pointer is valid and the string is nul-terminated.
85 // (There is no shared writable memory, so the string can't change
86 // between this check and being used by the kernel.)
87 int argstr(int n, char **pp)
88 {
89 int addr;
90
91 if(argint(n, &addr) < 0) {
92 return -1;
93 }
94
95 return fetchstr(addr, pp);
96 }
97
98 extern int sys_chdir(void);
99 extern int sys_close(void);
100 extern int sys_dup(void);
101 extern int sys_exec(void);
102 extern int sys_exit(void);
103 extern int sys_fork(void);
104 extern int sys_fstat(void);
105 extern int sys_getpid(void);
106 extern int sys_kill(void);
107 extern int sys_link(void);
108 extern int sys_mkdir(void);
109 extern int sys_mknod(void);
110 extern int sys_open(void);
111 extern int sys_pipe(void);
112 extern int sys_read(void);
113 extern int sys_sbrk(void);
114 extern int sys_sleep(void);
115 extern int sys_unlink(void);
116 extern int sys_wait(void);
117 extern int sys_write(void);
118 extern int sys_uptime(void);
119
120 extern __code cbc_read(__code(*)(int));
121
122 static int (*syscalls[])(void) = {
123 [SYS_fork] =sys_fork,
124 [SYS_exit] =sys_exit,
125 [SYS_wait] =sys_wait,
126 [SYS_pipe] =sys_pipe,
127 [SYS_read] =sys_read,
128 [SYS_kill] =sys_kill,
129 [SYS_exec] =sys_exec,
130 [SYS_fstat] =sys_fstat,
131 [SYS_chdir] =sys_chdir,
132 [SYS_dup] =sys_dup,
133 [SYS_getpid] =sys_getpid,
134 [SYS_sbrk] =sys_sbrk,
135 [SYS_sleep] =sys_sleep,
136 [SYS_uptime] =sys_uptime,
137 [SYS_open] =sys_open,
138 [SYS_write] =sys_write,
139 [SYS_mknod] =sys_mknod,
140 [SYS_unlink] =sys_unlink,
141 [SYS_link] =sys_link,
142 [SYS_mkdir] =sys_mkdir,
143 [SYS_close] =sys_close,
144 };
145
146 static __code (*cbccodes[])(__code (*)(int)) = {
147 [SYS_cbc_read] = cbc_read,
148 };
149
150 __ncode cbc_trap_return(){
151 return;
152 }
153
154 __ncode cbc_ret(int ret){
155 int num = proc->cbc_arg.cbc_console_arg.num;
156 if (num != SYS_exec) {
157 proc->tf->r0 = ret;
158 }
159 goto cbc_trap_return();
160 }
161
162 void syscall(void)
163 {
164 int num;
165 int ret;
166
167 num = proc->tf->r0;
168
169 if (num == 5)
170 num = 22;
171 //cprintf ("syscall(%d) from %s(%d)\n", num, proc->name, proc->pid);
172
173 if((num >= NELEM(syscalls)) && (num <= NELEM(cbccodes)) && cbccodes[num]) {
174 proc->cbc_arg.cbc_console_arg.num = num;
175 goto (cbccodes[num])(cbc_ret);
176 }
177
178
179 if((num > 0) && (num < NELEM(syscalls)) && syscalls[num]) {
180 ret = syscalls[num]();
181
182 // in ARM, parameters to main (argc, argv) are passed in r0 and r1
183 // do not set the return value if it is SYS_exec (the user program
184 // anyway does not expect us to return anything).
185 if (num != SYS_exec) {
186 proc->tf->r0 = ret;
187 }
188 } else {
189 cprintf("%d %s: unknown sys call %d\n", proc->pid, proc->name, num);
190 proc->tf->r0 = -1;
191 }
192 }