Understanding Kernel Execution and Processes

how when the kernel runs l.w
1 / 25
Embed
Share

Explore how the kernel interacts with processes, executes programs, and manages system resources in this insightful study by the Department of Computer Science and Engineering at Washington University.

  • Kernel Execution
  • Processes
  • System Calls
  • Operating Systems

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. How & When The Kernel Runs David Ferry, Chris Gill, Brian Kocoloski, James Orr and Marion Sudvarg Department of Computer Science and Engineering Washington University, St. Louis MO 1

  2. Overview 1. Programs & Processes 2. System Calls 3. Kernel Execution 4. Today s Studio CSE 422S Operating Systems Organization 2

  3. Program Source Code CSE 422S Operating Systems Organization 3

  4. Program Object Code CSE 422S Operating Systems Organization 4

  5. Source Code -> Program -> Process Generate program $ gcc program.c o a.out Create process ./a.out CSE 422S Operating Systems Organization 5

  6. Process Execution A process is an execution context for a program i.e., when you run your program, it is instantiated by the kernel as a process The kernel sees all processes in the system, manages their resources, and schedules them Q: how does the kernel interact with your processes? CSE 422S Operating Systems Organization 6

  7. Processes in Linux The kernel is not a process The kernel is a program What does that mean? The same process executes both: 1. your program s code 2. kernel code CSE 422S Operating Systems Organization 7

  8. Overview 1. Programs & Processes 2. System Calls 3. Kernel Execution 4. Today s Studio CSE 422S Operating Systems Organization 8

  9. User/Kernel Interaction The kernel has many purposes, which we will study in this course. But from the perspective of a user application, the primary purpose it serves is to implement system calls System call: An operation that requires a higher level of privilege than is granted to user applications (e.g., writing data to a file or network device) CSE 422S Operating Systems Organization 9

  10. User/Kernel Interaction ./test sys_open( ) sys_write( ) User space Kernel space < kernel implementation of the write system call > < kernel implementation of the open system call > This is all executed by the same process CSE 422S Operating Systems Organization 10

  11. View of Process Address Space User code User data System Call Interface System Call Handlers Kernel code Kernel data CSE 422S Operating Systems Organization 11

  12. Linux System Call Implementation System Call Steps x86 ARM Put function arguments and syscall number in registers Syscall number in %eax (%eax is lower 32-bits of %rax) Arguments in R0-R6s Syscall number in R7 Trap to kernel Old: int $0x80 New: sysenter or syscall svc CPU executes system_call() system_call saves execution environment (including argument registers) to stack Index into system call table based on syscall number *sys_call_table(,%rax,8) or *sys_call_table(,%rax,4) arch/arm/tools/syscall.tbl Execute indexed function: asmlinkage optimizes function call for arguments already on the stack CSE 422S Operating Systems Organization 12

  13. Example svc: ARM syscall instruction CSE 422S Operating Systems Organization 13

  14. Overview 1. Programs & Processes 2. System Calls 3. Kernel Execution 4. Today s Studio CSE 422S Operating Systems Organization 14

  15. Kernel Execution: Boot Bootloader loads kernel Initial kernel never returns Idle Task (pid 0) System Boot Initialize System Power On Creates init (starts userspace processes), kthreadd (creates other kernel threads) The kernel only runs deterministically at boot time. Otherwise, the kernel is entirely event driven. ksoftirq kthreadd (pid 2) init (pid 1) migration Like process threads, kernel threads are also scheduled Kernel entry point: start_kernel() in /init/main.c /lib/systemd/systemd CSE 422S Operating Systems Organization 15

  16. Kernel Execution: Threads Kernel threads perform background operations, e.g. [ksoftirq] does delayed interrupt handling [migrate] does inter-processor load balancing [kworker] handles misc. tasks Kernel threads are similar to user threads: are scheduled can be preempted However, there are differences: run in kernel context have no process memory space CSE 422S Operating Systems Organization 16

  17. View of Kernel Thread Address Space x No user code x No user data System Call Interface Kernel code Kernel data CSE 422S Operating Systems Organization 17

  18. Exercises Determine all the processes running on the system: ps aux $ ps aux | less - scroll through the list with up/down keys $ ps aux | grep search term - search for process name, pid, username, etc. What process has pid 1? Scroll to the bottom: what do you see? Several process commands start with k (kernel threads) CSE 422S Operating Systems Organization 18

  19. Summary: When does the kernel run? There are three ways the kernel runs 1. When explicitly invoked by processes via system calls (and traps e.g., divide by 0 errors) 2. In response to hardware interrupts 1. Timer interrupt by default, once every millisecond 2. External interrupts e.g., keyboard presses 3. When kernel threads are scheduled CSE 422S Operating Systems Organization 19

  20. Overview 1. Programs & Processes 2. System Calls 3. Kernel Execution 4. Today s Studio CSE 422S Operating Systems Organization 20

  21. Linux System Programming in C User space programs may call kernel functions Indirectly via library calls (more portable) printf ("getuid returned: %u\n", getuid()); Directly via the syscall interface printf ("syscall to getuid returned: %u\n", syscall(__NR_getuid)); Look at the man pages, for files to #include [cdgill@shell userspace_programs]$ man getuid [cdgill@shell userspace_programs]$ man syscall Use manifest constants (portability, good style) __NR_getuid CSE 422S Operating Systems Organization 21

  22. System Calls The syscall interface relies on an integer 1. User library loads syscall number into register 2. May load syscall arguments into other registers 3. Executes syscall trap (software exception) 4. Trap is caught by the kernel 5. Puts arguments on kernel stack (asmlinkage) 6. Kernel looks up syscall number in the interrupt vector 7. Jumps to syscall routine specified in interrupt table CSE 422S Operating Systems Organization 22

  23. System Calls on ARM The ARM-specific details are as follows 1. User library loads syscall number into register R7 2. May load syscall arguments into registers R0-R6 3. Executes SVC instruction ( supervisor call ) 4. Trap is caught by the kernel 5. Control jumps to function vector_swi() in arch/arm/kernel/entry-common.S 6. Control eventually jumps to a C function inside the kernel CSE 422S Operating Systems Organization 23

  24. Todays Studio: Studying the System Call Interface Linux is open source: you can modify a kernel Adding your own system call prototypes asmlinkage long sys_noargs(void); Adding your own system call definitions SYSCALL_DEFINE0( noargs ){ } Connecting them to the system call dispatch table Adding the new definition files to the Makefile Today s studio will give you experience invoking existing system calls, and adding new ones Note: For ARM, system call numbers now automatically added to arch/arm/include/uapi/asm/unistd- common.h by the build process CSE 422S Operating Systems Organization 24

  25. Rebuilding the Kernel Q: Do we really need to issue make clean when rebuilding the kernel? A: Not always, but probably a good idea to do so anyway, especially if you modify header files unistd.h #define NR_SYSCALLS 292 baz.c foo.c foo.o baz.o NR_SYSCALLS = 388 NR_SYSCALLS = 292 CSE 422S Operating Systems Organization 25

More Related Content