Architectural Support for Operating Systemscsl.skku.edu/uploads/SSE3044F15/2-arch.pdf · 2015. 9....
Transcript of Architectural Support for Operating Systemscsl.skku.edu/uploads/SSE3044F15/2-arch.pdf · 2015. 9....
Architectural Support for Operating Systems
Jinkyu Jeong ([email protected])
Computer Systems Laboratory Sungkyunkwan University
http://csl.skku.edu
2 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Today’s Topics
§ Basic services of OS
§ Basic computer system architecture § Architectural support for OS
§ Operating system structures
3 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS Internals (1)
Kernel
Arch-‐dependent kernel code
System Call Interface
Hardware Pla;orm
C Library (libc)
User ApplicaBon
User space
Kernel space
4 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Operating System Services (1) § Operating systems provide
• An execution environment to programs • Helpful services to programs and users
§ Operating-system services • User interface (UI)
– Varies between Command-Line (CLI), Graphics User Interface (GUI), Batch
• Program execution – Load a program into memory and to run that program – End execution, either normally or abnormally
• I/O operations – A running program may require I/O, which may involve a file
or an I/O device
5 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Operating System Services (2) § Operating-system services (cont’d)
• File-system manipulation – Programs need to read, write, create and delete files and
directories – Search, list, and permission management
• Communications – Processes may exchange information, on the same computer or
between computers over a network – Via shared memory or through message passing
• Error detection – Being constantly aware of possible errors – In CPU, memory, I/O devices and user programs – OS should take the appropriate action to ensure correct and
consistent computing – Debugging support (ex. core dump)
6 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Operating System Services (3) § Another set of OS functions
• Resource allocation – Resources must be allocated to multiple users and jobs running
concurrently – Many types of resources - CPU cycles, main memory, file
storage, I/O devices. • Accounting
– To keep track of which users use how much and what kinds of computer resources
• Protection and security – Concurrent processes should not interfere with each other – Protection
» All access to system resources is controlled – Security
» User authentication, depending external I/O devices from invalid access attempts
7 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS Internals (2)
Hardware
System Call Interface
shell shell
ls ps
Hardware Control (Interrupt handling, etc.)
File System Management
I/O Management (device drivers)
Memory Management
Process Management
ProtecBon
Kernel space
User space trap
scheduler IPC
synchronizaBon
8 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Computer Systems (1) § Computer system organization
9 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Computer Systems (2) § Characteristics
• I/O devices and CPU can execute concurrently • Each device controller is in charge of a particular
device type • Each device controller has a local buffer • CPU moves data from/to main memory to/from local
buffers • I/O is from the device to local buffer of controller • CPU issues specific commands to I/O devices • CPU should be able to know whether the issued
command has been completed or not
10 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS and Architecture § Mutual interaction
• The functionality of an OS is limited by architectural features. – Multiprocessing on DOS/8086?
• The structure of an OS can be simplified by architectural support. – Interrupt, DMA, synchronization, etc.
• Most proprietary OS’s were developed with the certain architecture in mind. – SunOS/Solaris for SPARC architecture – IBM AIX for Power/PowerPC architecture – HP-UX for PA-RISC architecture – ...
11 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Interrupts (1) § How does the kernel notice an I/O has
finished? • Polling • Hardware interrupt
12 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Interrupts (2) § Interrupt handling
• Preserves the state of the CPU – In a fixed location – In a location indexed by the device
number – On the system stack
• Determines the type – Polling – Vectored interrupt system
• Transfers control to the interrupt service routine (ISR) or interrupt handler
13 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (1) § Interrupts
• Generated by hardware devices – Triggered by a signal in INTR or NMI pins (x86)
• Asynchronous
§ Exceptions • Generated by software executing instructions
– INT instruction in x86
• Synchronous • Exception handling is same as interrupt handling
14 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (2) § Further classification of exceptions
• Traps – Intentional – System calls, breakpoint traps, special instructions, ... – Return control to “next” instruction
• Faults – Unintentional but possibly recoverable – Page faults (recoverable), protection faults (unrecoverable), ... – Either re-execute faulting (“current”) instruction or abort
• Aborts – Unintentional and unrecoverable – Parity error, machine check, ... – Abort the current program
15 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (3) § System calls
• Programming interface to the services provided by OS
• e.g., system call sequence to copy the contents of one file to another
16 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (4) § Important system calls (POSIX & Win32)
Create a new process Wait for a process to exit CreateProcess = fork + execve Terminate execuBon Send a signal
CreateProcess WaitForSingleObject (none) ExitProcess (none)
fork waitpid execve exit kill
Process
Management
Create a file or open an exisBng file Close a file Read data from a file Write data to a file Move the file pointer Get various file aWributes Change the file access permission
CreateFile CloseHandle ReadFile WriteFile SetFilePointer GetFileAWributesEx (none)
open close read write lseek stat chmod
File
Management
Create a new directory Remove an empty directory Make a link to a file Destroy an exisBng file Mount a file system Unmount a file system Change the curent working directory
CreateDirectory RemoveDirectory (none) DeleteFile (none) (none) SetCurrentDirectory
mkdir rmdir link unlink mount umount chdir
File System
Management
17 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (5) § Implementing system calls
18 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Exceptions (6) § Implementing system calls (cont’d)
count = read (fd, buffer, nbytes);
19 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
DMA (1) § Data transfer modes in I/O
• Programmed I/O (PIO) – CPU is involved in moving data between I/O devices and
memory – By special I/O instructions vs. by memory-mapped I/O
• DMA (Direct Memory Access) – Used for high-speed I/O devices able to transmit information
at close to memory speeds – Device controller transfers blocks of data from buffer storage
directly to main memory without CPU intervention. – Only an interrupt is generated per block.
20 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
DMA (2) § Processing I/O requests
21 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Timers § How does the OS take control of CPU from
the running programs? • Use a hardware timer that generates a periodic
interrupt • The timer interrupt transfers control back to OS • The OS preloads the timer with a time to interrupt.
– 10ms for Linux 2.4, 1ms for Linux 2.6 – (cf.) time slice
• The timer is privileged. – Only the OS can load it
22 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Protected Instructions § Protected or privileged instructions
• Direct I/O access – Use privileged instructions or memory-mapping
• Memory state management – Page table updates, page table pointers – TLB loads, etc.
• Setting special “mode bits” • Halt instruction
23 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS Protection (1) § How does the processor know if a protected
instruction should be executed? • The architecture must support at least two modes of
operation: kernel and user mode – 4 privilege levels in IA-32: Ring 0 > 1 > 2 > 3
• Mode is set by a status bit in a protected processor register – User programs in user mode, OS in kernel mode – Current Privilege Level (CPL) in IA-32: CS register
• Protected instructions can only be executed in the kernel mode
24 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS Protection (2) § Crossing protection boundaries
• User programs must call an OS to do something privileged. – OS defines a sequence of system calls
• There must be a system call instruction that: – causes an exception, which invokes a kernel handler – passes a parameter indicating which system call to invoke – saves caller’s state (registers, mode bits) so they can be
restored – OS must verify caller’s parameters (e.g. pointers) – must provide a way to return to user mode when done. – (cf.) INT 0x80 in Linux
25 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
OS Protection (3) § Making a system call
• System call changes mode to kernel • Return from system call resets it to user
26 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Memory Protection (1) § Requirements
• OS must protect user programs from each other – Malicious users
• OS must also protect itself from user programs – Integrity and security
27 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Memory Protection (2) § Simplest scheme
• Use base and limit registers • Base and limit registers are loaded by OS before
starting a program
Prog A
Prog B
Prog C
base reg limit reg
28 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Memory Protection (3) § MMU (Memory Management Unit)
• Memory management hardware provides more sophisticated memory protection mechanisms – base and limit registers – page table pointers, page protection, TLBs – virtual memory – segmentation
• Manipulation of memory management hardware are protected (privileged) operations
29 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Synchronization § Problems
• Interrupt can occur at any time and may interfere with the interrupted code.
• OS must be able to synchronize concurrent processes.
§ Synchronization • Turn off/on interrupts • Use a special atomic instructions
– read-modify-write (e.g., INC, DEC) – test-and-set – LOCK prefix in IA32 – LL (Load Locked) & SC (Store Conditional) in MIPS
30 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Operating System Structures
§ MS-DOS – written to provide the most functionality in the least space• Not divided into modules• Although MS-DOS has
some structure, its interfaces and levels of functionality are not well separated
31 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Operating System Structures § UNIX System (monolithic kernel)
32 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Layered Approach § The operating system is
divided into a number of layers• Each built on top of lower
layers• Bottom layer is hardware• Highest is user interface
§ With modularity, layers are selected such that each uses functions (operations) and services of only lower-level layers
33 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Microkernel System Structure
ApplicationProgram
FileSystem
DeviceDriver
InterprocessCommunication
memorymanagment
CPUscheduling
messagesmessages
microkernel
hardware
usermode
kernelmode
34 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Microkernel vs. Monolithic Kernel § Moves as much from the kernel into user space § Mach example of microkernel
• Mac OS X kernel (Darwin) partly based on Mach
§ Communication takes place between user modules using message passing
§ Benefits • Easier to extend a microkernel • Easier to port the operating system to new architectures • More reliable (less code is running in kernel mode) • More secure
§ Detriments • Performance overhead of user space to kernel space
communication
35 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Modules § Many modern operating systems implement
loadable kernel modules • Uses object-oriented approach • Each core component is separate • Each talks to the others over known interfaces • Each is loadable as needed within the kernel
§ Overall, similar to layers but with more flexible • Linux, Solaris, etc
36 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Solaris Modular Approach
37 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Hybrid Systems § Most modern operating systems are actually
not one pure model• Hybrid combines multiple approaches to address
performance, security, usability needs• Linux and Solaris kernels in kernel address space, so
monolithic, plus modular for dynamic loading of functionality
• Windows mostly monolithic, plus microkernel for different subsystem personalities
38 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Mac OS X
graphical user interfaceAqua
application environments and services
kernel environment
Java Cocoa Quicktime BSD
Mach
I/O kit kernel extensions
BSD
39 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Android § Developed by Open Handset Alliance (mostly Google)
• Open Source§ Similar stack to IOS§ Based on Linux kernel but modified
• Provides process, memory, device-driver management• Adds power management
§ Runtime environment includes core set of libraries and Dalvik virtual machine• Apps developed in Java plus Android API
– Java class files compiled to Java bytecode then translated to executable than runs in Dalvik VM
§ Libraries include frameworks for web browser (webkit), database (SQLite), multimedia, smaller libc
40 SSE3044: Operating Systems | Fall 2015 | Jinkyu Jeong ([email protected])
Android Applications
Application Framework
Android runtime
Core Libraries
Dalvikvirtual machine
Libraries
Linux kernel
SQLite openGL
surfacemanager
webkit libc
mediaframework