如何在內核源文件中包含 math.h #include <math.h>? (How to include math.h #include <math.h> on kernel source file?)


問題描述

如何在內核源文件中包含 math.h #include <math.h>? (How to include math.h #include <math.h> on kernel source file?)

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.

(by MadniDavid CournapeauSweeneyFreeMemorycodymanixAndrei Tanasescu)

參考文件

  1. How to include math.h #include <math.h> on kernel source file? (CC BY-SA 3.0/4.0)

#module #C #math #kernel






相關問題

如何在內核源文件中包含 math.h #include <math.h>? (How to include math.h #include <math.h> on kernel source file?)

根據服務器配置指令初始化 nginx 模塊的共享內存 (Initialize an nginx module's shared memory depending on a server configuration directive)

選擇安裝 python 模塊的位置 (Choosing where to install python module)

如何從 CLI 中查找 php 中內置模塊的版本號 (how to find the version numbers of built in modules in php from the CLI)

java.lang.SecurityException:無法為受信任的 CA (JBoss AS 7) 設置證書 (java.lang.SecurityException: Cannot set up certs for trusted CAs (JBoss AS 7))

自定義 AngularJS 過濾器忽略我的參數並接收其他一些 scope.data (custom AngularJS filter ignores my parameter and receive some other scope.data)

具有“插件”能力的 ASP.NET 內網應用程序 (ASP.NET intranet application with "plug-in" ability)

VBscript 有模塊嗎?我需要處理 CSV (Does VBscript have modules? I need to handle CSV)

自定義 Mage_Sales_Model_Quote_Address_Total_Abstract 計算兩次:帳單和送貨地址 (Custom Mage_Sales_Model_Quote_Address_Total_Abstract calculate twice: billing&shipping address)

Ocaml 函子、模塊和子模塊 (Ocaml Functors, Modules and Submodules)

類的 Python 模塊結構 (Python module structure for classes)

我想使用 HPC 的 gpu 並嘗試 module add CUDA ...但出現錯誤。錯誤是“Lmod 檢測到以下錯誤: (I want to use the gpu of the HPC and try module add CUDA... But errors occurs. The error is "Lmod has detected the following error:)







留言討論