Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/arch/mips/syscall/syscall.c
2096 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
#include <types.h>
31
#include <kern/errno.h>
32
#include <kern/syscall.h>
33
#include <lib.h>
34
#include <mips/trapframe.h>
35
#include <copyinout.h>
36
#include <thread.h>
37
#include <current.h>
38
#include <syscall.h>
39
40
#define MAKE_64BIT(x,y) (((int64_t)x) << 32 | y)
41
#define GET_LO(x) ((int32_t) x & 0x00000000FFFFFFFF)
42
#define GET_HI(x) ((int32_t) x & 0xFFFFFFFF00000000)
43
44
/*
45
* System call dispatcher.
46
*
47
* A pointer to the trapframe created during exception entry (in
48
* exception.S) is passed in.
49
*
50
* The calling conventions for syscalls are as follows: Like ordinary
51
* function calls, the first 4 32-bit arguments are passed in the 4
52
* argument registers a0-a3. 64-bit arguments are passed in *aligned*
53
* pairs of registers, that is, either a0/a1 or a2/a3. This means that
54
* if the first argument is 32-bit and the second is 64-bit, a1 is
55
* unused.
56
*
57
* This much is the same as the calling conventions for ordinary
58
* function calls. In addition, the system call number is passed in
59
* the v0 register.
60
*
61
* On successful return, the return value is passed back in the v0
62
* register, or v0 and v1 if 64-bit. This is also like an ordinary
63
* function call, and additionally the a3 register is also set to 0 to
64
* indicate success.
65
*
66
* On an error return, the error code is passed back in the v0
67
* register, and the a3 register is set to 1 to indicate failure.
68
* (Userlevel code takes care of storing the error code in errno and
69
* returning the value -1 from the actual userlevel syscall function.
70
* See src/user/lib/libc/arch/mips/syscalls-mips.S and related files.)
71
*
72
* Upon syscall return the program counter stored in the trapframe
73
* must be incremented by one instruction; otherwise the exception
74
* return code will restart the "syscall" instruction and the system
75
* call will repeat forever.
76
*
77
* If you run out of registers (which happens quickly with 64-bit
78
* values) further arguments must be fetched from the user-level
79
* stack, starting at sp+16 to skip over the slots for the
80
* registerized values, with copyin().
81
*/
82
void
83
syscall(struct trapframe *tf)
84
{
85
int callno;
86
int32_t retval;
87
int64_t retval64;
88
int err;
89
int nextra;
90
bool handle64;
91
92
KASSERT(curthread != NULL);
93
KASSERT(curthread->t_curspl == 0);
94
KASSERT(curthread->t_iplhigh_count == 0);
95
96
callno = tf->tf_v0;
97
98
/*
99
* Initialize retval to 0. Many of the system calls don't
100
* really return a value, just 0 for success and -1 on
101
* error. Since retval is the value returned on success,
102
* initialize it to 0 by default; thus it's not necessary to
103
* deal with it except for calls that return other values,
104
* like write.
105
*/
106
107
retval = 0;
108
handle64 = false;
109
110
//award points for the process that just called the systemcall.
111
curthread->td_proc->p_nsyscalls++;
112
113
switch (callno) {
114
case SYS_reboot:
115
err = sys_reboot(tf->tf_a0);
116
break;
117
118
case SYS___time:
119
err = sys___time((userptr_t)tf->tf_a0,
120
(userptr_t)tf->tf_a1);
121
break;
122
123
case SYS_open:
124
err = sys_open( (userptr_t) tf->tf_a0,
125
tf->tf_a1, &retval);
126
break;
127
128
case SYS_close:
129
err = sys_close( tf->tf_a0 );
130
break;
131
132
case SYS_write:
133
err = sys_write( tf->tf_a0,
134
(userptr_t) tf->tf_a1,
135
tf->tf_a2, &retval );
136
break;
137
138
case SYS_read:
139
err = sys_read( tf->tf_a0,
140
(userptr_t) tf->tf_a1,
141
tf->tf_a2, &retval);
142
break;
143
144
case SYS___getcwd:
145
err = sys___getcwd( (userptr_t)tf->tf_a0,
146
tf->tf_a1, &retval );
147
break;
148
149
case SYS_lseek:
150
//get the value of whence from sp+16
151
err = copyin( (userptr_t)(tf->tf_sp + 16),
152
&nextra, sizeof( int ) );
153
if( err )
154
break;
155
156
err = sys_lseek(
157
tf->tf_a0,
158
MAKE_64BIT( tf->tf_a2, tf->tf_a3 ),
159
nextra,
160
&retval64
161
);
162
163
//if no errors occurred, we must handle
164
//a 64-bit return value.
165
handle64 = true;
166
break;
167
168
case SYS_dup2:
169
err = sys_dup2( tf->tf_a0, tf->tf_a1, &retval );
170
break;
171
172
case SYS_chdir:
173
err = sys_chdir( (userptr_t)tf->tf_a0 );
174
break;
175
176
case SYS_getpid:
177
err = sys_getpid( &retval );
178
break;
179
case SYS__exit:
180
sys__exit( tf->tf_a0 );
181
err = 0;
182
break;
183
184
case SYS_waitpid:
185
err = sys_waitpid( tf->tf_a0, (userptr_t)tf->tf_a1, tf->tf_a2, &retval );
186
break;
187
188
case SYS_fork:
189
err = sys_fork( tf, &retval );
190
break;
191
192
case SYS_execv:
193
err = sys_execv( (userptr_t) tf->tf_a0, (userptr_t) tf->tf_a1 );
194
break;
195
196
case SYS_sbrk:
197
err = sys_sbrk( (intptr_t) tf->tf_a0, (void*)&retval );
198
break;
199
default:
200
kprintf("Unknown syscall %d\n", callno);
201
err = ENOSYS;
202
break;
203
}
204
205
206
if (err) {
207
/*
208
* Return the error code. This gets converted at
209
* userlevel to a return value of -1 and the error
210
* code in errno.
211
*/
212
tf->tf_v0 = err;
213
tf->tf_a3 = 1; /* signal an error */
214
}
215
else if( handle64 ) {
216
tf->tf_a3 = 0;
217
tf->tf_v0 = GET_HI( retval64 );
218
tf->tf_v1 = GET_LO( retval64 );
219
}
220
else {
221
/* Success. */
222
tf->tf_v0 = retval;
223
tf->tf_a3 = 0; /* signal no error */
224
}
225
226
/*
227
* Now, advance the program counter, to avoid restarting
228
* the syscall over and over again.
229
*/
230
231
tf->tf_epc += 4;
232
233
/* Make sure the syscall code didn't forget to lower spl */
234
KASSERT(curthread->t_curspl == 0);
235
/* ...or leak any spinlocks */
236
KASSERT(curthread->t_iplhigh_count == 0);
237
}
238
239
/*
240
* Enter user mode for a newly forked process.
241
*
242
* This function is provided as a reminder. You need to write
243
* both it and the code that calls it.
244
*
245
* Thus, you can trash it and do things another way if you prefer.
246
*/
247
void
248
enter_forked_process(struct trapframe *tf)
249
{
250
(void)tf;
251
}
252
253