Software Exceptions (trap)

The software trap instruction performs the following operations:


estatus ← status
PIE ← 0
U ← 0
ea ← PC + 4
PC ← exception handler address

It saves the address of the next instruction in register ea, saves the contents of the status register in estatus, disables interrupts, and transfers execution to the exception handler. The address of the exception handler is specified at system generation time.

To return from the exception handler, execute an exception return (eret) instruction. This instruction does the following:


   status ← estatus
   PC ← ea

In the example below, the system software generates a break instruction in response to a software trap. The IDE debugger stops execution when it encounters a break instruction. From the description of break it would seem that break should be treated similar to trap.

Example

Download: isa8.zip (project files)

Debug the program isa8.s (see source) which consists basically of the following code:

main:
	movi	r4,36
	movi	r5,18
	trap
end:
	br	end   /* wait here once the program has completed */

Exercise 1

Single step through the code. The trap instruction branches to the interrupt handler alt_irq_entry (see source) located at the exception handler address, 0x800020.

Continue single stepping. The last instructions on the disassembly window are:

0x00800074 <alt_irq_entry+84>: rdctl r4,ipending
0x00800078 <alt_irq_entry+88>: andi  r2,r5,1
0x0080007c <alt_irq_entry+92>: beq   r2,zero,0x80008c <alt_irq_entry+108>
0x00800080 <alt_irq_entry+96>: beq   r4,zero,0x80008c <alt_irq_entry+108> 

Stepping past the last instruction above brings up the following:

0x0080008c <alt_irq_entry+108>: stw   ea,72(sp)
0x00800090 <alt_irq_entry+112>: ldw   r2,-4(ea)
0x00800094 <alt_irq_entry+116>: break 0

The debugging process stops at the break instruction.

Exercise 2

Move alt_exception_trap.S ( see source) into the project.

This file was modified by adding the following trap handler, at the location described in the original source:

        movi    r3,1234
        break 11

Rebuild the project.

Single step as before through the trap instruction until you get to the following disassembly window. Note that the code is now from alt_exception_trap. Stepping through the remaining steps stops at the break instruction.

0x0080008c <alt_irq_entry+108>: stw   ea,72(sp)
0x00800090 <alt_irq_entry+112>: ldw   r2,-4(ea)
0x00800094 <alt_irq_entry+116>: movhi r3,59
0x00800098 <alt_irq_entry+120>: ori   r3,r3,26682
0x0080009c <alt_irq_entry+124>: bne   r2,r3,0x8000ac <alt_irq_entry+140>
0x008000a0 <alt_irq_entry+128>: movi  r3,1234
0x008000a4 <alt_irq_entry+132>: break 11

Exercise 3

Comment out the break 11 statement in alt_exception_trap.S and debug the program again using the instruction set simulator. This time the program will continue past the break location, restore saved registers, execute an eret instruction and return to the instruction just after the software trap.


Maintained by John Loomis, last updated 15 November 2008