Switch_to: This is a macro with three parameters Prev,next,last
Local variable Prev,next: The memory address that points to the process descriptor
The first is clear: Last and Prev is the same, with only for the sake of understanding convenience, can use two parameters prev,next. Because last is prev.
The SWITCH_TO macro is used for process switching, given the previous process struct pointer prev, and the process struct pointer that needs to be switched to next, switching from Prev to next.
However, in fact, the SWITCH_TO macro has three parameters, in addition to the two arguments above, there is a last parameter. And when you use SWITCH_TO macros, the prev and last are the same values, such as calling this macro:
Switch_to (Prev,next,prev).
What is this for?
Consider a scenario where process a switches to process B because the space for each process is different, so before switching, process a prev=a,next=b,last=a in space.
After some time, you need to switch back to process A, assuming that the current process is C, then prev=c,next=a,last=c for C.
Compare before and after two scenarios:
Process A before switching: prev=a,next=b,last=a
Process C before switching: prev=c,next=a,last=c
When you start switching from process C to process A, notice that the SWITCH_TO macro stores prev in the EAX register before switching, that is, before process C switches to process a, eax=c
After switching, it was clear that it came to the space of process A, so the prev,next,last pointer would go back to the point where process A was switched out, so prev=a,next=b,last=a, The EAX data remains the same (this is the case before the EAX content overwrites the prev of the A process's stack)
Before the SWITCH_TO macro returns, the data for the EAX register is stored in the last, so last=eax=c, in fact, is prev=c
Note that in the macro, the last place is essentially prev.
When I turn from C to a, how does a know which one of its previous processes is?
To make you understand, now make the following assumptions:
Add no last this parameter, then switch becomes: switch_to (prev, next);
When we return to the a process, prev points to itself and next points to B, so how do you know who the previous process of a is? is to not be able to get the C descriptor.
This time we add last,switch_to (prev, Next, prev), and the last parameter is an output parameter, so when Switch_to executes, Prev becomes the descriptor of process C.
This is achieved by MOVL%eax, last.
The key point is, why this eax is the C descriptor, which is because in the c->a from the time, Movl prev,%eax, this prev is C.
I personally think this is to facilitate understanding, otherwise, switch_to (prev, next), can also achieve the above functions, directly prev This parameter is when the input parameters, and when the output parameters can be.
The switch_to principle of the Linux kernel