CSE 153 Design of Operating Systems

Similar documents
CSE 153 Design of Operating Systems Fall 18

CSE 120 Principles of Operating Systems

CS 153 Design of Operating Systems

CSE 120 Principles of Operating Systems

Lecture 2: Architectural Support for OSes

Operating Systems ECE344

Traps and Faults. Review: Mode and Space

Hardware OS & OS- Application interface

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

Architectural Support for OS

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Architectural Support for Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

for Operating Systems Computer Systems Laboratory Sungkyunkwan University

Anne Bracy CS 3410 Computer Science Cornell University

Protection and System Calls. Otto J. Anshus

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

CS 5460/6460 Operating Systems

Architectural Support for Operating Systems

Machines and Virtualization. Systems and Networks Jeff Chase Spring 2006

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Memory Protection. Machines and Virtualization. Architectural Foundations of OS Kernels. Memory and the CPU. Introduction to Virtual Addressing

CS 550 Operating Systems Spring Interrupt

Lec 22: Interrupts. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

CSE 153 Design of Operating Systems

Architectural Support for Operating Systems. Jinkyu Jeong ( Computer Systems Laboratory Sungkyunkwan University

Mechanisms for entering the system

Processes (Intro) Yannis Smaragdakis, U. Athens

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Interrupts & System Calls

CPS104 Computer Organization and Programming Lecture 17: Interrupts and Exceptions. Interrupts Exceptions and Traps. Visualizing an Interrupt

CS 537 Lecture 2 Computer Architecture and Operating Systems. OS Tasks

CS5460: Operating Systems

CSC369 Lecture 2. Larry Zhang, September 21, 2015

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Interrupts and System Calls

Process Scheduling Queues

Syscalls, exceptions, and interrupts, oh my!

Anne Bracy CS 3410 Computer Science Cornell University

Exceptions and Processes

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

3. Process Management in xv6

CSC369 Lecture 2. Larry Zhang

CS 201. Exceptions and Processes. Gerson Robboy Portland State University

The Kernel Abstraction

Architecture and OS. To do. q Architecture impact on OS q OS impact on architecture q Next time: OS components and structure

Windows Interrupts

4. The Abstraction: The Process

Traps, Exceptions, System Calls, & Privileged Mode

Traps, Exceptions, System Calls, & Privileged Mode

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

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

TCSS 422: OPERATING SYSTEMS

COS 318: Operating Systems

CS 537 Lecture 2 - Processes

Design of Operating System

Processes. Johan Montelius KTH

Lecture 4: Memory Management & The Programming Interface

Architectural Support for Operating Systems

Processes. Dr. Yingwu Zhu

A process. the stack

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Even coarse architectural trends impact tremendously the design of systems

Systems Programming and Computer Architecture ( ) Timothy Roscoe

The Kernel Abstraction. Chapter 2 OSPP Part I

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

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

Even coarse architectural trends impact tremendously the design of systems

CS 326: Operating Systems. Process Execution. Lecture 5

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

The Kernel Abstrac/on

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

CS 318 Principles of Operating Systems

An Operating System in Action

EE458 - Embedded Systems Exceptions and Interrupts

OS Structure. Hardware protection & privilege levels Control transfer to and from the operating system

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Page 1, 5/4/99 8:22 PM

Architectural Support for Operating Systems

Lecture 11: Interrupt and Exception. James C. Hoe Department of ECE Carnegie Mellon University

Even coarse architectural trends impact tremendously the design of systems. Even coarse architectural trends impact tremendously the design of systems

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

W4118: interrupt and system call. Junfeng Yang

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

CS 104 Computer Organization and Design

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

Interrupts and System Calls

ECE 650 Systems Programming & Engineering. Spring 2018

Interrupts and System Calls. Don Porter CSE 506

Handling of Interrupts & Exceptions

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. P&H Chapter 4.9, pages , appendix A.

Timers 1 / 46. Jiffies. Potent and Evil Magic

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

x86 architecture et similia

CSE 120 Principles of Operating Systems Spring 2017

Operating Systems Engineering Recitation #3 (part 2): Interrupt and Exception Handling on the x86. (heavily) based on MIT 6.

OS: An Overview. ICS332 Operating Systems

Instruction Set Architecture of MIPS Processor

Transcription:

CSE 153 Design of Operating Systems Winter 19 Lecture 3: OS model and Architectural Support

Last time/today Historic evolution of Operating Systems (and computing!) Today: We start our journey in exploring Operating Systems Try to answer questions such as:» What is the OS?» What does it need to do?» How/When does the OS run?» How do programs interact with it?» How is this supported by CPUs? 2

Some questions to get you thinking What is the OS? Software? Is the OS always executing? If not, how do we make sure it gets to run? How do we prevent user programs from directly manipulating hardware? 3

Sleeping Beauty Model Answer: Sleeping beauty model Technically known as controlled direct execution OS runs in response to events ; we support the switch in hardware Only the OS can manipulate hardware or critical system state Most of the time the OS is sleeping Good! Less overhead Good! Applications are running directly on the hardware 4

What do we need from the architecture/cpu? Manipulating privileged machine state Protected instructions Manipulate device registers, TLB entries, etc. Controlling access Generating and handling events Interrupts, exceptions, system calls, etc. Respond to external events CPU requires software intervention to handle fault or trap Other stuff Mechanisms to handle concurrency, Isolation, virtualization 5

Types of Arch Support Manipulating privileged machine state Protected instructions Manipulate device registers, TLB entries, etc. Controlling access Generating and handling events Interrupts, exceptions, system calls, etc. Respond to external events CPU requires software intervention to handle fault or trap Other stuff Interrupts, atomic instructions, isolation 6

Protected Instructions OS must have exclusive access to hardware and critical data structures Only the operating system can Directly access I/O devices (disks, printers, etc.)» Security, fairness (why?) Manipulate memory management state» Page table pointers, page protection, TLB management, etc. Manipulate protected control registers» Kernel mode, interrupt level Halt instruction (why?) 7

Privilege mode Hardware restricts privileged instructions to OS Q: How does the HW know if the executed program is OS? HW must support (at least) two execution modes: OS (kernel) mode and user mode Mode kept in a status bit in a protected control register User programs execute in user mode OS executes in kernel mode (OS == kernel ) CPU checks mode bit when protected instruction executes Attempts to execute in user mode trap to OS 8

Switching back and forth Going from higher privilege to lower privilege Easy: can directly modify the mode register to drop privilege But how do we escalate privilege? Special instructions to change mode» System calls (int 0x80, syscall, svc)» Saves context and invokes designated handler You jump to the privileged code; you cannot execute your own» OS checks your syscall request and honors it only if safe Or, some kind of event happens in the system 9

Types of Arch Support Manipulating privileged machine state Protected instructions Manipulate device registers, TLB entries, etc. Controlling access Generating and handling events Interrupts, exceptions, system calls, etc. Respond to external events CPU requires software intervention to handle fault or trap Other stuff 10

Review: Computer Organization Branch Address +4 Select PC New PC Program Counter CPU Instructions Fetch Exec opcode 11

Events An event is an unnatural change in control flow Events immediately stop current execution Changes mode, context (machine state), or both The kernel defines a handler for each event type Event handlers always execute in kernel mode The specific types of events are defined by the machine Once the system is booted, OS is one big event handler all entry to the kernel occurs as the result of an event 12

Handling events Interrupt vector table Processor Register Interrupt Vector...... handletimerinterrupt() {... } handledividebyzero() {... } handlesystemcall() {... } 13

Categorizing Events Two kinds of events: synchronous and asynchronous Sync events are caused by executing instructions Example? Async events are caused by an external event Example? Asynchronous events CPU ticks Synchronous events 14

Categorizing Events Two kinds of events: synchronous and asynchronous Sync events are caused by executing instructions Async events are caused by an external event Two reasons for events: unexpected and deliberate Unexpected events are, well, unexpected Example? Deliberate events are scheduled by OS or application Why would this be useful? 15

Categorizing Events This gives us a convenient table: Unexpected Deliberate Synchronous fault syscall trap Asynchronous interrupt signal Terms may be slightly different by OS and architecture E.g., POSIX signals, asynch system traps, async or deferred procedure calls 16

Faults Hardware detects and reports exceptional conditions Page fault, memory access violation (unaligned, permission, not mapped, bounds ), illegal instruction, divide by zero Upon exception, hardware faults (verb) Must save state (PC, regs, mode, etc.) so that the faulting process can be restarted Invokes registered handler 17

Handling Faults Some faults are handled by fixing the exceptional condition and returning to the faulting context Page faults cause the OS to place the missing page into memory Fault handler resets PC of faulting context to re-execute instruction that caused the page fault 18

Handling Faults The kernel may handle unrecoverable faults by killing the user process Program fault with no registered handler Halt process, write process state to file, destroy process In Unix, the default action for many signals (e.g., SIGSEGV) What about faults in the kernel? Dereference NULL, divide by zero, undefined instruction These faults considered fatal, operating system crashes Unix panic, Windows Blue screen of death» Kernel is halted, state dumped to a core file, machine locked up 19

Categorizing Events Unexpected Deliberate Synchronous fault syscall trap Asynchronous interrupt signal 20

System Calls For a user program to do something privileged (e.g., I/O) it must call an OS procedure Known as crossing the protection boundary, or a protected procedure call Hardware provides a system call instruction that: Causes an exception, which invokes a kernel handler» Passes a parameter determining the system routine to call Saves caller state (PC, regs, mode) so it can be restored» Why save mode? Returning from system call restores this state 21

System Call emacs: read() User mode Kernel mode Trap to kernel mode, save state Trap handler Find read handler Restore state, return to user level, resume execution read() kernel routine 22

System Call Questions What if the kernel executes a system call? How to reference kernel objects as arguments or results to/from system calls? A naming issue Use integer object handles or descriptors» E.g., Unix file descriptors» Only meaningful as parameters to other system calls Also called capabilities (more later when we do protection) Why not use kernel addresses to name kernel objects? 23

CPU Modes/Privileges System call Ring 3 à Ring 0 24

Another view 0xFFFFFFFF 1G 0xC0000000 Kernel Stack Kernel Code SP2 PC2 Address Space 3G User Stack SP1 0x00000000 User Code PC1 25

System Call Questions There are hundreds of syscalls. How do we let the kernel know which one we intend to invoke? Before issuing int $0x80 or sysenter, set %eax/%rax with the syscall number System calls are like function calls, but how to pass parameters? Just like calling convention in syscalls, typically passed through %ebx, %ecx, %edx, %esi, %edi, %ebp 26

More questions How to reference kernel objects (e.g., files, sockets)? Naming problem an integer mapped to a unique object» int fd = open( file ); read(fd, buffer); Why can t we reference the kernel objects by memory address? 27

System calls in xv6 Look at trap.h and trap.c Interrupt handlers are initialized in two arrays (idt and vectors)» Tvinit() function does the initialization Syscalls have a single trap handler (T_SYSCALL, 64) Trap() handles all exceptions, including system calls» If the exception is a system call, it calls syscall() Keep digging from there to understand how system calls are supported You will be adding a new system call in Lab 1 28

Categorizing Events Unexpected Deliberate Synchronous fault syscall trap Asynchronous interrupt software interrupt Interrupts signal asynchronous events I/O hardware interrupts Software and hardware timers 29

Flavors of interrupts (cont d) Two flavors of interrupts Precise: CPU transfers control only on instruction boundaries Imprecise: CPU transfers control in the middle of instruction execution» What does that mean? OS designers like precise interrupts, CPU designers like imprecise interrupts» Why? From OS perspective Maskable interrupt (IRQ)» Why does the OS want to mask an interrupt? Non maskable interrupt (NMI)» Highest priority tasks 30

Timer The key to a timesharing OS The fallback mechanism by which the OS reclaims control Timer is set to generate an interrupt after a period of time» Setting timer is a privileged instruction» When timer expires, generates an interrupt Handled by the OS, forcing a switch from the user program» Basis for OS scheduler (more later ) Also used for time-based functions (e.g., sleep()) 31

I/O Control I/O issues Initiating an I/O Completing an I/O Initiating an I/O Special instructions Memory-mapped I/O» Device registers mapped into address space» Writing to address sends data to I/O device 32

I/O using Interrupts Interrupts are the basis for asynchronous I/O OS initiates I/O Device operates independently of rest of machine Device sends an interrupt signal to CPU when done OS maintains a vector table containing a list of addresses of kernel routines to handle various events CPU looks up kernel address indexed by interrupt number, context switches to routine 33

I/O Example 1. Ethernet receives packet, writes packet into memory 2. Ethernet signals an interrupt 3. CPU stops current operation, switches to kernel mode, saves machine state (PC, mode, etc.) on kernel stack 4. CPU reads address from vector table indexed by interrupt number, branches to address (Ethernet device driver) 5. Ethernet device driver processes packet (reads device registers to find packet in memory) 6. Upon completion, restores saved state from stack 34

Interrupt Questions Interrupts halt the execution of a process and transfer control (execution) to the operating system Can the OS be interrupted? (Consider why there might be different interrupt levels) Interrupts are used by devices to have the OS do stuff What is an alternative approach to using interrupts? What are the drawbacks of that approach? 35

Synchronization Interrupts cause difficult problems An interrupt can occur at any time A handler can execute that interferes with code that was interrupted OS must be able to synchronize concurrent execution Need to guarantee that short instruction sequences execute atomically Disable interrupts turn off interrupts before sequence, execute sequence, turn interrupts back on Special atomic instructions read/modify/write a memory address, test and conditionally set a bit based upon previous value» XCHG on x86 36

Memory Isolation OS must be able to protect programs from each other OS must protect itself from user programs OS may or may not protect user programs from itself Memory management unit (MMU) Hardware unit provides memory protection mechanisms Virtual memory Segmentation Manipulating memory management hardware uses protected (privileged) operations 37

Example memory protection Physical Memory INSTR DATA HEAP STACK Base Bounds CPU Memory Reference OK? Yes Continue No Exception 38

Summary Protection User/kernel modes Protected instructions System calls Used by user-level processes to access OS functions Access what is in the OS Exceptions Unexpected event during execution (e.g., divide by zero) Interrupts Timer, I/O 39

Next Time Processes Project: Continue to get familiar with the environment» In particular, Chapter 0 Read the system call/interrupt chapter in the xv6 book (Chapter 3) If you have time, work through at least some of the booting sequence tutorial» Read appendix A and B in xv6 book Ask questions on Piazza 40