Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kernel Structure and Infrastructure

Similar presentations


Presentation on theme: "Kernel Structure and Infrastructure"— Presentation transcript:

1 Kernel Structure and Infrastructure
David Ferry, Chris Gill CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63130

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 422S – Operating Systems Organization

3 CSE 422S – Operating Systems Organization
Two Big Problems 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 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 422S – Operating Systems Organization

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 422S – Operating Systems Organization

5 Example: kmalloc() A unified kernel memory allocator to manage the kernel virtual memory space 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... 0xfffffff High Memory Kernel Image Kernel Module Space (insmod) Userspace (mmap syscall) 0x0 CSE 422S – Operating Systems Organization

6 CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization

7 CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization

8 CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization

9 CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization

10 Example: Linked Lists See also: Moral of the story:
/include/linux/list.h /include/linux/types.h Moral of the story: Always search for functionality before writing it yourself. CSE 422S – Operating Systems Organization

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 422S – Operating Systems Organization

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 (until system restart) You can also pass parameters to modules at load time. CSE 422S – Operating Systems Organization

13 CSE 422S – Operating Systems Organization
Exported Symbols A kernel subsystem exports symbols that are designed to be used outside of the subsystem. [ ] module initialized at jiffies [ ] module unloaded at jiffies 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 422S – Operating Systems Organization

14 Loading and Unloading Modules
Modern approach uses modprobe utility Checks dependences, other important features Optional enrichment exercise today uses it Today we will use insmod and rmmod Must use sudo to invoke these on your Rpi ERROR: could not insert module jiffies_module.ko: Operation not permitted Cannot unload a module that’s not loaded ERROR: Module jiffies_module is not currently loaded Cannot load a module that’s already there ERROR: could not insert module simple_module.ko: File exists CSE 422S – Operating Systems Organization


Download ppt "Kernel Structure and Infrastructure"

Similar presentations


Ads by Google