-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscall.c
More file actions
588 lines (444 loc) · 11.5 KB
/
syscall.c
File metadata and controls
588 lines (444 loc) · 11.5 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
** SCCS ID: @(#)syscall.c 1.1 4/17/15
**
** File: syscall.c
**
** Author: CSCI-452 class of 20145
**
** Contributor:
**
** Description: System call module implementation
*/
#define __SP_KERNEL__
#include "common.h"
#include "syscall.h"
#include "process.h"
#include "stack.h"
#include "queue.h"
#include "scheduler.h"
#include "sio.h"
#include "fileSystem.h"
#include "support.h"
#include "startup.h"
#include <x86arch.h>
/*
** PRIVATE DEFINITIONS
*/
/*
** PRIVATE DATA TYPES
*/
/*
** PRIVATE GLOBAL VARIABLES
*/
// system call jump table
//
// initialized by _sys_modinit() to ensure that
// code::function mappings are correct
static void (*_syscalls[ N_SYSCALLS ])( pcb_t * );
/*
** PUBLIC GLOBAL VARIABLES
*/
queue_t _sleeping; // sleeping processes
/*
** PRIVATE FUNCTIONS
*/
/*
** _sys_isr(vector,code)
**
** Common handler for the system call module. Selects
** the correct second-level routine to invoke based on
** the contents of EAX.
**
** The second-level routine is invoked with a pointer to
** the PCB for the process. It is the responsibility of
** that routine to assign all return values for the call.
*/
static void _sys_isr( int vector, int code ) {
uint32_t which= _current->context->eax;
// verify that we were given a legal code
if( which >= N_SYSCALLS ) {
// nope - report it...
c_printf( "*** _sys_isr, PID %d syscall %d\n",
_current->pid, which );
// ...and force it to exit()
which = SYS_exit;
}
// invoke the appropriate syscall handler
// should work in C99: _syscalls[which]( _current );
(*_syscalls[which])( _current );
// tell the PIC we're done
__outb( PIC_MASTER_CMD_PORT, PIC_EOI );
}
/*
** Second-level syscall handlers
**
** All have this prototype:
**
** static void _sys_NAME( pcb_t * );
**
** Those which return results to the calling user do so through the
** context save area pointed to by the supplied PCB. This is also
** where incoming parameters to the syscall are found.
*/
/*
** _sys_exit - terminate the calling process
**
** implements: void exit( void );
**
** does not return
*/
static void _sys_exit( pcb_t *pcb ) {
// tear down the PCB structure
_stack_dealloc( pcb->stack );
_pcb_dealloc( pcb );
// if this was the current process, we need a new one
if( pcb == _current ) {
_dispatch();
}
}
/*
** _sys_spawnp - create a new process running the specified program
**
** implements: int spawnp( void (*entry)(void), prio );
**
** returns:
** pid of new process in original process, or -1 on error
*/
static void _sys_spawnp( pcb_t *pcb ) {
pcb_t *new;
// farm out all the work to this supporting routine
new = _create_process( (uint32_t) ARG(1,pcb->context),
(uint32_t) ARG(2,pcb->context) );
if( new == NULL ) {
// it failed - tell the parent
RET(pcb->context) = -1;
} else {
// it succeeded - set the parent PID
new->ppid = pcb->pid;
// tell the parent
RET(pcb->context) = new->pid;
// schedule the child
_schedule( new );
}
}
/*
** _sys_sleep - put the current process to sleep for some length of time
**
** implements: void sleep(uint32_t ms);
**
** If the sleep time (in milliseconds) is 0, just preempts the process;
** otherwise, puts it onto the sleep queue for the specified length of
** time.
*/
static void _sys_sleep( pcb_t *pcb ) {
uint32_t sleeptime = (uint32_t) ARG(1,pcb->context);
// if the desired sleep time is 0, treat this as a yield()
if( sleeptime == 0 ) {
// just preempt the process
_schedule( pcb );
} else {
// calculate the wakeup time for the process
pcb->wakeup = _system_time + MS_TO_TICKS(sleeptime);
// mark it as sleeping
pcb->state = STATE_SLEEPING;
// add it to the sleep queue
_queue_insert( _sleeping, (void *)pcb, (void *) pcb->wakeup );
}
// no current process - pick another one
_dispatch();
}
/*
** _sys_get_process_info - retrieve information about a process
**
** implements: int get_process_info( int code, int16_t pid );
**
** returns:
** the desired information, or -1 if the PID couldn't be
** located or the code wasn't recognized
*/
static void _sys_get_process_info( pcb_t *pcb ) {
int code = (int) ARG(1,pcb->context);
int pid = (int) ARG(2,pcb->context);
pcb_t *target;
// if PID is 0, use the current process;
// else, find the desired one
if( pid == 0 ) {
target = _current;
} else {
target = _pcb_find( pid );
// did we find it?
if( target == NULL ) {
RET(pcb->context) = -1; // NO!
return;
}
}
// produce the desired information
switch( code ) {
case INFO_PID:
RET(pcb->context) = target->pid;
break;
case INFO_PPID:
RET(pcb->context) = target->ppid;
break;
case INFO_STATE:
RET(pcb->context) = target->state;
break;
case INFO_WAKEUP:
RET(pcb->context) = target->wakeup;
break;
case INFO_PRIO:
RET(pcb->context) = target->prio;
break;
case INFO_QUANTUM:
RET(pcb->context) = target->quantum;
break;
case INFO_DEF_QUANTUM:
RET(pcb->context) = target->default_quantum;
break;
default:
RET(pcb->context) = -1;
}
}
/*
** _sys_get_system_info - retrieve information about the system
**
** implements: int get_system_info( int code );
**
** returns:
** the desired information, or -1 if the code wasn't recognized
*/
static void _sys_get_system_info( pcb_t *pcb ) {
int code = (int) ARG(1,pcb->context);
// produce the desired information
switch( code ) {
case SYSINFO_TIME:
RET(pcb->context) = _system_time;
break;
case SYSINFO_NUM_PROCS:
RET(pcb->context) = _system_active;
break;
case SYSINFO_MAX_PROCS:
RET(pcb->context) = N_PROCS;
break;
default:
RET(pcb->context) = -1;
}
}
/*
** _sys_read - read bytes from the specified input channel
**
** implements: int read( int fd, void *buf, int count );
**
** reads up to 'count' bytes or how many are available,
** whichever is smaller
**
** returns:
** the count of characters read in
*/
static void _sys_read( pcb_t *pcb ) {
int fd = (int) ARG(1,pcb->context);
char *buf = (char *) ARG(2, pcb->context);
int count = (int) ARG(3,pcb->context);
int n;
int (*qlength)(void);
int (*getchar)(void);
// set function pointers based on the stream
if( fd == FD_CONSOLE ) {
qlength = c_input_queue;
getchar = c_getchar;
} else if( fd == FD_SIO ) {
qlength = _sio_input_queue;
getchar = _sio_readc;
} else {
RET(pcb->context) = -1;
return;
}
n = qlength();
// if there are characters, read in the desired number of them
if( n > 0 ) {
// don't read more than the user wants, or more
// characters than there are in the buffer
if( n > count ) {
n = count;
} else {
count = n;
}
while( count-- ) {
*buf++ = getchar();
}
}
RET(pcb->context) = n;
}
/*
** _sys_write - write bytes to the specified output channel
**
** implements: int write( int fd, void *buf, int count );
**
** if 'count' is 0, write out a NUL-terminated buffer;
** otherwise, write 'count' bytes
**
** returns:
** the count of characters written
*/
static void _sys_write( pcb_t *pcb ) {
int fd = (int) ARG(1,pcb->context);
char *buf = (char *) ARG(2,pcb->context);
int count = (int) ARG(3,pcb->context);
if( fd == FD_CONSOLE ) {
if( count == 0 ) {
RET(pcb->context) = c_puts( buf );
} else {
c_putbuf( buf, count );
RET(pcb->context) = count;
}
} else if( fd == FD_SIO ) {
if( count == 0 ) {
RET(pcb->context) = _sio_puts( buf );
} else {
_sio_writes( buf, count );
RET(pcb->context) = count;
}
} else { // bad parameter!
RET(pcb->context) = -1;
}
}
/*
** _sys_create_file - creates a file in the file system
**
** implements: uint8_t create_file( char* filename );
**
** returns:
** 0 for success, 1 for failure
*/
static void _sys_create_file( pcb_t *pcb ) {
char *filename = (char *) ARG(1,pcb->context);
c_puts( "SYSCALL.C: Creating file...\n" );
int res = _sfs_create(filename, FILE);
if( res == 0 ) c_puts("SYSCALL.C: Create success\n");
else c_puts("SYSCALL.C Create failed\n");
RET(pcb->context) = res;
}
/*
** _sys_delete_file - deletes a file in the file system
**
** implements: uint8_t delete_file( char* filename );
**
** returns:
** 0 for success, 1 for failure
*/
static void _sys_delete_file( pcb_t *pcb ) {
char *filename = (char *) ARG(1,pcb->context);
c_puts( "SYSCALL.C: Deleting file...\n" );
int res = _sfs_delete(filename);
if( res == 0 ) c_puts("SYSCALL.C: Delete success\n");
else c_puts("SYSCALL.C Delete failed\n");
RET(pcb->context) = res;
}
/*
** _sys_write_file - writes to a file in the file system
**
** implements: uint8_t write_file( char* filename, uint8_t size, char* buf,
** int doAppend );
**
** returns:
** 0 for success, 1 for failure
*/
static void _sys_write_file( pcb_t *pcb ) {
char *filename = (char *) ARG(1,pcb->context);
uint16_t size = (uint16_t) ARG(2,pcb->context);
uint8_t *buf = (uint8_t *) ARG(3,pcb->context);
int doAppend = (int) ARG(4,pcb->context);
c_puts(filename);
c_puts((char *) buf);
c_puts( "SYSCALL.C Writing to file...\n" );
int res = _sfs_write(filename, size, buf, doAppend);
if(res == 0) c_puts("SYSCALL.C Write success\n");
else c_puts("SYSCALL.C Write failed\n");
RET(pcb->context) = res;
}
/*
** _sys_read_file - reads a file in the file system
**
** implements: uint8_t read_file( char* filename );
**
** returns:
** 0 for failure, or pointer to data
*/
static void _sys_read_file( pcb_t *pcb ) {
char *filename = (char *) ARG(1,pcb->context);
c_puts( "SYSCALL.C Reading from file...\n");
uint8_t *res = _sfs_read(filename);
c_puts("\n");
c_puts( (char *) res);
c_puts("\n");
if(*res != 0) c_puts("SYSCALL.C Read success\n");
else c_puts("SYSCALL.C Read failed\n");
RET(pcb->context) = (int) res;
}
/*
** _sys_list_file - lists the files in the file system
**
** implements: uint8_t list_file( void );
**
** returns:
** pointer for the string result
*/
static void _sys_list_files( pcb_t *pcb ) {
c_puts( "SYSCALL.C Calling list...\n");
uint8_t *res = _sfs_list();
RET(pcb->context) = (int) res;
}
/*
** _sys_cd - changes current directory in the file system
**
** implements: uint8_t _set_directory( char* new_dir );
**
** returns:
** 0 for success, anything else for failure
*/
static void _sys_cd( pcb_t *pcb ) {
char *new_dir = (char *) ARG(1,pcb->context);
c_puts( "SYSCALL.C: Changing directory...\n" );
int res = _set_directory(new_dir);
if( res == 0 ) c_puts("SYSCALL.C: cd success\n");
else c_puts("SYSCALL.C cd failed\n");
RET(pcb->context) = res;
}
/*
** PUBLIC FUNCTIONS
*/
/*
** _sys_modinit()
**
** initialize the syscall module
*/
void _sys_modinit( void ) {
// allocate and initialize the sleep queue
if( _queue_alloc(&_sleeping,1) != 1 ||
_sleeping == NULL ) {
_kpanic( "_sys_init", "sleep queue alloc failed" );
}
_queue_init( _sleeping, _compare_time );
/*
** Set up the syscall jump table. We do this here
** to ensure that the association between syscall
** code and function address is correct even if the
** codes change.
*/
_syscalls[ SYS_exit ] = _sys_exit;
_syscalls[ SYS_spawnp ] = _sys_spawnp;
_syscalls[ SYS_sleep ] = _sys_sleep;
_syscalls[ SYS_read ] = _sys_read;
_syscalls[ SYS_write ] = _sys_write;
_syscalls[ SYS_get_process_info ] = _sys_get_process_info;
_syscalls[ SYS_get_system_info ] = _sys_get_system_info;
_syscalls[ SYS_create_file ] = _sys_create_file;
_syscalls[ SYS_delete_file ] = _sys_delete_file;
_syscalls[ SYS_write_file ] = _sys_write_file;
_syscalls[ SYS_read_file ] = _sys_read_file;
_syscalls[ SYS_list_files ] = _sys_list_files;
_syscalls[ SYS_cd ] = _sys_cd;
// install our ISR
__install_isr( INT_VEC_SYSCALL, _sys_isr );
c_puts( " SYSCALL" );
}