0

I want to write a kernel space function that invoked by a user space function in Linux, like below:

// kernel space function.
void hello_kernel()
{
  printk(KERN_INFO "Hello kernel space.");
  printk(KERN_INFO "I can do any thing!");
}

// user space function
void hello_kernel();
int main()
{
  printf("Invoking a kernel space function.");
  hello_kernel();
  return 0;
}

I don't have any idea that this sample code is possible or not.

How to write a kernel space function that invoked by a user space function?

Mat
  • 202,337
  • 40
  • 393
  • 406
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
  • You have to write a Kernel module to do that. See http://lwn.net/Kernel/LDD3/ – djf Jun 02 '13 at 12:29
  • 3
    It's called a "system call". Searching for "linux system call" should give you quite a few hints. – Mat Jun 02 '13 at 12:31

1 Answers1

3

The mechanism through which userland code interfaces with kernel code is the system call, which you can read about here.

This means that calling a function directly cannot be done (this is the point of userland memory protection), but rather you'll need add it to the kernel and recompile it.

This information is available on the internet (just search) and has been asked before, for example, here.

Community
  • 1
  • 1
idoby
  • 907
  • 8
  • 20