Virtual Machines & the OS Kernel

Similar documents
Virtual Machines & the OS Kernel

Virtual Machines & the OS Kernel

Operating System Kernel

Process Scheduling Queues

Operating system issues

18. Devices and Interrupts

17. Virtualizing the Processor

Hardware OS & OS- Application interface

Virtualizing the Processor Worksheet

What are Exceptions? EE 457 Unit 8. Exception Processing. Exception Examples 1. Exceptions What Happens When Things Go Wrong

Threads and Concurrency

The Kernel Abstraction

EE 457 Unit 8. Exceptions What Happens When Things Go Wrong

Anne Bracy CS 3410 Computer Science Cornell University

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

Operating Systems: Virtual Machines & Exceptions

Threads and Concurrency

Traps, Exceptions, System Calls, & Privileged Mode

Processes and Non-Preemptive Scheduling. Otto J. Anshus

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

Syscalls, exceptions, and interrupts, oh my!

Anne Bracy CS 3410 Computer Science Cornell University

The Kernel Abstraction. Chapter 2 OSPP Part I

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Hakim Weatherspoon CS 3410 Computer Science Cornell University

CSC227: Operating Systems Fall Chapter 1 INTERRUPTS. Dr. Soha S. Zaghloul

The Operating System (OS) MicroComputer Engineering OperatingSystem slide 1!

Exceptions and Interrupts

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

by I.-C. Lin, Dept. CS, NCTU. Textbook: Operating System Concepts 8ed CHAPTER 13: I/O SYSTEMS

Protection and System Calls. Otto J. Anshus

Review: Program Execution. Memory program code program data program stack containing procedure activation records

CSC 2405: Computer Systems II

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

Processes and Threads Implementation

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Architectural Support for Operating Systems

ELEC 377 Operating Systems. Week 1 Class 2

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Review: Program Execution. Memory program code program data program stack containing procedure activiation records

Traps, Exceptions, System Calls, & Privileged Mode

Interrupts Peter Rounce

Implementation of Processes

Unit 2 : Computer and Operating System Structure

THE CPU SPENDS ALMOST ALL of its time fetching instructions from memory

Learning Outcomes. Processes and Threads Implementation. Processes Process s user-level stack and execution state. Threads The Thread Model.

Operating System: Chap13 I/O Systems. National Tsing-Hua University 2016, Fall Semester

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

Processes and Threads Implementation

EE 109 Unit 17 - Exceptions

Interrupts Peter Rounce - room 6.18

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

CSCE Introduction to Computer Systems Spring 2019

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

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

Computer architecture. A simplified model

Examples of IO, Interrupts, and Exceptions

Sistemi in Tempo Reale

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

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.

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

CSCI-375 Operating Systems

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

Processes. Dr. Yingwu Zhu

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Module 11: I/O Systems

Operating System Control Structures

The control of I/O devices is a major concern for OS designers

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

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

Computer Systems II. First Two Major Computer System Evolution Steps

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

OPERATING SYSTEMS CS136

Introduction to Operating Systems. Chapter Chapter

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Operating Systems and Interrupts/Exceptions

Background: Operating Systems

CS370 Operating Systems

3.1 Introduction. Computers perform operations concurrently

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming


a) Do exercise (5th Edition Patterson & Hennessy). Note: Branches are calculated in the execution stage.

Machines and Virtualization. Systems and Networks Jeff Chase Spring 2006

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

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

Timers 1 / 46. Jiffies. Potent and Evil Magic

System Building. Events, Co-routines, Continuations and Threads - OS (and application) Execution Models. Event Model. Events

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

Virtual Memory Review. Page faults. Paging system summary (so far)

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

Chapter 5 Input/Output. I/O Devices

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

The Kernel Abstrac/on

ECE331: Hardware Organization and Design

Announcement. Exercise #2 will be out today. Due date is next Monday

CS 316: Procedure Calls/Pipelining

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Transcription:

Virtual Machines & the OS Kernel Not in the book! L24 Virtual Machines & the OS Kernel 1

Power of Contexts: Sharing a CPU Virtual Memory 1 Physical Memory Virtual Memory 2 Every application can be written as if it has access to all of memory, without considering where other applications reside. More than Virtual Memory A VIRTUAL MACHINE 1. TIMESHARING among several programs -- Separate context for each program OS loads appropriate context into pagemap when switching among pgms 2. Separate context for OS Kernel (eg, interrupt handlers)... Kernel vs User contexts Switch to Kernel context on interrupt; Switch back on interrupt return. What is this OS KERNEL thingy? L24 Virtual Machines & the OS Kernel 2

Building a Virtual Machine PROCESS #0 PROCESS #1 virtual memory Context #0 physical memory P0 P1 P0 P1 shared? P0 P1?? Context #1 virtual memory Goal: give each program its own VIRTUAL MACHINE ; programs don t know about each other Abstraction: create a PROCESS which has its own machine state: $1,, $31 program (w/ possibly shared code) context (pagemap) virtual I/O devices (console ) stack L24 Virtual Machines & the OS Kernel 3

Multiplexing the CPU 1 When this process is interrupted. We RETURN to this process! PROCESS 0 Operating System PROCESS 1 Virtual time 2 3 4 5 And, vice versa. Result: Both processes get executed, and no one is the wiser 1. Running in process #0 2. Stop execution of process #0 either because of explicit yield or some sort of timer interrupt; trap to handler code, saving current PC in $27 3. First: save process #0 state (regs, context) Then: load process #1 state (regs, context) 4. Return to process #1: just like a return from other trap handlers (ex. jr $27) but we re returning from a different trap than happened in step 2! 5. Running in process #1 L24 Virtual Machines & the OS Kernel 4

Stack-Based Interrupt Handling BASIC SEQUENCE: Program A is running when some EVENT happens. PROCESSOR STATE saved on stack (like a procedure CALL) The HANDLER program to be run is selected. HANDLER state (PC, etc) installed as new processor state. HANDLER runs to completion State of interrupted program A popped from stack and reinstalled, JMP returns control to A A continues, unaware of interruption. CHARACTERISTICS: TRANSPARENT to to interrupted program! Handler Handler runs runs to to completion before before returning Obeys Obeys stack stack discipline: handler handler can can "borrow" stack stack from from interrupted program (and (and return return it it unchanged) or or use use a special special handler handler stack. stack. <SP> old <SP> SAVED STATE OF A L24 Virtual Machines & the OS Kernel 5

minimips Interrupt Handling Minimal Implementation: Check for EVENTS before each instruction fetch. On EVENT j: save PC into $27, ($k1); INSTALL 0x80000000 + j*40 as new PC. Handler Coding: Save state in User structure Call C procedure to handle the exception re-install saved state from User Return to $27, ($k1) WHERE to find handlers? minimips Scheme: WIRE IN a high-memory address for each exception handler entry point Real MIPS alternative: WIRE IN the address of a TABLE of handler addresses ( interrupt vectors ) 0xffffffff: 0x80000080: 0x80000040: 0x80000000: 0x10000000: 0x00400000: Kernel stack Kernel Data Kernel Code IRQ Handler Syscall Handler Reset Handler bottom of USER stack Dynamic Data Static Data Program Code KERNEL Addresses USER Addresses L24 Virtual Machines & the OS Kernel 6

External (Asynchronous) Interrupts Example: System maintains current time of day (TOD) count at a well-known memory location that can be accessed by programs. But...this value must be updated periodically in response to clock EVENTs, i.e. signal triggered by 60 Hz clock hardware. Program A (Application) Executes instructions of the user program. Doesn t want to know about clock hardware, interrupts, etc!! Can incorporate TOD into results by examining well-known memory location. Clock Handler GUTS: Sequence of instructions that increments TOD. Written in C. Entry/Exit sequences save & restore interrupted state, call the C handler. Written as assembler stubs. L24 Virtual Machines & the OS Kernel 7

Interrupt Handler Coding long TimeOfDay; struct Mstate { int R1,R2,,R31 User; /* Executed 60 times/sec */ Clock_Handler(){ TimeOfDay = TimeOfDay + 1; Clock_h: lui $k0,(user>>16) # make $k0 point to ori $k0,$k0,user # User struct sw $1,0($k0) # Save registers of sw $2,4($k0) # interrupted... # application pgm... sw $31,124($k0) # program add $sp,$0,$0 # Use KERNEL stack jal Clock_Handler # call handler lw $1,0($k0) # Restore saved lw $2,4($k0) # registers... lw $31,124($k0) jr $k1 # Return to app. Handler (written in C) Interrupt stub (written in assy.) L24 Virtual Machines & the OS Kernel 8

Time-Sharing the CPU We can make a small modification to our clock handler implement time sharing. long TimeOfDay; struct Mstate { int R1,R2,,R31 User; Our clock handler calls /* Executed 60 times/sec */ another function Clock_Handler(){ TimeOfDay = TimeOfDay + 1; if (TimeOfDay % QUANTUM == 0) Scheduler(); A Quantum is that smallest time-interval that we allocate to a process, typically this might be 50 to 100 ms. (Actually, most OS Kernels vary this number based on the processes priority). L24 Virtual Machines & the OS Kernel 9

Simple Timesharing Scheduler long TimeOfDay; struct Mstate { int R1,R2,,R31 User;... (PCB = Process Control Block) struct PCB { struct MState State; /* Processor state */ Context PageMap; /* VM Map for proc */ int DPYNum; /* Console number */ ProcTbl[N]; /* one per process */ int Cur; /* Active process */ Scheduler() { ProcTbl[Cur].State = User; /* Save Cur state */ Cur = (Cur+1)%N; /* Incr mod N */ User = ProcTbl[Cur].State; /* Install for next User */ L24 Virtual Machines & the OS Kernel 10

Avoiding Re-Entrance Handlers which are interruptable are called RE-ENTRANT, and pose special problems... minimips, like many systems, disallows reentrant interrupts! Mechanism: Interrupts are disabled in Kernel Mode (PC >= 0x80000000): USER mode (Application) main() {......... Processor State K-Mode Flag: PC 31 = 1 for Kernel Mode! PC = 0... PC = 0... KERNEL mode (Op Sys) User (saved state) Kernel Stack Page Fault Handler Interrupt Vector Clock Handler PC = 1... PC = 1... SYSCALL Handlers That s where the rest of memory is! L24 Virtual Machines & the OS Kernel 11

Polled I/O CPU flag data INTERFACE DEVICE KEY HIT : Flag 1, Data ASCII Application code deals directly with I/O (eg, by busy-waiting): loop: lw $t0, flag($t1) # $t1 points to beq $t0,$0,loop # device structure lw $t0, data($t1) # process keystroke PROBLEMS: Wastes (physical) CPU while busy-waiting (FIX: Multiprocessing, codestripping, etc) Poor system modularity: running pgm MUST know about ALL devices. Uses up CPU cycles even when device is idle! L24 Virtual Machines & the OS Kernel 12

Interrupt-driven I/O OPERATION: NO attention to Keyboard during normal operation on key strike: hardware asserts IRQ to request interrupt USER program interrupted, PC+4 saved in $k1 state of USER program saved on KERNEL stack; KeyboardHandler (a device driver ) is invoked, runs to completion; state of USER program restored; program resumes. TRANSPARENT to USER program. That s how data gets into the buffer. How does it get out? Keyboard Interrupt Handler (in O.S. KERNEL): Each keyboard has an associated buffer struct Device { char flag, data; Keyboard; KeyboardHandler(struct Mstate *s) { Buffer[inptr] = Keyboard.data; inptr = (inptr + 1) % 100; L24 Virtual Machines & the OS Kernel 13

ReadKey SYSCALL: Attempt #1 A system call (syscall) is an instruction that transfers control to the kernel so it can satisfy some user request. Kernel returns to user program when request is complete. (Can be implemented as a synchronous interrupt, a.k.a. Illop) First draft of a ReadKey syscall handler: returns next keystroke to user Each process has an index to a keyboard ReadKEY_h() { int kbdnum = ProcTbl[Cur].DPYNum; while (BufferEmpty(kbdnum)) { /* busy wait loop */ User.R2 = ReadInputBuffer(kbdnum); Problem: Can t interrupt code code running in in the the supervisor mode so so the the buffer never gets gets filled. L24 Virtual Machines & the OS Kernel 14

ReadKey SYSCALL: Attempt #2 A keyboard SYSCALL handler (slightly modified, eg to support a Virtual Keyboard): ReadKEY_h() { int kbdnum = ProcTbl[Cur].DPYNum; if (BufferEmpty(kbdnum)) { User.R27 = User.R27-4; else User.R2 = ReadInputBuffer(kbdnum); That s a funny way to write a loop Problem: The process just wastes its time-slice waiting for some one to hit a key... Problem: The process just wastes its time-slice waiting for some one to hit a key... L24 Virtual Machines & the OS Kernel 15

ReadKey SYSCALL: Attempt #3 BETTER: On I/O wait, YIELD remainder of time slot (quantum): ReadKEY_h() { int kbdnum = ProcTbl[Cur].DPYNum; if (BufferEmpty(kbdnum)) { User.R27 = User.R27-4; Scheduler( ); else User.R2 = ReadInputBuffer(kbdnum); RESULT: Better CPU utilization!! FALLACY: Timesharing causes a CPUs to be less efficient L24 Virtual Machines & the OS Kernel 16

Sophisticated Scheduling To improve efficiency further, we can avoid scheduling processes in prolonged I/O wait: Processes can be in ACTIVE or WAITING ( sleeping ) states; Scheduler cycles among ACTIVE PROCESSES only; Active process moves to WAITING status when it tries to read a character and buffer is empty; Waiting processes each contain a code (eg, in PCB) designating what they are waiting for (eg, keyboard N); Device interrupts (eg, on keyboard N) move any processes waiting on that device to ACTIVE state. UNIX kernel utilities: sleep(reason) - Puts CurProc to sleep. Reason is an arbitrary binary value giving a condition for reactivation. wakeup(reason) - Makes active any process in sleep(reason). L24 Virtual Machines & the OS Kernel 17

ReadKey SYSCALL: Attempt #4 ReadKEY_h() { if (BufferEmpty(kbdnum)) { User.R27 = User.R27-4; sleep(kbdnum); SYSCALL from application KEYhit_h() { WriteBuffer(kbdnum, key) wakeup(kbdnum); sleep(status s) { ProcTbl[Cur].status = s; Scheduler() wakeup(status s) { for (i = 0; i < N; i += 1) { if (ProcTbl[i].status == s) PCB[i].status = 0; Scheduler() { while (ProcTbl[i].status!= 0) { i = (i+1)%n; INTERRUPT from Keyboard n L24 Virtual Machines & the OS Kernel 18

A Typical OS layer cake Applications Word Processors Security Hardware PCs Shared Libraries Operating System Device Drivers Scheduler Registers ALUs Spread Sheets Device Queues Kernel Page Tables Caches Games Graphical User Interface (GUI) Network Interfaces Process Control Blocks (PCBs) Web Browser File system An OS is the Glue that holds a computer together. - Mediates between competing requests - Resolves names/bindings - Maintains order/fairness KERNEL - a RESIDENT portion of the O/S that handles the most common and fundamental service requests. L24 Virtual Machines & the OS Kernel 19

A Thin Slice of OS organization loop: addi $v0,$0,0 syscall... addi $v0,$0,1 syscall... beq $0,$0,loop P1 P2 syscall 0 handler syscall 1 handler KERNEL I/O Handler I/O Handler loop: addi $v0,$0,0 syscall... addi $v0,$0,1 syscall... beq $0,$0,loop PCBs: P1: P2: DPYNum=0 DPYNum=1 Scheduler Applications are quasi-parallel PROCESSES on VIRTUAL MACHINES, each with: - CONTEXT (virtual address space) - Virtual I/O devices O.S. KERNEL has: - Interrupt handlers - SYSCALL (trap) handlers - Scheduler - PCB structures containing the state of inactive processes Device 0 Device 1 Alarm Clock L24 Virtual Machines & the OS Kernel 20