[comp.sys.m68k] Bus Error handling

rbt@cernvax.UUCP (roberto divia) (11/09/89)

In article <6028@samsung.samsung.com> duane@samsung.COM (Andrew Duane) writes:
>In general, when an exception occurs, we trap it, do some stuff, and
>"jump" to another routine by resetting the return PC on the stack. When
>the RTE happens, the other routine is entered.  This works just fine in
>99% of the cases. The problem is that when a bus error happens (e.g. a
>reference to a memory location that does not exist), the other routine
>does not get returned to. Instead, the RTE returns to the offending code,
>after the instruction that bus error'd.

Ahem, we are getting into troubles... When a bus error occours, the 680[23]0
saves on the stack also the "internal" registers, the ones that normally we do
not see but are used by the processor during the various execution stages. The
description of all these registers is given in the Exception Stack Frames table
on the manual. Now, if you have a bus error, setting the DF flag and doing an
RTE forces the processor to reload the registers and resume execution. This is
not what you want. Solution: rewrite the BERR routine to do something like

	cmp.b	#$a0,6(a6)		($ means hexadecimal)
	beq	AEorBE1
	cmp.b	#$b0,6(a6)
	beq	AEorBE2
	.....				As you are doing now
AEorBE1	move.w	(a7),$1a(a7)
	adda.w	#$20,a7
bra	SimTr
AEroBE2	move.w	(a7),$56(a7)
	adda.w	#$5c,a7
SimTr	move.w	#$0080,-(a7)
	move.l	#forced_PC,-(a7)
	adda.w	#2,a7
	rte

This code, if the error is a BUS error, moves the executor's SR to the bottom
of the exception frame, throws away the frame and builds a TRAP 0 frame. By
doing this, you loose the frame content but - at least - execution can be
forced to a given routine within THE SAME ENVIRONMENT of the one that generated
the bus error. 

+-----------------------+----------------------------------------------+
|   Roberto Divia`      | Love at  first sight  is one of the greatest |
|   =============       | labor-saving devices the world has ever seen |
+-----------------------+----------------------------------------------+
|  CERN :  European Laboratory for Particle Physics,  1211  Geneva  23 |
|  Switzerland (CH)                                                    |
+----------------------------------------------------------------------+