Undefined exception in ARM processors is described as being taken when the processor encounters an instruction that is undefined or not supported by implementation.
I am not able to understand that processor is in this mode is either due to program portability or due to the wrong instruction of some program which is not part of instruction sets of processor?
Best Answer
The processor enters "undefined mode" when it encounters an invalid instruction.
Not all possible bit patterns are valid instructions, and contrary to older processors where at least something happens, the processor detects these patterns and then generates an exception.
The handler for this exception is executed normally, but the processor mode is changed so that the
R13_und
andR14_und
registers are mapped toR13
andR14
. This is necessary because the handler requires a local stack and information about the faulting address.The stack for the handler needs to be separate because it is not guaranteed that the code currently running has a valid stack. Simply using whatever
R13
contained at this point would be a security issue, as the handler executes with supervisor privilege. Thus, the operating system at start reserves stack space for the Undefined handler and places the stack top address inR13_und
(by switching to UND mode briefly):The
R14
register is also shadowed, because it is overwritten at exception entry, but the handler may decide to emulate the offending instruction and return to regular program flow, which would require restoringR14
(for which a stack would be required, which again is not guaranteed).For example,
The
do_something
function storess3
on the stack, because it modifies it and wishes to restore it at the end, but doesn't saver14
, because it doesn't call any subroutines. If the CPU doesn't have an FPU, the Undefined handler gets called for thevstm
instruction.Actual exception vector:
Handler:
The
r14
value fordo_something
was hidden in ther14_USR
register, which is not accessible from Undefined mode -- if the handler were interested in that, it would change to System mode (which is a privileged mode with access to USR registers), copy the value to another register and switch back to UND mode in order to get access to its own stack back.