annotate src/syscall.c @ 24:36bd61f5c847

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