Adding getNumFreePag getNumFreePages es system call call
Add the variable numFreePages to implement implement the given given system call in in kalloc.c : In kalloc.c add the entry in struct kmem: int numFreePages;
Adding given system system calls Add following following code in defs.h: int getNumFreePagesvoid getNumFreePagesvoid!; !;
Add following following code in sysproc.c: int sys"getNumFreePagesvoid sys"getNumFreePages void!! #
$
return getNumFreePages!; %
Add the following following declaration along along with other system system calls in syscall.c: syscall.c: e&tern int sys"getNumFreePages sys"getNumFreePagesvoid void!; !;
Add following following elds in the same same le like other system calls in syscall.c: '()("getNumFreePages* '()("getNumFreePages* sys"getNumFreePages+ sys"getNumFreePages+
Add the following following lines in usys.(: usys.(: ()(,A--getNumFreePages!
Add following following lines in syscall.h: syscall.h: define ()("getNumFreePages //
Add the function function body of getNumFreePages getNumFreePages in kalloc.c kalloc.c / 0eturns the number of free pages. int getNumFreePagesvoid getNumFreePagesvoid!! # if kmem.use"lock! kmem.use"lock! ac1uire2kmem.lock!; int r 3 kmem.numFreePages; kmem.numFreePages; if kmem.use"lock! kmem.use"lock! release2kmem.lock!; return r!; %
Initiali4e numFreePages in kinit$ kmem.numFreePages kmem.numFreePages 3 5;
In kfree kmem.numFreePages kmem.numFreePages 3 kmem.numFreePages kmem.numFreePages 6 $; 77 A new node is added to freelist so increase the number of free pages
In kalloc kmem.numFreePages kmem.numFreePages 3 kmem.numFreePages kmem.numFreePages 8 $ ; 77 A node is popped out from the freelist so decrease the number of free pages.
/
2
0einstalling 0einstalling of page table
9henever the ags are changed in copyuvm function the page table must be reinstalled using: lcrv/ppgdir!!; 77 reinstall the page table
3
eeping track of the reference count of pages
In vm.c add the declaration of array and lock: struct spinlock lock; char pg"refcount'P<)(=>P ?? P@(
In inituvm function: ac1uire2lock!; pg"refcount'v/pmem!! ?? P@(
In allocuvm function: ac1uire2lock!; pg"refcount'v/pmem!! ?? P@(
In deallocuvm free the page only when no other page table is pointing it: ac1uire2lock!; if 88pg"refcount'pa 88pg"refcount'pa ?? P@(
In copyuvm function when a process is forked the refcount of that permanent address should be incremented: ac1uire2lock!; pg"refcount'pa ?? P@(
,hange of copyuvm function
4
Bake the pagetable unwritable and then assign the same permanent addresses to the new page table pde"t copyuvmpde"t pgdir+ uint s4! # pde"t d; pte"t pte; uint pa+ i+ flags; 77char mem; 77No need to allocate new memory if d d 3 setupkvm!! 33 5! return 5; for i i 3 5; i C s4; i 63 P@(IDE!# if pte pte 3 walkpgdirpgdir+ void void ! i+ 5!! 33 5! paniccopyuvm: paniccopyuvm: pte should e&ist!; e&ist !; if Gpte Gpte 2 P=E"P!! paniccopyuvm: paniccopyuvm: page not present!; present !; pte 23 HP=E"9; 77 make this page table unwritable pa 3 P=E"A0pte!; flags 3 P=E"F-A@(pte!; P=E"F-A@(pte!; 77 No need of page allocation if mappagesd+ mappagesd+ void void!i+ !i+ P@(IDE+ pa+ flags! C 5! 77 map the childJs page table to same permanent addresses goto bad; ac1uire2lock!; pg"refcount'pa ?? P@(
K
5
Adding trap trap handler to handle handle pagefaults pagefaults
=o show error on page fault In defs.h void
pagefaultuint err"code!;
In trap.c: case ="P@F-=: pagefaulttf8?err!; break;; break
Adding trap trap handling function function to make make copy of user memory
In vm.c void pagefaultuint err"code! # uint va 3 rcr/!; uint pa; pte"t pte; char mem; if va va ?3 E0NLA(E! # cprintfpid cprintfpid Md Ms: Illegal memory access on ,P Md due to virtual address 5&M& is mapped to kernel code. (o killing the processOn+ processOn + proc8?pid+ proc8?name+ cpu8?id+ va!; proc8?killed 3 $; return;; return % if pte pte 3 walkpgdirproc8?pgdir walkpgdirproc8?pgdir++ void void!va+ !va+ 5!!335! # cprintfpid cprintfpid Md Ms: Illegal memory access on ,P Md due to virtual address 5&M& is mapped to N-- pte. (o killing the processOn+ processOn + proc8?pid+ proc8?name+ cpu8?id+ va!; proc8?killed 3 $; return;; return
% if Gpte Gpte 2 P=E"P!! # cprintfpid cprintfpid Md Ms: Illegal memory access on ,P Md due to virtual address 5&M& is mapped to pte which is not present. (o killing the processOn++ proc8?pid+ proc8?name+ cpu8?id+ va!; processOn proc8?killed 3 $; return;; return % if Gpte Gpte 2 P=E"!! # cprintfpid cprintfpid Md Ms: Illegal memory access on ,P Md due to virtual address 5&M& is mapped to pte which is not accessible to user. (o killing the processOn+ processOn + proc8?pid+ proc8?name+ cpu8?id+ va!; proc8?killed 3 $; return;; return % if pte pte 2 P=E"9! # panicnknown panicnknown page fault due to a writable pte!; pte !; % else # pa 3 P=E"A0pte!; ac1uire2lock!; if pg"refcount'pa pg"refcount'pa ?? P@(
R
=est case c ase testcow test cow include types.h include stat.h include user.h int i 3 ; int mainvoid mainvoid!! # int pid; pid 3 fork!; if pid pid 33 5! # printf$+Number printf$+Number of free pages in child $ before changing variable are: MdOn+ MdOn+ getNumFreePages!!; i 3 K; printf$+Number printf$+Number of free pages in child $ after changing variable due to copy are: MdOn+ MdOn + getNumFreePages!!; % else # wait!; pid 3 fork!; if pid pid 33 5! # printf$+Number printf$+Number of free pages in child / before changing variable are: MdOn+ MdOn+ getNumFreePages!!; i 3 K; printf$+Number printf$+Number of free pages in child / after changing variable due to copy are: MdOn+ MdOn + getNumFreePages!!; % else #
S
printf$+Number printf$+Number of free pages in parent are: MdOn+ MdOn + getNumFreePages!!; wait!; % % e&it!; %