This question is not exactly Linux related, but I figured this would be a good place to ask it.

I just can't understand how an OS kernel (not just Linux) knows whether it can do a context switch at the end of an interrupt routine. How does it know that the interrupt is not nested?

I read somewhere that an ISR usually notifies the kernel when it starts executing and later, when it is about to return. Thus the kernel keeps track of nested routines.

However, the following scenario is possible:

1.A low-priority interrupt routine starts.
2. Immediately, a high priority interrupt preempts the low priority one. The kernel is never notified about the first interrupt.
3. The high level interrupt completes and notifies the kernel. The kernel thinks that this is not a nested interrupt (it does not know about the first interrupt).
4. The kernel does a context switch.

I looked at the code for a real RTOS (uC/OS 1.11) to see how they solve the problem in practice. Well, it appears that they DO NOT solve it and their context switching is bugged ?

Thanks for helping!