Operating Systems

Introduction, Processes, Threads

Daniel Gruss October 2, 2020 Table of contents www.tugraz.at

1. Basics

2. History

3.

4. and Fundamentals

5. Context Switches

6. Process and Thread Organization

1

Basics What is an www.tugraz.at

2 What is an Operating System www.tugraz.at

3 What is an Operating System www.tugraz.at

4 What is an Operating System www.tugraz.at

ˆ Run on all sorts of devices: ˆ Servers, Desktops, Notebooks ˆ Tablets, Smartphones ˆ Routers, Switches, Displays ˆ Door Locks, Washing Machines, Toasters ˆ Cars, Airplanes ˆ .... ˆ We focus on general purpose operating systems

5 Operating System Roles www.tugraz.at

ˆ Referee ˆ Illusionist ˆ Glue

6 Operating System Roles: Referee www.tugraz.at

Referee

ˆ Manage resource allocation among users and applications ˆ Isolation of different users, applications from each other ˆ Isolation of applications and operating system ˆ Communication between users, application

7 Operating System Roles: Illusionist www.tugraz.at

Illusionist

ˆ Abstraction of hardware and other things ˆ Makes application design simpler ˆ Each application appears to have the entire machine to itself ˆ Illusion of ˆ Infinite number of processors ˆ (near) infinite amount of memory ˆ reliable storage ˆ reliable network transport

8 Operating System Roles: Glue www.tugraz.at

Glue

ˆ Libraries, user interface widgets, . . .

9 Example: File Systems www.tugraz.at

ˆ Referee ˆ Prevent users from accessing each other’s files without permission ˆ Even after a file is deleted and its space re-used ˆ Illusionist ˆ Files can grow (nearly) arbitrarily large ˆ Files persist even when the machine crashes in the middle of a save

10 Example: File Systems www.tugraz.at

ˆ Glue ˆ Nam ed directories, printf, . . .

11 OS Design Patterns I www.tugraz.at

ˆ OS challenges are not unique - apply to many different computing domains ˆ many complex software systems ˆ have multiple users ˆ run programs written by third-party developers ˆ need to coordinate simultaneous activities

12 OS Design Patterns II www.tugraz.at

Challenges:

ˆ resource allocation ˆ fault isolation ˆ communication ˆ abstraction ˆ how to provide a set of common services

13 ˆ Reliability and Availability ˆ Security ˆ Portability ˆ Performance ˆ Adoption

Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

14 ˆ Security ˆ Portability ˆ Performance ˆ Adoption

Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability

14 ˆ Portability ˆ Performance ˆ Adoption

Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability ˆ Security

14 ˆ Performance ˆ Adoption

Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability ˆ Security ˆ Portability

14 ˆ Adoption

Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability ˆ Security ˆ Portability ˆ Performance

14 Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability ˆ Security ˆ Portability ˆ Performance ˆ Adoption

14 ˆ It does, what it is designed to do

ˆ Application failures can be “ok” - OS provides fault isolation and clean restart after errors ˆ OS errors are bad, real bad...

Reliability and Availability www.tugraz.at

Reliability

15 ˆ Application failures can be “ok” - OS provides fault isolation and clean restart after errors ˆ OS errors are bad, real bad...

Reliability and Availability www.tugraz.at

Reliability

ˆ It does, what it is designed to do

15 Reliability and Availability www.tugraz.at

Reliability

ˆ It does, what it is designed to do

ˆ Application failures can be “ok” - OS provides fault isolation and clean restart after errors ˆ OS errors are bad, real bad...

15 ˆ hostile environment (viruses, malicious code take control) ˆ testing helps - but is much more difficult ˆ extremely rare corner cases can occur regularly

Reliability www.tugraz.at

Making an OS reliable is challenging

16 Reliability www.tugraz.at

Making an OS reliable is challenging

ˆ hostile environment (viruses, malicious code take control) ˆ testing helps - but is much more difficult ˆ extremely rare corner cases can occur regularly

16 ˆ Percentage of time the system is usable

ˆ Buggy OS that loses users work is unreliable AND unavailable ˆ Buggy OS that never loses users work is reliable BUT unavailable ˆ Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and sends them of) is available BUT unreliable

Availability www.tugraz.at

Availability

17 ˆ Buggy OS that loses users work is unreliable AND unavailable ˆ Buggy OS that never loses users work is reliable BUT unavailable ˆ Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and sends them of) is available BUT unreliable

Availability www.tugraz.at

Availability

ˆ Percentage of time the system is usable

17 Availability www.tugraz.at

Availability

ˆ Percentage of time the system is usable

ˆ Buggy OS that loses users work is unreliable AND unavailable ˆ Buggy OS that never loses users work is reliable BUT unavailable ˆ Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and sends them of) is available BUT unreliable

17 Reliability and Availability www.tugraz.at

ˆ Reliablity and Availability are desireable ˆ Two factors: MTTF: mean time to failure MTTR: mean time to repair

18 Security www.tugraz.at

Security: computers operation cannot be compromised by a malicious attacker Privacy: data stored on the computer is only accessible to authorized users

19 Security www.tugraz.at

ˆ Unfortunately: no useful computer is perfectly secure! ˆ OS is a complex piece of software ˆ complex software has bugs ˆ bugs can be exploited ˆ HW may be tampered with ˆ Sysadmin may be untrustworthy ˆ SW-developer may be untrustworthy (backdoors)

20 Security www.tugraz.at

ˆ OS should be designed to minimize its vulnerability to attack ˆ Fault isolation ˆ But: interaction between users and programs required

21 Security Policy www.tugraz.at

Security Policy defines

ˆ who can access what data ˆ who can perform what operations

22 Policies and Enforcement may possess vulnerabilities...

Enforcement www.tugraz.at

Enforcment

ˆ ensures that policy is followed

23 Enforcement www.tugraz.at

Enforcment

ˆ ensures that policy is followed

Policies and Enforcement may possess vulnerabilities...

23 does not change as the hardware changes

ˆ Portability is an important design criteria of the OS ˆ don’t want to reimplement everything when HW changes ˆ e.g. iOS was derived from the MacOS X code base

Portability www.tugraz.at

ˆ OS provides an abstraction of underlying HW

24 ˆ Portability is an important design criteria of the OS ˆ don’t want to reimplement everything when HW changes ˆ e.g. iOS was derived from the MacOS X code base

Portability www.tugraz.at

ˆ OS provides an abstraction of underlying HW

does not change as the hardware changes

24 ˆ don’t want to reimplement everything when HW changes ˆ e.g. iOS was derived from the MacOS X code base

Portability www.tugraz.at

ˆ OS provides an abstraction of underlying HW

does not change as the hardware changes

ˆ Portability is an important design criteria of the OS

24 ˆ e.g. iOS was derived from the MacOS X code base

Portability www.tugraz.at

ˆ OS provides an abstraction of underlying HW

does not change as the hardware changes

ˆ Portability is an important design criteria of the OS ˆ don’t want to reimplement everything when HW changes

24 Portability www.tugraz.at

ˆ OS provides an abstraction of underlying HW

does not change as the hardware changes

ˆ Portability is an important design criteria of the OS ˆ don’t want to reimplement everything when HW changes ˆ e.g. iOS was derived from the MacOS X code base

24 ˆ Windows 10 based on Windows NT (started 1988) ˆ MS/DOS introduced 1981 - phased out ˜2000 ˆ First Linux release 1991

Portability www.tugraz.at

ˆ Lifetime of successful operating systems measured in decades

25 ˆ MS/DOS introduced 1981 - phased out ˜2000 ˆ First Linux release 1991

Portability www.tugraz.at

ˆ Lifetime of successful operating systems measured in decades ˆ Windows 10 based on Windows NT (started 1988)

25 ˆ First Linux release 1991

Portability www.tugraz.at

ˆ Lifetime of successful operating systems measured in decades ˆ Windows 10 based on Windows NT (started 1988) ˆ MS/DOS introduced 1981 - phased out ˜2000

25 Portability www.tugraz.at

ˆ Lifetime of successful operating systems measured in decades ˆ Windows 10 based on Windows NT (started 1988) ˆ MS/DOS introduced 1981 - phased out ˜2000 ˆ First Linux release 1991

25 Portability www.tugraz.at

ˆ An OS must be designed to support ˆ applications that have not yet been written and ˆ hardware that has not been developed ˆ Simple, standard way for applications to interact with OS ˆ API ˆ memory access model ˆ instructions that can be executed

26 Example for a highly portable OS?

Hardware Abstraction Layer www.tugraz.at

ˆ in older/more theoretical literature sometimes “Abstract Virtual Machine” ˆ provides fixed point across which both application and hardware can develop independently

27 Layer www.tugraz.at

ˆ in older/more theoretical literature sometimes “Abstract Virtual Machine” ˆ provides fixed point across which both application and hardware can develop independently

Example for a highly portable OS?

27 Performance www.tugraz.at

ˆ Performance associated with application - OS design can affect the percieved performance ˆ OS decides when app can run how much memory it gets etc. ˆ Performance can be measured in different ways Overhead / Efficiency ˆ Added (lack of) cost of implementing an abstraction presented to applications

28 Performance aspects www.tugraz.at

Fairness: resource allocation can impact performance

ˆ divide resources equally - or treat some differently? ˆ How to decide which task gets priority?

29 Performance aspects www.tugraz.at

Response Time

ˆ (delay) how long does it take for a single task to run from the time it starts to the time it completes ˆ (move the mouse . . . mouse pointer reflects movement)

Throughput

ˆ Rate at which system completes tasks

30 Performance aspects www.tugraz.at

Predictability

ˆ whether systems response time is consistent over time ˆ can be more important than average performance

31 Network Effect

ˆ value of technology does not depend only on its intrinsic capabilities but on the number of people adopting it

Adoption www.tugraz.at

ˆ Success of an OS depends on adoption ˆ SWEB may be the best - but if no application providers support it, it remains doomed for the this exercise

32 Adoption www.tugraz.at

ˆ Success of an OS depends on adoption ˆ SWEB may be the best - but if no application providers support it, it remains doomed for the this exercise

Network Effect

ˆ value of technology does not depend only on its intrinsic capabilities but on the number of people adopting it

32 Adoption www.tugraz.at

ˆ App and HW-vendors focus on OS with most users ˆ users favor OS with best applications or cheapest hardware ˆ Goal for OS: take advantage of network effect ˆ make it easy to accommodate new hardware ˆ make it easy for applications to be ported across different versions of the OS

33 ˆ Windows, MacOS: proprietary ˆ Linux: open ˆ All are widely used

Adoption www.tugraz.at

ˆ API and OS source code - open? proprietary?

34 ˆ All are widely used

Adoption www.tugraz.at

ˆ API and OS source code - open? proprietary? ˆ Windows, MacOS: proprietary ˆ Linux: open

34 Adoption www.tugraz.at

ˆ API and OS source code - open? proprietary? ˆ Windows, MacOS: proprietary ˆ Linux: open ˆ All are widely used

34 Design Criteria for OS www.tugraz.at

Design Criteria for Operating Systems

ˆ Reliability and Availability ˆ Security ˆ Portability ˆ Performance ˆ Adoption

35 Balance between these goals www.tugraz.at

ˆ Improving portability can make the system less reliable and less secure ˆ e.g. preserving legacy interfaces ˆ Improving performance may add complexity and hurt reliability

36 Tradeoff www.tugraz.at

ˆ Research OS in the 1980’s: use type-safe language to reduce programmer errors ˆ For speed: frequently used routines implemented in assembly ˆ Optimized sequence of instructions - but which would sometimes break if the OS exceeded a specific size ˆ Initially nowhere near this limitation ˆ After a few years in production: random crashes occured ˆ Problem search took weeks .... ˆ Was it worth it?

37 History History www.tugraz.at

The first computers were so called “mainframes” that had no operating systems.

38 Mainframes www.tugraz.at

39 ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

ˆ later: collection of compatible utility programs (Multics [2])

Operating System www.tugraz.at

ˆ At that time: did not exist

40 ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2])

40 ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2]) ˆ assemblers, compilers, debugging tools

40 ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2]) ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output

40 ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2]) ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output

40 ˆ automate some of the reconfiguration performed by human operators

Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2]) ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory

40 Operating System www.tugraz.at

ˆ At that time: did not exist ˆ later: collection of compatible utility programs (Multics [2]) ˆ assemblers, compilers, debugging tools ˆ standard routines for input and output ˆ buffers to “spool” printer and tape output ˆ utilities designed to load sequence (or “batch”) of programs into memory ˆ automate some of the reconfiguration performed by human operators

40 ˆ (no PCs at that time) ˆ thought to be used by many users concurrently

ˆ goal: support powerful central computers

MULTICS www.tugraz.at

ˆ one of the first endeavors towards a “real operating system”

41 ˆ (no PCs at that time) ˆ thought to be used by many users concurrently

MULTICS www.tugraz.at

ˆ one of the first endeavors towards a “real operating system” ˆ goal: support powerful central computers

41 ˆ thought to be used by many users concurrently

MULTICS www.tugraz.at

ˆ one of the first endeavors towards a “real operating system” ˆ goal: support powerful central computers ˆ (no PCs at that time)

41 MULTICS www.tugraz.at

ˆ one of the first endeavors towards a “real operating system” ˆ goal: support powerful central computers ˆ (no PCs at that time) ˆ thought to be used by many users concurrently

41 ˆ memory ˆ disk and ˆ CPU ˆ between hundreds of users

ˆ responsible for sharing

ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software

42 ˆ memory ˆ disk and ˆ CPU ˆ between hundreds of users ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing

42 ˆ disk and ˆ CPU ˆ between hundreds of users ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing ˆ memory

42 ˆ CPU ˆ between hundreds of users ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing ˆ memory ˆ disk and

42 ˆ between hundreds of users ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing ˆ memory ˆ disk and ˆ CPU

42 ˆ Important: Protection against faults and tampering

Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing ˆ memory ˆ disk and ˆ CPU ˆ between hundreds of users

42 Requirements www.tugraz.at

ˆ required reliable system software ˆ responsible for sharing ˆ memory ˆ disk and ˆ CPU ˆ between hundreds of users ˆ Important: Protection against faults and tampering

42 ˆ hierarchical file systems ˆ multiple processor support ˆ shared memory ˆ modularized structure ˆ written in a high level programming language

Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory

43 ˆ multiple processor support ˆ shared memory ˆ modularized structure ˆ written in a high level programming language

Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory ˆ hierarchical file systems

43 ˆ shared memory ˆ modularized structure ˆ written in a high level programming language

Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory ˆ hierarchical file systems ˆ multiple processor support

43 ˆ modularized structure ˆ written in a high level programming language

Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory ˆ hierarchical file systems ˆ multiple processor support ˆ shared memory

43 ˆ written in a high level programming language

Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory ˆ hierarchical file systems ˆ multiple processor support ˆ shared memory ˆ modularized structure

43 Results www.tugraz.at

new concepts still valid today in modern operating systems

ˆ paged and segmented memory ˆ hierarchical file systems ˆ multiple processor support ˆ shared memory ˆ modularized structure ˆ written in a high level programming language

43 ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX ˆ by programmers - for programmers ˆ originally in assembly language ˆ rewritten in C ˆ portable operating system!

UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place

44 ˆ by programmers - for programmers ˆ originally in assembly language ˆ rewritten in C ˆ portable operating system!

UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX

44 ˆ originally in assembly language ˆ rewritten in C ˆ portable operating system!

UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX ˆ by programmers - for programmers

44 ˆ rewritten in C ˆ portable operating system!

UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX ˆ by programmers - for programmers ˆ originally in assembly language

44 ˆ portable operating system!

UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX ˆ by programmers - for programmers ˆ originally in assembly language ˆ rewritten in C

44 UNIX et al. www.tugraz.at

ˆ Multics never gained critical mass in the market place ˆ Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX ˆ by programmers - for programmers ˆ originally in assembly language ˆ rewritten in C ˆ portable operating system!

44 IBM’s OS/360 www.tugraz.at

Pugh et al. [5]

ˆ concept that the OS keeps track of all of the system resources that are used, including ˆ program and data space allocation in main memory ˆ file space in secondary storage ˆ file locking during update

45 Evolution of Operating Systems www.tugraz.at

Phase Idea Open shop operating systems Batch processing tape batching, first-in/first -out scheduling Multiprogramming processor multiplexing, atomic operations, demand paging, I/O spooling, priority scheduling, remote job entry Timesharing simultaneous user interactions, on-line file systems Concurrent programming hierarchical systems, extensible kernels, parallel programming Personal Computing Distributed Systems remote servers

46 ˆ operated manually ˆ Programmers were alloted time to use the machine. How did this work?

Open Shop www.tugraz.at

1954

ˆ computers had no operating systems

47 ˆ Programmers were alloted time to use the machine. How did this work?

Open Shop www.tugraz.at

1954

ˆ computers had no operating systems ˆ operated manually

47 Open Shop www.tugraz.at

1954

ˆ computers had no operating systems ˆ operated manually ˆ Programmers were alloted time to use the machine. How did this work?

47 Open Shop www.tugraz.at

Each user was allocated a minimum 15-minute slot, of which time he usually spent 10 minutes in setting up the equipment to do his computation... By the  time he got his computation going, he may have had only 5 minutes or less of actual computation completed - wasting two thirds of his time slot. (George Ryckman on operating IBM’s first computer, the IBM 701 [3])

 

48 ˆ punched cards to code programs ˆ line printers produce output

ˆ remove programmers from the computer room (closed shop).

Batch processing www.tugraz.at

ˆ Most efficient way to reduce idle computer time:

49 ˆ punched cards to code programs ˆ line printers produce output

Batch processing www.tugraz.at

ˆ Most efficient way to reduce idle computer time: ˆ remove programmers from the computer room (closed shop).

49 ˆ line printers produce output

Batch processing www.tugraz.at

ˆ Most efficient way to reduce idle computer time: ˆ remove programmers from the computer room (closed shop). ˆ punched cards to code programs

49 Batch processing www.tugraz.at

ˆ Most efficient way to reduce idle computer time: ˆ remove programmers from the computer room (closed shop). ˆ punched cards to code programs ˆ line printers produce output

49 ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Supervisor” [4]

ˆ 16.284 words of core store

50 ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store

50 ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store

50 ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum

50 ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms

50 ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers

50 ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches

50 ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters

50 ˆ 1 card reader ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer

50 ˆ 1 card punch

Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader

50 Atlas www.tugraz.at

One of the early batch processing Operating Systems (then called supervisor) running on “The Atlas Supervisor” [4]

ˆ 16.284 words of core store ˆ 8.192 words of fixed store ˆ 1.025 words of subsidiary store ˆ 98.304 words on drum ˆ 8 magnetic tape mechanisms ˆ 3 paper tape readers ˆ 3 paper tape punches ˆ 2 teleprinters ˆ 1 line printer ˆ 1 card reader ˆ 1 card punch

50 ˆ too much valuable time lost between two jobs ˆ Load multiple programs concurrently ˆ switch between programs ˆ requires protection mechanisms ˆ enhanced by

Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time

51 ˆ Load multiple programs concurrently ˆ switch between programs ˆ requires protection mechanisms ˆ enhanced by virtual memory

Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time ˆ too much valuable time lost between two jobs

51 ˆ switch between programs ˆ requires protection mechanisms ˆ enhanced by virtual memory

Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time ˆ too much valuable time lost between two jobs ˆ Load multiple programs concurrently

51 ˆ requires protection mechanisms ˆ enhanced by virtual memory

Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time ˆ too much valuable time lost between two jobs ˆ Load multiple programs concurrently ˆ switch between programs

51 ˆ enhanced by virtual memory

Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time ˆ too much valuable time lost between two jobs ˆ Load multiple programs concurrently ˆ switch between programs ˆ requires protection mechanisms

51 Multiprogramming www.tugraz.at

ˆ Drawback of Batch Processing: one job at a time ˆ too much valuable time lost between two jobs ˆ Load multiple programs concurrently ˆ switch between programs ˆ requires protection mechanisms ˆ enhanced by virtual memory

51 ˆ use computer via terminals ˆ interaction with computer

ˆ submit a job ˆ multiple jobs run in parallel ˆ receive results ˆ Timesharing

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like”

52 ˆ use computer via terminals ˆ interaction with computer

ˆ multiple jobs run in parallel ˆ receive results ˆ Timesharing

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job

52 ˆ use computer via terminals ˆ interaction with computer

ˆ receive results ˆ Timesharing

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job ˆ multiple jobs run in parallel

52 ˆ use computer via terminals ˆ interaction with computer

ˆ Timesharing

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job ˆ multiple jobs run in parallel ˆ receive results

52 ˆ use computer via terminals ˆ interaction with computer

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job ˆ multiple jobs run in parallel ˆ receive results ˆ Timesharing

52 ˆ interaction with computer

Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job ˆ multiple jobs run in parallel ˆ receive results ˆ Timesharing ˆ use computer via terminals

52 Timesharing www.tugraz.at

ˆ Multiprogramming still “Batch-like” ˆ submit a job ˆ multiple jobs run in parallel ˆ receive results ˆ Timesharing ˆ use computer via terminals ˆ interaction with computer

52 ˆ Scheduling ˆ Swapping ˆ File-systems ˆ Protection

Timesharing www.tugraz.at

ˆ Requires

53 ˆ Swapping ˆ File-systems ˆ Protection

Timesharing www.tugraz.at

ˆ Requires ˆ Scheduling

53 ˆ File-systems ˆ Protection

Timesharing www.tugraz.at

ˆ Requires ˆ Scheduling ˆ Swapping

53 ˆ Protection

Timesharing www.tugraz.at

ˆ Requires ˆ Scheduling ˆ Swapping ˆ File-systems

53 Timesharing www.tugraz.at

ˆ Requires ˆ Scheduling ˆ Swapping ˆ File-systems ˆ Protection

53 Concurrent Programming www.tugraz.at

ˆ Fundamental Problems were not understood properly ˆ Deadlocks ˆ Race Conditions ˆ Led to the discovery of fundamental principles of concurrent programming

54 THE Operating System www.tugraz.at

ˆ By Edsger E. Dijkstra (1968) ˆ Spooling System with paper tape in/output ˆ demand paging between a 512K word drum and 32K word memory ˆ 5 user processes, 10 I/O processes (one for each device) ˆ Used semaphores for synchronization

55 THE www.tugraz.at

ˆ Claim by Dijkstra [1] We have found that it is possible to design a refined multiprogramming system in such a way that it its logical soundness can be proved a priori and its implementation can admit exhaustive testing. The only errors that showed up during testing were trivial coding errors. The resulting system is guaranteed to be flawless.

56 Personal Computing www.tugraz.at

1968: First devices named “personal computer” (actually a calculator)

57 PCs www.tugraz.at

1973: Xerox Alto, first computer with mouse, desktop, and GUI

58 PC Operating Systems www.tugraz.at

ˆ Different requirements: only one user ˆ CP/M, DOS, Apple-DOS ˆ Windows ˆ OS-2, Windows-XP, OS-X, Linux....

59 Distributed Systems www.tugraz.at

ˆ By 1980 all major OS concepts discovered ˆ distributed systems developed ˆ communication via RPC - remote procedure call ˆ a few made it to commercial systems ˆ now, the “Internet” as a huge distributed system...

60

Booting ˆ actually only 4 bit, but shifted by 16 bits to the left → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

ˆ Address space: 1 MB ˆ How is that possible? ˆ CS register has a 20-bit base address

Starting in Real Mode www.tugraz.at

ˆ 16 bit mode

61 ˆ actually only 4 bit, but shifted by 16 bits to the left → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

ˆ How is that possible? ˆ CS register has a 20-bit base address

Starting in Real Mode www.tugraz.at

ˆ 16 bit mode ˆ Address space: 1 MB

61 ˆ actually only 4 bit, but shifted by 16 bits to the left → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

ˆ CS register has a 20-bit base address

Starting in Real Mode www.tugraz.at

ˆ 16 bit mode ˆ Address space: 1 MB ˆ How is that possible?

61 ˆ actually only 4 bit, but shifted by 16 bits to the left → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

Starting in Real Mode www.tugraz.at

ˆ 16 bit mode ˆ Address space: 1 MB ˆ How is that possible? ˆ CS register has a 20-bit base address

61 → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

Starting in Real Mode www.tugraz.at

ˆ 16 bit mode ˆ Address space: 1 MB ˆ How is that possible? ˆ CS register has a 20-bit base address ˆ actually only 4 bit, but shifted by 16 bits to the left

61 Starting in Real Mode www.tugraz.at

ˆ 16 bit mode ˆ Address space: 1 MB ˆ How is that possible? ˆ CS register has a 20-bit base address ˆ actually only 4 bit, but shifted by 16 bits to the left → 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address

61 Booting x86 Intel www.tugraz.at

62 ˆ CS register also has a 32-bit base address (initialized to 0xFFFF0000)

ˆ physical address space 6= RAM directly mapped

ˆ How is that possible?

ˆ What if I have < 4 GB RAM?

Booting in Real Mode www.tugraz.at

ˆ Address: 0xFFFFFFF0

63 ˆ physical address space 6= RAM directly mapped

ˆ CS register also has a 32-bit base address (initialized to 0xFFFF0000) ˆ What if I have < 4 GB RAM?

Booting in Real Mode www.tugraz.at

ˆ Address: 0xFFFFFFF0 ˆ How is that possible?

63 ˆ physical address space 6= RAM directly mapped

ˆ What if I have < 4 GB RAM?

Booting in Real Mode www.tugraz.at

ˆ Address: 0xFFFFFFF0 ˆ How is that possible? ˆ CS register also has a 32-bit base address (initialized to 0xFFFF0000)

63 ˆ physical address space 6= RAM directly mapped

Booting in Real Mode www.tugraz.at

ˆ Address: 0xFFFFFFF0 ˆ How is that possible? ˆ CS register also has a 32-bit base address (initialized to 0xFFFF0000) ˆ What if I have < 4 GB RAM?

63 Booting in Real Mode www.tugraz.at

ˆ Address: 0xFFFFFFF0 ˆ How is that possible? ˆ CS register also has a 32-bit base address (initialized to 0xFFFF0000) ˆ What if I have < 4 GB RAM? ˆ physical address space 6= RAM directly mapped

63 Physical Address Space www.tugraz.at

00000000-007fffff (prio 0, RW): alias ram-below-4g (this is our RAM) 000a0000-000bffff (prio 1, RW): vga-lowmem (remember for later) 000c0000-000dffff (prio 1, RW): pc.rom 000e0000-000fffff (prio 1, R-): alias isa-bios fd000000-fdffffff (prio 1, RW): vga.vram febc0000-febdffff (prio 1, RW): e1000-mmio febf0400-febf041f (prio 0, RW): vga ioports remapped febf0500-febf0515 (prio 0, RW): bochs dispi interface febf0600-febf0607 (prio 0, RW): qemu extended regs fffc0000-ffffffff (prio 0, R-): pc.bios (ahhh!) ...

64 ˆ Switch to protected mode (32 bit) ˆ Select a device to boot from ˆ Load MBR from device into memory ˆ Execute code from MBR

BIOS www.tugraz.at

ˆ BIOS initializes hardware platform

65 ˆ Select a device to boot from ˆ Load MBR from device into memory ˆ Execute code from MBR

BIOS www.tugraz.at

ˆ BIOS initializes hardware platform ˆ Switch to protected mode (32 bit)

65 ˆ Load MBR from device into memory ˆ Execute code from MBR

BIOS www.tugraz.at

ˆ BIOS initializes hardware platform ˆ Switch to protected mode (32 bit) ˆ Select a device to boot from

65 ˆ Execute code from MBR

BIOS www.tugraz.at

ˆ BIOS initializes hardware platform ˆ Switch to protected mode (32 bit) ˆ Select a device to boot from ˆ Load MBR from device into memory

65 BIOS www.tugraz.at

ˆ BIOS initializes hardware platform ˆ Switch to protected mode (32 bit) ˆ Select a device to boot from ˆ Load MBR from device into memory ˆ Execute code from MBR

65 Booting x86 Intel (Illustration) www.tugraz.at

66 GRUB www.tugraz.at

ˆ Boot loader for Linux, SWEB, . . . ˆ Loads the OS image from disk and starts OS

GRUB One of the important features in GRUB is flexibility; GRUB understands file-systems and kernel executable formats, so you can load an arbitrary operating system the way you like, without recording the physical position of your kernel on the disk. Thus you can load the kernel just by specifying its file name and the drive and partition where the kernel resides.

67 Booting www.tugraz.at

68 ˆ Start device drivers and initialize devices ˆ Start initial processes (e.g. init-process)

Booting the OS www.tugraz.at

ˆ Prepare hardware

69 ˆ Start initial processes (e.g. init-process)

Booting the OS www.tugraz.at

ˆ Prepare hardware ˆ Start device drivers and initialize devices

69 Booting the OS www.tugraz.at

ˆ Prepare hardware ˆ Start device drivers and initialize devices ˆ Start initial processes (e.g. init-process)

69 % readelf -a kernel.x | grep Entry Entry point address: 0x801001ba

% objdump -S kernel.x | less 801001ba : 801001ba: 55 push %ebp 801001bb: 89 e5 mov %esp,%ebp 801001bd: 83 ec 10 sub $0x10,%esp 801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000

Wait, that’s C-Code!

Booting the OS (SWEB) www.tugraz.at

Kernel is a compiled binary (e.g. an ELF binary)

70 % objdump -S kernel.x | less 801001ba : 801001ba: 55 push %ebp 801001bb: 89 e5 mov %esp,%ebp 801001bd: 83 ec 10 sub $0x10,%esp 801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000

Wait, that’s C-Code!

Booting the OS (SWEB) www.tugraz.at

Kernel is a compiled binary (e.g. an ELF binary)

% readelf -a kernel.x | grep Entry Entry point address: 0x801001ba

70 Wait, that’s C-Code!

Booting the OS (SWEB) www.tugraz.at

Kernel is a compiled binary (e.g. an ELF binary)

% readelf -a kernel.x | grep Entry Entry point address: 0x801001ba

% objdump -S kernel.x | less 801001ba : 801001ba: 55 push %ebp 801001bb: 89 e5 mov %esp,%ebp 801001bd: 83 ec 10 sub $0x10,%esp 801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000

70 Booting the OS (SWEB) www.tugraz.at

Kernel is a compiled binary (e.g. an ELF binary)

% readelf -a kernel.x | grep Entry Entry point address: 0x801001ba

% objdump -S kernel.x | less 801001ba : 801001ba: 55 push %ebp 801001bb: 89 e5 mov %esp,%ebp 801001bd: 83 ec 10 sub $0x10,%esp 801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000

Wait, that’s C-Code!

70 Booting the OS (SWEB) www.tugraz.at

extern "C" void entry() { asm("mov%ebx,multi_boot_structure_pointer- BASE");

PRINT("Booting...\n");

71 Booting the OS (SWEB) www.tugraz.at

PRINT("Clearing Framebuffer...\n"); memset((char*) 0xB8000, 0, 80 * 25 * 2);

PRINT("Clearing BSS...\n"); char* bss_start= TRUNCATE(&bss_start_address); memset(bss_start, 0, TRUNCATE(&bss_end_address)- bss_start);

PRINT("Initializing Kernel Paging Structures...\n"); //...

72 Booting the OS (SWEB) www.tugraz.at

PRINT("Enable PSE and PAE...\n"); asm("mov%cr4,%eax\n" "or $0x20,%eax\n" "mov%eax,%cr4\n");

PRINT("Setting CR3 Register...\n"); asm("mov%[pd],%%cr3"::[pd]"r"(TRUNCATE(kernel_page_map_level_4)));

PRINT("Enable EFER.LME and EFER.NXE...\n"); asm("mov $0xC0000080,%ecx\n" "rdmsr\n" "or $0x900,%eax\n" "wrmsr\n"); //... PRINT("Enable Paging...\n"); asm("mov%cr0,%eax\n" "or $0x80000001,%eax\n" "mov%eax,%cr0\n");

73 Booting the OS (SWEB) www.tugraz.at

PRINT("Setup TSS...\n"); TSS* g_tss_p=(TSS *) TRUNCATE(&g_tss); g_tss_p->ist0_h = -1U; g_tss_p->ist0_l=(uint32) TRUNCATE(boot_stack) | 0x80004000; g_tss_p->rsp0_h = -1U; g_tss_p->rsp0_l=(uint32) TRUNCATE(boot_stack) | 0x80004000;

74 Booting the OS (SWEB) www.tugraz.at

75 Booting the OS (SWEB) www.tugraz.at

76 Booting the OS (SWEB) www.tugraz.at

static void setSegmentDescriptor(uint32 index, uint32 baseH, uint32 baseL, uint32 limit, uint8 dpl, uint8 code, uint8 tss);

PRINT("Setup Segments...\n"); setSegmentDescriptor(1, 0, 0, 0, 0, 1, 0); setSegmentDescriptor(2, 0, 0, 0, 0, 0, 0); setSegmentDescriptor(3, 0, 0, 0, 3, 1, 0); setSegmentDescriptor(4, 0, 0, 0, 3, 0, 0); setSegmentDescriptor(5, -1U,(uint32) TRUNCATE(&g_tss) | 0x80000000, sizeof(TSS) - 1, 0, 0, 1);

PRINT("Loading Long Mode GDT...\n");

struct GDT32Ptr gdt32_ptr; gdt32_ptr.limit= sizeof(gdt) - 1; gdt32_ptr.addr=(uint32) TRUNCATE(gdt); asm("lgdt%[gdt_ptr]"::[gdt_ptr]"m"(gdt32_ptr)); // ...

77 Booting the OS (SWEB) www.tugraz.at

PRINT("Setting Long Mode Segment Selectors...\n"); asm("mov%%ax,%%ds\n" "mov%%ax,%%es\n" "mov%%ax,%%ss\n" "mov%%ax,%%fs\n" "mov%%ax,%%gs\n" ::"a"(KERNEL_DS));

PRINT("Calling entry64()...\n"); asm("ljmp%[cs],$entry64-BASE\n"::[cs]"i"(KERNEL_CS));

PRINT("Returned from entry64()? This should never happen.\n"); asm("hlt"); }

78 Booting the OS (SWEB) www.tugraz.at

PRINT("Setting Long Mode Segment Selectors...\n"); asm("mov%%ax,%%ds\n" "mov%%ax,%%es\n" "mov%%ax,%%ss\n" "mov%%ax,%%fs\n" "mov%%ax,%%gs\n" ::"a"(KERNEL_DS));

PRINT("Calling entry64()...\n"); asm("ljmp%[cs],$entry64-BASE\n"::[cs]"i"(KERNEL_CS));

PRINT("Returned from entry64()? This should never happen.\n"); asm("hlt"); }

79 Booting the OS (SWEB) www.tugraz.at

extern "C" void entry64() { PRINT("Parsing Multiboot Header...\n"); parseMultibootHeader(); PRINT("Initializing Kernel Paging Structures...\n"); initialisePaging(); PRINT("Setting CR3 Register...\n"); asm("mov%%rax,%%cr3"::"a"(VIRTUAL_TO_PHYSICAL_BOOT(ArchMemory:: getRootOfKernelPagingStructure()))); PRINT("Switch to our own stack...\n"); asm("mov%[stack],%%rsp\n" "mov%[stack],%%rbp\n"::[stack]"i"(boot_stack+0x4000));

80 Booting the OS (SWEB) www.tugraz.at

PRINT("Loading Long Mode Segments...\n");

gdt_ptr.limit= sizeof(gdt) - 1; gdt_ptr.addr=(uint64)gdt; asm("lgdt (%%rax)"::"a"(&gdt_ptr)); asm("mov%%ax,%%ds\n" "mov%%ax,%%es\n" "mov%%ax,%%ss\n" "mov%%ax,%%fs\n" "mov%%ax,%%gs\n" ::"a"(KERNEL_DS)); asm("ltr%%ax"::"a"(KERNEL_TSS)); PRINT("Calling startup()...\n"); asm("jmp *%[startup]"::[startup]"r"(startup)); while (1); }

81 Booting the OS (SWEB) www.tugraz.at

extern "C" void startup() { writeLine2Bochs("Removing Boot Time Ident Mapping...\n"); removeBootTimeIdentMapping(); system_state= BOOTING;

PageManager::instance(); writeLine2Bochs("PageManager and KernelMemoryManager created\n");

main_console= ArchCommon::createConsole(1); writeLine2Bochs("Console created\n"); // ...

82 Booting the OS (SWEB) www.tugraz.at

Scheduler::instance();

//needs to be done after scheduler and terminal, but prior to enableInterrupts kprintf_init();

debug(MAIN,"Threads init\n"); ArchThreads::initialise(); debug(MAIN,"Interupts init\n"); ArchInterrupts::initialise();

ArchInterrupts::setTimerFrequency(IRQ0_TIMER_FREQUENCY);

83 Booting the OS (SWEB) www.tugraz.at

ArchCommon::initDebug();

vfs.initialize(); debug(MAIN,"Mounting DeviceFS under/dev/\n"); DeviceFSType *devfs= new DeviceFSType(); vfs.registerFileSystem(devfs); default_working_dir= vfs.root_mount("devicefs", 0);

debug(MAIN,"Block Device creation\n"); BDManager::getInstance()->doDeviceDetection(); debug(MAIN,"Block Device done\n");

for (BDVirtualDevice* bdvd: BDManager::getInstance()->device_list_) { debug(MAIN,"Detected Device:%s::%d\n", bdvd->getName(), bdvd-> getDeviceNumber()); }

84 Booting the OS (SWEB) www.tugraz.at

// initialise global and static objects extern ustl::list global_fd; new (&global_fd) ustl::list(); extern Mutex global_fd_lock; new (&global_fd_lock) Mutex("global_fd_lock"); // ... debug(MAIN,"Timer enable\n"); ArchInterrupts::enableTimer(); KeyboardManager::instance(); ArchInterrupts::enableKBD();

85 Booting the OS (SWEB) www.tugraz.at

debug(MAIN,"Adding Kernel threads\n"); Scheduler::instance()->addNewThread(main_console); Scheduler::instance()->addNewThread(new ProcessRegistry(new FileSystemInfo(* default_working_dir), user_progs /*see user_progs.h*/)); Scheduler::instance()->printThreadList();

kprintf("Now enabling ...\n"); system_state= RUNNING;

ArchInterrupts::enableInterrupts();

Scheduler::instance()->yield(); //not reached assert(false); }

86 Booting www.tugraz.at

87 Process and Thread Fundamentals ˆ actions: run, , stop ˆ resources: CPU time, stack, registers

ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context

ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data

88 ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers

ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ resources: file ˆ A thread: an execution context

ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load

88 ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers

ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ A thread: an execution context

ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file

88 ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context

88 ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ resources: CPU time, stack, registers ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context ˆ actions: run, interrupt, stop

88 ˆ actions: create, start, terminate ˆ resources: threads, memory, program

ˆ A process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers

88 ˆ actions: create, start, terminate ˆ resources: threads, memory, program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers ˆ A process: a container for threads and memory contents of a program

88 ˆ resources: threads, memory, program

Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers ˆ A process: a container for threads and memory contents of a program ˆ actions: create, start, terminate

88 Program, Process, Thread www.tugraz.at

ˆ A program: a binary file containing code and data ˆ actions: write, compile, install, load ˆ resources: file ˆ A thread: an execution context ˆ actions: run, interrupt, stop ˆ resources: CPU time, stack, registers ˆ A process: a container for threads and memory contents of a program ˆ actions: create, start, terminate ˆ resources: threads, memory, program

88

ˆ File: abstraction of a disk or a device ˆ Socket: abstraction of a network connection ˆ Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

Abstractions www.tugraz.at

ˆ Process: abstraction of a computer

89 ˆ Socket: abstraction of a network connection ˆ Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

Abstractions www.tugraz.at

ˆ Process: abstraction of a computer ˆ File: abstraction of a disk or a device

89 ˆ Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

Abstractions www.tugraz.at

ˆ Process: abstraction of a computer ˆ File: abstraction of a disk or a device ˆ Socket: abstraction of a network connection

89 → Abstractions hide many details but provide the required capabilities

Abstractions www.tugraz.at

ˆ Process: abstraction of a computer ˆ File: abstraction of a disk or a device ˆ Socket: abstraction of a network connection ˆ Window: abstraction of a display

89 → Abstractions hide many details but provide the required capabilities

Abstractions www.tugraz.at

ˆ Process: abstraction of a computer ˆ File: abstraction of a disk or a device ˆ Socket: abstraction of a network connection ˆ Window: abstraction of a display

89 Abstractions www.tugraz.at

ˆ Process: abstraction of a computer ˆ File: abstraction of a disk or a device ˆ Socket: abstraction of a network connection ˆ Window: abstraction of a display

→ Abstractions hide many details but provide the required capabilities

89 CPU vs. Process www.tugraz.at

90 CPU vs. Process www.tugraz.at

91 Processes www.tugraz.at

Implemented by the kernel

92 Implementation? www.tugraz.at

ˆ We have “one hardware” ˆ We have many “processes” ˆ How do we solve this?

93 The Process Abstraction www.tugraz.at

94 ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by

95 ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and

95 ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction

95 ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program

95 ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes

95 ˆ OS keeps a list of process data structure (aka the “PCB”)

Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another

95 Program, Process, Thread www.tugraz.at

ˆ Once a program is loaded in memory, OS can start it(s first thread) by ˆ setting up a stack and setting the stack pointer and ˆ setting the instruction pointer (of the first thread) to the programs first instruction ˆ Process is an instance of a program ˆ Kernel must organize running code of multiple processes ˆ Must be able to switch from one process to another ˆ OS keeps a list of process data structure (aka the “PCB”)

95 ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ where program is loaded in memory ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ where image is on disk

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ what privileges the process has ˆ etc.

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ where image is on disk ˆ which user asked to execute

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ etc.

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has

96 ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ Process ID ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ User ID ˆ Process status ˆ Scheduling information ˆ I/O resources

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ Process status ˆ Scheduling information ˆ I/O resources

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ what privileges the process has ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ Scheduling information ˆ I/O resources

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

ˆ I/O resources

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc.

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

Process can have multiple threads

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

96 ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

Process can have multiple threads

96 ˆ own stack ˆ own registers (including instruction pointer)

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

Process can have multiple threads

ˆ same program code and data

96 ˆ own registers (including instruction pointer)

Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

Process can have multiple threads

ˆ same program code and data ˆ own stack

96 Process List (aka PCB) www.tugraz.at

Process list stores ˆ where program is loaded in memory ˆ Process ID ˆ where image is on disk ˆ User ID ˆ which user asked to execute ˆ Process status ˆ what privileges the process has ˆ Scheduling information ˆ etc. ˆ I/O resources

Process can have multiple threads

ˆ same program code and data ˆ own stack ˆ own registers (including instruction pointer)

96 Process Protection Mechanisms Malicious? ˆ We want to give the program restricted privileges ˆ How can we do that?

ˆ What code? ˆ Do we trust that code? ˆ Maybe buggy?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code

97 Malicious? ˆ We want to give the program restricted privileges ˆ How can we do that?

ˆ Do we trust that code? ˆ Maybe buggy?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code?

97 Malicious? ˆ We want to give the program restricted privileges ˆ How can we do that?

ˆ Maybe buggy?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code? ˆ Do we trust that code?

97 ˆ We want to give the program restricted privileges ˆ How can we do that?

Malicious?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code? ˆ Do we trust that code? ˆ Maybe buggy?

97 ˆ We want to give the program restricted privileges ˆ How can we do that?

Malicious?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code? ˆ Do we trust that code? ˆ Maybe buggy?

97 ˆ How can we do that?

How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code? ˆ Do we trust that code? ˆ Maybe buggy? Malicious? ˆ We want to give the program restricted privileges

97 How can it be safe to run untrusted software on your hardware? www.tugraz.at

Challenges:

ˆ Threads of a process run code ˆ What code? ˆ Do we trust that code? ˆ Maybe buggy? Malicious? ˆ We want to give the program restricted privileges ˆ How can we do that?

97 ˆ Some instructions can

asm("cli"); asm("hlt");

Privileged and unprivileged instructions www.tugraz.at

ˆ Most instructions cannot do any harm

98 asm("cli"); asm("hlt");

Privileged and unprivileged instructions www.tugraz.at

ˆ Most instructions cannot do any harm ˆ Some instructions can

98 Privileged and unprivileged instructions www.tugraz.at

ˆ Most instructions cannot do any harm ˆ Some instructions can

asm("cli"); asm("hlt");

98 Examples for Privileged Instructions (Intel) www.tugraz.at

ˆ LGDT: Load GDT register ˆ LLDT: Load LDT register ˆ LTR: Load task register ˆ LIDT: Load IDT register ˆ MOV (control registers): Load and store control registers ˆ LMSW: Load machine status word ˆ CLTS: Clear task-switched flag in register CR0 ˆ MOV (debug registers): Load and store debug registers ˆ INVD: Invalidate cache, without writeback ˆ WBINVD: Invalidate cache, with writeback ˆ INVLPG: Invalidate TLB entry ˆ HLT: Halt processor ˆ RDMSR: Read Model-Specific Registers ˆ WRMSR: Write Model-Specific Registers

99 Recall: DPL defined in segment descriptor

ˆ User-mode: DPL = 3 ˆ Kernel-mode: DPL = 0

→ hardware-assisted control mechanisms

Dual-mode operation www.tugraz.at

ˆ User-mode: limited privileges ˆ Kernel-mode: complete privileges

100 → hardware-assisted control mechanisms

Dual-mode operation www.tugraz.at

ˆ User-mode: limited privileges ˆ Kernel-mode: complete privileges

Recall: DPL defined in segment descriptor

ˆ User-mode: DPL = 3 ˆ Kernel-mode: DPL = 0

100 Dual-mode operation www.tugraz.at

ˆ User-mode: limited privileges ˆ Kernel-mode: complete privileges

Recall: DPL defined in segment descriptor

ˆ User-mode: DPL = 3 ˆ Kernel-mode: DPL = 0

→ hardware-assisted control mechanisms

100 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode:

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not accessible

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not ˆ Access to any I/O-device accessible ˆ If tried anyway: exception is raised by the CPU.

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not ˆ Access to any I/O-device accessible ˆ Access to all disk content ˆ If tried anyway: exception is raised by the CPU. ˆ Need something user mode can’t do?

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not ˆ Access to any I/O-device accessible ˆ Access to all disk content ˆ If tried anyway: exception is raised by the CPU. ˆ Access to all network traffic ˆ Need something user mode can’t do? → ask operating system for help

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not ˆ Access to any I/O-device accessible ˆ Access to all disk content ˆ If tried anyway: exception is raised by the CPU. ˆ Access to all network traffic ˆ Need something user mode can’t do? → “call” operating system for help

101 Hardware Support: Dual-Mode www.tugraz.at

Kernel Mode: User Mode: ˆ OS runs in kernel mode ˆ User programs run in user mode ˆ Full privileges for hardware accesses ˆ Limited privileges ˆ Read/write to any memory ˆ Some instructions and memory regions are not ˆ Access to any I/O-device accessible ˆ Access to all disk content ˆ If tried anyway: exception is raised by the CPU. ˆ Access to all network traffic ˆ Need something user mode can’t do? → “call” operating system for help → system call

101 Hardware Support: Dual-Mode Implementation www.tugraz.at

ˆ mode stored in EFLAGS register ˆ segment descriptors ˆ paging structures ˆ ...

102 IA-32 Ring Structure www.tugraz.at

103 How to change from ring to ring . . . www.tugraz.at

ˆ change from kernel mode (lower level ring) to user mode (higher level ring) not a problem

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from kernel mode (lower level ring) to user mode (higher level ring) not a problem ˆ change from user mode (higher level ring) to kernel mode (lower level ring) must be a controlled procedure

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from kernel mode (lower level ring) to user mode (higher level ring) not a problem ˆ change from user mode (higher level ring) to kernel mode (lower level ring) must be a controlled procedure

→ Otherwise there would be no protection

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from ring 0 to ring 3 not a problem ˆ change from user mode (higher level ring) to kernel mode (lower level ring) must be a controlled procedure

→ Otherwise there would be no protection

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from ring 0 to ring 3 not a problem ˆ change from ring 3 to ring 0 through controlled procedure

→ Otherwise there would be no protection

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from ring 0 to ring 3 through special return instruction (iret) ˆ change from ring 3 to ring 0 through controlled procedure

→ Otherwise there would be no protection

104 How to change from ring to ring . . . www.tugraz.at

ˆ change from ring 0 to ring 3 through special return instruction (iret) ˆ change from ring 3 to ring 0 through int 0x80, sysenter, or syscall

→ Otherwise there would be no protection

104 ˆ timer ˆ I/O-devices ˆ exceptions (divide-by-zero, page fault, etc.)

ˆ or by the hardware

Interrupts www.tugraz.at

ˆ either generated by the software (e.g. syscall)

105 ˆ timer ˆ I/O-devices ˆ exceptions (divide-by-zero, page fault, etc.)

Interrupts www.tugraz.at

ˆ either generated by the software (e.g. syscall) ˆ or by the hardware

105 ˆ I/O-devices ˆ exceptions (divide-by-zero, page fault, etc.)

Interrupts www.tugraz.at

ˆ either generated by the software (e.g. syscall) ˆ or by the hardware ˆ timer

105 ˆ exceptions (divide-by-zero, page fault, etc.)

Interrupts www.tugraz.at

ˆ either generated by the software (e.g. syscall) ˆ or by the hardware ˆ timer ˆ I/O-devices

105 Interrupts www.tugraz.at

ˆ either generated by the software (e.g. syscall) ˆ or by the hardware ˆ timer ˆ I/O-devices ˆ exceptions (divide-by-zero, page fault, etc.)

105 ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a

ˆ Why?

ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack

106 ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why?

106 ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability

106 ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points

106 ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address

106 ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel)

106 ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch

106 ˆ Do we need multiple stacks for the kernel?

Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need?

106 Interrupts and Stacks www.tugraz.at

ˆ Interrupts switch stack to a kernel stack ˆ Why? ˆ Security and stability ˆ Who knows where the users SP points ˆ Maybe SP points to illegal address ˆ Would raises an page fault exception (in the kernel) ˆ Some register values are pushed to stack by the CPU during a context switch ˆ How many stacks do we actually need? ˆ Do we need multiple stacks for the kernel?

106 Stacks www.tugraz.at

107 Stacks www.tugraz.at

108 Stacks www.tugraz.at

Running Ready to Run Blocked

system call

procedure 2 procedure 2 procedure 2

procedure 1 procedure 1 procedure 1

main main main User Stack

I/O-Driver

Syscall handler

CPU state CPU state user mode user mode Kerrnel Stack Kerrnel

109 Context Switches how?

ˆ how to switch between threads? ˆ how do we let a CPU / core execute a different function? ˆ change the instruction pointer?

Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time

110 how?

ˆ how do we let a CPU / core execute a different function? ˆ change the instruction pointer?

Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time ˆ how to switch between threads?

110 how?

ˆ change the instruction pointer?

Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time ˆ how to switch between threads? ˆ how do we let a CPU / core execute a different function?

110 how?

Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time ˆ how to switch between threads? ˆ how do we let a CPU / core execute a different function? ˆ change the instruction pointer?

110 how?

Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time ˆ how to switch between threads? ˆ how do we let a CPU / core execute a different function? ˆ change the instruction pointer?

110 Threads. Multiple Threads. www.tugraz.at

ˆ one CPU / core: one active thread at any point in time ˆ how to switch between threads? ˆ how do we let a CPU / core execute a different function? ˆ change the instruction pointer? how?

110 asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

111 does this work? Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

111 Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work?

111 ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

111 ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

ˆ what if the thread is in another process?

111 how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices:

111 ˆ what if we’re coming from kernelspace?

Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from?

111 Threads. Multiple Threads. www.tugraz.at

Changing the instruction pointer

asm("jmp *%[other_thread_function]" : :[other_thread_function]"r"(other_thread_function));

does this work? Yes, but . . .

ˆ what if the thread is in another process? ˆ scheduling thread slices: how do we later restore the state we came from? ˆ what if we’re coming from kernelspace?

111 ˆ Caused only by an interrupt → privilege level change ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip ˆ Store register values (→ next slide) ˆ Old thread executes Scheduler (code to switch to a new thread) ˆ Context switch to a new thread

Context Switch: Old Thread → New Thread www.tugraz.at

112 ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip ˆ Store register values (→ next slide) ˆ Old thread executes Scheduler (code to switch to a new thread) ˆ Context switch to a new thread

Context Switch: Old Thread → New Thread www.tugraz.at

ˆ Caused only by an interrupt → privilege level change

112 ˆ Store register values (→ next slide) ˆ Old thread executes Scheduler (code to switch to a new thread) ˆ Context switch to a new thread

Context Switch: Old Thread → New Thread www.tugraz.at

ˆ Caused only by an interrupt → privilege level change ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip

112 ˆ Old thread executes Scheduler (code to switch to a new thread) ˆ Context switch to a new thread

Context Switch: Old Thread → New Thread www.tugraz.at

ˆ Caused only by an interrupt → privilege level change ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip ˆ Store register values (→ next slide)

112 ˆ Context switch to a new thread

Context Switch: Old Thread → New Thread www.tugraz.at

ˆ Caused only by an interrupt → privilege level change ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip ˆ Store register values (→ next slide) ˆ Old thread executes Scheduler (code to switch to a new thread)

112 Context Switch: Old Thread → New Thread www.tugraz.at

ˆ Caused only by an interrupt → privilege level change ˆ CPU pushes to stack: ss, rsp, rflags, cs, rip ˆ Store register values (→ next slide) ˆ Old thread executes Scheduler (code to switch to a new thread) ˆ Context switch to a new thread

112 1. Push all CPU register values on the stack

ˆ No modification of instruction pointer / stack pointer: ˆ rip and rsp were already pushed to the stack by CPU

2. Pop all CPU register values into a struct

3. Set currentThreadInfo, etc. to kernel thread

Context Switch: Store register values www.tugraz.at

113 ˆ No modification of instruction pointer / stack pointer: ˆ rip and rsp were already pushed to the stack by CPU

2. Pop all CPU register values into a struct

3. Set currentThreadInfo, etc. to kernel thread

Context Switch: Store register values www.tugraz.at

1. Push all CPU register values on the stack

113 ˆ rip and rsp were already pushed to the stack by CPU

2. Pop all CPU register values into a struct

3. Set currentThreadInfo, etc. to kernel thread

Context Switch: Store register values www.tugraz.at

1. Push all CPU register values on the stack

ˆ No modification of instruction pointer / stack pointer:

113 2. Pop all CPU register values into a struct

3. Set currentThreadInfo, etc. to kernel thread

Context Switch: Store register values www.tugraz.at

1. Push all CPU register values on the stack

ˆ No modification of instruction pointer / stack pointer: ˆ rip and rsp were already pushed to the stack by CPU

113 3. Set currentThreadInfo, etc. to kernel thread

Context Switch: Store register values www.tugraz.at

1. Push all CPU register values on the stack

ˆ No modification of instruction pointer / stack pointer: ˆ rip and rsp were already pushed to the stack by CPU

2. Pop all CPU register values into a struct

113 Context Switch: Store register values www.tugraz.at

1. Push all CPU register values on the stack

ˆ No modification of instruction pointer / stack pointer: ˆ rip and rsp were already pushed to the stack by CPU

2. Pop all CPU register values into a struct

3. Set currentThreadInfo, etc. to kernel thread

113 currentThreadRegisters www.tugraz.at

struct ArchThreadRegisters { uint64 rip; //0 uint64 r12; // 120 uint64 cs; //8 uint64 r13; // 128 uint64 rflags; // 16 uint64 r14; // 136 uint64 rax; // 24 uint64 r15; // 144 uint64 rcx; // 32 uint64 ds; // 152 uint64 rdx; // 40 uint64 es; // 160 uint64 rbx; // 48 uint64 fs; // 168 uint64 rsp; // 56 uint64 gs; // 176 uint64 rbp; // 64 uint64 ss; // 184 uint64 rsi; // 72 uint64 dpl; // 192 uint64 rdi; // 80 uint64 rsp0; // 200 uint64 r8; // 88 uint64 ss0; // 208 uint64 r9; // 96 uint64 cr3; // 216 uint64 r10; // 104 uint32 fpu[28]; // 224 uint64 r11; // 112 };

114 1. “Restore” CPU register values 1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack 1.2 iretq pops values from stack into the registers 2. Instruction pointer has a new value, execution continues there

Context Switch: Old Thread → New Thread www.tugraz.at

115 1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack 1.2 iretq pops values from stack into the registers 2. Instruction pointer has a new value, execution continues there

Context Switch: Old Thread → New Thread www.tugraz.at

1. “Restore” CPU register values

115 2. Instruction pointer has a new value, execution continues there

Context Switch: Old Thread → New Thread www.tugraz.at

1. “Restore” CPU register values 1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack 1.2 iretq pops values from stack into the registers

115 Context Switch: Old Thread → New Thread www.tugraz.at

1. “Restore” CPU register values 1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack 1.2 iretq pops values from stack into the registers 2. Instruction pointer has a new value, execution continues there

115 Context Switch www.tugraz.at

Looks identical for 64 bits

116 Act as if:

ˆ Thread was running already ˆ We are returning from an interrupt

Context Switch: New Thread/First Thread www.tugraz.at

117 Context Switch: New Thread/First Thread www.tugraz.at

Act as if:

ˆ Thread was running already ˆ We are returning from an interrupt

117 1. Push stored register values to stack (modifies registers) 2. “Restore” CPU register values as before 3. Instruction pointer has a new value, execution continues there

Context Switch: New Thread/First Thread www.tugraz.at

118 2. “Restore” CPU register values as before 3. Instruction pointer has a new value, execution continues there

Context Switch: New Thread/First Thread www.tugraz.at

1. Push stored register values to stack (modifies registers)

118 3. Instruction pointer has a new value, execution continues there

Context Switch: New Thread/First Thread www.tugraz.at

1. Push stored register values to stack (modifies registers) 2. “Restore” CPU register values as before

118 Context Switch: New Thread/First Thread www.tugraz.at

1. Push stored register values to stack (modifies registers) 2. “Restore” CPU register values as before 3. Instruction pointer has a new value, execution continues there

118 ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow ˆ bad memory access

ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt)

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock

119 ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow ˆ bad memory access

ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt)

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device

119 ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow ˆ bad memory access

ˆ cpu fault (trap / interrupt)

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80)

119 ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow ˆ bad memory access

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt)

119 ˆ divide by 0 ˆ integer overflow ˆ bad memory access

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt) ˆ executing privileged instruction

119 ˆ integer overflow ˆ bad memory access

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt) ˆ executing privileged instruction ˆ divide by 0

119 ˆ bad memory access

Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt) ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow

119 Context Switches: When and why? www.tugraz.at

Interrupts:

ˆ clock ˆ device ˆ system call (syscall / sysenter / int 0x80) ˆ cpu fault (trap / interrupt) ˆ executing privileged instruction ˆ divide by 0 ˆ integer overflow ˆ bad memory access

119 Process and Thread Organization ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process

ˆ an instance of a program ˆ restricted to its own boundaries and rights

ˆ a mold for a process ˆ Thread: an execution context

ˆ Process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data

120 ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process

ˆ an instance of a program ˆ restricted to its own boundaries and rights

ˆ Thread: an execution context

ˆ Process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process

120 ˆ an instance of a program ˆ restricted to its own boundaries and rights

ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process ˆ Process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context

120 ˆ an instance of a program ˆ restricted to its own boundaries and rights

ˆ if part of a process: restricted to the boundaries of a process ˆ Process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context ˆ a sequence of instructions

120 ˆ an instance of a program ˆ restricted to its own boundaries and rights

ˆ Process: a container for threads and memory contents of a program

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process

120 ˆ an instance of a program ˆ restricted to its own boundaries and rights

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process ˆ Process: a container for threads and memory contents of a program

120 ˆ restricted to its own boundaries and rights

Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process ˆ Process: a container for threads and memory contents of a program ˆ an instance of a program

120 Program, Process, Thread www.tugraz.at

ˆ Program: a binary file containing code and data ˆ a mold for a process ˆ Thread: an execution context ˆ a sequence of instructions ˆ if part of a process: restricted to the boundaries of a process ˆ Process: a container for threads and memory contents of a program ˆ an instance of a program ˆ restricted to its own boundaries and rights

120 ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID

121 ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename

121 ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader)

121 ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors

121 ˆ Accounting ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register)

121 ˆ Threads ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting

121 ˆ Child processes?

Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads

121 Process Resources www.tugraz.at

A process is a container.

ˆ Process ID ˆ Filename ˆ Program file (Loader) ˆ (Open) file descriptors ˆ Address space (ArchMemory, CR3 register) ˆ Accounting ˆ Threads ˆ Child processes?

121 ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3)

ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function)

ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID

122 ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3)

ˆ A set of register values (defining the state of the execution of the userspace thread function)

ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . )

122 ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3) ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function)

122 ˆ Some register values are process-specific and not thread-specific (e.g. CR3) ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function) ˆ Not all registers are different

122 ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function) ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3)

122 ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function) ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3) ˆ A user stack

122 ˆ A second set of register values for the kernel (for syscalls)

Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function) ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3) ˆ A user stack ˆ A kernel stack (for syscalls)

122 Thread Resources www.tugraz.at

A thread is a unit for execution.

ˆ Thread ID ˆ Thread state (Running, Sleeping, . . . ) ˆ A set of register values (defining the state of the execution of the userspace thread function) ˆ Not all registers are different ˆ Some register values are process-specific and not thread-specific (e.g. CR3) ˆ A user stack ˆ A kernel stack (for syscalls) ˆ A second set of register values for the kernel (for syscalls)

122 ˆ 1 initial thread ˆ executes the main()-function ˆ it’s not a “main”-thread ˆ process may start further threads if required (how?)

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

123 ˆ executes the main()-function ˆ it’s not a “main”-thread ˆ process may start further threads if required (how?)

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

ˆ 1 initial thread

123 ˆ it’s not a “main”-thread ˆ process may start further threads if required (how?)

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

ˆ 1 initial thread ˆ executes the main()-function

123 ˆ process may start further threads if required (how?)

Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

ˆ 1 initial thread ˆ executes the main()-function ˆ it’s not a “main”-thread

123 Process and Thread Interaction www.tugraz.at

Load program, create process, . . .

ˆ 1 initial thread ˆ executes the main()-function ˆ it’s not a “main”-thread ˆ process may start further threads if required (how?)

123 ˆ Pure Userspace Threading: lightweight, but many drawbacks

ˆ As illustrated before: works by creative and clever usage of interrupts ˆ Threads can be implemented with and without support of the operating system

ˆ Threads can be implemented with and without support of the CPU (int 0x80 vs. sysenter/syscall)

Threads www.tugraz.at

ˆ “There is no such thing as a thread” at the CPU-level

124 ˆ Pure Userspace Threading: lightweight, but many drawbacks

ˆ Threads can be implemented with and without support of the operating system

ˆ Threads can be implemented with and without support of the CPU (int 0x80 vs. sysenter/syscall)

Threads www.tugraz.at

ˆ “There is no such thing as a thread” at the CPU-level ˆ As illustrated before: works by creative and clever usage of interrupts

124 ˆ Pure Userspace Threading: lightweight, but many drawbacks ˆ Threads can be implemented with and without support of the CPU (int 0x80 vs. sysenter/syscall)

Threads www.tugraz.at

ˆ “There is no such thing as a thread” at the CPU-level ˆ As illustrated before: works by creative and clever usage of interrupts ˆ Threads can be implemented with and without support of the operating system

124 ˆ Threads can be implemented with and without support of the CPU (int 0x80 vs. sysenter/syscall)

Threads www.tugraz.at

ˆ “There is no such thing as a thread” at the CPU-level ˆ As illustrated before: works by creative and clever usage of interrupts ˆ Threads can be implemented with and without support of the operating system ˆ Pure Userspace Threading: lightweight, but many drawbacks

124 Threads www.tugraz.at

ˆ “There is no such thing as a thread” at the CPU-level ˆ As illustrated before: works by creative and clever usage of interrupts ˆ Threads can be implemented with and without support of the operating system ˆ Pure Userspace Threading: lightweight, but many drawbacks ˆ Threads can be implemented with and without support of the CPU (int 0x80 vs. sysenter/syscall)

124 (that’s how it started) ˆ Implement threads in userspace as library ˆ can be implemented in all operating systems

Pure Userspace Threads www.tugraz.at

ˆ Kernel has no concept of threads and no idea they might exist

125 Pure Userspace Threads www.tugraz.at

ˆ Kernel has no concept of threads and no idea they might exist (that’s how it started) ˆ Implement threads in userspace as library ˆ can be implemented in all operating systems

125 Userspace Threads www.tugraz.at

126 ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

ˆ call method in libc to check: thread going to block?

ˆ user-mode-runtime-system in libc! ˆ function that might block the thread

Userspace Threads www.tugraz.at

ˆ process manages threads

127 ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

ˆ call method in libc to check: thread going to block?

ˆ function that might block the thread

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc!

127 ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

ˆ call method in libc to check: thread going to block?

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread

127 ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread ˆ call method in libc to check: thread going to block?

127 ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread ˆ call method in libc to check: thread going to block? ˆ YES: save registers in thread table

127 ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread ˆ call method in libc to check: thread going to block? ˆ YES: save registers in thread table ˆ choose other thread ready to run

127 ˆ change stack pointer and instruction pointer (this time jmp)

Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread ˆ call method in libc to check: thread going to block? ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table

127 Userspace Threads www.tugraz.at

ˆ process manages threads ˆ user-mode-runtime-system in libc! ˆ function that might block the thread ˆ call method in libc to check: thread going to block? ˆ YES: save registers in thread table ˆ choose other thread ready to run ˆ load chosen the thread’s registers from thread table ˆ change stack pointer and instruction pointer (this time jmp)

127 ˆ no system calls for thread handling ˆ thread-switches are very fast ˆ no change of memory configuration when switching threads ˆ can use specialized scheduling

Advantages www.tugraz.at

ˆ Advantages

128 ˆ thread-switches are very fast ˆ no change of memory configuration when switching threads ˆ can use specialized scheduling

Advantages www.tugraz.at

ˆ Advantages ˆ no system calls for thread handling

128 ˆ no change of memory configuration when switching threads ˆ can use specialized scheduling

Advantages www.tugraz.at

ˆ Advantages ˆ no system calls for thread handling ˆ thread-switches are very fast

128 ˆ can use specialized scheduling

Advantages www.tugraz.at

ˆ Advantages ˆ no system calls for thread handling ˆ thread-switches are very fast ˆ no change of memory configuration when switching threads

128 Advantages www.tugraz.at

ˆ Advantages ˆ no system calls for thread handling ˆ thread-switches are very fast ˆ no change of memory configuration when switching threads ˆ can use specialized scheduling

128 ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant

ˆ e.g. select-Systemcall

ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block

ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block

129 ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant

ˆ e.g. select-Systemcall

ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block

ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking

129 ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant

ˆ e.g. select-Systemcall

ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ sometimes you can find out if a syscall would block

ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs...

129 ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant

ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ e.g. select-Systemcall

ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block

129 ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall

129 ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select

129 ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ not very efficient and elegant ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later

129 ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

ˆ Sometimes not

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant

129 ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not

129 ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not ˆ Page faults

129 ˆ if thread has an endless loop and does not free CPU...

Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not ˆ Page faults ˆ if page not in memory, process will block

129 Disadvantages www.tugraz.at

ˆ threads are not allowed to make direct syscalls since they might block ˆ ... one could make system-calls non-blocking ˆ ... but since this should work with unchanged (and unaware) OSs... ˆ sometimes you can find out if a syscall would block ˆ e.g. select-Systemcall ˆ before read is called: call select ˆ should read block: switch threads and check again later ˆ not very efficient and elegant ˆ Sometimes not ˆ Page faults ˆ if page not in memory, process will block ˆ if thread has an endless loop and does not free CPU...

129 Implementation www.tugraz.at

Two and a half options:

ˆ Userspace ˆ Kernelspace ˆ Mixed

130 ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code

ˆ takes longer than before ˆ thread-recycling

ˆ less code the user can break ˆ thread management in kernel

ˆ thread creation and management via syscall

kernel mode threads www.tugraz.at

ˆ No runtime system needed

131 ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code

ˆ takes longer than before ˆ thread-recycling

ˆ thread management in kernel

ˆ thread creation and management via syscall

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break

131 ˆ takes longer than before ˆ thread-recycling

ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code ˆ thread creation and management via syscall

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel

131 ˆ takes longer than before ˆ thread-recycling

ˆ but: kernel programmers by definition only write safe code ˆ thread creation and management via syscall

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel ˆ more or less as in userspace

131 ˆ takes longer than before ˆ thread-recycling

ˆ thread creation and management via syscall

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code

131 ˆ takes longer than before ˆ thread-recycling

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code ˆ thread creation and management via syscall

131 ˆ thread-recycling

kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code ˆ thread creation and management via syscall ˆ takes longer than before

131 kernel mode threads www.tugraz.at

ˆ No runtime system needed ˆ less code the user can break ˆ thread management in kernel ˆ more or less as in userspace ˆ but: kernel programmers by definition only write safe code ˆ thread creation and management via syscall ˆ takes longer than before ˆ thread-recycling

131 Kernel mode threads www.tugraz.at

132 Hybrid solution www.tugraz.at

133 Thread states www.tugraz.at

134 Threads states www.tugraz.at

135 Thread states www.tugraz.at

136 Thread states www.tugraz.at

137 Thread states www.tugraz.at

138 Thread states www.tugraz.at

139 Thread states www.tugraz.at

140 Context Switching www.tugraz.at

141 ˆ also: start of a scheduled batch job (cronjob, how?)

ˆ at request of a user (how?)

Process Creation www.tugraz.at

ˆ at boot time (kernel threads, init processes)

142 ˆ also: start of a scheduled batch job (cronjob, how?)

Process Creation www.tugraz.at

ˆ at boot time (kernel threads, init processes) ˆ at request of a user (how?)

142 Process Creation www.tugraz.at

ˆ at boot time (kernel threads, init processes) ˆ at request of a user (how?) ˆ also: start of a scheduled batch job (cronjob, how?)

142 ˆ Windows: CreateProcess (new image) ˆ SWEB: fork (as soon as you have implemented it)

Process Creation at request of a user www.tugraz.at

via Syscall!

ˆ UNIX/Linux: fork (exact copy)

143 ˆ SWEB: fork (as soon as you have implemented it)

Process Creation at request of a user www.tugraz.at

via Syscall!

ˆ UNIX/Linux: fork (exact copy) ˆ Windows: CreateProcess (new image)

143 Process Creation at request of a user www.tugraz.at

via Syscall!

ˆ UNIX/Linux: fork (exact copy) ˆ Windows: CreateProcess (new image) ˆ SWEB: fork (as soon as you have implemented it)

143 What does the fork do? www.tugraz.at

Check http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html!!

144 Process Creation via fork (on Unix / Linux / SWEB) www.tugraz.at

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html:

pid_t fork(void); The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below:

ˆ unique PID ˆ copy of file descriptors ˆ semaphore state is copied ˆ shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. ˆ parent and the child processes shall be capable of executing independently before either one terminates. ˆ ...

145 fork/exec Return Value www.tugraz.at

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html:

pid_t fork(void); Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

146 ˆ parent knows the child ˆ parent waits for child to die (waitpid)

Fork www.tugraz.at

pid_t child_pid; child_pid= fork(); if (child_pid == -1) { printf("fork failed\n"); } else if (child_pid == 0) { ˆ child does not know the parent printf("i’m the child\n"); } else { printf("i’m the parent\n"); waitpid(child_pid,0,0); // wait for child to die }

147 ˆ parent waits for child to die (waitpid)

Fork www.tugraz.at

pid_t child_pid; child_pid= fork(); if (child_pid == -1) { printf("fork failed\n"); } else if (child_pid == 0) { ˆ child does not know the parent printf("i’m the child\n"); ˆ parent knows the child } else { printf("i’m the parent\n"); waitpid(child_pid,0,0); // wait for child to die }

147 Fork www.tugraz.at

pid_t child_pid; child_pid= fork(); if (child_pid == -1) { printf("fork failed\n"); } else if (child_pid == 0) { ˆ child does not know the parent printf("i’m the child\n"); ˆ parent knows the child } else { printf("i’m the parent\n"); ˆ parent waits for child to die (waitpid) waitpid(child_pid,0,0); // wait for child to die }

147 ˆ Error exit (return value: non-zero) ˆ Fatal error (e.g. ) ˆ Killed by another process

Process Termination www.tugraz.at

ˆ Normal exit (return value: zero)

148 ˆ Fatal error (e.g. segmentation fault) ˆ Killed by another process

Process Termination www.tugraz.at

ˆ Normal exit (return value: zero) ˆ Error exit (return value: non-zero)

148 ˆ Killed by another process

Process Termination www.tugraz.at

ˆ Normal exit (return value: zero) ˆ Error exit (return value: non-zero) ˆ Fatal error (e.g. segmentation fault)

148 Process Termination www.tugraz.at

ˆ Normal exit (return value: zero) ˆ Error exit (return value: non-zero) ˆ Fatal error (e.g. segmentation fault) ˆ Killed by another process

148 all children, grand-children, grand-grand-children, . . . , die aswell ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

ˆ process groups in UNIX/Linux ˆ doesn’t exist in Windows

ˆ when parent dies,

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking

Implicit parent-child hierarchy on Unix/Linux:

149 all children, grand-children, grand-grand-children, . . . , die aswell ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

ˆ doesn’t exist in Windows

ˆ when parent dies,

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking ˆ process groups in UNIX/Linux

Implicit parent-child hierarchy on Unix/Linux:

149 all children, grand-children, grand-grand-children, . . . , die aswell ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

ˆ when parent dies,

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking ˆ process groups in UNIX/Linux ˆ doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

149 ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

all children, grand-children, grand-grand-children, . . . , die aswell

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking ˆ process groups in UNIX/Linux ˆ doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

ˆ when parent dies,

149 ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

all children, grand-children, grand-grand-children, . . . , die aswell

Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking ˆ process groups in UNIX/Linux ˆ doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

ˆ when parent dies,

149 Process Hierarchies www.tugraz.at

Some operating systems have hierarchies:

ˆ implicit hierarchy from forking ˆ process groups in UNIX/Linux ˆ doesn’t exist in Windows

Implicit parent-child hierarchy on Unix/Linux:

ˆ when parent dies, all children, grand-children, grand-grand-children, . . . , die aswell ˆ UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.

149 Process/Thread State www.tugraz.at

git grep TODO | sort

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input ˆ what does the sort do in the meantime?

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input ˆ what does the sort do in the meantime? ˆ loop and check (busy wait)

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input ˆ what does the sort do in the meantime? ˆ loop and check (busy wait) ˆ sleep and get woken up

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input ˆ what does the sort do in the meantime? ˆ loop and check (busy wait) ˆ sleep and get woken up ˆ blocking the process makes sense

150 Process/Thread State www.tugraz.at

git grep TODO | sort

ˆ sort has to wait for input ˆ what does the sort do in the meantime? ˆ loop and check (busy wait) ˆ sleep and get woken up ˆ blocking the process makes sense ˆ do we actually block the process?

150 Using Processes and Threads ˆ Less independent than processes ˆ No protection

Processes vs. Threads www.tugraz.at

ˆ Threads are more lightweight than processes

151 ˆ No protection

Processes vs. Threads www.tugraz.at

ˆ Threads are more lightweight than processes ˆ Less independent than processes

151 Processes vs. Threads www.tugraz.at

ˆ Threads are more lightweight than processes ˆ Less independent than processes ˆ No protection

151 ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving

ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ Example: text processing

ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ automatically saving ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

ˆ Some of these things may block

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving

152 ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block

152 ˆ wait for disk ˆ etc.

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block ˆ wait for mouse-click / keyboard press

152 ˆ etc.

Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block ˆ wait for mouse-click / keyboard press ˆ wait for disk

152 Why threads? www.tugraz.at

ˆ Things should happen in parallel - even within one application ˆ Example: text processing ˆ typing ˆ spell checking ˆ formatting on screen ˆ automatically saving ˆ Some of these things may block ˆ wait for mouse-click / keyboard press ˆ wait for disk ˆ etc.

152 ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads)

ˆ No need to reconfigure memory

ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space

ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier

153 ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads)

ˆ No need to reconfigure memory

ˆ Like with processes ˆ But they cooperate easily because of the shared address space

ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks

153 ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads)

ˆ No need to reconfigure memory

ˆ But they cooperate easily because of the shared address space

ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes

153 ˆ No need to reconfigure memory

ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads) ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space

153 ˆ No need to reconfigure memory

ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads) ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space ˆ Consumes less memory

153 ˆ No need to reconfigure memory

ˆ Switching between threads can be faster

ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads)

153 ˆ No need to reconfigure memory ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads) ˆ Switching between threads can be faster

153 ˆ May achieve better performance

Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads) ˆ Switching between threads can be faster ˆ No need to reconfigure memory

153 Why threads www.tugraz.at

ˆ Make programming easier ˆ Split tasks in different blocks ˆ Like with processes ˆ But they cooperate easily because of the shared address space ˆ Consumes less memory ˆ Attention: do not confuse shared memory (between processes) with shared address space (between threads) ˆ Switching between threads can be faster ˆ No need to reconfigure memory ˆ May achieve better performance

153 Example www.tugraz.at

154 Example www.tugraz.at

while (TRUE) { get_next_request(&buf); handoff_work(&buf); }

while (TRUE) { wait_for_work(&buf); look_for_page_in_cache(&buf,&page); if (page_not_in_cache(&page)) read_page_from_disk(&buf,&page); return_page(&page); }

155 ˆ complicated program structure ˆ read content from disk may block process ˆ non-blocking read (polling!) decreases performance

Without Threads www.tugraz.at

Without threads,

ˆ just one thread

156 ˆ read content from disk may block process ˆ non-blocking read (polling!) decreases performance

Without Threads www.tugraz.at

Without threads,

ˆ just one thread ˆ complicated program structure

156 ˆ non-blocking read (polling!) decreases performance

Without Threads www.tugraz.at

Without threads,

ˆ just one thread ˆ complicated program structure ˆ read content from disk may block process

156 Without Threads www.tugraz.at

Without threads,

ˆ just one thread ˆ complicated program structure ˆ read content from disk may block process ˆ non-blocking read (polling!) decreases performance

156 Non-Blocking Read www.tugraz.at

while (TRUE){ // VERY simplified get_next_event(&buf); if (is_request_event(&buf)) { if (page_not_in_cache(&page)) { request_page_from_disk(&buf,&page); save_request_in_table(&buf); } else { return_page(&page); } } else if (is_disk_event(&buf)) { find_request_in_table(&buf); mark_requeust_as_done(&buf); return_page(&page); } else if (is_...

157 ˆ Actually simulates threads ˆ Better: use multithreading

Non-Blocking Read www.tugraz.at

ˆ Finite-state-machine!

158 ˆ Better: use multithreading

Non-Blocking Read www.tugraz.at

ˆ Finite-state-machine! ˆ Actually simulates threads

158 Non-Blocking Read www.tugraz.at

ˆ Finite-state-machine! ˆ Actually simulates threads ˆ Better: use multithreading

158 ˆ for the hardware: there is only 1 thread and a lot of interrupts ˆ for the userspace: we can have an arbitrary number of threads

ˆ Threads divide processor time amongst themsevles (and a few resources) ˆ Building block of modern multi-threading are context switches ˆ Operating system creates illusions

Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time)

159 ˆ for the hardware: there is only 1 thread and a lot of interrupts ˆ for the userspace: we can have an arbitrary number of threads

ˆ Building block of modern multi-threading are context switches ˆ Operating system creates illusions

Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time) ˆ Threads divide processor time amongst themsevles (and a few resources)

159 ˆ for the hardware: there is only 1 thread and a lot of interrupts ˆ for the userspace: we can have an arbitrary number of threads

ˆ Operating system creates illusions

Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time) ˆ Threads divide processor time amongst themsevles (and a few resources) ˆ Building block of modern multi-threading are context switches

159 ˆ for the hardware: there is only 1 thread and a lot of interrupts ˆ for the userspace: we can have an arbitrary number of threads

Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time) ˆ Threads divide processor time amongst themsevles (and a few resources) ˆ Building block of modern multi-threading are context switches ˆ Operating system creates illusions

159 ˆ for the userspace: we can have an arbitrary number of threads

Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time) ˆ Threads divide processor time amongst themsevles (and a few resources) ˆ Building block of modern multi-threading are context switches ˆ Operating system creates illusions ˆ for the hardware: there is only 1 thread and a lot of interrupts

159 Take Aways www.tugraz.at

ˆ Processes divide resources amongst themselves (except processor time) ˆ Threads divide processor time amongst themsevles (and a few resources) ˆ Building block of modern multi-threading are context switches ˆ Operating system creates illusions ˆ for the hardware: there is only 1 thread and a lot of interrupts ˆ for the userspace: we can have an arbitrary number of threads

159

Bibliography www.tugraz.at

[1] Edsger Dijkstra. The structure of the multiprogramming system. Communications of the ACM, 1968. [2] Thomas Haigh. Multicians.org and the history of operating systems. online, 9 2002. URL http://www.cbi.umn.edu/iterations/haigh.html. [3] . Classic Operating Systems: From Batch Processing to Distributed Systems. Springer, 2001. [4] Tom Kilburn, Bruce Payne, and David Howarth. The atlas supervisor. AFIPS Computer Conference 20, 1961. in [Hansen 2001]. [5] Emerson Pugh, Lyle Johnson, and John Palmer. IBM’s 360 and Early 370 Systems (History of Computing). The MIT Press, 2003.

160