How to include math.h #include <math.h> on kernel source file?

14,639

Solution 1

In experts view , its NOT a good approach to communicate data between kernel space and user space. Either fully work on kernel space OR only on user space.

But one solution can, use read() and write() command in a kernel module to send the information between user space and kernel space.

Solution 2

You cannot use the C library in a kernel module, this is even more true for the math library part.

Solution 3

You can't include a userspace C module in kernel space. Also are you sure that you want to be doing this? This thread may help http://kerneltrap.org/node/16570. You can do math functions inside the kernel, just search around on http://lxr.linux.no/ for the function you need.

Solution 4

Standard libraries are not available in the kernel. This includes libc, libm, etc. Although some of the functions in those libraries are implemented in kernel space, some are not. Without knowing what you're trying to call, it's impossible to say for sure whether or not you should be doing what you're trying to do in kernel space.

I should further note that the kernel does NOT have access to the FPU. This is to save time when switching tasks (since saving the FPU registers would add unnecessary overhead when performing context switches). You can get access to the FPU from kernel space if you really want it, but you need to be really careful not to trash the user space's FPU registers when doing so.

Edit: This summarizes the caveat about the FPU much better than I did.

Solution 5

Floating point operations is not supported in the kernel. This is because when switching from kernel context to user context, registers must be saved. If the kernel would make use of floating point, then also the floating point registers would have to be saved also, which would cause bad performance for each context switch. So because floating point is very rarely needed, especially in the kernel it is not supported.

If you really have to:

  • maybe you could compile your own kernel with floating point support
  • you could block context switch within your floating point operations
  • the best would be to use fixed point arithmetics.
Share:
14,639
Madni
Author by

Madni

Updated on July 01, 2022

Comments

  • Madni
    Madni almost 2 years

    I am trying to include math.h in my Linux kernel module. If I use,

    #include '/usr/include/math.h'
    

    It give me theses errors:

    error: features.h: No such file or directory
    error: bits/huge_val.h: No such file or directory
    error: bits/mathdef.h: No such file or directory
    error: bits/mathcalls.h: No such file or directory
    

    Why is this?

  • Madni
    Madni almost 15 years
    It uses double quotes " and < But still with the same problem
  • samoz
    samoz almost 15 years
    Wouldn't using the / mark tell it to reference the file system though?
  • FreeMemory
    FreeMemory almost 15 years
    My initial reaction: don't do that. Can you? Probably. But the divide between kernel space and user space is there for a damn good reason. There's no guarantee that by the time the kernel finishes doing what it needs to do that your application will be running, or even be alive. I think we need more detail about what you're trying to do. Also, unrelated: StackOverflow isn't a forum... if you need to add information to your question, please edit your original question.
  • PeterAllenWebb
    PeterAllenWebb almost 15 years
    I understand, but it is funny that the math part is more restricted than restricted. :)
  • FreeMemory
    FreeMemory almost 15 years
    Can't you just make your changes in your user-space application? I.e. if you get an event which moves to a bad location, you detect it in user-space?