1.05k likes | 1.06k Views
This article discusses the concept of lightweight contexts in operating systems, explaining the difference between processes and threads, the management of non-running threads, the use of thread control blocks, and the switching of threads. It also covers the distinction between kernel and user threads, the organization of memory, and the handling of interrupts.
E N D
Light-weight Contexts: An OS Abstraction for Safety and Performance Landon Cox February 19, 2018
What is a process? • Informal • A program in execution • Running code + things it can read/write • Process ≠ program • Formal • ≥ 1 threadsin their own address space • (soon threads will share an address space)
Parts of a process • Thread • Sequence of executing instructions • Active: does things • Address space • Data the process uses as it runs • Passive: acted upon by threads
Play analogy • Process is like a play performance • Program is like the play’s script What are the threads? Threads What is the address space? Address space
Threads that aren’t running • What is a non-running thread? • thread=“sequence of executing instructions” • non-running thread=“paused execution” • Must save thread’s private state • To re-run, re-load private state • Want thread to start where it left off
Thread control block (TCB) • What needs to access threads’ private data? • The CPU • This info is stored in the PC, SP, other registers • The OS needs pointers to non-running threads’ data • Thread control block (TCB) • Container for non-running threads’ private data • Values of PC, code, SP, stack, registers
Thread control block Address Space TCB1 PC SP registers TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Code Code Stack Stack Stack Thread 1 running CPU PC SP registers
Thread control block Address Space TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Stack Stack Stack Thread 1 running CPU PC SP registers
Switching threads • What needs to happen to switch threads? • Thread returns control to scheduler • For example, via timer interrupt • Scheduler chooses next thread to run • Scheduler saves state of current thread • To its thread control block • Scheduler loads context of next thread • From its thread control block • Jump to PC in next thread’s TCB
Kernel vs user threads • Kernel threads • Scheduler and queues reside in the kernel • User threads • Scheduler and queues reside in user space
32-bit address space 4GB Kernel data (same for all processes) 3GB (0xc0000000) User data (different for every process) 0GB Virtual memory
Switching threads 4GB 3GB Where is the interrupt handler code? In the kernel 0GB Virtual memory
Switching threads 4GB SP Interrupt-handler code PC 3GB On which stack is the IRQ handled? A kernel stack 0GB Virtual memory
Switching threads 4GB SP Interrupt-handler code PC 3GB Where are the threads’ stacks? In user space 0GB Virtual memory
Switching threads 4GB SP Interrupt-handler code PC 3GB Where are the threads’ stacks? In user space 0GB Virtual memory
Switching threads 4GB SP Interrupt-handler code PC 3GB So far, everything is the same for user-level and kernel threads 0GB Virtual memory
Kernel threads Scheduler code Handler code Interrupt-handler code For kernel threads, the thread scheduler and TCB queues are in the kernel TCB TCB TCB
Kernel threads Scheduler code Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB TCB
Kernel threads Scheduler code Synchron. code Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB Also, in the kernel TCB
User-level threads Handler code Interrupt-handler code For user-level threads, the thread scheduler and queues are in user space TCB TCB TCB Scheduler code
User-level threads Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB Also, in the user space TCB Scheduler code Synchron. code
User-level threads Handler code Interrupt-handler code SP TCB TCB PC Scheduler code Thread running user code Synchron. code Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB Timer interrupt! Scheduler code Synchron. code Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB Does the kernel know where user left off? Scheduler code Synchron. code Yes, CPU stores this on kernel stack Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB What does kernel do next? Scheduler code Synchron. code Depends on meaning of the timer … Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB Assuming it’s a signal for the process? Scheduler code Synchron. code Kernel delivers signal to process Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB To deliver a signal to user code: make it look like a forced function call to signal handler. Scheduler code Synchron. code Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB Where is the process’s timer signal handler? Scheduler code Synchron. code In scheduler code (e.g., yield) Program code
User-level threads SP Handler code Interrupt-handler code TCB PC TCB On what stack should the scheduler code run? Scheduler code Synchron. code yield Stack of interrupted thread Program code
User-level threads PC Handler code Interrupt-handler code TCB SP TCB On what stack should the scheduler code run? Scheduler code Synchron. code yield Stack of interrupted thread Program code
User-level threads PC Handler code Interrupt-handler code TCB SP TCB Could the kernel transfer control to any other thread? Scheduler code Synchron. code yield No, it’s only aware of interrupted one Program code
Kernel threads Scheduler code Synchron. code Handler code Interrupt-handler code SP TCB TCB PC TCB Thread running user code Program code
Kernel threads Scheduler code Synchron. code SP Handler code Interrupt-handler code TCB PC TCB TCB Timer interrupt! Program code
Kernel threads Scheduler code Synchron. code SP Handler code Interrupt-handler code TCB PC TCB TCB Which thread could run next? Any thread. Kernel aware of all of them Program code
Advantages of user-level threads • Synchronization primitives are just a function call • Why are kernel threads more expensive? • Accessing thread management must cross kernel boundary • CPU protection checks, argument checks, handler dispatch, etc. • Switching back to thread also crosses kernel boundary • For user threads, easy to tune scheduling policy to application • Why is this harder for kernel threads? • All processes share the same scheduler • Per-process policies require wider, more complex API
Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Which thread could run next? Scheduler code Synchron. code Only the one that was interrupted Program code
Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Why might we be unable to run the interrupted thread? Scheduler code Synchron. code Thread could have made a blocking system call Program code
Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Scheduler code Synchron. code Program code
Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Why is this wasteful? Scheduler code Synchron. code There are other threads that could run! Kernel doesn’t know they exist Program code
Parts of a process (revised) • Thread • Sequence of executing instructions • Active: does things • Address space • Data the process uses as it runs • Passive: acted upon by threads • File descriptors • Communication channels (e.g., files, sockets, pipes) • Passive: read-from/written-to by threads
Parts of a process (revised) Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Each thread has its own stack, but everything else is shared: same pages, file descriptors
Parts of a process (revised) Threads are not well isolated from each other. Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Threads can run in parallel on different cores.
Parts of a process (revised) Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file If one thread fails, entire process fails.
Parts of a process (revised) Process Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Compromising a thread, compromises all data.
Parts of a process (revised) Thread 2 Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Compromising a thread, compromises all data.
Parts of a process (revised) Thread 2 Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file See: heartbleed, cloudbleed, etc.
Heartbleed bug • Nasty bug in OpenSSL implementation • Made public by Google in April, 2014 • “A missing bounds check in the handling of the TLS heartbeat extension.” • What is a heartbeat message? • A small message to check if machine is alive (like in Raft) • In SSL, keeps peers from needlessly renegotiating keys