-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass3.patch
866 lines (842 loc) · 22.7 KB
/
ass3.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
diff --git a/Makefile b/Makefile
index 09d790c..d65bc04 100644
--- a/Makefile
+++ b/Makefile
@@ -70,7 +70,12 @@ QEMU = $(shell if which qemu > /dev/null; \
echo "*** or have you tried setting the QEMU variable in Makefile?" 1>&2; \
echo "***" 1>&2; exit 1)
endif
-
+ifndef SELECTION
+ SELECTION=SCFIFO
+endif
+ifndef VERBOSE_PRINT
+ VERBOSE_PRINT = FALSE
+endif
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
@@ -78,6 +83,7 @@ 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 += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
+CFLAGS += -D$(SELECTION) -D$(VERBOSE_PRINT)
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
# FreeBSD ld wants ``elf_i386_fbsd''
LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null | head -n 1)
@@ -181,6 +187,7 @@ UPROGS=\
_usertests\
_wc\
_zombie\
+ _sanity\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
@@ -251,7 +258,7 @@ EXTRA=\
mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
printf.c umalloc.c\
- README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
+ README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list sanity\
.gdbinit.tmpl gdbutil\
dist:
diff --git a/defs.h b/defs.h
index be45622..02897ad 100644
--- a/defs.h
+++ b/defs.h
@@ -186,7 +186,7 @@ void kvmalloc(void);
pde_t* setupkvm(void);
char* uva2ka(pde_t*, char*);
int allocuvm(pde_t*, uint, uint);
-int deallocuvm(pde_t*, uint, uint);
+int deallocuvm(pde_t*, uint, uint, int);
void freevm(pde_t*);
void inituvm(pde_t*, char*, uint);
int loaduvm(pde_t*, char*, struct inode*, uint, uint);
@@ -195,6 +195,9 @@ void switchuvm(struct proc*);
void switchkvm(void);
int copyout(pde_t*, uint, void*, uint);
void clearpteu(pde_t *pgdir, char *uva);
-
+void swapToFile(pde_t *pgdir, struct proc* p);
+void handle_fault(uint va);
+void maintainAQ();
+void maintainAge();
// 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..3d0dd48 100644
--- a/exec.c
+++ b/exec.c
@@ -28,7 +28,29 @@ exec(char *path, char **argv)
}
ilock(ip);
pgdir = 0;
-
+ //MYCODE
+#if (defined(SCFIFO) || defined(NFUA) || defined(AQ) || defined(LAPA))
+ if(curproc->pid > isRelevant)
+ {
+ curproc->pageFaultCounter=0;
+ curproc->pagedOutCounter=0;
+ curproc->totalPagedOutCounter=0;
+ curproc->allocatedPagesCounter=0;
+ for(int i=0;i<MAX_TOTAL_PAGES;i++)
+ {
+ curproc->pages[i].used = 0;
+ curproc->pages[i].va = -1;
+ curproc->pages[i].isInRam = 0;
+ curproc->pages[i].swapOffset = -1;
+ curproc->pages[i].age = 0;
+ if(i < MAX_PSYC_PAGES)
+ {
+ curproc->ramPages[i] = -1;
+ }
+ }
+ }
+#endif
+ // /MYCODE
// Check ELF header
if(readi(ip, (char*)&elf, 0, sizeof(elf)) != sizeof(elf))
goto bad;
@@ -99,6 +121,15 @@ exec(char *path, char **argv)
curproc->sz = sz;
curproc->tf->eip = elf.entry; // main
curproc->tf->esp = sp;
+ // MYCODE
+#if (defined(SCFIFO) || defined(NFUA) || defined(AQ) || defined(LAPA))
+ if(curproc->pid>isRelevant)
+ {
+ removeSwapFile(curproc);
+ createSwapFile(curproc);
+ }
+#endif
+ // /MYCODE
switchuvm(curproc);
freevm(oldpgdir);
return 0;
diff --git a/mmu.h b/mmu.h
index a82d8e2..7563d65 100644
--- a/mmu.h
+++ b/mmu.h
@@ -1,6 +1,10 @@
// This file contains definitions for the
// x86 memory management unit (MMU).
+// MY_CODE
+#define PTE_PG 0x200 // Paged out to secondary storage
+
+// /MY_CODE
// Eflags register
#define FL_IF 0x00000200 // Interrupt Enable
@@ -95,6 +99,7 @@ struct segdesc {
#define PTE_W 0x002 // Writeable
#define PTE_U 0x004 // User
#define PTE_PS 0x080 // Page Size
+#define PTE_A 0x020 // Accessed
// Address in page table or page directory entry
#define PTE_ADDR(pte) ((uint)(pte) & ~0xFFF)
diff --git a/param.h b/param.h
index a7e90ef..e09a3f2 100644
--- a/param.h
+++ b/param.h
@@ -12,3 +12,6 @@
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache
#define FSSIZE 1000 // size of file system in blocks
+#define MAX_PSYC_PAGES 16
+#define MAX_TOTAL_PAGES 32
+#define isRelevant 2
\ No newline at end of file
diff --git a/proc.c b/proc.c
index 806b1b1..a166395 100644
--- a/proc.c
+++ b/proc.c
@@ -111,7 +111,31 @@ found:
p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint)forkret;
+ // MYCODE
+ p->currentOffset = 0;
+ p->pagedOutCounter = 0;
+ p->totalPagedOutCounter = 0;
+ p->allocatedPagesCounter = 0;
+ p->pageFaultCounter = 0;
+ for(int i=0; i < MAX_TOTAL_PAGES; i++)
+ {
+ p->pages[i].va = 0;
+ p->pages[i].swapOffset = -1;
+ p->pages[i].isInRam = 0;
+ p->pages[i].used = 0;
+ #if defined(LAPA)
+ p->pages[i].age = 0xFFFFFFFF;
+ #else
+ p->pages[i].age = 0x00000000;
+ #endif
+ if(i < MAX_PSYC_PAGES)
+ {
+ p->ramPages[i]=-1;
+ }
+
+ }
+ // /MYCODE
return p;
}
@@ -166,7 +190,8 @@ growproc(int n)
if((sz = allocuvm(curproc->pgdir, sz, sz + n)) == 0)
return -1;
} else if(n < 0){
- if((sz = deallocuvm(curproc->pgdir, sz, sz + n)) == 0)
+ curproc->allocatedPagesCounter +=(PGROUNDUP(n)/PGSIZE);
+ if((sz = deallocuvm(curproc->pgdir, sz, sz + n, 1)) == 0)
return -1;
}
curproc->sz = sz;
@@ -212,6 +237,42 @@ fork(void)
pid = np->pid;
+ #if (defined(SCFIFO) || defined(NFUA) || defined(AQ) || defined(LAPA))
+ if(pid > isRelevant)
+ {
+ createSwapFile(np);
+
+ int i;
+ for (i = 0; i < MAX_TOTAL_PAGES; i++)
+ {
+ np->pages[i].va = curproc->pages[i].va;
+ np->pages[i].swapOffset = curproc->pages[i].swapOffset;
+ np->pages[i].isInRam = curproc->pages[i].isInRam;
+ np->pages[i].used = curproc->pages[i].used;
+ np->pages[i].age = curproc->pages[i].age;
+ }
+ np->currentOffset = curproc->currentOffset;
+ np->pagedOutCounter = curproc->pagedOutCounter;
+ np->allocatedPagesCounter = curproc->allocatedPagesCounter;
+ np->pageFaultCounter = 0;
+ np->totalPagedOutCounter = 0;
+ char* newPage = kalloc();
+ for(i=0; i< curproc->totalPagedOutCounter;i++)
+ {
+ readFromSwapFile(curproc,newPage,i*PGSIZE,PGSIZE);
+ writeToSwapFile(np,newPage,i*PGSIZE,PGSIZE);
+ }
+
+ for(i=0 ; i< MAX_PSYC_PAGES; i++) {
+ np->ramPages[i] = curproc->ramPages[i];
+ }
+
+ kfree(newPage);
+ }
+ #endif
+
+
+
acquire(&ptable.lock);
np->state = RUNNABLE;
@@ -246,6 +307,10 @@ exit(void)
iput(curproc->cwd);
end_op();
curproc->cwd = 0;
+ //remove swapfile
+ #if (defined(SCFIFO) || defined(NFUA) || defined(AQ) || defined(LAPA))
+ removeSwapFile(curproc);
+ #endif
acquire(&ptable.lock);
@@ -263,6 +328,10 @@ exit(void)
// Jump into the scheduler, never to return.
curproc->state = ZOMBIE;
+ #if (defined(TRUE))
+ cprintf("pid = %d, name: %s, allocated_pages=%d paged_out=%d page_faults=%d paged_out_total-num=%d\n", curproc->pid, curproc->name, curproc->allocatedPagesCounter,
+ curproc->pagedOutCounter, curproc->pageFaultCounter,curproc->totalPagedOutCounter);
+ #endif
sched();
panic("zombie exit");
}
@@ -319,6 +388,7 @@ wait(void)
// - swtch to start running that process
// - eventually that process transfers control
// via swtch back to the scheduler.
+
void
scheduler(void)
{
@@ -346,6 +416,12 @@ scheduler(void)
swtch(&(c->scheduler), p->context);
switchkvm();
+
+ #if (defined(NFUA) || defined(LAPA))
+ maintainAge();
+ #elif (defined(AQ))
+ maintainAQ();
+ #endif
// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
@@ -523,7 +599,11 @@ procdump(void)
state = states[p->state];
else
state = "???";
- cprintf("%d %s %s", p->pid, state, p->name);
+ state = state;
+ #if (defined(TRUE))
+ cprintf("pid = %d, name: %s, state=%s, allocated_pages=%d paged_out=%d page_faults=%d paged_out_total-num=%d\n", p->pid, p->name, state,p->allocatedPagesCounter,p->pagedOutCounter,
+ p->pageFaultCounter,p->totalPagedOutCounter);
+ #endif
if(p->state == SLEEPING){
getcallerpcs((uint*)p->context->ebp+2, pc);
for(i=0; i<10 && pc[i] != 0; i++)
diff --git a/proc.h b/proc.h
index f8d364e..b58f4ee 100644
--- a/proc.h
+++ b/proc.h
@@ -33,8 +33,19 @@ struct context {
};
enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
-
// Per-process state
+
+
+struct page{
+ uint va; // virtual address
+ int swapOffset; // offset in swap file
+ uint isInRam; // if in ram
+ int used; // if currently used
+ uint age; // counter for Aging algo
+ uint age2;
+};
+
+//proc struct
struct proc {
uint sz; // Size of process memory (bytes)
pde_t* pgdir; // Page table
@@ -51,6 +62,13 @@ struct proc {
char name[16]; // Process name (debugging)
//Swap file. must initiate with create swap file
struct file *swapFile; //page file
+ int currentOffset;
+ struct page pages[MAX_TOTAL_PAGES]; //all pages
+ int ramPages[MAX_PSYC_PAGES]; //indexes of pages in ram
+ int pageFaultCounter;
+ int pagedOutCounter;
+ int totalPagedOutCounter;
+ int allocatedPagesCounter;
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/sanity.c b/sanity.c
new file mode 100644
index 0000000..07c7691
--- /dev/null
+++ b/sanity.c
@@ -0,0 +1,37 @@
+#include "types.h"
+#include "stat.h"
+#include "user.h"
+#include "fs.h"
+
+#define PAGES_NUMBER 18
+#define PAGE_SIZE 4096
+
+int main(int argc, char *argv[]){
+
+ printf(1,"Will now allocate pages\n");
+ char *array;
+ int i;
+ array = sbrk(PAGE_SIZE * PAGES_NUMBER);
+ //put info
+ for(i=0; i<(PAGE_SIZE * PAGES_NUMBER); i++){
+ array[i] = i%10;
+ if(i%PAGE_SIZE==0 && i != 0)
+ {
+ printf(1, "Page-%d ALLOCATED\n", i/PAGE_SIZE);
+ }
+ }
+ //get info
+ for(i=0; i<(4096*PAGES_NUMBER); i++){
+ if(array[i]!=i%10)
+ {
+ printf(1, "ERROR\n");
+ exit();
+ }
+ if(i%PAGE_SIZE==0 && i != 0)
+ {
+ printf(1, "Page-%d test DONE\n", i/PAGE_SIZE);
+ }
+ }
+ printf(1,"\nTest Passed.\n");
+ exit();
+}
\ No newline at end of file
diff --git a/trap.c b/trap.c
index 41c66eb..470b1f4 100644
--- a/trap.c
+++ b/trap.c
@@ -47,6 +47,9 @@ trap(struct trapframe *tf)
}
switch(tf->trapno){
+ case T_PGFLT:
+ handle_fault(rcr2());
+ break;
case T_IRQ0 + IRQ_TIMER:
if(cpuid() == 0){
acquire(&tickslock);
diff --git a/vm.c b/vm.c
index 7134cff..30eb892 100644
--- a/vm.c
+++ b/vm.c
@@ -215,15 +215,256 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
}
return 0;
}
+//insert to RAM
+void
+insertToRam(struct proc* p, int index, uint add, int reset)
+{
+ int i;
+ if(reset)
+ {
+ p->pages[index].used = 1;
+ p->pages[index].va = add;
+ p->pages[index].isInRam = 1;
+ p->pages[index].swapOffset = -1;
+ }
+ for(i=0; i < MAX_PSYC_PAGES; i++)
+ {
+ //if we found free slot in ram insert page
+ if(p->ramPages[i]==-1)
+ {
+ #if defined(SCFIFO)
+ p->pages[index].age = ticks;
+ #endif
+ p->ramPages[i] = index;
+ return;
+ }
+ }
+ panic("RAM is FULL, could not insert page");
+
+
+}
+//Shifts queue left
+void
+rotateQueue(int index){
+ struct proc *p = myproc();
+ //rotating pages from given index
+ while(index < MAX_PSYC_PAGES - 1) {
+ p->ramPages[index] = p->ramPages[index + 1];
+ index++;
+ }
+ //update last page to be -1;
+ p->ramPages[MAX_PSYC_PAGES-1] = -1;
+}
+
+
+void
+scfifo_sort()
+{
+ struct proc* p = myproc();
+ int *a = p->ramPages;
+ for (int i = 0; i < MAX_PSYC_PAGES; i++)
+ {
+ for (int j = 0; j < MAX_PSYC_PAGES; j++)
+ {
+ if (p->pages[a[j]].age > p->pages[a[i]].age)
+ {
+ int tmp = a[i];
+ a[i] = a[j];
+ a[j] = tmp;
+ }
+ }
+ }
+}
+int
+getPageSCFIFO()
+{
+ struct proc *p = myproc();
+ int i;
+ int pageIndex = -1;
+ int index2 = 0;
+ //sort ram array by age
+ scfifo_sort();
+ for(i=0; i<MAX_PSYC_PAGES; i++)
+ {
+ uint va = p->pages[p->ramPages[i]].va;
+ pde_t* pte = walkpgdir(p->pgdir, (char*) va, 0);
+ if(*pte & PTE_A){
+ *pte &= ~PTE_A;
+ }
+ else
+ {
+ pageIndex = p->ramPages[i];
+ index2 = i;
+ break;
+ }
+ }
+ if(pageIndex == -1)
+ {
+ pageIndex = p->ramPages[0];
+ }
+ rotateQueue(index2);
+ return pageIndex;
+
+}
+
+int
+getPageNFUA()
+{
+ struct proc* p = myproc();
+ uint min = 4294967295;
+ int minIndex = -1;
+ int pageIndex = -1;
+ for(int i=0; i<MAX_PSYC_PAGES; i++)
+ {
+ if(p->pages[p->ramPages[i]].age < min)
+ {
+ min = p->pages[p->ramPages[i]].age;
+ minIndex = i;
+ pageIndex = p->ramPages[minIndex];
+ }
+ }
+ if(minIndex == -1)
+ panic("Couldnot find min in ram");
+ rotateQueue(minIndex);
+ return pageIndex;
+}
+int
+getNumberOfOnes(uint number)
+{
+ int count = 0;
+ while (number) {
+ count += number & 1;
+ number >>= 1;
+ }
+ return count;
+}
+int
+getPageLAPA()
+{
+ struct proc* p = myproc();
+ int minIndex = -1;
+ int pageIndex = -1;
+ int minNumberOfOnes = 33;
+ uint minAge = 4294967295;
+ for(int i=0; i<MAX_PSYC_PAGES; i++)
+ {
+ int number_of_ones = getNumberOfOnes(p->pages[p->ramPages[i]].age);
+ if(number_of_ones < minNumberOfOnes || (number_of_ones==minNumberOfOnes && p->pages[p->ramPages[i]].age < minAge))
+ {
+ minNumberOfOnes = number_of_ones;
+ minIndex = i;
+ pageIndex = p->ramPages[minIndex];
+ minAge = p->pages[p->ramPages[i]].age;
+ }
+
+ }
+ if(minIndex == -1)
+ panic("Couldnot find min in ram");
+ rotateQueue(minIndex);
+ return pageIndex;
+}
+int getPageAQ()
+{
+ struct proc *p = myproc();
+ //take last page
+ int index = p->ramPages[MAX_PSYC_PAGES-1];
+ //remove page from RamPages
+ p->ramPages[MAX_PSYC_PAGES-1]=-1;
+ return index;
+}
+
+int
+getPageToRemove()
+{
+ int pageIndex = 0;
+
+ #if defined(SCFIFO)
+ pageIndex = getPageSCFIFO();
+ #endif
+ #if defined(NFUA)
+ pageIndex = getPageNFUA();
+ #endif
+ #if defined(LAPA)
+ pageIndex = getPageLAPA();
+ #endif
+ #if defined(AQ)
+ pageIndex = getPageAQ();
+ #endif
+
+ return pageIndex;
+}
+
+//swap a page in ram make space
+void
+swapToFile(pde_t *pgdir, struct proc* p)
+{
+ int pageToRemoveIndex = getPageToRemove();
+ writeToSwapFile(p, (char *) p->pages[pageToRemoveIndex].va, p->currentOffset, PGSIZE);
+ //update page offset
+ p->pages[pageToRemoveIndex].swapOffset = p->currentOffset;
+ //update proc offset
+ p->currentOffset = p->currentOffset + PGSIZE;
+ //update proc state
+ p->pages[pageToRemoveIndex].isInRam = 0;
+ //p->pages[pageToRemoveIndex].used = 0;
+ p->pagedOutCounter++;
+ p->totalPagedOutCounter++;
+ pte_t *pte = walkpgdir(pgdir, (char *)p->pages[pageToRemoveIndex].va, 0);
+ *pte &= ~PTE_P;
+ *pte |= PTE_PG;
+ uint pa = PTE_ADDR(*pte);
+ char* va = P2V(pa);
+ kfree(va);
+ lcr3(V2P(p->pgdir));
+
+
+}
+void
+maintainAge()
+{
+ struct proc *p = myproc();
+ for(int i = 0; i < MAX_TOTAL_PAGES; i++) {
+ //shift right
+ p->pages[i].age = p->pages[i].age >> 1;
+ //get pte
+ pte_t* pte = walkpgdir(p->pgdir, (void*)p->pages[i].va, 0);
+ //check if accessed
+ if(*pte & PTE_A) {
+ //add bit to MSB
+ p->pages[i].age = p->pages[i].age | 0x80000000;
+ *pte &= ~PTE_A;
+ }
+ }
+}
+void
+maintainAQ()
+{
+ struct proc *p = myproc();
+ for(int i=MAX_PSYC_PAGES-1; i>0;i--)
+ {
+ if(p->ramPages[i] != -1)
+ {
+ int current = p->ramPages[i];
+ int prev = p->ramPages[i];
+ pte_t* curr_pte = walkpgdir(p->pgdir, (void*)p->pages[current].va, 0);
+ pte_t* prev_pte = walkpgdir(p->pgdir, (void*)p->pages[prev].va, 0);
+ if(((*prev_pte & PTE_A) != 0) && ((*curr_pte & PTE_A) == 0)){
+ p->ramPages[i] = prev;
+ p->ramPages[i-1] = current;
+ }
+ }
+ }
+}
// Allocate page tables and physical memory to grow process from oldsz to
// newsz, which need not be page aligned. Returns new size or 0 on error.
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
+ pte_t *pte;
char *mem;
uint a;
-
+ struct proc* p = myproc();
if(newsz >= KERNBASE)
return 0;
if(newsz < oldsz)
@@ -231,29 +472,84 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
a = PGROUNDUP(oldsz);
for(; a < newsz; a += PGSIZE){
+ // MYCODE
+ //if needed to be swapped(no place in Ram and not init or shell process)
+ #if (defined(SCFIFO) || defined(NFUA) || defined(LAPA) || defined(AQ))
+ if((p->pid > isRelevant) && ( a >= PGSIZE * MAX_PSYC_PAGES))
+ swapToFile(pgdir, p);
+ #endif
+ // /MYCODE
mem = kalloc();
if(mem == 0){
cprintf("allocuvm out of memory\n");
- deallocuvm(pgdir, newsz, oldsz);
+ deallocuvm(pgdir, newsz, oldsz, 0);
return 0;
}
memset(mem, 0, PGSIZE);
if(mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){
cprintf("allocuvm out of memory (2)\n");
- deallocuvm(pgdir, newsz, oldsz);
+ deallocuvm(pgdir, newsz, oldsz, 0);
kfree(mem);
return 0;
}
+ // MYCODE
+ #if (defined(SCFIFO) || defined(NFUA) || defined(LAPA) || defined(AQ))
+ if(p->pid > isRelevant)
+ {
+ int index = 0;
+ while((index < MAX_TOTAL_PAGES) && (p->pages[index].used == 1))
+ index++;
+
+ insertToRam(p, index, a, 1);
+ p->allocatedPagesCounter++;
+
+ pte = walkpgdir(pgdir, (char *)a , 0);
+ *pte=(uint)(*pte) | PTE_P;
+ *pte=(uint)(*pte) & ~PTE_PG;;
+ }
+ #endif
+ // /MYCODE
}
return newsz;
}
+//Dealocates a pages
+void deallocPage(uint va) {
+ struct proc* p = myproc();
+
+ int i;
+ int tmp;
+ for (i = 0; i < MAX_TOTAL_PAGES; i++) {
+ if ((p->pages[i].used != 0) && (p->pages[i].va == va)) {
+ //reset page vars
+ p->pages[i].va = 0;
+ #if defined(LAPA)
+ p->pages[i].age = 0xFFFFFFFF;
+ #elif defined(NFUA)
+ p->pages[i].age = 0x00000000;
+ #endif
+ p->pages[i].used = 0;
+ p->pages[i].isInRam = 0;
+ p->pages[i].swapOffset = -1;
+ break;
+ }
+ }
+ if (i == MAX_TOTAL_PAGES)
+ panic("no page found on deallocation");
+ tmp = i;
+ for (i = 0; i < MAX_PSYC_PAGES; i++) {
+ if (p->ramPages[i] == tmp) {
+ rotateQueue(i);
+ break;
+ }
+ }
+}
// Deallocate user pages to bring the process size from oldsz to
// newsz. oldsz and newsz need not be page-aligned, nor does newsz
// need to be less than oldsz. oldsz can be larger than the actual
// process size. Returns the new process size.
int
-deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
+deallocuvm(pde_t *pgdir, uint oldsz, uint newsz, int flag)
{
pte_t *pte;
uint a, pa;
@@ -266,10 +562,14 @@ deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
pte = walkpgdir(pgdir, (char*)a, 0);
if(!pte)
a = PGADDR(PDX(a) + 1, 0, 0) - PGSIZE;
- else if((*pte & PTE_P) != 0){
+ else if(((*pte & PTE_P) != 0) || ((*pte & PTE_PG) != 0)){
pa = PTE_ADDR(*pte);
if(pa == 0)
panic("kfree");
+ #if (defined(SCFIFO) || defined(NFUA) || defined(AQ) || defined(LAPA))
+ if(myproc()->pid > isRelevant && flag == 1)
+ deallocPage(a);
+ #endif
char *v = P2V(pa);
kfree(v);
*pte = 0;
@@ -287,7 +587,7 @@ freevm(pde_t *pgdir)
if(pgdir == 0)
panic("freevm: no pgdir");
- deallocuvm(pgdir, KERNBASE, 0);
+ deallocuvm(pgdir, KERNBASE, 0, 0);
for(i = 0; i < NPDENTRIES; i++){
if(pgdir[i] & PTE_P){
char * v = P2V(PTE_ADDR(pgdir[i]));
@@ -296,7 +596,6 @@ freevm(pde_t *pgdir)
}
kfree((char*)pgdir);
}
-
// Clear PTE_U on a page. Used to create an inaccessible
// page beneath the user stack.
void
@@ -325,8 +624,11 @@ copyuvm(pde_t *pgdir, uint sz)
for(i = 0; i < sz; i += PGSIZE){
if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
panic("copyuvm: pte should exist");
- if(!(*pte & PTE_P))
- panic("copyuvm: page not present");
+ if(!(*pte & PTE_P)){
+ if(!(*pte & PTE_PG))
+ panic("copyuvm: page not present");
+ continue;
+ }
pa = PTE_ADDR(*pte);
flags = PTE_FLAGS(*pte);
if((mem = kalloc()) == 0)
@@ -361,7 +663,8 @@ uva2ka(pde_t *pgdir, char *uva)
// Copy len bytes from p to user address va in page table pgdir.
// Most useful when pgdir is not the current page table.
-// uva2ka ensures this only works for PTE_U pages.
+// uva2ka ensures this only works for PTE_U pages.int i;
+
int
copyout(pde_t *pgdir, uint va, void *p, uint len)
{
@@ -385,6 +688,63 @@ copyout(pde_t *pgdir, uint va, void *p, uint len)
return 0;
}
+void
+handle_fault(uint va)
+{
+ struct proc *p = myproc();
+ p->pageFaultCounter++;
+
+ int i=0;
+ int count=0;
+ //get page
+ uint page = PGROUNDDOWN(va);
+ //count used pages
+ for(i=0;i<MAX_TOTAL_PAGES;i++)
+ if((p->pages[i].used == 1 ) && ( p->pages[i].isInRam))
+ count++;
+ if(count == MAX_PSYC_PAGES)
+ {
+ swapToFile(p->pgdir, p);
+ }
+ char* newPage = kalloc();
+ if(newPage == 0){
+ cprintf("failed kalloc\n");
+ return;
+ }
+ memset(newPage, 0, PGSIZE);
+ i=0;
+ while((i<MAX_TOTAL_PAGES)&&(p->pages[i].va != page))
+ i++;
+
+ if(i==MAX_TOTAL_PAGES)
+ {
+ cprintf("i was looking for this va - %d\n", page);
+ for(int k=0; k<MAX_TOTAL_PAGES; k++)
+ {
+ cprintf("page id: %d, page va - %d\n", k,p->pages[k].va);
+ }
+ panic("page not found");
+ }
+
+ uint offset = p->pages[i].swapOffset;
+ readFromSwapFile(p, newPage, offset, PGSIZE);
+ pte_t *pte = walkpgdir(p->pgdir,(char *) va, 0);
+ *pte &= ~PTE_P;
+ *pte |= PTE_PG;
+ mappages(p->pgdir, (void *) page, PGSIZE, V2P(newPage), PTE_W | PTE_U );
+
+ *pte |= PTE_P;
+ *pte &= ~PTE_PG;
+
+ p->pages[i].isInRam = 1;
+ p->pages[i].swapOffset = -1;
+
+ insertToRam(p, i, 0, 0);
+ p->pagedOutCounter--;
+
+ lapiceoi();
+}
+
//PAGEBREAK!
// Blank page.
//PAGEBREAK!