context switch animation
DESCRIPTION
Context Switch Animation. Another one by Anastasia . User Mode Stack of process P1, bottom of the stack, low addresses (0xFF) here. Stack grows down ↓. Process P1 is running in user mode. Doing some userFunction(…) Lets assume that userFunction() was called by main() - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/1.jpg)
Context Switch Animation
Another one by Anastasia
![Page 2: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/2.jpg)
hardware responsibility, call instruction, called by main()
parameters of userFunction() pushed on the stack by main()
the return address (the next instruction of main() to perform)
old (main()’s) ebp
esi, edi, ebx saved by callee userFunction()
local variables of userFunction()
eax, ecx, edx saved by caller main()
.
.
.
.
.
.
• Process P1 is running in user mode. Doing some userFunction(…)
• Lets assume that userFunction() was called by main()
• When P1 does so, it is not aware about task descriptor, kernel stack, etc. Nothing related to OS functionality
User Mode Stack of process P1, bottom of the stack, low
addresses (0xFF) here. Stack grows down ↓ .
main()
responsibility
userFunction()
responsibility
![Page 3: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/3.jpg)
parameters of userFunction() pushed on the stack by main()
the return address (the next instruction of main() to perform)
old (main()’s) ebp
esi, edi, ebx saved by callee userFunction()
local variables of userFunction()
eax, ecx, edx saved by caller main()
.
.
.
.
.
.
![Page 4: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/4.jpg)
parameters of userFunction() pushed on the stack by main()
the return address (the next instruction of main() to perform)old (main()’s) ebp
esi, edi, ebx saved by callee userFunction()
local variables of userFunction()
eax, ecx, edx saved by caller main()
.
.
.
• Now, inside the code of userFunction() there is a call for operation system interface – wait()
• wait() is a wrapper function and is called as regular function
![Page 5: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/5.jpg)
parameters of userFunction() pushed on the stack by main()
the return address (the next instruction of main() to perform)
old (main()’s) ebp
esi, edi, ebx saved by callee userFunction()
local variables of userFunction()
eax, ecx, edx saved by caller main()
eax, ecx, edx saved by caller userFunction() if needed
.
.
.
• Now, inside the code of userFunction() there is a need to call operation system interface – wait()
• wait() is a wrapper function and is called as regular function
(no parameters needed to wait()) save return address here
esi, edi, ebx saved by callee wait() if needed
old (userFunction()’s) ebp
userFunction()
responsibility
local variables of wait() if they exist
wait()
responsibility
![Page 6: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/6.jpg)
• wait() is going to invoke an operation system programmable intercept – system call
• wait() puts the relevant value of the system call number to the eax register
• wait() invokes the assembly language instruction: int $0x80
• Because of this hardware operation we leave the User Mode and we get to Kernel Mode!
parameters of userFunction() pushed on the stack by main()
the return address (the next instruction of main() to perform)
old (main()’s) ebp
esi, edi, ebx saved by callee userFunction()
local variables of userFunction()
eax, ecx, edx saved by caller main()
eax, ecx, edx saved by caller userFunction() if needed
.
.
.
(no parameters needed to wait()) save return address here
esi, edi, ebx saved by callee wait() if needed
old (userFunction()’s) ebp
local variables of wait() if they exist !!!!!!
![Page 7: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/7.jpg)
old eflags, cs, eip
old (user mode’s) ss, esp
.
.
.
.
.
.
• Process P1 is now running in Kernel Mode
• Registers ss, esp, eflags, cs, eip are getting new values after their old values were saved on the Kernel stack – this is all is done by one single assembler instruction int
Kernel Mode Stack of process P1, bottom of the stack
Hardware responsibility, int $0x80 assembler instruction called by wait() Here we already run the code of
system call handler interrupt, which is one for all system calls.
![Page 8: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/8.jpg)
old eflags, cs, eip
old eax (system call number)
old es, ds, eax, ebp, edi, esi, edx, ecx, ebx saved by SAVE_ALL macro
No need in caller-save registers, no parameters needed to sys_wait(). Return address.
old (user mode’s) ss, esp
.
.
.
.
.
.
Kernel Mode Stack of process P1, bottom of the stack
Hardware responsibility, int $0x80 assembler instruction called by wait() Here we already run the code of
system call handler interrupt, which is one for all system calls.
System call handler responsibility
User Stack
User Code
esp
eip
Kernel Code
Task descriptor of process P1
call *sys_call_table(0, %eax, 4)
gg
![Page 9: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/9.jpg)
old eflags, cs, eip
old eax (system call number)
old es, ds, eax, ebp, edi, esi, edx, ecx, ebx saved by SAVE_ALL macro
No need in caller-save registers, no parameters needed to sys_wait(). Return address.
old ebp
old (user mode’s) ss, esp
esi, edi saved if needed
.
.
.
.
.
.
local variables of sys_wait()
System call handler responsibility
User Stack
User Code
esp
eip
Kernel Code
Task descriptor of process P1
gg
OS function sys_wait() responsibility
ebp
Now assume inside sys_wait() we need to do a context switch.Thus, function schedule() have to be called.
![Page 10: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/10.jpg)
1. In Kernel Mode function schedule() is called.
Context switch process
![Page 11: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/11.jpg)
Call for function schedule() is a regular function call.
old eflags, cs, eip
old eax (system call number)
old es, ds, eax, ebp, edi, esi, edx, ecx, ebx saved by SAVE_ALL macro
No need in caller-save registers, no parameters needed to sys_wait(). Return address.
old ebp
old (user mode’s) ss, esp
esi, edi saved if needed
.
.
.
.
.
.
local variables of sys_wait()esp
eip
Kernel Code
Task descriptor of process P1
gg
ebp
![Page 12: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/12.jpg)
old eflags, cs, eip
old eax (system call number)
old es, ds, eax, ebp, edi, esi, edx, ecx, ebx saved by SAVE_ALL macro
No need in caller-save registers, no parameters needed to sys_wait(). Return address.
old ebp
old (user mode’s) ss, esp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
gg
ebpreturn adress to sys_wait()
old ebp
OS function sys_wait() responsibility
old esi, edi if needed
local variables of schedule()
prev
next
OS function schedule() responsibility
• Function schedule() chooses process P2 to get the CPU after P1.
• Then calls for: inline task_t* context_switch(task_t *prev, task_t *next)
![Page 13: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/13.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses the process to switch to, then inline function context_switch(…) is called.
Context switch process
![Page 14: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/14.jpg)
old eflags, cs, eip
old eax (system call number)
old es, ds, eax, ebp, edi, esi, edx, ecx, ebx saved by SAVE_ALL macro
No need in caller-save registers, no parameters needed to sys_wait(). Return address.
old ebp
old (user mode’s) ss, esp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
ggebp
return adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
OS function schedule() responsibility
Task descriptor of process P2
![Page 15: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/15.jpg)
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
ggebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
Task descriptor of process P2
Function context_switch() is a inline function!inline task_t *context_switch(task_t *prev, task_t *next) {
switch_mm....switch_to(prev, next, prev); //MACROreturn prev;
}
![Page 16: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/16.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses process to switch to, then inline function context_switch(…) is called.
3. Mainly context_switch(…) is calling switch_to(…) macro
Context switch process
![Page 17: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/17.jpg)
edx
old ebp
esi, edi saved if needed, ebx is not going to be chnaged
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
ggebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
Here is what switch_to macro does:1. Saves the values of prev and next in the eax and edx respectively2. Saves esi, edi and ebp on the stack3. Saves esp in prevthread.esp
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
![Page 18: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/18.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses process to switch to, then inline function context_switch(…) is called.
3. Mainly context_switch(…) is calling switch_to(…) macro
4. switch_to(…) first saves the registers of the previous process in the stack and task descriptor.
Context switch process
![Page 19: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/19.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
ggebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
The next step that switch_macro does is to load nextthread.esp in espContext switch !!!Context switch !!!
![Page 20: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/20.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses process to switch to, then inline function context_switch(…) is called.
3. Mainly context_switch(…) is calling switch_to(…) macro
4. switch_to(…) first saves the registers of the previous process in the stack and task descriptor.
5. Then switch_to moves esp register to point to the next processes kernel stack (stack switch = context switch)
Context switch process
![Page 21: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/21.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of switch_to macro’s code: movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eip jmp __switch_to 1: popl %ebp popl %edi popl %esi
![Page 22: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/22.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of switch_to macro’s code: movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eip jmp __switch_to 1: popl %ebp popl %edi popl %esi
the address of “popl %ebp” instruction
![Page 23: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/23.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses process to switch to, then inline function context_switch(…) is called.
3. Mainly context_switch(…) is calling switch_to(…) macro
4. switch_to(…) first saves the registers of the previous process in the stack and task descriptor.
5. Then switch_to moves esp register to point to the next processes kernel stack (stack switch = context switch)
6. eip of the previous process is saved in task descriptor as pointing to label $1, eip of the next process is loaded on the stack
Context switch process
![Page 24: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/24.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of switch_to macro’s code: movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eip jmp __switch_to 1: popl %ebp popl %edi popl %esi
the address of “popl %ebp” instruction
![Page 25: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/25.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of __switch_to function’s code:/* save special registers */tss->esp0 = next->esp0;…movl %fs, prev->fs (movl %gs, prev->gs)if (prev->fs | prev->gs | next->fs | next->gs) { .. movl next->fs, %fs (movl next->gs, %gs) ..}/* load debug registers, load IO permission bitmap */return;
the address of “popl %ebp” instruction
![Page 26: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/26.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of switch_to macro’s code: movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eip jmp __switch_to 1: popl %ebp popl %edi popl %esi
the address of “popl %ebp” instruction
![Page 27: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/27.jpg)
1. In Kernel Mode function schedule() is called.
2. schedule() chooses process to switch to, then inline function context_switch(…) is called.
3. Mainly context_switch(…) is calling switch_to(…) macro
4. switch_to(…) first saves the registers of the previous process in the stack and task descriptor
5. Then switch_to moves esp register to point to the next processes kernel stack (stack switch = context switch)
6. eip of the previous process is saved in task descriptor as pointing to label 1, eip of the next process is loaded on the stack
7. By jumping and returning from __switch_to function we load the address of label 1 into processor’s eip register
Context switch process
![Page 28: Context Switch Animation](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a03550346895dc74e30/html5/thumbnails/28.jpg)
edx
old ebp
esi, edi saved if needed
.
.
.
local variables of sys_wait()
save eax, ecx, edx if needed
esp
eip
Kernel Code
Task descriptor of process P1
movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eipebpreturn adress to sys_wait()
old ebp
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
Task descriptor of process P2
eax
thread field:esp0eipesp…
thread field:esp0eipesp…
PREV
PROCESS
STACK
Kernel mode stack ofprocess P2, which
was chosen to be next
… ↓esi, edi saved if needed
local variables of sys_wait()
save eax, ecx, edx if needed
return adress to sys_wait()
old ebp (saved by schedule())
old esi, edi if needed
local variables of schedule()
prev
next
esi, edi and ebp
NEXT
PROCESS
STACK
Part of switch_to macro’s code: movl %esp, prev->thread.esp movl next->thread.esp, %esp movl $1f, prev->thread.eip pushl next->thread.eip jmp __switch_to 1: popl %ebp popl %edi popl %esi
the address of “popl %ebp” instruction