CS 61C: Great Ideas in Computer Architecture Lecture 22
-
Upload
khangminh22 -
Category
Documents
-
view
1 -
download
0
Transcript of CS 61C: Great Ideas in Computer Architecture Lecture 22
CS61C:GreatIdeasinComputerArchitecture
Lecture22:OperatingSystem
BernhardBoser&RandyKatz
http://inst.eecs.berkeley.edu/~cs61c
Memory
CS61Csofar…
2
CPU
Caches
MIPSAssembly
CPrograms
• Four##words/block,#cache#size#=#1K#words##!
MulWwordKBlock#DirectKMapped#Cache#
8#Index#
Data#Index# Tag#Valid#
0#
1#
2#
.#
.#
.#
253#
254#
255#
31#30###.#.#.#################13#12##11####.#.#.####4##3##2##1##0#Byte#
offset#
20#
20#Tag#
Hit# Data#
32#
Block#offset#
What!kind!of!locality!are!we!taking!advantage!of?!31#
#include <stdlib.h>
int fib(int n) {returnfib(n-1) +fib(n-2);
}
.foolw $t0, 4($r0)addi $t1, $t0, 3beq $t1, $t2, foonop
Project1Project2
Labs
CS61c Lecture22:OperatingSystem
Project3
Memory
AddingI/O
4
CPU
Caches
MIPSAssembly
CPrograms
• Four##words/block,#cache#size#=#1K#words##!
MulWwordKBlock#DirectKMapped#Cache#
8#Index#
Data#Index# Tag#Valid#
0#
1#
2#
.#
.#
.#
253#
254#
255#
31#30###.#.#.#################13#12##11####.#.#.####4##3##2##1##0#Byte#
offset#
20#
20#Tag#
Hit# Data#
32#
Block#offset#
What!kind!of!locality!are!we!taking!advantage!of?!31#
#include <stdlib.h>
int fib(int n) {returnfib(n-1) +fib(n-2);
}
.foolw $t0, 4($r0)addi $t1, $t0, 3beq $t1, $t2, foonop
Project1
Project3
I/O(Input/Output)
Screen Keyboard Storage
CS61c Lecture22:OperatingSystem
CPU+$s,Memory
RaspberryPi($40onAmazon)
5
StorageI/O(MicroSDCard)
SerialI/O(USB)
NetworkI/O(Ethernet)ScreenI/O
(HDMI)
CS61c Lecture22:OperatingSystem
Butwait…
7
• That’snotthesame!WhenwerunMARS,itonlyexecutesoneprogramandthenstops.• WhenIswitchonmycomputer,Igetthis:
Yes,butthat’sjust software!TheOperatingSystem(OS)
CS61c Lecture22:OperatingSystem
Well,“justsoftware”
• Thebiggestpieceofsoftwareonyourmachine?• Howmanylinesofcode?Theseareguesstimates:
8
Codebases(inmillionsoflinesofcode).CCBY-NC3.0— DavidMcCandless ©2013http://www.informationisbeautiful.net/visualizations/million-lines-of-code/
CS61c Lecture22:OperatingSystem
WhatdoestheOSdo?
10
• OSisfirstthingthatrunswhencomputerstarts• Findsandcontrolsalldevicesinthemachineinageneralway
− Relyingonhardwarespecific“devicedrivers”
• Startsservices(100+)− Filesystem,− Networkstack(Ethernet,WiFi,Bluetooth,…),− TTY(keyboard),− …
• Loads,runsandmanagesprograms:− Multipleprogramsatthesametime(time-sharing)− Isolateprogramsfromeachother(isolation)− Multiplexresourcesbetweenapplications(e.g.,devices)
CS61c Lecture22:OperatingSystem
Agenda
11
• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing
CS61c Lecture22:OperatingSystem
Howtointeractwithdevices?
12
• AssumeaprogramrunningonaCPU.Howdoesitinteractwiththeoutsideworld?• NeedI/OinterfaceforKeyboards,Network,Mouse,Screen,etc.− Connecttomany typesofdevices− Controlthesedevices,respondtothem,andtransferdata
− Presentthemtouserprogramssotheyareuseful
cmd reg.datareg.
OperatingSystem
Processor Memory
PCIBus
USB
CS61c Lecture22:OperatingSystem
SATA,SAS,…
InstructionSetArchitectureforI/O
• WhatmusttheprocessordoforI/O?− Input:readasequenceofbytes− Output:writeasequenceofbytes
• Interfaceoptionsa) Specialinput/outputinstructions&hardwareb) MemorymappedI/O
§ PortionofaddressspacededicatedtoI/O§ I/Odeviceregistersthere(nomemory)§ Usenormalload/storeinstructions,e.g.lw/sw§ Verycommon,usedbyMIPS
13CS61c Lecture22:OperatingSystem
MemoryMappedI/O
• Certainaddressesarenotregularmemory• Instead,theycorrespondtoregistersinI/Odevices
cntrlreg.datareg.
0x0
0xFFFFFFFF
0xFFFF0000
address
14CS61c Lecture22:OperatingSystem
Processor-I/OSpeedMismatch• 1GHzmicroprocessorI/Othroughput:
− 4Gi-B/s(lw/sw)− TypicalI/Odatarates:
§ 10B/s (keyboard)§ 100Ki-B/s (Bluetooth)§ 60Mi-B/s (USB2)§ 100Mi-B/s (Wifi,dependsonstandard)§ 125Mi-B/s (G-bitEthernet)§ 550Mi-B/s (cuttingedgeSSD)§ 1.25Gi-B/s (USB3.1Gen2)§ 6.4GiB/s (DDR3DRAM)
− Thesearepeakrates– actualthroughputislower• CommonI/Odevicesneitherdelivernoracceptdatamatchingprocessorspeed
15CS61c Lecture22:OperatingSystem
Agenda
17
• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing
CS61c Lecture22:OperatingSystem
ProcessorChecksStatusbeforeActing
• Deviceregistersgenerallyserve2functions:• ControlRegister,saysit’sOKtoread/write(I/Oready)[thinkofaflagmanonaroad]• DataRegister,containsdata
• ProcessorreadsfromControlRegisterinloop−waitingfordevicetosetReady bitinControlreg (0à 1)− Indicates“dataavailable”or“readytoacceptdata”
• Processorthenloadsfrom(input)orwritesto(output)dataregister• I/Odeviceresetscontrolregisterbit(1à 0)
• Procedurecalled“Polling”
18CS61c Lecture22:OperatingSystem
• Input:Readfromkeyboardinto$v0lui $t0, 0xffff #ffff0000 (io addr)
Waitloop: lw $t1, 0($t0) #read controlandi $t1,$t1,0x1 #ready bitbeq $t1,$zero, Waitlooplw $v0, 4($t0) #data
• Output:Writetodisplayfrom$a0lui $t0, 0xffff #ffff0000
Waitloop: lw $t1, 8($t0) #write controlandi $t1,$t1,0x1 #ready bitbeq $t1,$zero, Waitloopsw $a0,12($t0) #data
“Ready”bitisfromprocessor’spointofview!
I/OExample(polling)
19CS61c Lecture22:OperatingSystem
CostofPolling?
• Assumeforaprocessorwith− 1GHzclockrate− Taking400clockcyclesforapollingoperation
§ Callpollingroutine§ Checkdevice(e.g.keyboardorwifi inputavailable)§ Return
−What’sthepercentageofprocessortimespentpolling?
• Example:−Mouse− Poll30timespersecond
§ Setbyrequirementnottomissanymousemotion(whichwouldleadtochoppymotionofthecursoronthescreen)
20CS61c Lecture22:OperatingSystem
PercentProcessorTimetoPollMouse
• MousePolling[instructions/sec]=30[polls/s]*400[instructions/poll]=12K[instructions/s]
• %Processorforpolling:− Totalprocessorthroughput:T=1*109 [instructions/s]− Polling:P=12*103 [instructions/s]− Ratio:P/T=0.0012%à Negligible:
§ Pollingmousehaslittleimpactonprocessorthroughput
21CS61c Lecture22:OperatingSystem
ClickerTimeHarddisk:transfersdatain16-Bytechunksandcantransferat16MB/second.Notransfercanbemissed.Whatpercentageofprocessortimeisspentinpolling(assume1GHzclock)?
• A:2%• B:4%• C:20%• D:40%• E:80%
22CS61c Lecture22:OperatingSystem
%Processortimeneededpolldisk• FrequencyofPollingDisk
=16[MB/s]/16[B/poll]=1M[polls/s]
• DiskPolling,instructions/sec=1M[polls/s]*400[instructions/poll]=400M[instructions/s]• %Processorforpolling:
400*106 [instructions/s]/1*109 [instructions/s]= 40%à Unacceptable
23CS61c Lecture22:OperatingSystem
Whatisthealternativetopolling?
• Pollingwastesprocessorresources• Akintowaitingatthedoorforgueststoshowup
−Whataboutabell?
• Computerlingoforbell:− Interrupt− OccurswhenI/Oisreadyorneedsattention
§ Interruptcurrentprogram§ Transfercontroltospecialcode“interrupthandler”
24CS61c Lecture22:OperatingSystem
Agenda
25
• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing
CS61c Lecture22:OperatingSystem
26
Traps/Interrupts/Exceptions:alteringthenormalflowofcontrol(P&HSection4.9)
Ii-1 HI1
HI2
HIn
Ii
Ii+1
programtraphandler
An external or internal event that needs to be processed - by another program – the OS. The event is often unexpected from original program’s point of view.
CS61c Lecture22:OperatingSystem
Interrupt-drivenI/O
Label: sll $t1,$s3,2addu $t1,$t1,$s5lw $t1,0($t1) or $s1,$s1,$t1addu $s3,$s3,$s4bne $s3,$s2,Label
StackFrame
StackFrame
StackFrame
handler: lui $t0, 0xfffflw $t1, 0($t0)andi $t1,$t1,0x1lw $v0, 4($t0)sw $t1, 8($t0)ret
Interrupt(SPI0)
CPUVector InterruptTable
SPI0 handler
… …
HandlerExecution1. Incominginterruptsuspendsinstructionstream2. Looksupthevector(functionaddress)ofahandlerin
aninterruptvectortablestoredwithintheCPU3. Performajal tothehandler(savePCinEPC*register)4. Handlerrunoncurrentstackandreturnsonfinish
(threaddoesn’tnoticethatahandlerwasrun)
27CS61c Lecture22:OperatingSystem
Codeforsaving/restoring$t0,$t1,$v0notshown
*EPC:Exceptionprogramcounter
TerminologyInCS61C(otherdefinitionsinuseelsewhere):• Interrupt – causedbyaneventexternal tocurrentrunningprogram− E.g.keypress,disk− Asynchronoustocurrentprogram
§ Canhandleinterruptonanyconvenientinstruction§ “Wheneverit’sconvenient,justdon’twaittoolong”
• Exception – causedbysomeeventduring executionofoneinstructionofcurrentrunningprogram− E.g.,overflow,buserror,illegalinstruction− Synchronous
§ Musthandleexceptionprecisely oninstructionthatcausesexception§ “Dropwhateveryouaredoingandactnow”
• Trap – actionofservicinginterruptorexceptionbyhardwarejumpto“interruptortraphandler”code
PreciseTraps
• Traphandler’sviewofmachinestateisthateveryinstructionpriortothetrappedone(e.g.overflow)hascompleted,andnoinstructionafterthetraphasexecuted.• Impliesthathandlercanreturnfromaninterruptbyrestoringuserregistersandjumpingbacktointerruptedinstruction− Interrupthandlersoftwaredoesn’tneedtounderstandthepipelineofthemachine,orwhatprogramwasdoing!
− Morecomplextohandletrapcausedbyanexceptionthaninterrupt
• Providingprecisetrapsistrickyinapipelinedsuperscalarout-of-orderprocessor!− Butarequiremente.g.for
§ Virtualmemorytofunctionproperly(seenextlecture)
29CS61c Lecture22:OperatingSystem
30
TrapHandlingin5-StagePipeline
Exceptionsarehandledlikepipelinehazards1) Completeexecutionofinstructionsbeforeexceptionoccurred2) Flushinstructionscurrentlyinpipeline
(converttonops or“bubbles”)3) Optionallystoreexceptioncauseinstatusregister(e.g.MIPS)
− Indicatetypeofexception− Note:severalexceptionscanoccurinasingleclockcycle!
4) Transferexecutiontotraphandler
PCInst. Mem D Decode E M
Data Mem W+
Illegal Opcode Overflow Data address
ExceptionsPC address Exception
Asynchronous Interrupts
CS61c Lecture22:OperatingSystem
31
TrapPipelineDiagramtimet0 t1 t2 t3 t4 t5 t6 t7 . . . .
(I1) 096: ADD IF1 ID1 EX1 MA1 - overflow!(I2) 100: XOR IF2 ID2 EX2 - -(I3) 104: SUB IF3 ID3 - - -(I4) 108: ADD IF4 - - - -(I5) Trap Handler code* IF5 ID5 EX5 MA5 WB5
*EPC=100(instructionfollowingoffendingADD)
CS61c Lecture22:OperatingSystem
Agenda
33
• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing
CS61c Lecture22:OperatingSystem
Whathappensatboot?
34
• Whenthecomputerswitcheson,itdoesthesameasMARS:theCPUexecutesinstructionsfromsomestartaddress(storedinFlashROM)
CPU
PC=0x2000(somedefaultvalue) AddressSpace
0x2000:addi $t0, $zero, 0x1000lw $t0, 4($r0)…
(Code to copy firmware into regular memory and jump into it)
Memorymapped
CS61c Lecture22:OperatingSystem
Whathappensatboot?
35
1.BIOS*:Findastoragedeviceandloadfirstsector(blockofdata)
2.Bootloader (storedon,e.g.,disk):LoadtheOSkernel fromdiskintoalocationinmemoryandjumpintoit.
3.OSBoot:Initializeservices,drivers,etc.
4.Init:Launchanapplicationthatwaitsforinputinloop(e.g.,Terminal/Desktop/...
CS61c Lecture22:OperatingSystem
*BIOS:BasicInputOutputSystem
LaunchingApplications
36
• Applicationsarecalled“processes”inmostOSs.− Thread:sharedmemory− Process:separatememory− Boththreadsandprocessesrun(pseudo)simultaneously
• Appsarestartedbyanotherprocess(e.g.shell)callinganOSroutine(usinga“syscall”)− DependsonOS,butLinuxusesfork tocreateanewprocess,andexecve toloadapplication.
• Loadsexecutablefilefromdisk(usingthefilesystemservice)andputsinstructions&dataintomemory(.text,.datasections),preparesstackandheap.• Setargc andargv,jumptostartofmain.• Shellwaitsformaintoreturn(join)
CS61c Lecture22:OperatingSystem
SupervisorMode
37
• Ifsomethinggoeswronginanapplication,itcouldcrashtheentiremachine.Andwhataboutmalware,etc.?• TheOSenforcesresourceconstraintstoapplications(e.g.,accesstomemory,devices).• TohelpprotecttheOSfromtheapplication,CPUshaveasupervisormode(sete.g.byastatusbit).− Aprocesscanonlyaccessasubsetofinstructionsand(physical)memorywhennotinsupervisormode(usermode).
− Processcanchangeoutofsupervisormodeusingaspecialinstruction,butnotintoitdirectly– onlyusinganinterrupt.
− Supervisorymodeisabitlike“superuser”§ Butusedmuchmoresparingly(mostofOScodedoesnot runinsupervisorymode)
§ Errorsinsupervisorymodeoftencatastrophic(blue“screenofdeath”,or“Ijustcorruptedyourdisk”)
CS61c Lecture22:OperatingSystem
Syscalls
38
• WhatifwewanttocallanOSroutine?E.g.,− toreadafile,− launchanewprocess,− askformorememory(malloc),− senddata,etc.
• Needtoperformasyscall:− setupfunctionargumentsinregisters,− raisesoftwareinterrupt(withspecialassemblyinstruction)
• OSwillperformtheoperationandreturntousermode• Thisway,theOScanmediateaccesstoallresources,anddevices.
CS61c Lecture22:OperatingSystem
Agenda
39
• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing
CS61c Lecture22:OperatingSystem
Multiprogramming
40
• TheOSrunsmultipleapplicationsatthesametime.• Butnotreally(unlessyouhaveacoreperprocess)• Switchesbetweenprocessesveryquickly(onhumantimescale).Thisiscalleda“contextswitch”.• Whenjumpingintoprocess,settimerinterrupt.
−Whenitexpires,storePC,registers,etc.(processstate).− Pickadifferentprocesstorunandloaditsstate.− Settimer,changetousermode,jumptothenewPC.
• Decidingwhatprocesstoruniscalledscheduling.
CS61c Lecture22:OperatingSystem
Protection,Translation,Paging
41
• SupervisormodealoneisnotsufficienttofullyisolateapplicationsfromeachotherorfromtheOS.− Applicationcouldoverwriteanotherapplication’smemory.− Typicallyprogramsstartatsomefixedaddress,e.g.0x1000
§ Howcan100’sofprogramssharememoryatlocation0x1000?− Also,maywanttoaddressmorememorythanweactuallyhave(e.g.,forsparsedatastructures).
• Solution:VirtualMemory.− Giveseachprocesstheillusion ofafullmemoryaddressspacethatithascompletelyforitself.
CS61c Lecture22:OperatingSystem
And,inConclusion,…
42
• Basicmachine(datapath,memory,IOdevices)areapplicationagnostic• Sameconcepts/processorarchitectureapplytolargevarietyofapplications.E.g.− OSwithcommandlineandgraphicalinterface(Linux,…)− Embeddedprocessorinnetworkswitch,carenginecontrol,…
• Input/output(I/O)− Memorymapped:appearslike“specialkindofmemory”− Accesswithusualload/storeinstructions(e.g.lw, sw)
• Exceptions− Notifyprocessorofspecialevents,e.g.overflow,pagefault(nextlecture)− “precise”handling:immediatelyatoffendinginstruction
• Interrupts− Notificationofexternalevents,e.g.keyboardinputorEthernettraffic
• Multiprogrammingandsupervisorymode− Enablesandisolatesmultipleprograms
• TakeCS162tolearnmoreaboutoperatingsystemsCS61c Lecture22:OperatingSystem