Task-Switching How the x86 processor assists with context-switching among multiple program-threads.

Slides:



Advertisements
Similar presentations
Processor Privilege-Levels
Advertisements

Intel MP.
Microprocessors system architectures – IA32 real and virtual-8086 mode Jakub Yaghob.
CSC 221 Computer Organization and Assembly Language
IA32 Paging Scheme Introduction to the Pentium’s support for “virtual” memory.
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
Operating Systems: Segments 1 Segmentation Hardware Support single user program system: – wish somehow to relocate address 0 to after operating system.
IA-32 Processor Architecture
Page-Faults in Linux How can we study the handling of page-fault exceptions?
Context Switch Animation Another one by Anastasia.
Exceptions and Interrupts How does Linux handle service- requests from the cpu and from the peripheral devices?
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
Processor Privilege-Levels How the x86 processor accomplishes transitions among its four distinct privilege-levels.
Deferred segment-loading An exercise on implementing the concept of ‘load-on-demand’ for the program-segments in an ELF executable file.
Venturing into protected-mode A first look at the CPU registers and instructions which provide the essential supporting infrastructure.
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language Advantages 1. It reveals the secret of your computer’s hardware and software. 2. Speed. 3. Some special applications and occasions. Disadvantages.
Task-Switching How the x86 processor assists with context-switching among multiple program-threads.
Segment-registers’ hidden bits A look at how segmentation attributes are cached within the CPU’s segment-registers.
Deferred segment-loading An exercise on implementing the concept of ‘load-on-demand’
Venturing into protected-mode
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 Fall 2012 Lecture 15: Protected mode intro.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
8086 emulation Using Virtual-8086 mode to execute real-mode procedures in a protected-mode environment.
UNIT 2 Memory Management Unit and Segment Description and Paging
80386DX.
Intel MP (32-bit microprocessor) Designed to overcome the limits of its predecessor while maintaining the software compatibility with the.
6.828: PC hardware and x86 Frans Kaashoek
The Pentium Processor.
The Pentium Processor Chapter 3 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Interrupts in the guest VM A look at the steps needed to “reflect” hardware interrupts back into the ROM-BIOS for servicing.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
1 ICS 51 Introductory Computer Organization Fall 2009.
1 i386 Memory Management Professor Ching-Chi Hsu 1998 年 4 月.
Virtual 8086 Mode  The supports execution of one or more 8086, 8088, 80186, or programs in an protected-mode environment.  An 8086.
Microprocessor system architectures – IA32 tasks Jakub Yaghob.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
6. HAL and IDT ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section  Hardware Abstraction Layer ◦ Section
1 Microprocessors CSE Protected Mode Memory Addressing Remember using real mode addressing we were previously able to address 1M Byte of memory.
X86 Assembly Language We will be using the nasm assembler (other assemblers: MASM, as, gas)
Chapter 2 The Microprocessor Architecture Microprocessors prepared by Dr. Mohamed A. Shohla.
Memory Management Unit and Segment Description and Paging
Information Security - 2. Descriptor Tables Descriptors are stored in three tables: – Global descriptor table (GDT) Maintains a list of most segments.
BITS Pilani Pilani Campus Pawan Sharma Lecture / ES C263 INSTR/CS/EEE F241 Microprocessor Programming and Interfacing.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
The Microprocessor & Its Architecture A Course in Microprocessor Electrical Engineering Department Universitas 17 Agustus 1945 Jakarta.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Microprocessor Architecture
Assembly language.
16.317: Microprocessor System Design I
Operating Systems Engineering
Microprocessor Systems Design I
Microprocessor Systems Design I
Anton Burtsev February, 2017
Anton Burtsev February, 2017
Microprocessor Systems Design I
Basic Microprocessor Architecture
x86 segmentation, page tables, and interrupts
Subject Name: Microprocesor Subject Code: 10CS45
Chapter 2: The Microprocessor and its Architecture
CS 301 Fall 2002 Computer Organization
The Microprocessor & Its Architecture
Assembly Language (CSW 353)
Computer Architecture CST 250
CSC 497/583 Advanced Topics in Computer Security
Presentation transcript:

Task-Switching How the x86 processor assists with context-switching among multiple program-threads

Program Model Programs consist of data and instructions Data consists of constants and variables, which may be ‘persistent’ or ‘transient’ Instructions may be ‘private’ or ‘shared’ These observations lead to a conceptual model for the management of programs, and to special processor capabilities that assist in supporting that conceptual model

Conceptual Program-Model TEXT DATA BSS STACK heap runtime library Private Instructions (persistent ) Initialized Data (persistent) Uninitialized Data (persistent) Private Data (transient) Shared Instructions and Data (persistent) created at compile time created during runtime

Task Isolation The CPU is designed to assist the system software in isolating the private portions of one program from those of another while they both are residing in physical memory, while allowing them also to share certain instructions and data in a controlled way This ‘sharing’ includes access to the CPU, whereby the tasks take turns at executing

Multi-tasking TEXT DATA BSS heap STACK TEXT DATA BSS heap STACK shared runtime library user-space (ring3) supervisor-space (ring0) TSS 1TSS 2 Task #1 Task #2 GDTIDT IDTR GDTR CS DS SS SP IP TR

Context-Switching The CPU can perform a ‘context-switch’ to save the current values of all its registers (in the memory-area referenced by the TR register), and to load new values into all its registers (from the memory-area specified by a new Task-State Segment selector) There are four ways to trigger this ‘task- switch’ operation on x86 processors

How to cause a task-switch Use an ‘ljmp’ instruction (long jump): ljmp $task_selector, $0 Use an ‘lcall’ instruction (long call): lcall $task_selector, $0 Use an ‘int-n’ instruction (with a task-gate): int $0x80 Use an ‘iret’ instruction (with NT=1): iret

‘ljmp’ and ‘lcall’ These instructions are similar – they both make use of a ‘selector’ for a Task-State Segment descriptor Base[ ]Limit[ ] Base[31..24] DPLDPL Base[23..16]type0P TSS Descriptor-Format type: 16bitTSS( 0x1=available or 0x3=busy) or 32bitTSS( 0x9=available or 0xB=busy) Limit [19..16] AVLAVL

The two TSS formats Intel introduced the Task-State Segment in the processor (used in IBM-PC/AT) The CPU had a 16-bit architecture Later Intel introduced its processor which had a 32-bit architecture requiring a larger and more elaborate format for its Task-State Segment data-structure The 286 TSS is now considered ‘obsolete’

The TSS format link sp0 ss0 sp1 ss1 sp2 ss2 IP FLAGS AX CX DX BX SP BP SI DI ES CS SS DS LDTR 22 words 16-bits = field is ‘static’ = field is ‘volatile’

The TSS format link esp0 ss0 esp1 ss1 esp2 ss2 PTDB EIP ss0 ES CS SS DS FS GS LDTR IOMAPTRAP EFLAGS EAX ECX EDX EBX ESP EBP ESI EDI I/O permission bitmap = field is ‘static’ = field is ‘volatile’ = field is ‘reserved’ longwords 32-bits

Which to use: ‘ljmp’ or ‘lcall’? Use ‘ljmp’ to switch to a different task in case you have no intention of returning Use ‘lcall’ to switch to a different task in case you want to ‘return’ to this task later The CPU treats ‘ljmp’ and ‘lcall’ differently in regard to the TSS, GDT and EFLAGS

No Task Reentrancy! Since each task has just one ‘save area’ (in its TSS), it must not not be permitted for a task to be recursively reentered! The CPU enforces this prohibition using a ‘busy’ bit within each task’s TSS descriptor Whenever the TR register is loaded with a new selector-value, the CPU checks to be sure the task isn’t already ‘busy’; if it’s not, the task is entered, but gets marked ‘busy’

Task-Nesting But it’s OK for one task to be nested within another, and another, and another… TSS #4 TR LINK current TSS #3 LINK TSS #2 LINK TSS #1 LINK lcall initial TSS

The NT-bit in FLAGS When the CPU switches to a new task via an ‘lcall’ instruction, it sets NT=1 in FLAGS (and it leaves the old TSS marked ‘busy’) The new task can then ‘return’ to the old task by executing an ‘iret’ instruction (the old task is still ‘busy’, so returning to it with an ‘lcall’ or an ‘ljmp’ wouldn’t be possible)

Task-switch Semantics Fieldljmp effectlcall effectiret effect new busy-bitchanges to 1 changes to 1 stays = 1 old busy-bitis clearedstays = 1is cleared new NT-flagIs clearedIs set to 1no change old NT-flagno change is cleared new LINK-fieldno changenew valueno change old LINK-fieldno change

Task-Gate Descriptor It is also possible to trigger a task-switch with a software or hardware interrupt, by using a Task-Gate Descriptor in the IDT Task-State Segment Selector DPLDPL P type (=0x5) 0 Task-Gate Descriptor Format

‘Threads’ versus ‘Tasks’ In some advanced applications, a task can consist of multiple execution-threads Like tasks, threads take turns executing (and thus require ‘context-switching’) CPU doesn’t distinguish between ‘threads’ and ‘tasks’ – context-switching semantics are the same for both Difference lies in ‘sharing’ of data/code

A task with multiple threads CODE 1CODE 2 DATA 1 STACK 1STACK 2 heap TEXT (some shared, some private) DATA (some shared, some private) STACKS (each is thread-private) DATA 2 user-space (ring3) supervisor-space (ring0) TSS 1TSS 2 Each thread has its own TSS-segment

Demo program: ‘twotasks.s’ We have constructed a simple demo that illustrates the CPU task-switching ability It’s one program, but with two threads Everything is in one physical segment, but the segment-descriptors create a number of different overlapping ‘logical’ segments One task is the ‘supervisor’ thread: it ‘calls’ a ‘subordinate’ thread (to print a message)

A thread could use an LDT To support isolation of memory-segments among distinct tasks or threads, the CPU allows use of ‘private’ descriptor-tables Same format for the segment-descriptors But selectors use a Table-Indicator bit Descriptor-table index fieldRPL TITI Format of a segment-selector (16-bits) TI = Table-Indicator (0 = GDT, 1 = LDT) RPL = Requested Privilege-Level

LDT descriptors Each Local Descriptor Table is described by its own ‘system’ segment-descriptor in the Global Descriptor Table Base[ ]Limit[ ] Base[31..24] DPLDPL Base[23..16]type0P LDT Descriptor-Format Type-field: the ‘type’ code for any LDT segment-descriptor is 0x2 Limit [19..16] AVLAVL

In-class Exercise #1 In our ‘twotasks.s’ demo, the two threads will both execute at privilege-level zero An enhanced version of this demo would have the ‘supervisor’ (Thread #1) execute in ring 0 and the ‘subordinate’ (Thread #2) execute in ring 3 Can you modify the demo-program so it incorporates that suggested improvement?

More enhancements? The demo-program could be made much more interesting if it used more than one subordinate thread, and if the supervisor thread took turns repeatedly making calls to each subordinate (i.e., ‘time-sharing’) You can arrange for a thread to be called more than once by using a ‘jmp’ after the ‘iret’ instruction (to re-execute the thread)

In-class Exercise #2 Modify the demo so it has two subordinate threads, each of which prints a message, and each of which can be called again and again (i.e., add a jmp-instruction after iret): begin:; entry-point to the thread... iret jmp begin