The Kernel. wants to be your friend

Similar documents
Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

SE350: Operating Systems

Processes. CS439: Principles of Computer Systems January 30, 2019

The Kernel Abstraction. Chapter 2 OSPP Part I

The Kernel Abstrac/on

Protection. OS central role. Fundamental to other OS goals. OS kernel. isolation of misbehaving applications. Relaibility Security Privacy fairness

Lecture 4: Memory Management & The Programming Interface

Interrupts, Fork, I/O Basics

What is a Process? Processes and Process Management Details for running a program

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Architectural Support for Operating Systems

Processes. CS439: Principles of Computer Systems January 24, 2018

The Kernel Abstraction

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Processes. Johan Montelius KTH

A process. the stack

Processes. Dr. Yingwu Zhu

CS 537 Lecture 2 - Processes

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Recall: Four fundamental OS concepts. Recall: give the illusion of multiple processors? Process Control Block

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes and Threads

Processes. CS3026 Operating Systems Lecture 05

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

518 Lecture Notes Week 3

CSE 153 Design of Operating Systems

Computer architecture. A simplified model

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CS 5460/6460 Operating Systems

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

* What are the different states for a task in an OS?

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

CS510 Operating System Foundations. Jonathan Walpole

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Operating System Structure

Exceptions and Processes

Processes (Intro) Yannis Smaragdakis, U. Athens

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CSE 153 Design of Operating Systems Fall 2018

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

(In columns, of course.)

TCSS 422: OPERATING SYSTEMS

COS 318: Operating Systems

Mon Sep 17, 2007 Lecture 3: Process Management

CS 550 Operating Systems Spring Interrupt

CS399 New Beginnings. Jonathan Walpole

Chap 4, 5: Process. Dongkun Shin, SKKU

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

The Big Picture So Far. Today: Process Management

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Processes and Exceptions

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Fall 2015 COMP Operating Systems. Lab #3

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

CS 333 Introduction to Operating Systems Class 2 OS-Related Hardware & Software The Process Concept

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

CSCE Introduction to Computer Systems Spring 2019

Lecture 4: Process Management

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Process Description and Control

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

CSC369 Lecture 2. Larry Zhang, September 21, 2015

COMP 3100 Operating Systems

Lecture 24: Multitasking and Signals

Multithreaded Programming

CS 326: Operating Systems. Process Execution. Lecture 5

Background: Operating Systems

3.1 Introduction. Computers perform operations concurrently

Introduction to Processes

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Address spaces and memory management

CSE 153 Design of Operating Systems Fall 18

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Operating Systems ECE344

Transcription:

The Kernel wants to be your friend

Boxing them in Buggy apps can crash other apps App 1 App 2 App 3 Operating System Reading and writing memory, managing resources, accessing I/O... Buggy apps can crash OS Buggy apps can hog all resources Malicious apps can violate privacy of other apps Malicious apps can change the OS

The Process An abstraction for protection App 1 OS the execution of an application program with restricted rights But there are tradeoffs (there always are tradeoffs!) Must not hinder functionality still efficient use of hardware enable safe communication

The Process An abstraction for protection App 1 OS the execution of an application program with restricted rights But there are tradeoffs (there always are tradeoffs!) Must not hinder functionality still efficient use of hardware enable safe communication

Actually I know what you are thinking

Special Part of the OS all kernel is in the OS not all the OS is in the kernel (why not? robustness) widgets libraries, window managers etc

Process: Getting to know you A process is a program during execution program is a static file process = executing program = program + execution state Header compiler Initialized data OS copy Source code Executable Image Data Heap Stack Data Heap Stack Physical memory

Keeping track of a process A process has code OS must track program counter A process has a stack OS must track stack pointer OS stores state of process in Process Control Block (PCB) Process Control Block PC Stack Pointer Registers PID UID Priority List of open files Process status Data (program instructions, stack & heap) resides in memory, metadata is in PCB

How can the OS enforce restricted rights? Easy: kernel interprets each instruction! slow many instructions are safe: do we really need to involve the OS?

How can the OS enforce restricted rights? Easy: kernel interprets each instruction! slow many instructions are safe: do we really need to involve the OS? Dual Mode Operation hardware to the rescue: use a mode bit in user mode, processor checks every instruction in kernel mode, unrestricted rights hardware to the rescue (again) to make checks efficient

Efficient protection in dual mode operation Privileged instructions in user mode, no way to execute potentially unsafe instructions Memory protection in user mode, memory accesses outside a process memory region are prohibited Timer interrupts kernel must be able to periodically regain control from running process + Efficient mechanism for switching modes

I. Privileged instructions Examples: Set mode bit; set accessible memory; disable interrupts; etc But how can an app do I/O then? system calls achieve access to kernel mode only at specific locations specified by OS Executing a privileged instruction while in user mode causes a processor exception......which passes control to the kernel

II. Memory Protection Step 1: Virtualize Memory Virtual address space: set of memory addresses that process can touch CPU works with virtual addresses mapped segments DLL s Stack Physical address space: set of memory addresses supported by hardware Virtual address space Heap Initialized data

II. Memory Protection Step 2: Address Translation Implement a function mapping pid, virtual address into physical address Virtual p i Physical a486d9 Advantages: protection relocation data sharing multiplexing 5e3a07

Protection At all times, the functions used by different processes map to disjoint ranges pi pj

Relocation The range of the function used by a process can change over time pi

Relocation The range of the function used by a process can change over time pi

Data Sharing Map different virtual addresses of different processes to the same physical address p i 04d26a p j 5e3a07 119af3

Multiplexing Create illusion of almost infinite memory by changing domain (set of virtual addresses) that maps to a given range of physical addresses pi

Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time pi

Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time pi

Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time pi

Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time pi

A simple mapping mechanism: Base & Bound MAXsys Memory Exception CPU Logical addresses no yes + Physical addresses 1500 1000 500 1000 Bound Register Base Register 0 p s physical address space

On Base & Limit Contiguous Allocation: contiguous virtual addresses are mapped to contiguous physical addresses Protection is easy, but sharing is hard Two copies of emacs: want to share code, but have data and stack distinct... And there is more Hard to relocate We want them as far as as possible in virtual address space, but...

III. Timer Interrupts Hardware timer can be set to expire after specified delay (time or instructions) when it does, control is passed back to the kernel Other interrupts (e.g. I/O completion) also give control to kernel

Crossing the line user process user process executing mode bit = 1 calls system call return from system call trap mode bit := 1 mode bit := 0 return kernel mode bit = 0 execute system call

From user mode to kernel mode... Exceptions user program acts silly (e.g. division by zero) attempt to perform a privileged instruction sometime on purpose! (breakpoints) synchronous Interrupts HW device requires OS service timer, I/O device, interprocessor asynchronous System calls/traps user program requests OS service synchronous

...and viceversa Resume p after exception, interrupt or syscall restore PC, SP, registers; toggle mode If new process copy program in memory, set PC and SP toggle mode Switch to different processq load PC, SP, registers from q s PCB toggles mode User-level upcall a sort of user-level interrupt handling

Making the transition: Safe mode switch Common sequences of instructions to cross boundary, which provide: Limited entry entry point in the kernel set up by kernel Atomic changes to process state PC, SP, memory protection, mode Transparent restartable execution user program must be restarted exactly as it was before kernel got control

Interrupt vector Hardware identifies why boundary is crossed trap? interrupt (which device)? exception? Hardware selects entry from interrupt vector Appropriate handler is invoked Processor Register 0 31 32 128 255 Interrupt Vector handledividebyzero() {... handletrap() {... handletimerinterrupt() {...

Interrupt stack Stores execution context of interrupted process HW saves SP, PC Handler saves remaining registers Stores handler s local variables Pointed by privileged register One per process (or per thread!) Why not use the stack in user s space?

Interrupt masking What if an interrupt occurs while running an interrupt handler? Disable interrupts via privileged instruction Overdramatic it actually defers them Just use the current SP of Interrupt stack

Mode switch on x86 User-level Process Stack segment Registers Offset Kernel foo() { while(...) { x = x+1; y = y-2 Stack segment SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Offset handler() { pusha... Interrupt Stack

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack

Mode switch on x86 User-level Process Registers Kernel foo() { while(...) { x = x+1; y = y-2 Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... handler() { pusha... Interrupt Stack SS:ESP CS:EIP EFLAGS 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack CS:EIP EFLAGS

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack 5. Save error code (optional) CS:EIP EFLAGS

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack 5. Save error code (optional) CS:EIP EFLAGS Error

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupts 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack 5. Save error code (optional) 6. Invoke interrupt handler CS:EIP EFLAGS Error

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupt 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack 5. Save error code (optional) 6. Invoke interrupt handler 7. Handler pushes all registers on stack CS:EIP EFLAGS Error

Mode switch on x86 User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... Kernel handler() { pusha... Interrupt Stack SS:ESP 1. Disable interrupt 2. Save key registers 3. Switch onto the kernel interrupt stack 4. Push key registers onto new stack 5. Save error code (optional) 6. Invoke interrupt handler 7. Handler pushes all registers on stack CS:EIP EFLAGS Error ALL Registers: SS,ESP,CS,EIP, EAX, EBX,...

Switching back From an interrupt, just reverse all steps! From exception and system call, increment PC on return on exception, handler changes PC at the base of the stack on system call, increment is done by hw when saving user level state

System calls Programming interface to the services provided by the OS User Program open() Mostly accessed through an API (Application Programming Interface) system call interface Win32, POSIX, Java API Parameters passed according to calling convention registers, stack, etc. i open() implementation of open() system call... return

System call stubs User Set up parameters call int 080 to context switch open: movl #SysCall_Open, %eax int 080 ret Kernel Validate parameters defend against errors in content and format of args Copy before check prevent TOCTOU Copy back any result

The Skinny Syscall interface allows separation of concern Innovation Compilers Databases robust Email Portable OS Library System call interface simple highly portable Word Processing Web Browsers Narrow powerful Web Servers Portable OS Kernel x86 ARM PowerPC 10Mbps/100Mbps/1Gbps Ethernet 1802.11 a/b/g/n Graphics accellerators SCSI LCD Screens

Upcalls: virtualizing interrupts Interrupts/Exceptions Hardware-defined Interrupts & exceptions Interrupt vector for handlers (kernel) Interrupt stack (kernel) Interrupt masking (kernel) Processor state (kernel) Upcalls/Signals Kernel-defined signals Handlers (user) Signal stack (user) Signal masking (user) Processor State (user)

Signaling Why? To terminate an application To suspend it/resume it (e.g., for debugging) To alert of timer expiration Upon receipt: Ignore Terminate process Catch through handler

More on signals ID Name Default Action 2 SIGINT Terminate 9 SIGKILL Terminate Corresponding Event Interrupt (e.g., CTRL-C from keyboard) Kill program (cannot override or ignore) 14 SIGALRM Terminate Tmer signal 17 SIGCHLD Ignore Child stopped or terminated 20 SIGSTP Stop until SIGCONT Stop signal from terminal (e.g., CTRL-Z from keyboard)

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... signal_handler() {... Kernel Interrupt Stack 1 HW copies current user state in Interrupt stack

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... 2 Kernel copies user state on user stack signal_handler() {... Kernel Interrupt Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX, Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... 3 Kernel changes PC saved on Interrupt stack to point to handler and SP to point after state saved on user stack signal_handler() {... Kernel Interrupt Stack SS:ESP CS:EIP' EFLAGS Other Registers: EAX, EBX,

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX, Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... 4 Kernel exits; Interrupt stack copied back into registers signal_handler() {... Kernel Interrupt Stack SS:ESP CS:EIP CS:EIP' EFLAGS Other Registers: EAX, EBX,

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... signal_handler() {... Kernel Interrupt Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX, 4 Kernel exits; Interrupt stack copied back into registers

Unix signals User-level Process foo() { while(...) { x = x+1; y = y-2 Stack Registers SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX,... signal_handler() {... Kernel Interrupt Stack SS:ESP CS:EIP EFLAGS Other Registers: EAX, EBX, 5 Signal handler returns

Booting an OS Kernel bootloader OS Kernel login app BIOS Basic Input/Output System In ROM, includes the first instructions fetched and executed BIOS copies bootloader, using a cryptographic signature to make sure it has not been tampered with

Booting an OS Kernel bootloader OS Kernel login app BIOS bootloader Bootloader copies OS kernel, checking its cryptographic signature

Booting an OS Kernel bootloader OS Kernel login app BIOS bootloader OS Kernel Kernel initializes its data structures Starts first process by copying it from disk Let the dance BEGIN!

Shall we dance? All processes are progeny of that first process Created with a little help from its friend via system calls!

Starting a new process A simple recipe: Allocate & initialize PCB Create and initialize a new address space Load program into address space Allocate user-level and kernel-level stacks Initialize hw context to start execution at start Copy arguments (if any) to the base of the userlevel stack Inform scheduler process new process is ready Transfer control to user-mode

Which API? Windows: CreateProcess System Call if (!CreateProcess( NULL, // No module name (use command line) argv[1], // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Ptr to PROCESS_INFORMATION structure ) Everything you might want to control CreateProcessAsUser CreateProcessWithLogonW but wait!

Which API? Unix: fork() and exec() fork() Creates a complete copy (child) of the invoking process (parent) but for return value: child := 0; parent := child s pid exec() Loads executable in memory & starts executing it code, stack, heap are overwritten the process is now running a different program!

The genius of fork() and exec() To redirect stdin/stdout: fork, close/open files, exec To switch users: fork, setuid, exec To start a process with a different current directory: fork, chdir, exec You get the idea! But what about overhead?

wait() and exit() wait() causes parent to wait until child terminates parent gets return value from child if no children alive, wait() return immediately exit() is called after program terminates closes open files deallocates memory dellaocates most OS structures checks if parent is alive. If so

In action /* See Figure 3.5 in textbook*/ #include <stdio.h> #include <unistd.h> int main() { int child_pid = fork(); if (child_pid == 0) { // child process printf("i am process #%d\n", getpid()); return 0; else { // parent process printf("i am the parent of process #%d\n", child_pid); return 0;

In action Process 13 Program A Process 13 Program A PC pid? pid = fork(); if (pid==0) exec(b); else wait(pid); PC pid 14 pid = fork(); if (pid==0) exec(b); else wait(pid); Process 14 Program A Process 14 Program B PC pid 0 pid = fork(); if (pid==0) exec(b); else wait(pid); PC pid 0 main() {

Shell Runs programs on behalf of the user Allows programmer to create/manage set of programs sh Original Unix shell (Bourne, 1977) csh bash BSD Unix C shell (tcsh enhances it) Bourne again shell Every command typed in the shell starts a child process of the shell