0

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?

  • I really don't understand what you mean by "some kind of disk driver," and you don't tell us what "tutorials you find ... prints the cause of the page fault." – Mike Robinson Feb 24 '20 at 22:02
  • Also ... what better example to study than Linux? A simple google-search for "linux page fault handling" produced links to numerous detailed online documents. – Mike Robinson Feb 24 '20 at 22:04
  • @MikeRobinson As far as I know, handling virtual memory involves being able to swap out currently unused pages to the disk and then swap them back in when they are needed. In order to do this, I need some way of "communicating" with the disk: a driver. – Davide Della Giustina Feb 24 '20 at 22:04
  • You will need a filesystem anyway, not just to be able to swap out memory blocks. –  Feb 24 '20 at 22:05
  • Presumably your operating system already has some kind of I/O architecture with the capability to have "disk drivers" and handle asynchronous requests. Once again, Linux has excellent architecture ideas, which have evolved somewhat over time. – Mike Robinson Feb 24 '20 at 22:06
  • @a_horse_with_no_name why do I need an entire filesystem? – Davide Della Giustina Feb 24 '20 at 22:07
  • Well, "horse," the OP actually does have a choice there. One way to do it is with a "swap partition." Another, more popular these days, is to use a system-owned "swap file" that lives in the filesystem. Again and again and again, I point to Linux. One way to set up Linux is with a swap file. Another way is with a swap partition. (Also note Linux's "kernel threads" architecture, and the various historical mechanisms that it replaced.) – Mike Robinson Feb 24 '20 at 22:07
  • @MikeRobinson Actually, I am writing the OS, and I have currently no disk driver: that's what I am asking for. – Davide Della Giustina Feb 24 '20 at 22:08
  • Well, Davide, you're just going to have to invent an I/O architecture, too! Maybe you need to do that next. – Mike Robinson Feb 24 '20 at 22:12
  • @MikeRobinson Well, not really. I am just writing the software, the hardware is already emulated by QEMU (which I am currently using), so I just need to write proper software to dirve that hardware, but I can't find suitable documentation. – Davide Della Giustina Feb 24 '20 at 22:17

1 Answers1

3

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.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • Yeah, my question was a little bit general, but the main point I was interested in is the last one from your answer. I was just asking if anyone can provide some documentation or something like that for reading/writing on a disk. Thank you anyway, definitely a good answer! – Davide Della Giustina Feb 25 '20 at 15:11