genie
genie

Reputation: 41

user defined page fault and exception handlers

I am trying to understand if we can add our page fault handlers / exception handlers in kernel / user mode and handle the fault we induced before giving the control back to the kernel. The task here will be not modifying the existing kernel code (do_page_fault fn) but add a user defined handler which will be looked up when a page fault or and exception is triggered

One could find tools like "kprobe" which provide hooks at instruction, but looks like this will not serve my purpose.

Will be great if somebody can help me understand this or point to good references.

Upvotes: 4

Views: 3810

Answers (4)

amrzar
amrzar

Reputation: 1

I do not think it would be possible - first of all, the page fault handler is a complex function which need direct access to virtual memory subsystem structures.

Secondly, imagine it would not be an issue, yet in order to write a page fault handler in user space you should be able to capture a fault which is by default a force transfer to kernel space, so at least you should prevent this to happen.

To this end you would need a supervisor to keep track of all memory access, but you cannot guarantee that supervisor code was already mapped and present in memory.

Upvotes: 0

zoska
zoska

Reputation: 1723

If you don't won't to change the way kernel handles these fault and just add yours before, then kprobes will server your purpose. They are a little difficult to handle, because you get arguments of probed functions in structure containing registers and on stack and you have to know, where exactly did compiler put each of them. BUT, if you need it for specific functions (known during creation of probes), then you can use jprobes (here is a nice example on how to use both), which require functions for probing with exactly same arguments as probed one (so no mangling in registers/stack).

You can dynamically load a kernel module and install jprobes on chosen functions without having to modify your kernel.

Upvotes: 2

vy32
vy32

Reputation: 29645

You want can install a user-level pager with gnu libsegsev. I haven't used it, but it seems to be just what you are looking for.

Upvotes: 0

Karmastan
Karmastan

Reputation: 5696

From user space, you can define a signal handler for SIGSEGV, so your own function will be invoked whenever an invalid memory access is made. When combined with mprotect(), this lets a program manage its own virtual memory, all from user-space.

However, I get the impression that you're looking for a way to intercept all page faults (major, minor, and invalid) and invoke an arbitrary kernel function in response. I don't know a clean way to do this. When I needed this functionality in my own research projects, I ended up adding code to do_page_fault(). It works fine for me, but it's a hack. I would be very interested if someone knew of a clean way to do this (i.e., that could be used by a module on a vanilla kernel).

Upvotes: 3

Related Questions