34
CSE 306: Opera.ng Systems Interrupts and System Calls Don Porter CSE 306 1

Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

InterruptsandSystemCalls

DonPorterCSE306

1

Page 2: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

App

LastTime…

2-2

Hardware

Libraries

Kernel

User

Super-visor

App

Libraries

App

Libraries

SystemCallTable(350—1200)

Openfile“hw1.txt”

Ok,here’shandle4

Page 3: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Lecturegoal•  Understandhowsystemcallswork–  AswellashowexcepUons(e.g.,dividebyzero)work

•  Understandthehardwaretoolsavailableforirregularcontrolflow.–  I.e.,thingsotherthanabranchinarunningprogram

•  Buildingblocksforcontextswitching,devicemanagement,etc.

3

Page 4: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Background:ControlFlow

// x = 2, y = true if (y) {

2 /= x; printf(x);

} //...

void printf(va_args) {

//...

}

Regularcontrolflow:branchesandcalls(logicallyfollowssourcecode)

pc

4

Page 5: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Background:ControlFlow

// x = 0, y = true if (y) {

2 /= x; printf(x);

} //...

void handle_divzero(){

x = 2;

}

Irregularcontrolflow:excepUons,systemcalls,etc.

pc Dividebyzero!Programcan’tmake

progress!

5

Page 6: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Twotypesofinterrupts•  Synchronous:willhappeneveryUmeaninstrucUonexecutes(withagivenprogramstate)–  Dividebyzero–  Systemcall–  Badpointerdereference

•  Asynchronous:causedbyanexternalevent–  UsuallydeviceI/O–  TimerUcks(well,clockscanbeconsideredadevice)

6

Page 7: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

AsynchronousInterruptExample

User Kernel

Stack Stack

if (x) { printf(“Boo”); ...

printf(va_args…){

...

Disk_handler (){ ...

}

RSP

RIP

RSP

RIP

DiskInterrupt!

7

Page 8: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Intelnomenclature•  Interrupt–onlyreferstoasynchronousinterrupts•  ExcepUon–synchronouscontroltransfer

•  Note:fromtheprogrammer’sperspecUve,thesearehandledwiththesameabstracUons

8

Page 9: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Lectureoutline•  Overview•  Howinterruptsworkinhardware•  Howinterrupthandlersworkinsoeware•  Howsystemcallswork•  Newsystemcallhardwareonx86

9

Page 10: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Interruptoverview•  EachinterruptorexcepUonincludesanumberindicaUngitstype

•  E.g.,14isapagefault,3isadebugbreakpoint•  Thisnumberistheindexintoaninterrupttable

10

Page 11: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

x86interrupttable

0 255

31

… …

47

ReservedfortheCPU

SoewareConfigurable

DeviceIRQs 48=JOSSystemCall

128=LinuxSystemCall

11

Page 12: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

x86interruptoverview•  Eachtypeofinterruptisassignedanindexfrom0—255.

•  0—31areforprocessorinterrupts;generallyfixedbyIntel–  E.g.,14isalwaysforpagefaults

•  32—255aresoewareconfigured–  32—47arefordeviceinterrupts(IRQs)inJOS

•  Mostdevice’sIRQlinecanbeconfigured•  LookupAPICsformoreinfo(Ch4ofBovetandCesaU)

–  0x80issuessystemcallinLinux(moreonthislater)

12

Page 13: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Soewareinterrupts•  Theint <num> instrucUonallowssoewaretoraiseaninterrupt–  0x80isjustaLinuxconvenUon.JOSuses0x30.

•  Therearealotofspareindices–  YoucouldhavemulUplesystemcalltablesfordifferentpurposesortypesofprocesses!•  Windowsdoes:oneforthekernelandoneforwin32k

13

Page 14: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Soewareinterrupts,cont•  OSsetsringlevelrequiredtoraiseaninterrupt–  Generally,userprogramscan’tissueanint 14 (pagefault)manually

–  AnunauthorizedintinstrucUoncausesageneralprotecUonfault•  Interrupt13

14

Page 15: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Whathappens(highlevel):•  Controljumpstothekernel–  Ataprescribedaddress(theinterrupthandler)

•  Theregisterstateoftheprogramisdumpedonthekernel’sstack–  SomeUmes,extrainfoisloadedintoCPUregisters–  E.g.,pagefaultsstoretheaddressthatcausedthefaultinthecr2register

•  Kernelcoderunsandhandlestheinterrupt•  Whenhandlercompletes,resumeprogram(seeiretinstr.)

15

Page 16: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Howisthisconfigured?•  KernelcreatesanarrayofInterruptdescriptorsinmemory,calledInterruptDescriptorTable,orIDT–  Canbeanywhereinmemory–  Pointedtobyspecialregister(idtr)

•  c.f.,segmentregistersandgdtr andldtr

•  Entry0configuresinterrupt0,andsoon

16

Page 17: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

x86interrupttable

0 255

31

… …

47

idtr

LinearAddressofInterruptTable

17

Page 18: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

x86interrupttable

0 255

31

… …

47

idtr

Code Segment: Kernel Code Segment Offset: &page_fault_handler //linear addr Ring: 0 // kernel Present: 1 Gate Type: Exception

14

18

Page 19: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Summary•  Mostinterrupthandlinghardwarestatesetduringboot

•  EachinterrupthasanIDTentryspecifying:– Whatcodetoexecute,privilegeleveltoraisetheinterrupt

19

Page 20: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Lectureoutline•  Overview•  Howinterruptsworkinhardware•  HowinterrupthandlersworkinsoBware•  Howsystemcallswork•  Newsystemcallhardwareonx86

20

Page 21: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

High-levelgoal•  Respondtosomeevent,returncontroltotheappropriateprocess

•  Whattodoon:–  Networkpacketarrives–  DiskreadcompleUon–  Dividebyzero–  Systemcall

21

Page 22: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

InterruptHandlers•  Justplainoldkernelcode–  SortoflikeexcepUonhandlersinJava–  Butseparatedfromthecontrolflowoftheprogram

•  TheIDTstoresapointertotherighthandlerrouUne

22

Page 23: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Lectureoutline•  Overview•  Howinterruptsworkinhardware•  Howinterrupthandlersworkinsoeware•  Howsystemcallswork•  Newsystemcallhardwareonx86

23

Page 24: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Whatisasystemcall?•  AfuncUonprovidedtoapplicaUonsbytheOSkernel–  GenerallytouseahardwareabstracUon(file,socket)–  OrOS-providedsoewareabstracUon(IPC,scheduling)

•  WhynotputthesedirectlyintheapplicaUon?–  ProtecUonoftheOS/hardwarefrombuggy/maliciousprograms

–  ApplicaUonsarenotallowedtodirectlyinteractwithhardware,oraccesskerneldatastructures

Page 25: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Systemcall“interrupt”•  Originally,systemcallsissuedusingintinstrucUon•  DispatchrouUnewasjustaninterrupthandler•  Likeinterrupts,systemcallsarearrangedinatable–  Seearch/x86/kernel/syscall_table*.SinLinuxsource

•  Programselectstheoneitwantsbyplacingindexineaxregister–  ArgumentsgointheotherregistersbycallingconvenUon–  Returnvaluegoesineax

25

Page 26: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Howmanysystemcalls?•  Linuxexportsabout350systemcalls•  Windowsexportsabout400systemcallsforcoreAPIs,andanother800forGUImethods

Page 27: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Butwhyuseinterrupts?•  AlsoprotecUon•  ForcesapplicaUonstocallwell-defined“public”funcUons–  RatherthancallingarbitraryinternalkernelfuncUons

•  Example:publicfoo(){

if(!permission_ok())return–EPERM;return_foo();//nopermissioncheck

}

Calling_foo()directlywouldcircumvent

permissioncheck

Page 28: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Summary•  Systemcallsarethe“public”OSAPIs•  KernelleveragesinterruptstorestrictapplicaUonstospecificfuncUons

•  Lab1hint:HowtoissueaLinuxsystemcall?–  int $0x80,withsystemcallnumberineaxregister

Page 29: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Lectureoutline•  Overview•  Howinterruptsworkinhardware•  Howinterrupthandlersworkinsoeware•  Howsystemcallswork•  Newsystemcallhardwareonx86

29

Page 30: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

AroundP4era…•  Processorsgotverydeeplypipelined–  Pipelinestalls/flushesbecameveryexpensive–  Cachemissescancausepipelinestalls

•  SystemcallstooktwiceaslongfromP3toP4– Why?–  IDTentrymaynotbeinthecache–  DifferentpermissionsconstraininstrucUonreordering

30

Page 31: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Idea•  WhatifwecachetheIDTentryforasystemcallinaspecialCPUregister?–  NomorecachemissesfortheIDT!– MaybewecanalsodomoreopUmizaUons

•  AssumpUon:systemcallsarefrequentenoughtobeworththetransistorbudgettoimplementthis– Whatelsecouldyoudowithextratransistorsthathelpsperformance?

31

Page 32: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

AMD:syscall/sysret•  TheseinstrucUonsuseMSRs(machinespecificregisters)tostore:–  Syscallentrypointandcodesegment–  Kernelstack

•  Adrop-inreplacementforint 0x80•  Everyoneloveditandadopteditwholesale–  EvenIntel!

32

Page 33: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Aeermath•  Getpid()onmydesktopmachine(recentAMD6-core):–  Int80:371cycles–  Syscall:231cycles

•  Sosystemcallsaredefinitelyfasterasaresult!

33

Page 34: Interrupts and System Calls - Computer Science · • 0—31 are for processor interrupts; generally fixed by Intel – E.g., 14 is always for page faults • 32—255 are soeware

CSE306:Opera.ngSystems

Summary•  InterrupthandlersarespecifiedintheIDT•  Understandhowsystemcallsareexecuted– Whyinterrupts?– WhyspecialsystemcallinstrucUons?