Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

How & When The Kernel Runs

David Ferry, Chris Gill, Brian Kocoloski, and Marion Sudvarg


Department of Computer Science and Engineering
Washington University, St. Louis MO

1
Overview
1. CSE 361 Review: Programs & Processes
2. CSE 361 Review: System Calls
3. Kernel Execution
4. Today’s Studio

CSE 422S – Operating Systems Organization 2


Program – Source Code

CSE 422S – Operating Systems Organization 3


Program – Object Code

CSE 422S – Operating Systems Organization 4


Source Code -> Program -> Process

Generate program
$ gcc program.c –o a.out

Create process
./a.out

CSE 422S – Operating Systems Organization 5


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


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


Overview
1. CSE 361 Review: Programs & Processes
2. CSE 361 Review: System Calls
3. Kernel Execution
4. Today’s Studio

CSE 422S – Operating Systems Organization 8


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


User/Kernel Interaction

./test sys_open(…) sys_write(…)

User space

Kernel space

< kernel implementation of the < kernel implementation of the


open system call > write system call >

This is all executed by the


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


Linux System Call Implementation
System Call Steps x86 ARM
Put function arguments and Syscall number in %eax Arguments in R0-R6
syscall number in registers %rax is lower 32-bits of Syscall number in R7
%eax
Trap to kernel Old: int $0x80 svc
New: sysenter
CPU executes system_call()
system_call saves execution environment (including argument registers) to stack

Index into system call table based *sys_call_table(,%rax,8) arch/arm/tools/syscall.tbl


on syscall number or
*sys_call_table(,%rax,4)
Execute indexed function: asmlinkage optimizes function call for arguments already on
the stack

CSE 422S – Operating Systems Organization 12


Example

svc: ARM syscall instruction

CSE 422S – Operating Systems Organization 13


Overview
1. CSE 361 Review: Programs & Processes
2. CSE 361 Review: System Calls
3. Kernel Execution
4. Today’s Studio

CSE 422S – Operating Systems Organization 14


Kernel Execution: Boot
Bootloader Initial kernel
loads kernel never returns Idle Task
Power On System Initialize
Boot System (pid 0)

Creates init, which


The kernel only runs creates all other threads
deterministically at ksoftirq
boot time. Otherwise, init
the kernel is entirely (pid 1) migration
event driven.
Like process threads,
Kernel entry point:
start_kernel() in kernel threads are also
/init/main.c scheduled
CSE 422S – Operating Systems Organization 15
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


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


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
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


Overview
1. CSE 361 Review: Programs & Processes
2. CSE 361 Review: System Calls
3. Kernel Execution
4. Today’s Studio

CSE 422S – Operating Systems Organization 20


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


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


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


Today’s 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
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

foo.c baz.c

foo.o baz.o
NR_SYSCALLS = 388 NR_SYSCALLS = 292

CSE 422S – Operating Systems Organization 25

You might also like