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

Slides:



Advertisements
Similar presentations
CPU Structure and Function
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
Tutorial 3 - Linux Interrupt Handling -
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Memory Management Paging &Segmentation CS311, CS350 & CS550.
Computer Organization and Assembly Languages Yung-Yu Chuang
Computer Organization and Architecture
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
IA-32 Processor Architecture
Context Switch Animation Another one by Anastasia.
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
Context switch in Linux
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
OS Spring’03 Introduction Operating Systems Spring 2003.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Using Two Queues. Using Multiple Queues Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Processor is faster.
1 OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
80386DX.
6.828: PC hardware and x86 Frans Kaashoek
Implementing Processes and Process Management Brian Bershad.
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Virtual 8086 Mode  The supports execution of one or more 8086, 8088, 80186, or programs in an protected-mode environment.  An 8086.
15-410, S’ Hardware Overview Jan. 19, 2004 Dave Eckhardt Bruce Maggs L04_Hardware “Dude, what were you thinking?”
LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Microprocessor system architectures – IA32 tasks Jakub Yaghob.
Processes & Threads Introduction to Operating Systems: Module 5.
AMD K-6 Processor Evaluation. Registers AMD-K6 Registers General purpose registers Segment registers Floating point registers MMX registers EFLAGS register.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
Chapter Overview General Concepts IA-32 Processor Architecture
Protection in Virtual Mode
Assembly language.
Process concept.
Memory Management Paging (continued) Segmentation
Operating Systems CMPSC 473
Operating Systems Engineering
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
Processes.
Anton Burtsev February, 2017
Anton Burtsev February, 2017
x86 segmentation, page tables, and interrupts
Memory Management Paging (continued) Segmentation
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
CS 301 Fall 2002 Computer Organization
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Lecture Topics: 11/1 General Operating System Concepts Processes
Assembly Language (CSW 353)
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Low-Level Thread Dispatching on the x86
System Calls System calls are the user API to the OS
Memory Management Paging (continued) Segmentation
Computer Architecture and System Programming Laboratory
Presentation transcript:

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

Context Switch PCBs move from queue to queue as they change state – Red circles indicate where context switches can occur

Context Switch Switch between one program control flow and another Context – Machine level: – integer/floating pt/stack/heap/PCB registers – status registers: overflow, branch – memory status: tlb, L1, L2, L3, cache, virtual memory status, addressing faults, memory ops – pipeline/hyperthread status – interrupts: status, pending

Instruction Seq for switch Hardware: – IA86 – heavy weight CALL, TSS context switch instructions change registers and stacks. Software: – Light weight, do it yourself register changes. – must change context prior to restore

Instruction Seq for switch Hybrid: – Hardware: change program counter/registers via interrupt or operation basic primitive – software must add any additional changed context after switch. – Restoring program counter using hardware primitive – must change context prior to restore

Simple software Context Switch) Switch(tCur,tNew) { /* Unload old thread */ TCB[tCur].regs.r7 = CPU.r7; … TCB[tCur].regs.r0 = CPU.r0; TCB[tCur].regs.sp = CPU.sp; TCB[tCur].regs.retpc = CPU.retpc; /*return addr*/ /* Load and execute new thread */ CPU.r7 = TCB[tNew].regs.r7; … CPU.r0 = TCB[tNew].regs.r0; CPU.sp = TCB[tNew].regs.sp; CPU.retpc = TCB[tNew].regs.retpc; return; /* Return to CPU.retpc */ }

Additional Context Switch Overhead Application State – stack, heap, sockets… Virtual Machine State – i/o and network support and VM mappings Virtual Memory State - vmtable, tlb cache, User Process, Thread State – thread/process stack, heap, ready queue, exception handling Kernel Process, Thread State – kernel thread/process stack, heap, interrupt vectors

Optimizing Context Switch Lazy context switching – wait until dispatch knows what is running next and then change minimized context. When switching between different protection domains, check parameters. Minimize protection checking – Light Remote Procedure Call – don’t check parameters if remain in same protection domain

Switch Details How many registers need to be saved/restored? – MIPS 4k: 32 Int(32b), 32 Float(32b) – Pentium: 14 Int(32b), 8 Float(80b), 8 SSE(128b),… – Sparc(v7): 8 Regs(32b), 16 Int regs (32b) * 8 windows = 136 (32b)+32 Float (32b) – Itanium: 128 Int (64b), 128 Float (82b), 19 Other(64b) retpc is where a return should jump to. – In reality, this is implemented as a jump Software switch is often implemented as assembly!

Intel Task Switching Instructions JMP, Call to a Task Segment S in a GDT (Global Descriptor Table) JMP, Call to a task-gate descriptor (contains a TSS) in the GDT, or current LDT Implicit call to an interrupt-handler task Implicit call to an exception-handler task A return (IRET) when the NT flag (Nested Task) is set. Used to return from interrupts.

Intel CALL Instruction This instruction can be used to execute four types of calls: Near Call — A call to a procedure in the current code segment (the segment currently pointed to by the CS register), sometimes referred to as an intrasegment call. Far Call — A call to a procedure located in a different segment than the current code segment, sometimes referred to as an inter-segment call. Inter-privilege-level far call — A far call to a procedure in a segment at a different privilege level than that of the currently executing program or procedure. Task switch — A call to a procedure located in a different task. See Chapter 7, “Task Management,” in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A, for information on performing task switches with the CALL

©Gabriel Kliot, Technion12 Context switch in Linux

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

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

©Gabriel Kliot, Technion15 prev TSS tss->esp0 esp #2 – stack of prev before switch_to macro in schedule() func task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() … …

©Gabriel Kliot, Technion16 TSS tss->esp0 #3 – switch_to: save esi, edi, ebp on the stack of prev task_struct thread.eip thread.esp thread.esp0 prev esp Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … …

©Gabriel Kliot, Technion17 TSS tss->esp0 #4 – switch_to:save esp in prev->thread.esp #4 – switch_to: save esp in prev->thread.esp esp Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 prev …

©Gabriel Kliot, Technion18 next TSS tss->esp0 #5 – switch_to:load next->thread.esp into esp #5 – switch_to: load next->thread.esp into esp prev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … esp $1f thread.eip thread.esp thread.esp0 task_struct … …

©Gabriel Kliot, Technion19 next TSS tss->esp0 prev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … esp $1f thread.eip thread.esp thread.esp0 task_struct … #6 – switch_to:save return address in the prev->thread.eip #6 – switch_to: save return address in the prev->thread.eip $1f …

©Gabriel Kliot, Technion20 next TSS tss->esp0 prev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … esp $1f thread.eip thread.esp thread.esp0 task_struct … $1f #7 – switch_to:save return address on the stack of next #7 – switch_to: save return address on the stack of next $1f …

©Gabriel Kliot, Technion21 nextprev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … esp $1f thread.eip thread.esp thread.esp0 task_struct … $1f #8 – __switch_to func:save the base of next’s stack in TSS #8 – __switch_to func: save the base of next’s stack in TSS TSS tss->esp0 …

©Gabriel Kliot, Technion22 nextprev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … esp $1f thread.eip thread.esp thread.esp0 task_struct … $1f #9 – back in switch_to:eip points to $1f instruction label #9 – back in switch_to: eip points to $1f instruction label eip 1: TSS tss->esp0 …

©Gabriel Kliot, Technion23 next TSS tss->esp0 prev Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() EDI ESI EBP … task_struct thread.eip thread.esp thread.esp0 Schedule() saved EAX, ECX, EDX Old (schedule’s()) EBP Arguments to contex_switch() Return address to schedule() … esp $1f thread.eip thread.esp thread.esp0 task_struct … $1f #10 – switch_to:restore esi, edi, ebp from the stack of next #10 – switch_to: restore esi, edi, ebp from the stack of next …

Where to look for more info The key function for context switching is __switch_to() in ocess_32.c. ocess_32.c The difficulties of optimizing context switching on i7: posts.php Intel processor manuals can be found here: See Page 120 for CALL instruction

Where to look for more info The key function for context switching is __switch_to() in x86/kernel/process_32.c. x86/kernel/process_32.c Reason given for software switching – difficult to recover from errors using hardware switch (particularly segment issues.)

Cost of Context Switch GHZ Pentium P4, approx 811 ns HW for interrupt, 184 ms address space switch, 67 ns to store/restore registers. As CPUs got faster, switches got faster but took relatively more time as clocks got faster. BUT, software switching vs hardware switching is used because can recover from errors. NOTE: context switching involves invalidating caches - cost varies and can be very large.

Arm Context switching “Context Switch Overheads for Linux on ARM Platforms” Francis M. David Jeffrey C. Carlyle Roy H. Campbell

Coordination Mutual Exclusion Signals – Buffering – Gang scheduling – Simultaneity – multiple processes waiting for two or more shared events Priorities State machine and other solutions – Atomicity and computation

Mutual Exclusion Turtles all the way down

Buffering ConsumingWaitingProducing Processes Consuming Processes Producing

Gang Scheduling Parallel Processes Rendezvous

Simultaneity Which transition fires? How to program?

Priorities Many Readers or Single Writer Writer Preference is many writer/readers waiting

State Machine Can always build a state machine representation of the synchrnoization condition? When can you do that using semaphores only?

CS Fall 2007 Summary Context Switch Coordination