简体   繁体   English

如何在模块中包含任意内核头文件?

[英]How to include an arbitrary kernel header file in a module?

Situation情况

I'm writing a kernel module for learning purpose and want to use sched_class_highest and for_each_class as defined here .我正在编写一个用于学习目的的内核模块,并希望使用此处定义的sched_class_highestfor_each_class

I saw that if I want to use symbols in the file /include/linux/sched.h, I'll #include <linux/sched.h> .我看到如果我想在文件 /include/linux/sched.h 中使用符号,我会#include <linux/sched.h> So in the same manner, since the file I'm trying to add now is at location /kernel/sched/sched.h , I tried to: #include :所以以同样的方式,由于我现在尝试添加的文件位于/kernel/sched/sched.h位置,我尝试: #include

  • First try: <kernel/sched.h>第一次尝试: <kernel/sched.h>
  • Second try: <kernel/sched/sched.h>第二次尝试: <kernel/sched/sched.h>
  • Third try: <../kernel/sched/sched.h> - just copying paste from a comment in this question第三次尝试: <../kernel/sched/sched.h> - 只是从这个问题的评论中复制粘贴

None of these worked and all gave me the error No such file or directory .这些都没有工作,都给了我错误No such file or directory

I'm not very familiar with C, just trying to learn the kernel and pick up C as I go a long the way.我对 C 不是很熟悉,只是想学习内核并在我走了很长一段路的时候拿起 C。

Here's my Makefile.这是我的 Makefile。 I knew basics about Makefile's target but don't understand the compiling process or what files it needs to feed it...我知道有关 Makefile 目标的基础知识,但不了解编译过程或需要提供哪些文件...

MODULE_FILENAME=my-schedule

obj-m +=  $(MODULE_FILENAME).o
KO_FILE=$(MODULE_FILENAME).ko

export KROOT=/lib/modules/$(shell uname -r)/build

modules:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules

modules_install:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules_install

clean: 
        @$(MAKE) -C $(KROOT) M=$(PWD) clean
        rm -rf   Module.symvers modules.order

insert: modules
        sudo insmod $(KO_FILE)
        sudo dmesg -c

remove:
        sudo rmmod $(MODULE_FILENAME)
        sudo dmesg -c

printlog:
        sudo dmesg -c 
        sudo insmod $(KO_FILE)
        dmesg

Questions问题

  1. How do I reference those 2 symbols in my kernel module?我如何在我的内核模块中引用这 2 个符号?
  2. Are those symbols supposed to be referenced directly by a standard module ?这些符号是否应该由标准模块直接引用? If so, what is the standard way of doing it?如果是这样,这样做的标准方法是什么?
  3. Is the Makefile related to how I can import a file in kernel code into my module? Makefile 是否与我如何将内核代码中的文件导入到我的模块中有关?

How do I reference those 2 symbols in my kernel module?我如何在我的内核模块中引用这 2 个符号?

You should not need those in a "normal" kernel module.您不应该在“普通”内核模块中需要它们。 If you really do, re-define them in your module.如果您真的这样做了,请在您的模块中重新定义它们。

Are those symbols supposed to be referenced directly by a standard module?这些符号是否应该由标准模块直接引用?

They are not, that particular sched.h file is only supposed to be used by scheduler code (files in kernel/sched ).他们不是,那个特定的sched.h文件只应该由调度程序代码( kernel/sched文件)使用。 That's why it is not exported (ie not in the include dir).这就是为什么它没有被导出(即不在include目录中)。 You will not find them in your headers folder ( /lib/modules/.../build ).您不会在标头文件夹 ( /lib/modules/.../build ) 中找到它们。

Is the Makefile related to how I can import a file in kernel code into my module? Makefile 是否与我如何将内核代码中的文件导入到我的模块中有关?

Unsure what this means really... if you want to know if there is some way to configure your Makefile such that it will be possible to include that file, then there is not.不确定这到底意味着什么……如果您想知道是否有某种方法可以配置 Makefile 以便可以包含该文件,那么就没有。 If you are building using the kernel headers exported by the kernel in /lib/modules (which is what you are doing when you hand over control to the kernel's Makefile with $(MAKE) -C $(KROOT) ) then the file is simply not there.如果您使用内核在/lib/modules导出的内核头文件进行构建(这就是您使用$(MAKE) -C $(KROOT)控制权移交给内核的 Makefile 时$(MAKE) -C $(KROOT) ),那么该文件就是不在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM