Linux Kernel Development

This page is for new or inexperienced developers wanting to work directly on the linux kernel.

Kernel sources

When choosing a kernel source you need to determine what support you are interested in.

Mainline upstream Linux kernel

Gateworks usually recommends using the mainline or upstream Linux kernel because it has the most up-to-date features and bug-fixes that have been accepted and peer reviewed from the widest array of kernel developers around the world.

The 'mainline' Linux kernel is the one officially released by Linus Torvalds and has an approximate release cycle of every 6 months.

Stable Kernel

The Stable Kernel adds backported bugfixes on top of previous mainline Linux kernel releases. A variant of this is the Long Term Support (LTS) kernel versions that are supported by the community for typically 4 years.

Our kernel source has a default config file (arch/arm/configs/gwventana_defconfig) which which we use but keep in mind that in general wireless drivers and subsystems are not defined there because those modules come from linux-backports instead.

Toolchains

A toolchain is a set of compiler tools (typically gcc) and libraries necessary for compiling and linking source (and possibly debugging, diss-assembling, etc).

Typically this includes the following (for the GNU C Compiler):

ar - linker

as - assembler

c++/g++ - C++ compiler

gcc - C compiler

ld - linker

nm - list symbols from object files

strip - strips debug info from object files

objcopy - copy and translate object files

objdump - display info from object files

ranlib - generate index to archive

lib/lib* - stdc libraries

include/ - stdc library headers

The kernel (which is written entirely in ANSI-C) is compiled with gcc and does not link against the stdc library.

Userspace and root filesystem

Userspace refers to anything in a Linux based OS which is 'not' the kernel. After initialization, the Linux kernel mounts the rootfs and executes PID1 (/sbin/init) which is the start of userspace. The init process and any processes it launches are part of userspace. When applications act on device files such as /dev/sda they use an ioctl API to interact with kernel functions from userspace. A useful way to determine the uses per directory is to execute a man hier on your Ubuntu system.

A root filesystem (sometimes referred to as 'a userspace') contains everything you need for a Linux based OS other than the kernel itself. The root filesystem could contain the kernel, but only if the bootloader mounts it and launches it from the rootfs (ie the Gateworks Ventana product family). Note that kernel modules are contained on the root filesystem but they are not considered userspace (yet the insmod/rmmod/modprobe apps that load/remove them are).

You likely have one of the following userspaces you want to work with:

In general, you should be able to mix and match kernels and userspace within reason, but note that certain Linux based OS's may require specific kernel functionality (ie Android requires several Android specific kernel features to exist).

When working on mainline Linux patches, Gateworks developers often use either an OpenWrt userspace root filesystem or a Yocto userspace root filesystem depending on their needs.

Building the Linux kernel (out-of-tree)

Building the Linux kernel 'out-of-tree' refers to building it outside of the Board Support Package development environment. Each BSP that Gateworks provides builds the kernel for you, but doing constant kernel development within those BSP's may not be very time efficient.

Gateworks developers typically do kernel development out-of-tree and boot the kernel over the network to speed up development.

If working with kernel modules, one could remove the module (rmmod), download it, install it (insmod) again and test and avoid constantly rebooting the board. Often its just as quick to build a driver static in the kernel, configure your board for network booting, and just rebuild/reboot continually.

Prerequisites:

Linux development host: Gateworks uses and supports Ubuntu which is used here as an example, but other Linux variants can work as well

Toolchain for the CPU architecture of the boards you are working with:

Newport uses the Cavium CN80xx OcteonTX SoC which has ARMv8 CPU cores. You can download the toolchain provided Cavium for their SDK ​here or use one that you have pre-built from OpenWrt

Setup shell environment for building which depends on the toolchain you are using. This includes the following env variables used by the kernel build system:

ARCH - the architecture to build (arm for 32bit ARM such as Ventana IMX6, arm64 for 64bit ARMv8 such as Newport CN80xx)

CROSS_COMPILE - the prefix the toolchain you are using has prior to the gcc executables (everything before the 'gcc').

PATH - make sure the directory where the toolchains compiler (${CROSS_COMPILE}gcc) executable exists. Typically you will prepend this to the default PATH

INSTALL_MOD_PATH - where to install kernel modules too if using the 'modules_install' build target (in our examples we install to ./install)

INSTALL_HDR_PATH - where to install header files if using the 'headers_install' build target (in our examples we install to ./install; note that this needs to be passed into make as opposed to an env var)

LOADADDR - where to relocate/uncompress the kernel to when using the uImage target

Note that STAGING_DIR is something required by the OpenWrt toolchain... your toolchain may differ

Some troubleshooting steps:

echo$ARCH# make sure this is not empty and is set to your target CPU architecture

which ${CROSS_COMPILE}gcc #is this your compiler

${CROSS_COMPILE}gcc -v #is target: your desired architecture

echo$PATH#have you appended the appropriate parameters to your path. When in doubt start new shell

Configure the kernel. Often you will want to start with an existing defconfig file followed by doing a 'make kernel menuconfig' to change any options you desire to modify.

Examples:

for the mainline Ventana/IMX6:

make imx_v6_v7_defconfig # Ventana/IMX6
make menuconfig

for the mainline Newport/CN80XX:

make defconfig # ARM64 defconfig
make menuconfig

for the Gateworks Newport kernel:

make newport_defconfig # Gateworks default config
make menuconfig

for the Gateworks Linux 3.14.x based downstream vendor kernel:

make gwventana_defconfig # Gateworks default config
make menuconfig

The menuconfig make target launches the ncurses based (serial console) Linux kernel menu configuration tool so that you can add or adjust the support to your needs. A common change to the default kernel configurations above would be to add support for a USB based device, or wireless (802.11) support for example.

Build kernel targets. What targets you build depends on the product family and the bootloader or boot method you are using. Some common examples are:

Notice that the KLIB variables are new; They refer to the location of the raw kernel, location where it is built, and location to the config file.

Make menuconfig to select drivers you want the latest of, then build.

make menuconfig
make modules

[OPTIONAL] Locate the .ko's and copy them to the built kernel

cp --parent $(find -name "*.ko") ../

And that's it! If you tar the kernel up, make sure to remove the backports directory as each .ko will be double counted if you already copied the .ko's to the kernel tree. Please see the updating section for details on how to update a target's kernel.

Installing or Updating the kernel artifacts

Installation of a newly build kernel and other artifacts (headers, modules, dtbs) depends on the target platform and how you are booting it.

Kernel modules are located in a directory structure /lib/modules/(kernel_version) which matches the structure used when you install the modules with the 'modules install' build target. If you created a tarball of the kernel modules you can install those on your target as such:

Booting the kernel from the network

The bootloader's job is to load the linux kernel and execute it. Often during kernel development its advantageous to boot the kernel from the network to create very quick edit/build/boot cycles.

This can be accomplished in a variety of ways depending on what you want to come from the network, the kernel, the device-tree blobs, the root filesystem etc. During kernel development usually its just the kernel and device-tree blobs that are needed to boot over the network and using a flash based userspace filesystem is fine.

Kernel Modules

Kernel modules are dynamically loadable portions of the kernel that can be loaded or unloaded at runtime. It is generally not feasible to build every kernel driver statically into a modern Linux kernel so instead only options required to load further modules are build static.

The userspace applications insmod and rmmod are used to load and remove modules at runtime.

Most Linux based Operating Systems these days also contain support for automatically determining and loading module dependencies (modules required by the module you are trying to load) and for these you will also have: