Lecture 2 - University of Massachusetts Amherst

Download Report

Transcript Lecture 2 - University of Massachusetts Amherst

Operating Systems
CMPSCI 377
Lecture 2: OS & Architecture
Emery Berger
University of Massachusetts, Amherst
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
Last Class: Introduction

Operating system =
interface between
user & architecture
User-level Applications
virtual machine interface
Operating System
physical machine interface
Hardware
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
2
Today:
OS & Computer Architecture

Modern OS Functionality


Goals of Operating Systems
Hardware Support for OS Features

Architecture Basics & Details
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
3
Modern OS Functionality

Provides support for:





Concurrency
I/O Device Management
Memory Management
Files
Distributed Systems & Networks
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
4
Concurrency

Supports multiple activities, apparently
occurring simultaneously

Multiple users:


Several users work as if each has private machine
Multiple processes/threads:


One on CPU at a time
Multiple active concurrently
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
5
Modern OS Functionality, Cont.

I/O


Memory Management



Coordinates allocation of RAM
Moves data between disk & main memory
Files


CPU works while waiting on slow I/O devices
Coordinates how disk space is used
Distributed Systems & Networks

Lets group of PC’s to work together on distributed h/w
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
6
Operating Systems = Governments

Goals surprisingly like utopian political
systems




Libertarianism
Socialism
Communism
OS researchers = benevolent dictators
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
7
Libertarians


Infinite RAM,
CPU
Protects users
from each other
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
8
OS as Utopian Governments

Socialism:



Safe & secure
communication
Protects everyone
Fair allocation of
resources
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
9
Communism



To each according to his
needs
Centralized control
Allocates resources
efficiently (in
theory…)
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
10
Theory vs. Practice

Goals





Efficient & fair resource allocation
Safe & secure communication
Protect everyone from each other
(isolation) & from “govt.” (OS)
Illusion: infinite RAM, CPU
But how can we do this efficiently?

Hardware support
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
11
Canonical System Hardware




CPU: Processor to perform
actual computations
I/O devices: terminal,
disks, video, printer…
Memory: data & programs
System Bus:
Communication channel
between above
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
12
Services & Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
13
Protection

OS protects users from each other



Users cannot read or write other user’s memory
Name OS’s that do and don’t do this!
Protects self from users



Safe from errant or malicious users
Privileged instructions (e.g., halt machine)
Code & data protected
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
14
Kernel Mode:
Privileged Instructions

CPU provides “kernel” mode restricted to OS



Privileged instructions & registers:





Inaccessible to ordinary users
Kernel = core of operating system
Direct access to I/O
Modify page table pointers, TLB
Enable & disable interrupts
Halt the machine, etc.
Indicated by status bit in protected CPU register
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
15
Protecting Memory:
Base and Limit Registers

Hardware support to protect
memory regions


CPU checks each reference


Loaded by OS before starting
program
Instruction & data addresses
Ensures reference in range
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
16
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
17
Interrupts

Polling = “are we there yet?” “no!”
(repeat…)



Inefficient use of resources
Annoys the CPU
Interrupt = silence, then: “we’re there”



I/O device has own processor
When finished, device sends interrupt on bus
CPU “handles” interrupt
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
18
CPU Interrupt Handling


Handling interrupts: relatively expensive
CPU must:

Save hardware state






Registers, program counter
Disable interrupts (why?)
Invoke via in-memory interrupt vector (like trap vector, soon)
Enable interrupts
Restore hardware state
Continue execution of interrupted process
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
19
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
20
Traps


Special conditions detected by architecture
 E.g.: page fault, write to read-only page, overflow,
system call
On detecting trap, hardware must:
 Save process state (PC, stack, etc.)
 Transfer control to trap handler (in OS)



CPU indexes trap vector by trap number
Jumps to address
Restore process state and resume
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
21
Memory Traps


Special case: trigger trap on write to protected
memory area
Widely used in operating systems:






Debugging
Distributed virtual memory
Approximating LRU
Garbage collection
Copy-on-write
Pay performance penalty only when needed
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
22
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
23
Memory-Mapped I/O


Direct access to I/O controller through memory
Reserve area of memory for communication
with device (“DMA”)

Video RAM:



CPU writes frame buffer
Video card displays it
Fast and convenient
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
24
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
25
Synchronization

How can OS synchronize concurrent
processes?


E.g., multiple threads, processes & interrupts,
DMA
CPU must provide mechanism for atomicity

Series of instructions that execute as one or not
at all
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
26
Synchronization: How-To

One approach:




Advantages:



Disable interrupts
Perform action
Enable interrupts
Requires no hardware support
Conceptually simple
Disadvantages:

Could cause starvation
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
27
Synchronization: How-To, II

Modern approach: atomic instructions


Small set of instructions that cannot be interrupted
Examples:




Test-and-set (“TST”)
if word contains given value, set to new value
Compare-and-swap (“CAS”)
if word equals value, swap old value with new
Intel: LOCK prefix (XCHG, ADD, DEC, etc.)
Used to implement locks
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
28
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
29
Virtual Memory

Provides illusion of complete access to RAM


OS loads pages from disk as needed


All addresses translated from physical addresses into
virtual addresses
Keeps track of which pages are in memory (“in core”)
and which are on disk
Many benefits, including:

Allows users to run programs without loading entire
program into RAM

May not fit in entirety (think MS Office)
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
30
Translation Lookaside Buffer

First virtual memory systems performed address
translation in software


Modern CPUs contain hardware to do this: the TLB




On every memory access! (s..l..o..w..)
Hash-based scheme
Maps virtual addresses to physical addresses
Fast, fully-associative cache
Today’s workloads are often “TLB-miss dominated”
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
31
Hardware Support
OS Service
Hardware Support
Protection
Kernel/User Mode
Protected Instructions
Base & Limit Registers
Interrupts
Interrupt Vectors
System Calls
Trap Instructions
I/O
Interrupts, Memory-Mapping
Synchronization
Atomic Instructions
Virtual Memory
Translation Lookaside Buffers
Scheduling
Timer
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
32
Scheduling & Timers

OS needs timers for:


Time of day
CPU scheduling



Fairness: limited quantum (e.g., 100ms) for each task
When quantum expires, switch processes
Uses interrupt vector
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
33
Summary

OS relies on hardware for many services







Protection
Interrupts
System Calls
Synchronization
Virtual memory
Timers
Otherwise impossible or impractically slow in
software
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
34