15-410, S’04- 1 -Hardware OverviewJan. 19, 2004Dave EckhardtDave EckhardtBruce MaggsBruce MaggsL04_Hardware15-410“Dude, what were you thinking?”15-410, S’04- 2 -SynchronizationToday's classToday's classNot exactly Chapter 2 or 13WednesdayWednesdayProject 0 is dueLecture on “The Process”15-410, S’04- 3 -OutlineComputer hardwareComputer hardwareCPU StateCPU StateFairy tales about system callsFairy tales about system callsCPU context switch (intro)CPU context switch (intro)Interrupt handlersInterrupt handlersInterrupt maskingInterrupt masking15-410, S’04- 4 -Inside The Box - Historical/LogicalCPUMemoryGraphicsEthernetIDEFloppyUSB15-410, S’04- 5 -Inside The Box - ReallyCPUMemoryAGP GraphicsIDEFloppyUSBNorth BridgeSouth BridgePCIEthernetSCSI15-410, S’04- 6 -CPU StateUser registers (on Planet Intel)User registers (on Planet Intel)General purpose - %eax, %ebx, %ecx, %edxStack Pointer - %espFrame Pointer - %ebpMysterious String Registers - %esi, %edi15-410, S’04- 7 -CPU StateNon-userNon-user registers, a.k.a.... registers, a.k.a....Processor status register(s)Processor status register(s)User process / Kernel processInterrupts on / offVirtual memory on / offMemory modelsmall, medium, large, purple, dinosaur15-410, S’04- 8 -CPU StateFloating Point Number registersFloating Point Number registersLogically part of “User registers”Sometimes special insteadSome machines don't have floating pointSome processes don't use floating point15-410, S’04- 9 -Story time!Time for some fairy talesTime for some fairy talesThe getpid() story (shortest legal fairy tale)The read() story (toddler version)The read() story (grade-school version)15-410, S’04- 10 -The Story of getpid()User process is computingUser process is computingUser process calls getpid() library routineLibrary routine executes TRAP(314159)The world changesThe world changesSome registers dumped into memory somewhereSome registers loaded from memory somewhereThe processor has The processor has entered kernel modeentered kernel mode15-410, S’04- 11 -User ModeOperatingSystemProcess 1Process 2CPU15-410, S’04- 12 -Entering Kernel ModeOperatingSystemProcess 1Process 2CPU15-410, S’04- 13 -Entering Kernel ModeOperatingSystemProcess 1Process 2CPUEthernetIDEFloppyUSB15-410, S’04- 14 -The Kernel Runtime EnvironmentLanguage runtimes differLanguage runtimes differML: no stack, “nothing but heap”C: stack-basedProcessor is mostly agnosticProcessor is mostly agnosticTrap handler builds kernel runtime environmentTrap handler builds kernel runtime environmentSwitches to correct stackTurns on virtual memoryFlushes caches15-410, S’04- 15 -The Story of getpid()Process in kernel modeProcess in kernel moderunning->u_reg[R_EAX] = running->u_pid;Return from interruptReturn from interruptProcessor state restored to user mode(modulo %eax)User process returns to computingUser process returns to computingLibrary routine returns %eax as value of getpid()15-410, S’04- 16 -Returning to User ModeOperatingSystemProcess 1Process 2CPU15-410, S’04- 17 -A Story About read()User process is computingUser process is computing•count = read(0, buf, sizeof (buf));User process “goes to sleep”User process “goes to sleep”Operating system issues disk readOperating system issues disk readTime passesTime passesOperating system copies dataOperating system copies dataUser process wakes upUser process wakes up15-410, S’04- 18 -Another Story About read()P1: read()P1: read() Trap to kernel modeKernel: issue disk readKernel: issue disk readKernel: switch to P2Kernel: switch to P2 Return from interrupt - but to P2, not P1!P2: compute 1/3 of Mandelbrot setP2: compute 1/3 of Mandelbrot set15-410, S’04- 19 -Another Story About read()Disk: done!Disk: done! Interrupt to kernel modeKernel: switch to P1Kernel: switch to P1 Return from interrupt - but to P1, not P2!15-410, S’04- 20 -Interrupt Vector TableHow does CPU handle How does CPU handle thisthis interrupt? interrupt? Disk interrupt -> disk driver Mouse interrupt -> mouse driverNeed to knowNeed to know Where to dump registersoften: property of current process, not of interrupt New register values to load into CPUkey: new program counter, new status register15-410, S’04- 21 -IA32 Single-Task Mode Example•Interrupt/Exception while in kernel mode (Project 1)Interrupt/Exception while in kernel mode (Project 1)•Hardware pushes registers on current stack, NO STACK CHANGEHardware pushes registers on current stack, NO STACK CHANGE1. EFLAGS (processor state)2.CS/EIP (return address)3. Error code (if interrupted exception procedure wants to)From intel-sys.pdf15-410, S’04- 22 -Interrupt Vector TableTable lookupTable lookup Interrupt controller says: this is interrupt source #3CPU knows table base pointer, table entry sizeCPU knows table base pointer, table entry sizeSpew, slurp, off we goSpew, slurp, off we go15-410, S’04- 23 -Race Conditionsif (device_idle) start_device(request);else enqueue(request);15-410, S’04- 24 -Race ConditionsaaaaaaUser process Interrupthandlerif(device_idle)INTERRUPT...device_idle= 1;RETURN FROMINTERRUPTenqueue(request)15-410, S’04- 25 -Interrupt maskingAtomic actionsAtomic actions Block device interrupt while checking and enqueueing Or use a lock-free data structure[left as an exercise for the reader]Avoid blocking Avoid blocking allall interrupts interrupts [not a big issue for 15-410]Avoid blocking too longAvoid blocking too long Part of Project 3 grading criteria15-410, S’04- 26 -Timer – BehaviorCount somethingCount somethingCPU cycles, bus cycles, microsecondsWhen you hit a limit, generate an interruptWhen you hit a limit, generate an interruptReload counter (don't wait for software to do it)Reload counter (don't wait for software to do it)15-410, S’04- 27 -Timer – Why?Why interrupt a perfectly good execution?Why interrupt a perfectly good execution?Avoid CPU hogsAvoid CPU hogs for (;;) ;Maintain accurate time of dayMaintain accurate time of dayBattery-backed calendar counts only seconds (poorly)Dual-purpose interruptDual-purpose interrupt++ticks_since_boot;force process switch
View Full Document