1.linux Kernel Intro
1.linux Kernel Intro
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 1/484
Linux Kernel Introduction
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 16/484
Linux Kernel Introduction
Linux features
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 17/484
History
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 18/484
Linux kernel in the system
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 19/484
Linux kernel main roles
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 20/484
Inside the Linux kernel
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 21/484
Linux license
I The whole Linux sources are Free Software released under the
GNU General Public License version 2 (GPL v2).
I For the Linux kernel, this basically implies that:
I When you receive or buy a device with Linux on it, you should
receive the Linux sources, with the right to study, modify and
redistribute them.
I When you produce Linux based devices, you must release the
sources to the recipient, with the same rights, with no
restriction..
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 22/484
Linux kernel key features
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 23/484
Supported hardware architectures
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 24/484
System calls
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 25/484
Virtual filesystems
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 26/484
Embedded Linux Kernel Usage
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 28/484
Location of kernel sources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 29/484
Getting Linux sources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 30/484
Linux kernel size (1)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 32/484
Kernel Source Code
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 34/484
Kernel Source Code
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 35/484
Programming language
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 37/484
No C library
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 38/484
Portability
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 39/484
No floating point computation
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 40/484
No stable Linux internal API 1/3
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 41/484
No stable Linux internal API 2/3
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 42/484
No stable Linux internal API 3/3
I USB example
I Linux has updated its USB internal API at least 3 times (fixes,
security issues, support for high-speed devices) and has now
the fastest USB bus speeds (compared to other systems)
I Windows XP also had to rewrite its USB stack 3 times. But,
because of closed-source, binary drivers that can’t be updated,
they had to keep backward compatibility with all earlier
implementation. This is very costly (development, security,
stability, performance).
I See “Myths, Lies, and Truths about the Linux Kernel”, by
Greg K.H., for details about the kernel development process:
http://kroah.com/log/linux/ols_2006_keynote.html
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 43/484
Kernel memory constraints
I No memory protection
I Accessing illegal memory locations result in (often fatal)
kernel oopses.
I Fixed size stack (8 or 4 KB). Unlike in userspace, there’s no
way to make it grow.
I Kernel memory can’t be swapped out (for the same reasons).
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 44/484
Linux kernel licensing constraints
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 45/484
Proprietary code and the kernel
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 46/484
Advantages of GPL drivers 1/2
I You don’t have to write your driver from scratch. You can
reuse code from similar free software drivers.
I You get free community contributions, support, code review
and testing. Proprietary drivers (even with sources) don’t get
any.
I Your drivers can be freely shipped by others (mainly by
distributions).
I Closed source drivers often support a given kernel version. A
system with closed source drivers from 2 different sources is
unmanageable.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 47/484
Advantages of GPL drivers 2/2
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 48/484
Advantages of in-tree kernel drivers
I Once your sources are accepted in the mainline tree, they are
maintained by people making changes.
I Cost-free maintenance, security fixes and improvements.
I Easy access to your sources by users.
I Many more people reviewing your code.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 49/484
Userspace device drivers 1/2
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 50/484
Userspace device drivers 2/2
I Advantages
I No need for kernel coding skills. Easier to reuse code between
devices.
I Drivers can be written in any language, even Perl!
I Drivers can be kept proprietary.
I Driver code can be killed and debugged. Cannot crash the
kernel.
I Can be swapped out (kernel code cannot be).
I Can use floating-point computation.
I Less in-kernel complexity.
I Drawbacks
I Less straightforward to handle interrupts.
I Increased latency vs. kernel code.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 51/484
Kernel Source Code
Linux sources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 52/484
Linux sources structure 1/4
I arch/<ARCH>
I Architecture specific code
I arch/<ARCH>/mach-<machine>, machine/board specific code
I arch/<ARCH>/include/asm, architecture-specific headers
I arch/<ARCH>/boot/dts, Device Tree files, for some
architectures
I block
I Block layer core
I COPYING
I Linux copying conditions (GNU GPL)
I CREDITS
I Linux main contributors
I crypto/
I Cryptographic libraries
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 53/484
Linux sources structure 2/4
I Documentation/
I Kernel documentation. Don’t miss it!
I drivers/
I All device drivers except sound ones (usb, pci...)
I fs/
I Filesystems (fs/ext3/, etc.)
I include/
I Kernel headers
I include/linux
I Linux kernel core headers
I init/
I Linux initialization (including main.c)
I ipc/
I Code used for process communication
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 54/484
Linux sources structure 3/4
I Kbuild
I Part of the kernel build system
I Kconfig
I Top level description file for configuration parameters
I kernel/
I Linux kernel core (very small!)
I lib/
I Misc library routines (zlib, crc32...)
I MAINTAINERS
I Maintainers of each kernel part. Very useful!
I Makefile
I Top Linux Makefile (sets arch and version)
I mm/
I Memory management code (small too!)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 55/484
Linux sources structure 4/4
I net/
I Network support code (not drivers)
I README
I Overview and building instructions
I REPORTING-BUGS
I Bug report instructions
I samples/
I Sample code (markers, kprobes, kobjects...)
I scripts/
I Scripts for internal or external use
I security/
I Security model implementations (SELinux...)
I sound/
I Sound support code and drivers
I usr/
I Code to generate an initramfs cpio archive
I virt/
I Virtualization support (KVM)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 56/484
Practical lab - Kernel Source Code - Exploring
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 62/484
Kernel Source Code
Kernel configuration
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 63/484
Kernel configuration and build system
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 64/484
Kernel configuration (1)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 65/484
Kernel configuration (2)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 66/484
Kernel or module?
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 67/484
Kernel option types
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 68/484
Kernel option dependencies
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 69/484
make xconfig
make xconfig
I The most common graphical interface to configure the kernel.
I Make sure you read
help -> introduction: useful options!
I File browser: easier to load configuration files
I Search interface to look for parameters
I Required Debian / Ubuntu packages: libqt4-dev g++
(libqt3-mt-dev for older kernel releases)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 70/484
make xconfig screenshot
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 71/484
make xconfig search interface
Looks for a keyword in the parameter name. Allows to select or
unselect found parameters.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 72/484
Kernel configuration options
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 73/484
Corresponding .config file excerpt
Options are grouped by sections and are prefixed with CONFIG_.
#
# CD-ROM/DVD Filesystems
#
CONFIG_ISO9660_FS=m
CONFIG_JOLIET=y
CONFIG_ZISOFS=y
CONFIG_UDF_FS=y
CONFIG_UDF_NLS=y
#
# DOS/FAT/NT Filesystems
#
# CONFIG_MSDOS_FS is not set
# CONFIG_VFAT_FS is not set
CONFIG_NTFS_FS=m
# CONFIG_NTFS_DEBUG is not set
CONFIG_NTFS_RW=y
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 74/484
make gconfig
make gconfig
I GTK based graphical
configuration interface.
Functionality similar to that
of make xconfig.
I Just lacking a search
functionality.
I Required Debian packages:
libglade2-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 75/484
make menuconfig
make menuconfig
I Useful when no graphics are
available. Pretty convenient
too!
I Same interface found in
other tools: BusyBox,
Buildroot...
I Required Debian packages:
libncurses-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 76/484
make nconfig
make nconfig
I A newer, similar text
interface
I More user friendly (for
example, easier to access
help information).
I Required Debian packages:
libncurses-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 77/484
make oldconfig
make oldconfig
I Needed very often!
I Useful to upgrade a .config file from an earlier kernel release
I Issues warnings for configuration parameters that no longer
exist in the new kernel.
I Asks for values for new parameters
If you edit a .config file by hand, it’s strongly recommended to
run make oldconfig afterwards!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 78/484
Undoing configuration changes
A frequent problem:
I After changing several kernel configuration settings, your
kernel no longer works.
I If you don’t remember all the changes you made, you can get
back to your previous configuration:
$ cp .config.old .config
I All the configuration interfaces of the kernel (xconfig,
menuconfig, oldconfig...) keep this .config.old backup
copy.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 79/484
Configuration per architecture
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 80/484
Overview of kernel options (1)
I General setup
I Local version - append to kernel release allows to concatenate
an arbitrary string to the kernel version that a user can get
using uname -r. Very useful for support!
I Support for swap, can usually be disabled on most embedded
devices
I Configure standard kernel features (expert users) allows to
remove features from the kernel to reduce its size. Powerful,
but use with care!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 81/484
Overview of kernel options (2)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 82/484
Overview of kernel options (3)
I Kernel features
I Tickless system, which allows to disable the regular timer tick
and use on-demand ticks instead. Improves power savings
I High resolution timer support. By default, the resolution of
timer is the tick resolution. With high resolution timers, the
resolution is as precise as the hardware can give
I Preemptible kernel enables the preemption inside the kernel
code (the userspace code is always preemptible). See our
real-time presentation for details
I Power management
I Global power management option needed for all power
management related features
I Suspend to RAM, CPU frequency scaling, CPU idle control,
suspend to disk
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 83/484
Overview of kernel options (4)
I Networking support
I The network stack
I Networking options
I Unix sockets, needed for a form of inter-process
communication
I TCP/IP protocol with options for multicast, routing,
tunneling, Ipsec, Ipv6, congestion algorithms, etc.
I Other protocols such as DCCP, SCTP, TIPC, ATM
I Ethernet bridging, QoS, etc.
I Support for other types of network
I CAN bus, Infrared, Bluetooth, Wireless stack, WiMax stack,
etc.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 84/484
Overview of kernel options (5)
I Device drivers
I MTD is the subsystem for flash (NOR, NAND, OneNand,
battery-backed memory, etc.)
I Parallel port support
I Block devices, a few misc block drivers such as loopback,
NBD, etc.
I ATA/ATAPI, support for IDE disk, CD-ROM and tapes. A
new stack exists
I SCSI
I The SCSI core, needed not only for SCSI devices but also for
USB mass storage devices, SATA and PATA hard drives, etc.
I SCSI controller drivers
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 85/484
Overview of kernel options (6)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 86/484
Overview of kernel options (7)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 87/484
Overview of kernel options (8)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 88/484
Overview of kernel options (9)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 89/484
Overview of kernel options (10)
I File systems
I The common Linux filesystems for block devices: ext2, ext3,
ext4
I Less common filesystems: XFS, JFS, ReiserFS, GFS2, OCFS2,
Btrfs
I CD-ROM filesystems: ISO9660, UDF
I DOS/Windows filesystems: FAT and NTFS
I Pseudo filesystems: proc and sysfs
I Miscellaneous filesystems, with amongst other flash filesystems
such as JFFS2, UBIFS, SquashFS, cramfs
I Network filesystems, with mainly NFS and SMB/CIFS
I Kernel hacking
I Debugging features useful for kernel developers
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 90/484
Kernel Source Code
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 91/484
Kernel compilation
I make
I in the main kernel source directory
I Remember to run make -j 4 if you have multiple CPU cores
to speed up the compilation process
I No need to run as root!
I Generates
I vmlinux, the raw uncompressed kernel image, at the ELF
format, useful for debugging purposes, but cannot be booted
I arch/<arch>/boot/*Image, the final, usually compressed,
kernel image that can be booted
I bzImage for x86, zImage for ARM, vmImage.gz for Blackfin,
etc.
I arch/<arch>/boot/dts/*.dtb, compiled Device Tree files
(on some architectures)
I All kernel modules, spread over the kernel source tree, as .ko
files.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 92/484
Kernel installation
I make install
I Does the installation for the host system by default, so needs
to be run as root. Generally not used when compiling for an
embedded system, and it installs files on the development
workstation.
I Installs
I /boot/vmlinuz-<version>
Compressed kernel image. Same as the one in
arch/<arch>/boot
I /boot/System.map-<version>
Stores kernel symbol addresses
I /boot/config-<version>
Kernel configuration for this version
I Typically re-runs the bootloader configuration utility to take
the new kernel into account.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 93/484
Module installation
I make modules_install
I Does the installation for the host system by default, so needs
to be run as root
I Installs all modules in /lib/modules/<version>/
I kernel/
Module .ko (Kernel Object) files, in the same directory
structure as in the sources.
I modules.alias
Module aliases for module loading utilities. Example line:
alias sound-service-?-0 snd_mixer_oss
I modules.dep
Module dependencies
I modules.symbols
Tells which module a given symbol belongs to.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 94/484
Kernel cleanup targets
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 95/484