-
Notifications
You must be signed in to change notification settings - Fork 0
/
part3.patch
1710 lines (1647 loc) · 56.7 KB
/
part3.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 09d790c..c7ae16a 100644
--- a/Makefile
+++ b/Makefile
@@ -71,12 +71,20 @@ QEMU = $(shell if which qemu > /dev/null; \
echo "***" 1>&2; exit 1)
endif
+ifndef VERBOSE_PRINT
+ VERBOSE_PRINT=FALSE
+endif
+ifndef SELECTION
+ SELECTION=SCFIFO
+endif
+
+
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
-CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
+CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer -D SELECTION=$(SELECTION) -D VERBOSE_PRINT=$(VERBOSE_PRINT)
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
# FreeBSD ld wants ``elf_i386_fbsd''
@@ -168,6 +176,7 @@ mkfs: mkfs.c fs.h
UPROGS=\
_cat\
_echo\
+ _ass3Tests\
_forktest\
_grep\
_init\
diff --git a/ass3Tests.c b/ass3Tests.c
new file mode 100644
index 0000000..38c482b
--- /dev/null
+++ b/ass3Tests.c
@@ -0,0 +1,252 @@
+#include "types.h"
+#include "stat.h"
+#include "user.h"
+
+//simple test, allocates 20 PGSIZE
+void test1()
+{
+ printf(1, "test1 START\n");
+ if (fork() == 0)
+ {
+ char *mem = sbrk(20 * 4096);
+ mem[5000] = 0;
+ mem[4069 * 20] = 0;
+ sleep(100);
+ exit();
+ }
+ wait();
+ printf(1, "test1 END\n\n");
+};
+
+//panics because of 32 Pages limit
+void test2()
+{
+ printf(1, "test2 START\n");
+ if (fork() == 0)
+ sbrk(32 * 4096);
+ printf(1, "test2 END\n\n"); //not reached
+}
+
+//retrieves page from swap file and checks the string we wrote if its as the original
+void test3(char *str)
+{
+ printf(1, "test3 START\n");
+ if (fork() == 0)
+ {
+ char *mem = sbrk(4096);
+ memmove(mem, str, strlen(str));
+ sbrk(20 * 4096);
+ printf(1, "%s === %s\n", str, mem);
+ exit();
+ }
+ wait();
+ printf(1, "test3 END\n\n");
+}
+
+//
+void test4(){
+ printf(1, "test4 START\n");
+ if(fork()==0){
+ sbrk(23*4096);
+ if (fork() == 0)
+ exit();
+ wait();
+ exit();
+ }
+ wait();
+ printf(1, "test4 END\n\n");
+}
+
+//floods paging algorithm, use to compare algorithms
+void test5()
+{
+ printf(1, "test5 START\n");
+ if (fork() == 0)
+ {
+ char *mem = sbrk(25 * 4096); //accessing 25*PGSIZE causes trap!
+ mem[0 * 4096] = 9;
+ mem[1 * 4096] = 9;
+ mem[15 * 4096] = 9;
+ mem[3 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ mem[24 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ mem[6 * 4096] = 9;
+ mem[7 * 4096] = 9;
+ mem[8 * 4096] = 9;
+ mem[9 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ mem[3 * 4096] = 9;
+ mem[18 * 4096] = 9;
+ mem[21 * 4096] = 9;
+ mem[12 * 4096] = 9;
+ mem[24 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ mem[19 * 4096] = 9;
+ mem[14 * 4096] = 9;
+ mem[16 * 4096] = 9;
+ mem[23 * 4096] = 9;
+ mem[2 * 4096] = 9;
+ mem[13 * 4096] = 9;
+ mem[17 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ mem[22 * 4096] = 9;
+ mem[6 * 4096] = 9;
+ mem[9 * 4096] = 9;
+ mem[1 * 4096] = 9;
+ exit();
+ }
+ wait();
+ printf(1, "test5 END\n\n");
+}
+
+//floods the system with swappings, writes strings into pages and retrieves them
+void test6(char *str1, char *str2, char *str3, char *str4, char *str5)
+{
+ printf(1, "test6 START\n");
+ if (fork() == 0)
+ {
+ char *mem = sbrk(25 * 4096); //accessing 25*PGSIZE causes trap!
+ memmove(&mem[1000], str1, strlen(str1));
+ mem[0 * 4096] = 9;
+ mem[1 * 4096] = 9;
+ mem[15 * 4096] = 9;
+ mem[3 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ memmove(&mem[5000], str2, strlen(str2));
+ mem[24 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ mem[6 * 4096] = 9;
+ mem[7 * 4096] = 9;
+ mem[8 * 4096] = 9;
+ mem[9 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ memmove(&mem[10000], str3, strlen(str3));
+ mem[3 * 4096] = 9;
+ mem[18 * 4096] = 9;
+ mem[21 * 4096] = 9;
+ mem[12 * 4096] = 9;
+ mem[24 * 4096] = 9;
+ mem[0 * 4096] = 9;
+ mem[19 * 4096] = 9;
+ mem[14 * 4096] = 9;
+ memmove(&mem[15000], str4, strlen(str4));
+ mem[16 * 4096] = 9;
+ mem[23 * 4096] = 9;
+ mem[2 * 4096] = 9;
+ memmove(&mem[20000], str5, strlen(str5));
+ mem[13 * 4096] = 9;
+ mem[17 * 4096] = 9;
+ mem[20 * 4096] = 9;
+ mem[22 * 4096] = 9;
+ mem[6 * 4096] = 9;
+ mem[9 * 4096] = 9;
+ mem[1 * 4096] = 9;
+ printf(1, "Strings: %s %s %s %s %s\n", &mem[1000], &mem[5000], &mem[10000], &mem[15000], &mem[20000]);
+ exit();
+ }
+ wait();
+ printf(1, "test6 END\n\n");
+}
+
+//copying pages into children
+void test7()
+{
+ printf(1, "test7 START\n");
+ if (fork() == 0)
+ {
+ sbrk(20*4096);
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ printf(1, "test7 END\n\n");
+}
+
+//copy a swapped file to child's and print content (content should equal str)
+void test8(char* str){
+ printf(1, "test8 START\n");
+ if(fork()==0){
+ char *mem=sbrk(1*4096);
+ sbrk(20*4096);
+ memmove(mem,str,strlen(str));
+ if (fork() == 0){
+ printf(1,"child print:%s\n",mem);
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ printf(1, "test8 END\n\n");
+}
+
+//pass 20 pages to numerous children processes, print str from leaf child through allocated pages
+void test9(char* str)
+{
+ printf(1, "test9 START\n");
+ if (fork() == 0)
+ {
+ char* mem=sbrk(20*4096);
+ memmove(mem,str,strlen(str));
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ if (fork() == 0)
+ {
+ printf(1,"last fork prints:%s\n",mem);
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ exit();
+ }
+ wait();
+ printf(1, "test9 END\n\n");
+}
+
+int main()
+{
+ test1();
+ //test2(); careful: panics!
+ test3("trololololo");
+ test4();
+ test5();
+ test6("hanna", "einas", "nitzan", "roaaya", "os202");
+ test7();
+ test8("assignment3");
+ test9("testting");
+ exit();
+}
diff --git a/defs.h b/defs.h
index be45622..6b85bdb 100644
--- a/defs.h
+++ b/defs.h
@@ -1,3 +1,12 @@
+#define NONE 0
+#define NFUA 1
+#define LAPA 2
+#define SCFIFO 3
+#define AQ 4
+int PagingMode;
+#define TRUE 5
+#define FALSE 6
+
struct buf;
struct context;
struct file;
@@ -10,6 +19,9 @@ struct sleeplock;
struct stat;
struct superblock;
+struct pagestat;
+struct pagingAlgorithm;
+
// bio.c
void binit(void);
struct buf* bread(uint, uint);
@@ -78,6 +90,10 @@ char* kalloc(void);
void kfree(char*);
void kinit1(void*, void*);
void kinit2(void*, void*);
+int getRefCount(uint pa); //task2
+void incrementRefCount(uint pa); //task2
+int countFreePages(void); //task4
+void decrementRefCount(uint pa);
// kbd.c
void kbdintr(void);
@@ -130,6 +146,7 @@ void userinit(void);
int wait(void);
void wakeup(void*);
void yield(void);
+void freePageStatus(struct proc*);
// swtch.S
void swtch(struct context**, struct context*);
@@ -195,6 +212,11 @@ void switchuvm(struct proc*);
void switchkvm(void);
int copyout(pde_t*, uint, void*, uint);
void clearpteu(pde_t *pgdir, char *uva);
+void movePageToSwapFile(struct proc*);
+pde_t* efficentCopyuvm(pde_t *pgdir, uint sz, struct proc* );
+int COWHandler(pde_t *pdgir, uint va);
+void updatePagingInfo(void);
+int retrievePage(uint address,int pageNumber);
// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
diff --git a/exec.c b/exec.c
index b40134f..88b3e86 100644
--- a/exec.c
+++ b/exec.c
@@ -7,6 +7,18 @@
#include "x86.h"
#include "elf.h"
+void copyBack(struct pagestat pstat, struct proc* proc){
+ proc->pagestatus.swapPages=pstat.swapPages;
+ proc->pagestatus.RAMpages=pstat.RAMpages;
+ proc->pagestatus.swapFileOffset=pstat.swapFileOffset;
+ for(int i=0; i<16; i++){
+ proc->pagestatus.pagingAlgo[i].address=pstat.pagingAlgo[i].address;
+ proc->pagestatus.pagingAlgo[i].counter=pstat.pagingAlgo[i].counter;
+ proc->pagestatus.pagingAlgo[i].writeFlag=pstat.pagingAlgo[i].writeFlag;
+ proc->pagestatus.swapAddresses[i]=pstat.swapAddresses[i];
+ }
+};
+
int
exec(char *path, char **argv)
{
@@ -18,6 +30,20 @@ exec(char *path, char **argv)
struct proghdr ph;
pde_t *pgdir, *oldpgdir;
struct proc *curproc = myproc();
+ //cprintf("entering exec pid:%d\n\n",curproc->pid);
+
+ struct pagestat copyOfCurrentPage;
+ //copy the current pagestatus of myproc in order to retrive when Something wrong
+ copyOfCurrentPage.swapPages=myproc()->pagestatus.swapPages;
+ copyOfCurrentPage.RAMpages= myproc()->pagestatus.RAMpages;
+ copyOfCurrentPage.swapFileOffset=myproc()->pagestatus.swapFileOffset;
+ for(int i=0; i<16; i++){
+ copyOfCurrentPage.pagingAlgo[i].address=myproc()->pagestatus.pagingAlgo[i].address;
+ copyOfCurrentPage.pagingAlgo[i].counter=myproc()->pagestatus.pagingAlgo[i].counter;
+ copyOfCurrentPage.swapAddresses[i]=myproc()->pagestatus.swapAddresses[i];
+ copyOfCurrentPage.pagingAlgo[i].writeFlag=myproc()->pagestatus.pagingAlgo[i].writeFlag;
+ }
+ freePageStatus(myproc());
begin_op();
@@ -38,6 +64,8 @@ exec(char *path, char **argv)
if((pgdir = setupkvm()) == 0)
goto bad;
+
+
// Load program into memory.
sz = 0;
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
@@ -101,9 +129,11 @@ exec(char *path, char **argv)
curproc->tf->esp = sp;
switchuvm(curproc);
freevm(oldpgdir);
+ //copyBack(copyOfCurrentPage,myproc()); //destroys RAM-Pages counter if restored, shouldn't be done!!
return 0;
bad:
+ copyBack(copyOfCurrentPage,myproc());
if(pgdir)
freevm(pgdir);
if(ip){
diff --git a/kalloc.c b/kalloc.c
index 14cd4f4..dd0834b 100644
--- a/kalloc.c
+++ b/kalloc.c
@@ -21,6 +21,7 @@ struct {
struct spinlock lock;
int use_lock;
struct run *freelist;
+ uint refCount[PHYSTOP >> 12]; //array for COW implement, every Page has counter to how many hold it (total pages in system = PHYSTOP/PGSIZE)
} kmem;
// Initialization happens in two phases.
@@ -48,8 +49,10 @@ freerange(void *vstart, void *vend)
{
char *p;
p = (char*)PGROUNDUP((uint)vstart);
- for(; p + PGSIZE <= (char*)vend; p += PGSIZE)
+ for(; p + PGSIZE <= (char*)vend; p += PGSIZE){
+ kmem.refCount[V2P(p) >> 12]=0; //init all refCounters to zero
kfree(p);
+ }
}
//PAGEBREAK: 21
// Free the page of physical memory pointed at by v,
@@ -64,14 +67,18 @@ kfree(char *v)
if((uint)v % PGSIZE || v < end || V2P(v) >= PHYSTOP)
panic("kfree");
- // Fill with junk to catch dangling refs.
- memset(v, 1, PGSIZE);
+ //removed
if(kmem.use_lock)
acquire(&kmem.lock);
- r = (struct run*)v;
- r->next = kmem.freelist;
- kmem.freelist = r;
+ if(kmem.refCount[V2P(v) >> 12]>0) //if process kfrees(page), dec counter if refCounter>0
+ kmem.refCount[V2P(v) >> 12]-=1;
+ if(kmem.refCount[V2P(v) >> 12]==0){ //decrementing the refCounter happens in vm.c (using decerementRefCount())
+ memset(v, 1, PGSIZE); //fill with junk (refCounter=0 page will be freed indeed)
+ r = (struct run*)v;
+ r->next = kmem.freelist;
+ kmem.freelist = r;
+ }
if(kmem.use_lock)
release(&kmem.lock);
}
@@ -87,10 +94,54 @@ kalloc(void)
if(kmem.use_lock)
acquire(&kmem.lock);
r = kmem.freelist;
- if(r)
+ if(r){
kmem.freelist = r->next;
+ kmem.refCount[V2P((char*)r) >> 12]=1; //first kalloc, increment once
+ }
if(kmem.use_lock)
release(&kmem.lock);
return (char*)r;
}
+//set of functions to support refCounter for pages (used in vm.c, proc.c)
+//lock kmem struct because we have multiple CPUs running (prevents errors in COWhandler -> vm.c)
+
+//get refCounter for specific page (using pa=physical address)
+int getRefCount(uint pa){
+ return kmem.refCount[pa >> 12];
+}
+
+//increment refCoutner for physical address pa
+void incrementRefCount(uint pa){
+ if(kmem.use_lock)
+ acquire(&kmem.lock);
+ kmem.refCount[pa >> 12]+=1;
+ if(kmem.use_lock)
+ release(&kmem.lock);
+}
+
+//decrement refCoutner for physical address pa
+void decrementRefCount(uint pa){
+ if(kmem.use_lock)
+ acquire(&kmem.lock);
+ kmem.refCount[pa >> 12]-=1;
+ if(kmem.use_lock)
+ release(&kmem.lock);
+}
+
+//expensive function, counts how many free pages in system
+int countFreePages(){
+ int ans=0;
+ for(int i=0; i<(PHYSTOP>>12); i+=1){
+ if(kmem.use_lock)
+ acquire(&kmem.lock);
+ if(kmem.refCount[i]==0)
+ ans++;
+ if(kmem.use_lock)
+ release(&kmem.lock);
+ }
+ return ans;
+}
+
+
+
diff --git a/mmu.h b/mmu.h
index a82d8e2..72183af 100644
--- a/mmu.h
+++ b/mmu.h
@@ -95,6 +95,9 @@ struct segdesc {
#define PTE_W 0x002 // Writeable
#define PTE_U 0x004 // User
#define PTE_PS 0x080 // Page Size
+#define PTE_A 0x020 // Accessed (Task 1)
+#define PTE_PG 0x200 // SWAP FILE 10th bit (ON if page in swapfile)
+#define PTE_READONLY 0x400 // the 11th bit
// Address in page table or page directory entry
#define PTE_ADDR(pte) ((uint)(pte) & ~0xFFF)
diff --git a/proc.c b/proc.c
index 806b1b1..120cab2 100644
--- a/proc.c
+++ b/proc.c
@@ -7,6 +7,9 @@
#include "proc.h"
#include "spinlock.h"
+extern int PagingMode; //global variable for paging mode (assigned according to SELECTION flag) defined in defs.h
+int Verbose; //global variable for VERBOSE_PRINT debugging
+
struct {
struct spinlock lock;
struct proc proc[NPROC];
@@ -20,6 +23,32 @@ extern void trapret(void);
static void wakeup1(void *chan);
+//check if swap file is empty return -1, otherwise returns i>=0
+int isSwapFileEmpty(){
+ for(int i=0; i<16; i++)
+ if(myproc()->pagestatus.swapAddresses[i]!=-1)
+ return i;
+ return -1;
+}
+
+//clears pagestatus field for process p
+void freePageStatus(struct proc* p){
+ p->pagestatus.numOfPageFaults=0;
+ p->pagestatus.swapFileOffset=0;
+ p->pagestatus.totalSwapPages=0;
+ p->pagestatus.RAMpages=0;
+ p->pagestatus.swapPages=0;
+ for(int i=0; i<16; i++){
+ p->pagestatus.swapAddresses[i]=-1;
+ p->pagestatus.pagingAlgo[i].address=-1;
+ p->pagestatus.pagingAlgo[i].writeFlag=0;
+ if(PagingMode==LAPA)
+ p->pagestatus.pagingAlgo[i].counter=0xFFFFFFFF;
+ else
+ p->pagestatus.pagingAlgo[i].counter=0;
+ }
+}
+
void
pinit(void)
{
@@ -89,8 +118,20 @@ found:
p->state = EMBRYO;
p->pid = nextpid++;
- release(&ptable.lock);
-
+ p->pagestatus.RAMpages=0; //init all pagestatus fields
+ p->pagestatus.swapPages=0;
+ p->pagestatus.swapFileOffset=0;
+ p->pagestatus.numOfPageFaults=0;
+ p->pagestatus.totalSwapPages=0;
+ for(int i=0; i<16; i++){
+ p->pagestatus.swapAddresses[i]=-1;
+ p->pagestatus.pagingAlgo[i].address=-1;
+ p->pagestatus.pagingAlgo[i].counter=0;
+ p->pagestatus.pagingAlgo[i].writeFlag=0;
+ }
+
+ release(&ptable.lock);
+
// Allocate kernel stack.
if((p->kstack = kalloc()) == 0){
p->state = UNUSED;
@@ -151,6 +192,38 @@ userinit(void)
p->state = RUNNABLE;
release(&ptable.lock);
+
+ //if not defined, PagingMode=SCFIFO
+ #ifndef SELECTION
+ PagingMode=SCFIFO;
+ #endif
+ #if SELECTION==NFUA
+ PagingMode=NFUA;
+ #endif
+ #if SELECTION==NONE
+ PagingMode=NONE;
+ #endif
+ #if SELECTION==SCFIFO
+ PagingMode=SCFIFO;
+ #endif
+ #if SELECTION==AQ
+ PagingMode=AQ;
+ #endif
+ #if SELECTION==LAPA
+ PagingMode=LAPA;
+ #endif
+ #if VERBOSE_PRINT==TRUE
+ Verbose=1;
+ #endif
+ #if VERBOSE_PRINT==FALSE
+ Verbose=0;
+ #endif
+
+ //Turn on to check VERBOSE_PRINT and SELECTION flags
+
+ //cprintf("VERBOSE%d\n",Verbose);
+ //cprintf("PAGINGMODE%d\n",PagingMode);
+
}
// Grow current process's memory by n bytes.
@@ -188,18 +261,33 @@ fork(void)
if((np = allocproc()) == 0){
return -1;
}
-
- // Copy process state from proc.
- if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){
- kfree(np->kstack);
- np->kstack = 0;
- np->state = UNUSED;
- return -1;
+
+ if(np->pid>2)
+ createSwapFile(np);
+ //Copy process state from proc.
+ //COW implement doesn't include sh and init (pid => 1,2)
+ if(curproc->pid>2 && (PagingMode!=NONE)){
+ if((np->pgdir = efficentCopyuvm(curproc->pgdir, curproc->sz, np)) == 0){
+ kfree(np->kstack);
+ np->kstack = 0;
+ np->state = UNUSED;
+ return -1;
+ }
}
+ else
+ { //if pid=1 || pid=2, do regular copyuvm
+ if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){
+ kfree(np->kstack);
+ np->kstack = 0;
+ np->state = UNUSED;
+ return -1;
+ }
+ }
+
np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;
-
+
// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;
@@ -227,6 +315,15 @@ fork(void)
void
exit(void)
{
+ static char *states[] = {
+ [UNUSED] "unused",
+ [EMBRYO] "embryo",
+ [SLEEPING] "sleep ",
+ [RUNNABLE] "runble",
+ [RUNNING] "run ",
+ [ZOMBIE] "zombie"
+ };
+
struct proc *curproc = myproc();
struct proc *p;
int fd;
@@ -246,6 +343,7 @@ exit(void)
iput(curproc->cwd);
end_op();
curproc->cwd = 0;
+ removeSwapFile(curproc); //remove swapFIle before exiting
acquire(&ptable.lock);
@@ -260,9 +358,14 @@ exit(void)
wakeup1(initproc);
}
}
-
// Jump into the scheduler, never to return.
curproc->state = ZOMBIE;
+ //if VERBOSE_PRINT=TRUE, print proper information for current process
+ if(Verbose==1){
+ cprintf("%d %s <%d> <%d> <%d> <%d> %s\n", myproc()->pid, states[myproc()->state],myproc()->pagestatus.RAMpages,
+ myproc()->pagestatus.swapPages,myproc()->pagestatus.numOfPageFaults,myproc()->pagestatus.totalSwapPages, myproc()->name);
+ cprintf("%d / %d free page frames in the system\n\n",countFreePages(),PHYSTOP>>12);
+ }
sched();
panic("zombie exit");
}
@@ -287,9 +390,11 @@ wait(void)
if(p->state == ZOMBIE){
// Found one.
pid = p->pid;
+ freePageStatus(p); //free page status struct
kfree(p->kstack);
p->kstack = 0;
freevm(p->pgdir);
+ p->pgdir = 0;
p->pid = 0;
p->parent = 0;
p->name[0] = 0;
@@ -342,10 +447,11 @@ scheduler(void)
c->proc = p;
switchuvm(p);
p->state = RUNNING;
-
swtch(&(c->scheduler), p->context);
switchkvm();
-
+ if(PagingMode!=NONE && p->pid>2) //update if PagingMode!=none and for processes with pid bigger than 2
+ updatePagingInfo();
+
// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
@@ -396,6 +502,7 @@ yield(void)
void
forkret(void)
{
+
static int first = 1;
// Still holding ptable.lock from scheduler.
release(&ptable.lock);
@@ -515,6 +622,7 @@ procdump(void)
struct proc *p;
char *state;
uint pc[10];
+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state == UNUSED)
@@ -523,7 +631,8 @@ procdump(void)
state = states[p->state];
else
state = "???";
- cprintf("%d %s %s", p->pid, state, p->name);
+ //print proper info on ^P launch
+ cprintf("%d %s <%d> <%d> <%d> <%d> %s", p->pid, state,p->pagestatus.RAMpages,p->pagestatus.swapPages,p->pagestatus.numOfPageFaults,p->pagestatus.totalSwapPages, p->name);
if(p->state == SLEEPING){
getcallerpcs((uint*)p->context->ebp+2, pc);
for(i=0; i<10 && pc[i] != 0; i++)
@@ -531,4 +640,7 @@ procdump(void)
}
cprintf("\n");
}
+ //same as line 654
+ cprintf("%d / %d free page frames in the system\n",countFreePages(),PHYSTOP>>12 );
}
+
diff --git a/proc.h b/proc.h
index f8d364e..60b8149 100644
--- a/proc.h
+++ b/proc.h
@@ -32,6 +32,22 @@ struct context {
uint eip;
};
+struct pagingAlgorithm {
+ uint counter; //counter used for LAPA/NFUA
+ uint address; //virtual address of page in RAM
+ uint writeFlag; //original PTE_W for COW implement
+};
+
+struct pagestat {
+ int RAMpages; //process's RAM pages
+ int swapPages; //process's swap pages
+ int swapFileOffset; //offset in swapFile (to know where to write) not needed!
+ uint swapAddresses[16]; //addresses of pages in swapFile (virtual)
+ struct pagingAlgorithm pagingAlgo[16]; //struct to maintain paging algorithms
+ uint numOfPageFaults; //total number of page faults for current process
+ uint totalSwapPages; //total number of swapped pages for current process
+};
+
enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state
@@ -51,6 +67,7 @@ struct proc {
char name[16]; // Process name (debugging)
//Swap file. must initiate with create swap file
struct file *swapFile; //page file
+ struct pagestat pagestatus; //Task 1, file swapping struct helper
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/trap.c b/trap.c
index 41c66eb..ea04694 100644
--- a/trap.c
+++ b/trap.c
@@ -14,6 +14,16 @@ extern uint vectors[]; // in vectors.S: array of 256 entry pointers
struct spinlock tickslock;
uint ticks;
+extern int retrievePage(uint address, int pageNumber);
+extern void updatePagingInfo();
+
+int inSwapFile(uint address){
+ for(int i=0; i<16; i++)
+ if(myproc()->pagestatus.swapAddresses[i]==address)
+ return i;
+ return -1;
+}
+
void
tvinit(void)
{
@@ -77,8 +87,22 @@ trap(struct trapframe *tf)
cpuid(), tf->cs, tf->eip);
lapiceoi();
break;
+ case T_PGFLT:
+ myproc()->pagestatus.numOfPageFaults+=1;
+ updatePagingInfo(); //update all counters for paging algorithms
+ if(inSwapFile(PTE_ADDR(rcr2()))>=0)
+ if(retrievePage(PTE_ADDR(rcr2()),inSwapFile(PTE_ADDR(rcr2())))==0)
+ break;
+ else
+ goto error;
+ else
+ if(COWHandler(myproc()->pgdir,PTE_ADDR(rcr2()))==0)
+ break;
+ else
+ goto error;
//PAGEBREAK: 13
+ error:
default:
if(myproc() == 0 || (tf->cs&3) == 0){
// In kernel, it must be our mistake.
diff --git a/types.h b/types.h
index e4adf64..9a30e4d 100644
--- a/types.h
+++ b/types.h
@@ -2,3 +2,4 @@ typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef uint pde_t;
+
diff --git a/vm.c b/vm.c
index 7134cff..77ec68c 100644
--- a/vm.c
+++ b/vm.c
@@ -7,13 +7,12 @@
#include "proc.h"
#include "elf.h"
-extern char data[]; // defined by kernel.ld
-pde_t *kpgdir; // for use in scheduler()
+extern char data[]; // defined by kernel.ld
+pde_t *kpgdir; // for use in scheduler()
// Set up CPU's kernel segment descriptors.
// Run once on entry on each CPU.
-void
-seginit(void)
+void seginit(void)
{
struct cpu *c;
@@ -22,9 +21,9 @@ seginit(void)
// because it would have to have DPL_USR, but the CPU forbids
// an interrupt from CPL=0 to DPL=3.
c = &cpus[cpuid()];
- c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
+ c->gdt[SEG_KCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, 0);
c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
- c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER);
+ c->gdt[SEG_UCODE] = SEG(STA_X | STA_R, 0, 0xffffffff, DPL_USER);
c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER);
lgdt(c->gdt, sizeof(c->gdt));
}
@@ -39,10 +38,13 @@ walkpgdir(pde_t *pgdir, const void *va, int alloc)
pte_t *pgtab;
pde = &pgdir[PDX(va)];
- if(*pde & PTE_P){
- pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
- } else {
- if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
+ if (*pde & PTE_P)
+ {
+ pgtab = (pte_t *)P2V(PTE_ADDR(*pde));
+ }
+ else
+ {
+ if (!alloc || (pgtab = (pte_t *)kalloc()) == 0)
return 0;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
@@ -63,15 +65,16 @@ mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
char *a, *last;
pte_t *pte;
- a = (char*)PGROUNDDOWN((uint)va);
- last = (char*)PGROUNDDOWN(((uint)va) + size - 1);
- for(;;){
- if((pte = walkpgdir(pgdir, a, 1)) == 0)
+ a = (char *)PGROUNDDOWN((uint)va);
+ last = (char *)PGROUNDDOWN(((uint)va) + size - 1);
+ for (;;)
+ {
+ if ((pte = walkpgdir(pgdir, a, 1)) == 0)
return -1;
- if(*pte & PTE_P)
+ if (*pte & PTE_P)
panic("remap");
*pte = pa | perm | PTE_P;
- if(a == last)
+ if (a == last)
break;
a += PGSIZE;
pa += PGSIZE;
@@ -79,6 +82,7 @@ mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
return 0;
}
+
// There is one page table per process, plus one that's used when
// a CPU is not running any process (kpgdir). The kernel uses the
// current process's page table during system calls and interrupts;
@@ -102,33 +106,35 @@ mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
// This table defines the kernel's mappings, which are present in
// every process's page table.
-static struct kmap {
+static struct kmap
+{
void *virt;
uint phys_start;
uint phys_end;
int perm;
} kmap[] = {
- { (void*)KERNBASE, 0, EXTMEM, PTE_W}, // I/O space
- { (void*)KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kern text+rodata
- { (void*)data, V2P(data), PHYSTOP, PTE_W}, // kern data+memory
- { (void*)DEVSPACE, DEVSPACE, 0, PTE_W}, // more devices
+ {(void *)KERNBASE, 0, EXTMEM, PTE_W}, // I/O space
+ {(void *)KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kern text+rodata
+ {(void *)data, V2P(data), PHYSTOP, PTE_W}, // kern data+memory
+ {(void *)DEVSPACE, DEVSPACE, 0, PTE_W}, // more devices
};
// Set up kernel part of a page table.
-pde_t*
+pde_t *
setupkvm(void)
{
pde_t *pgdir;
struct kmap *k;
- if((pgdir = (pde_t*)kalloc()) == 0)
+ if ((pgdir = (pde_t *)kalloc()) == 0)
return 0;
memset(pgdir, 0, PGSIZE);
- if (P2V(PHYSTOP) > (void*)DEVSPACE)
+ if (P2V(PHYSTOP) > (void *)DEVSPACE)
panic("PHYSTOP too high");
- for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
- if(mappages(pgdir, k->virt, k->phys_end - k->phys_start,
- (uint)k->phys_start, k->perm) < 0) {
+ for (k = kmap; k < &kmap[NELEM(kmap)]; k++)
+ if (mappages(pgdir, k->virt, k->phys_end - k->phys_start,
+ (uint)k->phys_start, k->perm) < 0)
+ {
freevm(pgdir);
return 0;