<<

9/16/19

Important Times

u Precepts: COS 318: Operating Systems l Mon: 7:30-8:20pm, 104 CS building l Tues: 7:30-8:20pm, 105 CS building l This week (TODAY and TOMORROW): Overview • Tutorial on Assembly programming and kernel debugging

u Project 1 Jaswinder Pal Singh and a Fabulous Course Staff l Design review: Computer Science Department • Next Mon/Tues: 3:00–7:00 pm (Signup online), 010 Friend Princeton University l Project 1 due: Sunday 9/29 at 11:55pm

(http://www.cs.princeton.edu/courses/cos318/) u Immediate To-Do: l Make sure you have your project partner

2

Today Hardware of A Typical Computer

u Overview of OS functionality CPU . . . CPU u Overview of OS components

u Interacting with the OS Memory Chipset u a Computer I/O bus

ROM

Network

3 4

1 9/16/19

An Overview of HW Functionality Software in a Typical Computer

u Executing the (CPU, cache, memory) l instructions for ALU, branch, memory operations Memory CPU l instructions for communicating with I/O devices Applications . u Performing I/O operations . . Libraries, Runtime Systems l I/O devices and the CPU can execute concurrently l Every device controller is in charge of one device type CPU BIOS l Every device controller has a local buffer ROM l CPU moves data btwn main memory and local buffers l I/O is btwn device and local buffer of device controller OS l Device controller uses interrupt to inform CPU it is done Apps Network u Protection Data l Timer, paging (e.g. TLB), mode bit (e.g., kernel/user ) 6

Typical OS Structure Typical Unix OS Structure

User function calls written by programmers and Application Application compiled by programmers.

Libraries User level Libraries

Kernel level Portable OS Layer Portable OS Layer

Machine-dependent layer Machine-dependent layer

7 8

2 9/16/19

Typical Unix OS Structure Quick Review: How Application is Created

• Written by elves foo. gcc foo.s as foo.o • Objects pre-compiled • Defined in headers bar.c gcc bar.s as bar.o ld a.out Application • Input to • Invoked like functions • May be “resolved” when libc.a … Libraries program is loaded

u gcc can compile, assemble, and link together u Compiler (part of gcc) compiles a program into assembly Portable OS Layer u Assembler compiles assembly code into relocatable u Linker links object files into an Machine-dependent layer u For more information: l Read man of a.out, elf, ld, and nm l Read the document of ELF

9 10

Application: How it’s executed What an executable application looks like

u On Unix, “loader” does the job u Four segments 2n -1 l Read an executable file l Code/Text – instructions Stack l Layout the code, data, heap and stack l Data – global variables l Dynamically link to shared libraries l Stack l Prepare for the OS kernel to run the application l Heap u Why: l Separate code and data? Application Heap *.o, *.a ld a.out loader l Have stack and heap go towards each other? Shared Initialized data Code 0

11 12

3 9/16/19

Responsibilities for the segments Typical Unix OS Structure

u Stack l Layout by ? l Allocated/deallocated by ? l Local names are absolute/relative? u Heap Application l Who sets the starting address? l Allocated/deallocated by ? Libraries l How do application programs manage it?

u Global data/code l Who allocates? Portable OS Layer “Guts” of system calls l Who defines names and references? l Who translates references? Machine-dependent layer l Who relocates addresses? l Who lays them out in memory?

13 14

Must Support Multiple Applications Multiple Application Processes

u In multiple windows l Browser, shell, powerpoint, word, …

Application Application Application u Use command line to run multiple applications … ‘ ’ % ls –al | grep ^d Libraries Libraries Libraries % foo & % bar &

Portable OS Layer

Machine-dependent layer

15 16

4 9/16/19

OS Service Examples Typical Unix OS Structure

u Examples that are not provided at user level l System calls: file open, close, read and write l Control the CPU so that users won’t cause problems • while ( 1 ) ; Application • Bootstrap l Protection: • System initialization • Interrupt and exception • Keep user programs from crashing OS Libraries • I/O device driver • Keep user programs from crashing each other • u Examples that are provided at user level • Mode switching • Processor management l Read time of day Portable OS Layer l Protected user-level activities Machine-dependent layer

17 18

Today OS components

u Overview of OS functionality u Resource manager for each HW resource l CPU: processor management u Overview of OS components l RAM: memory management u Interacting with the OS l Disk: file system and secondary-storage management u Booting a Computer l I/O device management (keyboards, mouse, …) u Additional services: l networking l window manager (GUI) l command-line interpreters (e.g., shell) l resource allocation and accounting l protection • Keep user programs from crashing OS • Keep user programs from crashing each other

20

5 9/16/19

Processor Management Memory Management

u Goals u Goals l Overlap between I/O and CPU I/O CPU Register: 1x computation l Support for programs to run CPU I/O and to be written more easily L1 cache: 2-4x l Time sharing l Allocation and management l Multiple CPU allocation CPU L2 cache: ~10x u Issues l Transfers from and to secondary storage L3 cache: ~50x l Do not waste CPU resources CPU u Issues l Synchronization and mutual I/O DRAM: ~200-500x exclusion CPU l Efficiency & convenience Disks: ~30M x l Fairness and deadlock CPU l Fairness l Protection CPU Archive storage: >1000M x

22 23

File System I/O Device Management u Goals: u Goals l Manage disk blocks l Interactions between l Map between files and disk blocks User 1 . . . User n devices and applications u Typical file system calls User 1 . . . User n l Ability to plug in new l Open a file with authentication File system services devices l Read/write data in files u l Close a file Issues Library support File . . . File u Issues l Diversity of devices, third- l Reliability party hardware Driver Driver l Safety l Efficiency I/O I/O l Efficiency l Fairness device . . . device l Manageability l Protection and sharing

24 25

6 9/16/19

Window Systems Summary

u Goals u Overview of OS functionality l Interacting with a user l Layers of abstraction l Interfaces to examine and l Services to applications manage apps and the system l Resource management u Issues u Overview of OS components l Inputs from keyboard, mouse, l Processor management touch screen, … l Memory management l Display output from applications l I/O device management and systems l File system l Where is the Window System? l Window system • All in the kernel (Windows) • All at user level l … • Split between user and kernel (Unix) 26 27

Today How the OS is Invoked

u Overview of OS functionality u Exceptions u Overview of OS components l Normal or program error: faults, traps, aborts u Interacting with the OS l Special software generated: INT 3 l Machine-check exceptions u Booting a Computer u Interrupts l Hardware (by external devices) l Software: INT n u System calls

u See Intel document volume 3 for details

28 29

7 9/16/19

Interrupts Interrupt and Exceptions (1)

u Raised by external events Vector # Mnemonic Description Type

u Interrupt handler is in kernel 0: 0 #DE Divide error (by zero) Fault u Eventually resume the 1: 1 #DB Debug Fault/trap Interrupt … 2 NMI interrupt Interrupt interrupted handler u A way to i: 3 #BP Breakpoint Trap i+1: 4 #OF Overflow Trap l Switch CPU to another … process 5 #BR BOUND range exceeded Trap l Overlap I/O with CPU N: 6 #UD Invalid opcode Fault 7 #NM Device not available Fault l Handle other long-latency events 8 #DF Double fault Abort 9 Coprocessor segment overrun Fault 10 #TS Invalid TSS (Task State Segment). Kernel/HW bug.

30 31

Interrupt and Exceptions (2) System Calls

Vector # Mnemonic Description Type u Operating system API 11 #NP Segment not present Fault l Interface between an application and the operating 12 #SS Stack-segment fault Fault system kernel

13 #GP u Categories of system calls

14 #PF Page fault Fault l Process management l Memory management 15 Reserved Fault l File management 16 #MF Floating-point error (math fault) Fault l Device management 17 #AC Alignment check Fault l Communication 18 #MC Machine check Abort

19-31 Reserved

32-255 User defined Interrupt

32 33

8 9/16/19

How many system calls? Mechanism

u 6th Edition Unix: ~45 u Assumptions l User code can be arbitrary u POSIX: ~130 l User code cannot modify kernel User User u FreeBSD: ~130 memory program program u : ~250 u Design Issues return call l User makes a system call with u Windows 7: > 900 parameters l The call mechanism switches entry code to kernel mode Kernel in l Execute system call protected memory l Return with results

34 35

OS Kernel: Trap Handler Interrupt, trap and syscall vector

Interrupt u Table set up by OS kernel; pointers to code to run service on different events HW Device Syscall table routines Interrupt Processor Interrupt Register Vector Table System Call System Service System HW exceptions dispatcher services handleTimerInterrupt() { SW exceptions ... Exception } Virtual address dispatcher Exception exceptions handlers handleDivideByZero() { ... VM } manager’s pager HW support handleSystemCall() { ... } 36

9 9/16/19

From http://minnie.tuhs.org/UnixTree/V6 Passing Parameters

u Pass by registers l # of registers l # of usable registers l # of parameters in system call l Spill/fill code in compiler u Pass by a memory vector (list) l Single register for starting address l Vector in user’s memory u Pass by stack l Similar to the memory vector l Procedure call convention

38 39

Library Stubs for System Calls System Call

u Example: EntryPoint: int read( int fd, char * buf, int size) User switch to kernel stack User { program User memory save context stack move fd, buf, size to R1, R2, R3 check R0 move READ to R Int $0x80 iret Registers 0 call the real code pointed by R int $0x80 Linux: 80 0 place result in Rresult move result to Rresult NT: 2E Registers } restore context switch to user stack Kernel Kernel in iret (change to user mode and return) stack Kernel protected memory memory (Assumes passing parameters in registers)

40 41

10 9/16/19

Kernel stacks System call stubs

Per-processor, located in kernel memory. Why can’t the interrupt User Program Kernel handler run on the stack of the interrupted user process? main () { file_open(arg1, arg2) { file_open(arg1, arg2); // do operation Running Ready to Run Waiting for I/O } }

Syscall (1) (6) (3) (4) User Stack Proc2 Proc2 Proc2 Proc1 Proc1 Proc1 Main Main Main User Stub (2) Kernel Stub Hardware Trap file_open(arg1, arg2) { file_open_handler() { I/O Driver push #SYSCALL_OPEN // copy arguments trap // from user memory Top Half Trap Return return // check arguments Kernel Stack Syscall } (5) file_open(arg1, arg2); Handler // copy return value // into user memory User CPU User CPU return; State State }

Design Issues Backward compatibility...

u System calls l There is one result register; what about more results? l How do we pass errors back to the caller?

u System calls vs. library calls l What should be system calls? l What should be library calls?

45 47

11 9/16/19

Division of Labor (Separation Of Concerns) Today

Memory management example u Overview of OS functionality u Kernel u Overview of OS components l Allocates “pages” with protection u Interacting with the OS l Allocates a big chunk (many pages) to library u Booting a Computer l Does not care about small allocations u Library l Provides malloc/free for allocation and deallocation l Applications use them to manage memory l When reaching the end, library asks kernel for more

48 50

Booting a Computer System Boot

u Power up a computer Physical u Power on (processor waits until Power Good Memory u Processor reset Signal) (1) l Set to known state BIOS copies BIOS u Processor jumps to a fixed address, which is the l Jump to ROM code Disk bootloader start of the ROM BIOS program (for x86, this is the BIOS) Bootloader instructions u Load in the boot loader from (2) and data Bootloader stable storage Bootloader copies OS kernel u OS kernel Jump to the boot loader OS kernel u Load the rest of the operating Login app instructions and data system (3) OS kernel copies u Initialize and run login application Login app instructions and data

51 COS318 Lec 2 52

12 9/16/19

ROM Bios Startup Program (1) ROM BIOS startup program (2)

u Look for logical devices u POST (Power-On Self-Test) l Label them • Stop booting if fatal errors, and report • Serial ports: COM 1, 2, 3, 4 u Look for video card and execute built-in BIOS • Parallel ports: LPT 1, 2, 3 code (normally at C000h) l Assign each an I/O address and interrupt numbers u Look for other devices ROM BIOS code u Detect and configure Plug-and-Play (PnP) devices l IDE/ATA disk ROM BIOS at C8000h 9=818200d) u Display configuration information on screen u Display startup screen • BIOS information u Execute more tests • memory • system inventory

COS318 Lec 2 53 COS318 Lec 2 54

ROM BIOS startup program (3) Summary u Search for a drive to BOOT from u Protection mechanism l Hard disk or USB drive or CD/DVD l Architecture support: two modes u Load code in boot sector l Software traps (exceptions) u Execute boot loader u OS structures u Boot loader loads program to be booted l Monolithic, layered, microkernel and virtual machine l If no OS: "Non-system disk or disk error - Replace and u System calls press any key when ready" l Implementation u Transfer control to loaded program l Design issues l Could be OS or another feature-rich bootloader (e.g. l Tradeoffs with library calls GRUB), which then loads the actual OS

COS318 Lec 2 55 56

13