Reputation: 120
I am currently developing a tiny operating system for learning purposes (see here for the code) and I am currently tinkering with paging and memory management.
For handling page faults, I understand I'd need some kind of disk driver in order to replace frames in physical memory. However, I cannot find anything about that online, and all the tutorials I can find have a simple page-fault-handling function which just prints the cause of the page fault, while I'd also want to load the correct page/frame if the fault was raised because the page was not present in memory.
Can anybody provide some help or any online reference?
Upvotes: 0
Views: 382
Reputation: 37222
For the page fault handler; start by determining what caused the page fault and what to do with it - may be illegal access (send signal? core dump? kernel panic?), may be "lazy TLB invalidation" (invalidate TLB entry and do nothing else), may be "copy on write" (copy data to newly allocated page and replace old page with new copy and adjust page permissions), may be "fetch from elsewhere" (swap? memory mapped file?), and may be "being fetched" (tell scheduler thread needs to wait).
For "fetch from elsewhere"; mark the page as "being fetched" somehow (so that if other threads in the same process try to access the page they know they can simply wait, and you don't end up fetching the same data multiple times); and create and submit an (asynchronous) IO request to whatever (file system, swap manager); and tell the scheduler "don't give this thread any CPU time until the virtual memory manager says it can continue" (probably causing task switch).
When the IO request completes (and interrupts something else); examine the status. If the IO request failed (e.g. faulty disk) you'll have to handle that somehow (treat it as unrecoverable error/crash?). If success, map the data into the process (removing the "being fetched" marker while doing that) and then tell the scheduler that any tasks/threads that were waiting for the page can continue now (possibly causing task switch).
Notes:
you will need some scheme to send pages to swap space, and in some cases (memory exhaustion) you may need to send some page/s to swap space to free RAM that you need to fetch pages from swap space. For this you might want to treat some things differently to better handle "swap thrashing" conditions (e.g. so that processes like system utilities and GUI remain responsive and can still be used to kill other processes)
you might want IO priorities - e.g. so that sending data to swap is highest priority, fetching data from swap is 2nd highest priority, and everything else is lower priority. This makes sure that (e.g.) an unimportant background task trying to read a 1234 GiB file doesn't cause the entire OS to grind to a halt.
you might want pre-fetching - e.g. when a huge process terminates and you suddenly find yourself with lots of free RAM, start pre-fetching data from swap space (in "most important first" order?) before its needed.
the disk IO side of it is completely separate; and should probably be implemented and tested before you consider adding support for swap space or memory mapped files.
Upvotes: 4