With the increasing popularity of the Raspberry Pi 64-bit kernel, many developers are concerned about how to successfully compile 64-bit kernel modules on the Raspberry Pi OS, which still runs a 32-bit user space. This article summarizes Zak Kemble’s practical experience and workflow on this topic, detailing compilation environment preparation, compilation steps, and solutions to common problems, helping readers grasp the key points and details of the process.
1. Background and Need Origin
Zak Kemble initially encountered hardware incompatibility issues caused by carrier network switching when developing a cellular-based email reminder device. To simplify hardware and optimize battery life, he turned to a solution based on WiFi communication. He also needed to compile and load modules for the 64-bit kernel on a Raspberry Pi system running a 32-bit OS but a 64-bit kernel.
This scenario is common in real-world projects and various embedded system development scenarios, so a thorough understanding of this process is valuable.
2. System Environment and Preparation
- Operating System: Raspberry Pi OS 10 (Buster) Lite 32-bit (May 7, 2021)
- Kernel Version: 5.10.17-v8+ (64-bit Kernel)
- Driver Module: r8168 v8.049.01 (Realtek Network Driver)
To ensure a smooth compilation process, you need to prepare the following tools and resources:
- Obtain the r8168 driver source code from the Realtek official website;
- Install and configure the cross-compilation environment to support the aarch64 architecture;
- Ensure that the kernel source code and related header files match the 64-bit kernel version used;
- Prepare necessary build dependencies, such as make, gcc, and bc.
3. Core Compilation Process Analysis
- Setting Up the Development Environment and Dependencies
Prepare an Ubuntu ARM64 base system in the build directory for setting up the cross-compilation environment. Download the kernel source code and switch to a branch compatible with the device kernel version.
- Compilation Preparation
Run the make modules_prepare
command to initialize the compilation environment for external modules and resolve missing module files and link errors.
- Cross-Compile Modules
In the driver source code directory, execute the following command:
make -j4 -C /path/to/kernel/source M=$(pwd)/src modules
Specify the correct kernel source code path and cross-compiler to complete 64-bit module compilation.
- Module Deployment and Loading
Copy the generated .ko
module file to the corresponding kernel module directory:
sudo cp r8168.ko /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/realtek/
sudo depmod $(uname -r)
sudo modprobe r8168
Verify that the module has been successfully loaded and the network interface is active using lsmod
and ip link show
.
4. Common Problems and Solutions
- Exec format error: This error is often caused by a mismatch between the module and kernel architecture or missing module preparation steps. Running
make modules_prepare
and ensuring the cross-compilation environment is correctly configured can effectively avoid it. - Missing dependency warning: Missing symbol warnings are often displayed during compilation. In most cases, this will not affect module loading and can be ignored.
- Kernel Version Mismatch: Configure and use source code and header files consistent with the target kernel version to prevent version conflicts.
5. Technical Highlights and Practical Suggestions
- Utilizing a cross-compilation environment significantly improves compilation efficiency and flexibility;
- Precisely control the correspondence between kernel source code and module versions to ensure compatibility;
- Simplify complex dependency issues through a systematic module preparation process;
- This process is applicable to module development needs for various 64-bit kernels on 32-bit userspace systems and has broad application value.
Zak Kemble, through detailed steps and practical verification, demonstrates the key to successfully compiling 64-bit kernel modules in common yet complex environments. This article systematically presents his core technical solution, which is suitable for developers to implement as a reference and provides a clear path for a deeper understanding of Raspberry Pi kernel module compilation. We hope this article will help you gain a deeper understanding of cross-architecture module development techniques and promote technological innovation in embedded systems.