System Calls & Libraries Vivek Pai Lecture 4, COS318 Sep 25, 2001
Gedankundmathematics <ul><li>Recall the pointer verification case for fread( ) </li></ul><ul><li>Can you speed up the chec...
Mechanics <ul><li>Is the project workable? </li></ul><ul><ul><li>Has everyone started? </li></ul></ul><ul><ul><li>Barring ...
Protection Issues <ul><li>I/O protection </li></ul><ul><ul><li>Prevent users from performing illegal I/Os </li></ul></ul><...
Protection Is Not Safety/Security <ul><li>Protection is a prerequisite </li></ul><ul><li>Safety can be separation of conce...
Support in Modern Processors: User    Kernel System Calls & Libraries <ul><li>User mode </li></ul><ul><li>Regular instruc...
Why a Privileged Mode? <ul><li>Special Instructions </li></ul><ul><ul><li>Mapping, TLB, etc </li></ul></ul><ul><ul><li>Dev...
x86 Protection Rings System Calls & Libraries Level 0 Level 1 Level 2 Level 3 Operating system kernel Operating system ser...
Other Design Approaches <ul><li>“ Capabilities” </li></ul><ul><ul><li>Fine-grained access control </li></ul></ul><ul><ul><...
Monolithic <ul><li>All kernel routines are together </li></ul><ul><li>A system call interface </li></ul><ul><li>Examples: ...
Monolithic Pros and Cons <ul><li>Pros </li></ul><ul><li>Relatively few crossings </li></ul><ul><li>Shared kernel address s...
Layered Structure <ul><li>Hiding information at each layer </li></ul><ul><li>Develop a layer at a time </li></ul><ul><li>E...
Layering Pros and Cons <ul><li>Pros </li></ul><ul><li>Separation of concerns </li></ul><ul><li>Simplicity / elegance </li>...
Microkernel <ul><li>Micro-kernel is “micro” </li></ul><ul><li>Services are implemented as regular process </li></ul><ul><l...
Microkernel Pros and Cons <ul><li>Pros </li></ul><ul><li>Easier to develop services </li></ul><ul><li>Fault isolation </li...
Virtual Machine <ul><li>Virtual machine monitor </li></ul><ul><ul><li>provide multiple virtual “real” hardware </li></ul><...
Hardware Support <ul><li>What is the minimal support? </li></ul><ul><li>Can virtual machine be protected without such supp...
System Call Mechanism <ul><li>User code can be arbitrary </li></ul><ul><li>User code cannot modify kernel memory </li></ul...
Interrupt and Exceptions <ul><li>Interrupt Sources </li></ul><ul><ul><li>Hardware (by external devices) </li></ul></ul><ul...
Interrupt and Exceptions (1) System Calls & Libraries Vector # Mnemonic Description Type 0 #DE Divide error (by zero) Faul...
Interrupt and Exceptions (2) System Calls & Libraries Vector # Mnemonic Description Type 11 #NP Segment not present Fault ...
System Calls <ul><li>Interface between a process and the operating system kernel </li></ul><ul><li>Categories </li></ul><u...
OS Kernel: Trap Handler System Calls & Libraries HW Device Interrupt HW exceptions SW exceptions System Service Call Virtu...
Passing Parameters <ul><li>Affects and depends on </li></ul><ul><ul><li>Architecture </li></ul></ul><ul><ul><li>Compiler <...
Passing Parameters - Registers <ul><li>Place parameters in registers </li></ul><ul><li># of registers </li></ul><ul><li># ...
Passing Parameters - Vector <ul><li>Register holds vector address </li></ul><ul><li>Single register </li></ul><ul><li>Vect...
Passing Parameters - Stack <ul><li>Place parameters on stack </li></ul><ul><li>Similar to vector approach </li></ul><ul><l...
Library Stubs for System Calls <ul><li>Use read( fd, buf, size) as an example: </li></ul><ul><ul><li>int read( int fd, cha...
System Call Entry Point <ul><li>Assume passing parameters in registers </li></ul><ul><ul><li>EntryPoint: </li></ul></ul><u...
Design & Performance Issues <ul><li>Can user code lie? </li></ul><ul><li>One result register – large results? </li></ul><u...
General Design Aesthetics <ul><li>Simplicity, obviousness </li></ul><ul><li>Generality – same call handles many cases </li...
Separation Of Concerns <ul><li>Memory management </li></ul><ul><li>Kernel allocates “pages” – hw protection </li></ul><ul>...
Library Benefits <ul><li>Call overhead </li></ul><ul><ul><li>Chains of alloc/free don’t go to kernel </li></ul></ul><ul><l...
Feedback To The Program <ul><li>System calls, libraries are program to OS </li></ul><ul><li>What about other direction? </...
Upcoming SlideShare
Loading in …5
×

Os note

955 views
930 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
955
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Os note

  1. 1. System Calls & Libraries Vivek Pai Lecture 4, COS318 Sep 25, 2001
  2. 2. Gedankundmathematics <ul><li>Recall the pointer verification case for fread( ) </li></ul><ul><li>Can you speed up the checking process? </li></ul><ul><li>What’s the best you could achieve? O(n)? O(logn)? O(1)? </li></ul><ul><li>What happens if you have >32 bits? </li></ul><ul><ul><li>Aside: # atoms in universe = 10 80 , or 2 256 </li></ul></ul><ul><li>Does this provide any other benefits? </li></ul>System Calls & Libraries
  3. 3. Mechanics <ul><li>Is the project workable? </li></ul><ul><ul><li>Has everyone started? </li></ul></ul><ul><ul><li>Barring major problems, due Tuesday midnight </li></ul></ul><ul><li>Readings updated </li></ul>System Calls & Libraries
  4. 4. Protection Issues <ul><li>I/O protection </li></ul><ul><ul><li>Prevent users from performing illegal I/Os </li></ul></ul><ul><li>Memory protection </li></ul><ul><ul><li>Prevent users from modifying kernel code and data structures </li></ul></ul><ul><li>CPU protection </li></ul><ul><ul><li>Prevent a user from using the CPU for too long </li></ul></ul>System Calls & Libraries
  5. 5. Protection Is Not Safety/Security <ul><li>Protection is a prerequisite </li></ul><ul><li>Safety can be separation of concerns </li></ul><ul><li>Security related to overall design </li></ul><ul><li>Examples? </li></ul><ul><li>Bad pointer access causing seg fault </li></ul><ul><li>Sniffing cleartext passwords on the wire </li></ul>System Calls & Libraries
  6. 6. Support in Modern Processors: User  Kernel System Calls & Libraries <ul><li>User mode </li></ul><ul><li>Regular instructions </li></ul><ul><li>Access user-mode memory </li></ul><ul><li>Kernel ( privileged ) mode </li></ul><ul><li>Regular instructions </li></ul><ul><li>Access user-mode memory </li></ul>An interrupt or exception ( INT ) A special instruction ( IRET )
  7. 7. Why a Privileged Mode? <ul><li>Special Instructions </li></ul><ul><ul><li>Mapping, TLB, etc </li></ul></ul><ul><ul><li>Device registers </li></ul></ul><ul><ul><li>I/O channels, etc. </li></ul></ul><ul><li>Mode Bits </li></ul><ul><ul><li>Processor features </li></ul></ul><ul><li>Device access </li></ul>System Calls & Libraries
  8. 8. x86 Protection Rings System Calls & Libraries Level 0 Level 1 Level 2 Level 3 Operating system kernel Operating system services Applications Privileged instructions Can be executed only When current privileged Level (CPR) is 0
  9. 9. Other Design Approaches <ul><li>“ Capabilities” </li></ul><ul><ul><li>Fine-grained access control </li></ul></ul><ul><ul><li>Crypto-like tokens </li></ul></ul><ul><li>Microkernels </li></ul><ul><ul><li>OS services in user space </li></ul></ul><ul><ul><li>Small core “hypervisor” </li></ul></ul>System Calls & Libraries
  10. 10. Monolithic <ul><li>All kernel routines are together </li></ul><ul><li>A system call interface </li></ul><ul><li>Examples: </li></ul><ul><ul><li>Linux </li></ul></ul><ul><ul><li>Most Unix OS </li></ul></ul><ul><ul><li>NT </li></ul></ul>System Calls & Libraries Kernel many many things entry User program User program call return
  11. 11. Monolithic Pros and Cons <ul><li>Pros </li></ul><ul><li>Relatively few crossings </li></ul><ul><li>Shared kernel address space </li></ul><ul><li>Performance </li></ul><ul><li>Cons </li></ul><ul><li>Flexibility </li></ul><ul><li>Stability </li></ul><ul><li>Experimentation </li></ul>System Calls & Libraries
  12. 12. Layered Structure <ul><li>Hiding information at each layer </li></ul><ul><li>Develop a layer at a time </li></ul><ul><li>Examples </li></ul><ul><ul><li>THE (6 layers) </li></ul></ul><ul><ul><li>MS-DOS (4 layers) </li></ul></ul>System Calls & Libraries Hardware Level 1 Level 2 Level N . . .
  13. 13. Layering Pros and Cons <ul><li>Pros </li></ul><ul><li>Separation of concerns </li></ul><ul><li>Simplicity / elegance </li></ul><ul><li>Cons </li></ul><ul><li>Boundary crossings </li></ul><ul><li>Performance? </li></ul>System Calls & Libraries
  14. 14. Microkernel <ul><li>Micro-kernel is “micro” </li></ul><ul><li>Services are implemented as regular process </li></ul><ul><li>Micro-kernel get services on behalf of users by messaging with the service processes </li></ul><ul><li>Examples: Taos, Mach, L4 </li></ul>System Calls & Libraries  kernel entry User program Services call return
  15. 15. Microkernel Pros and Cons <ul><li>Pros </li></ul><ul><li>Easier to develop services </li></ul><ul><li>Fault isolation </li></ul><ul><li>Customization </li></ul><ul><li>Smaller kernel => easier to optimize </li></ul><ul><li>Cons </li></ul><ul><li>Lots of boundary crossings </li></ul><ul><li>Really poor performance </li></ul>System Calls & Libraries
  16. 16. Virtual Machine <ul><li>Virtual machine monitor </li></ul><ul><ul><li>provide multiple virtual “real” hardware </li></ul></ul><ul><ul><li>run different OS codes </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>IBM VM/370 </li></ul></ul><ul><ul><li>virtual 8086 mode </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>VMWare </li></ul></ul>System Calls & Libraries Bare hardware Small kernel VM1 VMn . . . OS1 OSn user user
  17. 17. Hardware Support <ul><li>What is the minimal support? </li></ul><ul><li>Can virtual machine be protected without such support? </li></ul><ul><li>Hint: what is a Turing machine? </li></ul>System Calls & Libraries
  18. 18. System Call Mechanism <ul><li>User code can be arbitrary </li></ul><ul><li>User code cannot modify kernel memory </li></ul><ul><li>Makes a system call with parameters </li></ul><ul><li>The call mechanism switches code to kernel mode </li></ul><ul><li>Execute system call </li></ul><ul><li>Return with results </li></ul>System Calls & Libraries Kernel in protected memory entry User program User program call return
  19. 19. Interrupt and Exceptions <ul><li>Interrupt Sources </li></ul><ul><ul><li>Hardware (by external devices) </li></ul></ul><ul><ul><li>Software: INT n </li></ul></ul><ul><li>Exceptions </li></ul><ul><ul><li>Program error: faults, traps, and aborts </li></ul></ul><ul><ul><li>Software generated: INT 3 </li></ul></ul><ul><ul><li>Machine-check exceptions </li></ul></ul><ul><li>See Intel document chapter 5, volume 3 for details </li></ul>System Calls & Libraries
  20. 20. Interrupt and Exceptions (1) System Calls & Libraries Vector # Mnemonic Description Type 0 #DE Divide error (by zero) Fault 1 #DB Debug Fault/trap 2 NMI interrupt Interrupt 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 9 Coprocessor segment overrun Fault 10 #TS Invalid TSS
  21. 21. Interrupt and Exceptions (2) System Calls & Libraries 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 15 Reserved Fault 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
  22. 22. System Calls <ul><li>Interface between a process and the operating system kernel </li></ul><ul><li>Categories </li></ul><ul><ul><li>Process management </li></ul></ul><ul><ul><li>Memory management </li></ul></ul><ul><ul><li>File management </li></ul></ul><ul><ul><li>Device management </li></ul></ul><ul><ul><li>Communication </li></ul></ul>System Calls & Libraries
  23. 23. OS Kernel: Trap Handler System Calls & Libraries HW Device Interrupt HW exceptions SW exceptions System Service Call Virtual address exceptions HW implementation of the boundary System service dispatcher Sys_call_table System services Interrupt service routines Exception dispatcher Exception handlers VM manager’s pager
  24. 24. Passing Parameters <ul><li>Affects and depends on </li></ul><ul><ul><li>Architecture </li></ul></ul><ul><ul><li>Compiler </li></ul></ul><ul><ul><li>OS </li></ul></ul><ul><li>Different choices for different purposes </li></ul>System Calls & Libraries
  25. 25. Passing Parameters - Registers <ul><li>Place parameters in registers </li></ul><ul><li># of registers </li></ul><ul><li># of usable registers </li></ul><ul><li># of parameters in system call </li></ul><ul><li>Spill/fill code in compiler </li></ul><ul><li>Really fast </li></ul>System Calls & Libraries
  26. 26. Passing Parameters - Vector <ul><li>Register holds vector address </li></ul><ul><li>Single register </li></ul><ul><li>Vector in user’s memory </li></ul><ul><li>Nothing horrible, just not common </li></ul>System Calls & Libraries
  27. 27. Passing Parameters - Stack <ul><li>Place parameters on stack </li></ul><ul><li>Similar to vector approach </li></ul><ul><li>Stack already exists </li></ul><ul><li>Gets copied anyway </li></ul>System Calls & Libraries frame frame Top
  28. 28. Library Stubs for System Calls <ul><li>Use read( fd, buf, size) as an example: </li></ul><ul><ul><li>int read( int fd, char * buf, int size) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>move fd, buf, size to R 1 , R 2 , R 3 </li></ul></ul><ul><ul><li>move READ to R 0 </li></ul></ul><ul><ul><li>int $0x80 </li></ul></ul><ul><ul><li>move result to R result </li></ul></ul><ul><ul><li>} </li></ul></ul>System Calls & Libraries User stack Registers User memory Kernel stack Registers Kernel memory Linux: 80 NT: 2E
  29. 29. System Call Entry Point <ul><li>Assume passing parameters in registers </li></ul><ul><ul><li>EntryPoint: </li></ul></ul><ul><ul><li>switch to kernel stack </li></ul></ul><ul><ul><li>save context </li></ul></ul><ul><ul><li>check R 0 </li></ul></ul><ul><ul><li>call the real code pointed by R 0 </li></ul></ul><ul><ul><li>restore context </li></ul></ul><ul><ul><li>switch to user stack </li></ul></ul><ul><ul><li>iret (change to user mode and return) </li></ul></ul>System Calls & Libraries User stack Registers User memory Kernel stack Registers Kernel memory
  30. 30. Design & Performance Issues <ul><li>Can user code lie? </li></ul><ul><li>One result register – large results? </li></ul><ul><li>Parameters in user memory </li></ul><ul><li>Multiprocessors </li></ul>System Calls & Libraries
  31. 31. General Design Aesthetics <ul><li>Simplicity, obviousness </li></ul><ul><li>Generality – same call handles many cases </li></ul><ul><li>Composition / decomposition </li></ul><ul><li>But: </li></ul><ul><li>Expressiveness </li></ul><ul><li>Performance </li></ul>System Calls & Libraries
  32. 32. Separation Of Concerns <ul><li>Memory management </li></ul><ul><li>Kernel allocates “pages” – hw protection </li></ul><ul><li>Programs use malloc( ) – fine grained </li></ul><ul><li>Kernel doesn’t care about small allocs </li></ul><ul><ul><li>Allocates pages to library </li></ul></ul><ul><ul><li>Library handles malloc/free </li></ul></ul>System Calls & Libraries
  33. 33. Library Benefits <ul><li>Call overhead </li></ul><ul><ul><li>Chains of alloc/free don’t go to kernel </li></ul></ul><ul><li>Flexibility – easy to change policy </li></ul><ul><ul><li>Fragmentation </li></ul></ul><ul><ul><li>Coalescing, free list management </li></ul></ul><ul><li>Easier to program </li></ul>System Calls & Libraries
  34. 34. Feedback To The Program <ul><li>System calls, libraries are program to OS </li></ul><ul><li>What about other direction? </li></ul><ul><ul><li>Various exceptional conditions </li></ul></ul><ul><ul><li>General information, like screen resize </li></ul></ul><ul><li>When would this occur? </li></ul><ul><li>Answer: signals </li></ul>System Calls & Libraries

×