-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyscalls.c
740 lines (603 loc) · 17.9 KB
/
syscalls.c
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/**
** @file syscalls.c
**
** @author CSCI-452 class of 20205
**
** System call implementations
*/
#define SP_KERNEL_SRC
#include "common.h"
#include "x86arch.h"
#include "x86pic.h"
#include "uart.h"
#include "support.h"
#include "bootstrap.h"
#include "syscalls.h"
#include "scheduler.h"
#include "process.h"
#include "stacks.h"
#include "clock.h"
#include "cio.h"
#include "sio.h"
#include "filemanager.h"
// copied from ulib.h
extern void exit_helper( void );
/*
** PRIVATE DEFINITIONS
*/
/*
** PRIVATE DATA TYPES
*/
/*
** PRIVATE GLOBAL VARIABLES
*/
// the system call jump table
//
// initialized by _sys_init() to ensure that the code::function mappings
// are correct even if the code values should happen to change
static void (*_syscalls[N_SYSCALLS])( uint32_t args[] );
/*
** PUBLIC GLOBAL VARIABLES
*/
/*
** PRIVATE FUNCTIONS
*/
/**
** Name: _sys_isr
**
** System call ISR
**
** @param vector Vector number for the clock interrupt
** @param code Error code (0 for this interrupt)
*/
static void _sys_isr( int vector, int code ) {
// if there is no current process, we're in deep trouble
assert( _current != NULL );
// much less likely to occur, but still potentially problematic
assert2( _current->context != NULL );
// retrieve the arguments to the system call
// (even if they aren't needed)
uint32_t args[4];
args[0] = ARG( _current, 1 );
args[1] = ARG( _current, 2 );
args[2] = ARG( _current, 3 );
args[3] = ARG( _current, 4 );
// retrieve the code
uint32_t syscode = REG( _current, eax );
// validate the code
if( syscode >= N_SYSCALLS ) {
// uh-oh....
__sprint( b256, "PID %d bad syscall %d", _current->pid, syscode );
WARNING( b256 );
// force a call to exit()
syscode = SYS_exit;
args[0] = E_BAD_SYSCALL;
}
// handle the system call
_syscalls[syscode]( args );
// tell the PIC we're done
__outb( PIC_PRI_CMD_PORT, PIC_EOI );
}
/**
** Second-level syscall handlers
**
** All have this prototype:
**
** static void _sys_NAME( uint32_t args[4] );
**
** Values being returned to the user are placed into the EAX
** field in the context save area for that process.
*/
/**
** _sys_fcreate - create a file
**
** implements:
** int fcreate( char *filename );
*/
static void _sys_fcreate( uint32_t args[4] ) {
// the only argument is the file name
char *filename = ( char *) args[0];
// call the function in filemanager
int result = _fs_create( filename );
// return the success value given by filemanager
RET(_current) = result;
}
/**
** _sys_fdelete - delete a file
**
** implements:
** int fdelete( char *filename );
*/
static void _sys_fdelete( uint32_t args[4] ) {
// the only argument is the file name
char *filename = ( char *) args[0];
// call the function in filemanager
int result = _fs_delete( filename );
// return the success value given by filemanager
RET(_current) = result;
}
/**
** _sys_fopen - open a file for reading and writing
**
** implements:
** int fopen( char *filename );
*/
static void _sys_fopen( uint32_t args[4] ) {
// the only argument is the file name
char *filename = ( char *) args[0];
// call the function in filemanager
int result = _fs_open( filename );
// return the success value given by filemanager
RET(_current) = result;
}
/**
** _sys_fclose - close a file after usage
**
** implements:
** int fclose( char *filename );
*/
static void _sys_fclose( uint32_t args[4] ) {
// the only argument is the file name
char *filename = ( char *) args[0];
// call the function in filemanager
int result = _fs_close( filename );
// return the success value given by filemanager
RET(_current) = result;
}
/**
** _sys_fread - read from a file
**
** implements:
** int fread( char *filename, char *buf );
*/
static void _sys_fread( uint32_t args[4] ) {
// first argument is the file name
char *filename = ( char *) args[0];
// second argument is the buffer to store file contents in
char *buf = ( char * ) args[1];
// call the function in filemanager
int size = _fs_read( filename, buf );
// return the number of characters read
// includes the NULL terminator
RET(_current) = size;
}
/**
** _sys_fwrite - write to a file
**
** implements:
** int fwrite( char *filename, char *buf, int size );
*/
static void _sys_fwrite( uint32_t args[4] ) {
// first argument is the file name
char *filename = ( char *) args[0];
// second argument is the string to be written
char *buf = ( char * ) args[1];
// third argument is length of the string(not including NULL-terminator)
int32_t buf_size = ( int32_t ) args[2];
// call the function in filemanager
int size = _fs_write( filename, buf, buf_size );
// return the success value given by filemanager
RET(_current) = size;
}
/**
** _sys_exit - terminate the calling process
**
** implements:
** void exit( int32_t status );
*/
static void _sys_exit( uint32_t args[4] ) {
int32_t status = (int32_t) args[0];
// record the termination status
_current->exit_status = status;
// perform exit processing for this process
_force_exit( _current, status );
// this process is done, so we need to pick another one
_dispatch();
}
/**
** _sys_read - read into a buffer from a stream
**
** implements:
** int32_t read( int chan, void *buffer, uint32_t length );
*/
static void _sys_read( uint32_t args[4] ) {
int n = 0;
uint32_t chan = args[0];
char *buf = (char *) args[1];
uint32_t length = args[2];
// try to get the next character(s)
switch( chan ) {
case CHAN_CONS:
// console input is non-blocking
if( __cio_input_queue() < 1 ) {
RET(_current) = E_NO_DATA;
return;
}
// at least one character
n = __cio_gets( buf, length );
break;
case CHAN_SIO:
// this may block the process; if so,
// _sio_reads() will dispatch a new one
n = _sio_reads( buf, length );
break;
default:
// bad channel code
RET(_current) = E_BAD_CHANNEL;
return;
}
// if there was data, return the byte count to the process;
// otherwise, block the process until data is available
if( n > 0 ) {
RET(_current) = n;
} else {
// mark it as blocked
_current->state = Blocked;
// put it on the SIO input queue
assert( _que_enque(_reading,_current,0) == E_SUCCESS );
// select a new current process
_dispatch();
}
}
/**
** _sys_write - write from a buffer to a stream
**
** implements:
** int32_t write( int chan, const void *buffer, uint32_t length );
*/
static void _sys_write( uint32_t args[4] ) {
uint32_t chan = args[0];
char *buf = (char *) args[1];
uint32_t length = args[2];
// this is almost insanely simple, but it does separate the
// low-level device access fromm the higher-level syscall implementation
switch( chan ) {
case CHAN_CONS:
__cio_write( buf, length );
RET(_current) = length;
break;
case CHAN_SIO:
_sio_write( buf, length );
RET(_current) = length;
break;
default:
RET(_current) = E_BAD_CHANNEL;
break;
}
}
/**
** _sys_getpid - retrieve the PID of this process
**
** implements:
** pid_t getpid( void );
*/
static void _sys_getpid( uint32_t args[4] ) {
RET(_current) = _current->pid;
}
/**
** _sys_getppid - retrieve the PID of the parent of this process
**
** implements:
** pid_t getppid( void );
*/
static void _sys_getppid( uint32_t args[4] ) {
RET(_current) = _current->ppid;
}
/**
** _sys_gettime - retrieve the current system time
**
** implements:
** time_t gettime( void );
*/
static void _sys_gettime( uint32_t args[4] ) {
RET(_current) = _system_time;
}
/**
** _sys_getprio - retrieve the priority for this process
**
** implements:
** prio_t getprio( void );
*/
static void _sys_getprio( uint32_t args[4] ) {
RET(_current) = _current->priority;
}
/**
** _sys_setprio - set the priovity for this process
**
** implements:
** prio_t setprio( prio_t new );
*/
static void _sys_setprio( uint32_t args[4] ) {
if( args[0] > PRIO_LOWEST ) {
RET(_current) = E_BAD_PARAM;
} else {
RET(_current) = _current->priority;
_current->priority = args[0];
}
}
/**
** _sys_kill - terminate a process with extreme prejudice
**
** implements:
** int32_t kill( pid_t victim );
*/
static void _sys_kill( uint32_t args[4] ) {
pid_t pid = (pid_t) args[0];
// POTENTIAL DANGER: What if we try kill(init) or kill(idle)?
// Might want to guard for that here!
// kill(0) is a request to kill the calling process
if( pid == 0 ) {
pid = _current->pid;
}
// locate the victim
pcb_t *pcb = _pcb_find_pid( pid );
if( pcb == NULL ) {
RET(_current) = E_NOT_FOUND;
return;
}
// how we process the victim depends on its current state:
switch( pcb->state ) {
// for the first three of these states, the process is on
// a queue somewhere; just mark it as 'Killed', and when it
// comes off that queue via _schedule() or _dispatch() we
// will clean it up
case Ready: // FALL THROUGH
case Blocked: // FALL THROUGH
case Sleeping:
pcb->state = Killed;
// FALL THROUGH
// for Killed, it's already been marked as 'Killed', so we
// don't need to re-mark it
case Killed:
RET(_current) = E_SUCCESS;
break;
// we have met the enemy, and he is us!
case Running: // current process
_force_exit( _current, Killed );
_dispatch();
break;
// much like 'Running', except that it's not the current
// process, so we don't have to dispatch another one
case Waiting:
_force_exit( pcb, Killed );
break;
// you can't kill something if it's already dead
case Zombie:
RET(_current) = E_NOT_FOUND;
break;
default:
// this is a really bad potential problem - we have
// a bogus process state, so we report that
__sprint( b256, "*** kill(): victim %d, unknown state %d\n",
pcb->pid, pcb->state );
__cio_puts( b256 );
// after reporting it, we give up
PANIC( 0, _sys_kill );
}
}
/**
** _sys_sleep - put the current process to sleep for some length of time
**
** implements:
** void sleep( uint32_t msec );
*/
static void _sys_sleep( uint32_t args[4] ) {
uint32_t ticks = MS_TO_TICKS( args[0] );
if( ticks == 0 ) {
// handle the case where the process is just yielding
_schedule( _current );
} else {
// process is actually going to sleep - calculate wakeup time
_current->event.wakeup = _system_time + ticks;
_current->state = Sleeping;
// add to the sleep queue
if( _que_enque(_sleeping,_current,_current->event.wakeup) !=
E_SUCCESS ) {
// something went wrong!
WARNING( "cannot enque(_sleeping,_current)" );
_schedule( _current );
}
}
// either way, need a new "current" process
_dispatch();
}
/**
** _sys_spawn - create a new process
**
** implements:
** pid_t spawn( int (*entry)(uint32_t,uint32_t),
** prio_t prio, uint32_t arg1, uint32_t arg2 );
*/
static void _sys_spawn( uint32_t args[4] ) {
// is there room for one more process in the system?
if( _active_procs >= N_PROCS ) {
RET(_current) = E_NO_PROCS;
return;
}
// verify that there is an entry point
if( args[0] == NULL ) {
RET(_current) = E_BAD_PARAM;
return;
}
// and that the priority is legal
if( args[1] > PRIO_LOWEST ) {
RET(_current) = E_BAD_PARAM;
return;
}
// create the process
pcb_t *pcb = _proc_create( args, _next_pid++, _current->pid );
if( pcb == NULL ) {
RET(_current) = E_NO_MEMORY;
return;
}
// the parent gets the PID of the child as its return value
RET(_current) = pcb->pid; // parent
// schedule the child
_schedule( pcb );
// add the child to the "active process" table
++_active_procs;
// find an empty process table slot
int i;
for( i = 0; i < N_PROCS; ++i ) {
if( _ptable[i] == NULL ) {
break;
}
}
// if we didn't find one, we have a serious problem
assert( i < N_PROCS );
// add this to the table
_ptable[i] = pcb;
}
/**
** _sys_wait - wait for a child process to terminate
**
** implements:
** pid_t wait( int32_t *status );
*/
static void _sys_wait( uint32_t args[4] ) {
int children = 0;
int i;
// see if this process has any children, and if so,
// whether one of them has terminated
for( i = 0; i < N_PROCS; ++i ) {
if( _ptable[i] != NULL && _ptable[i]->ppid == _current->pid ) {
++children;
if( _ptable[i]->state == Zombie ) {
break;
}
}
}
// case 1: no children
if( children < 1 ) {
// return the bad news
RET(_current) = E_NO_PROCS;
return;
}
// case 2: children, but none are zombies
if( i >= N_PROCS ) {
// block this process until one of them terminates
_current->state = Waiting;
_dispatch();
return;
}
// case 3: bingo!
// return the zombie's PID
RET(_current) = _ptable[i]->pid;
// see if the parent wants the termination status
int32_t *ptr = (int32_t *) (args[0]);
if( ptr != NULL ) {
// yes - return it
// *****************************************************
// Potential VM issue here! This code assigns the exit
// status into a variable in the parent's address space.
// This works in the baseline because we aren't using
// any type of memory protection. If address space
// separation is implemented, this code will very likely
// STOP WORKING, and will need to be fixed.
// *****************************************************
*ptr = _ptable[i]->exit_status;
}
// clean up the zombie now
_pcb_cleanup( _ptable[i] );
return;
}
/*
** PUBLIC FUNCTIONS
*/
/**
** Name: _sys_init
**
** Syscall module initialization routine
**
** Dependencies:
** Must be called after _sio_init()
*/
void _sys_init( void ) {
__cio_puts( " Syscall:" );
/*
** 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_read ] = _sys_read;
_syscalls[ SYS_write ] = _sys_write;
_syscalls[ SYS_getpid ] = _sys_getpid;
_syscalls[ SYS_getppid ] = _sys_getppid;
_syscalls[ SYS_gettime ] = _sys_gettime;
_syscalls[ SYS_getprio ] = _sys_getprio;
_syscalls[ SYS_setprio ] = _sys_setprio;
_syscalls[ SYS_kill ] = _sys_kill;
_syscalls[ SYS_sleep ] = _sys_sleep;
_syscalls[ SYS_spawn ] = _sys_spawn;
_syscalls[ SYS_wait ] = _sys_wait;
_syscalls[ SYS_fcreate ] = _sys_fcreate;
_syscalls[ SYS_fdelete ] = _sys_fdelete;
_syscalls[ SYS_fopen ] = _sys_fopen;
_syscalls[ SYS_fclose ] = _sys_fclose;
_syscalls[ SYS_fread ] = _sys_fread;
_syscalls[ SYS_fwrite ] = _sys_fwrite;
// install the second-stage ISR
__install_isr( INT_VEC_SYSCALL, _sys_isr );
// all done
__cio_puts( " done" );
}
/**
** Name: _force_exit
**
** Do the real work for exit() and some kill() calls
**
** @param victim Pointer to the PCB for the exiting process
** @param state Termination status for the process
*/
void _force_exit( pcb_t *victim, int32_t status ) {
pid_t us = victim->pid;
// reparent all the children of this process so that
// when they terminate init() will collect them
for( int i = 0; i < N_PROCS; ++i ) {
// if (A) this is an active process, and
// (B) it's in a "really active" state, and
// (C) it's a child of this process,
// hand it off to 'init'
if( _ptable[i] != NULL
&& _ptable[i]->state >= Ready
&& _ptable[i]->ppid == us) {
_ptable[i]->ppid = PID_INIT;
}
}
// locate this process' parent
pcb_t *parent = _pcb_find_pid( victim->ppid );
// every process has a parent, even if it's 'init'
assert( parent != NULL );
if( parent->state != Waiting ) {
// if the parent isn't currently waiting, turn
// the exiting process into a zombie
victim->state = Zombie;
// leave it alone and unchanged for now
return;
}
// OK, we know that the parent is currently waiting. Waiting
// processes, like Zombie processes, are not on an actual queue;
// instead, they exist solely in the process table, with their
// state indicating their condition.
// Give the parent this child's PID
RET(parent) = victim->pid;
// if the parent wants it, also return this child's status
int32_t *ptr = (int32_t *) ARG( parent, 1 );
if( ptr != NULL ) {
// *****************************************************
// Potential VM issue here! This code assigns the exit
// status into a variable in the parent's address space.
// This works in the baseline because we aren't using
// any type of memory protection. If address space
// separation is implemented, this code will very likely
// STOP WORKING, and will need to be fixed.
// *****************************************************
*ptr = status;
}
// switch the parent back on to process the info we gave it
_schedule( parent );
// clean up this process
_pcb_cleanup( victim );
}