Repository navigation
Expand file tree
/
Copy pathpaging.patch
More file actions
2604 lines (2510 loc) · 69.3 KB
/
Copy pathpaging.patch
File metadata and controls
2604 lines (2510 loc) · 69.3 KB
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/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
new file mode 100644
index 0000000..c2098a2
--- /dev/null
+++ b/.vscode/c_cpp_properties.json
@@ -0,0 +1,18 @@
+{
+ "configurations": [
+ {
+ "name": "linux-gcc-x64",
+ "includePath": [
+ "${workspaceFolder}/**"
+ ],
+ "compilerPath": "/usr/bin/gcc",
+ "cStandard": "${default}",
+ "cppStandard": "${default}",
+ "intelliSenseMode": "linux-gcc-x64",
+ "compilerArgs": [
+ ""
+ ]
+ }
+ ],
+ "version": 4
+}
\ No newline at end of file
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..68a1adc
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,24 @@
+{
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "C/C++ Runner: Debug Session",
+ "type": "cppdbg",
+ "request": "launch",
+ "args": [],
+ "stopAtEntry": false,
+ "externalConsole": false,
+ "cwd": "/home/toriqe/Downloads/test-20260924T184016Z-1-001/xv6-riscv-314/kernel",
+ "program": "/home/toriqe/Downloads/test-20260924T184016Z-1-001/xv6-riscv-314/kernel/build/Debug/outDebug",
+ "MIMode": "gdb",
+ "miDebuggerPath": "gdb",
+ "setupCommands": [
+ {
+ "description": "Enable pretty-printing for gdb",
+ "text": "-enable-pretty-printing",
+ "ignoreFailures": true
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..3e5eb95
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,59 @@
+{
+ "C_Cpp_Runner.cCompilerPath": "gcc",
+ "C_Cpp_Runner.cppCompilerPath": "g++",
+ "C_Cpp_Runner.debuggerPath": "gdb",
+ "C_Cpp_Runner.cStandard": "",
+ "C_Cpp_Runner.cppStandard": "",
+ "C_Cpp_Runner.msvcBatchPath": "",
+ "C_Cpp_Runner.useMsvc": false,
+ "C_Cpp_Runner.warnings": [
+ "-Wall",
+ "-Wextra",
+ "-Wpedantic",
+ "-Wshadow",
+ "-Wformat=2",
+ "-Wcast-align",
+ "-Wconversion",
+ "-Wsign-conversion",
+ "-Wnull-dereference"
+ ],
+ "C_Cpp_Runner.msvcWarnings": [
+ "/W4",
+ "/permissive-",
+ "/w14242",
+ "/w14287",
+ "/w14296",
+ "/w14311",
+ "/w14826",
+ "/w44062",
+ "/w44242",
+ "/w14905",
+ "/w14906",
+ "/w14263",
+ "/w44265",
+ "/w14928"
+ ],
+ "C_Cpp_Runner.enableWarnings": true,
+ "C_Cpp_Runner.warningsAsError": false,
+ "C_Cpp_Runner.compilerArgs": [],
+ "C_Cpp_Runner.linkerArgs": [],
+ "C_Cpp_Runner.includePaths": [],
+ "C_Cpp_Runner.includeSearch": [
+ "*",
+ "**/*"
+ ],
+ "C_Cpp_Runner.excludeSearch": [
+ "**/build",
+ "**/build/**",
+ "**/.*",
+ "**/.*/**",
+ "**/.vscode",
+ "**/.vscode/**"
+ ],
+ "C_Cpp_Runner.useAddressSanitizer": false,
+ "C_Cpp_Runner.useUndefinedSanitizer": false,
+ "C_Cpp_Runner.useLeakSanitizer": false,
+ "C_Cpp_Runner.showCompilationTime": false,
+ "C_Cpp_Runner.useLinkTimeOptimization": false,
+ "C_Cpp_Runner.msvcSecureNoWarnings": false
+}
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 39a99d7..f104cdf 100644
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ OBJS = \
$K/sleeplock.o \
$K/file.o \
$K/pipe.o \
+ $K/swap.o \
$K/exec.o \
$K/sysfile.o \
$K/kernelvec.o \
@@ -48,7 +49,8 @@ TOOLPREFIX := $(shell if riscv64-unknown-elf-objdump -i 2>&1 | grep 'elf64-big'
echo "***" 1>&2; exit 1; fi)
endif
-QEMU = qemu-system-riscv64
+QEMU = /home/toriqe/qemu-install/bin/qemu-system-riscv64
+
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
@@ -132,6 +134,11 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_test\
+ $U/_usepage\
+ $U/_pagestats\
+
+
fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
diff --git a/kernel/bio.c b/kernel/bio.c
index 60d91a6..09326d4 100644
--- a/kernel/bio.c
+++ b/kernel/bio.c
@@ -119,6 +119,7 @@ brelse(struct buf *b)
if(!holdingsleep(&b->lock))
panic("brelse");
+ // printf("brelse enter\n");
releasesleep(&b->lock);
acquire(&bcache.lock);
@@ -134,6 +135,8 @@ brelse(struct buf *b)
}
release(&bcache.lock);
+ // printf("brelse exit\n");
+
}
void
diff --git a/kernel/defs.h b/kernel/defs.h
index a3c962b..014d0d1 100644
--- a/kernel/defs.h
+++ b/kernel/defs.h
@@ -1,3 +1,4 @@
+struct sleeplock;
struct buf;
struct context;
struct file;
@@ -5,9 +6,9 @@ struct inode;
struct pipe;
struct proc;
struct spinlock;
-struct sleeplock;
struct stat;
struct superblock;
+struct swap;
// bio.c
void binit(void);
@@ -36,6 +37,8 @@ int filewrite(struct file*, uint64, int n);
// fs.c
void fsinit(int);
+uint balloc(uint dev);
+void bfree(int dev, uint b);
int dirlink(struct inode*, char*, uint);
struct inode* dirlookup(struct inode*, char*, uint*);
struct inode* ialloc(uint, short);
@@ -61,15 +64,27 @@ void ramdiskrw(struct buf*);
// kalloc.c
void* kalloc(void);
+void* kalloc2(void);
void kfree(void *);
void kinit(void);
-
+void inc(uint64 ppn);
+int freePageCount() ;
+void addLive(pte_t *pte, int procId, int vpn, int);
+void pageCountInfo();
+void removeLive(int , int , pte_t* );
+void swap_in(int, int, uint64 *pte);
+void releaseSlock();
+void acquireSlock();
+void addSwapped(pte_t *, int, int, int);
+void removeFromSwapped(int , int , pte_t*) ;
+void pg_count(int pid) ;
// log.c
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);
@@ -88,7 +103,7 @@ int fork(void);
int growproc(int);
void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *);
-void proc_freepagetable(pagetable_t, uint64);
+void proc_freepagetable(pagetable_t, uint64, int);
int kill(int);
int killed(struct proc*);
void setkilled(struct proc*);
@@ -106,6 +121,18 @@ 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);
+void process_page_count();
+void releaseLock();
+void acquireLock();
+// swap.c
+void swapinit(void);
+void swapfree(struct swap*);
+struct swap* swapalloc(void);
+void swapout(struct swap *dst_sp, char *src_pa);
+void swapin(char *dst_pa, struct swap *src_sp);
+int swapCount(struct swap * ,int,int) ;
+
+
// swtch.S
void swtch(struct context*, struct context*);
@@ -145,7 +172,7 @@ void syscall();
extern uint ticks;
void trapinit(void);
void trapinithart(void);
-extern struct spinlock tickslock;
+extern struct spinlock tickslock;
void usertrapret(void);
// uart.c
@@ -159,20 +186,23 @@ int uartgetc(void);
void kvminit(void);
void kvminithart(void);
void kvmmap(pagetable_t, uint64, uint64, uint64, int);
-int mappages(pagetable_t, uint64, uint64, uint64, int);
+int mappages(pagetable_t, uint64, uint64, uint64, int,int,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 uvmfirst(pagetable_t, uchar *, uint, int);
+uint64 uvmalloc(pagetable_t, uint64, uint64, int, int);
+uint64 uvmdealloc(pagetable_t, uint64, uint64, int);
+int uvmcopy(pagetable_t, pagetable_t, uint64, int, int,struct spinlock*);
+void uvmfree(pagetable_t, uint64, int);
+void uvmunmap(pagetable_t, uint64, uint64, int, 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);
+pte_t * walk(pagetable_t, uint64, int,int);
+uint64 walkaddr(pagetable_t, uint64, int);
+int copyout(pagetable_t, uint64, char *, uint64, int);
+int copyin(pagetable_t, char *, uint64, uint64, int);
+int copyinstr(pagetable_t, char *, uint64, uint64, int);
+void pageCount(pagetable_t, int, int*, int*);
+int trapHandler(pagetable_t , int ) ;
+void Count(uint64,int,pagetable_t) ;
// plic.c
void plicinit(void);
diff --git a/kernel/exec.c b/kernel/exec.c
index e18bbb6..446c99d 100644
--- a/kernel/exec.c
+++ b/kernel/exec.c
@@ -7,7 +7,7 @@
#include "defs.h"
#include "elf.h"
-static int loadseg(pde_t *, uint64, struct inode *, uint, uint);
+static int loadseg(pde_t *, uint64, struct inode *, uint, uint, int);
int flags2perm(int flags)
{
@@ -30,7 +30,7 @@ exec(char *path, char **argv)
struct proghdr ph;
pagetable_t pagetable = 0, oldpagetable;
struct proc *p = myproc();
-
+ // printf("\n\np1: %d\n",p->pid);
begin_op();
if((ip = namei(path)) == 0){
@@ -62,10 +62,10 @@ exec(char *path, char **argv)
if(ph.vaddr % PGSIZE != 0)
goto bad;
uint64 sz1;
- if((sz1 = uvmalloc(pagetable, sz, ph.vaddr + ph.memsz, flags2perm(ph.flags))) == 0)
+ if((sz1 = uvmalloc(pagetable, sz, ph.vaddr + ph.memsz, flags2perm(ph.flags),p->pid)) == 0)
goto bad;
sz = sz1;
- if(loadseg(pagetable, ph.vaddr, ip, ph.off, ph.filesz) < 0)
+ if(loadseg(pagetable, ph.vaddr, ip, ph.off, ph.filesz, p->pid) < 0)
goto bad;
}
iunlockput(ip);
@@ -73,6 +73,7 @@ exec(char *path, char **argv)
ip = 0;
p = myproc();
+ // printf("p2: %d\n",p->pid);
uint64 oldsz = p->sz;
// Allocate two pages at the next page boundary.
@@ -80,7 +81,7 @@ exec(char *path, char **argv)
// Use the second as the user stack.
sz = PGROUNDUP(sz);
uint64 sz1;
- if((sz1 = uvmalloc(pagetable, sz, sz + 2*PGSIZE, PTE_W)) == 0)
+ if((sz1 = uvmalloc(pagetable, sz, sz + 2*PGSIZE, PTE_W, p->pid)) == 0)
goto bad;
sz = sz1;
uvmclear(pagetable, sz-2*PGSIZE);
@@ -95,7 +96,7 @@ exec(char *path, char **argv)
sp -= sp % 16; // riscv sp must be 16-byte aligned
if(sp < stackbase)
goto bad;
- if(copyout(pagetable, sp, argv[argc], strlen(argv[argc]) + 1) < 0)
+ if(copyout(pagetable, sp, argv[argc], strlen(argv[argc]) + 1,p->pid) < 0)
goto bad;
ustack[argc] = sp;
}
@@ -106,7 +107,7 @@ exec(char *path, char **argv)
sp -= sp % 16;
if(sp < stackbase)
goto bad;
- if(copyout(pagetable, sp, (char *)ustack, (argc+1)*sizeof(uint64)) < 0)
+ if(copyout(pagetable, sp, (char *)ustack, (argc+1)*sizeof(uint64),p->pid) < 0)
goto bad;
// arguments to user main(argc, argv)
@@ -126,13 +127,13 @@ exec(char *path, char **argv)
p->sz = sz;
p->trapframe->epc = elf.entry; // initial program counter = main
p->trapframe->sp = sp; // initial stack pointer
- proc_freepagetable(oldpagetable, oldsz);
+ proc_freepagetable(oldpagetable, oldsz,p->pid);
return argc; // this ends up in a0, the first argument to main(argc, argv)
bad:
if(pagetable)
- proc_freepagetable(pagetable, sz);
+ proc_freepagetable(pagetable, sz,p->pid);
if(ip){
iunlockput(ip);
end_op();
@@ -145,13 +146,13 @@ exec(char *path, char **argv)
// and the pages from va to va+sz must already be mapped.
// Returns 0 on success, -1 on failure.
static int
-loadseg(pagetable_t pagetable, uint64 va, struct inode *ip, uint offset, uint sz)
+loadseg(pagetable_t pagetable, uint64 va, struct inode *ip, uint offset, uint sz, int process_id)
{
uint i, n;
uint64 pa;
for(i = 0; i < sz; i += PGSIZE){
- pa = walkaddr(pagetable, va + i);
+ pa = walkaddr(pagetable, va + i, process_id);
if(pa == 0)
panic("loadseg: address should exist");
if(sz - i < PGSIZE)
diff --git a/kernel/file.c b/kernel/file.c
index 25fa226..479c1d1 100644
--- a/kernel/file.c
+++ b/kernel/file.c
@@ -94,7 +94,7 @@ filestat(struct file *f, uint64 addr)
ilock(f->ip);
stati(f->ip, &st);
iunlock(f->ip);
- if(copyout(p->pagetable, addr, (char *)&st, sizeof(st)) < 0)
+ if(copyout(p->pagetable, addr, (char *)&st, sizeof(st),p->pid) < 0)
return -1;
return 0;
}
diff --git a/kernel/fs.c b/kernel/fs.c
index c6bab15..33ad84b 100644
--- a/kernel/fs.c
+++ b/kernel/fs.c
@@ -62,7 +62,7 @@ bzero(int dev, int bno)
// Allocate a zeroed disk block.
// returns 0 if out of disk space.
-static uint
+uint
balloc(uint dev)
{
int b, bi, m;
@@ -88,9 +88,10 @@ balloc(uint dev)
}
// Free a disk block.
-static void
+void
bfree(int dev, uint b)
{
+
struct buf *bp;
int bi, m;
diff --git a/kernel/kalloc.c b/kernel/kalloc.c
index 0699e7e..efeb5f7 100644
--- a/kernel/kalloc.c
+++ b/kernel/kalloc.c
@@ -1,22 +1,613 @@
// Physical memory allocator, for user processes,
// kernel stacks, page-table pages,
// and pipe buffers. Allocates whole 4096-byte pages.
-
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "riscv.h"
#include "defs.h"
+#include "sleeplock.h"
+// #include "swap.c"
+// Given on assignment
+#define MAX_LIVE_PAGE 50
+#define PAGE_COUNT 1<<15
+int live_count = 0 ;
+void kfree_helper(void *pa);
+// void swapListSize();
+
+struct sleeplock slock;
+struct run {
+ struct run *next;
+};
+struct {
+ char count[PAGE_COUNT];
+ struct spinlock lock;
+} refCount;
+
+//implementation of linked-list
+
+struct liveListNode {
+ pte_t *pte; //the page table entry
+ int process_id;//for which process
+ int vpn;//virtual page number
+ struct liveListNode* next;
+ struct liveListNode* prev;
+};
+// must have a swap* s structure to know either it is swapped before!!
+
+struct swappedListNode{
+ int process_id;//for which process
+ int vpn;// virtual page number
+ struct swap* sp;//the important swap structure
+ struct swappedListNode* next;//link for next node
+ pte_t *pte; // the page table entry pointer
+};
+
+//implementing freelist
+struct {
+ struct spinlock lock;
+ struct run *freelist;
+} livelist_node_mem, swappedlist_node_mem;
+
+//simple allocation prinpiple in linkedList (using freelist)
+struct liveListNode *
+allocate_livelist_node(void)
+{
+ //printf("allocate live e asche!!\n") ;
+ struct run *r;
+ //need to acquire lock
+ acquire(&livelist_node_mem.lock) ;
+ r = livelist_node_mem.freelist;
+ if(r == 0) {
+ //need to allocate
+ release(&livelist_node_mem.lock) ;
+ char *mem = kalloc() ; // 4KB allocating
+ char *m_end = mem + PGSIZE ;
+ for(; mem + sizeof(struct liveListNode) <= m_end ; mem+=sizeof(struct liveListNode)) {
+ struct run *chunk = (struct run*) mem ;
+ acquire(&livelist_node_mem.lock) ;
+ chunk->next = livelist_node_mem.freelist;
+ livelist_node_mem.freelist = chunk ;
+ release(&livelist_node_mem.lock) ;
+ }
+ acquire(&livelist_node_mem.lock) ;
+ r = livelist_node_mem.freelist ;
+ }
+ livelist_node_mem.freelist = r->next ;
+ release(&livelist_node_mem.lock) ;
+ //printf("why can't allocate?\n") ;
+ return (struct liveListNode*) r ;
+}
+
+struct swappedListNode *
+allocate_swappedlist_node(void)
+{
+ struct run *r;
+
+ //need to acquire lock
+ acquire(&swappedlist_node_mem.lock) ;
+ r = swappedlist_node_mem.freelist;
+ if(r == 0) {
+
+ //need to allocate
+ release(&swappedlist_node_mem.lock) ;
+ char *mem = kalloc() ; // 4KB allocating
+ char *m_end = mem + PGSIZE ;
+ for(; mem + sizeof(struct swappedListNode) <= m_end ; mem+=sizeof(struct swappedListNode)) {
+ struct run *chunk = (struct run*) mem ;
+ acquire(&swappedlist_node_mem.lock) ;
+ chunk->next = swappedlist_node_mem.freelist ;
+ swappedlist_node_mem.freelist = chunk ;
+ release(&swappedlist_node_mem.lock) ;
+ }
+ acquire(&swappedlist_node_mem.lock) ;
+ r = swappedlist_node_mem.freelist ;
+ }
+ swappedlist_node_mem.freelist = r->next ;
+ release(&swappedlist_node_mem.lock) ;
+
+ return (struct swappedListNode*) r ;
+}
+//for root of the live linked list
+struct {
+ struct liveListNode* head; // MRU
+ struct liveListNode* tail; // LRU
+ int liveCount;
+} live;
+
+//for root of the swapped linked list
+struct {
+ struct swappedListNode* list ;
+ int swappedCount ;
+} swapped;
+
+// ============LRU helpers function====================
+
+static inline void
+recycle_live_node(struct liveListNode* n){
+ if(!n) return;
+ n->next = 0;
+ n->prev = 0;
+ n->pte = 0;
+ n->process_id = 0;
+ n->vpn = 0;
+ struct run *rr = (struct run*)n;
+ acquire(&livelist_node_mem.lock);
+ rr->next = livelist_node_mem.freelist;
+ livelist_node_mem.freelist = rr;
+ release(&livelist_node_mem.lock);
+}
+
+void
+unlink_live_node(struct liveListNode* n){
+ if(!n) return;
+ if(n->prev)
+ n->prev->next = n->next;
+ else
+ live.head = n->next;
+
+ if(n->next)
+ n->next->prev = n->prev;
+ else
+ live.tail = n->prev;
+ live.liveCount--;
+}
+
+void
+push_head_live(struct liveListNode* n){
+ n->prev = 0;
+ n->next = live.head;
+
+ if(live.head)
+ live.head->prev = n;
+ live.head = n;
+
+ if(!live.tail)
+ live.tail = n;
+
+ live.liveCount++;
+}
+
+void
+moveToHead(struct liveListNode* n){
+ if(!n || live.head == n)
+ return;
+ if(n->prev)
+ n->prev->next = n->next;
+ else
+ live.head = n->next;
+ if(n->next)
+ n->next->prev = n->prev;
+ else
+ live.tail = n->prev;
+
+ // push to head
+ n->prev = 0;
+ n->next = live.head;
+
+ if(live.head)
+ live.head->prev = n;
+ live.head = n;
+ if(!live.tail)
+ live.tail = n;
+}
+
+void touch_pte(pte_t *pte){
+ struct liveListNode* cur = live.head;
+ while(cur){
+ if(cur->pte == pte){ // found matching pte
+ moveToHead(cur);
+ return;
+ }
+ cur = cur->next;
+ }
+}
+
+void touch_ppn(int ppn){
+ struct liveListNode* cur = live.head;
+ while(cur){
+ if(PTE2PPN(*cur->pte) == ppn){
+ moveToHead(cur);
+ return;
+ }
+ cur = cur->next;
+ }
+}
+
+void swap_out(struct liveListNode* n){
+ //printf("comming to swap-out\n") ;
+ if(n == 0){
+ panic("null pointer to in swap_out");
+ }
+ struct swap *s = swapalloc();
+ if(s == 0)
+ panic("swap not allocated") ;
+ uint64 set = *n->pte & PTE_SWAPPED ;
+ if(set){
+ panic("swapped bit on");
+ }
+ //printf("========swapping out=========\n") ;
+ uint64 pa = PTE2PA(*n->pte) ;
+ int rppn = PTE2PPN(*n->pte) ;
+ swapout(s,(char*)pa);
+ printf("<==============Swapped Out (ppn=%d)=================>\n", rppn);
+ int ref_cnt = 0;
+ int f = 0;
+
+ struct liveListNode* l = live.head ;//pointing to the head of live list
+ if(l == 0)
+ panic("swap_out: live list head empty\n");
+ // struct liveListNode* found;
+ while(l){ // traverse through the whole livenode list
+ //if physical page number matches then set the valid bit 0 and swapped bit to 1 ;
+ struct liveListNode* next = l->next; // save next because we may unlink l
+ if(rppn == PTE2PPN(*l->pte)){
+ f = 1;
+ *l->pte &= (~PTE_V);
+ *l->pte |= PTE_SWAPPED;
+
+ // struct liveListNode* curr = l->next;
+ //now copying the l->next node to cur and
+ //adding it to swappedlist
+ //before we need to allocate a node using freelist
+ //creating a temporary node
+
+ struct swappedListNode* sn = 0 ;
+ // panic("here-panic") ;
+ sn = allocate_swappedlist_node();
+ if(sn == 0)
+ panic("swapped list node is not alloacted");
+ sn->process_id = l->process_id ;
+ sn->pte = l->pte ;
+ sn->vpn = l->vpn ;
+ sn->sp = s ; //As we are swapping out we need to have that swap* s saved in our linked list
+
+ if(swapped.list == 0)
+ panic("swap_out: swapped list head null");
+
+ //Now the next will be head->next(linked list)
+ //Now This is a FIFO linked list
+ //at each time we are inserting at the head of the list
+
+ sn->next = swapped.list->next;
+ swapped.list->next = sn;
+ swapped.swappedCount++ ;
+ //printf("Swapped out a pte and swapped size : %d\n\n",swapped.swappedCount) ;
+ //================Freeing===================//
+ unlink_live_node(l);
+ recycle_live_node(l);
+
+ ref_cnt++;
+ }
+ l = next;
+ }
+ if(!f)
+ panic("swap_out: no matching PTEs for ppn");
+
+ printf("<===============Removed from livePages,number of live pages : %d =====>\n ",live.liveCount) ;
+
+ swapCount(s,4,ref_cnt) ;
+
+
+
+
+ acquire(&refCount.lock);
+ refCount.count[rppn] = 0;
+ release(&refCount.lock);
+ kfree_helper((void*) PPN2PA(rppn));
+ //printf("=========Swap-Out done==========\n") ;
+}
+
+void addSwapped(pte_t *pte, int oldprocess_id, int newprocess_id, int vpn){
+ //printf("comming to add-swapped\n") ;
+ //does it need to be removed from Live-list?
+ //No because addswapped is called in map-pages
+ //when there's swapped bit is set(must've been) swapped out before!!
+ //See swap_out function
+ //It sets the swappedbit
+ if(*pte & PTE_V){
+ panic("valid bit on");
+ }
+ struct swappedListNode* new = allocate_swappedlist_node() ;
+ if(!new) panic("swapped node alloc!") ;
+ new->next = 0 ;
+ new->sp = 0 ;
+ new->process_id = newprocess_id ;
+ new->pte = pte ;
+ new->vpn = vpn ;
+
+ if(swapped.list == 0)
+ panic("swapped list head null");
+
+ struct swappedListNode* curr = swapped.list ;//pointing to the head
+ int f = 0;
+ while(curr->next){
+ if(curr->next->process_id == oldprocess_id ){
+ int x = curr->next->vpn ;
+ if(x == vpn){
+ //using the same swap structure
+ new->sp = curr->next->sp;
+ //for fork()
+ //without fork ei condition jiboneo execute hobe na!!
+ //============KRV SIR DEKHBEN=============//
+ //the function is called on map-pages
+ //map-pages is called on uvm-copy
+ //uvm-copy is called on fork()
+ //So we need to update the reference cnt because we don't want to free it if some child process also use it
+ //That's why iterating through and checking parent_process_id(old) with the live_list_process_ids
+ swapCount(new->sp , 1, 0) ;
+ f = 1;
+ break ;
+ }
+ }
+ curr = curr->next;
+ }
+ if(f) {
+ new->next = swapped.list->next;
+ swapped.list->next = new ;
+ swapped.swappedCount++ ;
+ }
+ else {
+ panic("addSwapped: swap not found");
+ }
+}
+
+void swap_in(int vpn, int process_id, uint64 *pte){
+ //printf("comming to swap-in\n") ;
+ //swapin requires the node to be added in live-pages
+ while(live.liveCount >= MAX_LIVE_PAGE){
+ if (!live.tail)
+ panic("swap_in : live tail is null in swap_in");
+ swap_out(live.tail);
+ }
+ if(*pte & PTE_V){
+ panic("valid bit set\n");
+ }
+ struct swappedListNode* n;
+ n = swapped.list ; //pointing to the head
+ if(n == 0){
+ panic("swap_in : swap list empty\n");
+ }
+ int f = 0;
+ struct swap *s;
+ char* mem ;
+ int ref_cnt = 0 ;
+ //Now what??
+ while(n->next){
+ //the condition is needed for fork()
+ if(n->next->process_id == process_id && n->next->vpn == vpn){
+ f = 1 ;
+ //for swappin we need to allocate memory
+ mem =(char *) kalloc();
+ if(n->next->sp == 0){
+ panic("sp 0\n");
+ }
+ swapin(mem,n->next->sp);
+ printf("<==============Swapped In=================>\n") ;
+ //printf("Swapped in\n") ;
+ s = n->next->sp;
+ break;
+ }
+ else
+ n = n->next;
+ }
+ if(f == 0){
+ panic("swap in: swap not found\n");
+ }
+
+ n = swapped.list ; // again poiniting to head
+ while(n->next){
+ if(n->next->sp == s){
+ if(s == 0)
+ panic("swap_in : swap is null");
+
+ pte_t *pte = n->next->pte ;
+ *pte = (PTE_FLAGS(*pte)) | (PA2PTE((uint64)mem)) | (PTE_V) ;
+ *pte &= (~PTE_SWAPPED) ;
+
+ int pid = n->next->process_id;
+ int vp = n->next->vpn;
+
+ struct swappedListNode *tt = n->next ; // victim node
+ if(tt == 0)
+ panic("swap_in : swapList Node is null");
+ n->next = tt->next;
+
+ swapCount(s, 2, 0); // decrement swap count reference
+ if(!swapCount(s,3,0)) // if no more references
+ swapfree(s);
+ ++ref_cnt;
+
+ // recycle the swaped node
+ tt->next = 0 ;
+ tt->process_id = 0 ;
+ tt->vpn = 0 ;
+ tt->sp = 0 ;
+ struct run *rr = (struct run*) tt ;
+ acquire(&swappedlist_node_mem.lock) ;
+ rr->next = swappedlist_node_mem.freelist ;
+ swappedlist_node_mem.freelist = rr ;
+ release(&swappedlist_node_mem.lock) ;
+ //===========Freeing done===========//
+ swapped.swappedCount-- ;
+
+ struct liveListNode* nd = allocate_livelist_node();
+ if(!nd)
+ panic("swap_in: live node alloc");
+ nd->pte = pte;
+ nd->process_id = pid;
+ nd->vpn = vp;
+ nd->prev = 0; nd->next = 0;
+ push_head_live(nd);
+ }
+ else
+ n = n->next;
+ }
+ acquire(&refCount.lock);
+ int ppn = PA2PPN(mem);
+ refCount.count[ppn] = ref_cnt;
+ release(&refCount.lock);
+}
+
+void removeFromSwapped(int process_id, int vpn, pte_t* pte){
+ //printf("comming to swapremovedfrom\n") ;
+ //does it need to be added in Live-pages?
+ //No because it's only called when uvmunmap is called
+ //basically it frees the physical address
+ //so need to swapfree on that particular swap*
+ if(*pte & PTE_V){
+ panic("removeFromSwapped : valid bit is on");
+ }
+ struct swappedListNode *s , *t;
+ // acquire(&swapped.lock);
+ s = swapped.list;
+ if(s == 0){
+ panic("removeFromSwapped : swapped list head empty\n");
+ }
+ int f = 0;
+ while(s->next){
+ //the same type of iteration like addswapped, this time we just need to remove it!!
+ if(s->next->vpn == vpn && s->next->process_id == process_id && s->next->pte == pte){
+ f = 1;
+ t = s->next;
+ s->next = t->next;
+ swapCount(t->sp,2,0) ;
+ //we are freeing the swap structure finally!!
+
+ if(!swapCount(t->sp,3,0)){
+ swapfree(t->sp) ;
+ }
+ struct swappedListNode *tt = t ;
+ struct run *rr ;
+ //======Freeing===========//
+ //freeing from swappedlist
+ if(!tt)
+ panic("swapfree");
+ tt->next = 0 ;
+ tt->process_id = 0 ;
+ tt->vpn = 0 ;
+ tt->sp = 0 ;
+ rr = (struct run*) tt ;
+ acquire(&swappedlist_node_mem.lock) ;
+ rr->next = swappedlist_node_mem.freelist ;
+ swappedlist_node_mem.freelist = rr ;
+ release(&swappedlist_node_mem.lock) ;
+ swapped.swappedCount-- ;
+ //==========Freeing done===========//
+ break;
+ }
+ else
+ s = s->next;
+ }
+ if(!f){
+ panic("removeFromSwapped : swap not found\n");
+ }
+}
+
+void addLive(pte_t *pte, int process_id, int vpn, int h){
+ //printf("comming to addlive\n") ;
+
+ if(*pte & PTE_SWAPPED){
+ panic("swapped bit on");
+ }
+
+ //printf("hoise\n") ;
+ struct liveListNode* nd = allocate_livelist_node();
+ //printf("allocate hoise addlive e \n") ;
+ if(nd == 0){
+ panic("addLive : liveListNode alloc");
+ }
+ nd->process_id = process_id;
+ nd->pte = pte;
+ nd->vpn = vpn;
+ nd->next = 0;
+
+ int ppn = PTE2PPN(*pte);
+ if(ppn < 0 || ppn >= PAGE_COUNT){
+ panic("invalid ppn");
+ }
+ push_head_live(nd);
+
+ printf("<=================Added livePages (LRU), number of live pages : %d ========>\n", live.liveCount);
+
+ // Evict until within capacity
+ while(live.liveCount > MAX_LIVE_PAGE){
+ if(!live.tail)
+ panic("addLive : live list empty while over capacity");
+ printf("<================Over capacity; swapping out LRU (tail) ==================>\n");
+ swap_out(live.tail);
+ }
+ //printf("reaching end of addlive\n") ;
+
+}
+
+void removeLive(int vpn, int process_id, uint64* pte){
+ //printf("comming to removelive\n") ;