I am currently reading Operating Systems - Three Easy Pieces, by Remzi and Andrea, and at the part on context switching, it states that during an interrupt, the registers of the running process is saved onto its kernel stack, and then the registers for the next process is loaded from its PCB, but then later on it says its loaded from its kernel stack
My question is, why load the registers from the PCB when you later load them from the kernel stack, and what even is the point of a PCB when you save all the info on the kernel stack?
31 Answer
I have created a diagram to help understand the flow better. I've organized it similar to the figure in the question.
During a context switch, does the OS use PCB or kernel stack to restore registers?
It uses both.
why load the registers from the PCB when you later load them from the kernel stack
Because you only load the esp of the Kernel Stack from the PCB, and the rest of the registers from the Kernel Stack.
what even is the point of a PCB when you save all the info on the kernel stack
Because you DON'T save ALL the info on the Kernel Stack, you save MOST info on the Kernel Stack. The very location of the Kernel Stack (aka its esp) is stored in the PCB.
NOTE: Kindly note that the diagram is purely for illustrative purposes (i.e. to specifically and only answer the OP's question as to what is the point of separately saving the user-space registers and kernel-space registers into the Kernel Stack and PCB respectively). It is not entirely accurate in the sense that there are a bunch of other registers that get saved & restored depending on the OS and its architecture. Nevertheless, it illustrates the answer to the OP's question accurately.
2