Virtual Machines & the OS Kernel

Similar documents
Virtual Machines & the OS Kernel

Virtual Machines & the OS Kernel

Operating System Kernel

18. Devices and Interrupts

Operating system issues

Process Scheduling Queues

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

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

Anne Bracy CS 3410 Computer Science Cornell University

Threads and Concurrency

Traps, Exceptions, System Calls, & Privileged Mode

Operating Systems: Virtual Machines & Exceptions

Syscalls, exceptions, and interrupts, oh my!

The Kernel Abstraction

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

Anne Bracy CS 3410 Computer Science Cornell University

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

The Kernel Abstraction. Chapter 2 OSPP Part I

Threads and Concurrency

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Exceptions and Interrupts

CSC 2405: Computer Systems II

Processes and Non-Preemptive Scheduling. Otto J. Anshus

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Protection and System Calls. Otto J. Anshus

Traps, Exceptions, System Calls, & Privileged Mode

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

EE 109 Unit 17 - Exceptions

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

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

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

Examples of IO, Interrupts, and Exceptions

ELEC 377 Operating Systems. Week 1 Class 2

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

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

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

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

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

Architectural Support for Operating Systems

Computer architecture. A simplified model

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

Background: Operating Systems

CSCI-375 Operating Systems

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

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

CSCE Introduction to Computer Systems Spring 2019

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

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

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

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

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

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

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

Sistemi in Tempo Reale

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

Processes and Threads Implementation

Operating Systems and Interrupts/Exceptions

Introduction to Operating Systems. Chapter Chapter

OPERATING SYSTEMS CS136

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

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

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

Unit 2 : Computer and Operating System Structure

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

Implement a C language function that computes the factorial of a number, recursively, according to the following prototype:

Machines and Virtualization. Systems and Networks Jeff Chase Spring 2006

ECE 30 Introduction to Computer Engineering


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

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

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

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

The MIPS Processor Datapath

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

Processes and Threads Implementation

Architectural Support for Operating Systems

Implementation of Processes

Operating Systems. System calls. Guillaume Salagnac. Fall Insa-Lyon IST Semester

Computer Structure. Unit 4. Processor

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

CS370 Operating Systems

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Initial Representation Finite State Diagram. Logic Representation Logic Equations

Lecture 5: Procedure Calls

Microprogramming. Microprogramming

Processes. Dr. Yingwu Zhu

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

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

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

ECE 250 / CPS 250 Computer Architecture. Processor Design Datapath and Control

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Computer Architecture. The Language of the Machine

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. 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. More than Virtual Memory A VIRTUAL MACHINE 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 2 3 4 5 Virtual time 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 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 re-installed, 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. 0xffffffff: Kernel stack 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) 0x80000080: 0x80000040: 0x80000000: Kernel Data Kernel Code IRQ Handler Syscall Handler Reset Handler bottom of USER stack KERNEL Addresses USER Addresses WHERE to find handlers? minimips Scheme: WIRE IN a high-memory address for each exception handler entry point 0x10000000: 0x00400000: Dynamic Data Static Data Program Code Real MIPS alternative: WIRE IN the address of a TABLE of handler addresses ( interrupt vectors ) 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; if (TimeOfDay % QUANTUM == 0) Scheduler(); Handler (written in C) Clock_h: lui $k0,(user>>16) # $k0 points to ori $k0,$k0,user # User structure 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. Interrupt stub (written in assy.) L24 Virtual Machines & the OS Kernel 8

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 9

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 PC = 0... 0... KERNEL mode (Op Sys) User (saved state) Kernel Stack Page Fault Handler Interrupt Vector Clock Handler PC PC = 1... 1... SYSCALL Handlers That s where the rest of memory is! L24 Virtual Machines & the OS Kernel 10

Polled I/O CPU INTERFACE flag data 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 11

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 invoked, runs to completion; state of USER program restored; program resumes. TRANSPARENT to USER program. Keyboard Interrupt Handler (in O.S. KERNEL): That s how data gets into the buffer. How does it get out? 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 12

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 13

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 The process just just wastes its its time-slice waiting for for some one one to to hit hit a key... key... L24 Virtual Machines & the OS Kernel 14

ReadKey SYSCALL: Attempt #3 BETTER: On I/O wait, YIELD remainder of 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 15

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 16

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); INTERRUPT from Keyboard n 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; L24 Virtual Machines & the OS Kernel 17

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 Graphical User Interface (GUI) Caches Games 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 18

A Thin Slice of OS organization loop: addi $v0,$0,0 syscall... addi $v0,$0,1 syscall... beq $0,$0,loop P1 syscall 0 handler syscall 1 handler KERNEL I/O Handler P2 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 19