1500012703LabkernelLabkernelsystemcallexceptionLabenvironmentprocessenvironmentJOSenvironmentUNIXprocessKernelEnvLabLabLab4kern/env.cenvs,curenv,env_free_listEnvpagespage_free_listNENVEnvinc/env.hstructEnv{structTrapframeenv_tf;//SavedregistersstructEnv*env_link;//NextfreeEnvenvid_tenv_id;//Uniqueenvironmentidentifierenvid_tenv_parent_id;//env_idofthisenv'sparentenumEnvTypeenv_type;//Indicatesspecialsystemenvironmentsunsignedenv_status;//Statusoftheenvironmentuint32_tenv_runs;//Numberoftimesenvironmenthasrun//Addressspacepde_t*env_pgdir;//Kernelvirtualaddressofpagedir};Lab3IntroductionPartA:UserEnvironmentsandExceptionHandlingEnvironmentStateenv_tf:Trapframeinc/trap.hstructTrapframe{structPushRegstf_regs;uint16_ttf_es;uint16_ttf_padding1;uint16_ttf_ds;uint16_ttf_padding2;uint32_ttf_trapno;/*belowheredefinedbyx86hardware*/uint32_ttf_err;uintptr_ttf_eip;uint16_ttf_cs;uint16_ttf_padding3;uint32_ttf_eflags;/*belowhereonlywhencrossingrings,suchasfromusertokernel*/uintptr_ttf_esp;uint16_ttf_ss;uint16_ttf_padding4;}__attribute__((packed));Trapframees,dstrapnotrapenv_link:env_id:idEnvenv_parent_id:id'env_type:ENV_TYPE_USERenv_status:1.ENV_FREE:2.ENV_RUNNABLE:3.ENV_RUNNING:4.ENV_NOT_RUNNABLE:5.ENV_DYING:env_pgdir:UnixJOSthreadaddressspacethreadenv_tfaddressspaceenv_pgdirxv6JOSkernelExerciseenvsLab2UENVSLabenvload_icode.o(ELF)kerneluserenv_pop_tfeipELFentryiretkerneluserExerciseenv_init()pagesenv_free_listEnvenv_setup_vm()envpageenvrefpagekern_pgdirkernelkernelregion_alloc()lene-env_pgdirvaPGSIZEpage_insertenvuserPTE_UPTE_W1load_icode()envELFboot\main.cbootmainenvmemset,memcpykern_pgdirenvenv_pgdirlcr3(PADDR(e-env_pgdir))staticinlinevoidlcr3(uint32_tval){asmvolatile(movl%0,%%cr3::r(val));}AllocatingtheEnvironmentsArrayExercise1.CreatingandRunningEnvironmentsExercise2.valcr3cr3lcr3env_pgdirenv_create()env_allocload_icodeenvELFenv_run()eIf1.2.,3.,...Iftriplefaulthandoutkern\init.cENV_CREATE.oenv_pop_tfkerneluserenv_pop_tfeipELFentryiretkerneluserChapterIDTTSSgateTaskgatesInterruptgatesTrapgatesHandlingInterruptsandExceptionsExercise3.gate64callgateStackOverfloweflagsTFIFTF1debugIF(maskableinterruptrequests)1gategateDPLCPLRPLgateCPL=DPLandRPL=DPLDespitethefactthatallgatesaresimilar,theyhavesomedifferences,becauseoriginallyIntelengineersthoughtthatdifferentgateswillbeusedfordifferentpurposes.Taskgates:IDTGDTintgate3IntelTSS(TaskStateSegment)TSS:EIPCSTSSx86kernelSS,ESP,EFLAGS,CS,EIPCSEIPESPSS0TSSESP0SS0kernelTSStaskgatecalljmptasktaskgateTSSTSSGDTTrapgates:IDTintgatetaskgateInterruptiongates:IDTinttrapgateinterruptiongateIFCallgates:GDTLGTinttrapgateInterruptDescriptorTable(IDT):kernelx862560255CPUIDTEIPBasicsofProtectedControlTransferCS0-1JOS0gateTaskStateSegment(TSS):03132~255intLab0-310x30Lab4JOSTSSSS0ESP0JOSGD_KD(kerneldata)KSTACKTOPkernelTypesofExceptionsandInterruptsAnExamplekernel+--------------------+KSTACKTOP|0x00000|oldSS|-4|oldESP|-8|oldEFLAGS|-12|0x00000|oldCS|-16|oldEIP|-20----ESP+--------------------+0IDT0entryCS:EIPerrorcodeerrorcode+--------------------+KSTACKTOP|0x00000|oldSS|-4|oldESP|-8|oldEFLAGS|-12|0x00000|oldCS|-16|oldEIP|-20|errorcode|-24----ESP+--------------------+errorcodekernel0errorcodeNestedExceptionsandInterrupts(CS20)CPUNestedExceptionsandInterruptskernelSSESPerrorcode+--------------------+----oldESP|oldEFLAGS|-4|0x00000|oldCS|-8|oldEIP|-12+--------------------+errorcodeEIPerrorcodeIDTIDTtrapentry.Strap.c+----------------+|&handler1|---------handler1:trap(structTrapframe*tf)||//dostuff{||calltrap//handletheexception/interrupt||//...}+----------------+|&handler2|--------handler2:||//dostuff||calltrap||//...+----------------+...+----------------+|&handlerX|--------handlerX:||//dostuff||calltrap||//...+----------------+IDTGatedescidt[256]inc\mmu.hGatedescGateSettingUptheIDT//GatedescriptorsforinterruptsandtrapsstructGatedesc{unsignedgd_off_15_0:16;//low16bitsofoffsetinsegmentunsignedgd_sel:16;//segmentselectorunsignedgd_args:5;//#args,0forinterrupt/trapgatesunsignedgd_rsv1:3;//reserved(shouldbezeroIguess)unsignedgd_type:4;//type(STS_{TG,IG32,TG32})unsignedgd_s:1;//mustbe0(system)unsignedgd_dpl:2;//descriptor(meaningnew)privilegelevelunsignedgd_p:1;//Presentunsignedgd_off_31_16:16;//highbitsofoffsetinsegment};kern\trapentry.Shandler.text.datatrap.c.datakernel\trap.cSETGATEIDThandlertrapentry.Strap.ctrapentry.S:TRAPHANDLERTRAPHANDLER_NOECerrorcodeTRAPHANDLER_NOEC0TRAPHANDLER.text.text3trapno_alltrapsTrapframetrapnodses:tf_padding2,tf_ds,tf_padding1,tf_es,tf_regspushalkernel.datadsesmovw$GD_KD,%dsGNU%eaxmovw%ax,%dsTRAPHANDLERTRAPHANDLER_NOECerrorcodenamehandlertrap.chandlerSETGATEinc\mmu.histrapExercise4.trapselkernel.textGD_KToffdplgatebreakpointgate0x30#defineSETGATE(gate,istrap,sel,off,dpl)\{\(gate).gd_off_15_0=(uint32_t)(off)&0xffff;\(gate).gd_sel=(sel);\(gate).gd_args=0;\(gate).gd_rsv1=0;\(gate).gd_type=(istrap)?STS_TG32:STS_IG32;\(gate).gd_s=0;\(gate).gd_dpl=(dpl);\(gate).gd_p=1;\(gate).gd_off_31_16=(uint32_t)(off)16;\}1.Whatisthepurposeofhavinganindividualhandlerfunctionforeachexc