03/23/22 CMPUT 229 1 Exception Handling Exception Handling Exceptions (when things go Exceptions (when things go wrong) wrong) Things that are not expected Things that are not expected Things that are not a part of Things that are not a part of normal operations normal operations Exceptions Exceptions Multiple threads with controls Multiple threads with controls exchanged frequently exchanged frequently Based on Chapter 12, Goodman, James and Miller Karen
40
Embed
2/06/2015CMPUT 229 1 Exception Handling Exceptions (when things go wrong) Things that are not expected Things that are not a part of normal operations.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
04/18/23 CMPUT 229 1
Exception HandlingException Handling Exceptions (when things go wrong)Exceptions (when things go wrong)
Things that are not expectedThings that are not expected Things that are not a part of normal Things that are not a part of normal
operationsoperations Exceptions Exceptions
Multiple threads with controls Multiple threads with controls exchanged frequently exchanged frequently
Based on Chapter 12, Goodman, James and Miller Karen
What we do if things go wrong?What we do if things go wrong? Send out bad resultsSend out bad results let it crashlet it crash Try to recoverTry to recover
How to shift controls between two How to shift controls between two regular programs?regular programs? All information must be savedAll information must be saved Me able to resume normal execution Me able to resume normal execution
after shiftafter shift
04/18/23 CMPUT 229 3
Exception handlingException handling JavaJava
Two classes for modeling problemsTwo classes for modeling problems• ErrorError• ExceptionException
Exception handlingException handling• Raise an exceptionRaise an exception• Catch an exceptionCatch an exception• Throw an exception Throw an exception
Database management systemsDatabase management systems Logs for recording update informationLogs for recording update information Check point for periodically enforcing Check point for periodically enforcing
log writinglog writing Recovery for recovering the database Recovery for recovering the database
into a consistent state after any type into a consistent state after any type of errors and crashof errors and crash
04/18/23 CMPUT 229 6
PollingPolling Has to stop working every once in a while to
see if there is someone in need of attention. Advantages
• Never interrupted in the middle of a task, thus can plan when to stop to check.
Disadvantages• Will stop working several times to check when
there is nobody there.• Someone might have to wait for a while until the
next pooling opportunity to be noticed.
How to detect How to detect exceptions?exceptions?
04/18/23 CMPUT 229 7
InterruptionWhen someone needs attention that person will interrupt the work.
Advantages• Never need to stop working if nobody needs
attention.
DisadvantagesDisadvantages• Will be stopped in the middle of a task. More
difficult to save/restore state (context switching can be expensive).
• Hardware and software must be involvedto provide a context switching mechanism.
How to detect How to detect exceptions?exceptions?
from the execution of the program, such asfrom the execution of the program, such as• Arithmetic overflowArithmetic overflow• Out of bond memory accessOut of bond memory access
InterruptionInterruption Asynchronous exceptions, resulting from Asynchronous exceptions, resulting from
external events, such as external events, such as • IO requestIO request• Control shiftControl shift
04/18/23 CMPUT 229 9
The MIPS Exception The MIPS Exception MechanismMechanism
A MIPS processor operates in one of two modes: kernel and user.( Kernel can be viewed as the exception handler of the operating system)
When running in the kernel mode, a program has access to anadditional set of registers that are not accessible to a programrunning in the user mode.
Memory locations in the upper half of the address space, i.e.memory locations whose most significant bit is 1, can only beaccessed in the kernel mode.
This region include:- Exception handler code- Data that is only accessible by the operating system- Memory-mapped I/O addresses
04/18/23 CMPUT 229 10
Co-processor C0Co-processor C0
Co-processor C0 only canbe accessed when executingin the kernel mode.
The registers of coprocessor C0 are accessed, when executing in the kernel mode,through the instructionsmtc0 and mfc0
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersCU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 17
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersCU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 18
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersCU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 19
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersCU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 20
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersCU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 21
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersRP: Enables reduced-power operation (reduce clock freq.)CU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 22
The Status RegisterThe Status Register
KUc: Is the program running in kernel mode or user mode?PUp: Was the processor in kernel or user mode when the last exception occurred? IM(0)-IM(7): Interrupt Mask. It controls which interrupts are enabled.RE: Reverse-Endian (RE) bit. Reverses the endianess of the machine.FR: Enables additional floating point registersRP: Enables reduced-power operation (reduce clock freq.)CU: 4 bits to control co-processor usability
CU
31-28
•••
24-16
IM(1) IM(0)
9 8
•••
7-6
KUo
5
IM(7) IM(6) IM(5) IM(4) IM(3) IM(2)
101112131415
ExternalInterrupts
SoftwareInterrupts
IEo
4
PUp
3
IEp
2
KUc
1
IEc
0
RP
27
FR
26
RE
25
04/18/23 CMPUT 229 23
Invocation and Return Invocation and Return from the Exception from the Exception
HandlerHandler
The MIPS architecture fix a single address --- 0x8000 0080 --- to bethe starting address of the exception handler.
When an exception is invoked, control is transferred to this location.
The exception invocation must also save the returning address forthe exception. But it cannot save this address in $ra, because $ra contains the returning address for the routine that is currently executing.
The Exception Program Counter (EPC) is loaded with the address atwhich processing resumes after an exception is serviced.
The (EPC) is a register of the co-processor C0. The content of theEPC can be transferred to a general register using the instruction:
Examining the Exception Examining the Exception CodeCode
Exception Handler:<preliminary check for reentrance>
OK: <set flag><save registers>mfc0 $k0, $13 # Get the Cause Registerandi $k0, $k0, $0x7c # Mask out the ExcCode bitslw $k0, JumpTable($k0) # Read address from jump tablejr $k0 # Jump to exception handler
04/18/23 CMPUT 229 26
Exception Handler:<preliminary check for reentrance>
OK: <set flag><save registers>mfc0 $k0, $13 # Get the Cause Registerandi $k0, $k0, $0x7c # Mask out the ExcCode bitslw $k0, JumpTable($k0) # Read address from jump tablejr $k0 # Jump to exception handler
Examining the Exception Examining the Exception CodeCode
Return: <restore registers><clear flag>mfc0 $k0, $14 # Get EPC registerrfe # clean up state registerjr $k0 # return to the address in EPC
04/18/23 CMPUT 229 27
Building the Jump Table Building the Jump Table (The Cause Register)(The Cause Register)
We assume that the value of $v0 was saved at the beginning ofthe exception service in the memory location tmpv0
In this example of exception handling, lets assume that we want to handle the following syscall functions: putc, puts, exit, and getc.
04/18/23 CMPUT 229 30
The Syscall HandleThe Syscall Handle
Exception Handler:HandleSys:
mfc0 $k1, $14 # Get the EPC Registeraddi $k1, $k1, 4 # Increment PC past syscall instructionmtc0 $k1, $14 # Set the EPC Register
lw $v0, tmpv0 # get the previously saved value of $v0li $a0, 11 # Is it putc?beq $v0, $a0, Putcli $a0, 4 # Is it puts?beq $v0, $a0, Putsli $a0, 12 # Is it getc?beq $v0, $a0, Getcli $a0, 10 # Is it exit?beq $v0, $a0, Exit
la $a0, SysMessage # Bad number in $v0jal Print String # Print messagej Return
04/18/23 CMPUT 229 31
The Syscall HandleThe Syscall Handle
Exception Handler:Exit: <code for Exit>
Putc: <code for Putc>
Puts: <code for Putc>
Getc: <code for Getc>
04/18/23 CMPUT 229 32
Checking for ReentranceChecking for Reentrance
Exception Handler:flag: .word 0 # remembers if it was already in the
# exception handler when the exception# occurred
BadReentry:.asciiz “Bad re-entry into kernel\nHalting\n”.ktext.space 0x80 # skip space so that the handler is at 0x80000080add $k1, $at, $zero # immediately save $atlw $k0, flag # SPIM translates this instruction to:
# lui $at, 4097# lw $k0, 0($at)
beq $k0, $zero, OKla $a0, BadReentryj Panic
OK: <set flag><save registers>mfc0 $k0, $13 # Get the Cause Registerandi $k0, $k0, 0x7c # Mask out the ExcCode bitslw $k0, JumpTable($k0) # Read address from jump tablejr $k0 # Jump to exception handler
04/18/23 CMPUT 229 33
Code for PanicCode for Panic
The code for Panic has to print the following message:
Pmess: .asciiz “Panic: “.ktext # Panic prints a message and quits
Panic: la $a1, PmessPRead1: lb $a2, ($a1) # read letter to print
beq $a2, $zero, PRead2 # done when we find a nullPWait1: lw $a3, DisplayStatus # Read the status of the display
bge $a3,$zero, PWait1 # keep reading until it is readysw $a2, DisplayData # output characteraddi $a1, $a1, 1 # advance characterj PRead1
PRead2: lb $a2, ($a0) # Print message pointed by $a0beq $a2, $zero, Pcontinue # done when we find a null
PWait2: lw $a3, DisplayStatus # Read the status of the displaybge $a3, $zero, PWait2 # keep reading until it is readysw $a2, DisplayData # output characteraddi $a0, $a0, 1 # advance characterj PRead2
Pcontinue: li $v0, 0 # clear re-entrance flagsw $v0, flagli $v0, 13 # the quit_now syscallsyscall
<save registers>mfc0 $k0, $13 # Get the Cause Registerandi $k0, $k0, $0x7c # Mask out the ExcCode bitslw $k0, JumpTable($k0) # Read address from jump tablejr $k0 # Jump to exception handler
Return: <restore registers><clear flag>mfc0 $k0, $14 # Get EPC registerrfe # clean up state registerjr $k0 # return to the address in EPC
Puta0: lw $k0, pqhead # load head of put queuelw $k1, pqtail # load tail of put queueaddi $k1, 1 # increment tailandi $k1, $k1, 0x00ff # module counterbeq $k1, $k0, Full # try to print a charn if queue is fullsb $a0, putqueue($k1) # add byte to the queuesw $k1, pqtail # save new tailmfc0 $k1, $12 # Get status registerori $k1, 0x5000 # Turn on display interruptsmtc0 $k1, $12 # Set status registersw $ra, tmpputra # Save return addressjal DP_handler # try to printlw $ra, tmpputra # Restore return addressjr $ra
Full: sw $ra, tmpputra # Save return addressjal DP_handler # Try to printlw $ra, tmpputra # Restore return addressj Puta0
04/18/23 CMPUT 229 38
Code for DP_handlerCode for DP_handler
Exception Handler:.kdata
putqueue: .space 256.align 2.ktext
pqtail: .word 0pqhead: .word 0
.ktextDP_handler:
lw $k0, DisplayStatus # Get display status wordbge $k0, $zero, DPRet # Return if it is not Readylw $k0, pqhead # lw $k1, pqtailbeq $k0, $k1, DPRet # Return if put queue is emptyaddi $k0, $k0, 1 # Increment headandi $k0, $k0, 0x00ff # Module countersw $k0, pqhead # Save new headlb $k0, putqueue($k0) # get character from put queuesb $k0, DisplayData # Send character to display
DPRet: jr $ra # Return to caller
04/18/23 CMPUT 229 39
Code for PutsCode for Puts
Exception Handler:.kdata
tmpputsra: .word 0Puts: lw $k0, tmpa0 # Get address of first characterPutsLoop: lbu $a0, ($k0) # Get a character
beq $a0, $zero, PutsRet # If character is null, we are donesw $ra, tmpputsra # Save return addressjal Puta0 # Print the charactersw $ra, tmpputsra # Save return addressaddi $k0, $k0, 1 # Move pointer to next characterj PutsLoop # Loop back
PutsRet: jr $ra
04/18/23 CMPUT 229 40
Bad Virtual Address Bad Virtual Address RegisterRegister
The register $8 of the coprocessor 0 is the BadVAddr (Bad Virtual Address)register. It is a read only address register that contains the most recentvirtual address that caused one of the following exceptions:
TLB InvalidTLB ModifiedTLB RefillVirtual Coherency Data AccessVirtual Coherency Instruction Fetch
The BadVAddr does not save any information for bus errors, since bus errors are not addressing errors.