Architectural Support For Operating Systems
Architectural Support For Operating Systems
Architectural Support For Operating Systems
Operating Systems
Announcements
• Most office hours are finalized
• Assignments (if any) up every Tuesday
– Due one week later
– Main TA to contact about assignments: Nazrul Alam
• Also contact Levent Kartaltepe
• OS structure
Synchronizes
memory access
I/O operations
• I/O devices and the CPU can execute concurrently.
• I/O is moving data between device & controller’s buffer
– CPU moves data between controller’s buffer & main memory
• Each device controller is in charge of certain device type.
– May be more than one device per controller
• SCSI can manage up to 7 devices
– Each device controller has local buffer, special registers
• A device driver for every device controller
– Knows details of the controller
– Presents a uniform interface to the rest of OS
Accessing I/O Devices
• Memory Mapped I/O
– I/O devices appear as regular memory to CPU
– Regular loads/stores used for accessing device
– This is more commonly used
• Programmed I/O
– Also called “channel” I/O
– CPU has separate bus for I/O devices
– Special instructions are required
• Which is better?
Polling I/O
• Each device controller typically has:
– Data-in register (for host to receive input from device)
– Data-out (for host to send output to device)
– Status register (read by host to determine device
state)
– Control register (written by host to invoke command)
Polling I/O handshaking:
• To write data to a device:
1. Host repeatedly reads busy bit in status register until clear
2. Host sets write bit in command register and writes output in data-out
register
3. Host sets command-ready bit in control register
4. Controller notices command-ready bit, sets busy bit in status register
5. Controller reads command register, notices write bit: reads data-out
register and performs I/O (magic)
6. Controller clears command-ready bit, clears the error bit (to indicate
success) and clears the busy bit (to indicate it’s finished)
Polling I/O
• What’s the problem?
– CPU could spend most its time polling devices, while
other jobs go undone.
• But devices can’t be left to their own devices for too long
– Limited buffer at device - could overflow if doesn’t get
CPU service.
• Modern operating systems use Interrupts to solve this
dilemma.
CPU
Interrupt Priority
Clock
Data
Addr
R/W
Interrupt
Memory
Device
Interrupts
• CPU hardware has a interrupt-request line (a wire) it
checks after processing each instruction.
• On receiving signal save processing state
– Save current context
– Jump to interrupt handler routine at fixed address in memory
– Interrupt handler:
• Determine cause of interrupt.
• Do required processing.
• Restore state.
• Execute return from interrupt instruction.
• Device controller raises interrupt, CPU catches it,
interrupt handler dispatches and clears it.
cache
execution Memory
Data movement
CPU (*N)
Instructions
and
I/O Request
Data
Interrupt
Data
Keyboard
Disk Device
Device
Driver
Driver Perform I/O
and
Keyboard
Disk Controller
Controller
Read Data
Interrupt Timeline
Modern interrupt handling
• Modern OS needs more sophisticated mechanism:
– Ability to defer interrupt
– Efficient way to dispatch interrupt handler
– Multilevel interrupts to distinguish between high and low priority
• Higher priority interrupts can pre-empt processing of lower priority ones
– Use a specialized Interrupt Controller
• CPUs typically have two interrupt request lines:
– Maskable interrupts: Can be turned off before critical processing
– Nonmaskable interrupts: cannot be turned off
• Signifies serious error (e.g. unrecoverable memory error)
• Interrupts contain address pointing to interrupt vector
– Interrupt vector contains addresses of interrupt handlers.
– If more devices than elements in interrupt vector, then chain:
• A list of interrupt handlers for a given address which must be traversed to determine the
appropriate one.
Traps and Exceptions
• Software generated interrupt
– Exception: user program acts silly
• Caused by an error (div by 0, or memory access violation)
• Just a performance optimization
– Trap: user program requires OS service
• Caused by system calls
• Handled similar to hardware interrupts:
– Stops executing the process
– Calls handler subroutine
– Restores state after servicing the trap
Goals for Today and
Next Lecture
• I/O subsystem and device drivers
• OS structure
Needed for:
• Abstraction/ease of use and protection
Dual-Mode Operation
• Allows OS to protect itself and other system components
– User mode and kernel mode
• OS runs in kernel mode, user programs in user mode
– OS is supreme being, the applications are peasants
– Privileged instructions only executable in kernel mode
• Mode bit provided by hardware
– Can distinguish if system is running user code or kernel code
– System call changes mode to kernel
– Return from call using RTI resets it to user
• How do user programs do something privileged?
Crossing Protection Boundaries
• User calls OS procedure for “privileged” operations
• Calling a kernel mode service from user mode program:
– Using System Calls
– System Calls switches execution to kernel mode
User Mode
Mode bit = 1 Resume process
User process System Call
Standard API
Reducing System Call Overhead
• Problem: The user-kernel mode distinction poses a
performance barrier
• Crossing this hardware barrier is costly.
• System calls take 10x-1000x more time than a procedure call
• Goal:
– Keep User Programs from Crashing OS
– Keep User Programs from Crashing each other
– [Keep Parts of OS from crashing other parts?]
• (Some of the required) Mechanisms:
– Dual Mode Operation
– Address Translation (base/limit registers, page tables, etc)
– Privileged instructions (set timer, I/O, etc)
• Simple Policy:
– Programs are not allowed to read/write memory of other
Programs or of Operating System
Summary: Dual Mode Operation
• Hardware provides at least two modes:
– “Kernel” mode (or “supervisor” or “protected”)
– “User” mode: Normal programs executed
• Some instructions/ops prohibited in user mode:
– Example: cannot modify page tables in user mode
• Attempt to modify Exception generated
• Transitions from user mode to kernel mode:
– System Calls, Interrupts, Other exceptions
Today’s Lectures
• I/O subsystem and device drivers
• OS structure
Exception
Idle
Loop
Operating System Modules
RTI
Operating System Structure
• Simple Structure: MS-DOS
– Written to provide the most functionality in the least space
– Applications have direct
control of hardware
• Disadvantages:
– Not modular
– Inefficient
– Low protection or security
General OS Structure
App App App
API
Security Service
Module Module
Monolithic Structure
Layered Structure
• OS divided into number of layers
– bottom layer (layer 0), is the hardware
– highest (layer N) is the user interface
– each uses functions and services of only lower-level layers
• Advantages:
– Simplicity of construction
– Ease of debugging
– Extensible
• Disadvantages:
– Defining the layers
– Each layer adds overhead
Layered Structure
App App App
API
Process Network Object
File Memory
Manager Support Support
Systems Manager
Process Security
Manager Module
File
Systems Network
Support