Docstoc

System Calls _ Libraries

Document Sample
System Calls _ Libraries Powered By Docstoc
					System Calls & Libraries

Vivek Pai Lecture 4, COS318 Sep 25, 2001

Gedankundmathematics
Recall the pointer verification case for fread( )  Can you speed up the checking process?  What’s the best you could achieve? O(n)? O(logn)? O(1)?  What happens if you have >32 bits?


Aside: # atoms in universe = 1080, or 2256



Does this provide any other benefits?
System Calls & Libraries

2

Mechanics


Is the project workable?
 

Has everyone started? Barring major problems, due Tuesday midnight



Readings updated

System Calls & Libraries

3

Protection Issues
 

I/O protection


Prevent users from performing illegal I/Os Prevent users from modifying kernel code and data structures Prevent a user from using the CPU for too long
System Calls & Libraries

Memory protection




CPU protection


4

Protection Is Not Safety/Security
  

Protection is a prerequisite Safety can be separation of concerns Security related to overall design

Examples?  Bad pointer access causing seg fault  Sniffing cleartext passwords on the wire
System Calls & Libraries

5

Support in Modern Processors: User  Kernel
An interrupt or exception (INT)

User mode Regular instructions Access user-mode memory

Kernel (privileged) mode Regular instructions Access user-mode memory

A special instruction (IRET) 6

System Calls & Libraries

Why a Privileged Mode?


Special Instructions
  

Mapping, TLB, etc Device registers I/O channels, etc. Processor features

 

Mode Bits


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

Level 0
Level 1 Level 2

Level 3

System Calls & Libraries

8

Other Design Approaches


“Capabilities”
 

Fine-grained access control Crypto-like tokens OS services in user space Small core “hypervisor”



Microkernels
 

System Calls & Libraries

9

Monolithic
 



All kernel routines are together A system call interface Examples:
  

User program

User program

entry Kernel many many things

Linux Most Unix OS NT

System Calls & Libraries

10

Monolithic Pros and Cons
Pros  Relatively few crossings  Shared kernel address space  Performance Cons  Flexibility  Stability  Experimentation
System Calls & Libraries

11

Layered Structure
 



Hiding information at each layer Develop a layer at a time Examples
 

Level N . . .

Level 2
Level 1

THE (6 layers) MS-DOS (4 layers)

Hardware

System Calls & Libraries

12

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

Cons  Boundary crossings  Performance?

System Calls & Libraries

13

Microkernel
 





Micro-kernel is “micro” Services are implemented as regular process Micro-kernel get services on behalf of users by messaging with the service processes Examples: Taos, Mach, L4

User program

Services

entry m-kernel

System Calls & Libraries

14

Microkernel Pros and Cons
Pros  Easier to develop services  Fault isolation  Customization  Smaller kernel => easier to optimize Cons  Lots of boundary crossings  Really poor performance
System Calls & Libraries

15

Virtual Machine


Virtual machine monitor



provide multiple virtual “real” hardware run different OS codes IBM VM/370 virtual 8086 mode Java VMWare

user OS1 ...

user OSn



Example
   

VM1

VMn

Small kernel Bare hardware

System Calls & Libraries

16

Hardware Support
 

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



Hint: what is a Turing machine?

System Calls & Libraries

17

System Call Mechanism



  



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

System Calls & Libraries

Interrupt and Exceptions


Interrupt Sources



Hardware (by external devices) Software: INT n
Program error: faults, traps, and aborts Software generated: INT 3 Machine-check exceptions



Exceptions
  



See Intel document chapter 5, volume 3 for details
System Calls & Libraries

19

Interrupt and Exceptions (1)
Vector # 0 1 2 3 4 5 #BP #OF #BR Mnemonic #DE #DB Debug NMI interrupt Breakpoint Overflow BOUND range exceeded Description Divide error (by zero) Type Fault Fault/trap Interrupt Trap Trap Trap

6
7 8

#UD
#NM #DF #TS

Invalid opcode
Device not available Double fault

Fault
Fault Abort

9
10

Coprocessor segment overrun
Invalid TSS
System Calls & Libraries

Fault

20

Interrupt and Exceptions (2)
Vector # 11 Mnemonic #NP Description Segment not present Type Fault

12
13 14

#SS
#GP #PF #MF #AC #MC

Stack-segment fault
General protection Page fault

Fault
Fault Fault

15
16 17 18 19-31 32-255

Reserved
Floating-point error (math fault) Alignment check Machine check Reserved User defined
System Calls & Libraries

Fault
Fault Fault Abort Interrupt

21

System Calls
 

Interface between a process and the operating system kernel Categories



  

Process management Memory management File management Device management Communication

System Calls & Libraries

22

OS Kernel: Trap Handler
HW Device Interrupt System Service Call HW exceptions SW exceptions Virtual address exceptions Exception dispatcher Exception handlers
Sys_call_table

Interrupt service routines

System service dispatcher

System services

VM manager’s pager HW implementation of the boundary
System Calls & Libraries

23

Passing Parameters


Affects and depends on
  

Architecture Compiler OS



Different choices for different purposes

System Calls & Libraries

24

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

Really fast
System Calls & Libraries

25

Passing Parameters - Vector
Register holds vector address  Single register  Vector in user’s memory  Nothing horrible, just not common

System Calls & Libraries

26

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

System Calls & Libraries

27

Library Stubs for System Calls


Use read( fd, buf, size) as an 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 to Rresult }

User stack

User memory
Registers Registers

Kernel stack

Kernel memory

System Calls & Libraries

28

System Call Entry Point


Assume passing parameters in registers
EntryPoint: switch to kernel stack save context check R0 call the real code pointed by R0 restore context switch to user stack iret (change to user mode and return)

User stack

User memory
Registers Registers

Kernel stack

Kernel memory

System Calls & Libraries

29

Design & Performance Issues
   

Can user code lie? One result register – large results? Parameters in user memory Multiprocessors

System Calls & Libraries

30

General Design Aesthetics
  

Simplicity, obviousness Generality – same call handles many cases Composition / decomposition

But:  Expressiveness  Performance
System Calls & Libraries

31

Separation Of Concerns
Memory management  Kernel allocates “pages” – hw protection  Programs use malloc( ) – fine grained  Kernel doesn’t care about small allocs
 

Allocates pages to library Library handles malloc/free

System Calls & Libraries

32

Library Benefits
 

Call overhead


Chains of alloc/free don’t go to kernel Fragmentation Coalescing, free list management

Flexibility – easy to change policy
 



Easier to program

System Calls & Libraries

33

Feedback To The Program
 

System calls, libraries are program to OS What about other direction?
 

Various exceptional conditions General information, like screen resize



When would this occur?

Answer: signals
System Calls & Libraries

34


				
DOCUMENT INFO