What is the kernel stack used for?

Below is a description that I read about context switching between process A and process B. I don’t understand what the kernel stack is used for. This is assumed to be a stack for each process. And the description that I read says about saving registers A to the kernel stack A, as well as saving the registers A in the structure of the process A. What exactly is the purpose of saving the registers both in the kernel stack and in the structure of the process and why is there a need for both?

The context switch is conceptually simple: all OSs need to save several register values ​​for the current process (on its kernel stack, for example) and restore several for a quickly executed process (from its kernel stack). Thus, Thus, the OS ensures that when the return-from-trap command is final, instead of returning to the running process, the system resumes the execution of another process ...

Process A runs and then interrupts with a timer interrupt. The device saves its registers (on the kernel stack) and enters the kernel (transition to kernel mode). In the timer interrupt handler, the OS decides to switch from starting process A to process B. At this point, it calls the switch () procedure, which carefully stores the current values ​​(into the structure of process A), restores the registers of process B (from entering it into the structure process), and then switches contexts, in particular by changing the stack pointer to use Bs (not As). Finally, the OS returns from a trap that restores the Bs registers and starts it.

+6
source share
2 answers

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.

  • Interruption occurs.

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).

  1. 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 
  1. The interrupt handler does whatever it wants to do.

  2. 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. 
  1. 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.

+4
source

It’s hard to talk about how the OS works “under the hood,” because it depends on how the equipment works. In addition, the terminology is not very standardized.

My assumption is that in a “process structure record” a writer means what is usually called the “context” of a process and which contains a copy of each register. The interrupt code cannot immediately save the registers to this structure, because in doing so it will have to use (and therefore modify) the registers. That's why he needs to save several registers, enough for him to be able to do this work, somewhere right away, for example, where the stack pointer indicates which author calls the "kernel stack".

Depending on the architecture, it can be one stack or separate for each process.

-1
source

Source: https://habr.com/ru/post/987809/


All Articles