What happens after segmentation fault in linux kernel?

2.8k Views Asked by At

while I was thinking of making a networked paging (request the faulting page from remote node), I got this question:

First, let's consider the following steps:

1) a user-space program tries to access at memory X.

2) MMU walks the page table to find the physical address of X.

3) while walking the page table, it notice that the page table entry is invalid.

4) CPU traps and is catched by the Linux trap vector. (In ARM case, but I think x86 is also the same, right?)

5) At this point, I can retrieve the proper data from remote node, copy into some physical address and map it in page table.

6) Here goes the question: After this point, would the program that has page fault at X safely read the data?, Then, does it mean MMU or CPU somehow remembers the page faulting page table entry and return to that entry and resume the walking of page table?

If any of the steps are not right, please enlighten me.

2

There are 2 best solutions below

0
On

The solution is tricky and non-portable.

You can get the values of the CPU registers, when the segmentation fault occurred, from a signal handler (link: http://man7.org/linux/man-pages/man2/sigaction.2.html). You need to analyse these to decide whether you can fix the situation. First you need to check that the instruction pointer is valid. Then, you need to check that the faulty address lies in a valid range. Then, you need to map memory for the non existent pages with mmap() system call. Then, you need to copy the required data to these pages. After the signal handler returns, the process will resume from where the segmentation fault had occurred.

0
On

Data abort handler just assigns to the pc the same value as before the data abort handling started, and instruction gets executed again, with right data in place, so data abort won't happen again.