Free Linux kernel development course online:
Hello students welcome to this free linux kernel development course online by embedded pathashala, in this course we learn the basics of linux kernel development and understand different concepts related to loadable kernel modules how to insert the lodable kernel modules and different operations we can perform on linux kernel modules, so lets begin this free linux kernel development course.
Environment Setup:
To start your linux kernel development journey you should have below system requirements:
- Operating systems: Ubuntu( either Ubuntu PC or Ubuntu in Virtual Machine), you can use any other machine like Debian, Fedora, Arch Linux or anything but in this course all examples are compiled on Ubuntu 22version
- RAM: Min of 4gb of RAM and is expected
- Harddisk: Min 500GB harddisk is expected
- build tools: build tools need to be installed which we already covered in our previous lecture please follow
Tools required:
- GCC: you should install gcc in your setup to check if it is already installed please give below command: gcc –version if you see a version then gcc is installed if not give command sudo apt install gcc and this will install gcc in your setup.
- build essentials:
sudo apt install build-essentialthis will install the gcc compiler, make utility and standard development libraries - Kernel Headers: Kernel headers provide the required definitions, macros and structures requried to compile the kernel modules, because with out kernel modules compilation will fail, please use below command to install linux kernel modules.
sudo apt install linux-headers-$(uname -r) - Linux kernel source Code: before starting linux kernel development you should clone linux kernel source code from its official repository, this will contain all hte linux kernel subsystems and will help you to understand the kernel structure and explore kernel internals.
git clone https://github.com/torvalds/linux.git - Git version Control: Linux kerenl is massive collaborative project so version control like git is extremely essential, using git you can clone repositories, track code changes, contribute patches to kernel, collaborate with other developers for code enhancements.
sudo apt install git - Text Editor: to write code you should have a text editor, you can use text editor of your choice like vim, GNU Nano, Visual Studio Code, etc.. for beginners I recommend vs code as it has many friently features, to install vs code you can directly download from its officail webiste or you can use this code
sudo snap install code --classic - Kernel Log Viewer: kernel messages are printed using printk() so to see this logs you should have a kernel log viewer tool which is dmesg, for example to see the latest dmesg logs you can give command
dmesg | tail - Module Management Tools: as we will work extensively with linux kernel modules we should have few tools which are below like
insmod to insert the linux kernel module rmmod to remove the linux kernel module lsmod to load the linux kernel module modprobe to load the linux kernel modules with dependencies - Debugging Tools: to debug linux kernel you should have below tools like GDB: GNU debugger, KGDB: kernel debugging
- Flow of Linux kernel Development Tool chain:
Developer Code
|
v
C Source File (.c)
|
v
GCC Compiler
|
v
Kernel Object File (.ko)
|
v
insmod / modprobe
|
v
Loaded into Linux kernel
INTRODUCTION TO LINUX KERNEL DEVELOPMENT:
Linux kernel is the core component of linux operating systems, it act as an intermediate between the computer hardware and user space applications, so when any user space application need to access hardware resources such as memory, CPU or storage devices then it access them through kernel so basically it is bridge.
Clear understanding of linux kernel is needed for engineers who want to work on linux kernel development, device drivers role and who want to work on operating systems desgin roles
In this lecture we will cover what is linux kernel, what is linux kernel architecture, kernel space vs user space, what are kernel modules, how to load linux kernel modules how to unload linux kernel modules and how the kernel module flow works, this is introduction lecture in our free linux kernel development course online.
What is linux kernel:
It is the core component in operating sytems which is responsible for managing the sytems resources and enabling communication between hardware and software components of device. below are the critical tasks that linux kernel performs:
- Process Management
- Memory Management
- Device Management
- File system Management
- Network stack handling
Key point is without kernel user space applications can not interact with hardware, for example if you are trying to read a file from disk then the request goes through kernel which communcate with the storage device and then return the requested data back to the application so kernel will act as an intermediate between user space and kernel space
Difference between kernel space and User space:
In linux we have two spaces user space and kernel space so let us understand what is the difference between linux user space and linux kernel space.
User space: Linux user space is the place where linux user space application will run so when you write any desktop application or any user applications all of them will run in the user space example web browsers, text editors, compilers, media players, etc.. and programs running in the userspace can not directly access hardware for security and stability reasons because if by mistake userspace application writes any malicious code or any code that can cause harm to kernel, then entire kernel will crash and entire system will go down.
Kernel space: this is the region where linux kernel will run, so user space can not directly access the hardware, below are functions of linux kerne space Cpu scheduling, memory allocation, device drivers, system calls and everything.
Note: User space communicates with kernel space using linux system calls, examples of linux systems calls: open(), read(), write(), fork() and so on.
Linux kernel architecture:
There are three kinds of linux kernel architectures
1.Monolithic kernel
2. micro kernel
3. Hybrid kernel let us discuss about them in detail
Monolithic kernel:
Monolithc kernel kernel is a kernel architectyure where all the operating sytems services run inside kernel space, that means the components like device drivers, file system management, memory management, proces scheduling, network stack. all are part of single large kernel running in the high previliged mode. Linux kernel follows a monolithic design with modula support.
+-----------------------------+
| User Applications |
+-----------------------------+
System Calls
+--------------------------------------------------+
| Monolithic Kernel |
| |
| Process Management | Memory Management |
| File System | Device Drivers |
| Network Stack | IPC |
+--------------------------------------------------+
| Hardware |
+--------------------------------------------------+
Linux uses a monolithic kernel architecture where most services run inside kernel space.
Microkernel:
A micro kernel is type of architecture that keeps the kernel very samll and then moved the most operating sytesm services to user space, so keeping only essential componets in kernel.
Kernel will contain only essential components such as Interprocess communication, scheduling, low-level memory management.
other applications like device drivers, file systems, networking are moved to user space and they run as separate user space process.
+--------------------------------------+
| User Applications |
+--------------------------------------+
| File System | Drivers | Networking |
| (User Space Services) |
+--------------------------------------+
IPC Communication
+--------------------------------------+
| Microkernel |
| Scheduler | IPC | Memory Mgmt |
+--------------------------------------+
| Hardware |
+--------------------------------------+
Linux uses a micro kernel architecture where most services run inside user space.
advantages of micro kernel: it offers better stability, any crash in driver will not crash the complete kernel, easy to debug
disadvantages of micro kernel: slower performatce due to overhead , and more complex inter process communication. Examples: MINIX, QNX.
Hybrid kernel model:
A hybrid kernel model combines both monolithic kenrel and micro kernel modules, it is used to achieve the performance of monolithic kenrel and modularity, stability of micro kernels, in hybrid kernels aslo some services run in user space and some in kernel space.
+-----------------------------+ | User Applications | +-----------------------------+ +-------------------------------------------+ | Hybrid Kernel | | | | Microkernel Core | | Device Drivers | | File System | | Memory Manager | +-------------------------------------------+ +-----------------------------+ | Hardware | +-----------------------------+
Hybrid kernel architecture
Examples of hybrid kernel : Windows NT, macOS.
Free Learning to all !!
EmbeddedPathashala.
