wiki:linux/kernel

Version 1 (modified by trac, 7 years ago) ( diff )

--

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. Gateworks recommends using our Gateworks downstream vendor kernel if you are needing full hardware support as the mainline Linux kernel is still lacking certain IMX6 support.

Gateworks downstream vendor kernel

Gateworks maintains a downstream Linux kernel that contains patches from Freescale and Gateworks to add support for Ventana including some items that have not made it into mainline Linux yet.

Our pre-built Gateworks downstream vendor kernel is a build artifact of our Yocto BSP and contains the following:

  • drivers we feel are important for our user base
  • wireless drivers from the Linux backports project
  • firmware for various devices that require run-time firmware loading
  • kernel headers

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.

Mainline upstream linux kernel

The mainline Linux kernel is missing the following support (which is present in the Gateworks vendor kernel):

  • IMX6 video capture (this is slowly being worked on but not yet present as of 4.2)
  • adv7393 analog video output (GW54xx)
  • HW crypto support
  • IMX6 GPU driver (there is work ongoing on an open-source reverse engineered Vivante driver called etna_viv)
  • VPU support (technically, the upstream coda driver does support the chips-and-media VPU but not in a way that gstreamer can utilize it)

See also:

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.

You likely have one of the following toolchains to work with:

  • toolchain from an OpenWrt build directory
  • toolchain from a prebuilt OpenWrt BSP SDK
  • toolchain from a Yocto build directory
  • toolchain from a prebuilt Yocto BSP SDK
  • pre-built toolchain from somewhere else, like Android

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:

  • OpenWrt root filesystem
  • Yocto root filesystem
  • Android root filesystem
  • Ubuntu root filesystem

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:
    • Ventana uses the Freescale IMX6 SoC which has ARM Cortex-A9 cores with NEON SIMD support. You can download our pre-built OpenWrt toolchain here
    • Laguna uses the Cavium cns3xxx SoC which has ARM11 MP-Core cores

Steps to build the Gateworks kernel for the Ventana family using the Gateworks OpenWrt toolchain:

  1. Install pre-requisites:
    apt-get install build-essential ncurses-dev bc u-boot-tools liblzo2-dev lzop git
    
    • typically 'build-essential' provides enough for compiling, however we need a few other things for kernel development:
      • ncurses-dev is needed for menuconfig
      • u-boot-tools, bc, and lzop are needed for uImage
      • git is needed for checking out the source
  1. Obtain and install compiler toolchain:
    • For Ventana, you can use the pre-built Gateworks OpenWrt 14.08 BSP tailored to the ARM Cortex-A9 CPU used in the IMX6 SoC:
      wget http://dev.gateworks.com/openwrt/14.08/imx6/OpenWrt-Toolchain-imx6-for-arm_cortex-a9+neon-gcc-4.8-linaro_uClibc-0.9.33.2_eabi.tar.bz2
      tar -xvf OpenWrt-Toolchain-imx6-for-arm_cortex-a9+neon-gcc-4.8-linaro_uClibc-0.9.33.2_eabi.tar.bz2
      
  1. Obtain Linux kernel source (see above to help you decide which kernel version you should use):
    • for the Gateworks Linux 3.14.x based downstream vendor kernel (with full Ventana support including video capture which hasn't made it fully into mainline yet):
      git clone https://github.com/Gateworks/linux-imx6.git
      cd linux-imx6
      git checkout gateworks_fslc_3.14_1.0.x_ga
      
    • for mainline Linux v4.1 for example (which is missing video capture support for IMX6/Ventana):
      git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
      cd linux
      git checkout v4.1
      
  1. Setup shell env for building:
    • Note that the paths here are dependent on the toolchain you installed. The following is for the pre-built Gateworks OpenWrt 14.08 BSP toolchain:
      export STAGING_DIR=../OpenWrt-Toolchain-imx6-for-arm_cortex-a9+neon-gcc-4.8-linaro_uClibc-0.9.33.2_eabi
      TOOLCHAIN=toolchain-arm_cortex-a9+neon_gcc-4.8-linaro_uClibc-0.9.33.2_eabi
      PATH=$PATH:$STAGING_DIR/$TOOLCHAIN/bin
      export INSTALL_MOD_PATH=install
      export INSTALL_HDR_PATH=install
      export ARCH=arm
      export CROSS_COMPILE=arm-openwrt-linux-
      
    • Note that STAGING_DIR is something required by the OpenWrt toolchain... your toolchain may differ
    • The INSTALL_MOD_PATH env var is used by the modules_install and firmware_install make targets to specify the directory to install modules/firmware into
    • The ARCH and CROSS_COMPILE env args are required for proper cross-compilation using your toolchain
  1. Configure the kernel:
    • for the Gateworks Linux 3.14.x based downstream vendor kernel start with gwventana_defconfig *
      make gwventana_defconfig
      
    • for mainline Linux v4.1 start with the imx_v6_v7_defconfig:
      make imx_v6_v7_defconfig
      
    • Optional, modify kernel:
      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.
  1. Build kernel:
    LOADADDR=0x10008000 make uImage modules dtbs modules_install
    
    • the default make target will build zImage, modules, and dtbs - we want uImage, modules and dtbs
    • when building a multi-arch kernel, the build system needs to know the load address to put in the u-boot image header which you provide via the LOADADDR env var. For IMX6 its 0x10008000
    • the firmware_install make target can also be specified if you need specific firmware to be placed in $INSTALL_MOD_PATH/lib/firmware
    • The uImage make target builds a U-Boot image of the kernel
    • The dtbs make target builds the device-tree binaries needed to boot the Ventana kernel (not used for non device-tree kernels)
    • The modules_install make target installs the kernel modules to the INSTALL_MOD_PATH directory
    • After building and copying the uImage and dtbs to install/boot, you can copy or extract the contents of the install directory to your Linux root filesystem taking care to make the user and group root
    • headers will be in the install dir
  1. Copy additional artifacts:
    mkdir $INSTALL_MOD_PATH/boot
    cp arch/arm/boot/uImage arch/arm/boot/dts/imx6*gw*.dtb $INSTALL_MOD_PATH/boot
    

If you wish to make a tarball of this kernel relative to the root fs:

tar -C $INSTALL_MOD_PATH --owner=0 --group=0 -cvzf linux-4.1.tar.gz .

If you need kernel headers for development you can build them via:

make headers_install
  • headers will be in INSTALL_HDR_PATH which we set to the install subdir above

If you wish to add additional drivers from a newer kernel (for example, you are using the 3.14.x Gateworks downstream vendor kernel but wish to have updated wireless drivers) see below.

Building the Latest Kernel Modules - Wireless Backports

Because we don't use mainline linux, but require the latest drivers in wireless, we use the backports project. For more detail on this project, please visit their documentation site.

In order to build this, you will need a kernel tree already built. In this example, I will assume the gateworks_fslc_3.14_1.0.x_ga kernel is being used.

  1. Change into the kernel tree directory, making sure it is built using the example shown in the building section above.
    cd gateworks_fslc_3.14_1.0.x_ga
    make gwventana_defconfig
    etc...
    
  1. Grab the backports version you want to use and cd into it. This example will use the one currently used for our Yocto BSPs.
    wget https://www.kernel.org/pub/linux/kernel/projects/backports/2016/01/22/backports-20160122.tar.gz
    tar xzf backports-20160122.tar.gz
    cd backports-20160122
    
  1. [OPTIONAL] You may have to patch the backports project, depending on the version being used
    wget https://raw.githubusercontent.com/Gateworks/meta-gateworks/fido/recipes-kernel/compat-wireless/compat-wireless-all/0001-disable_kconf.patch
    wget https://raw.githubusercontent.com/Gateworks/meta-gateworks/fido/recipes-kernel/compat-wireless/compat-wireless-all/0002-add-KLIB_CONFIG.patch
    wget https://raw.githubusercontent.com/Gateworks/meta-gateworks/fido/recipes-kernel/compat-wireless/compat-wireless-all/add_db_txt.patch
    
    patch -p1 < 0001-disable_kconf.patch   # Used if no kconfig available (generally never needed)
    patch -p1 < 0002-add-KLIB_CONFIG.patch # Used if config is not located in kernel directory
    patch -p1 < add_db_txt.patch           # Used if needing static wireless regulatory database
    
  1. Configure shell for cross compiler (please see the building section for more details)
    export STAGING_DIR=../OpenWrt-Toolchain-imx6-for-arm_cortex-a9+neon-gcc-4.8-linaro_uClibc-0.9.33.2_eabi
    TOOLCHAIN=toolchain-arm_cortex-a9+neon_gcc-4.8-linaro_uClibc-0.9.33.2_eabi
    PATH=$PATH:$STAGING_DIR/$TOOLCHAIN/bin
    export INSTALL_MOD_PATH=install
    export ARCH=arm
    export CROSS_COMPILE=arm-openwrt-linux-
    export KLIB=..
    export KLIB_BUILD=..
    export KLIB_CONFIG=..
    

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.

  1. Make menuconfig to select drivers you want the latest of, then build.
    make menuconfig
    make modules
    
  1. [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.

Kernel Module Commands

  • depmod - builds module dependency database
  • modprobe loads module and all dependecies
  • insmod loads module but NO depedencies

See Linux documentation for more information.

Updating the kernel dtbs and modules on a running target

If you want to update the kernel, dtbs, and modules on a running target using the tarball created after building the kernel (see above), you can do this by booting and using:

# download kernel specific tarball suitable for booted kernel
KERNEL=$(uname -r)
URL=http://192.168.1.165/tftpboot/ventana/
rm -f /tmp/linux-$KERNEL.tar.gz
wget -P /tmp $URL/linux-$KERNEL.tar.gz

# untar appropriately
if [ -d /etc/config ]; then
  # OpenWrt stores modules in a flat tree
  echo "OpenWrt Filesystem"
  ( cd /; tar -xvzf /tmp/linux-$KERNEL.tar.gz )
  find /lib/modules/$KERNEL -name *.ko -exec mv {} /lib/modules/$KERNEL \;
else
  ( cd /; tar -xvf /tmp/linux-$KERNEL.tar.gz )
fi
depmod -a
sync

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.

To do this using Ventana as an example:

# boot kernel+dtbs over network via tftp, using NAND filesystem
tftp ${loadaddr} ${prefix}uImage && \
  tftp ${fdt_addr} ${prefix}${fdt_file2} && \
  fdt addr ${fdt_addr} && \
  fdt boardsetup && \
  setenv bootargs console=${console},${baudrate} root=ubi0:rootfs ubi.mtd=2 rootfstype=ubifs ${video} ${extra} && \
  bootm ${loadaddr} - ${fdt_addr}
# boot kernel+dtbs over netowrk via tftp, using MMC filesystem
tftp ${loadaddr} ${prefix}uImage && \
  tftp ${fdt_addr} ${prefix}${fdt_file2} && \
  fdt addr ${fdt_addr} && \
  fdt boardsetup && \
  setenv bootargs console=${console},${baudrate} root=/dev/mmcblk0p1 rootfstype=ext4 rootwait rw ${video} ${extra} && \
  bootm ${loadaddr} - ${fdt_addr}
# boot kernel_dtbs over network via tftp, using USB filesystem
tftp ${loadaddr} ${prefix}uImage && \
  tftp ${fdt_addr} ${prefix}${fdt_file2} && \
  fdt addr ${fdt_addr} && \
  fdt boardsetup && \
  setenv bootargs console=${console},${baudrate} root=/dev/sda1 rootfstype=ext4 rootwait rw ${video} ${extra} && \
  bootm ${loadaddr} - ${fdt_addr}
Note: See TracWiki for help on using the wiki.