可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
You cannot use the C library in a kernel module, this is even more true for the math library part.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
AFAIK kernel space is separated from user space, and so should the source code. /usr/include is for general programming.
回答6:
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.
回答7:
well you cannot, you can rewrite functions you need in your module, it's dirty but it should work...
回答8:
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.
回答9:
You cannot (often, without lots of kernel know-how to lock and preserve these registers while not impacting other critical sections) use floating point registers in the kernel, and besides it is of course inappropriate to do "processing" in the kernel. Many others have mentioned this. Performance will be terrible. Thus, math.h is not provided for kernel modules. We accept this and move on...
However, as I am also a victim of crazy requirements and completely insane designs forced on us by others, this is a legitimate question. After reducing the usage of the math.h API to minimize the performance impact, you can use floating point emulation (soft-float) via correct compiler settings to implement your required functions without using floating point registers. Kernel code should already compile with these soft-float settings.
In order to implement math.h
functionality, you can look at glibc or uClibc and perhaps others. Both of these libraries have generic "C" implementations of libm which implement math.h
without the use of special intrinsics or platform specific types and should therefore compile just fine in the kernel.
uClibc: The above link takes you directly to the libm section of uClibc.
glibc: After "git"-ing glibc, you'll find what you're looking for in glibc/sysdeps/ieee754/flt-32.
glibc may be more difficult to understand because it is more sophisticated and has more inter-dependencies within itself, but uClibc only provides (at the moment) C89 math.h. If you want single precision (read: faster) or complex math functionality as in C99+, you'll have to look at glibc.
回答10:
Maybe try using double quotes (") instead of single quotes?
回答11:
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.