I'm in the process of porting an RTOS-like framework to AVR32-UC3 and, as any RTOS, I need to *reliably* detect if an ISR nests on top of another ISR. This is important, so that the preemptive scheduler is never invoked from a nested ISR. Only an ISR that is about to return to the "task level" can call the scheduler.
I've seen two solutions to this problem. One is in uC/OS-II (www.micrium.com), as described in the Micrium Application Note AN-1030 "uC/OS-II and the AVR32 UC3 Architecture". The Micrium port disables interrupts as the very *first* instruction of the interrupt handler:
Int0: SSRF VECTORS_SR_GM_OFFSET /* Set GM */ NOP NOP MFSR R12, VECTORS_SR_OFFSET ...
Later on, a interrupt nesting counter is incremented, the user ISR is called, and the interrupt nesting counter is decremented. The uC/OS-II scheduler is invoked only when the nesting counter is zero (i.e., no interrupt nesting).
But this implementation assumes that interrupt nesting cannot happen *before* the first instruction is executed. Now, I've carefully studied the AVR32 documentation, but I could not find any statement that would confirm that this assumption is true.
I mean, if this assumption was not true, a "late arriving" interrupt of a higher priority could preempt the current interrupt, but this would bypass incrementing the nesting counter and the scheduler would be invoked (and schedule a context switch) while the previous interrupt would be pending. It's hard to say how badly this can end, but at the very least this is a priority inversion.
So, could someone authoritatively confirm that Micrium's assumption is correct?
The second RTOS implementation I've seen (FreeRTOS.org) loads the *saved* SR from the stack and looks at the mode bits directly to determine if the ISR nests on top of another ISR or "task level" (supervisor mode in case of FreeRTOS). This method seems to me better and bullet-proof. At this point, I'm inclined to use this method.
However, checking the stacked SR is a little extra work and my scheduler maintains the interrupt nesting counter anyway. So, if the Micrium's method is actually correct, I'd prefer to use it.
Thanks a lot for your help!