Presentation is loading. Please wait.

Presentation is loading. Please wait.

How & When The Kernel Runs

Similar presentations


Presentation on theme: "How & When The Kernel Runs"— Presentation transcript:

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

2 Traditional View of Process Execution
Ready Scheduled Running Preempted Creation Terminate Unblocked Blocked Waiting However, the kernel is not a traditional process! CSE 422S –Operating Systems Organization

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

4 Kernel Execution: init Thread
Q: Why does the kernel create an init thread at boot instead of using the original execution context? A: The start_kernel execution context in main.c eventually becomes the main processor idle loop, a.k.a. the swapper or sched process. (Follow the start_kernel function through to cpu_idle_loop() ) CSE 422S –Operating Systems Organization

5 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

6 Kernel Execution: Event Driven
Software/hardware Interrupt arrives Interrupt Handler Hardware interrupt Runs in interrupt context Returns control Software interrupt Kernel preemption Runs in syscall context Returns control Interrupt context is not preemptive Syscall context is preemptive (though preemption can be disabled or enabled) Return path may return control to kernel or user space, or may call schedule() CSE 422S –Operating Systems Organization

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

8 CSE 422S – Operating Systems Organization
System Calls on ARM The ARM-specific details are as follows User library loads syscall number into register R7 May load syscall arguments into registers R0-R6 Executes SWI instruction(software exception) Trap is caught by the kernel Control jumps to function vector_swi() in arch/arm/kernel/entry-common.S Control eventually jumps to a C function inside the kernel CSE 422S – Operating Systems Organization

9 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 userspace_programs]$ man getuid userspace_programs]$ man syscall Use manifest constants (portability, good style) __NR_getuid CSE 422S –Operating Systems Organization

10 Extending the Kernel in C
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 CSE 422S –Operating Systems Organization

11 CSE 422S –Operating Systems Organization
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 unistd.h #define NR_SYSCALLS 292 baz.c foo.c foo.o NR_SYSCALLS = 388 baz.o NR_SYSCALLS = 292 CSE 422S –Operating Systems Organization


Download ppt "How & When The Kernel Runs"

Similar presentations


Ads by Google