This Answer Record describes how software can use the MicroBlaze user vector exception functionality, including simple code examples to illustrate typical use cases, and references to the documentation where the feature is described.
The MicroBlaze processor provides a user exception that can be invoked by calling the user exception vector.
This article describes how to declare and call a user exception handler from software.
The User Vector, by default at address 0x8, is defined in the GNU Compiler Tool crt0.o file, where it calls the function named _exception_handler.
This function is defined in the newlib library, but not used anywhere in the BSP, so you can directly implement the function in your code.
It can be written in a high level language (C, C++) or assembler.
There are two ways to call a user exception from software, using the normal subroutine call instruction bralid, or using the brki instruction.
The brki instruction has the advantage of automatically disabling breaks, and thus also interrupts.
When a user exception handler coded in C is called with brki, breaks must be enabled in the handler before returning.
This can be done with an msrclr instruction, provided that the MicroBlaze parameter C_USE_MSR_INSTR is set to 1.
Also, the return from the user exception handler normally assumes that the call was made with an instruction using a delay slot.
This can be handled by adding a nop instruction after the brki.
When the MMU is enabled, the user exception handler is executed in real privileged mode, which makes it suitable as an operating system call entry point.
Here are two simple code examples written in C:
Note that although a message is printed in the handler in these examples, this is generally not a good practice.
Figure 1 Calling handler from bralid
Figure 2 Calling handler from brki