Predefine a macro in kernel module - linux

I would like to define a macro for my kernel module by using the -D flag, but I can't figure out how to do it in a custom loadable kernel module.
Just to be clear, to set the macro TEST to 1 I usually do something like:
cc -D TEST=1 file.c -o file
And inside the file.c I have
#if TEST
//do something
#endif
Now, having the same code in a kernel module, how can I set TEST to 1 without touching the code?
This is my the Makefile:
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Since the -C flag it's recursively calling multiple makefiles, adding -D TEST=1 does not work, I get the following error:
make: invalid option -- 'D'
Anybody knows how to solve this problem?
Thanks in advance.

As suggested by #n.m. in the comments, the solution is to use the EXTRA_CFLAGS. So in my case it would be something like this:
all:
make EXTRA_CFLAGS=-DTEST=2 -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
or simply
EXTRA_CFLAGS:= -D TEST=2
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

Related

How to write a Linux kernel module makefile?

I'm trying to write a simple hello world kernel module. I'm working in Ubuntu 18.04.2 LTS on Virtual Box. In the directory /usr/src I created a directory named hello and inside that hello directory I've created hello.c and a makefile. Here is my makefile:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
When I make the command "sudo make" I get this output:
make -C /lib/modules/5.3.0-28-generic/build M= modules
make[1]: Entering directory '/usr/src/linux-headers-5.3.0-28-generic'
make[2]: *** No rule to make target 'arch/x89/tools/relocs_32.c', needed by 'arch/x86/tools/relocs_32.o'. Stop.
arch/x86/Makefile:232: recipe for target 'archscripts' failed
make[1]: *** [archscripts] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-28-generic'
makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
I've tried a few variations of my makefile including changing "PWD" to "shell pwd". I've installed all the essential tools and libraries as far as I know. What could be the problem here?
Variable PWD is set by the shell, so your Makefile relies on make invocation to be performed from the shell, as we normally do when type make in the terminal.
But sudo make executes make from a plain user environment. This environment lacks for PWD variable, so your Makefile behaves incorrectly. This can be found from your output:
make -C /lib/modules/5.3.0-28-generic/build M= modules
Use $(shell pwd) expression instead, it is more reliable:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
Alternatively, you may call make with root privileges using this way:
sudo /bin/sh -c make
This way make is executed not from the plain root environment, but under an intermediate shell. This shell sets PWD variable.
Update
As noted by #Ian Abbott in the comments, another alternative for $(PWD) is $(CURDIR). Variable CURDIR is set by the make itself (precisely, by GNU Make). See also that question: bash: What is the difference between PWD and CURDIR?.
Your Makefile should look like this
obj-m += hello.o
Then your build command will look like
make -C $KDIR M=$PWD
Where $KDIR is the source tree of your kernel build.
is the way. The rest is handled by the kbuild system.
Please refer to refer to the official documentation for building off-tree modules

How to provide include directory path in kernel module Makefile

I was learning kernel interrupt using a small demo kernel module
which use these two header include
asm/exception.h
asm/mach/irq.h
My Makefile is
ifeq (${KERNELRELEASE},)
KERNEL_SOURCE := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
make -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
clean:
make -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
else
obj-m := irq_demo.o
endif
The error I am getting
irq_demo.c:9:27: fatal error: asm/exception.h: No such file or directory
#include <asm/exception.h>
I found asm/exception.h in my system in /usr/src/linux-headers-3.16.0-30-generic/arch/arm/include/
[1]But how to include this path in Makefile
[2]Is /usr/src/linux-headers-3.16.0-30-generic/include/asm-generic/ linked with arch/arm/include/asm/ ? if yes , than How ?
First try
adding ARCH=arm after make
i.e.
make ARCH=arm -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
============================= OR =============================
Can you try adding line before make
CARGS = -I /lib/modules/$(shell uname -r)/arch/arm/include/
make $(CARGS) -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

Is there any way to use premake to build a linux kernel module?

I'm trying to build a kernel module in part of a larger project that uses premake5 to generate the Makefiles. However, there's no documentation on using premake to build a kernel module. Is this possible in premake? I'd like to stay consistent with using Premake in the project, if possible.
Thank you in advance for any insights.
My attempt thus far:
project "mymod"
kind "ConsoleApp"
language "C"
location "./"
files { "**.h", "**.c" }
if _ACTION == "clean" then
os.rmdir("obj")
end
buildcommands {
"make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules %{cfg.buildcfg}"
}
rebuildcommands {
"make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules %{cfg.buildcfg} rebuild"
}
cleancommands {
"make clean %{cfg.buildcfg}"
}
buildoptions{ "-Wno-unused-value", "-Wno-sign-compare",
"-Wno-unused-parameter", "-Wno-unused-function",
"-Wno-missing-field-initializers"}
configuration {"Release"}
defines { "NDEBUG" }
flags { "Optimize", "ExtraWarnings", "FatalWarnings", "FloatStrict", "EnableSSE2" }
What follows is an example of a working Makefile for building a kernel module. Granted, it's short and probably doesn't seem worth the trouble of using Premake, but that's because it is missing several object files from the listing, which currently have to be entered manually.
# Note that multiple KBUILD_EXTRA_SYMBOLS Entries are space-separated
KBUILD_EXTRA_SYMBOLS=$(ETHERCAT_TOPDIR)/Module.symvers \
$(HOME_DIR)/shmdrv/Module.symvers \
# the output kernel module
obj-m := corert.o
# the object files used
corert-objs := ctsrt_main.o globals.o ProcessMain.o \
CtsSharedData.o ProcessData.o \
ProcessSdo.o ProcessSimulate.o \
FsmWrite.o
ETHERCAT_TOPDIR = /usr/local/src/ethercat
EXTRA_CFLAGS = -I$(ETHERCAT_TOPDIR)/include -ffast-math -mhard-float
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) \
modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Linux modules version error "Invalid module format"

I made Linux external module and because my module need Linux kernel symbol,
I set the Modversion config and make it.(also did make modules_install)
My module was made exactly in lib/modules/(my linux)/extra/
But when I insert my module, shell prints error message
insmod: ERROR: could not insert module oxen_aggregator_module.ko: Invalid module format
This is dmesg contents
[ 341.458351] oxen_aggregator_module: version magic '3.17.8-gentoo-r1 SMP mod_unload modversions ' should be '3.17.8-gentoo-r1 SMP mod_unload '
But I need modversions flag because if I didn't set that flag, Module.symvers have just 0x00000000 addresses.
Could you help me? How can I solve?
My Make file
SRCS = oxen_aggregator_module.c
OBJS = $(SRCS:.c=.o)
obj-m += $(OBJS)
KBUILD_EXTRA_SYMBOLS={/usr/src/linux-$(shell unamr -r)/Module.symvers}
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
$(RM) Module.markers modules.order
You attempt to load module into kernel, for which it(module) hasn't be built. Or you build module for one kernel, but use KBUILD_EXTRA_SYMBOLS with Module.symvers file, created for another kernel.
If you need to use KBUILD_EXTRA_SYMBOLS with kernel's Module.symvers file, then it is definitely mess with different kernels somewhere.

module compiling : asm/linkage.h file not found

I am trying to compile an example of "hello world" Kernel Module,
problems found on ubuntu 11.04, kernel 3.2.6, gcc 4.5.2 and fedora 16, kernel 3.2.7, gcc 4.6.7.
code:
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init hello_init (void)
{
printk("Hello module init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
compiled with:
gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o
error:
In file included from /usr/src/linux/include/linux/kernel.h:13:0,
from /usr/src/linux/include/linux/cache.h:4,
from /usr/src/linux/include/linux/time.h:7,
from /usr/src/linux/include/linux/stat.h:60,
from /usr/src/linux/include/linux/module.h:10,
from hello.c:1: /usr/src/linux/include/linux/linkage.h:5:25: fatal error:
asm/linkage.h: file not found
then I found in /usr/src/linux/include/ there is no folder named 'asm' but 'asm-generic';
so I made a soft link 'asm' to 'asm-generic', and compiled agail:
this time the error was:
In file included from /usr/src/linux/include/linux/preempt.h:9:0,
from /usr/src/linux/include/linux/spinlock.h:50,
from /usr/src/linux/include/linux/seqlock.h:29,
from /usr/src/linux/include/linux/time.h:8,
from /usr/src/linux/include/linux/stat.h:60,
from /usr/src/linux/include/linux/module.h:10,
from hello.c:1: /usr/src/linux/include/linux/thread_info.h:53:29: fatal error:
asm/thread_info.h: file not found
So I realized I was wrong, but why ? T_T
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
is a proper way to build modules see kbuild documentation
And to see difference beetween your compiler invocation you could
cat /lib/modules/$(shell uname -r)/build/Makefile
And analyze an output
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Here hello.c is your kernel source file. just use make to build your hello.ko module.
asm should be a link to the actual architecture you're compiling for, not to asm-generic.
You can't compile a generic kernel module, that would work on a generic architecture. You have to compile it for the particular architecture you're going to use.
I don't know why the asm didn't exist. It should be created as part of the configuration process.
You might get other errors later, if configuration is incomplete in other ways.
The asm includes (such as linkage.h) are architecture specific. There should be a set of directories under:
/usr/src/kernels/(kernel version goes here)/arch
that provide specific includes for the specific CPU architecture you are targeting your code to be compiled for.
Try adding this to your Makefile:
KVERSION :=R(shell uname -r)
and add the kernel and architecture (x86 in this example):
INCDIRS = -I./include -I/usr/src/kernels/$(KVERSION)/include -I/usr/src/kernels/$(KVERSION)/arch/x86
module compiling : asm/linkage.h file not found
This means this particular file was not found in specified DIR, which gets specified when we use -I option with make.
We can either link that asm-generic to asm, if all headers are present in asm-generic, or we can use make utility.
Make utility is preferred in case of building kernel modules.
Create a 'Makefile' in working DIR.
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Use of -C option will change to DIR specified before reading the makefiles or doing anything else.
So to avoid this error, use -C option with DIR/lib/modules/$(shell uname -r)/build
By this your program will be able to find required files, you will get hello.ko file.
You can add this to kernel modules by
sudo insmod hello.ko
Similarly you can remove by
sudo rmmod hello

Resources