cs 61c: great ideas in computer architecture lecture 22...

42
CS 61C: Great Ideas in Computer Architecture Lecture 22: Operating Systems Krste Asanović & Randy H. Katz http://inst.eecs.berkeley.edu/~cs61c/fa17 1 Fall 2017 -- Lecture #22 11/13/17

Upload: others

Post on 30-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

CS61C:GreatIdeasinComputerArchitecture

Lecture22:OperatingSystems

Krste Asanović &RandyH.Katz

http://inst.eecs.berkeley.edu/~cs61c/fa17

1Fall2017 -- Lecture#2211/13/17

Page 2: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Memory

CS61Csofar…

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 HW1

Project1

Project2

2Fall2017 -- Lecture#2211/13/17

Page 3: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

SoHowisaLaptopAnyDifferent?

Keyboard

Screen

Storage

3Fall2017 -- Lecture#2211/13/17

Page 4: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Memory

AddingI/O

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

HW1

Project2

I/O(Input/Output)

Screen Keyboard Storage

4Fall2017 -- Lecture#2211/13/17

Project1

Page 5: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

CPU+$s,Memory

RaspberryPi(Lessthan$40onAmazonin2017)

StorageI/O(MicroSDCard)

SerialI/O(USB)

NetworkI/O(Ethernet)ScreenI/O

(HDMI)

5Fall2017 -- Lecture#2211/13/17

Page 6: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

It’saRealComputer!

6Fall2017 -- Lecture#2211/13/17

Page 7: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

ButWait…• That’snotthesame!WhenwerunVENUS,itonlyexecutesoneprogramandthenstops.• WhenIswitchonmycomputer,Igetthis:

Yes,butthat’sjust software!TheOperatingSystem(OS)

7Fall2017 -- Lecture#2211/13/17

Page 8: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Well,“JustSoftware”

• Thebiggestpieceofsoftwareonyourmachine?• Howmanylinesofcode?Theseareguesstimates:

Codebases(inmillionsoflinesofcode).CCBY-NC3.0—

DavidMcCandless©2013http://www.informationisbeautiful.net/visualizations/million-lines-of-code/

8Fall2017 -- Lecture#2211/13/17

Page 9: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

OperatingSystem

9Fall2017 -- Lecture#2211/13/17

Page 10: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

WhatDoestheOSdo?• OSisfirstthingthatrunswhencomputerstarts• Findsandcontrolsalldevicesinthemachineinageneralway

− Relyingonhardwarespecific“devicedrivers”• Startsservices(100+)

− Filesystem,− Networkstack(Ethernet,WiFi,Bluetooth,…),− TTY(keyboard),−…

• Loads,runsandmanagesprograms:−Multipleprogramsatthesametime(time-sharing)− Isolateprogramsfromeachother(isolation)−Multiplexresourcesbetweenapplications(e.g.,devices)

10Fall2017 -- Lecture#2211/13/17

Page 11: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

11Fall2017 -- Lecture#2211/13/17

Page 12: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

12Fall2017 -- Lecture#2211/13/17

Page 13: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

HowtoInteractwithDevices?• AssumeaprogramrunningonaCPU.Howdoesitinteractwiththeoutsideworld?• NeedI/OinterfaceforKeyboards,Network,Mouse,Screen,etc.− Connecttomany typesofdevices− Controlthesedevices,respondtothem,andtransferdata

− Presentthemtouserprogramssotheyareuseful

cmd reg.datareg.

OperatingSystem

Processor Memory

PCIBus

USB

SATA,SAS,…

13Fall2017 -- Lecture#2211/13/17

Page 14: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

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,usedbyRISC-V

14Fall2017 -- Lecture#2211/13/17

Page 15: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

MemoryMappedI/O

• Certainaddressesarenotregularmemory• Instead,theycorrespondtoregistersinI/Odevices

cntrl reg.datareg.

0xFFFFFFFF

0x00000000

0x8000000

address

15Fall2017 -- Lecture#2211/13/17

0x7FFFFFFFMemory-mapped

Program&DataMemory

Page 16: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

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

16Fall2017 -- Lecture#2211/13/17

Page 17: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

17Fall2017 -- Lecture#2211/13/17

Page 18: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

18Fall2017 -- Lecture#2211/13/17

Page 19: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

ProcessorChecksStatusbeforeActing

• Deviceregistersgenerallyservetwofunctions:• 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”

19Fall2017 -- Lecture#2211/13/17

Page 20: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

• Input:Readfromkeyboardintoa0lui t0,0x7ffff #7ffff000 (io addr)

Waitloop: lw t1,0(t0) #read controlandi t1,t1,0x1 #ready bitbeq t1,zero,Waitlooplw a0,4(t0) #data

• Output:Writetodisplayfroma1lui t0,0x7ffff #7ffff0000

Waitloop: lw t1,8($t0) #write controlandi t1,t1,0x1 #ready bitbeq t1,zero,Waitloopsw a1,12(t0) #data

“Ready”bitisfromprocessor’spointofview!

I/OExample(Polling)

20Fall2017 -- Lecture#2211/13/17

Page 21: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

CostofPolling?

• Assumeforaprocessorwith− 1GHzclockrate− Taking400clockcyclesforapollingoperation

§ Callpollingroutine§ Checkdevice(e.g.,keyboardorwifi inputavailable)§ Return

−What’sthepercentageofprocessortimespentpolling?

• Example:−Mouse− Poll30timespersecond

§ Setbyrequirementnottomissanymousemotion(whichwouldleadtochoppymotionofthecursoronthescreen)

21Fall2017 -- Lecture#2211/13/17

Page 22: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

PeerInstructionHarddisk:transfersdatain16-Bytechunksandcantransferat16MB/second.Notransfercanbemissed.Whatpercentageofprocessortimeisspentinpolling(assume1GHzclock)?

• 2%• 4%• 20%• 40%

22Fall2017 -- Lecture#2211/13/17

Page 23: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

WhatistheAlternativetoPolling?

• Pollingwastesprocessorresources• Akintowaitingatthedoorforgueststoshowup

−Whataboutabell?

• Computerlingoforbell:− Interrupt− OccurswhenI/Oisreadyorneedsattention

§ Interruptcurrentprogram§ Transfercontroltospecialcode“interrupthandler”

23Fall2017 -- Lecture#2211/13/17

Page 24: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

24Fall2017 -- Lecture#2211/13/17

Page 25: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Traps/Interrupts/Exceptions:alteringthenormalflowofcontrol

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.

25Fall2017 -- Lecture#2211/13/17

Page 26: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Interrupt-DrivenI/O

Label: sll t1,s3,2add t1,t1,s5lw t1,0(t1) or s1,s1,t1add s3,s3,s4bne s3,s2,Label

StackFrame

StackFrame

StackFrame

handler: save registersdecode interrupt causehandle interruptclear interruptrestore registersmret

Interrupt(SPI0)

CPUVector InterruptTable

SPI0 handler

… …

HandlerExecution1. Incominginterruptsuspendsinstructionstream2. Looksupthevector(functionaddress)ofahandlerin

aninterruptvectortablestoredwithintheCPU3. Performajal tothehandler(savePCinspecialMEPC*register)4. Handlerrunoncurrentstackandreturnsonfinish

(threaddoesn’tnoticethatahandlerwasrun)

*MEPC:MachineExceptionProgramCounter26Fall2017 -- Lecture#2211/13/17

Page 27: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

TerminologyInCS61C(otherdefinitionsinuseelsewhere):• Interrupt – causedbyaneventexternal tocurrentrunningprogram

− E.g.,keypress,diskI/O− Asynchronoustocurrentprogram

§ Canhandleinterruptonanyconvenientinstruction§ “Wheneverit’sconvenient,justdon’twaittoolong”

• Exception – causedbysomeeventduring executionofoneinstructionofcurrentrunningprogram− E.g.,dividebyzero,buserror,illegalinstruction− Synchronous

§ Musthandleexceptionprecisely oninstructionthatcausesexception§ “Dropwhateveryouaredoingandactnow”

• Trap – actionofservicinginterruptorexceptionbyhardwarejumpto“interruptortraphandler”code

27Fall2017 -- Lecture#2211/13/17

Page 28: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

PreciseTraps

• Traphandler’sviewofmachinestateisthateveryinstructionpriortothetrappedone(e.g.,overflow)hascompleted,andnoinstructionafterthetraphasexecuted.• Impliesthathandlercanreturnfromaninterruptbyrestoringuserregistersandjumpingbacktointerruptedinstruction− Interrupthandlersoftwaredoesn’tneedtounderstandthepipelineofthemachine,orwhatprogramwasdoing!

−Morecomplextohandletrapcausedbyanexceptionthaninterrupt

• Providingprecisetrapsistrickyinapipelinedsuperscalarout-of-orderprocessor!− Butarequirement,e.g.,for

§ Virtualmemorytofunctionproperly(seenextlecture)

28Fall2017 -- Lecture#2211/13/17

Page 29: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

TrapHandlingin5-StagePipeline

Exceptionsarehandledlikepipelinehazards1) Completeexecutionofinstructionsbeforeexceptionoccurred2) Flushinstructionscurrentlyinpipeline(i.e.,converttonops or“bubbles”)3) Optionallystoreexceptioncauseinstatusregister

− Indicatetypeofexception− Note:severalexceptionscanoccurinasingleclockcycle!

4) Transferexecutiontotraphandler

PCInst. Mem D Decode E M

Data Mem W+

Illegal Opcode

DivideBy 0

Data address Exceptions

PC address Exception

Asynchronous Interrupts

29Fall2017 -- Lecture#2211/13/17

Page 30: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

TrapPipelineDiagramtimet0 t1 t2 t3 t4 t5 t6 t7 . . . .

(I1) 096: DIV IF1 ID1 EX1 MA1 - divide by zero!(I2) 100: XOR IF2 ID2 EX2 - -(I3) 104: SUB IF3 ID3 - - -(I4) 108: ADD IF4 - - - -(I5) Trap Handler code* IF5 ID5 EX5 MA5 WB5

*MEPC=100(instructionfollowingoffendingADD)

30Fall2017 -- Lecture#2211/13/17

Page 31: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

31Fall2017 -- Lecture#2211/13/17

Page 32: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Administrivia

• Homework5isduetomorrow!• Project3:PerformanceProgramming

− DueMonday,Nov20(rightbeforeThanksgivingbreak)− ProjectPartytomorrow7-9pminCory293

• GuerrillaSessiontonightin7-9pminCory293− Topicscovered:Parallelism&MapReduce(FloatingPointiftimepermits)− Secondtolastguerrillasession—comeandgetvaluableexampractice!

• Project4willbereleasedbynextMondayatthelatest− You’llgetatleast2weekstocompleteit− Designedtobestraightforward!

11/13/17 Fall2017 -- Lecture#22 32

Page 33: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

33Fall2017 -- Lecture#2211/13/17

Page 34: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

WhatHappensatBoot?• Whenthecomputerswitcheson,itdoesthesameasVENUS:theCPUexecutesinstructionsfromsomestartaddress(storedinFlashROM)

CPU

PC=0x2000(somedefaultvalue) AddressSpace

0x0002000:Code to copy firmware into regular memory and jump into it)

Memorymapped

34Fall2017 -- Lecture#2211/13/17

Page 35: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

WhatHappensatBoot?

1.BIOS*:Findastoragedeviceandloadfirstsector(blockofdata)

2.Bootloader(storedon,e.g.,disk):LoadtheOSkernel fromdiskintoalocationinmemoryandjumpintoit

3.OSBoot:Initializeservices,drivers,etc.

4.Init:Launchanapplicationthatwaitsforinputinloop(e.g.,Terminal/Desktop/...

*BIOS:BasicInputOutputSystem35Fall2017 -- Lecture#2211/13/17

Page 36: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

LaunchingApplications• Applicationsarecalled“processes”inmostOSs

− Thread:sharedmemory− Process:separatememory− Boththreadsandprocessesrun(pseudo)simultaneously

• Appsarestartedbyanotherprocess(e.g.,shell)callinganOSroutine(usinga“syscall”)− DependsonOS,butLinuxusesfork tocreateanewprocess,andexecve (executefilecommand)toloadapplication

• Loadsexecutablefilefromdisk(usingthefilesystemservice)andputsinstructions&dataintomemory(.text,.datasections),preparesstackandheap• Setargc andargv,jumptostartofmain• Shellwaitsformaintoreturn(join)

36Fall2017 -- Lecture#2211/13/17

Page 37: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

SupervisorMode• Ifsomethinggoeswronginanapplication,itcouldcrashtheentiremachine.Andwhataboutmalware,etc.?• TheOSenforcesresourceconstraintstoapplications(e.g.,accesstomemory,devices)• TohelpprotecttheOSfromtheapplication,CPUshaveasupervisormode(e.g.,setbyastatusbitinaspecialregister)− Aprocesscanonlyaccessasubsetofinstructionsand(physical)memorywhennotinsupervisormode(usermode)

− Processcanchangeoutofsupervisormodeusingaspecialinstruction,butnotintoitdirectly– onlyusinganinterrupt

− Supervisorymodeisabitlike“superuser”§ Butusedmuchmoresparingly(mostofOScodedoesnot runinsupervisorymode)§ Errorsinsupervisorymodeoftencatastrophic(blue“screenofdeath”,or“Ijustcorruptedyourdisk”)

37Fall2017 -- Lecture#2211/13/17

Page 38: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Syscalls• WhatifwewanttocallanOSroutine?E.g.,

− toreadafile,− launchanewprocess,− askformorememory(malloc),− senddata,etc.

• Needtoperformasyscall:− Setupfunctionargumentsinregisters,− Raisesoftwareinterrupt(withspecialassemblyinstruction)

• OSwillperformtheoperationandreturntousermode• Thisway,theOScanmediateaccesstoallresources,anddevices

38Fall2017 -- Lecture#2211/13/17

Page 39: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Agenda

• DevicesandI/O• Polling• Interrupts• OSBootSequence• Multiprogramming/time-sharing

39Fall2017 -- Lecture#2211/13/17

Page 40: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Multiprogramming• TheOSrunsmultipleapplicationsatthesametime• Butnotreally(unlessyouhaveacoreperprocess)• Switchesbetweenprocessesveryquickly(onhumantimescale)–thisiscalleda“contextswitch”• Whenjumpingintoprocess,settimerinterrupt

−Whenitexpires,storePC,registers,etc.(processstate)− Pickadifferentprocesstorunandloaditsstate− Settimer,changetousermode,jumptothenewPC

• Decidingwhatprocesstoruniscalledscheduling

40Fall2017 -- Lecture#2211/13/17

Page 41: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

Protection,Translation,Paging• SupervisormodealoneisnotsufficienttofullyisolateapplicationsfromeachotherorfromtheOS− Applicationcouldoverwriteanotherapplication’smemory.− Typicallyprogramsstartatsomefixedaddress,e.g.0x8FFFFFFF

§ Howcan100’sofprogramssharememoryatlocation0x8FFFFFFF?− Also,maywanttoaddressmorememorythanweactuallyhave(e.g.,forsparsedatastructures)

• Solution:VirtualMemory− Giveseachprocesstheillusion ofafullmemoryaddressspacethatithascompletelyforitself

41Fall2017 -- Lecture#2211/13/17

Page 42: CS 61C: Great Ideas in Computer Architecture Lecture 22 ...inst.eecs.berkeley.edu/~cs61c/fa17/lec/22/L22 OS (1up).pdf · Great Ideas in Computer Architecture Lecture 22: Operating

And,inConclusion,…• 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.divideby0,pagefault(nextlecture)− “Precise”handling:immediatelyatoffendinginstruction

• Interrupts− Notificationofexternalevents,e.g.,keyboardinput,diskorEthernettraffic

• Multiprogrammingandsupervisorymode− Enablesandisolatesmultipleprograms

• TakeCS162tolearnmoreaboutoperatingsystems

42Fall2017 -- Lecture#2211/13/17