Context Switch Animation Another one by Anastasia.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Calling sequence ESP.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Project 2 Roadmap. Background – Context Switching One processor and multiple threads running concurrently – How?!! Give each thread a small time quantum.
Context switch in Linux
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)
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
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.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Linux Operating System
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
6.828: PC hardware and x86 Frans Kaashoek
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
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.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
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.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
What You Need to Know for Project Three Steve Muckle Wednesday, February 19 th Spring 2003.
1 Assembly Language: Function Calls Jennifer Rexford.
Microprocessor, Programming & Interfacing Tutorial 2- Module 3.
What You Need to Know for Project Three Dave Eckhardt Steve Muckle.
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
Assembly function call convention
Reading Condition Codes (Cont.)
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Assembly language.
C function call conventions and the stack
Operating Systems Engineering
Processes.
CS 5204 Operating Systems Processes and Threads Godmar Back.
CS-401 Computer Architecture & Assembly Language Programming
Anton Burtsev February, 2017
ICS143A: Principles of Operating Systems Lecture 13: Context switching
Homework Reading Machine Projects Labs PAL, pp ,
Aaron Miller David Cohen Spring 2011
Basic Microprocessor Architecture
Machine-Level Programming 4 Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Discussions on HW2 Objectives
Tutorial No. 11 Module 10.
Computer Architecture CST 250
Discussions on HW2 Objectives
X86 Assembly Review.
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 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 ebp return 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 gg ebp 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 gg ebp return 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); //MACRO return 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 gg ebp return 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 respectively 2.Saves esi, edi and ebp on the stack 3.Saves esp in prev  thread.esp eax thread field: esp0 eip esp … thread field: esp0 eip esp …

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 gg ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK The next step that switch_macro does is to load next  thread.esp in esp 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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.eip ebp return 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: esp0 eip esp … thread field: esp0 eip esp … PREVPROCESSSTACKPREVPROCESSSTACK Kernel mode stack of process 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 NEXTPROCESSSTACKNEXTPROCESSSTACK 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