Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a user-space function from the kernel space

Im writing a custom device driver in linux that has to be able to respond very rapidly on interrupts. Code to handle this already exists in a user-space implementation but that is too slow as it relies on software constantly checking the state of the interrupt line. After doing some research, I found that you can register these interrupt lines from a kernel module, and execute a function given by a function pointer. However the code we want to execute is in the user-space, is there a way to call a function in the user-space from a kernel space module?

like image 660
S E Avatar asked Mar 09 '11 13:03

S E


2 Answers

You are out of luck with invoking user-space functions from the kernel since the kernel doesn't and isn't supposed to know about individual user-space application functions and logic, not to mention that each user-space application has its own memory layout, that no other process nor the kernel is allowed to invade in that way (shared objects are the exception here, but still you can't tap into that from the kernel space). What about the security model, you aren't supposed to run user-space code (which is automatically considered unsafe code in the kernel context) in the kernel context in the first place since that will break the security model of a kernel right there in that instant. Now considering all of the above mentioned, plus many other motives you might want to reconsider your approach and focus on Kernel <-> User-space IPC and Interfaces, the file system or the user-mode helper API(read bellow).

You can invoke user space apps from the kernel though, that using the usermode-helper API. The following IBM DeveloperWorks article should get you started on using the usermode-helper Linux kernel API:

Kernel APIs, Part 1: Invoking user-space applications from the kernel

like image 134
Shinnok Avatar answered Nov 15 '22 06:11

Shinnok


I think the easiest way is to register a character device which becomes ready when the device has some data.

Any process which tries to read from this device, then gets put to sleep until the device is ready, then woken up, at which point it can do the appropriate thing.

If you just want to signal readyness, a reader could just read a single null byte.

The userspace program would then just need to execute a blocking read() call, and would be blocked appropriately, until you wake it up.

You will need to understand the kernel scheduler's wait queue mechanism to use this.

like image 39
MarkR Avatar answered Nov 15 '22 05:11

MarkR