Skip to contentSkip to conversation

page table cleanups

patch/unstable Gerd Knorr 2 participants 3 messages Mar 14, 2005 x86

lore
Share & tools
lore

Gerd Knorr

author
#0 lore raw

Hi,

In many places xen uses "unsigned long" instead of the l*_pgentry_t types to pass around page table entries. Here is a patch which fixes this in a number of places (mostly in shadow mode code). Thats what I've trapped in so far, maybe more of these patches follow.

Fixing this is needed for adding PAE support to xen. In PAE paging mode the page table entries are 64 bit and thus will not fit into "unsigned long".

cheers,

Gerd

arch/x86/shadow.c +31 -25
Index: xen/arch/x86/shadow.c
===================================================================
@@ -532,13 +532,15 @@ static void shadow_map_l1_into_current_l
 { 
     struct exec_domain *ed = current;
     struct domain *d = ed->domain;
-    unsigned long    *gpl1e, *spl1e, gl2e, sl2e, gl1pfn, sl1mfn, sl1ss;
+    l1_pgentry_t     *gpl1e, *spl1e;
+    l2_pgentry_t     gl2e, sl2e;
+    unsigned long    gl1pfn, sl1mfn, sl1ss;
     struct pfn_info  *sl1mfn_info;
     int               i;
 
     __guest_get_l2e(ed, va, &gl2e);
 
-    gl1pfn = gl2e >> PAGE_SHIFT;
+    gl1pfn = l2_pgentry_val(gl2e) >> PAGE_SHIFT;
 
     sl1ss = __shadow_status(d, gl1pfn);
     if ( !(sl1ss & PSH_shadowed) )
@@ -561,10 +563,10 @@ static void shadow_map_l1_into_current_l
         __guest_set_l2e(ed, va, gl2e);
         __shadow_set_l2e(ed, va, sl2e);
 
-        gpl1e = (unsigned long *) &(linear_pg_table[
+        gpl1e = &(linear_pg_table[
             (va>>L1_PAGETABLE_SHIFT) & ~(L1_PAGETABLE_ENTRIES-1)]);
 
-        spl1e = (unsigned long *) &(shadow_linear_pg_table[
+        spl1e = &(shadow_linear_pg_table[
             (va>>L1_PAGETABLE_SHIFT) & ~(L1_PAGETABLE_ENTRIES-1)]);
 
         for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
@@ -584,7 +586,8 @@ static void shadow_map_l1_into_current_l
 
 void shadow_invlpg(struct exec_domain *ed, unsigned long va)
 {
-    unsigned long gpte, spte;
+    l1_pgentry_t gpte, spte;
+    l1_pgentry_t zero = mk_l1_pgentry(0);
 
     ASSERT(shadow_mode_enabled(ed->domain));
 
@@ -592,21 +595,22 @@ void shadow_invlpg(struct exec_domain *e
      * XXX KAF: Why is this set-to-zero required?
      *          Why, on failure, must we bin all our shadow state?
      */
-    if (__put_user(0L, (unsigned long *)
-                   &shadow_linear_pg_table[va >> PAGE_SHIFT])) {
+    if (__copy_to_user(&shadow_linear_pg_table[va >> PAGE_SHIFT],
+		       &zero, sizeof(zero))) {
         vmx_shadow_clear_state(ed->domain);
         return;
     }
 
-    if (__get_user(gpte, (unsigned long *)
-                   &linear_pg_table[va >> PAGE_SHIFT])) {
+    if (__copy_from_user(&gpte,
+			 &linear_pg_table[va >> PAGE_SHIFT],
+			 sizeof(gpte))) {
         return;
     }
 
     l1pte_propagate_from_guest(ed->domain, &gpte, &spte);
 
-    if (__put_user(spte, (unsigned long *)
-                   &shadow_linear_pg_table[va >> PAGE_SHIFT])) {
+    if (__copy_to_user(&shadow_linear_pg_table[va >> PAGE_SHIFT],
+		       &spte, sizeof(spte))) {
         return;
     }
 }
@@ -715,17 +719,18 @@ int shadow_fault(unsigned long va, struc
 
 
 void shadow_l1_normal_pt_update(
-    unsigned long pa, unsigned long gpte,
+    unsigned long pa, l1_pgentry_t gpte,
     unsigned long *prev_smfn_ptr,
     l1_pgentry_t **prev_spl1e_ptr)
 {
-    unsigned long smfn, spte, prev_smfn = *prev_smfn_ptr;    
+    l1_pgentry_t spte;
+    unsigned long smfn, prev_smfn = *prev_smfn_ptr;    
     l1_pgentry_t *spl1e, *prev_spl1e = *prev_spl1e_ptr;
 
     /* N.B. To get here, we know the l1 page *must* be shadowed. */
     SH_VVLOG("shadow_l1_normal_pt_update pa=%p, gpte=%p, "
              "prev_smfn=%p, prev_spl1e=%p",
-             pa, gpte, prev_smfn, prev_spl1e);
+             pa, l1_pgentry_val(gpte), prev_smfn, prev_spl1e);
 
     smfn = __shadow_status(current->domain, pa >> PAGE_SHIFT) & PSH_pfn_mask;
 
@@ -737,23 +742,24 @@ void shadow_l1_normal_pt_update(
     {
         if ( prev_spl1e != NULL )
             unmap_domain_mem( prev_spl1e );
-        spl1e = (l1_pgentry_t *)map_domain_mem(smfn << PAGE_SHIFT);
+        spl1e = map_domain_mem(smfn << PAGE_SHIFT);
         *prev_smfn_ptr  = smfn;
         *prev_spl1e_ptr = spl1e;
     }
 
     l1pte_propagate_from_guest(current->domain, &gpte, &spte);
-    spl1e[(pa & ~PAGE_MASK) / sizeof(l1_pgentry_t)] = mk_l1_pgentry(spte);
+    spl1e[(pa & ~PAGE_MASK) / sizeof(l1_pgentry_t)] = spte;
 }
 
-void shadow_l2_normal_pt_update(unsigned long pa, unsigned long gpde)
+void shadow_l2_normal_pt_update(unsigned long pa, l2_pgentry_t gpde)
 {
-    unsigned long sl2mfn, spde = 0;
-    l2_pgentry_t *spl2e;
+    unsigned long sl2mfn;
+    l2_pgentry_t *spl2e, spde = mk_l2_pgentry(0);
     unsigned long sl1mfn;
 
     /* N.B. To get here, we know the l2 page *must* be shadowed. */
-    SH_VVLOG("shadow_l2_normal_pt_update pa=%p, gpde=%p",pa,gpde);
+    SH_VVLOG("shadow_l2_normal_pt_update pa=%p, gpde=%p",pa,
+	     l1_pgentry_t(gpde));
 
     sl2mfn = __shadow_status(current->domain, pa >> PAGE_SHIFT) & PSH_pfn_mask;
 
@@ -761,15 +767,15 @@ void shadow_l2_normal_pt_update(unsigned
      * Only propagate to shadow if _PAGE_ACCESSED is set in the guest.
      * Otherwise, to ensure coherency, we blow away the existing shadow value.
      */
-    if ( gpde & _PAGE_ACCESSED )
+    if ( l2_pgentry_val(gpde) & _PAGE_ACCESSED )
     {
-        sl1mfn = (gpde & _PAGE_PRESENT) ?
-            __shadow_status(current->domain, gpde >> PAGE_SHIFT) : 0;
+        sl1mfn = (l2_pgentry_val(gpde) & _PAGE_PRESENT) ?
+            __shadow_status(current->domain, l2_pgentry_val(gpde) >> PAGE_SHIFT) : 0;
         l2pde_general(current->domain, &gpde, &spde, sl1mfn);
     }
 
-    spl2e = (l2_pgentry_t *)map_domain_mem(sl2mfn << PAGE_SHIFT);
-    spl2e[(pa & ~PAGE_MASK) / sizeof(l2_pgentry_t)] = mk_l2_pgentry(spde);
+    spl2e = map_domain_mem(sl2mfn << PAGE_SHIFT);
+    spl2e[(pa & ~PAGE_MASK) / sizeof(l2_pgentry_t)] = spde;
     unmap_domain_mem(spl2e);
 }
 
include/asm-x86/shadow.h +55 -51

221 lines not on this page · raw message

arch/x86/vmx.c +4 -3
Index: xen/arch/x86/vmx.c
===================================================================
@@ -107,7 +107,8 @@ static void inline __update_guest_eip(un
 static int vmx_do_page_fault(unsigned long va, struct xen_regs *regs) 
 {
     unsigned long eip;
-    unsigned long gpte, gpa;
+    l1_pgentry_t gpte;
+    unsigned long gpa; /* FIXME: PAE */
     int result;
 
 #if VMX_DEBUG
@@ -130,9 +131,9 @@ static int vmx_do_page_fault(unsigned lo
     }
 
     gpte = gva_to_gpte(va);
-    if (!(gpte & _PAGE_PRESENT) )
+    if (!(l1_pgentry_val(gpte) & _PAGE_PRESENT) )
             return 0;
-    gpa = (gpte & PAGE_MASK) + (va & ~PAGE_MASK);
+    gpa = (l1_pgentry_val(gpte) & PAGE_MASK) + (va & ~PAGE_MASK);
 
     /* Use 1:1 page table to identify MMIO address space */
     if (mmio_space(gpa))
arch/x86/mm.c +35 -35

191 lines not on this page · raw message

common/grant_table.c +4 -5
Index: xen/common/grant_table.c
===================================================================
@@ -281,12 +281,11 @@ __gnttab_map_grant_ref(
     {
         /* Write update into the pagetable
          */
+	l1_pgentry_t pte = mk_l1_pgentry((frame << PAGE_SHIFT)
+		| _PAGE_PRESENT  | _PAGE_ACCESSED | _PAGE_DIRTY
+		| ((flags & GNTMAP_readonly) ? 0 : _PAGE_RW));
         if ( 0 > (rc = update_grant_va_mapping( host_virt_addr,
-                                (frame << PAGE_SHIFT) | _PAGE_PRESENT  |
-                                                        _PAGE_ACCESSED |
-                                                        _PAGE_DIRTY    |
-                       ((flags & GNTMAP_readonly) ? 0 : _PAGE_RW),
-                       ld, led )) )
+			pte, ld, led )) )
         {
             /* Abort. */
             act->pin -= (flags & GNTMAP_readonly) ?
include/asm/mm.h +1 -1
Index: xen/include/asm/mm.h
===================================================================
@@ -340,7 +340,7 @@ void propagate_page_fault(unsigned long 
  * Caller must own d's BIGLOCK, is responsible for flushing the TLB,
  * and have already get_page'd */
 int update_grant_va_mapping(unsigned long va,
-                            unsigned long val,
+                            l1_pgentry_t  val,
                             struct domain *d,
                             struct exec_domain *ed);
 #define GNTUPDVA_prev_ro 1
arch/x86/vmx_platform.c +2 -2
Index: xen/arch/x86/vmx_platform.c
===================================================================
@@ -367,7 +367,7 @@ static int vmx_decode(const unsigned cha
 
 static int inst_copy_from_guest(unsigned char *buf, unsigned long guest_eip, int inst_len)
 {
-    unsigned long gpte;
+    l1_pgentry_t gpte;
     unsigned long mfn;
     unsigned long ma;
     unsigned char * inst_start;
@@ -378,7 +378,7 @@ static int inst_copy_from_guest(unsigned
 
     if ((guest_eip & PAGE_MASK) == ((guest_eip + inst_len) & PAGE_MASK)) {
         gpte = gva_to_gpte(guest_eip);
-        mfn = phys_to_machine_mapping(gpte >> PAGE_SHIFT);
+        mfn = phys_to_machine_mapping(l1_pgentry_to_pfn(gpte));
         ma = (mfn << PAGE_SHIFT) | (guest_eip & (PAGE_SIZE - 1));
         inst_start = (unsigned char *)map_domain_mem(ma);
                 

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

#1 lore raw
Quoted 8 lines · Gerd Knorr

In many places xen uses "unsigned long" instead of the l*_pgentry_t types to pass around page table entries. Here is a patch which fixes this in a number of places (mostly in shadow mode code). Thats what I've trapped in so far, maybe more of these patches follow.

Fixing this is needed for adding PAE support to xen. In PAE paging mode the page table entries are 64 bit and thus will not fit into "unsigned long".

This is certainly one way to go. The other is to change those unsigned longs into a physaddr_t which is u64 on 32-bit PAE systems. I'm not fussed too much which way we go, but Ian has pointed out that the l*_pgentry_t types haven't actually found us any bugs (although I might argue that it has prevented any bugs ever getting as far as the master repository :-) ).

We'll need a physaddr_t (or machaddr_t?) in some places in any case -- as a parameter to map_domain_mem(), for example.

-- Keir

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

author
#2 lore raw

Hi,

Quoted 2 lines

The other is to change those unsigned longs into a physaddr_t which is u64 on 32-bit PAE systems.

I've considered doing something like that as well, but I don't think that this is a good idea.

Quoted 4 lines

I'm not fussed too much which way we go, but Ian has pointed out that the l*_pgentry_t types haven't actually found us any bugs (although I might argue that it has prevented any bugs ever getting as far as the master repository :-) ).

I'd argue that way as well. The whole point of the "typedef { u{32|64} l? } l?_pgentry_t" types and the access macros is to make the _compiler_ notice bugs, so they can't creep into the code base in the first place because gcc refuses to compile the buggy code ;)

cheers,

Gerd

Signature · 8 lines
#define printk(args...) fprintf(stderr, ## args)


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click