Context switch in Linux
Download
Report
Transcript Context switch in Linux
Context switch in Linux
©Gabriel Kliot, Technion
1
Context switch in Linux – OS course
Memory layout – general picture
Stack
Stack
Process X user memory
Stack
Stack
Process Y user memory
Process Z user memory
Stack
Stack
tss->esp0
task_struct
task_struct
Process X kernel
stack and task_struct
Process Y kernel
stack and task_struct
TSS of
CPU i
task_struct
Process Z kernel stack
and task_struct
Kernel memory
©Gabriel Kliot, Technion
2
Context switch in Linux – OS course
#1 – kernel stack after any system call, before context switch
prev
ss
User Stack
esp
eflags
cs
…
TSS
eip
…
…
tss->esp0
Schedule() function frame
User Code
orig_eax
es
esp
ds
eax
ebp
task_struct
edi
esi
thread.esp0
Saved on the
kernel stack during
a transition to
kernel mode by a
jump to interrupt
and by SAVE_ALL
macro
edx
ecx
©Gabriel Kliot, Technion
3
ebx
Context switch in Linux – OS course
#2 – stack of prev before switch_to macro in schedule() func
prev
…
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Return address to schedule()
TSS
Old (schedule’s()) EBP
…
tss->esp0
esp
task_struct
thread.eip
thread.esp
thread.esp0
©Gabriel Kliot, Technion
4
Context switch in Linux – OS course
#3 – switch_to: save esi, edi, ebp on the stack of prev
prev
…
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Return address to schedule()
TSS
Old (schedule’s()) EBP
tss->esp0
…
ESI
EDI
EBP
esp
task_struct
thread.eip
thread.esp
thread.esp0
©Gabriel Kliot, Technion
5
Context switch in Linux – OS course
#4 – switch_to: save esp in prev->thread.esp
prev
…
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Return address to schedule()
TSS
Old (schedule’s()) EBP
tss->esp0
…
ESI
EDI
EBP
esp
task_struct
thread.eip
thread.esp
thread.esp0
©Gabriel Kliot, Technion
6
Context switch in Linux – OS course
#5 – switch_to: load next->thread.esp into esp
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
…
ESI
ESI
EDI
EDI
EBP
EBP
esp
©Gabriel Kliot, Technion
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
7
$1f
Context switch in Linux – OS course
#6 – switch_to: save return address in the prev->thread.eip
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
…
ESI
ESI
EDI
EDI
EBP
EBP
esp
$1f
©Gabriel Kliot, Technion
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
8
$1f
Context switch in Linux – OS course
#7 – switch_to: save return address on the stack of next
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
…
ESI
ESI
EDI
EDI
EBP
EBP
esp
$1f
©Gabriel Kliot, Technion
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
9
$1f
$1f
Context switch in Linux – OS course
#8 – __switch_to func: save the base of next’s stack in TSS
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
…
ESI
ESI
EDI
EDI
EBP
EBP
esp
$1f
©Gabriel Kliot, Technion
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
10
$1f
$1f
Context switch in Linux – OS course
#9 – back in switch_to: eip points to $1f instruction label
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
ESI
EDI
EBP
$1f
©Gabriel Kliot, Technion
…
ESI
eip
EDI
1:
esp
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
11
EBP
$1f
Context switch in Linux – OS course
#10 – switch_to: restore esi, edi, ebp from the stack of next
prev
…
next
…
Schedule() saved EAX, ECX, EDX
Schedule() saved EAX, ECX, EDX
Arguments to contex_switch()
Arguments to contex_switch()
Return address to schedule()
Return address to schedule()
TSS
Old (schedule’s()) EBP
Old (schedule’s()) EBP
tss->esp0
…
…
esp
ESI
EDI
EBP
$1f
©Gabriel Kliot, Technion
task_struct
task_struct
thread.eip
thread.esp
thread.esp0
thread.eip
thread.esp
thread.esp0
12
$1f
Context switch in Linux – OS course