bp4k wrote: ↑Fri Feb 27, 2026 12:26 pm
If the request is just retried, how do we "cancel" it? Is the only way to loop infinitely in a panic?
Most operating systems have a way to crash userspace programs. If a bad page fault happens in kernel space (in a place where you really didn't expect it), then yes, a panic is the only resolution, but if it happens in userspace, then you crash the faulting process. On POSIX-like systems, the kernel forces a signal like SIGSEGV against the proceess. Forcing a signal is like sending a signal, but if it is blocked or ignored, the signal is atomically unblocked, reset to default handling, and then sent. Result is that the process ends and the parent process can learn that it is because of SIGSEGV.
Note that you can use page faults to tell if userspace is handing bad addresses to the kernel in a system call. The CPU is checking the access anyway, so may as well use it, right? Linux for example expects page faults and general protection faults to happen in userspace access functions, and if they do, it rewrites the IP part of the interrupt frame to go to a handler, that will generally return -EFAULT to its caller.
bp4k wrote: ↑Fri Feb 27, 2026 12:26 pm
Does the trap (specifically for page faults) have to "return" anything?
No, they should only either rectify the page table so the access will succeed or else kill or suspend the program. However, spurious page faults (page faults for no reason) are still possible, and in those cases you really do just return.
I wrote "suspend" in the previous paragraph, because what page faults were originally for was page swapping, where a program would access a page that was moved to some storage elsewhere, and now you have to load it back into RAM before the program can continue. In times of 32GB laptops, not really a huge concern anymore, especially for hobby OSes. How could you ever run out of RAM except by running Google Chrome?