|
|
|
|
|
by kentonv
1594 days ago
|
|
Sort of, but not exactly. You can't redirect the program to a different address, but you can respond to the fault by mapping memory at address 0x4000 and placing whatever you want in it. Perhaps, you can map the memory as an alias of some other page, so that they refer to the same underlying physical memory. But the program will still see the memory as living at 0x4000, it won't jump to a different address. (Though I suppose if you're specifically thinking of the instruction pointer faulting at 0x4000, you could respond by filling in that address with a jump instruction jumping to the address of your choice...) To map memory at a specific address, you use mmap() with the MAP_FIXED flag. This lets you tell the kernel: "Please put these pages at this virtual address, replacing what's already there." Note that userfaultfd is not the only way to detect and handle faults. You can also register a signal handler for SIGSEGV. The signal handler function receives information from the kernel specifying what address caused the fault. If, in the signal handler, you use mmap() to map memory at that address, and then return, the program will continue as if nothing happened. userfaultfd allows you to do the same but from a different thread or process. The SIGSEGV signal handler would actually run inside the thread that faulted, but with userfaultfd the faulting thread pauses and some other thread or process receives the notification. |
|