Traps, Exceptions, System Calls, & Privileged Mode!Hakim&Weatherspoon&CS&3410,&Spring&2011&Computer)Science)Cornell)University)P&H)Chapter)4.9,)pages)509–515,)appendix)B.7)2)OperaGng)Systems)3)Control Transfers!Control)Transfers)to)OS)Case)1:)Program)invokes)OS)• eg:)sbrk(),(mmap(),(sleep()(• Like)a)funcGon)call:)invoke,)do)stuff,)return)results(APempt)#1:)OS)as)a)library)• Just)a)funcGon)call:))JAL)sbrk)• Standard)calling)convenGons)4)VM Hardware/Software Boundary!Virtual)to)physical)address)translaGon)Hardware)(typical):)• Traverse)PageTables)on)TLB)miss,)install)TLB)entries)• Update)dirty)bit)in)PTE)when)evicGng)• Flush)when)PTBR)changes)So[ware)(typical):)• Decide)when)to)do)context)switches,)update)PTBR)• Decide)when)to)add,)remove,)modify)PTEs)and)PDEs)– and)invoke)MMU)to)invalidate)TLB)entries)• Handle)page)faults:)swap)to/from)disk,)kill)processes)Hardware)(minimal):)• NoGfy)OS)on)TLB)miss;)so[ware)does)everything)else)5)Control Transfers!Control)Transfers)to)OS)Case)1:)Program)invokes)OS)• eg:)sbrk(),(mmap(),(sleep()(• Like)a)funcGon)call:)invoke,)do)stuff,)return)results)(Case)2:)Hardware)invokes)OS)on)behalf)of)program)• Page)fault,)divide)by)zero,)arithmeGc)overflow,)…)• OS)takes)correcGve)acGon;)then)restarts/kills)program))Can)CPU)simply)fake)this:)a0)=)cause)JAL)excepGon_handler)6)Attempt #2:!APempt)#2:)OS)as)a)library)+)ExcepGon)Handler)Program)invokes)OS:)regular)calling)convenGon)HW)invokes)OS:))• New)registers:)EPC,)Cause,)Vector*,)…)• On)excepGon,)CPU)does…))EPC)PC))Cause))error/reason)code))PC))Vector)• Code)at)Vector)does…))take)correcGve)acGon)based)on)Cause))return)to)EPC)*)x86:)via)IDTR)register)and)IDT;)MIPS)used)a)constant)7)Sketch of Exception Handler!#)MIPS)excepGon)vector)is)0x80000180).ktext)0x80000180)#)EPC)has)offending)PC,)Cause)has)errcode)#)(step)1))save)*everything*)but)$k0,)$k1)lui)$k0,)0xB000)sw)$1,)0($k0))sw)$2,)4($k0))sw)$3,)8($k0))sw)$4,)12($k0))…)sw)$31,)120($k0))mflo)$1)sw)$1,)124($k0))mmi)$1)sw)$1,)128($k0))…)*)approximate)8)Sketch of Exception Handler!#)MIPS)excepGon)vector)is)0x80000180).ktext)0x80000180)#)EPC)has)offending)PC,)Cause)has)errcode)#)(step)1))save)*everything*)but)$k0,)$k1)#)(step)2))set)up)a)usable)OS)context)li)$sp,)0xFFFFFF00)li)$fp,)0xFFFFFFFF)li)$gp,)…)*)approximate)9)Sketch of Exception Handler!#)MIPS)excepGon)vector)is)0x80000180).ktext)0x80000180)#)EPC)has)offending)PC,)Cause)has)errcode)#)(step)1))save)*everything*)but)$k0,)$k1)#)(step)2))set)up)a)usable)OS)context)#)(step)3))examine)Cause)register,)and)take)correcGve)acGon)mfc0)$t0,)Cause)#)movenfromncoprocessorn0)if)($t0)==)PAGE_FAULT)){)))mfc0)$a0,)BadVAddr)#)another)dedicated)register)))jal)kernel_handle_pagefault)})else)if)($t0)==)PROTECTION_FAULT)){)))…)})else)if)($t0)==)DIV_BY_ZERO)){)))…)}))*)approximate)10)Sketch of Exception Handler!#)MIPS)excepGon)vector)is)0x80000180).ktext)0x80000180)#)EPC)has)offending)PC,)Cause)has)errcode)#)(step)1))save)*everything*)but)$k0,)$k1)#)(step)2))set)up)a)usable)OS)context)#)(step)3))examine)Cause)register,)and)take)correcGve)acGon)#)(step)4))restore)registers)and)return)to)where)program)le[)off)lui)$k0,)0xB000)lw)$1,)0($k0))lw)$2,)4($k0))lw)$3,)8($k0))…)lw)$31,)120($k0))…)mfc0)$k1,)EPC)jr)$k1))*)approximate)11)Hardware/Software Boundary!Hardware)Support:)• registers:)EPC,)Cause,)Vector,)BadVAddr,)…)• instrucGons:)mfc0,)TLB)flush/invalidate,)cache)flush,)…)Hardware)guarantees)for)precise)excepGons:)• EPC)points)at)offending)instrucGon)• Earlier)instrucGons)are)finished)• EPC)and)later))instrucGons)have)not)started)• Returning)to)EPC)will)pick)up)where)we)le[)off)12)Double Faults, Triple Faults!• EPC)points)at)offending)inst)• Earlier)inst)are)finished;)EPC)and)later)inst)not)started)• Returning)to)EPC)will)pick)up)where)we)le[)off)What)could)possibly)go)wrong?)ExcepGon)happens)during)excepGon)handler…)original)EPC)and)Cause)are)lost)• Disable)excepGons)unGl)current)excepGon)is)resolved?)– MIPS:)Status)register)has)a)bit)for)enable/disable)– turn)excepGons)back)on)just)when)returning)to)EPC)– works)for)issues)that)can)be)(temporarily))ignored)• Use)a)“double)fault”)excepGon)handler)for)rest)– BSOD)• And)if)that)faults?)Triple)fault))instant)shutdown )13)Precise Exceptions!• EPC)points)at)offending)inst)• Earlier)inst)are)finished;)EPC)and)later)inst)not)started)• Returning)to)EPC)will)pick)up)where)we)le[)off)What)could)possibly)go)wrong?)MulGple)simultaneous)excepGons)in)pipeline)lw)$4,)0($0))#)page)fault)xxx)$4,)$5,)$5)#)illegal)instrucGon)add)$2,)$2,)$3)#)overflow)• need)stalls)to)let)earlier)inst)raise)excepGon)first)• even)worse)with)speculaGve)/)“outnofnorder”)execuGon)14)Precise Exceptions!• EPC)points)at)offending)inst)• Earlier)inst)are)finished;)EPC)and)later)inst)not)started)• Returning)to)EPC)will)pick)up)where)we)le[)off)What)could)possibly)go)wrong?)ExcepGon)happened)in)delay)slot)jal)prints)lw)$4,)0($0))#)page)fault)…)• need)more)than)just)EPC)to)idenGfy)“where)we)le[)off”)15)Precise Exceptions!• EPC)points)at)offending)inst)• Earlier)inst)are)finished;)EPC)and)later)inst)not)started)• Returning)to)EPC)will)pick)up)where)we)le[)off)What)could)possibly)go)wrong?)InstrucGons)with)mulGple)faults)or)side)effects)storenandnupdatenregister)memoryntonmemoryncopy)memorynfill,)x86)“string”)prefix,)x86)“loop”)prefix)• need)more)than)just)EPC)to)idenGfy)“where)we)le[)off”)• or:)try)to)undo)effects)that)have)already)happened)• or:)have)so[ware)try)to)finish)the)parGally)finished)EPC)• or:)all)of)the)above)16)“The)interacGon)between)branch)delay)slots)and)excepGon)handling)is)extremely)unpleasant)and)you'll)be)happier)if)you)don't)think)about)it.” )–)MaP)Welch)17)Attempt #2: Recap!APempt)#2:)Recap)Program)invokes)OS)• regular)calling)convenGon)HW)invokes)OS:))• precise)excepGons)vector)to)OS)excepGon)handler))Drawbacks?)18)Attempt #2 is broken!Drawbacks:)• Any)program)can)muck)with)TLB,)PageTables,)OS)code…)• A)program)can)intercept)excepGons)of)other)programs)• OS)can)crash)if)program)messes)up)$sp,)$fp,)$gp,)…))Wrong:)Make)these)instrucGons)and)registers)available)only)to)“OS)Code”)•
View Full Document