The HyperNews Linux KHG Discussion Pages

Idea: How to call a user routine from kernel mode

Forum: The Linux Kernel Hackers' Guide
Re: Question How to call a function in user space from inside the kernel ? (Ronald Tonn)
Date: Thu, 09 Apr 1998 21:11:48 GMT
From: David Welch <welch@mcmail.com>

There are several possible solutions

i) On kernels before 2.1.xx the kernel uses a different code segment to user programs. To call user space would require a far call. The functions get_user and put_user similarly use a segment override to access user space. Calls are far more complicated because (for protection) the processor won't allow a user routine to return to a more priveleged code segment. The best way round this would be to use a similar method to signal handlers i.e. setup a routine on the stack which on return from the signal handler executes a system call which returns to kernel mode. See arch/i386/kernel/signal.c for more information.

The problem is further complicated because on the i386 executing a system call will restore the default kernel stack. The vm86 routines allocate a new kernel stack so when a GPF happens in vm86 mode they can restore the original stack and return to the caller process. You may be able to use a similar method.

ii) On kernels after 2.1.xx the kernel segment has the same base as user space. It should be possible to directly call a user routine and have it return normally. However the user routine will have normal kernel priveleges (a big security hole!).

iii) Use kerneld. If the desired routine can be written as a seperate program then kerneld can be called from kernel mode to execute it. An example is calling the request-routine script when a network connection is attempted.

iv) If the calling process is multithreaded then you should be able to use the ipc semaphores from kernel mode to signal to another thread to execute the routine.