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