From: Jui-Hao Chiang Subject: Re: Consult some concepts about shadow paging mechanism Date: Sun, 3 May 2009 09:39:26 -0400 Message-ID: <52cf60ee0905030639y6138427fi166d8abdb3f2b17d@mail.gmail.com> References: <52cf60ee0904220614m343aa6c2v13c244fc878825f7@mail.gmail.com> <52cf60ee0904232123n6e38711fjbd8fe084c464b059@mail.gmail.com> <52cf60ee0905011947v5a7ca8a2k1bee8e9bd9e2c763@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <52cf60ee0905011947v5a7ca8a2k1bee8e9bd9e2c763@mail.gmail.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Gianluca Guida Cc: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org I got the answer because I made a mistake to pass four sl2mfn entries in v->arch.paging.shadow.l3table[] to sh_walk_l2_table(). Then truth is I only need to pass v->arch.paging.shadow.l3table[0] because SHADOW_FOREACH_L2E has already done a good job on looping the four sl2mfns. But I have another doubt in traversing SPT from level 3, level 2, and level= 1. When I am traversing down to the level 1 SPT, I found several inconsistency between gl1e and sl1e content, which is the same as the mechanism in sh_audit_l1_table(). Is this a normal case? I thought they should keep consistent at all times. My purpose is to walk down the SPT and GPT during each process context switch (sh_update_cr3), and do some statistics first, e.g. dirty, access, present bit. Now I tried another checking in level 2 SPT by skipping those sl1mfn which does not pass sh_mfn_is_a_page_table(sl1mfn) check, then the inconsistency is gone is level 1 SPT traversing. Can anyone show some hint about how to do the right thing? Is there some special type of SPTE that I should not traverse down? Many thanks, Jui-Hao On Fri, May 1, 2009 at 10:47 PM, Jui-Hao Chiang w= rote: > Hi, sorry for disturbing you guys again. > > Assume guest's paging level is 2 and shadow is using level 3 PAE. > I am now trying to dump the L2 shadow page table information in the > beginning of sh_update_cr3() as the following (actually copying the > code from sh_audit_l2_table and audit_gfn_to_mfn functions) > > The code accidentally crashes in =A0guest_l2e_get_flags(*gl2e) of the > sh_walk_l2_table I wrote. > However, the weird part is the code doesn't crash in gfn =3D > guest_l2e_get_gfn(*gl2e) which is accessing the *gl2e in a similar way > as guest_l2e_get_flags. > > static inline mfn_t > convert_gfn_to_mfn(struct vcpu *v, gfn_t gfn, mfn_t gmfn) > { > =A0 =A0p2m_type_t p2mt; > =A0 =A0if ( !shadow_mode_translate(v->domain) ) > =A0 =A0 =A0 =A0return _mfn(gfn_x(gfn)); > > =A0 =A0if ( (mfn_to_page(gmfn)->u.inuse.type_info & PGT_type_mask) > =A0 =A0 =A0 =A0 !=3D PGT_writable_page ) > =A0 =A0 =A0 =A0return _mfn(gfn_x(gfn)); // This is a paging-disabled shad= ow > =A0 =A0else > =A0 =A0 =A0 =A0return gfn_to_mfn(v->domain, gfn, &p2mt); > } > > /* JuiHao: walk the l2 shadow page table based on input sl2mfn */ > static int sh_walk_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x) > { > =A0 =A0 =A0 =A0guest_l2e_t *gl2e, *gp; > =A0 =A0 =A0 =A0shadow_l2e_t *sl2e; > =A0 =A0 =A0 =A0mfn_t sl1mfn, gl2mfn; > =A0 =A0 =A0 =A0gfn_t gfn; > =A0 =A0 =A0 =A0mfn_t gmfn; > =A0 =A0 =A0 =A0int done =3D 0; > > =A0 =A0 =A0 =A0/* Follow the backpointer in struct shadow_page_info to ge= t guest l2mfn */ > =A0 =A0 =A0 =A0gl2mfn =3D _mfn(mfn_to_shadow_page(sl2mfn)->backpointer); > =A0 =A0 =A0 =A0gl2e =3D gp =3D sh_map_domain_page(gl2mfn); > > =A0 =A0 =A0 =A0SHADOW_FOREACH_L2E(sl2mfn, sl2e, &gl2e, done, v->domain, { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0gfn =3D guest_l2e_get_gfn(*gl2e); =A0// ##= #!!!! Works Fine !!!!!#### > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0sl1mfn =3D shadow_l2e_get_mfn(*sl2e); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (mfn_valid(sl1mfn) && (shadow_l2e_get_f= lags(*sl2e) & _PAGE_PRESENT)) { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// We get this gmfn is jus= t to double check if this is equal to sl1mfn > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0gmfn =3D (guest_l2e_get_fl= ags(*gl2e) & _PAGE_PSE) // ###!!!! CRASH !!!!!#### > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0? get_fl1_= shadow_status(v, gfn) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0: get_shad= ow_status(v, convert_gfn_to_mfn(v, gfn, gl2mfn), > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SH_type_l1= _shadow); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (mfn_x(gmfn) !=3D mfn_x= (sl1mfn)) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0printk("!!= gmfn %" PRI_mfn " !=3D sl1mfn %" PRI_mfn "\n", gmfn, sl1mfn); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} else { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0printk("go= ing down to traverse level 1 SPT\n"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > > =A0 =A0 =A0 =A0}); > =A0 =A0 =A0 =A0sh_unmap_domain_page(gp); > =A0 =A0 =A0 =A0return 0; > } > > Could you help a little bit on this? > Many thanks, > Jui-Hao > > On Fri, Apr 24, 2009 at 9:32 AM, Gianluca Guida > wrote: >> On Fri, Apr 24, 2009 at 6:23 AM, Jui-Hao Chiang wrote: >>> I have some additional doubts as the following: >>> (1) For normal data page, in order to propagate the Dirty or Access >>> bit from SPTE to GPTE, the hypervisor needs to set Read-Only in the >>> SPTE. When the write page fault of this data page comes, hypervisor >>> can propagate the Dirty or Access bit to GPTE and set it to R/W. My >>> question is when does the hypervisor make it Read-Only again? Is there >>> any place inside the source code you can point out? >> >> What happens is this: the guest has to clear the dirty/accessed bit >> and then flush the tlb (or invlpg the entry). >> If the pagetable is mapped read only (as in levels > 1) the write to >> the pagetable will trigger the emulator that will update the entry. >> Otherwhise (if the page is out of sync, which means a writable guest >> pagetable, and this happens when it's an L1) the flushtlb will do the >> job of updating the shadow entry. >> >> Look at how sh_propagate function works and when it get called. It's >> what you're looking for. >> >>> (2) How many shadow pages are maintained for each guest domain? If the >>> hypervisor keep only one shadow page table for the active process in >>> each guest domain, then during the guest context-switch, it might >>> erase the entire shadow page table, and re-construct it for the new >>> process, which seems a lot of overhead. I have checked the >>> sh_update_cr3(), but not sure of the detailed mechanism. >> >> There's a pool of shadow memory that get reused in a pseudo-LRU >> manner. Across cr3 switch toplevel pagetables are kept in memory, and >> unshadowed when evicted by the allocator or when other things happens, >> mostly based on heuristic and reference counting. >> >> Thanks, >> Gianluca >> >> -- >> It was a type of people I did not know, I found them very strange and >> they did not inspire confidence at all. Later I learned that I had been >> introduced to electronic engineers. >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0E. W. Dijkstra >> >