views:

1316

answers:

10

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?

A: 

Maybe try using double quotes (") instead of single quotes?

samoz
It uses double quotes " and <But still with the same problem
Madni
+1  A: 

AFAIK kernel space is separated from user space, and so should the source code. /usr/include is for general programming.

Andrei Tanasescu
Wouldn't using the / mark tell it to reference the file system though?
samoz
+1  A: 

This suggests that doing floating point math in the kernel is not as simple is in user-space code. Another instance suggesting that this is hard.

Still looking for a more definitive answer.

dmckee
+8  A: 

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

David Cournapeau
I understand, but it is funny that the math part is more restricted than restricted. :)
PeterAllenWebb
+2  A: 

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.

Sweeney
A: 

Thanks a lot for your comments

To use math functions

Is it possiable to make a plane C application and pass variables from kernel source file. So the C Application will compute the variables and sends back the information .

Kernel source file (kernel space) ---> C Application (user space)

                                       |

                                   <---|

Kernel source file

So we may include header file in kernel source code. In case of any event, it pass the values to a C application (user space)

Details: I am trying to modify my HID joystick events(absolute x, y) So It may only move to the improved location, which will be genarated by my application, with some math functions like (pow, tan,etc).

So I used hid-input.c to get raw events, and modify them. which will be used for input subsystem through hid kernel module –

Looking for your comments

Regards.

Madni
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.
FreeMemory
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?
FreeMemory
+3  A: 

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.

FreeMemory
A: 

well you cannot, you can rewrite functions you need in your module, it's dirty but it should work...

Lopoc
That's only if he doesn't need any floating point operations...
FreeMemory
+1  A: 

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.
codymanix
A: 

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 reed() and write() command in a kernel module to send the information between user space and kernel space.

Madni