2

I know that Kprobes can be used to probe any kernel function. But after going through its documents I realise that it is mostly a kind of passive entity. It simply puts a probe in the middle of an execution sequence.

But what if I want to invoke any kernel function directly without bothering about the execution sequence.

How can I achieve that?

Updated:

Note: I want to invoke any kernel function inside my kernel module and not from any user space application.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Utkal Sinha
  • 1,021
  • 1
  • 6
  • 24
  • 1
    `I want to invoke any kernel function inside my kernel module` - So, do that. What is a problem with calling kernel's function inside of kernel module? – Tsyvarev Jan 24 '16 at 15:38
  • Can I just call any kernel function without including any header files related to that function in my module ? – Utkal Sinha Jan 24 '16 at 15:43
  • Yes .. but you may be calling it wrong – Soren Jan 24 '16 at 15:51
  • Here is help: http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html This explains most everything about writing a Linux kernel modules and has examples. – John Hascall Jan 24 '16 at 17:06

2 Answers2

2

Kernel functions cannot be simply invoked from applications that live in user space. System calls are the only functions in user space that can request kernel services.

To call kernel functions directly, if you are interested in kernel programming, you must implement a kernel module. This is a starting point.

EDIT


As you have specified that you want to call kernel functions from within a module, then there is no problem at all. Just follow the link I posted above for the documentation.

terence hill
  • 3,354
  • 18
  • 31
  • Yes, I am building a kernel module only. So if I want to invoke any other kernel function from my module, how would I achieve that ? I know about user space application cannot directly invoke a kernel function. – Utkal Sinha Jan 24 '16 at 15:24
  • I see, I guess Kprobes is already implemented to be used in kernel modules, isn't it? I try to reformulate my answer. – terence hill Jan 24 '16 at 15:29
1

what if I want to invoke any kernel function directly

Not all functions can be used directly at least.

Consider the following points when calling a kernel function in your case.

  • kernel function from different module can be used only if it is exported using EXPORT_SYMBOL family of macros.
  • static functions can't be used directly outside of that file.

Example

Function definition (i2c_smbus_read_byte_data)
http://lxr.free-electrons.com/source/drivers/i2c/i2c-core.c#L2689

Used here
http://lxr.free-electrons.com/source/drivers/i2c/i2c-core.c#L350

Community
  • 1
  • 1
Jeyaram
  • 9,158
  • 7
  • 41
  • 63