Linux Kernel Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of Linux kernel programming. It covers various aspects, including the kernel's structure, interaction with user programs, interrupt handling, kernel modules, and concurrency issues. This document is useful for understanding the inner workings of the Linux kernel.
Full Transcript
Linux kernel programming Linux kernel programming 22 / 44 Linux kernel programming Structure of kernel Simplified structure of kernel: initialise data structures at boot time; while (true) { while (timer not gone off) { assign C...
Linux kernel programming Linux kernel programming 22 / 44 Linux kernel programming Structure of kernel Simplified structure of kernel: initialise data structures at boot time; while (true) { while (timer not gone off) { assign CPU to suitable process; execute process; } select next suitable process; } 23 / 44 Linux kernel programming Kernel programming Kernel has access to all resources Kernel programs not subject to any constraints for memory acces or hardware access ⇒ faulty kernel programs can cause system crash 24 / 44 Linux kernel programming Interaction between kernel and user programs Kernel provides its functions only via special functions, called system calls standard C-library provides them Have strict separation of kernel data and data for user programs ⇒ need explicit copying between user program and kernel (copy to user(), copy from user()) 25 / 44 Linux kernel programming Memory of Memory of Memory of user pro ess 1 user pro ess 2 user pro ess 3 o p y _ f r o m _ u s e r o p y _ f r o m _ u s e r o p y _ f r o m _ u s e r o p y _ t o _ u s e r o p y _ t o _ u s e r o p y _ t o _ u s e r Kernel memory 26 / 44 Linux kernel programming In addition, have interrupts: kernel asks HW to perform certain action HW sends interrupt to kernel which performs desired action interrupts must be processed quickly ⇒ any code called from interrupts must not sleep 27 / 44 Linux kernel programming Linux kernel modes Structure of kernel gives rise to two main modes for kernel code: process context: kernel code working for user programs by executing a system call interrupt context: kernel code handling an interrupt (eg by a device) have access to user data only in process context Any code running in process context may be pre-empted at any time by an interrupt Interrupts have priority levels Interrupt of lower priority are pre-empted by interrupts of higher priority 28 / 44 Linux kernel programming Kernel modules can add code to running kernel useful for providing device drivers which are required only if hardware present modprobe inserts module into running kernel rmmod removes module from running kernel (if unused) lsmod lists currently running modules 29 / 44 Linux kernel programming Concurrency issues in the kernel Correct handling concurrency in the kernel important: Manipulation of data structures which are shared between code running in process mode and code running in interrupt mode code running in interrupt mode must happen only within critical regions In multi-processor system even manipulation of data structures shared between code running in process context must happen only within critical sections 30 / 44 Linux kernel programming Achieving mutual exclusion Two ways: Semaphores/Mutex: when entering critical section fails, current process is put to sleep until critical region is available ⇒ only usable if all critical regions are in process context Functions: DEFINE_MUTEX(), mutex_lock(), mutex_unlock() Spinlocks: processor tries repeatedly to enter critical section Usable anywhere Disadvantage: Have busy waiting Functions: spin_lock_init(), spin_lock(), spin_unlock() 31 / 44 Linux kernel programming Use of semaphores Have two kinds of semaphores: Normal semaphores Read-Write semaphores: useful if some critical regions only read shared data structures, and this happens often 32 / 44 Linux kernel programming Programming data transfer between userspace and kernel Linux maintains a directory called proc as interface between user space and kernel Files in this directory do not exist on disk Read-and write-operations on these files translated into kernel operations, together with data transfer between user space and kernel Useful mechanism for information exchange between kernel and user space 33 / 44 Linux kernel programming A tour of the Linux kernel Major parts of the kernel: Device drivers: in the subdirectory drivers, sorted according to category file systems: in the subdirectory fs scheduling and process management: in the subdirectory kernel memory management: in the subdirectory mm networking code: in the subdirectory net architecture specific low-level code (including assembly code): in the subdirectory arch include-files: in the subdirectory include 34 / 44