Operating System Structures Vivek Pai Princeton University COS318 Lec 2 1 Gedankenexperiment What does this program do? static void Loop(void) { static char *startAddr; char local; printf(“diff is %d\n”, startAddr – (&local)); startAddr = &local; Loop( ); } int main(int argc, char *argv[ ]) { Loop( ); } COS318 Lec 2 2 Mechanics Have you: Subscribed to pu.cs.318? Sent me mail with your details? – Hey, it was an assignment after all… Sent me a picture of yourself? COS318 Lec 2 3 More Mechanics Syllabus is now “complete”, along with goals for each lecture Preparatory readings are listed – External readings are not yet included Projects are being finalized – Regular lab not yet operational How do we want to handle quizzes? – Scheduled or random? COS318 Lec 2 4 Reading Assignment Sections 1.0-1.3 inclusive Keep up with what’s on home page I’ll try to remember to remind you COS318 Lec 2 5 A Typical Computer from a Hardware Point of View CPU Memory ... CPU Chipset I/O bus Network COS318 Lec 2 6 A Typical Computer System CPU .. . Memory Programs and data Operating System Software CPU OS Apps Data Network COS318 Lec 2 7 Typical Unix OS Structure Application Libraries User space/level Kernel space/level Portable OS Layer Machine-dependent layer COS318 Lec 2 8 Typical Unix OS Structure Written by programmer Compiled by programmer Uses function calls Application Libraries Portable OS Layer Machine-dependent layer COS318 Lec 2 9 Typical Unix OS Structure Written by elves Provided pre-compiled Defined in headers Input to linker (compiler) Invoked like functions May be “resolved” when program is loaded Application Libraries Portable OS Layer Machine-dependent layer COS318 Lec 2 10 Typical Unix OS Structure Application Libraries Portable OS Layer “Guts” of system calls All “high-level” code Machine-dependent layer COS318 Lec 2 11 Typical Unix OS Structure Application Libraries Portable OS Layer Machine-dependent layer COS318 Lec 2 Bootstrap System initialization Interrupt and exception I/O device driver Memory management Kernel/user mode switching Processor management 12 Another Look: Unix “Onion” Applications User and Kernel boundary OS Service Device Hardware Driver COS318 Lec 2 13 What’s An Application? Four parts (“segments”) Code/Text – instructions Data – initialized global variables Stack Heap What’s a stack and heap? COS318 Lec 2 14 OS Service Examples Examples that are not provided at user level – System calls: file open, close, read and write – Control the CPU so that users won’t stuck by running while ( 1 ) ; – Protection: • Keep user programs from crashing OS • Keep user programs from crashing each other Examples that can be provided at user level – Read time of the day – Protected user level stuff COS318 Lec 2 15 Processor Management Goals – Overlap between I/O and computation – Time sharing – Multiple CPU allocations CPU I/O CPU CPU CPU I/O Issues CPU – Do not waste CPU resources I/O – Synchronization and mutual CPU CPU exclusion – Fairness and deadlock free CPU COS318 Lec 2 16 Memory Management Goals – Support programs to run – Allocation and management – Transfers from and to secondary storage Register L2 10x Memory 200x Issues – Efficiency & convenience – Fairness – Protection COS318 Lec 2 Disk 10Mx Tape 100Mx 17 x86 Architecture Registers 31 15 87 AL BL CL DL AH BH CH DH BP SI DI SP 0 16-bit AX BX CX DX 32-bit EAX EBX ECX EDX EBP ESI EDI ESP 15 0 CS DS SS ES FS GS Segment registers General-purpose registers EFLAGS register EIP (Instruction Pointer register) COS318 Lec 2 18 x86 Memory 232-1 31 24 23 16 15 87 0 . . . Byte 7 Byte 6 Byte 5 Byte 4 Byte 3 Byte 2 Byte 1 Byte 0 0 Byte order is little endian COS318 Lec 2 19 I/O Device Management Goals – Interactions between devices and applications – Ability to plug in new devices Issues – Efficiency – Fairness – Protection and sharing User 1 ... User n Library support Driver Driver I/O device I/O device COS318 Lec 2 ... 20 Window Systems All in the kernel (Windows) – Pros: efficient? – Cons: difficult to develop new services All at user level – Pros: easy to develop new apps – Cons: protection Split between user and kernel (Unix) – Kernel: display driver and mouse driver – User: the rest COS318 Lec 2 21 File System A typical file system – Open a file with authentication – Read/write data in files – Close a file Can the services be moved to user level? User 1 ... User n File system services File COS318 Lec 2 ... File 22 Bootstrapping Power up a computer Processor reset – Set to known state – Jump to ROM code Load in the boot loader from stable storage Jump to the boot loader Load the rest of the operating system Initialize and run Boot loader COS318 Lec 2 Boot loader OS sector 1 OS sector 2 . . . OS sector n 23 System Boot Maps to FFFFFFF0h= 232-16 Power on (processor waits until Power Good Signal) Processor jumps on a PC (“Intel Inside”) to address FFFF0h • 1M= 1,048,576= 220 =FFFFFh+1 • FFFFFh=FFFF0h+16 is the end of the (first 1MB of) system memory • The original PC using Intel 8088 had 20 address lines :-) (FFFFFFF0h) is a JMP instruction to the ROM BIOS startup program COS318 Lec 2 24 ROM BIOS startup program (1) POST (Power-On Self-Test) • If pass then AX:=0; DH:=5 (586: Pentium); • Stop booting if fatal errors, and report Look for video card and execute built-in ROM BIOS code (normally at C000h) Look for other devices ROM BIOS code • IDE/ATA disk ROM BIOS at C8000h (=819,200d) Display startup screen • BIOS information SCSI disks: must often provide their own BIOS Execute more tests • memory • system inventory COS318 Lec 2 25 ROM BIOS startup program (2) Look for logical devices – Label them • Serial ports – COM 1, 2, 3, 4 • Parallel ports – LPT 1, 2, 3 – Assign each an I/O address and IRQ Detect and configure PnP devices Display configuration information on screen COS318 Lec 2 26 ROM BIOS startup program (3) Search for a drive to BOOT from – Floppy or Hard disk • Boot at cylinder 0, head 0, sector 1 Load code in boot sector Execute boot loader Boot loader loads program to be booted • If no OS: "Non-system disk or disk error - Replace and press any key when ready" Transfer control to loaded program Is it okay to boot at first sector on the floppy COS318 Lec 2 or disk? 27 Ways to Develop An Operating System A hardware simulator A virtual machine A good kernel debugger – When OS crashes, always goes to the debugger – Debugging over the network COS318 Lec 2 28