context switch animation

Post on 04-Feb-2016

35 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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 Presentation

TRANSCRIPT

Context Switch Animation

Another one by Anastasia

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

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

.

.

.

.

.

.

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

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

• 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 !!!!!!

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.

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

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.

1. In Kernel Mode function schedule() is called.

Context switch process

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

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)

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

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

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;

}

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

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…

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

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 !!!

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

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

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

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

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

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

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

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

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

top related