-
Notifications
You must be signed in to change notification settings - Fork 0
/
1905004.patch
2895 lines (2714 loc) · 72.4 KB
/
1905004.patch
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile b/Makefile
index 39a99d7..57cd232 100644
--- a/Makefile
+++ b/Makefile
@@ -132,6 +132,9 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_trace\
+ $U/_history\
+ $U/_shutdown\
fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
diff --git a/kernel/defs.h b/kernel/defs.h
index a3c962b..a1a4f33 100644
--- a/kernel/defs.h
+++ b/kernel/defs.h
@@ -8,182 +8,187 @@ struct spinlock;
struct sleeplock;
struct stat;
struct superblock;
+struct syscall_stat;
+
+// start.c
+void timerhalt(void);
// bio.c
-void binit(void);
-struct buf* bread(uint, uint);
-void brelse(struct buf*);
-void bwrite(struct buf*);
-void bpin(struct buf*);
-void bunpin(struct buf*);
+void binit(void);
+struct buf *bread(uint, uint);
+void brelse(struct buf *);
+void bwrite(struct buf *);
+void bpin(struct buf *);
+void bunpin(struct buf *);
// console.c
-void consoleinit(void);
-void consoleintr(int);
-void consputc(int);
+void consoleinit(void);
+void consoleintr(int);
+void consputc(int);
// exec.c
-int exec(char*, char**);
+int exec(char *, char **);
// file.c
-struct file* filealloc(void);
-void fileclose(struct file*);
-struct file* filedup(struct file*);
-void fileinit(void);
-int fileread(struct file*, uint64, int n);
-int filestat(struct file*, uint64 addr);
-int filewrite(struct file*, uint64, int n);
+struct file *filealloc(void);
+void fileclose(struct file *);
+struct file *filedup(struct file *);
+void fileinit(void);
+int fileread(struct file *, uint64, int n);
+int filestat(struct file *, uint64 addr);
+int filewrite(struct file *, uint64, int n);
// fs.c
-void fsinit(int);
-int dirlink(struct inode*, char*, uint);
-struct inode* dirlookup(struct inode*, char*, uint*);
-struct inode* ialloc(uint, short);
-struct inode* idup(struct inode*);
-void iinit();
-void ilock(struct inode*);
-void iput(struct inode*);
-void iunlock(struct inode*);
-void iunlockput(struct inode*);
-void iupdate(struct inode*);
-int namecmp(const char*, const char*);
-struct inode* namei(char*);
-struct inode* nameiparent(char*, char*);
-int readi(struct inode*, int, uint64, uint, uint);
-void stati(struct inode*, struct stat*);
-int writei(struct inode*, int, uint64, uint, uint);
-void itrunc(struct inode*);
+void fsinit(int);
+int dirlink(struct inode *, char *, uint);
+struct inode *dirlookup(struct inode *, char *, uint *);
+struct inode *ialloc(uint, short);
+struct inode *idup(struct inode *);
+void iinit();
+void ilock(struct inode *);
+void iput(struct inode *);
+void iunlock(struct inode *);
+void iunlockput(struct inode *);
+void iupdate(struct inode *);
+int namecmp(const char *, const char *);
+struct inode *namei(char *);
+struct inode *nameiparent(char *, char *);
+int readi(struct inode *, int, uint64, uint, uint);
+void stati(struct inode *, struct stat *);
+int writei(struct inode *, int, uint64, uint, uint);
+void itrunc(struct inode *);
// ramdisk.c
-void ramdiskinit(void);
-void ramdiskintr(void);
-void ramdiskrw(struct buf*);
+void ramdiskinit(void);
+void ramdiskintr(void);
+void ramdiskrw(struct buf *);
// kalloc.c
-void* kalloc(void);
-void kfree(void *);
-void kinit(void);
+void *kalloc(void);
+void kfree(void *);
+void kinit(void);
// log.c
-void initlog(int, struct superblock*);
-void log_write(struct buf*);
-void begin_op(void);
-void end_op(void);
+void initlog(int, struct superblock *);
+void log_write(struct buf *);
+void begin_op(void);
+void end_op(void);
// pipe.c
-int pipealloc(struct file**, struct file**);
-void pipeclose(struct pipe*, int);
-int piperead(struct pipe*, uint64, int);
-int pipewrite(struct pipe*, uint64, int);
+int pipealloc(struct file **, struct file **);
+void pipeclose(struct pipe *, int);
+int piperead(struct pipe *, uint64, int);
+int pipewrite(struct pipe *, uint64, int);
// printf.c
-void printf(char*, ...);
-void panic(char*) __attribute__((noreturn));
-void printfinit(void);
+void printf(char *, ...);
+void panic(char *) __attribute__((noreturn));
+void printfinit(void);
// proc.c
-int cpuid(void);
-void exit(int);
-int fork(void);
-int growproc(int);
-void proc_mapstacks(pagetable_t);
-pagetable_t proc_pagetable(struct proc *);
-void proc_freepagetable(pagetable_t, uint64);
-int kill(int);
-int killed(struct proc*);
-void setkilled(struct proc*);
-struct cpu* mycpu(void);
-struct cpu* getmycpu(void);
-struct proc* myproc();
-void procinit(void);
-void scheduler(void) __attribute__((noreturn));
-void sched(void);
-void sleep(void*, struct spinlock*);
-void userinit(void);
-int wait(uint64);
-void wakeup(void*);
-void yield(void);
-int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
-int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
-void procdump(void);
+int cpuid(void);
+void exit(int);
+int fork(void);
+int growproc(int);
+void proc_mapstacks(pagetable_t);
+pagetable_t proc_pagetable(struct proc *);
+void proc_freepagetable(pagetable_t, uint64);
+int kill(int);
+int killed(struct proc *);
+void setkilled(struct proc *);
+struct cpu *mycpu(void);
+struct cpu *getmycpu(void);
+struct proc *myproc();
+void procinit(void);
+void scheduler(void) __attribute__((noreturn));
+void sched(void);
+void sleep(void *, struct spinlock *);
+void userinit(void);
+int wait(uint64);
+void wakeup(void *);
+void yield(void);
+int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
+int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
+void procdump(void);
// swtch.S
-void swtch(struct context*, struct context*);
+void swtch(struct context *, struct context *);
// spinlock.c
-void acquire(struct spinlock*);
-int holding(struct spinlock*);
-void initlock(struct spinlock*, char*);
-void release(struct spinlock*);
-void push_off(void);
-void pop_off(void);
+void acquire(struct spinlock *);
+int holding(struct spinlock *);
+void initlock(struct spinlock *, char *);
+void release(struct spinlock *);
+void push_off(void);
+void pop_off(void);
// sleeplock.c
-void acquiresleep(struct sleeplock*);
-void releasesleep(struct sleeplock*);
-int holdingsleep(struct sleeplock*);
-void initsleeplock(struct sleeplock*, char*);
+void acquiresleep(struct sleeplock *);
+void releasesleep(struct sleeplock *);
+int holdingsleep(struct sleeplock *);
+void initsleeplock(struct sleeplock *, char *);
// string.c
-int memcmp(const void*, const void*, uint);
-void* memmove(void*, const void*, uint);
-void* memset(void*, int, uint);
-char* safestrcpy(char*, const char*, int);
-int strlen(const char*);
-int strncmp(const char*, const char*, uint);
-char* strncpy(char*, const char*, int);
+int memcmp(const void *, const void *, uint);
+void *memmove(void *, const void *, uint);
+void *memset(void *, int, uint);
+char *safestrcpy(char *, const char *, int);
+int strlen(const char *);
+int strncmp(const char *, const char *, uint);
+char *strncpy(char *, const char *, int);
// syscall.c
-void argint(int, int*);
-int argstr(int, char*, int);
-void argaddr(int, uint64 *);
-int fetchstr(uint64, char*, int);
-int fetchaddr(uint64, uint64*);
-void syscall();
+void argint(int, int *);
+int argstr(int, char *, int);
+void argaddr(int, uint64 *);
+int fetchstr(uint64, char *, int);
+int fetchaddr(uint64, uint64 *);
+void historyinit();
+void syscall();
// trap.c
-extern uint ticks;
-void trapinit(void);
-void trapinithart(void);
+extern uint ticks;
+void trapinit(void);
+void trapinithart(void);
extern struct spinlock tickslock;
-void usertrapret(void);
+void usertrapret(void);
// uart.c
-void uartinit(void);
-void uartintr(void);
-void uartputc(int);
-void uartputc_sync(int);
-int uartgetc(void);
+void uartinit(void);
+void uartintr(void);
+void uartputc(int);
+void uartputc_sync(int);
+int uartgetc(void);
// vm.c
-void kvminit(void);
-void kvminithart(void);
-void kvmmap(pagetable_t, uint64, uint64, uint64, int);
-int mappages(pagetable_t, uint64, uint64, uint64, int);
-pagetable_t uvmcreate(void);
-void uvmfirst(pagetable_t, uchar *, uint);
-uint64 uvmalloc(pagetable_t, uint64, uint64, int);
-uint64 uvmdealloc(pagetable_t, uint64, uint64);
-int uvmcopy(pagetable_t, pagetable_t, uint64);
-void uvmfree(pagetable_t, uint64);
-void uvmunmap(pagetable_t, uint64, uint64, int);
-void uvmclear(pagetable_t, uint64);
-pte_t * walk(pagetable_t, uint64, int);
-uint64 walkaddr(pagetable_t, uint64);
-int copyout(pagetable_t, uint64, char *, uint64);
-int copyin(pagetable_t, char *, uint64, uint64);
-int copyinstr(pagetable_t, char *, uint64, uint64);
+void kvminit(void);
+void kvminithart(void);
+void kvmmap(pagetable_t, uint64, uint64, uint64, int);
+int mappages(pagetable_t, uint64, uint64, uint64, int);
+pagetable_t uvmcreate(void);
+void uvmfirst(pagetable_t, uchar *, uint);
+uint64 uvmalloc(pagetable_t, uint64, uint64, int);
+uint64 uvmdealloc(pagetable_t, uint64, uint64);
+int uvmcopy(pagetable_t, pagetable_t, uint64);
+void uvmfree(pagetable_t, uint64);
+void uvmunmap(pagetable_t, uint64, uint64, int);
+void uvmclear(pagetable_t, uint64);
+pte_t *walk(pagetable_t, uint64, int);
+uint64 walkaddr(pagetable_t, uint64);
+int copyout(pagetable_t, uint64, char *, uint64);
+int copyin(pagetable_t, char *, uint64, uint64);
+int copyinstr(pagetable_t, char *, uint64, uint64);
// plic.c
-void plicinit(void);
-void plicinithart(void);
-int plic_claim(void);
-void plic_complete(int);
+void plicinit(void);
+void plicinithart(void);
+int plic_claim(void);
+void plic_complete(int);
// virtio_disk.c
-void virtio_disk_init(void);
-void virtio_disk_rw(struct buf *, int);
-void virtio_disk_intr(void);
+void virtio_disk_init(void);
+void virtio_disk_rw(struct buf *, int);
+void virtio_disk_intr(void);
// number of elements in fixed-size array
-#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+#define NELEM(x) (sizeof(x) / sizeof((x)[0]))
diff --git a/kernel/history.h b/kernel/history.h
new file mode 100644
index 0000000..9c9dd27
--- /dev/null
+++ b/kernel/history.h
@@ -0,0 +1,17 @@
+#ifndef HISTORY
+#define HISTORY
+
+#include "syscall.h"
+#include "spinlock.h"
+
+struct syscall_stat
+{
+ char syscall_name[16];
+ int count;
+ int accum_time;
+};
+
+extern struct syscall_stat syscall_stats[NSYSCALLS + 1];
+extern struct spinlock syscall_stat_locks[NSYSCALLS + 1];
+
+#endif
\ No newline at end of file
diff --git a/kernel/kernelvec.S b/kernel/kernelvec.S
index fb31b32..2e52b9d 100644
--- a/kernel/kernelvec.S
+++ b/kernel/kernelvec.S
@@ -97,12 +97,17 @@ timervec:
# scratch[0,8,16] : register save area.
# scratch[24] : address of CLINT's MTIMECMP register.
# scratch[32] : desired interval between interrupts.
-
+ # scratch[40] : halt flag set by timerhalt.
+
csrrw a0, mscratch, a0
sd a1, 0(a0)
sd a2, 8(a0)
sd a3, 16(a0)
+ # halt if timerhalt has set halt flag to 1
+ ld a1, 40(a0)
+ bne a1, zero, halt
+
# schedule the next timer interrupt
# by adding interval to mtimecmp.
ld a1, 24(a0) # CLINT_MTIMECMP(hart)
@@ -122,3 +127,14 @@ timervec:
csrrw a0, mscratch, a0
mret
+
+halt:
+ # based on qemu's hw/riscv/virt.c:
+ # qemu halts if FINISHER_PASS(=0x5555) is
+ # written at address *VIRT_TEST(=0x100000L)
+ lui a1, 0x100
+ lui a2, 0x5
+ addi a2, a2, 0x555
+ sw a2, 0(a1)
+spin:
+ j spin
\ No newline at end of file
diff --git a/kernel/main.c b/kernel/main.c
index f0d3171..9fc888a 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -7,39 +7,42 @@
volatile static int started = 0;
// start() jumps here in supervisor mode on all CPUs.
-void
-main()
+void main()
{
- if(cpuid() == 0){
+ if (cpuid() == 0)
+ {
consoleinit();
printfinit();
printf("\n");
printf("xv6 kernel is booting\n");
printf("\n");
- kinit(); // physical page allocator
- kvminit(); // create kernel page table
- kvminithart(); // turn on paging
- procinit(); // process table
- trapinit(); // trap vectors
- trapinithart(); // install kernel trap vector
- plicinit(); // set up interrupt controller
- plicinithart(); // ask PLIC for device interrupts
- binit(); // buffer cache
- iinit(); // inode table
- fileinit(); // file table
+ historyinit(); // create syscall_stat for all syscalls
+ kinit(); // physical page allocator
+ kvminit(); // create kernel page table
+ kvminithart(); // turn on paging
+ procinit(); // process table
+ trapinit(); // trap vectors
+ trapinithart(); // install kernel trap vector
+ plicinit(); // set up interrupt controller
+ plicinithart(); // ask PLIC for device interrupts
+ binit(); // buffer cache
+ iinit(); // inode table
+ fileinit(); // file table
virtio_disk_init(); // emulated hard disk
- userinit(); // first user process
+ userinit(); // first user process
__sync_synchronize();
started = 1;
- } else {
- while(started == 0)
+ }
+ else
+ {
+ while (started == 0)
;
__sync_synchronize();
printf("hart %d starting\n", cpuid());
- kvminithart(); // turn on paging
- trapinithart(); // install kernel trap vector
- plicinithart(); // ask PLIC for device interrupts
+ kvminithart(); // turn on paging
+ trapinithart(); // install kernel trap vector
+ plicinithart(); // ask PLIC for device interrupts
}
- scheduler();
+ scheduler();
}
diff --git a/kernel/proc.c b/kernel/proc.c
index 959b778..b1a4894 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -29,40 +29,39 @@ struct spinlock wait_lock;
// Allocate a page for each process's kernel stack.
// Map it high in memory, followed by an invalid
// guard page.
-void
-proc_mapstacks(pagetable_t kpgtbl)
+void proc_mapstacks(pagetable_t kpgtbl)
{
struct proc *p;
-
- for(p = proc; p < &proc[NPROC]; p++) {
+
+ for (p = proc; p < &proc[NPROC]; p++)
+ {
char *pa = kalloc();
- if(pa == 0)
+ if (pa == 0)
panic("kalloc");
- uint64 va = KSTACK((int) (p - proc));
+ uint64 va = KSTACK((int)(p - proc));
kvmmap(kpgtbl, va, (uint64)pa, PGSIZE, PTE_R | PTE_W);
}
}
// initialize the proc table.
-void
-procinit(void)
+void procinit(void)
{
struct proc *p;
-
+
initlock(&pid_lock, "nextpid");
initlock(&wait_lock, "wait_lock");
- for(p = proc; p < &proc[NPROC]; p++) {
- initlock(&p->lock, "proc");
- p->state = UNUSED;
- p->kstack = KSTACK((int) (p - proc));
+ for (p = proc; p < &proc[NPROC]; p++)
+ {
+ initlock(&p->lock, "proc");
+ p->state = UNUSED;
+ p->kstack = KSTACK((int)(p - proc));
}
}
// Must be called with interrupts disabled,
// to prevent race with process being moved
// to a different CPU.
-int
-cpuid()
+int cpuid()
{
int id = r_tp();
return id;
@@ -70,7 +69,7 @@ cpuid()
// Return this CPU's cpu struct.
// Interrupts must be disabled.
-struct cpu*
+struct cpu *
mycpu(void)
{
int id = cpuid();
@@ -79,7 +78,7 @@ mycpu(void)
}
// Return the current struct proc *, or zero if none.
-struct proc*
+struct proc *
myproc(void)
{
push_off();
@@ -89,11 +88,10 @@ myproc(void)
return p;
}
-int
-allocpid()
+int allocpid()
{
int pid;
-
+
acquire(&pid_lock);
pid = nextpid;
nextpid = nextpid + 1;
@@ -106,16 +104,20 @@ allocpid()
// If found, initialize state required to run in the kernel,
// and return with p->lock held.
// If there are no free procs, or a memory allocation fails, return 0.
-static struct proc*
+static struct proc *
allocproc(void)
{
struct proc *p;
- for(p = proc; p < &proc[NPROC]; p++) {
+ for (p = proc; p < &proc[NPROC]; p++)
+ {
acquire(&p->lock);
- if(p->state == UNUSED) {
+ if (p->state == UNUSED)
+ {
goto found;
- } else {
+ }
+ else
+ {
release(&p->lock);
}
}
@@ -124,9 +126,11 @@ allocproc(void)
found:
p->pid = allocpid();
p->state = USED;
+ p->trace_sys_num = -1;
// Allocate a trapframe page.
- if((p->trapframe = (struct trapframe *)kalloc()) == 0){
+ if ((p->trapframe = (struct trapframe *)kalloc()) == 0)
+ {
freeproc(p);
release(&p->lock);
return 0;
@@ -134,7 +138,8 @@ found:
// An empty user page table.
p->pagetable = proc_pagetable(p);
- if(p->pagetable == 0){
+ if (p->pagetable == 0)
+ {
freeproc(p);
release(&p->lock);
return 0;
@@ -155,10 +160,10 @@ found:
static void
freeproc(struct proc *p)
{
- if(p->trapframe)
- kfree((void*)p->trapframe);
+ if (p->trapframe)
+ kfree((void *)p->trapframe);
p->trapframe = 0;
- if(p->pagetable)
+ if (p->pagetable)
proc_freepagetable(p->pagetable, p->sz);
p->pagetable = 0;
p->sz = 0;
@@ -180,23 +185,25 @@ proc_pagetable(struct proc *p)
// An empty page table.
pagetable = uvmcreate();
- if(pagetable == 0)
+ if (pagetable == 0)
return 0;
// map the trampoline code (for system call return)
// at the highest user virtual address.
// only the supervisor uses it, on the way
// to/from user space, so not PTE_U.
- if(mappages(pagetable, TRAMPOLINE, PGSIZE,
- (uint64)trampoline, PTE_R | PTE_X) < 0){
+ if (mappages(pagetable, TRAMPOLINE, PGSIZE,
+ (uint64)trampoline, PTE_R | PTE_X) < 0)
+ {
uvmfree(pagetable, 0);
return 0;
}
// map the trapframe page just below the trampoline page, for
// trampoline.S.
- if(mappages(pagetable, TRAPFRAME, PGSIZE,
- (uint64)(p->trapframe), PTE_R | PTE_W) < 0){
+ if (mappages(pagetable, TRAPFRAME, PGSIZE,
+ (uint64)(p->trapframe), PTE_R | PTE_W) < 0)
+ {
uvmunmap(pagetable, TRAMPOLINE, 1, 0);
uvmfree(pagetable, 0);
return 0;
@@ -207,8 +214,7 @@ proc_pagetable(struct proc *p)
// Free a process's page table, and free the
// physical memory it refers to.
-void
-proc_freepagetable(pagetable_t pagetable, uint64 sz)
+void proc_freepagetable(pagetable_t pagetable, uint64 sz)
{
uvmunmap(pagetable, TRAMPOLINE, 1, 0);
uvmunmap(pagetable, TRAPFRAME, 1, 0);
@@ -219,32 +225,30 @@ proc_freepagetable(pagetable_t pagetable, uint64 sz)
// assembled from ../user/initcode.S
// od -t xC ../user/initcode
uchar initcode[] = {
- 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02,
- 0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02,
- 0x93, 0x08, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00,
- 0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00,
- 0xef, 0xf0, 0x9f, 0xff, 0x2f, 0x69, 0x6e, 0x69,
- 0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
-};
+ 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02,
+ 0x97, 0x05, 0x00, 0x00, 0x93, 0x85, 0x35, 0x02,
+ 0x93, 0x08, 0x70, 0x00, 0x73, 0x00, 0x00, 0x00,
+ 0x93, 0x08, 0x20, 0x00, 0x73, 0x00, 0x00, 0x00,
+ 0xef, 0xf0, 0x9f, 0xff, 0x2f, 0x69, 0x6e, 0x69,
+ 0x74, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00};
// Set up first user process.
-void
-userinit(void)
+void userinit(void)
{
struct proc *p;
p = allocproc();
initproc = p;
-
+
// allocate one user page and copy initcode's instructions
// and data into it.
uvmfirst(p->pagetable, initcode, sizeof(initcode));
p->sz = PGSIZE;
// prepare for the very first "return" from kernel to user.
- p->trapframe->epc = 0; // user program counter
- p->trapframe->sp = PGSIZE; // user stack pointer
+ p->trapframe->epc = 0; // user program counter
+ p->trapframe->sp = PGSIZE; // user stack pointer
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
@@ -256,18 +260,21 @@ userinit(void)
// Grow or shrink user memory by n bytes.
// Return 0 on success, -1 on failure.
-int
-growproc(int n)
+int growproc(int n)
{
uint64 sz;
struct proc *p = myproc();
sz = p->sz;
- if(n > 0){
- if((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0) {
+ if (n > 0)
+ {
+ if ((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0)
+ {
return -1;
}
- } else if(n < 0){
+ }
+ else if (n < 0)
+ {
sz = uvmdealloc(p->pagetable, sz, sz + n);
}
p->sz = sz;
@@ -276,20 +283,21 @@ growproc(int n)
// Create a new process, copying the parent.
// Sets up child kernel stack to return as if from fork() system call.
-int
-fork(void)
+int fork(void)
{
int i, pid;
struct proc *np;
struct proc *p = myproc();
// Allocate process.
- if((np = allocproc()) == 0){
+ if ((np = allocproc()) == 0)
+ {
return -1;
}
// Copy user memory from parent to child.
- if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
+ if (uvmcopy(p->pagetable, np->pagetable, p->sz) < 0)
+ {
freeproc(np);
release(&np->lock);
return -1;
@@ -303,8 +311,8 @@ fork(void)
np->trapframe->a0 = 0;
// increment reference counts on open file descriptors.
- for(i = 0; i < NOFILE; i++)
- if(p->ofile[i])
+ for (i = 0; i < NOFILE; i++)
+ if (p->ofile[i])
np->ofile[i] = filedup(p->ofile[i]);
np->cwd = idup(p->cwd);
@@ -327,13 +335,14 @@ fork(void)
// Pass p's abandoned children to init.
// Caller must hold wait_lock.
-void
-reparent(struct proc *p)
+void reparent(struct proc *p)
{
struct proc *pp;
- for(pp = proc; pp < &proc[NPROC]; pp++){
- if(pp->parent == p){
+ for (pp = proc; pp < &proc[NPROC]; pp++)
+ {
+ if (pp->parent == p)
+ {
pp->parent = initproc;
wakeup(initproc);
}
@@ -343,17 +352,18 @@ reparent(struct proc *p)
// Exit the current process. Does not return.
// An exited process remains in the zombie state
// until its parent calls wait().
-void
-exit(int status)
+void exit(int status)
{
struct proc *p = myproc();
- if(p == initproc)
+ if (p == initproc)
panic("init exiting");
// Close all open files.
- for(int fd = 0; fd < NOFILE; fd++){
- if(p->ofile[fd]){
+ for (int fd = 0; fd < NOFILE; fd++)
+ {
+ if (p->ofile[fd])
+ {
struct file *f = p->ofile[fd];
fileclose(f);
p->ofile[fd] = 0;
@@ -372,7 +382,7 @@ exit(int status)
// Parent might be sleeping in wait().
wakeup(p->parent);
-
+
acquire(&p->lock);
p->xstate = status;
@@ -387,8 +397,7 @@ exit(int status)
// Wait for a child process to exit and return its pid.
// Return -1 if this process has no children.
-int
-wait(uint64 addr)
+int wait(uint64 addr)
{
struct proc *pp;
int havekids, pid;
@@ -396,20 +405,25 @@ wait(uint64 addr)
acquire(&wait_lock);
- for(;;){
+ for (;;)
+ {
// Scan through table looking for exited children.
havekids = 0;
- for(pp = proc; pp < &proc[NPROC]; pp++){
- if(pp->parent == p){
+ for (pp = proc; pp < &proc[NPROC]; pp++)
+ {
+ if (pp->parent == p)
+ {
// make sure the child isn't still in exit() or swtch().
acquire(&pp->lock);
havekids = 1;
- if(pp->state == ZOMBIE){
+ if (pp->state == ZOMBIE)
+ {
// Found one.
pid = pp->pid;
- if(addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate,
- sizeof(pp->xstate)) < 0) {
+ if (addr != 0 && copyout(p->pagetable, addr, (char *)&pp->xstate,
+ sizeof(pp->xstate)) < 0)
+ {
release(&pp->lock);
release(&wait_lock);
return -1;
@@ -424,13 +438,14 @@ wait(uint64 addr)
}
// No point waiting if we don't have any children.
- if(!havekids || killed(p)){
+ if (!havekids || killed(p))
+ {
release(&wait_lock);
return -1;
}
-
+
// Wait for a child to exit.
- sleep(p, &wait_lock); //DOC: wait-sleep
+ sleep(p, &wait_lock); // DOC: wait-sleep
}
}
@@ -441,20 +456,22 @@ wait(uint64 addr)
// - swtch to start running that process.
// - eventually that process transfers control
// via swtch back to the scheduler.
-void
-scheduler(void)
+void scheduler(void)
{
struct proc *p;
struct cpu *c = mycpu();
-
+
c->proc = 0;
- for(;;){
+ for (;;)
+ {
// Avoid deadlock by ensuring that devices can interrupt.
intr_on();
- for(p = proc; p < &proc[NPROC]; p++) {
+ for (p = proc; p < &proc[NPROC]; p++)
+ {
acquire(&p->lock);
- if(p->state == RUNNABLE) {
+ if (p->state == RUNNABLE)
+ {
// Switch to chosen process. It is the process's job
// to release its lock and then reacquire it
// before jumping back to us.
@@ -478,19 +495,18 @@ scheduler(void)
// be proc->intena and proc->noff, but that would
// break in the few places where a lock is held but
// there's no process.
-void
-sched(void)
+void sched(void)
{
int intena;
struct proc *p = myproc();
- if(!holding(&p->lock))
+ if (!holding(&p->lock))
panic("sched p->lock");
- if(mycpu()->noff != 1)
+ if (mycpu()->noff != 1)
panic("sched locks");
- if(p->state == RUNNING)
+ if (p->state == RUNNING)
panic("sched running");
- if(intr_get())
+ if (intr_get())
panic("sched interruptible");
intena = mycpu()->intena;
@@ -499,8 +515,7 @@ sched(void)
}
// Give up the CPU for one scheduling round.
-void
-yield(void)
+void yield(void)
{
struct proc *p = myproc();
acquire(&p->lock);
@@ -511,15 +526,15 @@ yield(void)
// A fork child's very first scheduling by scheduler()
// will swtch to forkret.
-void
-forkret(void)
+void forkret(void)
{
static int first = 1;
// Still holding p->lock from scheduler.
release(&myproc()->lock);
- if (first) {
+ if (first)
+ {
// File system initialization must be run in the context of a
// regular process (e.g., because it calls sleep), and thus cannot
// be run from main().
@@ -532,11 +547,10 @@ forkret(void)
// Atomically release lock and sleep on chan.
// Reacquires lock when awakened.
-void
-sleep(void *chan, struct spinlock *lk)
+void sleep(void *chan, struct spinlock *lk)
{
struct proc *p = myproc();
-
+
// Must acquire p->lock in order to
// change p->state and then call sched.
// Once we hold p->lock, we can be
@@ -544,7 +558,7 @@ sleep(void *chan, struct spinlock *lk)
// (wakeup locks p->lock),
// so it's okay to release lk.
- acquire(&p->lock); //DOC: sleeplock1
+ acquire(&p->lock); // DOC: sleeplock1
release(lk);
// Go to sleep.
@@ -563,15 +577,17 @@ sleep(void *chan, struct spinlock *lk)
// Wake up all processes sleeping on chan.
// Must be called without any p->lock.
-void
-wakeup(void *chan)
+void wakeup(void *chan)
{
struct proc *p;