Here is my understanding of the process switching (in amd64 Linux).
- A hardware interrupt or a software interrupt is triggered.
- Context of currently running process is saved to its PCB. This includes program counter, stack pointer, general-purpose registers, etc.
- Interrupt routine handler is run. This in turn calls scheduler code. Scheduler loads another process context onto the registers.
I am confused about who actually saves the context of the interrupted process. It cannot be the user program, because it doesn't know when it will be preempted. It cannot be the kernel code, because to run kernel code in the first place, the program counter has to point to the kernel code. If you do that, you are losing the interrupted process' program counter.