Professional Documents
Culture Documents
Linux - Introduction: Lionel Auroux
Linux - Introduction: Lionel Auroux
Introduction
Lionel Auroux
Introduction
The Linux
kernel
Linux - Introduction
Meeting Linux
Interacting
with Linux
More docu-
mentation
2017-09-29
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux Introduction
Your first module
More docu-
mentation
Lionel Auroux
Introduction
Meeting Linux
manages the hardware
Interacting
with Linux allocates resources eg. memory pages or CPU cycles
Writing code is responsible for the file system and network
for Linux
Your first module communication
More docu- provides an abstraction layer for the applications: the
mentation
userland
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux The Linux kernel
Your first module
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Portable
Meeting Linux
Interacting
Versatile
with Linux Stable
Writing code
for Linux
Mature
Your first module Secure
More docu-
mentation
Robust
Lionel Auroux
Introduction
The Linux
kernel
Writing code
Around 200 maintainers
for Linux Around 80% of the changes are sponsored
Your first module
More docu-
Linus Torvalds makes the official releases
mentation
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
Interacting
Binder was moved late 2014:
with Linux drivers/android/binder.c
Writing code
for Linux
Your first module Usage stats
More docu-
mentation
Android now has 1 billion monthly active users
– Sundar Pichai
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux Meeting Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
More docu-
mentation
Figure 3:kernel.org
Meeting Linux
drivers/ Device drivers (except sound ones)
Interacting
with Linux firmware/ Device firmware needed for some devices
Writing code
for Linux
fs/ Filesystems infrastructure
Your first module include/ Kernel headers
More docu-
mentation include/linux/ Core kernel headers
include/uapi/ User space API headers
init/ Kernel initialization
ipc/ System V InterProcess Communication (sem, shm,
msgqueue)
Lionel Auroux
Meeting Linux
net/ Network support code (not drivers!)
Interacting
with Linux samples/ Sample code (trace, kobject, krpobes)
Writing code
for Linux
scripts/ Scripts for internal or external use
Your first module security/ Security frameworks
More docu-
mentation
sound/ Sound code and drivers
tools/ User space tools
usr/ Generate the initramfs
virt/ Virtualization infrastructure (kvm)
Lionel Auroux
Introduction
The Linux
kernel
COPYING The kernel licence (GPLv2)
Meeting Linux
CREDITS The people who have contributed to the kernel
Interacting Kbuild Kernel build system
with Linux
Kconfig Description of configuration parameters
Writing code
for Linux MAINTAINERS Maintainer of subsystems and drivers
Your first module
Makefile Base kernel Makefile
More docu-
mentation README Overview of the kernel
REPORTING-BUGS Instructions for reporting bugs
Lionel Auroux
Introduction
Each file has a set of maintainers.
The Linux
kernel They are responsible for triaging bugs, reviewing patches
Meeting Linux and directing changes.
Interacting Patches are sent to mailing-lists for review.
with Linux
Writing code
Once approved, they are sent to the maintainer of the
for Linux subsystem.
Your first module
More docu-
Subsystem maintainers review them and place them in a
mentation special branch.
They send this branch to Linus Torvalds, that will merge it
to his branch.
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
The merge window is open (~2 weeks).
Interacting
with Linux Release candidates (-rc) are published.
Writing code No new feature is added, only bug fixes (~6-10 weeks).
for Linux
Your first module
The final release is tagged by Linus Torvalds.
More docu-
mentation
Lionel Auroux
Introduction
The kernel is a single file, resulting of the compilation
The Linux
process.
kernel
Compile-time options (-D flags) can be used to select
Meeting Linux
which features are compiled-in and their settings.
Interacting
with Linux
However, after the boot process, it can load module from
Writing code
for Linux the filesystem at runtime to extend its features. Each
Your first module
module is a single file.
More docu-
mentation Configuring the kernel is:
Choosing what’s going into the main file, and what will be
built as modules.
Setting various options.
Lionel Auroux
[...]
Lionel Auroux Linux - Introduction 2017-09-29 19 / 52
Config files
Linux -
Introduction .config
Lionel Auroux Simple text files, key=value format.
Introduction
Meeting Linux make defconfig: new config with default from $ARCH
Interacting supplied defconfig
with Linux
make i386_defconfig: request defconfig from a platform
Writing code
for Linux
More docu-
mentation
make config: text based
make menuconfig, make nconfig: menu/ncurses
interface
make xconfig, make gconfig: graphical interface
make oldconfig: upgrade .config with options from the
new release
Lionel Auroux Linux distributions typically enable a lot of kernel features and
Introduction drivers, most of them are compiled as modules.
The Linux
kernel Read the config file of your kernel
Meeting Linux Require CONFIG_IKCONFIG_PROC=y:
Interacting
with Linux $ zcat /proc/config.gz
Writing code
for Linux
CONFIG_64BIT=y
Your first module CONFIG_X86_64=y
More docu-
mentation
CONFIG_X86=y
CONFIG_INSTRUCTION_DECODER=y
CONFIG_OUTPUT_FORMAT="elf64-x86-64"
CONFIG_ARCH_DEFCONFIG="arch/x86/configs/x86_64_defconf
...
Lionel Auroux
Introduction
Only interact with the top-directory Makefile
The Linux
kernel $ make menuconfig # Edit .config
Meeting Linux $ make # Build the kernel and modules
Interacting
with Linux
Writing code
Produces:
for Linux
Your first module
...
More docu-
mentation vmlinux: ELF object of the kernel, cannot be booted
arch/x86/boot/bzImage: bootable compressed kernel
image
**/*.ko: Modules
Lionel Auroux
Introduction
The Linux
kernel
make help: list all available targets
make modules: build/rebuild modules
Meeting Linux
Interacting
with Linux make headers_install: “install” headers in the local
Writing code usr/
for Linux
Your first module make modules_install: install to /lib/modules/KVER
More docu-
mentation INSTALL_MOD_PATH=dir/ to select the directory
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux Interacting with Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
It is a string for runtime configuration:
The Linux no recompilation
kernel
can be builtin, using the CONFIG_CMDLINE option
Meeting Linux
can be used to pass arguments to the init program
Interacting
with Linux Many kernel options, examples:
Writing code
for Linux root=/dev/sda1 the root filesystem
Your first module
console=ttyS0 where to write kernel messages
More docu-
mentation
debug, loglevel=7 kernel verbosity
usbcore.blinkenlights=true also available for modules
More documentation:
Documentation/kernel-parameters.txt
Lionel Auroux
Introduction
The Linux
The system call is the fundamental interface between an
kernel application and the Linux kernel.
Meeting Linux Typically accessed via wrapper functions of the libc. The
Interacting
with Linux
name of the wrapper function is usually the same as the
Writing code name of the system call that it invokes.
for Linux
Your first module
More than 320 on x86, some are architecture-specific, but
More docu- most are common.
mentation
One of the key component of Linux’ maturity.
Lionel Auroux
Introduction
The kernel stores msgs in a circular log buffer:
The Linux
kernel
/proc/ksmg for raw output
Meeting Linux
/dev/kmsg for structured message reading
Interacting
with Linux By default the kernel log is outputted on the console, see
Writing code
for Linux
the console= kernel parameter.
Your first module The dmesg tool (diagnostic message) can be used to read
More docu-
mentation
those messages.
When using systemd, journalctl -k displays the kernel
log.
Writing code
for Linux Unloading
Require CONFIG_MODULE_UNLOAD=y
Your first module
More docu-
mentation rmmod: Unplug the module.
modprobe -r: Also remove unused dependencies.
Info
lsmod: Show the status of modules in the Linux Kernel.
modinfo: Show information about a Linux Kernel module.
Lionel Auroux
Introduction
The Linux There are many pseudo file systems in Linux, here are some of
kernel
them:
Meeting Linux
Interacting
with Linux proc: (/proc) processes info, raw stuff, etc.
Writing code sysfs: (/sys) structured information about various kernel
for Linux
Your first module
subsystems, tied to kobjects
More docu- devtmpfs: (/dev) kernel populated devices nodes
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux Writing code for Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
More docu-
mentation
Lionel Auroux
Introduction http://cscope.sourceforge.net/
The Linux
kernel
Built in VIM and Emacs!
Meeting Linux
Search:
Interacting Symbol definition
with Linux
Symbol usage
Writing code
for Linux Function callers/callee
Your first module
Text
More docu-
mentation
...
You must generate the database.
Use make cscope to get the database of your architecture.
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
More docu-
mentation
Lionel Auroux
Introduction
First off, I’d suggest printing out a copy of the GNU
The Linux
kernel coding standards, and NOT read it. Burn them, it’s a
Meeting Linux great symbolic gesture.
Interacting – Documentation/CodingStyle
with Linux
Writing code
for Linux
Your first module Indentation uses tabs.
More docu- Tabs are 8 spaces.
mentation
No more than 80 chars per line (more a guideline than a
hard rule).
Meeting Linux
Interacting
if (cond) {
with Linux do_foo();
Writing code
for Linux
do_bar();
Your first module }
More docu-
mentation
if (cond) {
do_foo();
do_bar();
} else {
do_baz();
}
Lionel Auroux Linux - Introduction 2017-09-29 37 / 52
Coding style
Linux -
Introduction
Lionel Auroux
if (cond)
do_foo();
Introduction
else
do_baz();
The Linux
kernel
Meeting Linux
Writing code
for Linux int r;
Your first module
More docu-
mentation r = kmalloc(sizeof(*p), GFP_KERNEL);
if (!r)
return -ENOMEM;
return 0;
}
Lionel Auroux
Introduction
The Linux
kernel
No access to the usual libc.
Meeting Linux
No unbreakable memory protection.
Interacting
with Linux No floating-point operations.
Writing code
for Linux
Fixed-size stack.
Your first module Preemptive and Symetric Multi-Processors (SMP) capable.
More docu-
mentation
Synchronization and concurrency are major concerns.
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux
Your first module
Your first module
More docu-
mentation
Lionel Auroux
Introduction
Two alternatives:
The Linux
kernel
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello module");
MODULE_AUTHOR("Rémi Audebert");
Lionel Auroux
Introduction Macros
The Linux
kernel __init: The code is discarded after initialization (both for
Meeting Linux static and module compilation).
Interacting __exit: The code is discarded if the module is built
with Linux
statically or if module unloading is disabled.
Writing code
for Linux
Your first module
More docu-
Module setup/cleanup
int mod_init(void);
mentation
void mod_exit(void);
Lionel Auroux
Lionel Auroux
Introduction
printk() is the kernel space version of printf.
The Linux
kernel Multiple logging levels:
Meeting Linux
pr_emerg(), pr_alert(), pr_crit(), pr_err(),
Interacting
with Linux pr_warn(), pr_notice(), pr_info(), pr_debug()
Writing code
for Linux
Modern code uses pr_xxx.
Your first module pr_devel() while you develop your code.
More docu-
mentation
pr_cont() for continuing lines with no newline (\n).
Formats are described in
Documentation/printk-formats.txt.
Lionel Auroux
Introduction
The Linux
kernel You define the pr_fmt macro to set the default format to all
Meeting Linux your pr_xxx calls.
Interacting
with Linux
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Writing code
for Linux #include <linux/printk.h>
Your first module
...
pr_devel("Test.\n");
More docu-
mentation
Lionel Auroux
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
Introduction # You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
The Linux
kernel
# The current directory is passed to sub-makes as argument
Meeting Linux
PWD := $(shell pwd)
Interacting
with Linux
modules:
Writing code $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
for Linux modules_install:
Your first module
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
More docu- clean:
mentation
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
Lionel Auroux
Enabled if your kernel have CONFIG_DYNAMIC_DEBUG=y.
Introduction
Registers a control file:
The Linux
kernel /sys/kernel/debug/dynamic_debug/control.
Meeting Linux Display the current configuration by reading this file.
Interacting Enable debug calls using:
with Linux
Writing code
for Linux # debug for the module `mymodule`
$ cd /sys/kernel/debug/dynamic_debug
Your first module
More docu-
mentation $ echo 'module mymodule +p' > control
Documentation in
Documentation/dynamic-debug-howto.txt
Lionel Auroux
Introduction
1 Commit:
The Linux
kernel git commit --signoff
Meeting Linux
2 Make a patch:
Interacting
with Linux
git format-patch origin..master
Writing code
for Linux
Your first module
3 Check your patch:
More docu- ./scripts/checkpatch.pl 0001-my-commit.patch
mentation
4 Send email:
git send-email 0001-my-commit.patch
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux
Writing code
for Linux More documentation
Your first module
More docu-
mentation
Lionel Auroux
irc.oftc.net #kernelnewbies
Introduction Web
The Linux
kernel http://kernelnewbies.org
Meeting Linux
Interacting Books
with Linux
Writing code
Warning: Linux moves fast, those books contain outdated
for Linux information.
Your first module
Lionel Auroux
Introduction
The Linux
kernel
Meeting Linux
Interacting
with Linux lionel.auroux [at] gmail.com
Writing code
for Linux
Your first module
More docu-
mentation