Skip to contentSkip to conversation

minios: don't rely on specific page table allocation scheme

PATCH Superseded Juergen Gross 2 participants 4 messages Nov 19, 2015 – Nov 20, 2015 x86

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 minios: don't rely on specific page table allocation scheme

Today mini-os is making assumptions how the page tables it is started with are being allocated. Especially it is using the number of page table frames to calculate which is the first unmapped pfn.

Instead of relying on page table number assumptions just look into the page tables to find the first pfn not already mapped.

---
 arch/x86/mm.c         | 47 ++++++++++++++++++++++++++++++++++++-----------
 include/x86/arch_mm.h |  7 -------
 2 files changed, 36 insertions(+), 18 deletions(-)
arch/x86/mm.c +36 -11
index 9c6d1b8..5d7c006 100644
@@ -200,8 +200,8 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
     int count = 0;
     int rc;
 
-    pfn_to_map = 
-        (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
+    pfn_to_map = (*start_pfn + L1_PAGETABLE_ENTRIES - 1) &
+                 ~(L1_PAGETABLE_ENTRIES - 1);
 
     if ( *max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START) )
     {
@@ -229,9 +229,15 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
 #if defined(__x86_64__)
         offset = l4_table_offset(start_address);
         /* Need new L3 pt frame */
-        if ( !(start_address & L3_MASK) )
-            if ( need_pt_frame(start_address, L3_FRAME) ) 
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L3_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L3 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
+        }
 
         page = tab[offset];
         pt_mfn = pte_to_mfn(page);
@@ -239,18 +245,37 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
 #endif
         offset = l3_table_offset(start_address);
         /* Need new L2 pt frame */
-        if ( !(start_address & L2_MASK) )
-            if ( need_pt_frame(start_address, L2_FRAME) )
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L2_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L2_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L2 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L2_FRAME);
+        }
 
         page = tab[offset];
         pt_mfn = pte_to_mfn(page);
         tab = to_virt(mfn_to_pfn(pt_mfn) << PAGE_SHIFT);
         offset = l2_table_offset(start_address);        
         /* Need new L1 pt frame */
-        if ( !(start_address & L1_MASK) )
-            if ( need_pt_frame(start_address, L1_FRAME) )
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L1_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L1_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L1 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L1_FRAME);
+        }
+        else if ( !(start_address & L1_MASK) )
+        {
+            /* Already mapped, skip this L1 entry. */
+            start_address += L1_PAGETABLE_ENTRIES << PAGE_SHIFT;
+            pfn_to_map += L1_PAGETABLE_ENTRIES;
+            continue;
+        }
 
         page = tab[offset];
         pt_mfn = pte_to_mfn(page);
include/x86/arch_mm.h +0 -7
index 23cfca7..58f29fc 100644
@@ -56,12 +56,6 @@
 
 #define L2_MASK  ((1UL << L3_PAGETABLE_SHIFT) - 1)
 
-/*
- * If starting from virtual address greater than 0xc0000000,
- * this value will be 2 to account for final mid-level page
- * directory which is always mapped in at this location.
- */
-#define NOT_L1_FRAMES           3
 #define PRIpte "016llx"
 #ifndef __ASSEMBLY__
 typedef uint64_t pgentry_t;
@@ -87,7 +81,6 @@ typedef uint64_t pgentry_t;
 #define L2_MASK  ((1UL << L3_PAGETABLE_SHIFT) - 1)
 #define L3_MASK  ((1UL << L4_PAGETABLE_SHIFT) - 1)
 
-#define NOT_L1_FRAMES           3
 #define PRIpte "016lx"
 #ifndef __ASSEMBLY__
 typedef unsigned long pgentry_t;
Signature · 1 line
2.6.2
#1 lore raw
Quoted 8 lines · Juergen Gross

Today mini-os is making assumptions how the page tables it is started with are being allocated. Especially it is using the number of page table frames to calculate which is the first unmapped pfn.

Instead of relying on page table number assumptions just look into the page tables to find the first pfn not already mapped.

I ran both pv-grub-x86_{64,32} up to the point when grub menu was shown -- That's beyond the point of the original crash.

I also ran mini-os 32 and 64 bit with this patch on toolstack without large p2m series. They worked.

Quoted 25 lines
---
 arch/x86/mm.c         | 47 ++++++++++++++++++++++++++++++++++++-----------
 include/x86/arch_mm.h |  7 -------
 2 files changed, 36 insertions(+), 18 deletions(-)
arch/x86/mm.c +2 -3
index 9c6d1b8..5d7c006 100644
@@ -200,8 +200,8 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
     int count = 0;
     int rc;
 
-    pfn_to_map = 
-        (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
+    pfn_to_map = (*start_pfn + L1_PAGETABLE_ENTRIES - 1) &
+                 ~(L1_PAGETABLE_ENTRIES - 1);
 
     if ( *max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START) )
     {
@@ -229,9 +229,15 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
 #if defined(__x86_64__)
         offset = l4_table_offset(start_address);
         /* Need new L3 pt frame */
-        if ( !(start_address & L3_MASK) )

arch/x86/mm.c static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn) · line 239

Quoted 10 lines
arch/x86/mm.c +8 -2
-            if ( need_pt_frame(start_address, L3_FRAME) ) 
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L3_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L3 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

I'm not sure about this hunk and other similar hunks. Shouldn't this be

if ( need_pt_frame(start_address, L3_FRAME) )
new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

That is, just like before.

Wei.

#2 lore raw
Quoted 54 lines · Wei Liu
Quoted 8 lines · Juergen Gross

Today mini-os is making assumptions how the page tables it is started with are being allocated. Especially it is using the number of page table frames to calculate which is the first unmapped pfn.

Instead of relying on page table number assumptions just look into the page tables to find the first pfn not already mapped.

I ran both pv-grub-x86_{64,32} up to the point when grub menu was shown -- That's beyond the point of the original crash.

I also ran mini-os 32 and 64 bit with this patch on toolstack without large p2m series. They worked.

Quoted 25 lines
---
 arch/x86/mm.c         | 47 ++++++++++++++++++++++++++++++++++++-----------
 include/x86/arch_mm.h |  7 -------
 2 files changed, 36 insertions(+), 18 deletions(-)
arch/x86/mm.c +2 -3
index 9c6d1b8..5d7c006 100644
@@ -200,8 +200,8 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
     int count = 0;
     int rc;
 
-    pfn_to_map = 
-        (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
+    pfn_to_map = (*start_pfn + L1_PAGETABLE_ENTRIES - 1) &
+                 ~(L1_PAGETABLE_ENTRIES - 1);
 
     if ( *max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START) )
     {
@@ -229,9 +229,15 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
 #if defined(__x86_64__)
         offset = l4_table_offset(start_address);
         /* Need new L3 pt frame */
-        if ( !(start_address & L3_MASK) )

arch/x86/mm.c static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn) · line 239

Quoted 10 lines
arch/x86/mm.c +8 -2
-            if ( need_pt_frame(start_address, L3_FRAME) ) 
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L3_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L3 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

I'm not sure about this hunk and other similar hunks. Shouldn't this be

Quoted 4 lines

if ( need_pt_frame(start_address, L3_FRAME) )
new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

That is, just like before.

I didn't want to keep it like before. What sense does it make to find a page table is not existing and to decide it don't need to be created as it should exist already and then to carry on as if it would be there? The only viable alternative would have been to nuke the need_pt_frame() call.

Juergen

#3 lore raw
Quoted 58 lines · Juergen Gross
Quoted 57 lines · Wei Liu
Quoted 8 lines · Juergen Gross

Today mini-os is making assumptions how the page tables it is started with are being allocated. Especially it is using the number of page table frames to calculate which is the first unmapped pfn.

Instead of relying on page table number assumptions just look into the page tables to find the first pfn not already mapped.

I ran both pv-grub-x86_{64,32} up to the point when grub menu was shown -- That's beyond the point of the original crash.

I also ran mini-os 32 and 64 bit with this patch on toolstack without large p2m series. They worked.

Quoted 25 lines
---
 arch/x86/mm.c         | 47 ++++++++++++++++++++++++++++++++++++-----------
 include/x86/arch_mm.h |  7 -------
 2 files changed, 36 insertions(+), 18 deletions(-)
arch/x86/mm.c +2 -3
index 9c6d1b8..5d7c006 100644
@@ -200,8 +200,8 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
     int count = 0;
     int rc;
 
-    pfn_to_map = 
-        (start_info.nr_pt_frames - NOT_L1_FRAMES) * L1_PAGETABLE_ENTRIES;
+    pfn_to_map = (*start_pfn + L1_PAGETABLE_ENTRIES - 1) &
+                 ~(L1_PAGETABLE_ENTRIES - 1);
 
     if ( *max_pfn >= virt_to_pfn(HYPERVISOR_VIRT_START) )
     {
@@ -229,9 +229,15 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
 #if defined(__x86_64__)
         offset = l4_table_offset(start_address);
         /* Need new L3 pt frame */
-        if ( !(start_address & L3_MASK) )

arch/x86/mm.c static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn) · line 239

Quoted 10 lines
arch/x86/mm.c +8 -2
-            if ( need_pt_frame(start_address, L3_FRAME) ) 
-                new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
+        if ( !(tab[offset] & _PAGE_PRESENT) )
+        {
+            if ( !need_pt_frame(start_address, L3_FRAME) )
+            {
+                printk("ERROR: build_pagetable(): L3 frame not present\n");
+                do_exit();
+            }
+            new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

I'm not sure about this hunk and other similar hunks. Shouldn't this be

if ( need_pt_frame(start_address, L3_FRAME) )
new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);

Quoted 8 lines
Quoted 1 line

That is, just like before.

I didn't want to keep it like before. What sense does it make to find a page table is not existing and to decide it don't need to be created as it should exist already and then to carry on as if it would be there? The only viable alternative would have been to nuke the need_pt_frame() call.

Yeah, just nuking the need_pt_frame is fine. That's simpler.

Wei.

Quoted 2 lines

Juergen