-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibuser.c
More file actions
253 lines (221 loc) · 5.58 KB
/
libuser.c
File metadata and controls
253 lines (221 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* File: libuser.c
*
* Description: This file contains the interface declarations
* to the OS kernel support package.
*/
#include <phase1.h>
#include <phase2.h>
#include <libuser.h>
#include <usyscall.h>
#include <usloss.h>
#define CHECKMODE { \
if (USLOSS_PsrGet() & USLOSS_PSR_CURRENT_MODE) { \
USLOSS_Console("Trying to invoke syscall from kernel\n"); \
USLOSS_Halt(1); \
} \
}
/*
* Routine: Spawn
*
* Description: This is the call entry to fork a new user process.
*
* Arguments: char *name -- new process's name
* PFV func -- pointer to the function to fork
* void *arg -- argument to function
* int stacksize -- amount of stack to be allocated
* int priority -- priority of forked process
* int *pid -- pointer to output value
* (output value: process id of the forked process)
*
* Return Value: 0 means success, -1 means error occurs
*/
int Spawn(char *name, int (*func)(char *), char *arg, int stack_size,
int priority, int *pid)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_SPAWN;
sysArg.arg1 = (void *) func;
sysArg.arg2 = arg;
sysArg.arg3 = (void *) ( (long) stack_size);
sysArg.arg4 = (void *) ( (long) priority);
sysArg.arg5 = name;
USLOSS_Syscall(&sysArg);
*pid = (int) sysArg.arg1;
return (int) sysArg.arg4;
} /* end of Spawn */
/*
* Routine: Wait
*
* Description: This is the call entry to wait for a child completion
*
* Arguments: int *pid -- pointer to output value 1
* (output value 1: process id of the completing child)
* int *status -- pointer to output value 2
* (output value 2: status of the completing child)
*
* Return Value: 0 means success, -1 means error occurs
*/
int Wait(int *pid, int *status)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_WAIT;
USLOSS_Syscall(&sysArg);
*pid = (int) sysArg.arg1;
*status = (int) sysArg.arg2;
return (int) sysArg.arg4;
} /* End of Wait */
/*
* Routine: Terminate
*
* Description: This is the call entry to terminate
* the invoking process and its children
*
* Arguments: int status -- the commpletion status of the process
*
* Return Value: 0 means success, -1 means error occurs
*/
void Terminate(int status)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_TERMINATE;
sysArg.arg1 = (void *) ( (long) status);
USLOSS_Syscall(&sysArg);
return;
} /* End of Terminate */
/*
* Routine: SemCreate
*
* Description: Create a semaphore.
*
* Arguments: int value -- initial semaphore value
* int *semaphore -- semaphore handle
* (output value: completion status)
*/
int SemCreate(int value, int *semaphore)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_SEMCREATE;
sysArg.arg1 = (void *) ( (long) value);
USLOSS_Syscall(&sysArg);
*semaphore = (int) sysArg.arg1;
return (int) sysArg.arg4;
} /* end of SemCreate */
/*
* Routine: SemP
*
* Description: "P" a semaphore.
*
*
* Arguments: int semaphore -- semaphore handle
* (output value: completion status)
*
*/
int SemP(int semaphore)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_SEMP;
sysArg.arg1 = (void *) ( (long) semaphore);
USLOSS_Syscall(&sysArg);
return (int) sysArg.arg4;
} /* end of SemP */
/*
* Routine: SemV
*
* Description: "V" a semaphore.
*
*
* Arguments: int semaphore -- semaphore handle
* (output value: completion status)
*
*/
int SemV(int semaphore)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_SEMV;
sysArg.arg1 = (void *) ( (long) semaphore);
USLOSS_Syscall(&sysArg);
return (int) sysArg.arg4;
} /* end of SemV */
/*
* Routine: SemFree
*
* Description: Free a semaphore.
*
*
* Arguments: int semaphore -- semaphore handle
* (output value: completion status)
*
*/
int SemFree(int semaphore)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_SEMFREE;
sysArg.arg1 = (void *) ( (long) semaphore);
USLOSS_Syscall(&sysArg);
return (int) sysArg.arg4;
} /* end of SemFree */
/*
* Routine: GetTimeofDay
*
* Description: This is the call entry point for getting the time of day.
*
* Arguments: int *tod -- pointer to output value
* (output value: the time of day)
*
*/
void GetTimeofDay(int *tod)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_GETTIMEOFDAY;
USLOSS_Syscall(&sysArg);
*tod = (int) sysArg.arg1;
return;
} /* end of GetTimeofDay */
/*
* Routine: CPUTime
*
* Description: This is the call entry point for the process' CPU time.
*
*
* Arguments: int *cpu -- pointer to output value
* (output value: the CPU time of the process)
*
*/
void CPUTime(int *cpu)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_CPUTIME;
USLOSS_Syscall(&sysArg);
*cpu = (int) sysArg.arg1;
return;
} /* end of CPUTime */
/*
* Routine: GetPID
*
* Description: This is the call entry point for the process' PID.
*
*
* Arguments: int *pid -- pointer to output value
* (output value: the PID)
*
*/
void GetPID(int *pid)
{
systemArgs sysArg;
CHECKMODE;
sysArg.number = SYS_GETPID;
USLOSS_Syscall(&sysArg);
*pid = (int) sysArg.arg1;
return;
} /* end of GetPID */
/* end libuser.c */