Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CSE306:Opera.ngSystems
InterruptsandSystemCalls
DonPorterCSE306
1
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
CSE306:Opera.ngSystems
Lecturegoal• Understandhowsystemcallswork– AswellashowexcepUons(e.g.,dividebyzero)work
• Understandthehardwaretoolsavailableforirregularcontrolflow.– I.e.,thingsotherthanabranchinarunningprogram
• Buildingblocksforcontextswitching,devicemanagement,etc.
3
CSE306:Opera.ngSystems
Background:ControlFlow
// x = 2, y = true if (y) {
2 /= x; printf(x);
} //...
void printf(va_args) {
//...
}
Regularcontrolflow:branchesandcalls(logicallyfollowssourcecode)
pc
4
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
CSE306:Opera.ngSystems
Twotypesofinterrupts• Synchronous:willhappeneveryUmeaninstrucUonexecutes(withagivenprogramstate)– Dividebyzero– Systemcall– Badpointerdereference
• Asynchronous:causedbyanexternalevent– UsuallydeviceI/O– TimerUcks(well,clockscanbeconsideredadevice)
6
CSE306:Opera.ngSystems
AsynchronousInterruptExample
User Kernel
Stack Stack
if (x) { printf(“Boo”); ...
printf(va_args…){
...
Disk_handler (){ ...
}
RSP
RIP
RSP
RIP
DiskInterrupt!
7
CSE306:Opera.ngSystems
Intelnomenclature• Interrupt–onlyreferstoasynchronousinterrupts• ExcepUon–synchronouscontroltransfer
• Note:fromtheprogrammer’sperspecUve,thesearehandledwiththesameabstracUons
8
CSE306:Opera.ngSystems
Lectureoutline• Overview• Howinterruptsworkinhardware• Howinterrupthandlersworkinsoeware• Howsystemcallswork• Newsystemcallhardwareonx86
9
CSE306:Opera.ngSystems
Interruptoverview• EachinterruptorexcepUonincludesanumberindicaUngitstype
• E.g.,14isapagefault,3isadebugbreakpoint• Thisnumberistheindexintoaninterrupttable
10
CSE306:Opera.ngSystems
x86interrupttable
0 255
…
31
… …
47
ReservedfortheCPU
SoewareConfigurable
DeviceIRQs 48=JOSSystemCall
128=LinuxSystemCall
11
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
CSE306:Opera.ngSystems
Soewareinterrupts• Theint <num> instrucUonallowssoewaretoraiseaninterrupt– 0x80isjustaLinuxconvenUon.JOSuses0x30.
• Therearealotofspareindices– YoucouldhavemulUplesystemcalltablesfordifferentpurposesortypesofprocesses!• Windowsdoes:oneforthekernelandoneforwin32k
13
CSE306:Opera.ngSystems
Soewareinterrupts,cont• OSsetsringlevelrequiredtoraiseaninterrupt– Generally,userprogramscan’tissueanint 14 (pagefault)manually
– AnunauthorizedintinstrucUoncausesageneralprotecUonfault• Interrupt13
14
CSE306:Opera.ngSystems
Whathappens(highlevel):• Controljumpstothekernel– Ataprescribedaddress(theinterrupthandler)
• Theregisterstateoftheprogramisdumpedonthekernel’sstack– SomeUmes,extrainfoisloadedintoCPUregisters– E.g.,pagefaultsstoretheaddressthatcausedthefaultinthecr2register
• Kernelcoderunsandhandlestheinterrupt• Whenhandlercompletes,resumeprogram(seeiretinstr.)
15
CSE306:Opera.ngSystems
Howisthisconfigured?• KernelcreatesanarrayofInterruptdescriptorsinmemory,calledInterruptDescriptorTable,orIDT– Canbeanywhereinmemory– Pointedtobyspecialregister(idtr)
• c.f.,segmentregistersandgdtr andldtr
• Entry0configuresinterrupt0,andsoon
16
CSE306:Opera.ngSystems
x86interrupttable
0 255
…
31
… …
47
idtr
LinearAddressofInterruptTable
17
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
CSE306:Opera.ngSystems
Summary• Mostinterrupthandlinghardwarestatesetduringboot
• EachinterrupthasanIDTentryspecifying:– Whatcodetoexecute,privilegeleveltoraisetheinterrupt
19
CSE306:Opera.ngSystems
Lectureoutline• Overview• Howinterruptsworkinhardware• HowinterrupthandlersworkinsoBware• Howsystemcallswork• Newsystemcallhardwareonx86
20
CSE306:Opera.ngSystems
High-levelgoal• Respondtosomeevent,returncontroltotheappropriateprocess
• Whattodoon:– Networkpacketarrives– DiskreadcompleUon– Dividebyzero– Systemcall
21
CSE306:Opera.ngSystems
InterruptHandlers• Justplainoldkernelcode– SortoflikeexcepUonhandlersinJava– Butseparatedfromthecontrolflowoftheprogram
• TheIDTstoresapointertotherighthandlerrouUne
22
CSE306:Opera.ngSystems
Lectureoutline• Overview• Howinterruptsworkinhardware• Howinterrupthandlersworkinsoeware• Howsystemcallswork• Newsystemcallhardwareonx86
23
CSE306:Opera.ngSystems
Whatisasystemcall?• AfuncUonprovidedtoapplicaUonsbytheOSkernel– GenerallytouseahardwareabstracUon(file,socket)– OrOS-providedsoewareabstracUon(IPC,scheduling)
• WhynotputthesedirectlyintheapplicaUon?– ProtecUonoftheOS/hardwarefrombuggy/maliciousprograms
– ApplicaUonsarenotallowedtodirectlyinteractwithhardware,oraccesskerneldatastructures
CSE306:Opera.ngSystems
Systemcall“interrupt”• Originally,systemcallsissuedusingintinstrucUon• DispatchrouUnewasjustaninterrupthandler• Likeinterrupts,systemcallsarearrangedinatable– Seearch/x86/kernel/syscall_table*.SinLinuxsource
• Programselectstheoneitwantsbyplacingindexineaxregister– ArgumentsgointheotherregistersbycallingconvenUon– Returnvaluegoesineax
25
CSE306:Opera.ngSystems
Howmanysystemcalls?• Linuxexportsabout350systemcalls• Windowsexportsabout400systemcallsforcoreAPIs,andanother800forGUImethods
CSE306:Opera.ngSystems
Butwhyuseinterrupts?• AlsoprotecUon• ForcesapplicaUonstocallwell-defined“public”funcUons– RatherthancallingarbitraryinternalkernelfuncUons
• Example:publicfoo(){
if(!permission_ok())return–EPERM;return_foo();//nopermissioncheck
}
Calling_foo()directlywouldcircumvent
permissioncheck
CSE306:Opera.ngSystems
Summary• Systemcallsarethe“public”OSAPIs• KernelleveragesinterruptstorestrictapplicaUonstospecificfuncUons
• Lab1hint:HowtoissueaLinuxsystemcall?– int $0x80,withsystemcallnumberineaxregister
CSE306:Opera.ngSystems
Lectureoutline• Overview• Howinterruptsworkinhardware• Howinterrupthandlersworkinsoeware• Howsystemcallswork• Newsystemcallhardwareonx86
29
CSE306:Opera.ngSystems
AroundP4era…• Processorsgotverydeeplypipelined– Pipelinestalls/flushesbecameveryexpensive– Cachemissescancausepipelinestalls
• SystemcallstooktwiceaslongfromP3toP4– Why?– IDTentrymaynotbeinthecache– DifferentpermissionsconstraininstrucUonreordering
30
CSE306:Opera.ngSystems
Idea• WhatifwecachetheIDTentryforasystemcallinaspecialCPUregister?– NomorecachemissesfortheIDT!– MaybewecanalsodomoreopUmizaUons
• AssumpUon:systemcallsarefrequentenoughtobeworththetransistorbudgettoimplementthis– Whatelsecouldyoudowithextratransistorsthathelpsperformance?
31
CSE306:Opera.ngSystems
AMD:syscall/sysret• TheseinstrucUonsuseMSRs(machinespecificregisters)tostore:– Syscallentrypointandcodesegment– Kernelstack
• Adrop-inreplacementforint 0x80• Everyoneloveditandadopteditwholesale– EvenIntel!
32
CSE306:Opera.ngSystems
Aeermath• Getpid()onmydesktopmachine(recentAMD6-core):– Int80:371cycles– Syscall:231cycles
• Sosystemcallsaredefinitelyfasterasaresult!
33
CSE306:Opera.ngSystems
Summary• InterrupthandlersarespecifiedintheIDT• Understandhowsystemcallsareexecuted– Whyinterrupts?– WhyspecialsystemcallinstrucUons?