I disagree with the second paragraph.
Process A runs and then interrupts with a timer interrupt. The device saves its registers (on the kernel stack) and enters the kernel (switches to kernel mode).
I am not aware of a system that saves all the registers in the kernel stack on interrupt. Program counter, processor status, and stack pointer (assuming the hardware does not have a separate stack pointer in kernel mode). As a rule, processors keep the minimum necessary in the kernel stack after interruption. The interrupt handler will then save any additional registers that it wants to use and restore them before exiting. Then, the RETURN FROM INTERRUPT or EXCEPTION command restores the registers automatically saved by the interrupt.
This description does not imply changes in the process.
If the interrupt descriptor decides to change the process, it saves the current state of the register ("process context" - most processors have one instruction for this. In the Intel area, you may need several instructions), and then runs another instruction to load the process context of the new process.
To answer the title question, “What is the kernel stack that is used for?”, It is used whenever the processor is in kernel mode. If the kernel did not have a stack that is protected from user access, system integrity may be compromised. The core kernel tends to be very small.
To answer the second question: "What exactly is the task of saving registers for both the kernel stack and the process structure, and what is the need for?"
They serve two different purposes. Stored registers in the kernel stack are used to exit kernel mode. The context process block stores the entire set of registers for modifying processes.
I think your misunderstanding comes from the wording of your source, which assumes that all registers are stored on the stack when entering kernel mode, and not just the minimum number of registers needed to switch kernel mode. The system usually saves only what it needs to return to user mode (and can use the same information to return to the original process in another context switch, depending on the system). A change in the process context saves all registers.
Edits answers to additional questions:
If the interrupt handler should automatically use a register that the CPU did not save automatically with the interrupt, it pushes them onto the kernel stack when writing and pushes them when they exit. The interrupt handler must explicitly save and restore any [general] registers that it uses. The process context block is not affected for this.
The process context block only changes as part of the actual context switch.
Example:
Suppose we have a processor with a software counter, a stack pointer, processor status and 16 general registers (I do not know that such a system really exists) and that the same SP is used for all modes.
The hardware pushes the PC, SP and PS to the stack, loads the SP with the address of the kernel mode stack and the PC from the interrupt handler (from the processor manager table).
- The interrupt handler is called.
The writer of the handler decides that he is going to R0-R3 for us. So, the first lines of the handler have:
Push R0 ; on to the kernel mode stack Push R1 Push R2 Push R3
The interrupt handler does whatever it wants to do.
Cleanup
The interrupt handler writer should do:
Pop R3 Pop R2 Pop R1 Pop R0 REI ; Whatever the system return from interrupt or exception instruction is.
- Hardware overcomes
Restores PS, PC and SP from the kernel mode stack, then resumes execution where it was before the interrupt.
I made my own processor for simplification. Some processors have long instructions that are interrupted (for example, moving a block symbol). Such instructions often use registers to maintain their context. In such a system, the processor should automatically save any registers that are used to maintain context within the instruction.
An interrupt handler does not trick a process context block if it does not modify processes.