Context Switch in Linux – OS Course Memory Layout – General Picture
Total Page:16
File Type:pdf, Size:1020Kb
ContextContext switchswitch inin LinuxLinux ©Gabriel Kliot, Technion 1 Context switch in Linux – OS course Memory layout – general picture Stack Stack Stack Process X user memory Process Y user memory Process Z user memory Stack Stack Stack tss->esp0 TSS of CPU i task_struct task_struct task_struct Process X kernel Process Y kernel Process Z kernel stack stack and task_struct stack and task_struct 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 … User Code eip TSS … orig_eax … tss->esp0 es Schedule() function frame esp ds eax Saved on the kernel stack during ebp a transition to task_struct kernel mode by a edi jump to interrupt and by SAVE_ALL esi macro edx thread.esp0 ecx ebx ©Gabriel Kliot, Technion 3 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … ESI ESI EDI EDI EBP EBP esp task_struct task_struct thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 7 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … ESI ESI EDI EDI EBP EBP esp task_struct task_struct $1f thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 8 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … ESI ESI EDI EDI EBP EBP esp $1f task_struct task_struct $1f thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 9 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … ESI ESI EDI EDI EBP EBP esp $1f task_struct task_struct $1f thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 10 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … ESI ESI EDI eip EDI EBP EBP 1: esp task_struct task_struct $1f thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 11 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() TSS Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … tss->esp0 … esp ESI EDI EBP task_struct task_struct $1f thread.eip thread.eip $1f thread.esp thread.esp thread.esp0 thread.esp0 ©Gabriel Kliot, Technion 12 Context switch in Linux – OS course.