COMPX203 Computer Systems: Exceptions and Interrupts
COMPX203 Computer Systems: Exceptions and Interrupts
h p://q.xorro.com/tyhu
tt
Structure
• Recap
• Overview
• WRAMP Implementation
Recap
• Data representation, ASCII codes
• Memory-mapped style of Input and Output
• Special addresses are used to interact with I/O devices
• WRAMP implementation
• Addresses are mapped to I/O device “special registers”
• I/O with Parallel port
• I/O with Serial port
• Polling-based I/O
A Solution: Notifications
How about we have the phone tell you when a
message arrives?
Interrupt Example
Main Program
An interrupt occurs
Interrupt Handler
9
Interrupt Handler vs. a subroutine
• An interrupt handler performs actions like a
subroutine but
• Is triggered by an event instead of called
• No “passed parameters”- info stored in status registers
• Rather than returning a value, usually reset triggers
10
11
12
CPU should be able to
• Enable and disable specific interrupts (which to
notice and which to ignore)
• Automatically jump to a handler routine at interrupt
• Detect what caused the interrupt
• Return back to the main program when the interrupt
handler is finished
14
15
16
17
19
IRQ0 Unused
IRQ1 User Interrupt Button
IRQ2 Timer Interrupt
IRQ3 Parallel Interrupt
IRQ4 Serial Port 1 Interrupt
IRQ5 Serial Port 2 Interrupt
IRQ6 Unused
IRQ7 Unused
20
Example (pseudocode) for using Interrupts
main:
# Setup interrupts
…
loop:
# Main program loop ("Mainline" code)
…
j loop
handler:
# Check which exception occurred
# If it is the one we want, jump to
# handle_interrupt
# Otherwise, jump to the system handler
handle_interrupt:
# Do our handler stuff
# Acknowledge the interrupt
# Return from the interrupt (rfe)
21
22
Access to WRAMP Special Registers
• Access achieved via instructions movsg and movgs
• movsg copies a value from a special register to a general
one, e.g. movsg $2, $cctrl
• movgs copies a value from a general register to a special
one, e.g. movgs $cctrl, $2
• We must be in kernel mode to be able to use these
instructions
• Otherwise we’d cause a General Protection Fault (GPF)
23
31 11 4 3 2 1 0
OKU
OIE
KU
IE
Undefined IntMask
IRQ7
Interrupt Mask Bits …
IRQ0
Interrupt Mask provides a way to selectively turn on and off individual interrupts (but not exceptions)
24
25
26
CPU Control Register ($cctrl)
e.g. We wish to setup the CPU to allow only IRQ1 (user interrupt button):
…
# Copy the current value of $cctrl into $2
movsg $2, $cctrl
27
28
29
Exception Vector Register ($evec)
e.g. Set the CPU to go to our handler when an exception occurs …
…
# Copy the old handler’s address to $2
movsg $2, $evec
# Save it to memory
sw $2, old_vector($0)
30
31
Arithmetic
Software Breakpoint
Exceptions Syscall
GPF
IRQ7
Hardware
…
Interrupts
IRQ0
32
handle_irq1:
# Handle our interrupt
…
33
34
35
36
Example: Timer
Set up the timer to generate an interrupt every 10s
Basic steps:
1. Acknowledge interrupt
2. Reload timer
3. Restart timer
37
Programmable Timer
• WRAMP contains a • The programmer sees:
programmable timer, which can • Four registers
create an interrupt at defined
intervals from 1 msec to 30 sec • Base address is 0x72000
with a resolution of about 0.5
msec. Register Name Offset
• The timer has an internal 16-bit
register, decremented at a Timer Control 0
constant rate of 2400 Hz. When
the count reaches 0, an Timer Load 1
interrupt is triggered.
• The starting value can be set Timer Count 2
(timer load register)
• The timer can be set to auto Timer IA 3
reload the value
38
39
Interrupt Ack
40
Example: Timer
e.g. Setup the timer to generate an interrupt every 10s
…
# Acknowledge any outstanding interrupts
sw $0, 0x72003($0)
# Put our count value into the timer load reg
addi $11, $0, 24000
sw $11, 0x72001($0)
# Enable the timer and set auto-restart mode
addi $11, $0, 0x3
sw $11, 0x72000($0)
…
41
42
43
$cctrl on an Exception
OKU
OIE
KU
IE
1 0
44
$cctrl on RFE
OKU
OIE
KU
IE
45
Interrupting an Interrupt Handler?
• Any interrupts that occur while interrupts are
disabled will be handled as soon as interrupts are
turned back on (e.g. after rfe)
• Software exceptions, however, cannot be disabled!
• To prevent software exceptions, we need to write our
handler code very carefully (no overflow, or divide-
by-zero)
46
Interrupt Process
• The CPU checks if interrupts are enabled (IE = ‘1’)
• The CPU checks if that particular interrupt is enabled
• PC is saved to $ear
• Bits in $estat are set to indicate which exception(s) have
occurred
• The IE and KU bits in $cctrl are copied into the OIE and
OKU bits respectively
• Interrupts are disabled and the CPU is set to kernel mode (IE =
‘0’, KU = ‘1’)
• Value of register $13 is automatically saved to $ers
• The CPU jumps to the address stored in $evec
47
Interrupt Process
• Once the handler is finished, and the interrupt has
been acknowledged, the rfe instruction must be
executed.
• rfe will:
• Copy the OIE and OKU bits back to IE and KU,
respectively
• Restore the value of register $13 from $ers
• Jump back to where the exception occurred ($ear)
48
50
Homework
• Read chapter 4 of the manual
51