Context switch in Linux

Slides:



Advertisements
Similar presentations
CS 423 – Operating Systems Design Lecture 9 –Context Switches, Coordination Roy Campbell Fall 2010.
Advertisements

Calling sequence ESP.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
Context Switch Animation Another one by Anastasia.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
‘struct sigcontext’ On using Linux’s signaling mechanism for debugqing application programs.
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
Project 2 Roadmap. Background – Context Switching One processor and multiple threads running concurrently – How?!! Give each thread a small time quantum.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Linux Operating System 許 富 皓. 2 Chapter 3 Processes.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Spring 2014 Lecture 4: x86 memory.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
Linux Operating System
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
By – Jayant Upadhyay 2003CS50214 Pankaj K. Sharma 2003CS50219
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
1 Linux Operating System 許 富 皓. 2 Processes Switch.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Chapter 4 Process Abstraction Chien-Chung Shen CIS, UD
Microprocessor system architectures – IA32 tasks Jakub Yaghob.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Compiler Construction Code Generation Activation Records
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
Microprocessor, Programming & Interfacing Tutorial 2- Module 3.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Virtualizing the CPU: Processes 1. How to provide the illusion of many CPUs? CPU virtualizing – The OS can promote the illusion that many virtual CPUs.
Chapter Overview General Concepts IA-32 Processor Architecture
Exploiting & Defense Day 1 Recap
Instructions for test_function
Assembly function call convention
Assembly language.
C function call conventions and the stack
Operating Systems Engineering
Processes.
Anton Burtsev February, 2017
ICS143A: Principles of Operating Systems Lecture 13: Context switching
Exploiting & Defense Day 2 Recap
Aaron Miller David Cohen Spring 2011
Assembly IA-32.
x86 segmentation, page tables, and interrupts
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Procedure Calls CSE 378 – Section 3.
Tutorial No. 11 Module 10.
Computer Architecture CST 250
Low-Level Thread Dispatching on the x86
System Calls System calls are the user API to the OS
“Way easier than when we were students”
Computer Architecture and System Programming Laboratory
Presentation transcript:

Context switch in Linux ©Gabriel Kliot, Technion

Memory layout – general picture Stack Stack Stack Process X user memory Process Y user memory Process Z user memory Stack Process X kernel stack and task_struct task_struct Stack Process Y kernel stack and task_struct task_struct Stack Process Z kernel stack and task_struct task_struct TSS of CPU i tss->esp0 Kernel memory ©Gabriel Kliot, Technion

©Gabriel Kliot, Technion #1 – kernel stack after any system call, before context switch prev ss esp eflags cs eip orig_eax es ds eax ebp edi esi edx ecx ebx User Stack User Code … TSS … … tss->esp0 Schedule() function frame esp Saved on the kernel stack during transition to kernel mode by int80 and by SAVE_ALL macro task_struct thread.esp0 ©Gabriel Kliot, Technion

#2 – stack of prev before switch_to macro in schedule() func … 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

#3 – switch_to: save esi, edi, ebp on the stack of 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

#4 – switch_to: save esp in prev->thread.esp … 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 – 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 thread.eip thread.esp thread.esp0 task_struct thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion

#6 – switch_to: save return address in the prev->thread.eip 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 thread.eip thread.esp thread.esp0 task_struct $1f thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion

#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 $1f esp task_struct thread.eip thread.esp thread.esp0 task_struct $1f thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion

#8 – __switch_to func: save the base of next’s stack in TSS prev next … … TSS tss->esp0 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() Old (schedule’s()) EBP Old (schedule’s()) EBP … … ESI ESI EDI EDI EBP EBP $1f esp task_struct thread.eip thread.esp thread.esp0 task_struct $1f thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion

#9 – back in switch_to: eip points to $1f instruction label prev next … … TSS tss->esp0 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() Old (schedule’s()) EBP Old (schedule’s()) EBP … … ESI ESI eip 1: EDI EDI EBP EBP esp task_struct thread.eip thread.esp thread.esp0 task_struct $1f thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion

#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 tss->esp0 Return address to schedule() Old (schedule’s()) EBP Old (schedule’s()) EBP … … esp ESI EDI EBP task_struct thread.eip thread.esp thread.esp0 task_struct $1f thread.eip $1f thread.esp thread.esp0 ©Gabriel Kliot, Technion