You are on page 1of 34

System Calls & Libraries

Vivek Pai
Lecture 4, COS318
Sep 25, 2001
Gedankundmathematics
Recall the pointer verification case for fread( )
r Can you speed up the checking process?
r What¶s the best you could achieve? O(n)?
O(logn)? O(1)?
r What happens if you have >32 bits?
r Aside: # atoms in universe = 1080, or 2256
r Does this provide any other benefits?

System Calls & Libraries 2


Mechanics
r Is the project workable?
r Has everyone started?
r Barring major problems, due Tuesday
midnight
r Readings updated

System Calls & Libraries 3


Protection Issues
r I/O protection
r Prevent users from performing illegal I/Os
r Memory protection
r Prevent users from modifying kernel code
and data structures
r CPU protection
r Prevent a user from using the CPU for too
long

System Calls & Libraries 4


Protection Is Not Safety/Security
r Protection is a prerequisite
r Safety can be separation of concerns
r Security related to overall design

Examples?
r Bad pointer access causing seg fault
r Sniffing cleartext passwords on the wire

System Calls & Libraries 5


Support in Modern Processors:
User s Kernel
An interrupt or exception (_ )

±   
 

·
    ·
   
·   ·  

A special instruction (_)

System Calls & Libraries 6


Why a Privileged Mode?
r Special Instructions
r Mapping, TLB, etc
r Device registers
r I/O channels, etc.
r Mode Bits
r Processor features
r Device access

System Calls & Libraries 7


x86 Protection Rings
Privileged instructions
Can be executed only
When current privileged
Level (CPR) is 0

Operating system
kernel
|

Operating system
services |

|

Applications
|


System Calls & Libraries 8


Other Design Approaches
r ³Capabilities´
r Fine-grained access control
r Crypto-like tokens
r Microkernels
r OS services in user space
r Small core ³hypervisor´

System Calls & Libraries 9


Monolithic
r All kernel routines User User
program program
are together
r A system call
interface
r Examples: entry
r Linux Kernel
r Most Unix OS many many things
r NT

System Calls & Libraries 10


Monolithic Pros and Cons
Pros
r Relatively few crossings
r Shared kernel address space
r Performance

Cons
r Flexibility
r Stability
r Experimentation

System Calls & Libraries 11


Layered Structure
r Hiding information at
Level N
each layer ‰‰
r Develop a layer at a ‰
time Level 2
r Examples
Level 1
r THE (6 layers)
r MS-DOS (4 layers) Hardware

System Calls & Libraries 12


Layering Pros and Cons
Pros
r Separation of concerns
r Simplicity / elegance

Cons
r Boundary crossings
r Performance?

System Calls & Libraries 13


Microkernel
r Micro-kernel is ³micro´ User
Services
program
r Services are
implemented as regular
process
r Micro-kernel get
services on behalf of entry
users by messaging with A kernel
the service processes
r Examples: Taos, Mach,
L4
System Calls & Libraries 14
Microkernel Pros and Cons
Pros
r Easier to develop services
r Fault isolation
r Customization
r Smaller kernel => easier to optimize

Cons
r Lots of boundary crossings
r Really poor performance

System Calls & Libraries 15


Virtual Machine
r Virtual machine monitor
r provide multiple virtual
³real´ hardware user user
r run different OS codes OS1 OSn
r Example ...
VM1 VMn
r IBM VM/370
r virtual 8086 mode Small kernel
r Java
r VMWare Bare hardware

System Calls & Libraries 16


Hardware Support
r What is the minimal support?
r Can virtual machine be protected without such
support?

r Hint: what is a Turing machine?

System Calls & Libraries 17


System Call Mechanism
r User code can be arbitrary
User User
r User code cannot modify program program
kernel memory
r Makes a system call with
parameters
r The call mechanism switches entry
code to kernel mode
Kernel in
r Execute system call
protected memory
r Return with results

System Calls & Libraries 18


Interrupt and Exceptions
r Interrupt Sources
r Hardware (by external devices)
r Software: _  
r Exceptions
r Program error: faults, traps, and aborts
r Software generated: _ 
r Machine-check exceptions
r See Intel document chapter 5, volume 3 for
details

System Calls & Libraries 19


Interrupt and Exceptions (1)
V        
    !" #

 $ !  #
% 
 &'   '  
 $( $ )   
* +# +,
-  
. $ $+±& /  
0 ± ' 
 #

1 &    
!
 #

2 #  !
,
 !
3 4    #

 5 ' 
55
System Calls & Libraries 20
Interrupt and Exceptions (2)
V        
 &( 5     #

 55 5 )  ,
 #

 6( 6 
  #

* (# ( ,
 #

.   #

0 # #
    7,
 #

1 4 
  7) #

2 4  7 7) !
3  
.. ± ,  '  

System Calls & Libraries 21


System Calls
r Interface between a process and the
operating system kernel
r Categories
r Process management
r Memory management
r File management
r Device management
r Communication

System Calls & Libraries 22


OS Kernel: Trap Handler
Interrupt
service
HW Device Sys_call_table routines
Interrupt

  
 
 
 
  
HW exceptions  
SW exceptions
Exception
dispatcher
Virtual address  
exceptions   

VM manager¶s
pager
HW implementation of the boundary
System Calls & Libraries 23
Passing Parameters
r Affects and depends on
r Architecture
r Compiler
r OS
r Different choices for different purposes

System Calls & Libraries 24


Passing Parameters - Registers
Place parameters in registers
r # of registers
r # of usable registers
r # of parameters in system call
r Spill/fill code in compiler

Really fast
System Calls & Libraries 25
Passing Parameters - Vector
Register holds vector address
r Single register
r Vector in user¶s memory
r Nothing horrible, just not common

System Calls & Libraries 26


Passing Parameters - Stack
Place parameters on stack
r Similar to vector approach Top
r Stack already exists frame
r Gets copied anyway frame

System Calls & Libraries 27


Library Stubs for System Calls
r Use read( fd, buf, size) as
an example: User
User memory
int read( int fd, char * buf, int stack
size)
Registers
{
move fd, buf, size to
R1, R2, R3 Registers
move READ to R0 Linux: 80 Kernel
int $0x80 NT: 2E stack Kernel
move result to Rresult memory
}

System Calls & Libraries 28


System Call Entry Point
r Assume passing parameters
in registers User
User memory
EntryPoint: stack
switch to kernel stack
Registers
save context
check R0
Registers
call the real code pointed by
R0 Kernel
restore context stack Kernel
memory
switch to user stack
iret (change to user mode and
return)

System Calls & Libraries 29


Design & Performance Issues
r Can user code lie?
r One result register ± large results?
r Parameters in user memory
r Multiprocessors

System Calls & Libraries 30


General Design Aesthetics
r Simplicity, obviousness
r Generality ± same call handles many cases
r Composition / decomposition

But:
r Expressiveness
r Performance

System Calls & Libraries 31


Separation Of Concerns
Memory management
r Kernel allocates ³pages´ ± hw protection
r Programs use malloc( ) ± fine grained
r Kernel doesn¶t care about small allocs
r Allocates pages to library
r Library handles malloc/free

System Calls & Libraries 32


Library Benefits
r Call overhead
r Chains of alloc/free don¶t go to kernel
r Flexibility ± easy to change policy
r Fragmentation
r Coalescing, free list management
r Easier to program

System Calls & Libraries 33


Feedback To The Program
r System calls, libraries are program to OS
r What about other direction?
r Various exceptional conditions
r General information, like screen resize
r When would this occur?

Answer: signals

System Calls & Libraries 34