Download COS 318: Operating Systems OS Structures and System Calls

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Library (computing) wikipedia , lookup

Copland (operating system) wikipedia , lookup

Burroughs MCP wikipedia , lookup

Acorn MOS wikipedia , lookup

Unix security wikipedia , lookup

RSTS/E wikipedia , lookup

Security-focused operating system wikipedia , lookup

CP/M wikipedia , lookup

Spring (operating system) wikipedia , lookup

Distributed operating system wikipedia , lookup

VS/9 wikipedia , lookup

Kernel (operating system) wikipedia , lookup

DNIX wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
COS 318: Operating Systems
OS Structures and System Calls
Kai Li and Andy Bavier
Computer Science Department
Princeton University
http://www.cs.princeton.edu/courses/archive/fall13/cos318/
Logistics
u 
Weekly TA office hours posted on Piazza
l 
u 
May change from week to week
Four Lab TAs available over the weekends (Fri – Sun)
David Durst
l  Anna Simpson
l  Catherine Wu
l  Harvest Zhang
l 
2
Baby Steps
3
Outline
Protection mechanisms
u  OS structures
u  System and library calls
u 
4
Protection Issues
u 
CPU
Kernel has the ability to take CPU away from users to
prevent a user from using the CPU forever
l  Users should not have such an ability
l 
u 
Memory
Prevent a user from accessing others’ data
l  Prevent users from modifying kernel code and data
structures
l 
u 
I/O
l 
u 
Prevent users from performing “illegal” I/Os
Question
l 
What’s the difference between protection and security?
5
Architecture Support: Privileged Mode
An interrupt or exception (INT)
User mode
•  Regular instructions
•  Access user memory
Kernel (privileged) mode
•  Regular instructions
•  Privileged instructions
•  Access user memory
•  Access kernel memory
A special instruction (IRET)
6
Privileged Instruction Examples
Memory address mapping
u  Flush or invalidate data cache
u  Invalidate TLB entries
u  Load and read system registers
u  Change processor modes from kernel to user
u  Change the voltage and frequency of processor
u  Halt a processor
u  Reset a processor
u  Perform I/O operations
u 
7
x86 Protection Rings
Privileged instructions
Can be executed only
When current privileged
Level (CPR) is 0
Operating system
kernel
Operating system
services
Applications
Level 0
Level 1
Level 2
Level 3
8
Layered OS Structure
u 
u 
u 
Hiding information at each layer
Layered dependency
Examples:
l 
l 
l 
u 
Pros
l 
l 
u 
THE (6 layers)
MS-DOS (4 layers)
MULTICS (8 layers)
Layered abstraction
Separation of concerns
Cons
l 
l 
Level N
..
.
Level 1
Level 0
Hardware
Inefficient
Inflexible
9
Monolithic OS
u 
u 
u 
All kernel routines are together, any
can call any
A system call interface
Examples:
l 
u 
User
program
Pros
l 
l 
u 
Linux, BSD Unix, Windows
User
program
Shared kernel space
Good performance
Cons
l 
l 
l 
No information hiding
Instability
How many bugs in 5M lines of code?
Kernel
(many things)
10
Microkernel
u 
u 
u 
u 
u 
u 
Put less in kernel mode; only small
part of OS
Services are implemented as
regular process
µ-kernel gets svcs on for users by
messaging with service processes
Examples:
l  Mach, Taos, L4, OS-X
Pros?
l  Modularity
l  Fault isolation
Cons?
l  Inefficient (lots of boundary
crossings)
User
program
OS
Services
entry
µ-kernel
11
Virtual Machine
u 
u 
Separate multiprogramming
from abstraction; VMM
provides former
Virtual machine monitor
l 
l 
l 
Virtualize hardware, but
expose as multiple
instances of “raw” HW
Run several OSes, one on
each instance
Examples
•  IBM VM/370
•  Java VM
•  VMWare, Xen
u 
Apps
OS1
VM1
Apps
...
OSk
VMk
Virtual Machine Monitor
Raw Hardware
What would you use a virtual
machine for?
12
Two Popular Ways to Implement VMM
Win Apps
Linux Apps
Win Apps
Linux
Win Vista
Win Vista
Linux Apps
VMM
VMM
Linux
Hardware
Hardware
VMM runs on hardware
VMM as an application
(A special lecture later in the semester)
13
Interlude
“UNIX is basically a simple operating system, but you
have to be a genius to understand the simplicity.”
-- Dennis Ritchie
14
Outline
Protection mechanisms
u  OS structures
u  System and library calls
u 
15
System Calls
u 
Operating system API
l 
u 
Interface between an application and the operating
system kernel
Categories
Process management
l  Memory management
l  File management
l  Device management
l  Communication
l 
16
How many system calls?
6th Edition Unix:
u  POSIX:
u  FreeBSD:
u  Linux:
u  Windows:
u 
~45
~130
~500
~300
400? 1000? 1M?
17
System Call Mechanism
u 
Assumptions
l 
l 
u 
User code can be arbitrary
User code cannot modify kernel
memory
Design Issues
l 
l 
l 
l 
User makes a system call with
parameters
The call mechanism switches
code to kernel mode
Execute system call
Return with results
User
program
User
program
Kernel in
protected memory
18
Passing Parameters
u 
Pass by registers
# of registers
l  # of usable registers
l  # of parameters in system call
l  Spill/fill code in compiler
l 
u 
Pass by a memory vector (list)
Single register for starting address
l  Vector in user’s memory
l 
u 
Pass by stack
Similar to the memory vector
l  Procedure call convention
l 
19
OS Kernel: Trap Handler
HW Device
Interrupt
System Call
HW
exceptions
SW exceptions
Virtual address
exceptions
Syscall table
System
Service
dispatcher
System
service
dispatcher
HW implementation of the boundary
Interrupt
service
routines
System
services
Exception
dispatcher
Exception
handlers
VM
manager’s
pager
20
From http://minnie.tuhs.org/UnixTree/V6
21
Library Stubs for System Calls
u 
Example:
int read( int fd, char * buf, int size)
{
move fd, buf, size to R1, R2, R3
move READ to R0
Linux: 80
int $0x80
NT: 2E
move result from Rresult
}
User
program
Kernel in
protected memory
22
System Call Entry Point
EntryPoint:
save context
switch to kernel stack
check R0
call the real code pointed by R0
place result in Rresult
switch to user stack
restore context
iret (change to user mode and return)
User
stack
User
memory
Registers
Registers
Kernel
stack
Kernel
memory
(Assume passing parameters in registers)
23
Design Issues
u 
System calls
l 
l 
u 
There is one result register; what about more results?
How do we pass errors back to the caller?
System calls vs. library calls
l 
l 
What should go in system calls?
What should go in library calls?
24
Syscall or library?
25
Backwards compatibility...
26
Division of Labors
Memory management example
u  Kernel
l 
l 
l 
u 
Allocates “pages” with hardware protection
Allocates a big chunk (many pages) to library
Does not care about small allocs
Library
l 
l 
l 
Provides malloc/free for allocation and deallocation
Application use these calls to manage memory at fine
granularity
When reaching the end, library asks the kernel for
more
27
Feedback To The Program
u 
u 
Applications view system
calls and library calls as
procedure calls
What about OS to apps?
l 
l 
u 
Various exceptional
conditions
General information, like
screen resize
Application
Operating
System
What mechanism would OS
use for this?
28
Interrupts and Exceptions
u 
Interrupt Sources
Hardware (by external devices)
l  Software: INT n
l 
u 
Exceptions
Program error: faults, traps, and aborts
l  Software generated: INT 3
l  Machine-check exceptions
l 
u 
See Intel document volume 3 for details
29
Interrupts and Exceptions (1)
Vector #
Mnemonic
Description
Type
0
#DE
Divide error (by zero)
Fault
1
#DB
Debug
Fault/trap
NMI interrupt
Interrupt
2
3
#BP
Breakpoint
Trap
4
#OF
Overflow
Trap
5
#BR
BOUND range exceeded
Trap
6
#UD
Invalid opcode
Fault
7
#NM
Device not available
Fault
8
#DF
Double fault
Abort
Coprocessor segment overrun
Fault
9
10
#TS
Invalid TSS
30
Interrupts and Exceptions (2)
Vector #
Mnemonic
Description
Type
11
#NP
Segment not present
Fault
12
#SS
Stack-segment fault
Fault
13
#GP
General protection
Fault
14
#PF
Page fault
Fault
Reserved
Fault
15
16
#MF
Floating-point error (math fault)
Fault
17
#AC
Alignment check
Fault
18
#MC
Machine check
Abort
19-31
Reserved
32-255
User defined
Interrupt
31
Example: Divide error
u 
What happens when your program divides by zero?
l 
Processor exception
•  Defined by x86 architecture as INT 0
Jump to kernel, execute handler 0 in interrupt vector
l  Handler 0 sends SIGFPE to process
l  Kernel returns control to process
l  Process has outstanding signal
l  Did process register SIGFPE handler?
l 
•  Yes:
•  Execute SIGFPE handler
•  When handler returns, resume program and redo divide
•  No: kills process
32
Summary
u 
Protection mechanism
Architecture support: two modes
l  Software traps (exceptions)
l 
u 
OS structures
l 
u 
Monolithic, layered, microkernel and virtual machine
System calls
Implementation
l  Design issues
l  Tradeoffs with library calls
l 
33