Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Similar documents
Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Assembly Language: Function Calls" Goals of this Lecture"

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

X86 Stack Calling Function POV

Machine-level Programming (3)

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CPS104 Recitation: Assembly Programming

CPEG421/621 Tutorial

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Process Layout and Function Calls

CIT Week13 Lecture

Instruction Set Architecture

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CPSC W Term 2 Problem Set #3 - Solution

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

You may work with a partner on this quiz; both of you must submit your answers.

CS213. Machine-Level Programming III: Procedures

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine-Level Programming III: Procedures

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

COMP 210 Example Question Exam 2 (Solutions at the bottom)

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

CS241 Computer Organization Spring 2015 IA

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

x86 assembly CS449 Fall 2017

Second Part of the Course

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Machine Programming 3: Procedures

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

System Programming and Computer Architecture (Fall 2009)

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Program Exploitation Intro

Instruction Set Architecture

Instruction Set Architecture

Sungkyunkwan University

CSE 351: Week 4. Tom Bergan, TA

CS429: Computer Organization and Architecture

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Machine Program: Procedure. Zhaoguo Wang

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CSC 2400: Computing Systems. X86 Assembly: Function Calls

X86 Assembly -Procedure II:1

Systems I. Machine-Level Programming V: Procedures

Chapter 4! Processor Architecture!

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Lab 10: Introduction to x86 Assembly

Process Layout, Function Calls, and the Heap

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

CSC 8400: Computer Systems. Using the Stack for Function Calls

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

University of Washington

Compiler Construction D7011E

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

W4118: PC Hardware and x86. Junfeng Yang

CS61 Section Solutions 3

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

Giving credit where credit is due

Processes (Intro) Yannis Smaragdakis, U. Athens

The Hardware/Software Interface CSE351 Spring 2013

Machine-Level Programming II: Control and Arithmetic

Instructor: Alvin R. Lebeck

CSC 2400: Computer Systems. Using the Stack for Function Calls

Assignment 11: functions, calling conventions, and the stack

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Data Representa/ons: IA32 + x86-64

An Elegant Weapon for a More Civilized Age

Assembly Language: IA-32 Instructions

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Function Call Convention

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

CS642: Computer Security

Instruction Set Architectures

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Machine Level Programming II: Arithmetic &Control

CSCI 2021: x86-64 Control Flow

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Transcription:

Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved

The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018 Thomas W Doeppner All rights reserved

Adding More Stuff stack 1 stack 2 stack 3 mapped file 1 mapped file 2 mapped file 3 mapped file 117 bss & dynamic data text Operating Systems In Depth II 3 Copyright 2018 Thomas W Doeppner All rights reserved

Subroutines int main( ) { int i; int a; } i = sub(a, 1); return(0); int sub(int x, int y) { int i; int result = 1; for (i=0; i<y; i++) result *= x; return(result); } Operating Systems In Depth II 4 Copyright 2018 Thomas W Doeppner All rights reserved

Intel x86 (32-Bit): Subroutine Linkage args eip ebp saved registers local variables args eip ebp saved registers local variables stack frame ebp esp Operating Systems In Depth II 5 Copyright 2018 Thomas W Doeppner All rights reserved

Intel x86: Subroutine Code (1) main: pushl %ebp movl %esp, %ebp pushl %esi pushl %edi subl $8, %esp pushl $1 movl -12(%ebp), %eax pushl %eax call sub addl $8, %esp movl %eax, -16(%ebp) set up stack frame push args pop args; get result movl popl popl movl popl ret $0, %eax %edi %esi %ebp, %esp %ebp set return value and restore frame Operating Systems In Depth II 6 Copyright 2018 Thomas W Doeppner All rights reserved

Intel x86: Subroutine Code (2) sub: pushl %ebp movl %esp, %ebp subl $8, %esp movl $1, -4(%ebp) movl $0, -8(%ebp) movl -4(%ebp), %ecx movl -8(%ebp), %eax beginloop: cmpl 12(%ebp), %eax jge endloop imull 8(%ebp), %ecx addl $1, %eax jmp beginloop init locals get args endloop: movl %ecx, -4(%ebp) movl -4(%ebp), %eax movl %ebp, %esp popl %ebp ret Operating Systems In Depth II 7 Copyright 2018 Thomas W Doeppner All rights reserved

x86-64 Twice as many registers Arguments may be passed in registers, rather than on stack No special-purpose frame pointer use stack pointer instead Operating Systems In Depth II 8 Copyright 2018 Thomas W Doeppner All rights reserved

Intel x86-64: Subroutine Code (1) main: subq $24, %rsp movl 12(%rsp), %edi movl $1, %esi call sub addl $24, %rsp movl $0, %eax ret # reserve space on stack for locals # set first argument # set second argument # set return value Operating Systems In Depth II 9 Copyright 2018 Thomas W Doeppner All rights reserved

Intel x86-64: Subroutine Code (2) sub: testl %esi, %esi jle skiploop movl $1, %eax movl $0, %edx loop: imull %edi, %eax addl $1, %edx cmpl %esi, %edx jne loop ret skiploop: movl $1, %eax ret # leaf function: no stack setup Operating Systems In Depth II 10 Copyright 2018 Thomas W Doeppner All rights reserved

SPARC Architecture return address i7 r31 frame pointer i6 r30 i5 r29 i4 r28 i3 r27 i2 r26 i1 r25 i0 r24 Input Registers o7 r15 stack pointer o6 r14 o5 r13 o4 r12 o3 r11 o2 r10 o1 r9 o0 r8 Output Registers Local Registers l7 l6 l5 l4 l3 l2 l1 l0 r23 r22 r21 r20 r19 r18 r17 r16 g7 g6 g5 g4 g3 g2 g1 r7 r6 r5 r4 r3 r2 r1 0 g0 r0 Global Registers Operating Systems In Depth II 11 Copyright 2018 Thomas W Doeppner All rights reserved

SPARC Architecture: Register Windows input window 1 local output input local window 2 input output window 3 local output Operating Systems In Depth II 12 Copyright 2018 Thomas W Doeppner All rights reserved

SPARC Architecture: storage for local variables FP, old SP dynamically allocated stack space space for compiler temporaries and saved floating point registers outgoing parameters beyond 6th save area for callee to store register arguments one-word hidden parameter 16 words to save in and local regs SP Operating Systems In Depth II 13 Copyright 2018 Thomas W Doeppner All rights reserved

SPARC Architecture: Subroutine Code ld [%fp-8], %o0! put local var (a)! into out register mov 1, %o1! deal with 2nd! parameter call sub nop st %o0, [%fp-4]! store result into! local var (i) sub: save %sp, -64, %sp! push a new! stack frame add %i0, %i1, %i0! compute sum ret! return to caller restore! pop frame off! stack (in delay slot) Operating Systems In Depth II 14 Copyright 2018 Thomas W Doeppner All rights reserved

Representing Threads Thread A Control Block Thread B Control Block sp sp Operating Systems In Depth II 15 Copyright 2018 Thomas W Doeppner All rights reserved

Switching Between Threads thread_switch() thread_switch() Coroutine linkage thread_switch() thread_switch() thread_switch() Operating Systems In Depth II 16 Copyright 2018 Thomas W Doeppner All rights reserved

Switching Between Threads 1 void thread_switch(thread_t *next_thread) { 2 SaveContext(&CurrentThread->ctx); 3 CurrentThread = next_thread; 4 GetContext(&CurrentThread->ctx); 5 return; 6 } Operating Systems In Depth II 17 Copyright 2018 Thomas W Doeppner All rights reserved

Switching Between Threads, Take 2 1 void thread_switch(thread_t *next_thread) { 2 SwapContext(&CurrentThread->ctx, 3 &next_thread->ctx); 4 CurrentThread = next_thread; 5 return; 6 } Operating Systems In Depth II 18 Copyright 2018 Thomas W Doeppner All rights reserved

Quiz 1 Does this implementation of thread_switch work? a) yes: in all cases b) yes, except for a few edge cases c) no Operating Systems In Depth II 19 Copyright 2018 Thomas W Doeppner All rights reserved

Switching Between Threads, Take 3 1 void thread_switch(thread_t *next_thread) { 2 thread_t *oldcurrentthread = CurrentThread; 3 CurrentThread = next_thread; 4 SwapContext(&oldCurrentThread->ctx, 5 &CurrentThread->ctx); 6 return; 7 } Operating Systems In Depth II 20 Copyright 2018 Thomas W Doeppner All rights reserved

A Simple Threads Implementation Basis for user-level threads package Straight-threads implementation no interrupts everything in thread contexts one processor Operating Systems In Depth II 21 Copyright 2018 Thomas W Doeppner All rights reserved

Basic Representation Thread object Operating Systems In Depth II 22 Copyright 2018 Thomas W Doeppner All rights reserved

Current Thread A Collection of Threads Run Queue Mutex Queue Thread Object Thread Object Thread Object Thread Object Thread Object Thread Object Thread Object Operating Systems In Depth II 23 Copyright 2018 Thomas W Doeppner All rights reserved

Thread Switch (in C) void thread_switch( ) { thread_t *NextThread, *OldCurrent; NextThread = dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext(&oldcurrent->context, &NextThread->context); // We re now in the new thread s context } Operating Systems In Depth II 24 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; SP NextThread = dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( &OldCurrent->context, thread_switch(); IP } &NextThread->context); Operating Systems In Depth II 25 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; NextThread = Return address NextThread OldCurrent SP dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( &OldCurrent->context, thread_switch(); IP } &NextThread->context); Operating Systems In Depth II 26 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; Return address NextThread OldCurrent SP NextThread = dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( &OldCurrent->context, IP Return address NextThread OldCurrent thread_switch(); } &NextThread->context); thread_switch(); Operating Systems In Depth II 27 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; NextThread = Return address NextThread OldCurrent SP dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( &OldCurrent->context, IP Return address NextThread OldCurrent thread_switch(); } &NextThread->context); thread_switch(); Operating Systems In Depth II 28 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; NextThread = Return address NextThread OldCurrent dequeue(runqueue); OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( SP Return address NextThread OldCurrent &OldCurrent->context, thread_switch(); } &NextThread->context); IP thread_switch(); Operating Systems In Depth II 29 Copyright 2018 Thomas W Doeppner All rights reserved

Thread-Switch Exchange void thread_switch( ) { thread_t NextThread, OldCurrent; NextThread = dequeue(runqueue); SP OldCurrent = CurrentThread; CurrentThread = NextThread; swapcontext( &OldCurrent->context, } &NextThread->context); IP thread_switch(); Operating Systems In Depth II 30 Copyright 2018 Thomas W Doeppner All rights reserved

Mutexes mutex_t mut; mutex_lock(&mut); x = x+1; mutex_unlock(&mut); Operating Systems In Depth II 31 Copyright 2018 Thomas W Doeppner All rights reserved

Implementing Mutexes void mutex_lock(mutex_t *m) { if (m->locked) { } } enqueue(m->wait_queue, CurrentThread); thread_switch(); m->locked = 1; void mutex_unlock(mutex_t *m) { m->locked = 0; } if (!queue_empty(m->wait_queue)) enqueue(runqueue, dequeue(m->wait_queue)); Operating Systems In Depth II 32 Copyright 2018 Thomas W Doeppner All rights reserved

Quiz 2 a) It works b) It works as long as there are just two threads c) It doesn t work Period Operating Systems In Depth II 33 Copyright 2018 Thomas W Doeppner All rights reserved

Implementing Mutexes, Take 2 void mutex_lock(mutex_t *m) { if (m->locked) { } enqueue(m->queue, CurrentThread); thread_switch(); } else m->locked = 1; void mutex_unlock(mutex_t *m) { if (queue_empty(m->queue)) } m->locked = 0; else enqueue(runqueue, dequeue(m->queue)); Operating Systems In Depth II 34 Copyright 2018 Thomas W Doeppner All rights reserved