Exception Handling and Interrupts in MIPS Architectures

What are Exception and Interrupts?
 
MIPS terminology
Exception: any unexpected change in the internal
control flow
Invoking an operating system service from user
program
Integer arithmetic overflow
Using an undefined or unimplemented instruction
Hardware malfunctions
Interrupt: event is externally caused
I/O device request
Tracing instruction execution
Breakpoint (programmer-requested interrupt)
J
a
n
u
a
r
y
 
 
2
0
1
4
1
Exceptions in MIPS
J
a
n
u
a
r
y
 
 
2
0
1
4
2
What Happens During an Exception?
 
An exception occurs
Operating system trap
Saving the PC where the exception happens
Save the operating system state
Run exception code
Resume the last instruction before it traps, or
terminate the program
J
a
n
u
a
r
y
 
 
2
0
1
4
3
 
normal control flow:
sequential, jumps, branches, calls, returns
J
a
n
u
a
r
y
 
 
2
0
1
4
4
Exception Handling in Multi-Cycle MIPS
 
Consider two types of exceptions:
arithmetic overflow
undefined instruction
First save the address of the offending instruction in
Exception Program Counter 
(
EPC
).
Transfer control to the 
operating system 
(
OS
) to
some specified address.
OS provides some service.
OS can stop the program or restart the program
using EPC.
J
a
n
u
a
r
y
 
 
2
0
1
4
5
 
Vectored interrupts example:
The addresses are separated by 32 bytes (8 instructions).
OS performs some limited process.
Communicating Exception Reason to OS
 
Two methods:
Using a status register called 
Cause Register
, holding the
reason of exception
Using vectored interrupts. The control is transferred to an
address determined by the interrupt cause.
J
a
n
u
a
r
y
 
 
2
0
1
4
6
Non Vectored Exceptions
 
A single entry point 
8000 0180 
hex 
is used for all
exceptions.
The OS is decoding the 
status register 
to find the
cause.
Two registers are added to the data-path
EPC
: 32-bit holding the address of the affected instruction.
It is required for vectored exceptions too.
Cause
: 32-bit recording the exception cause (some bits
unused).
In this example LSB of Cause encodes the exception.
EPC must trim 4 from PC.
J
a
n
u
a
r
y
 
 
2
0
1
4
7
E
x
c
e
p
t
i
o
n
s
 
H
a
n
d
l
i
n
g
 
i
n
 
M
u
l
t
i
-
C
y
c
l
e
 
M
I
P
S
 
J
a
n
u
a
r
y
 
 
2
0
1
4
8
 
J
a
n
u
a
r
y
 
 
2
0
1
4
9
Exception in pipelined architecture
 
Force a trap instruction into the pipeline on
the next IF
Flush the pipeline for the faulting instruction
and all instructions that follow
After exception handling routine finishes,
restore the PC of the saved PC and delay
branches if exist
J
a
n
u
a
r
y
 
 
2
0
1
4
10
10
Additions to MIPS ISA
EPC (Exceptional Program Counter)
A 32-bit register
Holds the address of the offending instruction
Cause
A 32-bit register
Records the cause of the exception
Status
interrupts mask and enable bits that determines
what exceptions can occur.
J
a
n
u
a
r
y
 
 
2
0
1
4
11
11
 
Control signals to write EPC , Cause, and Status
Be able to write exception address into PC,
increase mux set PC to exception address
(MIPS uses 8000 00180
hex
 
).
May have to undo PC = PC + 4, since EPC
should point to offending instruction (not to
its successor); PC = PC - 4
What else?
Flush all succeeding instructions in pipeline
J
a
n
u
a
r
y
 
 
2
0
1
4
12
12
Additions to MIPS ISA
J
a
n
u
a
r
y
 
 
2
0
1
4
13
13
Exceptions example
40
hex
 
    sub 
 
$11, 
 
$2, 
 
$4
44
hex
 
   and
 
$12, 
 
$2, 
 
$5
48
hex
 
   or   
 
$13, 
 
$2, 
 
$6
4C
hex
 
   
add
 
$1, 
 
$2, 
 
$1;   // arithmetic overflow
50
hex
 
     slt  
 
$15,
 
 $6,
 
$7
54
hex
      lw  
 
$16, 
 
50($7)
80000240
hex
 
sw 
 
$25, 
 
1000($0)
80000244
hex
 
 sw
 
$12, 
 
1004($0)
E
x
c
e
p
t
i
o
n
 
h
a
n
d
l
i
n
g
 
p
r
o
g
r
a
m
:
J
a
n
u
a
r
y
 
 
2
0
1
4
14
14
J
a
n
u
a
r
y
 
 
2
0
1
4
15
15
16
16
C
l
o
c
k
 
6
J
a
n
u
a
r
y
 
 
2
0
1
4
17
17
C
l
o
c
k
 
7
J
a
n
u
a
r
y
 
 
2
0
1
4
More on Exceptions
J
a
n
u
a
r
y
 
 
2
0
1
4
18
18
Precise Exceptions
If the pipeline can be stopped so that the
instructions just before the faulting instruction
are completed and the faulting instruction can
be restarted from scratch.
J
a
n
u
a
r
y
 
 
2
0
1
4
19
19
MIPS Exception Mechanism
 
The processor operates in
user mode
kernel mode
Access to additional set of registers and to
user mode 
restricted memory space  available
when the processor operates in 
kernel mode
.
The MIPS architecture includes the notion of
co-processors
.
J
a
n
u
a
r
y
 
 
2
0
1
4
20
20
Co-processor
Contains registers useful for handling exceptions
Not accessible in 
user mode
.
Includes the status register, cause register, BadVaddr,
and EPC (Exception Program Counter).
J
a
n
u
a
r
y
 
 
2
0
1
4
21
21
Cause Register
The cause register contains information about pending interrupts
and the kinds of exception that occurs.
J
a
n
u
a
r
y
 
 
2
0
1
4
22
22
The contents of the cause register can be copied into
an ordinary register and have the individual bits
tested to determine what caused an exception to
occur.
   
 
mfc0 $26, $13
The above instruction moves data from 
coprocessor0
register $13 (cause register) to general purpose
register $26
J
a
n
u
a
r
y
 
 
2
0
1
4
23
23
Status Register
The status register contains information about the
status of features of the computer that can be set by
the processor while in 
kernel mode
J
a
n
u
a
r
y
 
 
2
0
1
4
24
24
Exception Program Counter (EPC)
 
Contains the address of the instruction that
was executing when the exception was
generated.
Control can be made to return to this location
to continue the program.
The contents of EPC can be transferred to a
general register via the following instruction
mfc0 R
t
, $14
J
a
n
u
a
r
y
 
 
2
0
1
4
25
25
Exception Handler
 
MIPS R32 fixes the starting address of the exception
handler to 
0x8000 0180
.
A jump table consists of a list of procedure addresses
to be called to deal with the various exception
conditions.
In an interrupt, the PC had already been incremented
and EPC would contain the correct return address.
In a syscall, the EPC contains the address of the
syscall itself, thus the exception handler 
must first
increment
 
the return address by one before the
return.
J
a
n
u
a
r
y
 
 
2
0
1
4
26
26
Handling an Exception
 
An exception has occurred. What happens?
The hardware
copies PC into EPC ($14 on cop0) and puts correct
code into Cause Reg ($13 on cop0)
Sets PC to 
0x80000180
enters 
kernel mode
Exception handler (software)
Checks cause register (bits 5 to 2 of $13 in cp0)
jumps to exception service routine for the current
exception
J
a
n
u
a
r
y
 
 
2
0
1
4
27
27
J
a
n
u
a
r
y
 
 
2
0
1
4
28
28
As with any procedure,  the exception handler
must first save any registers it may modify, and
then restore them before returning to the
interrupted program.
 
Saving registers in memory poses a problem in
MIPS. Addressing the memory requires a register
(base) to form an address.
 
This means that a register must be modified
before any register can be saved!
 
The MIPS register usage convention reserves $26
($k0) and $27 ($k1) for the use of the interrupt
handler.
J
a
n
u
a
r
y
 
 
2
0
1
4
29
29
 
This means that the interrupt handler can use
those without saving them first. A user program
that uses those may find them unexpectedly
changed!
Slide Note
Embed
Share

Exception and interrupts in MIPS play a crucial role in handling unexpected events and external requests efficiently. Exceptions include changes in control flow, arithmetic overflows, and hardware malfunctions, while interrupts are externally caused events like I/O requests. When an exception occurs, the system traps it, saves the program counter, runs exception code, and resumes execution. Exception handling involves saving the instruction address, transferring control to the operating system, and potentially stopping or restarting the program. Communication of exception reasons to the OS can be done through status registers or vectored interrupts. Non-vectored exceptions use a single entry point for all exceptions, with the OS decoding the cause from the status register.

  • Exception Handling
  • Interrupts
  • MIPS Architecture
  • Operating System
  • Hardware Malfunctions

Uploaded on Oct 03, 2024 | 3 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow Invoking an operating system service from user program Integer arithmetic overflow Using an undefined or unimplemented instruction Hardware malfunctions Interrupt: event is externally caused I/O device request Tracing instruction execution Breakpoint (programmer-requested interrupt) 1 January 2014

  2. Exceptions in MIPS stage Problem exceptions occurring Page fault on IF, misaligned memory access, memory protection violation Undefined or illegal opcode IF ID EX Arithmetic exception MEM Page fault on data fetch, misaligned memory access, memory protection violation None WB January 2014 2

  3. What Happens During an Exception? An exception occurs Operating system trap Saving the PC where the exception happens Save the operating system state Run exception code Resume the last instruction before it traps, or terminate the program 3 January 2014

  4. System Exception Handler user program Exception return from exception normal control flow: sequential, jumps, branches, calls, returns 4 January 2014

  5. Exception Handling in Multi-Cycle MIPS Consider two types of exceptions: arithmetic overflow undefined instruction First save the address of the offending instruction in Exception Program Counter (EPC). Transfer control to the operating system (OS) to some specified address. OS provides some service. OS can stop the program or restart the program using EPC. 5 January 2014

  6. Communicating Exception Reason to OS Two methods: Using a status register called Cause Register, holding the reason of exception Using vectored interrupts. The control is transferred to an address determined by the interrupt cause. Vectored interrupts example: Exception Type Exception vector address (in hex) Undefined instruction C000 0000hex C000 0020hex Arithmetic overflow The addresses are separated by 32 bytes (8 instructions). OS performs some limited process. 6 January 2014

  7. Non Vectored Exceptions A single entry point 8000 0180 hex is used for all exceptions. The OS is decoding the status register to find the cause. Two registers are added to the data-path EPC: 32-bit holding the address of the affected instruction. It is required for vectored exceptions too. Cause: 32-bit recording the exception cause (some bits unused). In this example LSB of Cause encodes the exception. EPC must trim 4 from PC. 7 January 2014

  8. Exceptions Handling in Multi-Cycle MIPS 8 January 2014

  9. ALU output overflow. 9 January 2014

  10. Exception in pipelined architecture Force a trap instruction into the pipeline on the next IF Flush the pipeline for the faulting instruction and all instructions that follow After exception handling routine finishes, restore the PC of the saved PC and delay branches if exist 10 January 2014

  11. Additions to MIPS ISA EPC (Exceptional Program Counter) A 32-bit register Holds the address of the offending instruction Cause A 32-bit register Records the cause of the exception Status interrupts mask and enable bits that determines what exceptions can occur. 11 January 2014

  12. Control signals to write EPC , Cause, and Status Be able to write exception address into PC, increase mux set PC to exception address (MIPS uses 8000 00180hex). May have to undo PC = PC + 4, since EPC should point to offending instruction (not to its successor); PC = PC - 4 What else? Flush all succeeding instructions in pipeline 12 January 2014

  13. Additions to MIPS ISA 13 January 2014

  14. Exceptions example 40hex 44hex 48hex 4Chex 50hex 54hex sub $11, $2, and $12, $2, or $13, $2, add $1, slt $15, lw $16, 50($7) $4 $5 $6 $1; // arithmetic overflow $7 $2, $6, Exception handling program: 80000240hex sw 80000244hex sw $25, 1000($0) $12, 1004($0) 14 January 2014

  15. 15 January 2014

  16. overflow detected deasserting add flush EX flush ID flush IF nop Clock 6 16 January 2014

  17. first instruction of exception routine Clock 7 17 January 2014

  18. More on Exceptions 18 January 2014

  19. Precise Exceptions If the pipeline can be stopped so that the instructions just before the faulting instruction are completed and the faulting instruction can be restarted from scratch. 19 January 2014

  20. MIPS Exception Mechanism The processor operates in user mode kernel mode Access to additional set of registers and to user mode restricted memory space available when the processor operates in kernel mode. The MIPS architecture includes the notion of co-processors. 20 January 2014

  21. Co-processor Contains registers useful for handling exceptions Not accessible in user mode. Includes the status register, cause register, BadVaddr, and EPC (Exception Program Counter). 21 January 2014

  22. Cause Register The cause register contains information about pending interrupts and the kinds of exception that occurs. 22 January 2014

  23. The contents of the cause register can be copied into an ordinary register and have the individual bits tested to determine what caused an exception to occur. mfc0 $26, $13 The above instruction moves data from coprocessor0 register $13 (cause register) to general purpose register $26 23 January 2014

  24. Status Register The status register contains information about the status of features of the computer that can be set by the processor while in kernel mode 24 January 2014

  25. Exception Program Counter (EPC) Contains the address of the instruction that was executing when the exception was generated. Control can be made to return to this location to continue the program. The contents of EPC can be transferred to a general register via the following instruction mfc0 Rt, $14 25 January 2014

  26. Exception Handler MIPS R32 fixes the starting address of the exception handler to 0x8000 0180. A jump table consists of a list of procedure addresses to be called to deal with the various exception conditions. In an interrupt, the PC had already been incremented and EPC would contain the correct return address. In a syscall, the EPC contains the address of the syscall itself, thus the exception handler must first increment the return address by one before the return. 26 January 2014

  27. Handling an Exception An exception has occurred. What happens? The hardware copies PC into EPC ($14 on cop0) and puts correct code into Cause Reg ($13 on cop0) Sets PC to 0x80000180 enters kernel mode Exception handler (software) Checks cause register (bits 5 to 2 of $13 in cp0) jumps to exception service routine for the current exception 27 January 2014

  28. As with any procedure, the exception handler must first save any registers it may modify, and then restore them before returning to the interrupted program. Saving registers in memory poses a problem in MIPS. Addressing the memory requires a register (base) to form an address. This means that a register must be modified before any register can be saved! The MIPS register usage convention reserves $26 ($k0) and $27 ($k1) for the use of the interrupt handler. 28 January 2014

  29. This means that the interrupt handler can use those without saving them first. A user program that uses those may find them unexpectedly changed! 29 January 2014

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#