Mastering Embedded Linux Programming - Sample Chapter
Mastering Embedded Linux Programming - Sample Chapter
$ 49.99 US
31.99 UK
P U B L I S H I N G
Chris Simmonds
Mastering Embedded
Linux Programming
ee
Sa
m
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Mastering Embedded
Linux Programming
Harness the power of Linux to create versatile and
robust embedded solutions
Foreword by Richard Purdie, Yocto Project Architect,
Linux Foundation Fellow
Chris Simmonds
England. He has been using Linux in embedded systems since the late 1990s, during
which he has worked on many interesting projects, including a stereoscopic camera,
intelligent weighing scales, various set-top boxes and home routers, and even a large
walking robot.
He is a frequent presenter at open source and embedded conferences, including the
Embedded Linux Conference, Embedded World, and the Android Builders' Summit.
He has been conducting training courses and workshops in embedded Linux since
2002 and in embedded Android since 2010. He has delivered hundreds of sessions to
many well-known companies. You can see some of his work on the "Inner Penguin"
blog at www.2net.co.uk.
Preface
An embedded system is a device with a computer inside that doesn't look like a
computer. Washing machines, televisions, printers, cars, aircraft, and robots are all
controlled by a computer of some sort, and in some cases, more than one. As these
devices become more complex, and as our expectations of the things that we can do
with them expand, the need for a powerful operating system to control them grows.
Increasingly, Linux is the operating system of choice.
The power of Linux stems from its open source model, which encourages sharing
of code. This means that software engineers from many backgrounds, and often
employed by competing companies, can cooperate to create an operating system
kernel that is up-to-date and tracks the development of the hardware. From this one
code base, there is support from the largest super computers down to a wristwatch.
Linux is only one component of the operating system. Many other components are
needed to create a working system, from basic tools, such as a command shell, to
graphical user interfaces, with web content and communicating with cloud services.
The Linux kernel together with an extensive range of other open source components
allow you to build a system that can function in a wide range of roles.
However, flexibility is a double-edged sword. While it gives a system designer
a wide choice of solutions to a particular problem, it also presents the problem of
knowing which are the best choices. The propose of this book is to describe in detail
how to construct an embedded Linux system using free, open source projects to
produce a robust, reliable, and efficient system. It is based on the experience of the
author as a consultant and trainer over a period of many years, using examples to
illustrate best practices.
Preface
Preface
Chapter 8, Introducing Device Drivers, describes how kernel device drivers interact
with the hardware with worked examples of a simple driver. It also describes the
various ways of calling device drivers from the user space.
Chapter 9, Starting up - the init Program, shows how the first user space program,
init, which starts the rest of the system. It describes the three versions of the init
program, each suitable for a different group of embedded systems, with increasing
complexity from BusyBox init to systemd.
Chapter 10, Learning About Processes and Threads, describes embedded systems from
the point of view of the application programmer. This chapter looks at processes and
threads, inter-process communication, and scheduling policies.
Chapter 11, Managing Memory, introduces the ideas behind virtual memory and how
the address space is divided into memory mappings. It also covers how to detect
memory that is being used and memory leaks.
Chapter 12, Debugging with GDB, shows you how to use the GNU debugger, GDB,
to interactively debug both the user space and kernel code. It also describes the
kernel debugger, kdb.
Chapter 13, Profiling and Tracing, covers the techniques available to measure the
system performance, starting from whole system profiles and then zeroing in on
particular areas where bottlenecks are causing poor performance. It also describes
Valgrind as a tool to check the correctness of an application's use of thread
synchronization and memory allocation.
Chapter 14, Real-time Programming, provides a detailed guide to real-time programming
on Linux, including the configuration of the kernel and the real-time kernel patch,
and also provides a description of tools to measure real-time latencies. It also covers
information on how to reduce the number of page faults by locking the memory.
Starting Out
You are about to begin working on your next project, and this time it is going to be
running Linux. What should you think about before you put finger to keyboard?
Let's begin with a high-level look at embedded Linux and see why it is popular,
what are the implications of open source licenses, and what kind of hardware
you will need to run Linux.
Linux first became a viable choice for embedded devices around 1999. That was
when Axis (www.axis.com) released their first Linux-powered network camera and
TiVo (www.tivo.com) their first DVR (Digital Video Recorder). Since 1999, Linux
has become ever more popular, to the point that today it is the operating system of
choice for many classes of product. At the time of writing, in 2015, there are about two
billion devices running Linux. That includes a large number of smartphones running
Android, which uses a Linux kernel, and hundreds of millions of set top boxes, smart
TVs, and Wi-Fi routers, not to mention a very diverse range of devices such as vehicle
diagnostics, weighing scales, industrial devices, and medical monitoring units that
ship in smaller volumes.
So, why does your TV run Linux? At first glance, the function of a TV is simple: it
has to display a stream of video on a screen. Why is a complex Unix-like operating
system like Linux necessary?
The simple answer is Moore's Law: Gordon Moore, co-founder of Intel, observed in
1965 that the density of components on a chip will double about every two years.
That applies to the devices that we design and use in our everyday lives just as much
as it does to desktops, laptops, and servers. At the heart of most embedded devices
is a highly integrated chip that contains one or more processor cores and interfaces
with main memory, mass storage, and peripherals of many types. This is referred
to as a System on Chip, or SoC, and they are increasing in complexity in accordance
with Moore's Law. A typical SoC has a technical reference manual that stretches
to thousands of pages. Your TV is not simply displaying a video stream as the old
analog sets used to do.
[1]
Starting Out
The stream is digital, possibly encrypted, and it needs processing to create an image.
Your TV is (or soon will be) connected to the Internet. It can receive content from
smartphones, tablets, and home media servers. It can be (or soon will be) used to
play games. And so on and so on. You need a full operating system to manage this
degree of complexity.
Here are some points that drive the adoption of Linux:
Linux is open source, so you have the freedom to get the source code and
modify it to meet your needs. You, or someone working on your behalf,
can create a board support package for your particular SoC board or device.
You can add protocols, features, and technologies that may be missing from
the mainline source code. You can remove features that you don't need to
reduce memory and storage requirements. Linux is flexible.
Linux has an active community; in the case of the Linux kernel, very active.
There is a new release of the kernel every 10 to 12 weeks, and each release
contains code from around 1,000 developers. An active community means
that Linux is up to date and supports current hardware, protocols,
and standards.
Open source licenses guarantee that you have access to the source code.
There is no vendor tie-in.
For these reasons, Linux is an ideal choice for complex devices. But there are a few
caveats I should mention here. Complexity makes it harder to understand. Coupled
with the fast moving development process and the decentralized structures of
open source, you have to put some effort into learning how to use it and to keep
on re-learning as it changes. I hope that this book will help in the process.
[2]
Chapter 1
Do you have the right skill set? The early parts of a project, board bring-up,
require detailed knowledge of Linux and how it relates to your hardware.
Likewise, when debugging and tuning your application, you will need to
be able to interpret the results. If you don't have the skills in-house you may
want to outsource some of the work. Of course, reading this book helps!
Consider these points carefully. Probably the best indicator of success is to look
around for similar products that run Linux and see how they have done it; follow
best practice.
The players
Where does open source software come from? Who writes it? In particular, how
does this relate to the key components of embedded developmentthe toolchain,
bootloader, kernel, and basic utilities found in the root filesystem?
The main players are:
The open source community. This, after all, is the engine that generates
the software you are going to be using. The community is a loose alliance
of developers, many of whom are funded in some way, perhaps by a
not-for-profit organization, an academic institution, or a commercial
company. They work together to further the aims of the various projects.
There are many of them, some small, some large. Some that we will be
making use of in the remainder of this book are Linux itself, U-Boot,
BusyBox, Buildroot, the Yocto Project, and the many projects under
the GNU umbrella.
[3]
Starting Out
CPU architectsThese are the organizations that design the CPUs we use.
The important ones here are ARM/Linaro (ARM-based SoCs), Intel (x86 and
x86_64), Imagination Technologies (MIPS), and Freescale/IBM (PowerPC).
They implement or, at the very least, influence support for the basic CPU
architecture.
SoC vendors (Atmel, Broadcom, Freescale, Intel, Qualcomm, TI, and many
others)They take the kernel and toolchain from the CPU architects and
modify it to support their chips. They also create reference boards: designs
that are used by the next level down to create development boards and
working products.
Board vendors and OEMsthese people take the reference designs from
SoC vendors and build them in to specific products, for instance set-topboxes or cameras, or create more general purpose development boards, such
as those from Avantech and Kontron. An important category are the cheap
development boards such as BeagleBoard/BeagleBone and Raspberry Pi that
have created their own ecosystems of software and hardware add-ons.
These form a chain, with your project usually at the end, which means that you do
not have a free choice of components. You cannot simply take the latest kernel from
kernel.org, except in a few rare cases, because it does not have support for the chip
or board that you are using.
This is an ongoing problem with embedded development. Ideally, the developers
at each link in the chain would push their changes upstream, but they don't. It is
not uncommon to find a kernel which has many thousands of patches that are not
merged upstream. In addition, SoC vendors tend to actively develop open source
components only for their latest chips, meaning that support for any chip more
than a couple of years old will be frozen and not receive any updates.
The consequence is that most embedded designs are based on old versions of
software. They do not receive security fixes, performance enhancements, or features
that are in newer versions. Problems such as Heartbleed (a bug in the OpenSSL
libraries) and Shellshock (a bug in the bash shell) go unfixed. I will talk more
about this later in this chapter under the topic of security.
What can you do about it? First, ask questions of your vendors: what is their update
policy, how often do they revise kernel versions, what is the current kernel version,
what was the one before that? What is their policy for merging changes up-stream?
Some vendors are making great strides in this way. You should prefer their chips.
Secondly, you can take steps to make yourself more self-sufficient. This book aims to
explain the dependencies in more detail and show you where you can help yourself.
Don't just take the package offered to you by the SoC or board vendor and use it
blindly without considering the alternatives.
[4]
Chapter 1
Project lifecycle
This book is divided into four sections that reflect the phases of a project. The phases
are not necessarily sequential. Usually they overlap and you will need to jump back
to revisit things that were done previously. However, they are representative of a
developer's preoccupations as the project progresses:
System architecture and design choices (chapters 7 to 9) will help you to look
at some of the design decisions you will have to make concerning the storage
of programs and data, how to divide work between kernel device drivers
and applications, and how to initialize the system.
The fifth section on real-time (Chapter 14, Real-time Programming) stands somewhat
alone because it is a small, but important, category of embedded systems. Designing
for real-time behavior has an impact on each of the four main phases.
Toolchain: This consists of the compiler and other tools needed to create
code for your target device. Everything else depends on the toolchain.
Bootloader: This is necessary to initialize the board and to load and boot
the Linux kernel.
Kernel: This is the heart of the system, managing system resources and
interfacing with hardware.
Root filesystem: This contains the libraries and programs that are run once
the kernel has completed its initialization.
[5]
Starting Out
Of course, there is also a fifth element, not mentioned here. That is the collection of
programs that are specific to your embedded application which make the device do
whatever it is supposed to do, be it weigh groceries, display movies, control a robot,
or fly a drone.
Typically you will be offered some or all of these elements as a package when you
buy your SoC or board. But, for the reasons mentioned in the preceding paragraph,
they may not be the best choices for you. I will give you the background to make
the right selections in the first six chapters and I will introduce you to two tools that
automate the whole process for you: Buildroot and the Yocto Project.
Open source
The components of embedded Linux are open source, so now is a good time to
consider what that means, why open sources work the way they do and how this
affects the often proprietary embedded device you will be creating from it.
Licenses
When talking about open source, the word, "free" is often used. People new to the
subject often take it to mean nothing to pay, and open source software licenses do
indeed guarantee that you can use the software to develop and deploy systems for
no charge. However, the more important meaning here is freedom, since you are
free to obtain the source code and modify it in any way you see fit and redeploy it
in other systems. These licenses give you this right. Compare that with shareware
licenses which allow you to copy the binaries for no cost but do not give you the
source code, or other licenses that allow you to use the software for free under
certain circumstances, for example, for personal use but not commercial.
These are not open source.
I will provide the following comments in the interest of helping you understand the
implications of working with open source licenses, but I would like to point out that
I am an engineer and not a lawyer. What follows is my understanding of the licenses
and the way they are interpreted.
Open source licenses fall broadly into two categories: the GPL (General Public
License) from the Free Software Foundation and the permissive licenses derived
from BSD (Berkeley Software Distribution), the Apache Foundation, and others.
The permissive licenses say, in essence, that you may modify the source code and
use it in systems of your own choosing so long as you do not modify the terms of the
license in any way. In other words, with that one restriction, you can do with it what
you want, including building it into possibly proprietary systems.
[6]
Chapter 1
The GPL licenses are similar, but have clauses which compel you to pass the rights
to obtain and modify the software on to your end users. In other words you share
your source code. One option is to make it completely public by putting it onto a
public server. Another is to offer it only to your end users by means of a written
offer to provide the code when requested. The GPL goes further to say that you
cannot incorporate GPL code into proprietary programs. Any attempt to do so
would make the GPL apply to the whole. In other words, you cannot combine
GPL and proprietary code in one program.
So, what about libraries? If they are licensed with the GPL, any program linked
with them becomes GPL also. However, most libraries are licensed under the Lesser
General Public License (LGPL). If this is the case, you are allowed to link with them
from a proprietary program.
All of the preceding description relates specifically to GPL v2 and LGPL v2.1.
I should mention the latest versions of GPL v3 and LGPL v3. These are controversial,
and I will admit that I don't fully understand the implications. However, the intention
is to ensure that the GPLv3 and LGPL v3 components in any system can be replaced by
the end user, which is in the spirit of open source software for everyone. It does pose
some problems though. Some Linux devices are used to gain access to information
according to a subscription level or another restriction, and replacing critical parts
of the software may compromise that. Set-top boxes fit into this category. There are
also issues with security. If the owner of a device has access to the system code, then
so might an unwelcome intruder. Often the defense is to have kernel images that are
signed by an authority, the vendor, so that unauthorized updates are not possible.
Is that an infringement of my right to modify my device? Opinions differ.
The TiVo set-top box is an important part of this debate. It uses
a Linux kernel, which is licensed under GPL v2. TiVo release the
source code of their version of the kernel and so comply with the
license. TiVo also have a bootloader that will only load a kernel
binary that is signed by them. Consequently, you can build a
modified kernel for a TiVo box, but you cannot load it on the
hardware. The FSF take the position that this is not in the spirit of
open source software and refer to this procedure as "tivoization".
The GPL v3 and LGPL v3 were written to explicitly prevent this
happening. Some projects, the Linux kernel in particular, have
been reluctant to adopt the version three licenses because of the
restrictions it would place on device manufacturers.
[7]
Starting Out
[8]
Chapter 1
In addition to these basics, there are interfaces to the specific bits of hardware your
device needs to get its job done. Mainline Linux comes with open source drivers for
many thousands of different devices, and there are drivers (of variable quality) from
the SoC manufacturer and drivers from the OEMs of third-party chips that may be
included in the design, but remember my comments on the commitment and ability
of some manufacturers. As a developer of embedded devices, you will find that you
spend quite a lot of time evaluating and adapting third-party code, if you have it, or
liaising with the manufacturer if you don't. Finally, you will have to write the device
support for any interfaces that are unique to the device, or find someone to do it
for you.
Starting Out
Mini USB OTG client/host port that can also be used to power the board
In addition, there are two 46-pin expansion headers for which there are a great
variety of daughter boards, known as capes, which allow you to adapt the board
to do many different things. However, you do not need to fit any capes in the
examples in this book.
In addition to the board itself, you will need:
a mini USB to full-size USB cable (supplied with the board) to provide
power, unless you have the last item on this list.
an RS-232 cable that can interface with the 6-pin 3.3 volt TTL level
signals provided by the board. The Beagleboard website has links
to compatible cables.
QEMU
QEMU is a machine emulator. It comes in a number of different flavors, each of
which can emulate a processor architecture and a number of boards built using
that architecture. For example, we have the following:
qemu-system-arm: ARM
qemu-system-mips: MIPS
qemu-system-ppc: PowerPC
[ 10 ]
Chapter 1
For each architecture, QEMU emulates a range of hardware, which you can see by
using the option -machine help. Each machine emulates most of the hardware
that would normally be found on that board. There are options to link hardware
to local resources, such as using a local file for the emulated disk drive. Here is a
concrete example:
$ qemu-system-arm -machine vexpress-a9 -m 256M -drive
file=rootfs.ext4,sd -net nic -net use -kernel zImage -dtb vexpressv2p-ca9.dtb -append "console=ttyAMA0,115200 root=/dev/mmcblk0" serial stdio -net nic,model=lan9118 -net tap,ifname=tap0
-kernel zImage: loads the Linux kernel from the local file named zImage
-dtb vexpress-v2p-ca9.dtb: loads the device tree from the local file
vexpress-v2p-ca9.dtb
-serial stdio: connects the serial port to the terminal that launched
QEMU, usually so that you can log on to the emulated machine via the
serial console
To configure the host side of the network, you need the tunctl command from
the User Mode Linux (UML) project; on Debian and Ubuntu the package is named
uml-utilities. You use it to create a virtual network using the following command:
$ sudo tunctl -u $(whoami) -t tap0
This creates a network interface named tap0 which is connected to the network
controller in the emulated QEMU machine. You configure tap0 in exactly the same
way as any other interface.
All of these options are described in detail in the following chapters. I will be using
Versatile Express for most of my examples, but it should be easy to use a different
machine or architecture.
[ 11 ]
Starting Out
Summary
Embedded hardware will continue to get more complex, following the trajectory set
by Moore's Law. Linux has the power and the flexibility to make use of hardware in
an efficient way.
Linux is just one component of open source software out of the many that you
need to create a working product. The fact that the code is freely available means
that people and organizations at many different levels can contribute. However,
the sheer variety of embedded platforms and the fast pace of development lead
to isolated pools of software which are not shared as efficiently as they should be.
In many cases, you will become dependent on this software, especially the Linux
kernel that is provided by your SoC or Board vendor, and to a lesser extent the
toolchain. Some SoC manufacturers are getting better at pushing their changes
upstream and the maintenance of these changes is getting easier.
Fortunately, there are some powerful tools that can help you create and maintain
the software for your device. For example, Buildroot is ideal for small systems and
the Yocto Project for larger ones.
Before I describe these build tools, I will describe the four elements of embedded
Linux, which you can apply to all embedded Linux projects, however they are
created. The next chapter is all about the first of these, the toolchain, which you
need to compile code for your target platform.
[ 12 ]
www.PacktPub.com
Stay Connected: