Download presentation
Presentation is loading. Please wait.
Published byBeverly Benson Modified over 8 years ago
1
Kernel Structure and Infrastructure David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63130 1
2
Kernel vs. Application Coding Two major differences: – The core kernel must be a monolithic binary – The core kernel must be statically linked Thus, no standard libraries: – No malloc, pthreads, string handling, etc. – Partly because of chicken/egg situation – Also, standard libraries can be too slow No dynamic loading for the core kernel: – Early symbol addresses must be at fixed locations – Core kernel utilities must be efficient – Core kernel utilities must always be available CSE 522S – Advanced Operating Systems2
3
Two Big Problems 1.Programmers rely on library functions for efficiency and correctness – Chicken and egg problem: traditional libraries (like libc ) are built on top of kernel utilities – Kernel code has to be entirely self-contained 2.A totally static kernel would be enormous – About 20 million lines of code in 2015 – Most of this is hardware drivers that are never used on any given platform CSE 522S – Advanced Operating Systems3
4
First Solution: Kernel Libraries Kernel libraries re-implement a lot of the functionality programmers expect in user space – Are statically compiled into the kernel – Automatically available just by including relevant header – Built to be kernel-safe (sleeping, waiting, locking, etc. is done properly) Features: – Utilities: kmalloc, kthreads, string parsing, etc. – Containers: hash tables, binary trees etc. – Algorithms: sorting, compression Mostly found under /lib and /include/linux CSE 522S – Advanced Operating Systems4
5
Example: kmalloc() A unified kernel memory allocator to manage the kernel virtual memory space CSE 522S – Advanced Operating Systems5 Kernel Image Kernel Module Space (insmod) Userspace (mmap syscall) High Memory Like traditional malloc, but handles kernel concerns, such as how to allocate and where to allocate inside of virtual address space. E.g.: GFP_KERNEL - Normal kernel allocation, may block GFP_ATOMIC - Never blocks, for use in interrupt handlers and critical sections GFP_USER - Used when allocating space for user processes And many more... 0x0 0xfffffff
6
Example: Linked Lists A list-able struct must contain struct list_head struct list_head { struct list_head *next; struct list_head *prev; }; If you wanted a list of structs of type data : struct data{ int foo; int bar; struct list_head list; } CSE 522S – Advanced Operating Systems6
7
Example: Linked Lists Initializing a list dynamically: struct data *myList; myList = kmalloc(sizeof(*myList), GFP_KERNEL); myList->foo = 5; myList->bar = 10; INIT_LIST_HEAD(&myList->list); Or statically at compile time: static LIST_HEAD(data_list_head); CSE 522S – Advanced Operating Systems7
8
Example: Linked Lists Functions may take pointers to list nodes or pointers to the list head: Adding: struct data *new_data; list_add(&new_data->list, &data_list_head); Deleting: list_del(&new_data->list); kfree(new_data); //if dynamic CSE 522S – Advanced Operating Systems8
9
Example: Linked Lists Iterating: struct list_head ptr; list_for_each(ptr, data_list_head){ //ptr points to each list structure } struct data *data_ptr; list_for_each_entry(d_ptr, data_list_head, list){ //d_ptr points to each data structure } Also: list_for_each_entry_reverse() list_for_each_entry_safe() //for modifying list elements CSE 522S – Advanced Operating Systems9
10
Example: Linked Lists See also: – /include/linux/list.h – /include/linux/types.h Moral of the story: Always search for functionality before writing it yourself. CSE 522S – Advanced Operating Systems10
11
Second Solution: Loadable Kernel Modules Kernel modules are kernel code that can be loaded dynamically: – Can be loaded/unloaded whenever – Runs in kernel mode – Can access exported kernel variables and functions – Can export variables and functions to kernel or other modules CSE 522S – Advanced Operating Systems11
12
Module Implementation Must define: – An initialization function called on load – An exit function called on unload The init function must be self contained! – Must unwind actions if initialization cannot complete successfully – E.g. if you kmalloc() space but don’t free it, that physical memory is now lost forever. You can also pass parameters to modules at load time. CSE 522S – Advanced Operating Systems12
13
Exported Symbols Not all kernel symbols (functions/variables) are designed to be shared. A kernel subsystem exports symbols that are designed to be used outside of the subsystem. The set of exported symbols is the closest thing to a formal, intra-kernel API. Symbols are exported via the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() Modules should only use exported symbols CSE 522S – Advanced Operating Systems13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.