0% found this document useful (0 votes)
52 views26 pages

COS 318: Operating Systems OS Structures and System Calls

This document discusses operating system structures and system calls. It covers protection mechanisms like privileged modes that separate kernel and user code. It describes OS design options like monolithic, layered, microkernel and virtual machine architectures. It also discusses system call mechanisms, design issues like parameter passing, and the division of labor between kernel and library calls.

Uploaded by

mnnit37
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views26 pages

COS 318: Operating Systems OS Structures and System Calls

This document discusses operating system structures and system calls. It covers protection mechanisms like privileged modes that separate kernel and user code. It describes OS design options like monolithic, layered, microkernel and virtual machine architectures. It also discusses system call mechanisms, design issues like parameter passing, and the division of labor between kernel and library calls.

Uploaded by

mnnit37
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

COS 318: Operating Systems OS Structures and System Calls

Jaswinder Pal Singh Computer Science Department Princeton University (http://www.cs.princeton.edu/courses/cos318/)

Outline
! ! !

Protection mechanisms OS structures System and library calls

Protection Issues
!

CPU
" "

Kernel has the ability to take CPU away from users to prevent a user from using the CPU forever Users should not have such an ability Prevent a user from accessing others data Prevent users from modifying kernel code and data structures Prevent users from performing illegal I/Os

Memory
" "

I/O
"

Architecture Support: Privileged Mode


An interrupt or exception (INT)

User mode Regular instructions Access user memory

Kernel (privileged) mode Regular instructions Privileged instructions Access user memory Access kernel memory

A special instruction (IRET)


4

Privileged Instruction Examples


! ! ! ! ! ! ! ! !

Memory address mapping Flush or invalidate data cache Invalidate TLB entries Load and read system registers Change processor modes from kernel to user Change the voltage and frequency of processor Halt a processor Reset a processor Perform I/O operations

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

Outline
! ! !

Protection mechanisms OS structures System and library calls

Monolithic
!

All kernel routines are together, any can call any A system call interface (main program, sys calls, utility funcs) Examples:
"

User program
sy sc al l

User program
a sc sy ll

Linux, BSD Unix, Windows Shared kernel space Good performance No information hiding Inflexible Chaotic Difficult to understand How many bugs in 5 million lines of code?

Pros
" "

Cons
" " " " "

Kernel (many things)

Layered Structure
! ! !

Level N constructed on top of N-1 Hiding information at each layer E.g. level 1 is processor allocation, level 1 memory management, level 2 comm, level 3 I/O, etc. Examples
" "

Level N . . . Level 2 Level 1 Hardware

THE System (6 layers) MS-DOS (4 layers) Layered abstraction Separation of concerns, elegance Protection, boundary crossings Performance

Pros
" "

Cons
" "

Microkernel
! !

! !

Put less in kernel mode: only small part of OS; reduce kernel bugs Services are regular processes; one file system crashing doesnt crash full system; cant corrupt kernel memory !-kernel gets svcs on behalf of users by messaging with service processes Examples: " Mach, Taos, L4, OS-X Pros? " Flexibility " Fault isolation and reliability (used in avionics and military apps) Cons? " Inefficient (boundary crossings) " Insufficient protection " Inconvenient to share data between kernel and services

User program
sy sc

OS Services

al l

entry !-kernel

10

Virtual Machine
!

Separate out multiprogramming from abstraction; VMM provides former Virtual machine monitor
" " "

Apps OS1 VM1

Apps

Virtualize hardware, but expose it as multiple instances of raw hw Run several OSes, one on each set Examples IBM VM/370 Java VM VMWare, Xen

...

OSk VMk

Virtual Machine Monitor

What would you use virtual machine for?

Raw Hardware

11

Two Popular Ways to Implement VMM

Win Apps Win Apps


Linux Apps

Win Vista
Linux Apps

Win Vista VMM

VMM

Linux

Linux Hardware VMM as an application

Hardware VMM runs on hardware

(A special lecture later in the semester)


12

Outline
! ! !

Protection mechanisms OS structures System and library calls

13

System Call Mechanism


!

Assumptions
" "

User code can be arbitrary User code cannot modify kernel memory User code makes a system call with parameters The call mechanism switches code to kernel mode Execute system call Return with results (Like a procedure call, just crosses kernel boundary)

Design Issues
" " " " "

User program
sy sc al l

User program
a sc sy ll

Kernel in protected memory

14

System Calls
!

Operating system API


"

Interface between an application and the operating system kernel Process management Memory management File management Device management Communication

Categories
" " " " "

15

OS Kernel: Trap Handler


Interrupt service routines

HW Device Interrupt System Call HW exceptions SW exceptions Virtual address exceptions

Syscall table System Service dispatcher System service dispatcher

System services Exception dispatcher


VM managers pager

Exception handlers

HW implementation of the boundary


16

Passing Parameters
!

Pass by registers
" " " "

# of registers # of usable registers # of parameters in system call Spill/fill code in compiler Single register for starting address Vector in users memory Similar to the memory vector Procedure call convention
17

Pass by a memory vector (list)


" "

Pass by stack
" "

Library Stubs for System Calls

Example:
int read( int fd, char * buf, int size) { move fd, buf, size to R1, R2, R3 move READ to R0 int $0x80 move result to Rresult }

User program
In t$ ire 0x 80 t

Linux: 80 NT: 2E

Kernel in protected memory

18

System Call Entry Point

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

User stack

User memory Registers Registers

Kernel stack

Kernel memory

19

Design Issues
!

System calls
" " " "

There is one result register; what about more results? How do we pass errors back to the caller? Can user code lie? How would you perform QA on system calls? What should be system calls? What should be library calls?

System calls vs. library calls


" "

20

Division of Labor (or Separation Of Concerns)


Memory management example ! Kernel
" " "

Allocates pages with hardware protection Allocates a big chunk (many pages) to library Does not care about small allocs Provides malloc/free for allocation and deallocation Application use these calls to manage memory at fine granularity When reaching the end, library asks the kernel for more

Library
" " "

21

Feedback To The Program


!

Applications view system calls and library calls as procedure calls What about OS to apps?
" "

Application

Various exceptional conditions General information, like screen resize

What mechanism would OS use for this?

Operating System

22

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 volume 3 for details

23

Interrupt and Exceptions (1)


Vector # 0 1 2 3 4 5 6 7 8 9 10 #TS #BP #OF #BR #UD #NM #DF Mnemonic #DE #DB Description Divide error (by zero) Debug NMI interrupt Breakpoint Overflow BOUND range exceeded Invalid opcode Device not available Double fault Coprocessor segment overrun Invalid TSS
24

Type Fault Fault/trap Interrupt Trap Trap Trap Fault Fault Abort Fault

Interrupt and Exceptions (2)


Vector # 11 12 13 14 15 16 17 18 19-31 32-255 #MF #AC #MC Mnemonic #NP #SS #GP #PF Description Segment not present Stack-segment fault General protection Page fault Reserved Floating-point error (math fault) Alignment check Machine check Reserved User defined Interrupt
25

Type Fault Fault Fault Fault Fault Fault Fault Abort

Summary
!

Protection mechanism
" "

Architecture support: two modes Software traps (exceptions) Monolithic, layered, microkernel and virtual machine Implementation Design issues Tradeoffs with library calls

OS structures
"

System calls
" " "

26

You might also like