3 OSStructure
3 OSStructure
(http://www.cs.princeton.edu/courses/cos318/)
Outline
u Protection mechanisms
u OS structures
u System and library calls
2
Protection Issues
u CPU
l 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
u Memory
l Prevent a user from accessing others’ data
l Prevent users from modifying kernel code and data
structures
u I/O
l Prevent users from performing “illegal” I/Os
u Question
l What’s the difference between protection and security?
4
Privileged Instruction Examples
u 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
Privileged instructions
Can be executed only
When current privileged
Level (CPR) is 0
Operating system
kernel
Level 0
Operating system
Level 1
services
Level 2
Applications
Level 3
6
Layered Structure
u Hiding information at each layer
u Layered dependency
u Examples Level N
l THE (6 layers) ..
l MS-DOS (4 layers) .
u Pros
Level 2
l Layered abstraction
u Cons Level 1
l Inefficiency
l Inflexible
Hardware
Monolithic
u All kernel routines are together
u A system call interface
u Examples:
User User
l Linux
program program
l BSD Unix
l Windows
u Pros
l Shared kernel space
l Good performance
u Cons Kernel
l Instability (many things)
l Inflexible / hard to maintain and
extend
8
Microkernel
u Services are implemented as
regular process
u Micro-kernel obtain services on
behalf of users by messaging
with the service processes User OS
u Examples: program Services
l Mach, Taos, L4, OS-X
u Pros?
l Flexibility
l Fault isolation
u Cons? entry
l Inefficient (Lots of boundary
crossings) µ-kernel
l Insufficient protection
l Inconvenient to share data
between kernel and services
l Just shifts the problem?
9
Virtual Machine
u Virtual machine monitor
l Virtualize hardware
Apps Apps
l Run several OSes
l Examples OS1 ... OSk
10
Two Popular Ways to Implement VMM
Win Apps
Hardware Hardware
11
12
Exceptions
u Sources
l Hardware (by external devices)
l Software: INT n
u Exceptions
l Normal: faults, traps, aborts, and interrupts
l Special software generated: INT 3
l Machine-check exceptions
u See Intel document volume 3 for details
13
14
Interrupt and Exceptions (2)
Vector # Mnemonic Description Type
11 #NP Segment not present Fault
15 Reserved Fault
19-31 Reserved
15
System Calls
u Operating system API
l Interface between an application and the operating
system kernel
u Categories
l Process management
l Memory management
l File management
l Device management
l Communication
16
How many system calls?
u 6th Edition Unix: ~45
u POSIX: ~130
u FreeBSD: ~130
u Linux: ~250 ("fewer than most")
u Windows 7: ?
17
From http://minnie.tuhs.org/UnixTree/V6
18
OS Kernel: Trap Handler
Interrupt
Syscall table service
HW Device routines
Interrupt
System
System Call Service
dispatcher System
HW services
exceptions
System Exception
SW exceptions service
Virtual address dispatcher dispatcher Exception
exceptions handlers
VM
manager’s
pager
HW implementation of the boundary
19
Passing Parameters
u Pass by registers
l # of registers
l # of usable registers
l # of parameters in system call
l Spill/fill code in compiler
u Pass by a memory vector (list)
l Single register for starting address
l Vector in user’s memory
u Pass by stack
l Similar to the memory vector
l Procedure call convention
20
Library Stubs for System Calls
u Example:
int read( int fd, char * buf, int size) User
{ program
move fd, buf, size to R1, R2, R3
move READ to R0
int $0x80 Linux: 80
move result to Rresult NT: 2E
}
Kernel in
protected memory
21
EntryPoint:
switch to kernel stack User
User memory
save context
stack
check R0
Registers
call the real code pointed by R0
place result in Rresult
restore context Registers
switch to user stack Kernel
iret (change to user mode and return) stack Kernel
memory
(Assume passing parameters in registers)
22
A simple system call (6th Edition chdir)
u "call the real code
pointed by R0
place result in Rresult"
23
Design Issues
u System calls
l There is one result register; what about more results?
l How do we pass errors back to the caller?
l Can user code lie?
24
Syscall or library?
25
Backwards compatibility...
26
Division of Labor (or Separation Of Concerns)
27
28
Summary
u Protection mechanism
l Architecture support: two modes
l Software traps (exceptions)
u OS structures
l Monolithic, layered, microkernel and virtual machine
u System calls
l Implementation
l Design issues
l Tradeoffs with library calls
29