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
|
// SMP.1 + SMP.3
// spinlock + mask interrupts
#include "config.h"
#include "errno.h"
#include "fs/file.h"
#include "fs/vfs.h"
#include "fs/vnode.h"
#include "globals.h"
#include "kernel.h"
#include "mm/slab.h"
#include "util/debug.h"
#include "util/printf.h"
#include "util/string.h"
#include "util/time.h"
#include <drivers/screen.h>
#include <fs/vfs_syscall.h>
#include <main/apic.h>
/*==========
* Variables
*=========*/
/*
* Global variable that maintains the current process
*/
proc_t *curproc CORE_SPECIFIC_DATA;
/*
* Global list of all processes (except for the idle process) and its lock
*/
static list_t proc_list = LIST_INITIALIZER(proc_list);
/*
* Allocator for process descriptors
*/
static slab_allocator_t *proc_allocator = NULL;
/*
* Statically allocated idle process
* Each core has its own idleproc, so the idleproc is stored in static memory
* rather than in the global process list
*/
proc_t idleproc CORE_SPECIFIC_DATA;
/*
* Pointer to the init process
*/
static proc_t *proc_initproc = NULL;
/*===============
* System startup
*==============*/
/*
* Initializes the allocator for process descriptors.
*/
void proc_init()
{
proc_allocator = slab_allocator_create("proc", sizeof(proc_t));
KASSERT(proc_allocator);
}
/*
* Initializes idleproc for the current core. Sets initial values for curproc
* and curthr.
*/
void proc_idleproc_init()
{
proc_t *proc = &idleproc;
proc->p_pid = 0;
list_init(&proc->p_threads);
list_init(&proc->p_children);
proc->p_pproc = NULL;
list_link_init(&proc->p_child_link);
list_link_init(&proc->p_list_link);
proc->p_status = 0;
proc->p_state = PROC_RUNNING;
memset(&proc->p_wait, 0, sizeof(ktqueue_t)); // should not be used
proc->p_pml4 = pt_get();
proc->p_vmmap = vmmap_create();
proc->p_cwd = NULL;
memset(proc->p_files, 0, sizeof(proc->p_files));
char name[8];
snprintf(name, sizeof(name), "idle%ld", curcore.kc_id);
strncpy(proc->p_name, name, PROC_NAME_LEN);
proc->p_name[PROC_NAME_LEN - 1] = '\0';
dbg(DBG_PROC, "created %s\n", proc->p_name);
curproc = &idleproc;
curthr = NULL;
}
/*=================
* Helper functions
*================*/
/*
* Gets the next available process ID (pid).
*/
static pid_t next_pid = 1;
static pid_t _proc_getid()
{
pid_t pid = next_pid;
restart:
list_iterate(&proc_list, p, proc_t, p_list_link)
{
if (p->p_pid == pid)
{
pid = pid + 1 == PROC_MAX_COUNT ? 1 : pid + 1;
if (pid == next_pid)
{
return -1;
}
else
{
goto restart;
}
}
}
next_pid = pid + 1 == PROC_MAX_COUNT ? 1 : pid + 1;
KASSERT(pid);
return pid;
}
/*
* Searches the global process list for the process descriptor corresponding to
* a pid.
*/
proc_t *proc_lookup(pid_t pid)
{
if (pid == 0)
{
return &idleproc;
}
list_iterate(&proc_list, p, proc_t, p_list_link)
{
if (p->p_pid == pid)
{
return p;
}
}
return NULL;
}
/*==========
* Functions
*=========*/
/*
* Creates a new process with the given name.
* Returns the newly created process, or NULL on failure.
*
* Hints:
* Use _proc_getid() to get a new pid.
* Allocate a new proc_t with the process slab allocator (proc_allocator).
* Use pt_create() to create a new page table (p_pml4).
* If the newly created process is the init process (i.e. the generated PID
* matches the init process's PID, given by the macro PID_INIT), set the
* global proc_initproc to the created process.
*
* There is some setup to be done for VFS and VM - remember to return to this
* function! For VFS, clone and ref the files from curproc. For VM, clone the
* vmmap from curproc.
*
* Be sure to free resources appropriately if proc_create() fails midway!
*/
proc_t *proc_create(const char *name)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_create");
int proc_pid = _proc_getid();
if (proc_pid == -1)
{
return NULL;
}
proc_t *proc = (proc_t *)slab_obj_alloc(proc_allocator);
if (proc == NULL)
{
return NULL;
}
proc->p_pid = proc_pid;
list_init(&proc->p_threads);
list_init(&proc->p_children);
proc->p_pproc = curproc;
list_init(&proc->p_threads);
list_init(&proc->p_children);
proc->p_status = 0;
proc->p_state = PROC_RUNNING; // TODO: check if this is correct
sched_queue_init(&proc->p_wait);
pml4_t *pml4 = pt_create();
if (pml4 == NULL)
{
slab_obj_free(proc_allocator, proc);
return NULL;
}
// if successful building memory, fill the struct
strncpy(proc->p_name, name, PROC_NAME_LEN);
proc->p_name[PROC_NAME_LEN - 1] = '\0';
proc->p_pml4 = pml4;
list_link_init(&proc->p_child_link);
list_link_init(&proc->p_list_link);
list_insert_tail(&curproc->p_children, &proc->p_child_link);
list_insert_tail(&proc_list, &proc->p_list_link);
if (proc_pid == PID_INIT)
{
// if it's init proc, set to global variable
proc_initproc = proc;
}
#ifdef __VFS__
// clone and ref the files from curproc
for (int fd = 0; fd < NFILES; fd++)
{
proc->p_files[fd] = NULL;
}
if (proc->p_pid != PID_INIT)
{
proc->p_cwd = proc->p_pproc->p_cwd;
vref(proc->p_cwd);
}
else
{
proc->p_cwd = NULL;
}
#endif
#ifdef __VM__
proc->p_vmmap = vmmap_clone(curproc->p_vmmap);
curproc->p_vmmap->vmm_proc = proc;
KASSERT(proc->p_vmmap != NULL); //FIXME!
// copy the table of file descriptors
for (int i = 0; i < NFILES; i++)
{
if (curproc->p_files[i] != NULL)
{
fref(curproc->p_files[i]);
proc->p_files[i] = curproc->p_files[i];
}
}
#endif
return proc;
}
/*
* Helper for proc_thread_exiting() that cleans up resources from the current
* process in preparation for its destruction (which occurs later via proc_destroy()).
* Reparents child processes to the init process, or initiates Weenix shutdown
* if the current process is the init process.
*
* Hints:
* You won't have much to clean up until VFS and VM -- remember to revisit this
* function later!
* **VFS/VM** - there may be some repeat code in proc_destroy()). The initial process
* does not have a parent process and thus cleans itself up, hence why we need to cleanup
* here as well.
*
* Remember to set the state and status of the process.
* The init process' PID is given by PID_INIT.
* Use initproc_finish() to shutdown Weenix when cleaning up the init process.
*/
void proc_cleanup(long status)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_cleanup");
dbg(DBG_PROC, "proc_cleanup called on proc with pid=%d with exit status=%d\n", curproc->p_pid, status);
curproc->p_state = PROC_DEAD;
// update state and status
// if (curthr->kt_cancelled) {
// curproc->p_status = curthr->kt_retval;
// } else {
// }
curproc->p_status = status;
for (int fd = 0; fd < NFILES; fd++)
{
do_close(fd);
}
vput(&curproc->p_cwd);
#ifdef __VM__
vmmap_destroy(&curproc->p_vmmap);
#endif
if (curproc->p_pid == PID_INIT)
{
// if it's init proc, shutdown weenix
initproc_finish();
return;
}
// if it's not init proc, reparent child processes to parent proc
if (curproc->p_pproc == NULL)
{
panic("parent process not found in proc_pleanup");
}
list_iterate(&curproc->p_children, child, proc_t, p_child_link)
{
// remove & insert to init process
child->p_pproc = proc_initproc;
list_remove(&child->p_child_link);
list_insert_head(&proc_initproc->p_children, &child->p_child_link);
}
}
/*
* Cleans up the current process and the current thread, broadcasts on its
* parent's p_wait, then forces a context switch. After this, the process is
* essentially dead -- this function does not return. The parent must eventually
* finish destroying the process.
*
* Hints:
* Use proc_cleanup() to clean up the current process. As retval specifies the current
* thread's return value, you should pass (long)retval as the status argument to
* proc_cleanup().
* Remember to set the exit state and return value of the current thread after calling
* proc_cleanup(), as this may block and cause the thread's state to be overwritten.
* The context switch should be performed by a call to sched_switch().
*/
void proc_thread_exiting(void *retval)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_thread_exiting");
proc_cleanup((long)retval);
curthr->kt_state = KT_EXITED;
curthr->kt_retval = retval;
sched_broadcast_on(&curproc->p_pproc->p_wait);
sched_switch(0);
}
/*
* Cancels all the threads of proc. This should never be called on curproc.
*
* Hints:
* The status argument should be passed to kthread_cancel() as the retval.
*/
void proc_kill(proc_t *proc, long status)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_kill");
KASSERT(proc != curproc && "proc_kill called on curproc");
list_iterate(&proc->p_threads, thr, kthread_t, kt_plink)
{
kthread_cancel(thr, (void *)status);
}
}
/*
* Kills all processes that are not curproc and not a direct child of idleproc (i.e.,
* the init process), then kills the current process.
*
* Hints:
* The PID of the idle process is given by PID_IDLE.
* Processes should be killed with a status of -1.
* Use do_exit() to kill the current process.
*/
void proc_kill_all()
{
// NOT_YET_IMPLEMENTED("PROCS: proc_kill_all");
list_iterate(&proc_list, p, proc_t, p_list_link)
{
if (
p->p_pid != curproc->p_pid
&&
p->p_pproc->p_pid != PID_IDLE
)
{
proc_kill(p, curthr->kt_retval);
}
}
// kill this proc
do_exit(0);
}
/*
* Destroy / free everything from proc. Be sure to remember reference counting
* when working on VFS.
*
* In contrast with proc_cleanup() (in which a process begins to clean itself up), this
* will be called on proc by some other process to complete its cleanup.
* I.e., the process we are destroying should not be curproc.
*/
void proc_destroy(proc_t *proc)
{
list_remove(&proc->p_list_link);
list_iterate(&proc->p_threads, thr, kthread_t, kt_plink)
{
kthread_destroy(thr);
}
#ifdef __VFS__
for (int fd = 0; fd < NFILES; fd++)
{
if (proc->p_files[fd])
{
fput(proc->p_files + fd);
}
}
if (proc->p_cwd)
{
vput(&proc->p_cwd);
}
#endif
#ifdef __VM__
if (proc->p_vmmap)
vmmap_destroy(&proc->p_vmmap);
#endif
dbg(DBG_PROC, "destroying P%d\n", proc->p_pid);
KASSERT(proc->p_pml4);
pt_destroy(proc->p_pml4);
slab_obj_free(proc_allocator, proc);
}
/*=============
* System calls
*============*/
/*
* Waits for a child process identified by pid to exit. Finishes destroying the
* process and optionally returns the child's status in status.
*
* If pid is a positive integer, tries to clean up the process specified by pid.
* If pid is -1, cleans up any child process of curproc that exits.
*
* Returns the pid of the child process that exited, or error cases:
* - ENOTSUP: pid is 0, a negative number not equal to -1,
* or options are specified (options does not equal 0)
* - ECHILD: pid is a positive integer but not a child of curproc, or
* pid is -1 and the process has no children
*
* Hints:
* Use sched_sleep_on() to be notified of a child process exiting.
* Destroy an exited process by removing it from any lists and calling
* proc_destroy(). Remember to set status (if it was provided) to the child's
* status before destroying the process.
* If waiting on a specific child PID, wakeups from other exiting child
* processes should be ignored.
* If waiting on any child (-1), do_waitpid can return when *any* child has exited,
* it does not have to return the one that exited earliest.
* Which field can you use to determine whether a given process exited?
*/
pid_t do_waitpid(pid_t pid, int *status, int options)
{
// NOT_YET_IMPLEMENTED("PROCS: do_waitpid");
if (pid == 0 || options != 0 || pid < -1)
{
return -ENOTSUP;
}
dbg(DBG_PROC, "waiting for pid=%d\n", pid);
if (pid > 0)
{
proc_t *child = proc_lookup(pid);
if (child == NULL || child->p_pproc != curproc)
{
return -ECHILD;
}
// sleep until this specific child process exits
while (child->p_state != PROC_DEAD)
{
dbg(DBG_PROC, "waiting for specific child, calling sched_sleep_on\n");
sched_sleep_on(&curproc->p_wait);
}
if (status != NULL)
{
dbg(DBG_PROC, "setting status to %d\n", child->p_status);
*status = child->p_status;
}
list_remove(&child->p_child_link);
proc_destroy(child);
dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid);
return pid;
}
else if (pid == -1)
{
if (list_empty(&curproc->p_children))
{
return -ECHILD;
}
while(1) {
list_iterate(&curproc->p_children, child, proc_t, p_child_link)
{
if (child->p_state == PROC_DEAD)
{
dbg(DBG_PROC, "found a dead thread with pid=%d\n", child->p_pid);
if (status != NULL)
{
dbg(DBG_PROC, "setting status to %d\n", child->p_status);
*status = child->p_status;
}
int child_pid = child->p_pid;
list_remove(&child->p_child_link);
proc_destroy(child);
dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid);
return child_pid;
}
}
dbg(DBG_PROC, "waiting for any child, calling sched_sleep_on\n");
sched_sleep_on(&curproc->p_wait);
}
}
return 0;
}
/*
* Wrapper around kthread_exit.
*/
void do_exit(long status)
{
// NOT_YET_IMPLEMENTED("PROCS: do_exit");
dbg(DBG_PROC, "proc exiting with pid=%d and status=%ld\n", curproc->p_pid, status);
kthread_exit((void *)status);
}
/*==========
* Debugging
*=========*/
size_t proc_info(const void *arg, char *buf, size_t osize)
{
const proc_t *p = (proc_t *)arg;
size_t size = osize;
proc_t *child;
KASSERT(NULL != p);
KASSERT(NULL != buf);
iprintf(&buf, &size, "pid: %i\n", p->p_pid);
iprintf(&buf, &size, "name: %s\n", p->p_name);
if (NULL != p->p_pproc)
{
iprintf(&buf, &size, "parent: %i (%s)\n", p->p_pproc->p_pid,
p->p_pproc->p_name);
}
else
{
iprintf(&buf, &size, "parent: -\n");
}
if (list_empty(&p->p_children))
{
iprintf(&buf, &size, "children: -\n");
}
else
{
iprintf(&buf, &size, "children:\n");
}
list_iterate(&p->p_children, child, proc_t, p_child_link)
{
iprintf(&buf, &size, " %i (%s)\n", child->p_pid, child->p_name);
}
iprintf(&buf, &size, "status: %ld\n", p->p_status);
iprintf(&buf, &size, "state: %i\n", p->p_state);
#ifdef __VFS__
#ifdef __GETCWD__
if (NULL != p->p_cwd)
{
char cwd[256];
lookup_dirpath(p->p_cwd, cwd, sizeof(cwd));
iprintf(&buf, &size, "cwd: %-s\n", cwd);
}
else
{
iprintf(&buf, &size, "cwd: -\n");
}
#endif /* __GETCWD__ */
#endif
#ifdef __VM__
iprintf(&buf, &size, "start brk: 0x%p\n", p->p_start_brk);
iprintf(&buf, &size, "brk: 0x%p\n", p->p_brk);
#endif
return size;
}
size_t proc_list_info(const void *arg, char *buf, size_t osize)
{
size_t size = osize;
KASSERT(NULL == arg);
KASSERT(NULL != buf);
#if defined(__VFS__) && defined(__GETCWD__)
iprintf(&buf, &size, "%5s %-13s %-18s %-s\n", "PID", "NAME", "PARENT",
"CWD");
#else
iprintf(&buf, &size, "%5s %-13s %-s\n", "PID", "NAME", "PARENT");
#endif
list_iterate(&proc_list, p, proc_t, p_list_link)
{
char parent[64];
if (NULL != p->p_pproc)
{
snprintf(parent, sizeof(parent), "%3i (%s)", p->p_pproc->p_pid,
p->p_pproc->p_name);
}
else
{
snprintf(parent, sizeof(parent), " -");
}
#if defined(__VFS__) && defined(__GETCWD__)
if (NULL != p->p_cwd)
{
char cwd[256];
lookup_dirpath(p->p_cwd, cwd, sizeof(cwd));
iprintf(&buf, &size, " %3i %-13s %-18s %-s\n", p->p_pid, p->p_name,
parent, cwd);
}
else
{
iprintf(&buf, &size, " %3i %-13s %-18s -\n", p->p_pid, p->p_name,
parent);
}
#else
iprintf(&buf, &size, " %3i %-13s %-s\n", p->p_pid, p->p_name, parent);
#endif
}
return size;
}
|