
In this course, we will use the following definitions:
Definition
The operating system is the set of software components that enables applications to use the underlying hardware and provides APIs to ease development.
Definition
The kernel is the set of components of the operating system that are executed in a privileged mode, usually in supervisor mode.
Kernels are usually classified in various types:
Let’s have a quick recap of these kernel architectures!
A monolithic kernel embeds all the system functionalities in a single binary. It contains all the core features of an operating system (scheduling, memory management, etc…) as well as drivers for devices or less essential components.
Characteristics
Examples
Why ‘monolithic’?
Monolithic means it is built as a single binary and runs in the same address space. The source code can still be organised in a modular way (e.g., using libraries).
Modularity
Some monolithic kernels allow dynamic code loading as modules, e.g., for drivers. These are usually called modular monolithic kernels.
A microkernel contains only the minimal set of features needed in kernel space:
address-space management, basic scheduling and basic inter-process communication.
All other services are pushed in user space as servers:
file systems, device drivers, high level interfaces, etc.
Characteristics
Examples
The hybrid kernel architecture sits between monolithic kernels and microkernels.
It is a monolithic kernel where some components have been moved out of kernel space as servers running in user space.
While the structure is similar microkernels, i.e., using user servers, hybrid kernels do not provide the same safety guarantees as most components still run in the kernel.
Controversial architecture
This architecture’s existence is controversial, as some just define it as a stripped down monolithic kernel.
Examples
A unikernel, or library operating system, embeds all the software in supervisor mode.
The kernel as well as all user applications run in the same privileged mode.
It is used to build single application operating systems, embedding only the necessary set of applications in a minimal image.
Characteristics
Examples
The choice of architecture has various impacts on performance, safety and interfaces:
In this course, we will focus on a monolithic modular kernel: Linux.
In the 1960s, MIT, AT&T Bell Labs and General Electric built Multics (Multiplexed Information and Computing Service).
Multics is a time-sharing operating system for mainframes that introduced new concepts:
In 1970, AT&T Bell Labs left the project and started Unix, led by Ken Thompson, with Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna.
Unix kept the hierarchical file system but dropped the single-level store, going for an “everything is a file” philosophy.
Unix was originally a single-tasking OS.
Why ‘Unix’?
The name Unix is a pun on Multics/Unics. Kernighan came up with the name, but states that “no one can remember” who came up with the spelling.
Source: https://en.wikipedia.org/wiki/History_of_Unix
From: Linus Benedict Torvalds
To: comp.os.minix
Subject: What would you like to see most in minix?
Date: 25 August 1991, 22:57:08
Hello everybody out there using minix -
I'm doing a (free) operating system (just a hobby, won't be
big and professional like gnu) for 386(486) AT clones. This
has been brewing since april, and is starting to get ready.
I'd like any feedback on things people like/dislike in minix,
as my OS resembles it somewhat (same physical layout of the
file-system (due to practical reasons) among other things).
I've currently ported bash(1.08) and gcc(1.40), and things
seem to work. This implies that I'll get something practical
within a few months, and I'd like to know what features most
people would want. Any suggestions are welcome, but I won't
promise I'll implement them :-)
Linus (torv...@kruuna.helsinki.fi)
PS. Yes - it's free of any minix code, and it has a
multi-threaded fs. It is NOT protable (uses 386 task switching
etc), and it probably never will support anything other than
AT-harddisks, as that's all I have :-(.From: Andrew S. Tanenbaum
To: comp.os.minix
Subject: What would you like to see most in minix?
Date: 30 January 1992, 09:04
/* blablabla */
I still maintain the point that designing a monolithic kernel
in 1991 is a fundamental error. Be thankful you are not my
student. You would not get a high grade for such a design :-)
/* blablabla */
Prof. Andrew S. Tanenbaum (a...@cs.vu.nl)| Year | Version | Features |
|---|---|---|
| 1994 | 1.0 | stable kernel with basic UNIX functionalities |
| 1995 | 1.2–1.3 | round-robin scheduler, loadable modules, /dev/random |
| 1996 | 2.0 | PowerPC support, multicore, improved networking, Tux |
| 1999 | 2.2 | frame buffer, NTFS, FAT32, IPv6, USB, SLAB allocator |
| 2001 | 2.4 | new file systems (ext3, XFS, tmpfs), netfilter |
| 2003 | 2.6 | preemptible kernel, O(1) scheduler, ALSA |
| 2004 | 2.6.4–2.6.10 | EFI support, x86-64, ARMv6, CFQ IO scheduler |
| 2005 | 2.6.14 | FUSE support |
| 2007 | 2.6.20–2.6.23 | KVM, tickless kernel, SLUB allocator, CFS scheduler |
| 2008 | 2.6.24–2.6.28 | cgroups, ext4 |
| 2011 | 2.6.39 | removal of the Big Kernel Lock (BKL) |
| 2014 | 3.14–3.18 | OverlayFS, eBPF, kernel address space layout randomization (KASLR) |
| 2015 | 4.0 | live patching |
| 2018 | 4.15 | kernel page table isolation (security mitigations) |
| 2019 | 5.1 | io_uring |
| 2020 | 5.6 | wireguard |
| 2022 | 6.1 | multi-gen LRU eviction algorithm, initial Rust support |
| 2023 | 6.6 | new EEVDF scheduler |
| 2024 | 6.12 | PREEMPT_RT, sched_ext |
Linux offers six main functions:
through five abstraction layers:
User space interfaces
System calls, procfs, sysfs, device files, …
Virtual subsystems
Virtual memory, virtual filesystem, network protocols, …
Functional subsystems
Filesystems, memory allocators, scheduler, …
Devices control
Interrupts, generic drivers, block devices, …
Hardware interfaces
Device drivers, architecture-specific code, …
1. Tools and environment
2. Core components
3. Specific subsystems
4. Drivers and architecture-specific code
arch/
block/
COPYING
CREDITS
crypto/
Documentation/
drivers/
fs/
include/
init/
ipc/
Kbuild
Kconfig
kernel/
lib/
MAINTAINERS
Makefile
mm/
net/
README
REPORTING-BUGS
samples/
scripts/
security/
sound/
tools/
usr/
virt/
1. Tools and environment
2. Core components
3. Specific subsystems
4. Drivers and architecture-specific code
arch/
block/
COPYING
CREDITS
crypto/
Documentation/
drivers/
fs/
include/
init/
ipc/
Kbuild
Kconfig
kernel/
lib/
MAINTAINERS
Makefile
mm/
net/
README
REPORTING-BUGS
samples/
scripts/
security/
sound/
tools/
usr/
virt/
1. Tools and environment
2. Core components
3. Specific subsystems
4. Drivers and architecture-specific code
arch/
block/
COPYING
CREDITS
crypto/
Documentation/
drivers/
fs/
include/
init/
ipc/
Kbuild
Kconfig
kernel/
lib/
MAINTAINERS
Makefile
mm/
net/
README
REPORTING-BUGS
samples/
scripts/
security/
sound/
tools/
usr/
virt/
1. Tools and environment
2. Core components
3. Specific subsystems
4. Drivers and architecture-specific code
arch/
block/
COPYING
CREDITS
crypto/
Documentation/
drivers/
fs/
include/
init/
ipc/
Kbuild
Kconfig
kernel/
lib/
MAINTAINERS
Makefile
mm/
net/
README
REPORTING-BUGS
samples/
scripts/
security/
sound/
tools/
usr/
virt/
1. Tools and environment
2. Core components
3. Specific subsystems
4. Drivers and architecture-specific code
arch/
block/
COPYING
CREDITS
crypto/
Documentation/
drivers/
fs/
include/
init/
ipc/
Kbuild
Kconfig
kernel/
lib/
MAINTAINERS
Makefile
mm/
net/
README
REPORTING-BUGS
samples/
scripts/
security/
sound/
tools/
usr/
virt/
Tools and environment:
Documentation/
scripts/
usr/
tools/
samples/
x
text documentation, in addition to comments
scripts used for configuration, formatting, etc…
utilities to generate the Linux image
user space tools to interact with the kernel
code samples (a good place to start)
Core components:
init/
x
kernel/
lib/
include/
x
kernel start up code (including main.c)
main kernel components code
libc used to build the kernel
headers
x
Specific subsystems
block/
crypto/
fs/
ipc/
mm/
net/
security/
sound/
virt/
x
x
drivers for block devices
cryptographic algorithms, hashes, …
file systems
inter-process communication
memory management
network support
kernel security mechanisms
sound drivers, audio support
virtualisation support (kvm)
x
Drivers
arch/
x
drivers/
x
x
architecture-specific code for each processor family
drivers for various hardware

git, checkpatch.pl, and sparse help maintain code quality and consistency.C Standard: C11 since version 5.18 (March 2022).

| Keyword | Description |
|---|---|
#pragma GCC |
Provides compiler-specific directives. |
__attribute__ |
Modifies the behavior of functions, variables, or types. |
__builtin__ |
Provides built-in functions for specific operations, often optimized. |
__asm__ |
Allows embedding assembly code directly in C/C++. |
__typeof__ |
Retrieves the type of an expression or variable. |
__restrict__ |
Indicates exclusive use of a pointer for optimizations. |
__volatile__ |
Prevents the compiler from optimizing an instruction or variable. |
__thread |
Declares a thread-local variable (Thread Local Storage). |
asmlinkage |
Enforces specific calling conventions for system calls. |
__builtin_expect |
Hints to the compiler about branch prediction. |
Note
These keywords are not part of the C standard and are specific to gcc. Some of them are also available in other compilers, but their syntax may vary. The kernel can currently be compiled with Clang and ICC.
Before diving into the core of this vast system, in this lecture we will review or learn some advanced aspects of the C language.
However, to fully benefit from this course, you should already have a solid understanding of the basic system concepts.
Macros in C are preprocessor directives defined with #define, enabling text substitution before compilation.
They are invaluable for centralizing constants, simplifying code, and enhancing maintainability.
$ gcc -E your_file.c -o your_file_after_preprocessor.c
The C standard defines several predefined macros that provide useful information about the compilation environment:
__FILE__: Expands to the name of the current file as a string literal.__LINE__: Expands to the current line number in the source file as an integer constant.__func__: Expands to the name of the current function as a string literal (C99).__DATE__: Expands to the date of compilation as a string literal in the format “Mmm dd yyyy”.__TIME__: Expands to the time of compilation as a string literal in the format “hh:mm:ss”.In addition to the standard predefined macros, GCC provides several useful predefined macros:
__VERSION__: Expands to a string literal containing the version of GCC.__OPTIMIZE__: Defined if optimizations are enabled.__BASE_FILE__: Expands to the name of the main source file being compiled.__FILE_NAME__: Expands to the name of the current file being compiled, including the path.__COUNTER__: Expands to the number of times the macro has been invoked in the current translation unit.The C standard allows adding arguments to macros to make them more generic, but they require special attention to avoid errors.
The kernel uses and abuses parameterized macros to avoid function calls and their associated overhead.
Since it does not use naming conventions, it is often necessary to look at the declaration to know if a call is a macro or a function.
Provide the declaration of the macro phy_div found in the file net/wireless/realtek/rtw89/phy.h of the Linux kernel.
This macro divides two numbers but returns 0 if the denominator is zero.
GCC provides a way to refer to the type of an expression is with typeof.
The syntax of using this keyword looks like sizeof, but the construct acts semantically like a type name defined with typedef.
The use of macros should be completely transparent to the user. In the kernel, it is often discovered late that a function being used is actually a macro.
This poses a problem when the function-like macro is used in a conditional expression, as in the macro virtio_cread used in virtio_bt.c.
$ make virtio_bt.c: In function ‘main’: virtio_bt.c:313:9: error: ‘else’ without a previous ‘if’ 313 | else | ^~
do { ... } while(0) is a generic solution commonly used in the kernel.Note
In practice, the do {} while(0) construct is often used to define empty functions in macros. This ensures that the macro behaves like a regular function, even when it does nothing. An example can be seen in the file include/linux/interrupt.h when we want to disable the hard_irq_disable function:
While macros are powerful, they can lead to unexpected behavior due to their lack of type checking and potential for side effects.
Inline functions provide a safer and more maintainable alternative in many cases.
The inline keyword allows the compiler to replace a function call by the body of the called function.
| Criterion | Classic Function | Macro with Parameters | Inline Function |
|---|---|---|---|
| Readability and debugging | Easy to read and debug | Makes debugging harder | Easy to read and debug |
| Portability | Standard C | Depends on preprocessor | Depends on standard version |
| Types as parameters | Impossible | parameter can be a type | impossible |
| Criterion | Classic Function | Macro with Parameters | Inline Function |
|---|---|---|---|
| Execution Control | Always a function call | Always substituted | May or may not be inlined |
| Type checking | Compiler checks types | No type checking | Compiler checks types |
| Side-effect handling | Managed by compiler | Side effects not controlled | Managed by compiler |
| Criterion | Classic Function | Macro with Parameters | Inline Function |
|---|---|---|---|
| Function call cost | Function call overhead | No call, direct substitution | No call, inlined in code |
| Code size | Code factorization | Multiple substitutions | Multiple Inlining |
| Optimization after substitution | Generic code | Contextual optimization | Contextual optimization |
| Register usage | Optimized by compiler | Increased register pressure | Increased register pressure |
GCC provides several pragmas to control the behavior of the compiler for specific sections of code.
Pragmas are introduced with #pragma and are often used for optimizations, warnings, or memory alignment.
| Pragma | Description |
|---|---|
#pragma once |
Ensures a header file is included only once during compilation. |
#pragma GCC optimize |
Specifies optimization levels or flags for specific sections of code. |
#pragma GCC diagnostic |
Controls warnings or errors (e.g., ignore or treat as errors). |
#pragma pack |
Adjusts the alignment of structure members to reduce memory usage. |
#pragma GCC poison |
Prevents the use of specific identifiers in the code. |
#pragma GCC target |
Enables specific CPU instructions (e.g., SSE, AVX) for a section of code. |
Warning: Pragmas are compiler-specific and may not be portable across different compilers.
To avoid name conflicts, GCC introduces a generic keyword __attribute__((...)).
Each attribute is a GCC-specific extension that allows modifying the behavior of functions, variables, and types.
Example: The noreturn attribute specifies that a function does not return, allowing the compiler to optimize accordingly.
The syntax of GCC attributes can be verbose, so the Linux kernel simplifies their usage with macros.
Each declaration includes links to external attribute documentation (e.g., gcc, clang).
The kernel defines, for example, an alias for the attribut noreturn as __noreturn:
This alias is used in the scheduler include/linux/sched/task.h:
The kernel code introduces the attributes likely() and unlikely() to improve branch prediction.
/*
* Using __builtin_constant_p(x) to ignore cases where the return
* value is always the same. This idea is taken from a similar patch
* written by Daniel Walker.
*/
# ifndef likely
# define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
# endif
# ifndef unlikely
# define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
# endifThe asmlinkage annotation tells the compiler to always place the arguments of a function on the stack.
Without it, gcc may try to optimise function calls by placing arguments in registers instead.
Using asmlinkage prevents this optimisation, simplifying calling this function from assembly code.
It is mainly used in system calls in order to enforce the calling convention.
In practice, asmlinkage is a macro defined in asm/linkage.h:
A union is a special type that allows storing different types of data at the same memory location.
Each member of a union is a typed alias of the same memory location.
The allocated size is equal to the size of the largest member of the union.
Examples in the kernel
Each kernel thread has its own stack.
Historically, the thread_info structure was placed at the bottom of the kernel stack.
The union overlays both in the same memory, allowing the kernel to find thread_info by masking the stack pointer.
Note
Today, major architectures (x86, ARM, ARM64, RISC-V, PowerPC, S390) have moved thread_info into task_struct, for security reasons: a stack overflow could corrupt thread_info and escalate privileges.
This union remains in use only on older or less maintained architectures (MIPS, SPARC, M68K, etc.).
struct pageThe struct page is one of the worst union example \(\rightarrow\)
struct page {
unsigned long flags; /* Atomic flags, some possibly
* updated asynchronously */
/*
* Five words (20/40 bytes) are available in this union.
* WARNING: bit 0 of the first word is used for PageTail(). That
* means the other users of this union MUST NOT use the bit to
* avoid collision and false-positive PageTail().
*/
union {
struct { /* Page cache and anonymous pages */
/**
* @lru: Pageout list, eg. active_list protected by
* lruvec->lru_lock. Sometimes used as a generic list
* by the page owner.
*/
union {
struct list_head lru;
/* Or, for the Unevictable "LRU list" slot */
struct {
/* Always even, to negate PageTail */
void *__filler;
/* Count page's or folio's mlocks */
unsigned int mlock_count;
};
/* Or, free page */
struct list_head buddy_list;
struct list_head pcp_list;
};
/* See page-flags.h for PAGE_MAPPING_FLAGS */
struct address_space *mapping;
union {
pgoff_t index; /* Our offset within mapping. */
unsigned long share; /* share count for fsdax */
};
/**
* @private: Mapping-private opaque data.
* Usually used for buffer_heads if PagePrivate.
* Used for swp_entry_t if PageSwapCache.
* Indicates order in the buddy system if PageBuddy.
*/
unsigned long private;
};
struct { /* page_pool used by netstack */
/**
* @pp_magic: magic value to avoid recycling non
* page_pool allocated pages.
*/
unsigned long pp_magic;
struct page_pool *pp;
unsigned long _pp_mapping_pad;
unsigned long dma_addr;
atomic_long_t pp_ref_count;
};
struct { /* Tail pages of compound page */
unsigned long compound_head; /* Bit zero is set */
};
struct { /* ZONE_DEVICE pages */
/** @pgmap: Points to the hosting device page map. */
struct dev_pagemap *pgmap;
void *zone_device_data;
/*
* ZONE_DEVICE private pages are counted as being
* mapped so the next 3 words hold the mapping, index,
* and private fields from the source anonymous or
* page cache page while the page is migrated to device
* private memory.
* ZONE_DEVICE MEMORY_DEVICE_FS_DAX pages also
* use the mapping, index, and private fields when
* pmem backed DAX files are mapped.
*/
};
/** @rcu_head: You can use this to free a page by RCU. */
struct rcu_head rcu_head;
};
union { /* This union is 4 bytes in size. */
/*
* For head pages of typed folios, the value stored here
* allows for determining what this page is used for. The
* tail pages of typed folios will not store a type
* (page_type == _mapcount == -1).
*
* See page-flags.h for a list of page types which are currently
* stored here.
*
* Owners of typed folios may reuse the lower 16 bit of the
* head page page_type field after setting the page type,
* but must reset these 16 bit to -1 before clearing the
* page type.
*/
unsigned int page_type;
/*
* For pages that are part of non-typed folios for which mappings
* are tracked via the RMAP, encodes the number of times this page
* is directly referenced by a page table.
*
* Note that the mapcount is always initialized to -1, so that
* transitions both from it and to it can be tracked, using
* atomic_inc_and_test() and atomic_add_negative(-1).
*/
atomic_t _mapcount;
};
/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */
atomic_t _refcount;
#ifdef CONFIG_MEMCG
unsigned long memcg_data;
#elif defined(CONFIG_SLAB_OBJ_EXT)
unsigned long _unused_slab_obj_exts;
#endif
/*
* On machines where all RAM is mapped into kernel address space,
* we can simply calculate the virtual address. On machines with
* highmem some memory is mapped into kernel virtual memory
* dynamically, so we need a place to store that address.
* Note that this field could be 16 bits on x86 ... ;)
*
* Architectures with slow multiplication can define
* WANT_PAGE_VIRTUAL in asm/page.h
*/
#if defined(WANT_PAGE_VIRTUAL)
void *virtual; /* Kernel virtual address (NULL if
not kmapped, ie. highmem) */
#endif /* WANT_PAGE_VIRTUAL */
#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
int _last_cpupid;
#endif
#ifdef CONFIG_KMSAN
/*
* KMSAN metadata for this page:
* - shadow page: every bit indicates whether the corresponding
* bit of the original page is initialized (0) or not (1);
* - origin page: every 4 bytes contain an id of the stack trace
* where the uninitialized value was created.
*/
struct page *kmsan_shadow;
struct page *kmsan_origin;
#endif
} _struct_page_alignment;A structure is a collection of one or more variables.
Memory alignment
A memory access is aligned if the accessed address is a multiple of the size of the access.
Example: an access to an unsigned long is aligned if the address is a multiple of 8 bytes.
Ordering and Padding - GCC has significant freedom in arranging structure members in memory, but:
The size of primitive types, and therefore structures, varies across architectures and operating systems.
unsigned long| Architecture | Linux (LP64) | Windows (LLP64) | macOS (LP64) |
|---|---|---|---|
| x86-32 | 4 | 4 | 4 |
| x86-64 | 8 | 4 | 8 |
| ARM32 | 4 | 4 | - |
| ARM64 | 8 | 4 | 8 |
struct version| Architecture | Data | Linux | Windows | macOS |
|---|---|---|---|---|
| x86-32 | 7 | 12 | 12 | 12 |
| x86-64 | 11 / 7 | 24 | 12 | 24 |
| ARM32 | 7 | 12 | 12 | - |
| ARM64 | 11 / 7 | 24 | 12 | 24 |
Padding wastes memory, especially in arrays of structures or frequently allocated objects.
GCC provides two ways to compact a structure by removing padding, with attribute or pragma directive.
The Linux kernel also defines its own macro for this in linux/compiler_attributes.h.
__attribute__((packed))#pragma packCaution
Packing removes padding but can degrade performance:
Only use packing when memory savings outweigh the performance cost (e.g., network protocols, on-disk formats, or memory-constrained environments).
__attribute__((aligned(n)))Forces a minimum alignment for a variable or structure member.
Note
False sharing occurs when two CPUs access different variables that share the same cache line, causing unnecessary cache invalidations and performance degradation.
| Macro | Definition | File |
|---|---|---|
__packed |
__attribute__((__packed__)) |
linux/compiler_attributes.h |
__aligned(n) |
__attribute__((__aligned__(n))) |
linux/compiler_attributes.h |
____cacheline_aligned |
__attribute__((__aligned__(SMP_CACHE_BYTES))) |
linux/cache.h |
____cacheline_aligned_in_smp |
Same as above but only in SMP configuration | linux/cache.h |
__page_aligned_data |
Aligned to PAGE_SIZE; placed in the .data section |
linux/linkage.h |
__page_aligned_bss |
Aligned to PAGE_SIZE; placed in the .bss section |
linux/linkage.h |
In C, an array must have a size. It is common to use a struct to keep it close to the array:
This has several drawbacks:
Allocation is done in two steps (allocate the struct, then allocate the array)
One way to overcome this is called tail-padded structures: placing an undefined size array as the last member of a structure.
Multiple implementations are possible:
int buffer[]: in the C99 standard (flexible array member), preferred formint buffer[1]: non-standard, but supported by compilersint buffer[0]: non-standard, but supported by compilersIf you run this code, you get this error:
yes is a pointer to a char (here, the first character of the string "da").
ja is an array identifier, a symbolic constant.
A symbolic constants’s address doesn’t really make sense, so the compiler gives it the value of the constant (hence ja == &ja).
Declaration
A function pointer is declared with the following syntax:
Addressing
You can get a function’s address with the & operator.
Calling a function pointer
As a function argument
Function pointers are frequently used in the kernel to set up callbacks.
Kernel stack is small compared to user stack!
Stack size is statically defined at kernel compile time, cannot grow dynamically.
Usually fits on a few pages:
What to avoid?
Avoid floating point operations at all cost!
Why?
Extremely costly!
Not very useful!
gccMaking changes to your kernel can render it unstable and lead to a kernel panic, i.e., a full system crash.
Keep a backup kernel
Never replace your running kernel with a new one!
Always keep a fully working backup kernel installed in your bootloader!
Work in modules
Always implement your changes as modules if possible.
Important
Keep in mind that a bug can corrupt persistent data, e.g. on your hard drive. You could lose data for good if you work directly on your system!
Tip
Working in a virtual machine alleviates most of these issues!
In the kernel, you won’t have access to the usual libraries like the libc.
Thankfully, the kernel provides its own internal “library” with basic functionalities.
They are described in Documentation/core-api/index.rst.
Let’s make a quick tour of some of these functionalities!
To ensure portability across architectures, the kernel offers generic types defined in include/linux/types.h
Functions in the kernel follow the same convention as system calls by returning an integer:
If the function returns a pointer:
Return NULL if there is only one reason to fail
Return the error code encoded with the ERR_PTR() macro.
The calling function can check if there was an error with IS_ERR() and get the error code with PTR_ERR()
int do_shash(unsigned char *name, unsigned char *result, const u8 *data1, unsigned int data1_len,
const u8 *data2, unsigned int data2_len, const u8 *key, unsigned int key_len)
{
int rc;
unsigned int size;
struct crypto_shash *hash;
struct sdesc *sdesc;
hash = crypto_alloc_shash(name, 0, 0);
if (IS_ERR(hash)) {
rc = PTR_ERR(hash);
pr_err("%s: Crypto %s allocation error %d\n", __func__, name, rc);
return rc;
}
/* ... */If you need to print information to be available from user space, e.g., tracing or debugging, you can use the printk() function.
It works similarly to printf(), with a couple of differences:
include/linux/kern_levels.h, from KERN_EMERG to KERN_DEBUG.There are also predefined macros for each level:
Tip
Formats are available at Documentation/printk-formats.txt.
Memory allocation is done with the kmalloc() function, similar to malloc().
Some specific characteristics:
Fast (except if blocked waiting for pages)
Allocated memory is not initialised
Allocated memory is contiguous in physical memory
Memory is allocated by areas of \(2^n - k\) bytes (\(k\): a few metadata bytes).
Do not allocate 1024 B if you need 1000 B, you will end up with 2048 B!
Example:
kmalloc GFP flags
The second parameter of kmalloc() is a Get Free Pages (GFP) flag:
More combinations available in include/linux/gfp_types.h.
If you need large chunks of memory, you should not use kmalloc(), and request pages directly with one of these functions:
returns a pointer to a free page after filling it with zeros
returns a pointer to a free page
returns a pointer to a memory area with \(2^{order}\) contiguous pages
Virtual allocation
If you don’t need the memory to be contiguous, you can allocate in the virtual address space instead of physical:
If you need to wait for a resource (e.g., network packet, message), the interface should implement a wait queue to allow your thread to sleep and be woken up when the resource is available.
thread sleeps and will be woken up if wake_up() is called on the wait queue and the condition is true
same as wait_event(), but the thread can also be woken up by a signal
The resource handler calls wake_up() on the queue to wake up waiting threads.
Workqueues allow you to execute code asynchronously.
At creation time, a pool of thread is initialised.
Jobs can then be submitted in the form of a function pointer and a pointer to an argument.
A thread from the workqueue will, asynchronously, check the queue, pop a job and execute it.
Tip
Documentation available in Documentation/core-api/workqueue.rst.
The kernel also offers generic data structures to work with:
Important
Generic data structures in C are not obvious to build…
Instead of having objects in a list, we have the list in the objects!
The “naive” version:
Not generic! You need one list type of list per object type.
When you iterate over the list, how do you get the containing object?
From the address of any member in a structure, how can we get the address of the structure?
e.g., from the address of a list_head element in a structure
Linux implements the container_of macro!
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
* WARNING: any const qualifier of @ptr is lost.
*/
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })After expanding all macros, this looks like this:
offset_of: cast the address 0 to type * and access the membercontainer_of: substract this offset from the address of the memberFor each generic data structure, the kernel provides helpers to use them.
Let’s see examples for circular doubly linked lists (list_head from include/linux/list.h):
Allocators:
Insert/delete:
And a lot more!
Tip
You can find similar helpers for all generic data structures.
Go check them out in the kernel sources!
Resources/objects can be accessed concurrently in the kernel.
There are two reasons this can happen:
Possible solutions:
Concurrency problems can arise due to preemption both on single- and multi-core systems.
In the case of single-core CPUs, it can be solved solely by disabling interrupts, making the kernel code non-preemptible.
In Linux, you can use the following macros:
local_irq_disable(): this uses the proper assembly instruction to disable interrupts on the current core, e.g., cli on x86.local_irq_enable(): this uses the proper assembly instruction to enable interrupts on the current core, e.g., sti on x86.On multi-core systems, disabling interrupts is not sufficient, as other cores might also access data concurrently.
One potential solution is to serialise all kernel code, allowing only one thread at a time to execute code in supervisor mode.
This was the initial solution used in Linux when support for multi-core CPUs was added.
The Big Kernel Lock (BKL) was taken when entering the kernel and released when exiting.
Only one thread at a time was running kernel code.
Pro: Extremely simple to implement and safe
Con: Large performance degradation due to the loss of parallelism for kernel code
Linux and the BKL
Linux had a Big Kernel Lock from the introduction of Symmetric Multi-Processor (SMP) in version 2.0 in 1999 until its removal in 2.6.39 in 2011.
Since the BKL removal, fine-grained synchronisation mechanisms are used in the kernel.
A non-exhaustive list of synchronisation mechanisms and their (partial) API:
Note
Most of these have variations that also disable interrupts when taking a lock.
Concurrent access problems can also be solved by using atomic operations in some cases.
Atomic operations are architecture-specific, and are defined in include/linux/atomic/atomic-instrumented.h
These operations should be used on a specific type, atomic_t, to represent the atomic variable.
You can find the usual atomic operations, for example:
void atomic_add(int i, atomic_t *v);void atomic_dec(atomic_t *v);void atomic_or(int i, atomic_t *v);Defined in Documentation/process/coding-style.rst.
It defines a set of rules that will be enforced when a patch is submitted:
Tip
You can check if your patches are valid with regard to the coding style with the scripts/checkpatch.pl script!
Indentation is done with tabs, not spaces.
Tabs are 8 characters long.
For better readability, the preferred limit on the length of a line is 80 characters.
However, never break user-visible strings, as it also breaks the ability to grep them.
Since 2020, checkpatch.pl only complains about lines longer than 100 characters.
From the coding style documentation:
Now, some people will claim that having 8-character indentations makes the code
move too far to the right, and makes it hard to read on a 80-character terminal
screen. The answer to that is that if you need more than 3 levels of
indentation, you're screwed anyway, and should fix your program.
Philosphy of function-versus-keyword usage.
No spaces after functions
Spaces after keywords (except if they are used like function, e.g., sizeof)
if, switch, case, for, do, while
For operators:
Spaces on both sides of binary and ternary operators
= + - < > * / % | & ^ <= >= == != ? :
No space after unary operators or before/after postfix/prefix increment and decrement
& * + - ~ ! ++ –
No space around structure member operators
. ->
No trailing spaces at the end of lines
From the official kernel website, kernel.org, download the tarball archive.
Or from the command line, for example:
$ wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.5.7.tar.xz
You can (and should) also check out the integrity of the tarball with the PGP signature:
$ wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.5.7.tar.sign $ unxz linux-6.5.7.tar.xz # the signature is done on the decompressed tarball $ gpg –verify linux-6.5.7.tar.sign linux-6.5.7.tar
This will probably fail because you don’t have the public keys of the maintainers that generated the tarball.
Get them from the kernel’s key server (documentation):
$ gpg2 –locate-keys torvalds@kernel.org gregkh@kernel.org
You can also clone Linus Torvalds’ git tree:
$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
The kernel configuration describes the features that will be enabled in the built binary, as well as change their behavior.
It also describes if features should be built-in the binary or compiled as modules.
By default, the Makefile-based build system uses the file .config located at the root of the kernel sources.
You can generate initial configurations with the following commands (non-exhaustive list):
make allnoconfig : minimal, everything that can be disabled is disabledmake defconfig : default configuration for the local architecturemake localmodconfig : configuration based on the current state of the machine (plugged devices, etc.) and builds them as modulesmake localyesconfig : same but everything is built-inmake oldconfig : keeps the values of the current .config and asks for the new optionsOr you can copy the configuration of your running kernel:
$ cp /boot/config-$(uname -r)* .config # available on some distros $ zcat /proc/config.gz > .config # available if CONFIG_IKCONFIG_PROC is enabled
If you need to know more about your hardware to generate your config, check out these commands:
lshwd, lscpu, lspci, lsusb, …cat /proc/cpuinfo, cat /proc/meminfo, …dmidecode, hdparmdmesgThe kernel build system is based on Makefiles.
Just run make to compile it.
$ time make real 80m15.486s user 74m54.606s sys 5m32.300s
Compilation can take a long time, so do it in parallel!
$ make -j $(nproc)
The compilation produces the following important files:
vmlinux: the raw Linux kernel image. This ELF is used for debugging and profiling;System.map: symbol table of the kernel. Not necessary at run time, used for debugging;arch/<arch>/boot/bzImage: compressed image of the kernel. This is the one that will be loaded and used.$ du -sh vmlinux arch/x86/boot/bzImage 49M vmlinux 13M arch/x86/boot/bzImage
You can get some info on the image with the file command:
$ file arch/x86/boot/bzImage arch/x86/boot/bzImage: Linux kernel x86 boot executable bzImage, version 6.5.7-lkp (redha@wano) #2 SMP PREEMPT_DYNAMIC Thu Oct 19 16:05:37 CEST 2023, RO-rootFS, swap_dev 0XC, Normal VGA
Two main steps:
1. Install the kernel image, the symbol map and the initrd
$ make install
This will copy the image and symbol map in /boot, and generate the initramfs.
2. Install the modules
$ make modules_install
This will copy the modules (.ko files) into /lib/modules/<version>.
The symbol map (System.map) provides the list of the symbols available in this kernel, their address and type.
$ head System.map 0000000000000000 D __per_cpu_start 0000000000000000 D fixed_percpu_data 0000000000001000 D cpu_debug_store 0000000000002000 D irq_stack_backing_store 0000000000006000 D cpu_tss_rw 000000000000b000 D gdt_page 000000000000c000 d exception_stacks 0000000000014000 d entry_stack_storage 0000000000015000 D espfix_waddr 0000000000015008 D espfix_stack
Check the manpage of the nm program for an explanation of the types.
As a rule of thumb (mostly true), lowercase means local scope while uppercase means global scope (i.e., exported symbol).
In Lab 2, task 3, you were asked to replace the init binary by a hello_world program, which led to a kernel panic. Why?
Roles of init
Characteristics of init
Demo time!
Multiple development methods:
In this course, we will use the last method with QEMU as a hypervisor.
A module is a library dynamically loaded into the kernel. It triggers a call to a registered function when loaded and when unloaded.
The kernel provides two macros to register these functions: module_init() and module_exit().
For these to work, you will need some header files included:
You should also add some information about your module with some pre-defined macros, usually at the beginning of the file:
These can be checked on any module:
$ modinfo hello.ko filename: hello.ko description: Hello World module author: Redha Gouicem, RWTH license: GPL vermagic: 6.5.7-ARCH 686 gcc-13.2.1 depends:
Warning
The license is not only informative. It is also used to check if you are allowed to use some symbols in the kernel.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
MODULE_DESCRIPTION("Hello world module");
MODULE_AUTHOR("Redha Gouicem, RWTH");
MODULE_LICENSE("GPL");
static int __init hello_init(void)
{
pr_info("Hello World!\n");
return 0;
}
module_init(hello_init);
static void __exit hello_exit(void)
{
pr_info("Goodbye World...\n");
}
module_exit(hello_exit);Annotations
The __init and __exit annotations are used to help the compiler optimize the memory usage.
When some module is statically built-in the kernel binary, functions tagged with these annotations are placed in specific segments:
.init.text that is freed after the boot of the kernel.exit.text that is never loaded in memoryThe running kernel is deployed with a generic Makefile located in /lib/modules/$(uname -r)/build.
You can use it from anywhere like this:
$ make -C /lib/modules/$(uname -r)/build M=$PWD
This will generate your module as a .ko file (kernel object).
Loading a module can be done with insmod:
$ insmod hello.ko $ dmesg [177814.017370] Hello World!
Unloading a module can be done with rmmod:
$ rmmod hello $ dmesg [177919.956567] Goodbye World…
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
static char *month = "January";
module_param(month, charp, 0660);
static int day = 1;
module_param(day, int, 0000);
static int __init hello_init(void)
{
pr_info("Hello ! We are on %d %s\n", day, month);
return 0;
}
module_init(hello_init);
static void __exit hello_exit(void)
{
pr_info("Goodbye, cruel world\n");
}
module_exit(hello_exit);With default values:
$ insmod hello.ko $ dmesg [180525.067016] Hello ! We are on 1 January
With parameters:
$ insmod hello.ko month=December day=31 $ dmesg [181086.216097] Hello ! We are on 31 December
Like shared libraries, modules are dynamically loaded: they only have access to symbols explicitly exported to them!
By default, they have access to absolutely no variable or function from the kernel, even if they are not static!
Two macros allow to explicitly export symbols to modules:
EXPORT_SYMBOL(s) makes the symbol s visible to all loaded modulesEXPORT_SYMBOL_GPL(s) makes the symbol s visible to all modules with a license compatible with GPL (according to their MODULE_LICENSE)Example: using the pm_power_off() function exported in arch/x86/kernel/reboot.c and available on my system:
$ grep pm_power_off /lib/modules/$(uname -r)/build/System.map ffffffff810ed2f0 t legacy_pm_power_off ffffffff8274d7d8 r __ksymtab_pm_power_off ffffffff838a47f8 B pm_power_off
If a module X uses at least one symbol from module Y, then X depends on Y.
Dependencies are not explicitly defined: they are automatically inferred during the kernel/module compilation.
You can find the list of dependencies in the file /lib/modules/<version>/modules.dep.
This file is generated by the depmod program, who checks which symbols are used by a module, and which module provide these symbols.
You can also check the dependencies of a module with modinfo.
Automated dependency solving
Obviously, modules must be inserted in the proper order: if X depends on Y, Y needs to be inserted before X.
If you are using modprobe, it will automatically insert dependencies first.
This is also true for unloading modules (in the reverse order).
When developing something in the kernel, the first design choice is “how?”
You have two choices:
Whenever possible, modules are the best choice, as they have more chances to be merged in the mainline.
While using modules should be your first choice, it also has some drawbacks depending on what you are doing.
Pros:
Cons:
If you need to modify the kernel and distribute your changes, you most likely will use patches.
A patch is the result of the diff command applied on the original files and your modified version.
It contains all the data for the patch to automatically apply the changes.
diff: Compares files line by line
patch: Apply changes to existing files
Creating a patch for the kernel tree:
-r to enable recursive patch-u to use the unified diff format (more compact and easier to read)$ unxz linux-6.5.7.tar.xz $ cp -r linux-6.5.7 linux-6.5.7-orig $ cd linux-6.5.7 $ emacs kernel/sched/fair.c $ emacs kernel/sched/sched.h $ cd .. $ diff -r -u linux-6.5.7-orig linux-6.5.7 > new_sched.patch $ xz new_sched.patch
Applying a patch on the kernel tree:
-p 1 to omit the first level in all paths--dry-run to only simulate the patch (for testing purposes)$ unxz linux-6.5.7.tar.xz $ cd linux-6.5.7 $ zcat new_sched.patch | patch -p 1 –dry-run
When you think your code is ready for review by the kernel maintainer, you need to send it to them!
Note: This is just an overview of the process!
Ready your code for public eyes
Prepare your patch(es)
diff or use git format-patch to automatically generate patches from your commits (assuming you used git in the first place)Format your patch series for emailing
git format-patchSend your patch series to the mailing list
scripts/get_maintainer.pl script on your patch. You should also CC anyone who might need to see this, e.g., they work on something similargit send-emailDon’t rely on these slides only!
Go check the full version in the kernel documentation, starting with the kernel development process and patch submission process.
You can check the mailing list online at https://lore.kernel.org.
In monolithic architectures, kernel and user programs are run in different permission levels:
supervisor mode and user mode.
There needs to be communication between the user and the kernel.
Multiple mechanisms are available in the Linux kernel, and choosing the right one is a common discussion on the mailing list.
Communication mechanisms:
An in-memory file system, or ramfs, is a file system not backed by a storage device.
The data stored on it is completely in memory or computed when accessed.
The Linux kernel provides a set of pseudo file systems that are ramfs representing kernel data or configuration.
They usually have a semantic where one file represents one value.
Each pseudo file system has slightly different semantics and answer to different needs: procfs, sysfs, configfs, debugfs, …
Advantage: User space programs can access these files with the standard POSIX file API: read and write.
You can thus use regular shell programs such as cat and echo to read/write from/to them.
Drawback: These mechanisms are synchronous from user to kernel, but asynchronous in the other direction,
i.e., user space applications cannot be notified when the value represented by a file changes in memory.
A pseudo file system is a component of the kernel that has to be enabled and mounted before being used.
1. Check that your kernel has been built with the needed pseudo file system
$ zcat /proc/config.gz | grep CONFIG_DEBUG_FS # you can also grep in your .config directly CONFIG_DEBUG_FS=y
2. Mount the pseudo file system
$ mount -t debugfs none /sys/kernel/debug
3. Read a value by reading from a file
$ cat /sys/kernel/debug/sched/wakeup_granularity_ns 4000000
4. Modify a value by writing to a file
$ echo 3000000 > /sys/kernel/debug/sched/wakeup_granularity_ns
Tip
You might need to have root privileges to read/write to/from these special files.
sudo cat <file>echo <value> | sudo tee <file>The oldest pseudo file system, mounted in /proc.
In lab 2, we saw that the procfs was mounted by the init script.
Goal: Export information about processes.
Since its creation, it has been used for more than that, e.g., exporting kernel data from various subsystems.
Using it is now discouraged for anything unrelated to processes.
Advantage: most widely documented
Drawback: no real structure enforced
The procfs provides two APIs:
PAGE_SIZE, usually 4 KB)seq_file API: more complex, but allows larger data to be exported with a list of buffersLet’s see an example procfs file that exports a variable from the kernel in human-readable form in /proc/my_state.
We want to export the value of the system_enabled global variable.
read() function that will be called when our file is read from:
struct file_operations that will be used for our fileread() function following the prototypestruct file_operations we just created in the init function of your modulestatic int system_enabled;
static ssize_t system_state_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
const char *tmp = system_enabled ? "The system is enabled\n"
: "The system is disabled\n";;
return simple_read_from_buffer(buf, count, ppos, tmp, strlen(tmp));
}
static const struct file_operations system_state_fops = {
.open = simple_open,
.read = system_state_read,
.llseek = noop_llseek,
};
static struct proc_dir_entry *system_state_proc_dir;
static int system_state_init(void)
{
system_state_proc_dir = proc_create("my_state", 0, NULL,
&system_state_fops);
return 0;
}
module_init(system_state_init);
static void system_state_exit(void)
{
remove_proc_entry("my_state", NULL);
}
module_exit(system_state_exit);Successor to the procfs, mounted in /sys.
Goal: Store information about subsystems, hardware devices, drivers, …
This should be the default choice!
Advantages:
Cons:
PAGE_SIZE)The struct kobject is at the heart of the sysfs:
Most important fields:
struct kobject {
const char *name; // name of the directory
struct kobject *parent; // kobject of the parent directory
struct kset *kset; // the collection of kobjects this object belongs to
struct kref kref; // reference counter, used to free the memory properly
const struct kobj_type *ktype; // type of the object, with functions pointers to manipulate it
/* ... */
};To create a kobject and add it to the sysfs, you can use this functions:
Caution
This works for simple cases (most likely what you want to do). For more complex scenarios, there are other functions to initialize, create and register kobjects. Check out the documentation for more information!
Kobject attributes
Each file in the sysfs corresponds to one single value and is associated with an instance of a struct kobj_attribute.
Kobject attributes can be created with the following macro:
You can also create group of attributes to have multiple files in the same directory:
We want to export the state of our system represented by an int system_enabled global variable in a human-readable form in the file located at
/sys/kernel/my_state/system_enabled.
static int system_enabled;
static ssize_t system_state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "The system is %srunning\n", system_enabled ? "" : "not ");
}
static struct kobj_attribute system_state_attribute = __ATTR(system_enabled, 0400, system_state_show, NULL);
static struct kobject *my_state_kobj;Now, we need to instantiate the sysfs file when loading our module and destroy it when unloading.
static int __init my_state_init(void)
{
int retval;
my_state_kobj = kobject_create_and_add("my_state", kernel_kobj);
if (!my_state_kobj)
goto error_init_1;
retval = sysfs_create_file(my_state_kobj, &system_state_attribute.attr);
if (retval)
goto error_init_2;
return 0;
error_init_2:
kobject_put(my_state_kobj);
error_init_1:
return -ENOMEM;
}
module_init(my_state_init);
static void __exit my_state_exit(void)
{
kobject_put(my_state_kobj);
}
module_exit(my_state_exit);static int system_enabled;
static u64 clock;
static ssize_t system_state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "The system is %srunning\n", system_enabled ? "" : "not ");
}
static ssize_t clock_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%llu\n", clock++);
}
static ssize_t clock_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
u64 val;
int rc = sscanf(buf, "%llu", &val);
if (rc != 1 || rc < 0)
return -EINVAL;
clock = val;
return count;
}
static struct kobj_attribute system_state_attribute = __ATTR(system_enabled, 0400, system_state_show, NULL);
static struct kobj_attribute clock_attribute = __ATTR(clock, 0600, clock_show, clock_store);
static struct attribute *attrs[] = {
&system_state_attribute.attr,
&clock_attribute.attr,
NULL,
};
static struct attribute_group attr_grp = { .attrs = attrs };static struct kobject *my_state_kobj;
static int __init my_state_init(void)
{
int retval;
my_state_kobj = kobject_create_and_add("my_state", kernel_kobj);
if (!my_state_kobj)
goto error_init_1;
retval = sysfs_create_group(my_state_kobj, &attr_grp);
if (retval)
goto error_init_2;
return 0;
error_init_2:
kobject_put(my_state_kobj);
error_init_1:
return -ENOMEM;
}
module_init(my_state_init);
static void __exit my_state_exit(void)
{
kobject_put(my_state_kobj);
}
module_exit(my_state_exit);configfs is another ram-based file system offering the converse functionality to sysfs, mounted in /sys/kernel/config.
While the sysfs is a view of kernel objects, configfs is a manager of kernel objects,
i.e., it allows to create/destroy kernel objects from user space.
Example: Allowing user programs to create and configure a virtual network devices by doing an mkdir in the configfs directory of a driver.
Advantage:
Drawbacks:
The debugfs offers a very flexible and simple API targeted at simplifying kernel development and debugging.
It is mounted in /sys/kernel/debug.
Advantages:
Drawback:
static int system_state;
static struct dentry *my_state_dir;
static int my_state_init(void)
{
struct dentry *new_file;
my_state_dir = debugfs_create_dir("my_state", NULL);
if (my_state_dir == 0)
return -ENOTDIR;
new_file = debugfs_create_u8("system_state", 0444, my_state_dir, (u8 *) &system_state);
if (new_file == 0) {
debugfs_remove_recursive(my_state_dir);
return -EINVAL;
}
return 0;
}
module_init(my_state_init);
static void my_state_exit(void)
{
debugfs_remove_recursive(my_state_dir);
}
module_exit(my_state_exit);Pseudo file system-based mechanisms are:
The kernel provides various synchronous communication mechanisms:
System calls are the most classic user-kernel communication mechanism.
They allow user space applications to execute privileged kernel code:
They are the core API of the kernel, with some limitations:
There are two ways of making system calls:
syscall instruction way (x86, amd64, ARM64)System calls are a software interrupt like the others:
1. Place the system call number in a register
2. Place the arguments in the proper registers and/or on the stack
3. Trigger the “system call” interrupt (switching to supervisor mode)
4. Jump to the “system call” interrupt handler
5. Load the system call table and jump to the index given by the syscall number
6. Execute the system call handler
7. Return the result to user space (switching back to user mode)
Some architectures provide a specific instruction (syscall, svc, sysenter, …):
1. Place the system call number in a register
2. Place the arguments in the proper registers and/or on the stack
3. Use the system call instruction (switching to supervisor mode)
4. Jump to the index given by the syscall number in the syscall table
5. Execute the system call handler
6. Return the result to user space (switching back to user mode)
One level of indirection is bypassed!
Tip
You can find a very detailed (with less omissions) explanation of how system calls work in Linux here!
API: Application Programming Interface
High-level interface for programmers (function prototypes, data types, …)
ABI: Application Binary Interface
Low-level interface for compilers/OS (calling conventions, architecture-specific)
| Architecture | syscall# | retval | arg1 | arg2 | arg3 | arg4 | arg5 | arg6 | arg7 |
|---|---|---|---|---|---|---|---|---|---|
| Arm EABI | r7 | r0 | r0 | r1 | r2 | r3 | r4 | r5 | r6 |
| arm64 | w8 | x0 | x0 | x1 | x2 | x3 | x4 | x5 | |
| mips | v0 | v0 | a0 | a1 | a2 | a3 | a4 | a5 | |
| riscv | a7 | a0 | a0 | a1 | a2 | a3 | a4 | a5 | |
| x86-64 | rax | rax | rdi | rsi | rdx | r10 | r8 | r9 |
How do we add a system call to Linux?
We’ll see that a bit later, when you’ll need to do it for an exercise.
ioctls are a way to provide custom system calls, mainly for device drivers.
They are called from user space by the ioctl system call and provide an additional level of indirection, with each ioctl having a number.
Advantages:
Drawback:
An ioctl is tied to a file. It is registered as a file operation similar to read or write in the kernel.
For a given device, each ioctl has a number generated through a set of macros.
How can we use ioctls?
We won’t see the API in details here, you will have a task solely on that in the next lab.
The kernel also provide a socket-based interface with user space called netlink.
It is similar to regular sockets in user space, but with the AF_NETLINK socket family.
Advantage:
Drawback:
Linux Kernel Programming