Professional Documents
Culture Documents
Lecture24 PDF
Lecture24 PDF
Exceptions
Input/Output
Lecture 24: 1
Announcements
• HW8 due tomorrow
• Lab 6 on Monday/Tuesday
– Please return your FPGA boards
Lecture 24: 2
Example: Page Fault
• Given the following virtual address stream (in decimal),
identify the potential page faults
– 256 (VPN=0), 2049 (VPN=0), 4096 (VPN=1), 1024 (VPN=0)
Lecture 24: 3
Virtual Memory Write Policy
• Disk writes can take millions of cycles
Lecture 24: 4
Faster Address Translation
• Have to access the page table before an
instruction can be fetched and before data
cache/memory can be accessed
Lecture 24: 5
Translation Lookaside Buffer (TLB)
• Small cache of recently accessed PTE (typically 16-512
entries, fully associative)
Tag: virtual page Data: physical
number page number
accessed only
on a TLB miss
Lecture 24: 6
TLB Miss Scenarios
• TLB miss, page table hit
– Bring in the PTE information from page table to TLB
– Retry the access
– Usually completely performed by hardware
Lecture 24: 7
Exceptions and Interrupts
• Useful methods for signaling the CPU that some event
has occurred that requires action
– In response, the CPU may suspend the running program in order
to handle the exception/interrupt
Lecture 24: 8
Why are Exceptions Useful?
• Allow user programs to get service from the OS
– A system call creates an exception that kicks out the
user program and transfers control to exception
handler, which will invoke a proper OS service routine
Lecture 24: 9
Pipeline with Exception Handling
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 10
Pipeline with Exception Handling
• An Exception signal is sent to the CU
Lecture 24: 11
Enabling Program Restart
• After the exception is handled, want to restart
the program starting at the faulting instruction
Lecture 24: 12
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 13
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 14
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 15
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
[SUB R5,R5,R7]
<page fault>
Lecture 24: 16
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
[SUB R5,R5,R7]
<page fault>
Lecture 24: 17
Instruction Page Fault PC of SUB “page fault”
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
[SUB R5,R5,R7]
<page fault>
Lecture 24: 18
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
[SUB R5,R5,R7]
<page fault>
Lecture 24: 19
Instruction Page Fault
exception Control
CU Signals Exception Exception
PC Cause
=?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
1st instruction in
exception handler
Lecture 24: 20
Handling a Page Fault
ADD R1,R2,R3
instruction
LW R4,0(R1)
page TLB miss CPU generates a page
boundary Page Table miss fault exception
Lecture 24: 21
Computer with Input/Output
Processor
L1 L1
Inst Data
Cache Cache
+ +
Inst Data
TLB TLB
disk, keyboard,
interconnect graphics,
(e.g., bus) network, etc
L2 Cache
Input
Output
Input/
Main Memory
Output
Lecture 24: 22
Input/Output Devices
Lecture 24: 23
I/O Controller
• An I/O controller manages one or more
peripheral devices
– Function: coordinates data transfers between the
device(s) and the rest of computer system
Lecture 24: 24
Example Server System with I/O
PCIe
Lecture 24: 25
Accessing I/O Devices
• How do we get a command/data to the right device?
• Memory-mapped I/O
– Portion of the physical address space is assigned to
I/O device registers
– Only the OS can access these addresses
– Each I/O device register has a unique memory address
Lecture 24: 26
Data Transfer Between I/O and Memory
• Programmed I/O (PIO)
– Processor completely arbitrates transfer of data from
device to memory
• Typically much less efficient than DMA
Lecture 24: 27
Reading Data from Disk
FSB
interface
memory
controller
PCIe
NB-SB
interface
SATA
controller
Lecture 24: 28
Reading Data from Disk
FSB
interface
memory
controller
PCIe
NB-SB
interface
SATA
controller
“read N bytes
from disk
starting at addr X
to memory
starting at addr Y”
Lecture 24: 29
DMA Transfer from Disk Controller
FSB
interface
data written to
memory location Y memory
controller
PCIe
NB-SB
interface
Lecture 24: 30
Informing the Processor
• Need to inform the processor when an I/O
operation is completed
• Polling
– Processor periodically reads the Status Register,
which indicates when an operation is done
• Interrupt-driven I/O
– I/O device sends an interrupt to the processor when
the operation is done
– More efficient than polling
Lecture 24: 31
Informing Using an Interrupt
interrupt
“I/O complete”
FSB
interface
memory
controller
PCIe
NB-SB
interface
SATA
controller
Lecture 24: 32
Let’s Pull Some Pieces Together
Lecture 24: 33
Data Page Fault Occurs in Task A
exception Control PC of LW “page fault”
CU Signals
Exception Exception
=? PC Cause
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 34
Exception Handler Gets Loaded
exception Control
CU Signals
Exception Exception
=? PC Cause
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 35
Exception Handler Takes Action
• Saves task A state
Lecture 24: 36
OS Sets Up Disk Transfer
FSB
interface
memory
controller
PCIe
NB-SB
interface
SATA
controller
“read 4K bytes
from disk
starting at addr X
to memory
starting at addr Y”
Lecture 24: 37
OS Schedules Another Task
• While waiting for the disk read to complete, the
OS scheduler runs a different task (task B)
Lecture 24: 38
OS Switches from Task A to Task B
exception Control
CU Signals Exception Exception
PC Cause
Page Table Register =?
MW
sign bit
EL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
exception TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 39
OS Switches from Task A to Task B
1. What do we do about the PC?
Lecture 24: 40
Data are Read from Disk
(now running
task B)
FSB
interface
memory
controller
PCIe
NB-SB
interface
Lecture 24: 41
Data are Read from Disk
(now running
task B)
FSB
interface
memory
controller
PCIe
NB-SB
interface
Lecture 24: 42
DMA Transfer to Memory
(now running
task B)
FSB
interface
data written to
memory location Y memory
controller
PCIe
NB-SB
interface
SATA
controller
data transferred to
memory controller
Lecture 24: 43
Device Interrupts Processor
(now running
task B)
interrupt
“I/O complete”
FSB
interface
memory
controller
PCIe
NB-SB
interface
SATA
controller
Lecture 24: 44
Task B is Interrupted
exception/ PC of ADD “I/O complete”
Control
interrupt CU Signals Interrupt Interrupt
PC Cause
=?
MW
sign bit
EL/IL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
interrupt TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 45
Interrupt Handler Gets Loaded
exception/ Control
interrupt CU Signals Interrupt Interrupt
PC Cause
=?
MW
sign bit
EL/IL
Adder
+2 Fm … F0 L1
M Data
RF U M Cache
M LD X
P U
Decoder
L1 M
U Inst
SA X
M ALU
X C Cache
SB
M
U U
DR X D_IN
U
X
M
U
X
PCJ X MB Data
D_in TLB MD
PCL
PC of the Inst SE
interrupt TLB
handler IF/ID ID/EX EX/MEM MEM/WB
Lecture 24: 46
Task A Can Now Run Again
• Page fault was handled, so OS marks task A
as runnable
Lecture 24: 47
Building a Complete Computer
A
Y
B
Lecture 24: 48
Next Time
Lecture 24: 49