operating systems wt 2019/20 · operating systems 9 user mode vs. kernel mode cpu operational modes...

22
Operating Systems WT 2019/20 Operating System Concepts and Principles

Upload: others

Post on 12-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems WT 2019/20Operating System Concepts and Principles

Page 2: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 2

Tasks of an Operating System● Memory Management

● Translation between Virtual and Physical Memory● Swapping / Paging

● Processor Management● Scheduling – Goals: Utilization / Fairness / Responsiveness / ...● Dispatching – Switching Thread Contexts

● Storage Management● File Systems, Access to external Media

Page 3: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 3

Tasks of an Operating System● Device Management

● Organize Drivers / Services● Processing of Hardware Interrupts

● User Management / Security● Access Control / Authorization / Authentication● Multi-User Abstractions

● (Real-) Time Management● Batch Processing / Job Control

Page 4: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 4

Tasks of an Operating System● Hardware Abstraction

● Low-Level: Context Switches, Memory Maps● High-Level: Standard APIs mapped to specific driver calls

● Power Management● Relevant for battery powered devices / green IT

● Fault Tolerance● strong Isolation of activities from each other and the system● Kernel reacts to Errors – Service Restarts / Bluescreen

Page 5: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 5

Design Goals● Extensibility / Maintainability

● adapt to changing requirements and systems● ease of development

● Portability● support for multiple CPU architectures● Codebase largely System-Independent C code

● Compatibility● Run executables built for previous versions seamlessly

Page 6: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 6

Design Goals● Reliability / Resilience

● Protection against internal malfunction and external attacks● erratic Application behaviour should not degrade system

● Performance / Scalability● System should be reasonably fast and responsive● should make use of and benefit from additional resources

Page 7: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 7

Structure of an Operating System● Computer Systems consist of Layers

Physical devices

Microprogramming

Machine language

Operating system

Compilers Editors Command interpreter

Banking system

Airline reservation Web browser Application programs

Hardware

System programs

Page 8: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 8

Structure of an Operating System● Operating Systems also consist of Layers

Hardware Abstraction

Kernel

Executive / Interface

Libraries

Linkers / Loaders Services Command

interpreter

Banking System

Airline reservation

Compilers / Editors

Kernel Mode

User Mode

Drivers

Page 9: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 9

User Mode vs. Kernel Mode● CPU Operational Modes with different Hardware Privileges

● Privileged Instructions executed in User Mode are illegal● System Control (CPU Context, System Memory, …) restricted● This enables process isolation

● Mode switch from Kernel to User is voluntary● Mode switch from User to Kernel is the result of an Interrupt

● trap / syscall / exception ● hardware event, e.g. Timer

Page 10: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 10

Interrupt HandlingInterrupt dispatch routine

Disable interrupts

Record machine state (trap frame) to allow resume

Mask equal- and lower-IRQL interrupts

Find and call appropriate ISR

Dismiss interrupt

Restore machine state (including mode and enabled interrupts)

Tell the device to stop interruptingInterrogate device state, start next operation on device, etc. Propagate resultsReturn to caller

Interrupt service routine

interrupt !

user or kernel mode

code

kernel mode

Page 11: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 11

Interrupt Handling● incoming interrupts handled in Interrupt Service Routine (ISR)

● does not trigger a context switch – previous thread context stays active, but CPU state is saved and later restored

● Interrupt Handling is uninterruptable● priority inversion – long running low priority ISR will block

high priority interrupt from being handled● ISR should be as short as possible

● Watch interrupt behavior on Linux with the command:$> dstat -t --top-int

Page 12: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 12

System Calls● generally invoked by a call to a library function

● e.g. open(), CreateFile()● Implementation stores arguments and triggers a trap

● Synchronous Software Interrupt● Interrupt Service Routine (ISR) will dispatch within the kernel

executive by the syscall number● executing thread paused

Page 13: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 13

Windows Subsystems● Windows has multiple Personalities

● Programs interface with Subsystems instead of native API● Subsystems are documented and interface to undocumented

windows system service calls● each Executable associated with one Subsystem ● e.g. OS/2 (until Win2k), POSIX (until WinXP)

● POSIX Subsystem later became Windows Services for Unix, which evolved into Linux Subsystem for Windows

Page 14: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 14

Windows Subsystems● Subsystem DLL and stateful Environment Subsystem Process● Providing distinct:

● Programming interface● File system syntax● Process semantics

● Example: CreateFile in Kernel32.Dll calls native NtCreateFile● Windows Environment Subsystem Process needs to be running,

otherwise the system crashes.

Page 15: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 15

Subsystem Service Calls● “System Calls” with slightly different semantics● Functions fall into one of three categories:

1) Entirely implemented in User Mode (Subsystem Library)● e.g. some geometry functions like PtInRect(), IsRectEmpty()

2) Requires one or more syscalls to the Executive● e.g. ReadFile(), WriteFile(); made in Subsystem Library

3) Requires work in Environment Subsystem Process● e.g. CreateFile(); through Inter-Process Communication

Page 16: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 16

Subsystem Service Calls

Application Subsystem Process

Subsystem Library

Inter-Process Communication

Kernel Mode

User Mode

Executive

Page 17: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 17

Example: The Windows Subsystem● CreateFile() in Kernel32.dll

● Part of the Windows Environment Subsystem

● Translates a pathname to a file location to open the file● Working Directories and Drive letters (C:, D:, …) are stored in

the subsystem process state● requires Inter-Process Communication through Local

Procedure Calls to access subsystem state

Page 18: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 18

Windows Subsystems

Re-implementation of a simple subsystem library and process on linux is located in the environment_subsystem project on gitlab:

https://gitlab.hpi.de/osm-teaching/opsys19labs/environment_subsystem

Page 19: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 19

Recap: Structure of an Operating System

Hardware Abstraction

Kernel

Executive / Interface

Libraries

Linkers / Loaders Services Command

interpreter

Banking System

Airline reservation

Compilers / Editors

Kernel Mode

User Mode

Drivers

Page 20: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 20hardware interfaces (buses, I/O devices, interrupts, interval timers, DMA, memory cache control, etc., etc.)

Original copyright by Microsoft Corporation. Used by permission.

Structure of the Windows Operating System

System Service Dispatcher

Task ManagerExplorer

SvcHost.ExeWinMgt.Exe

SpoolSv.Exe

ServiceControl Mgr.

LSASS

ObjectM

gr.

WindowsUSER,

GDI

File System C

ache

I/O Mgr

UserApplication

Subsystem DLLs

System Processes Services Applications

SystemThreads

UserMode

KernelMode

NTDLL.DLL

Device &File Sys.Drivers

WinLogon

Session Manager

Services.Exe POSIXWindows DLLs

Plug andPlay M

gr.

Power

Mgr.

SecurityR

eferenceM

onitor

VirtualM

emory

Processes&

Threads

LocalProcedure

Call Graphics

Drivers

Kernel

Hardware Abstraction Layer (HAL)

(kernel mode callable interfaces)

Configura-tion M

gr(registry)

OS/2

Windows

Page 21: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 21

Structure of the Linux Operating System

Page 22: Operating Systems WT 2019/20 · Operating Systems 9 User Mode vs. Kernel Mode CPU Operational Modes with different Hardware Privileges Privileged Instructions executed in User Mode

Operating Systems 22