sifive / freedom-u-sdk

Freedom U Software Development Kit (FUSDK)
278 stars 125 forks source link

Version magic issue for compiling kernel module when passing in another version of Linux kernel #166

Closed yslys closed 2 years ago

yslys commented 2 years ago

Hi, I would like to make some changes to the Linux kernel, recompile it, and pass it in with -kernel option to qemu-system-riscv64.

I did the above without changing the disk image, in which it contains the Linux kernel version 5.10.25-yocto-standard.

I booted up the VM and checked the kernel version with uname -r, which showed 5.16.15, as expected.

In order to compile the kernel module I designed myself, I executed the following command line:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

But it did not work, so I changed a bit to the following:

make -C /lib/modules/5.10.25-yocto-standard/build M=$(PWD) modules

It worked, but when I was trying to load the kernel module, it showed the following:

version magic '5.10.25-yocto-standard SMP preempt mod_unload riscv' should be '5.16.15 SMP preempt mod_unload riscv'
insmod: ERROR: could not insert module my_kernel_module.ko: Invalid module format

May I know how to solve it without changing to the same version of Linux kernel (5.10.25-yocto-standard)?

If the only solution is to change to 5.10.25-yocto-standard, may I know where I could download the source of Linux 5.10.25-yocto-standard?

Is there another way of generating the disk image which allows me to put the kernel I specified (for instance, v5.16.15) into it?

Note: I noticed that there is a disk image tar file in build/tmp-glibc/deploy/images directory with name demo-coreip-cli-qemuriscv64-20220101074232.rootfs.tar.bz2.

Thanks in advance!

yslys commented 2 years ago

The solution is as follows:

  1. Download Linux kernel source, check out any version you want
  2. Add the kernel module to /drivers directory to eliminate the need of compiling it as an out-of-tree kernel module. Detailed steps could be found here: https://stackoverflow.com/questions/11710022/adding-new-driver-code-to-linux-source-code.
  3. The link above targets ARM, not RISCV, so needs to change ARM to RISCV. The link above also mentions you need to modify kernel_source/arch/arm/configs/--defconfig, it just means the file with defconfig as suffix. So you could simply modify the file kernel_source/arch/riscv/configs/defconfig to set CONFIG_YOUR_DRIVER=y.
  4. Re-compile the kernel source, and pass the generated Image file to -kernel option to QEMU, then run QEMU.
  5. The kernel module will be automatically loaded had there been a device supported. Don't forget to mknod as what you would normally do after insmod when dealing with out-of-tree kernel modules.