I have recently studied about interrupts and context switch i have read that the hardware interrupts cause the interrupt service subroutine to execute for this i read mixed articles saying that there no need for a context switch to kernel mode and some say we need a context switch to kernel mode from user mode do we need a switch to kernel mode in case of hardware interrupts
1 Answer
I think the answer partially intersects that of Do system calls always means a context switch?: it depends on the exact definition of "context switch". On most CPUs, an interrupt is not serviced in user mode but rather in a more privileged mode (IRQ mode on ARM, on x86 the IDT can specify the ring level). This is necessary because simply clearing the interrupt or determining its source requires accessing hardware registers (which by definition user mode cannot in general). That being said, the OS does not need to run the entire ISR in a privilege mode. Typically in a microkernel, an IRQ will be added to a queue and processed by a (user space) driver.
So an interrupt implies a privilege switch, which implies some amount of context switching (saving registers for example) but not necessarily a full process switch, depending on the architecture and the kernel.