Skip to contentSkip to conversation

mini-os: mm: use a generic page table walker

PATCH Superseded Juergen Gross 2 participants 15 messages Jul 31, 2024 – Aug 1, 2024 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mini-os: mm: use a generic page table walker

Instead of open coding a page table walk multiple times, use a generic page table walker instead.

This new page table walker will be used later for kexec support, too.

Juergen Gross (3):
mini-os: mm: introduce generic page table walk function
mini-os: mm: switch need_pgt() to use walk_pt()
mini-os: mm: convert set_readonly() to use walk_pt()

 arch/x86/mm.c | 337 ++++++++++++++++++++++++++++----------------------
 1 file changed, 187 insertions(+), 150 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 mini-os: mm: introduce generic page table walk function

In x86 mm code there are multiple instances of page table walks for different purposes.

Introduce a generic page table walker being able to cover the current use cases. It will be used for other cases in future, too.

The page table walker needs some per-level data, so add a table for that data. Merge it with the already existing pt_prot[] array.

Rewrite get_pgt() to use the new walker.

---
 arch/x86/mm.c | 152 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 113 insertions(+), 39 deletions(-)
arch/x86/mm.c +113 -39
index 7ddf16e4..cc4d41e9 100644
@@ -125,20 +125,25 @@ void arch_mm_preinit(void *p)
 }
 #endif
 
+static const struct {
+    unsigned int shift;
+    unsigned int entries;
+    pgentry_t prot;
+} ptdata[PAGETABLE_LEVELS + 1] = {
+    { 0, 0, 0 },
+    { L1_PAGETABLE_SHIFT, L1_PAGETABLE_ENTRIES, L1_PROT },
+    { L2_PAGETABLE_SHIFT, L2_PAGETABLE_ENTRIES, L2_PROT },
+    { L3_PAGETABLE_SHIFT, L3_PAGETABLE_ENTRIES, L3_PROT },
+#if defined(__x86_64__)
+    { L4_PAGETABLE_SHIFT, L4_PAGETABLE_ENTRIES, L4_PROT },
+#endif
+};
+
 /*
  * Make pt_pfn a new 'level' page table frame and hook it into the page
  * table at offset in previous level MFN (pref_l_mfn). pt_pfn is a guest
  * PFN.
  */
-static pgentry_t pt_prot[PAGETABLE_LEVELS] = {
-    L1_PROT,
-    L2_PROT,
-    L3_PROT,
-#if defined(__x86_64__)
-    L4_PROT,
-#endif
-};
-
 static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn, 
                          unsigned long offset, unsigned long level)
 {   
@@ -170,7 +175,7 @@ static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn,
     mmu_updates[0].ptr = (tab[l2_table_offset(pt_page)] & PAGE_MASK) + 
         sizeof(pgentry_t) * l1_table_offset(pt_page);
     mmu_updates[0].val = (pgentry_t)pfn_to_mfn(*pt_pfn) << PAGE_SHIFT | 
-        (pt_prot[level - 1] & ~_PAGE_RW);
+        (ptdata[level].prot & ~_PAGE_RW);
     
     if ( (rc = HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF)) < 0 )
     {
@@ -183,7 +188,7 @@ static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn,
     mmu_updates[0].ptr =
         ((pgentry_t)prev_l_mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
     mmu_updates[0].val = (pgentry_t)pfn_to_mfn(*pt_pfn) << PAGE_SHIFT |
-        pt_prot[level];
+        ptdata[level + 1].prot;
 
     if ( (rc = HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF)) < 0 ) 
     {
@@ -192,7 +197,7 @@ static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn,
     }
 #else
     tab = mfn_to_virt(prev_l_mfn);
-    tab[offset] = (*pt_pfn << PAGE_SHIFT) | pt_prot[level];
+    tab[offset] = (*pt_pfn << PAGE_SHIFT) | ptdata[level + 1].prot;
 #endif
 
     *pt_pfn += 1;
@@ -202,6 +207,82 @@ static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn,
 static mmu_update_t mmu_updates[L1_PAGETABLE_ENTRIES + 1];
 #endif
 
+/*
+ * Walk recursively through all PTEs calling a specified function. The function
+ * is allowed to change the PTE, the walker will follow the new value.
+ * The walk will cover the virtual address range [from_va .. to_va].
+ * The supplied function will be called with the following parameters:
+ * va: base virtual address of the area covered by the current PTE
+ * lvl: page table level of the PTE (1 = lowest level, PAGETABLE_LEVELS =
+ *      PTE in page table addressed by %cr3)
+ * is_leaf: true if PTE doesn't address another page table
+ * pte: address of the PTE
+ * par: parameter, passed to walk_pt() by caller
+ * Return value of func() being non-zero will terminate walk_pt(), walk_pt()
+ * will return that value in this case, zero else.
+ */
+static int walk_pt(unsigned long from_va, unsigned long to_va,
+                   int (func)(unsigned long va, unsigned int lvl,
+                              bool is_leaf, pgentry_t *pte, void *par),
+                   void *par)
+{
+    unsigned int lvl = PAGETABLE_LEVELS;
+    unsigned int ptindex[PAGETABLE_LEVELS + 1];
+    unsigned long va = round_pgdown(from_va);
+    unsigned long va_lvl;
+    pgentry_t *tab[PAGETABLE_LEVELS + 1];
+    pgentry_t *pte;
+    bool is_leaf;
+    int ret;
+
+    /* Start at top level page table. */
+    tab[lvl] = pt_base;
+    ptindex[lvl] = (va >> ptdata[lvl].shift) & (ptdata[lvl].entries - 1);
+
+    while ( va < (to_va | (PAGE_SIZE - 1)) )
+    {
+        pte = tab[lvl] + ptindex[lvl];
+        is_leaf = (lvl == L1_FRAME) || (*pte & _PAGE_PSE) ||
+                  !(*pte & _PAGE_PRESENT);
+        va_lvl = va & ~((1UL << ptdata[lvl].shift) - 1);
+        ret = func(va_lvl, lvl, is_leaf, pte, par);
+        if ( ret )
+            return ret;
+
+        /* PTE might have been modified by func(), reevaluate leaf state. */
+        is_leaf = (lvl == L1_FRAME) || (*pte & _PAGE_PSE) ||
+                  !(*pte & _PAGE_PRESENT);
+
+        if ( is_leaf )
+        {
+            /* Reached a leaf PTE. Advance to next page. */
+            va += 1UL << ptdata[lvl].shift;
+            ptindex[lvl]++;
+
+            /* Check for the need to traverse up again. */
+            while ( ptindex[lvl] == ptdata[lvl].entries )
+            {
+                /* End of virtual address space? */
+                if ( lvl == PAGETABLE_LEVELS )
+                    return 0;
+                /* Reached end of current page table, one level up. */
+                lvl++;
+                ptindex[lvl]++;
+            }
+        }
+        else
+        {
+            /* Not a leaf, walk one level down. */
+            lvl--;
+            tab[lvl] = mfn_to_virt(pte_to_mfn(*pte));
+            ptindex[lvl] = (va >> ptdata[lvl].shift) &
+                           (ptdata[lvl].entries - 1);
+        }
+    }
+
+    return 0;
+}
+
 /*
  * Build the initial pagetable.
  */
@@ -407,36 +488,29 @@ static void set_readonly(void *text, void *etext)
 /*
  * get the PTE for virtual address va if it exists. Otherwise NULL.
  */
-static pgentry_t *get_pgt(unsigned long va)
+static int get_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                        pgentry_t *pte, void *par)
 {
-    unsigned long mfn;
-    pgentry_t *tab;
-    unsigned offset;
+    pgentry_t **result;
 
-    tab = pt_base;
-    mfn = virt_to_mfn(pt_base);
+    if ( !(*pte & _PAGE_PRESENT) && lvl > L1_FRAME )
+        return -1;
 
-#if defined(__x86_64__)
-    offset = l4_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) )
-        return NULL;
-    mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(mfn);
-#endif
-    offset = l3_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) )
-        return NULL;
-    mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(mfn);
-    offset = l2_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) )
-        return NULL;
-    if ( tab[offset] & _PAGE_PSE )
-        return &tab[offset];
-    mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(mfn);
-    offset = l1_table_offset(va);
-    return &tab[offset];
+    if ( lvl > L1_FRAME && !(*pte & _PAGE_PSE) )
+        return 0;
+
+    result = par;
+    *result = pte;
+
+    return 0;
+}
+
+static pgentry_t *get_pgt(unsigned long va)
+{
+    pgentry_t *tab = NULL;
+
+    walk_pt(va, va, get_pgt_func, &tab);
+    return tab;
 }
 
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] mini-os: mm: introduce generic page table walk function

Juergen Gross, le mer. 31 juil. 2024 15:00:24 +0200, a ecrit:

arch/x86/mm.c static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn, · line 279

Quoted 2 lines
arch/x86/mm.c +2 -0
+            ptindex[lvl] = (va >> ptdata[lvl].shift) &
+                           (ptdata[lvl].entries - 1);

It seems that this kind of va+lvl-to-index computation happens several times? It's probably worth making it a macro.

Samuel

#3 lore raw

Re: [PATCH 1/3] mini-os: mm: introduce generic page table walk function

Quoted 6 lines · Samuel Thibault

Juergen Gross, le mer. 31 juil. 2024 15:00:24 +0200, a ecrit:

arch/x86/mm.c static void new_pt_frame(unsigned long *pt_pfn, unsigned long prev_l_mfn, · line 279

Quoted 2 lines
arch/x86/mm.c +2 -0
+            ptindex[lvl] = (va >> ptdata[lvl].shift) &
+                           (ptdata[lvl].entries - 1);

It seems that this kind of va+lvl-to-index computation happens several times? It's probably worth making it a macro.

Agreed.

Juergen

#4 lore raw

PATCH 2/3 mini-os: mm: switch need_pgt() to use walk_pt()

In reply to Juergen Gross

Instead of open coding a page table walk, use walk_pt() in need_pgt().

---
 arch/x86/mm.c | 66 +++++++++++++++++++--------------------------------
 1 file changed, 24 insertions(+), 42 deletions(-)
arch/x86/mm.c +24 -42
index cc4d41e9..accde291 100644
@@ -518,57 +518,39 @@ static pgentry_t *get_pgt(unsigned long va)
  * return a valid PTE for a given virtual address. If PTE does not exist,
  * allocate page-table pages.
  */
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
 {
+    pgentry_t **result = par;
     unsigned long pt_mfn;
-    pgentry_t *tab;
     unsigned long pt_pfn;
-    unsigned offset;
+    unsigned int idx;
 
-    tab = pt_base;
-    pt_mfn = virt_to_mfn(pt_base);
+    if ( !is_leaf )
+        return 0;
 
-#if defined(__x86_64__)
-    offset = l4_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) )
-    {
-        pt_pfn = virt_to_pfn(alloc_page());
-        if ( !pt_pfn )
-            return NULL;
-        new_pt_frame(&pt_pfn, pt_mfn, offset, L3_FRAME);
-    }
-    ASSERT(tab[offset] & _PAGE_PRESENT);
-    pt_mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(pt_mfn);
-#endif
-    offset = l3_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) ) 
-    {
-        pt_pfn = virt_to_pfn(alloc_page());
-        if ( !pt_pfn )
-            return NULL;
-        new_pt_frame(&pt_pfn, pt_mfn, offset, L2_FRAME);
-    }
-    ASSERT(tab[offset] & _PAGE_PRESENT);
-    pt_mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(pt_mfn);
-    offset = l2_table_offset(va);
-    if ( !(tab[offset] & _PAGE_PRESENT) )
+    if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )
     {
-        pt_pfn = virt_to_pfn(alloc_page());
-        if ( !pt_pfn )
-            return NULL;
-        new_pt_frame(&pt_pfn, pt_mfn, offset, L1_FRAME);
+        *result = pte;
+        return 1;
     }
-    ASSERT(tab[offset] & _PAGE_PRESENT);
-    if ( tab[offset] & _PAGE_PSE )
-        return &tab[offset];
 
-    pt_mfn = pte_to_mfn(tab[offset]);
-    tab = mfn_to_virt(pt_mfn);
+    pt_mfn = virt_to_mfn(pte);
+    pt_pfn = virt_to_pfn(alloc_page());
+    if ( !pt_pfn )
+        return -1;
+    idx = (va >> ptdata[lvl].shift) & (ptdata[lvl].entries - 1);
+    new_pt_frame(&pt_pfn, pt_mfn, idx, lvl - 1);
 
-    offset = l1_table_offset(va);
-    return &tab[offset];
+    return 0;
+}
+
+pgentry_t *need_pgt(unsigned long va)
+{
+    pgentry_t *tab = NULL;
+
+    walk_pt(va, va, need_pgt_func, &tab);
+    return tab;
 }
 EXPORT_SYMBOL(need_pgt);
 
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
 {

[...]

Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

arch/x86/mm.c static pgentry_t *get_pgt(unsigned long va) · line 536

Quoted 4 lines
     {
arch/x86/mm.c +2 -0
+        *result = pte;
+        return 1;
     }

Samuel

#6 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Quoted 11 lines · Samuel Thibault

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
  {

[...]

Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No. I want to bail out if the PTE does not require a page table to be added to it. This is the case if the PTE is valid or if it is at the lowest page table level already.

An invalid PTE with PSE set is still invalid, so the PSE bit has no real meaning.

Juergen

#7 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Jürgen Groß, le jeu. 01 août 2024 07:56:36 +0200, a ecrit:

Quoted 9 lines · Samuel Thibault
Quoted 8 lines

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
  {

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No.

I don't understand: it doesn't map what I know of need_pgt and what the existing code is doing.

AIKI, the point of need_pgt is to make sure there is a L1 page table entry for a VA, and return it, so the caller can put in it at pte for a mfn or such. In the case a PSE is met, we don't go further, and it's up to the caller to decide what it wants to do (most often it's actually unexpected and asserted out). In both cases, the PRESENT bit of the pte whose address is returned does not matter, most often it's the caller which will set it.

The existing code for need_pgt thus always adds page table entries down to level1 (except if _PAGE_PSE is met, i.e. a large page was already set up): the termination was:

[... walk down to level 2]
- if ( tab[offset] & _PAGE_PSE )
- return &tab[offset];
[... walk down to level 1]
- return &tab[offset];

i.e. we always return either a PSE L2 entry, or an L1 entry, and never above (while keeping sure that the entries above are present).

Quoted 2 lines

I want to bail out if the PTE does not require a page table to be added to it. This is the case if the PTE is valid

There being an entry at e.g. level 3 (without PSE) does not mean that we have an entry at level 1 for the VA, so we have to continue adding levels for the VA in the sparse page table tree.

Quoted 2 lines

An invalid PTE with PSE set is still invalid, so the PSE bit has no real meaning.

Mmm, I don't think we ever create entries with PSE but not PRESENT (i.e. we don't "plan" to have PSE entries), but even if we did, if we "plan" to have PSE entries, we should return that entry, and it's up to the caller to fill it (and make it PRESENT)

Samuel

#8 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Quoted 27 lines · Samuel Thibault

Jürgen Groß, le jeu. 01 août 2024 07:56:36 +0200, a ecrit:

Quoted 9 lines · Samuel Thibault
Quoted 8 lines

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
   {

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No.

I don't understand: it doesn't map what I know of need_pgt and what the existing code is doing.

AIKI, the point of need_pgt is to make sure there is a L1 page table entry for a VA, and return it, so the caller can put in it at pte for a mfn or such. In the case a PSE is met, we don't go further, and it's up to the caller to decide what it wants to do (most often it's actually unexpected and asserted out). In both cases, the PRESENT bit of the pte whose address is returned does not matter, most often it's the caller which will set it.

Quoted 5 lines

The existing code for need_pgt thus always adds page table entries down to level1 (except if _PAGE_PSE is met, i.e. a large page was already set up): the termination was:

[... walk down to level 2]

You have omitted:

       ASSERT(tab[offset] & _PAGE_PRESENT);
Quoted 4 lines
(quoted diff) +0 -2
-    if ( tab[offset] & _PAGE_PSE )
-        return &tab[offset];

[... walk down to level 1]
- return &tab[offset];

Juergen

#9 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Jürgen Groß, le jeu. 01 août 2024 10:13:07 +0200, a ecrit:

Quoted 32 lines · Samuel Thibault
Quoted 31 lines

Jürgen Groß, le jeu. 01 août 2024 07:56:36 +0200, a ecrit:

Quoted 9 lines · Samuel Thibault
Quoted 8 lines

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
   {

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No.

I don't understand: it doesn't map what I know of need_pgt and what the existing code is doing.

AIKI, the point of need_pgt is to make sure there is a L1 page table entry for a VA, and return it, so the caller can put in it at pte for a mfn or such. In the case a PSE is met, we don't go further, and it's up to the caller to decide what it wants to do (most often it's actually unexpected and asserted out). In both cases, the PRESENT bit of the pte whose address is returned does not matter, most often it's the caller which will set it.

The existing code for need_pgt thus always adds page table entries down to level1 (except if _PAGE_PSE is met, i.e. a large page was already set up): the termination was:

Quoted 5 lines
Quoted 1 line

[... walk down to level 2]

You have omitted:

       ASSERT(tab[offset] & _PAGE_PRESENT);

Right, that confirms that we never set PSE without PRESENT. We probably want to keep that assertion in the PSE case.

But we still want to continue walking down to level1 (or pse) even when L4/L3/L2 have PRESENT.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -2
-    if ( tab[offset] & _PAGE_PSE )
-        return &tab[offset];

[... walk down to level 1]
- return &tab[offset];

Samuel

#10 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Quoted 33 lines · Samuel Thibault

Jürgen Groß, le jeu. 01 août 2024 10:13:07 +0200, a ecrit:

Quoted 32 lines · Samuel Thibault
Quoted 31 lines

Jürgen Groß, le jeu. 01 août 2024 07:56:36 +0200, a ecrit:

Quoted 9 lines · Samuel Thibault
Quoted 8 lines

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
arch/x86/mm.c +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
    {

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No.

I don't understand: it doesn't map what I know of need_pgt and what the existing code is doing.

AIKI, the point of need_pgt is to make sure there is a L1 page table entry for a VA, and return it, so the caller can put in it at pte for a mfn or such. In the case a PSE is met, we don't go further, and it's up to the caller to decide what it wants to do (most often it's actually unexpected and asserted out). In both cases, the PRESENT bit of the pte whose address is returned does not matter, most often it's the caller which will set it.

The existing code for need_pgt thus always adds page table entries down to level1 (except if _PAGE_PSE is met, i.e. a large page was already set up): the termination was:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

[... walk down to level 2]

You have omitted:

        ASSERT(tab[offset] & _PAGE_PRESENT);

Right, that confirms that we never set PSE without PRESENT. We probably want to keep that assertion in the PSE case.

Hmm, I'm not sure.

The hardware allows to have any bit set in the PTE when PRESENT isn't set. It will never look at any other bit in this case. Why should the software require something different here?

Quoted 2 lines

But we still want to continue walking down to level1 (or pse) even when L4/L3/L2 have PRESENT.

We do that. In this case the is_leaf flag won't be set and need_pgt_func() will return early with the return value 0, meaning that the walk will be continued.

Juergen

#11 lore raw

Re: [PATCH 2/3] mini-os: mm: switch need_pgt() to use walk_pt()

Jürgen Groß, le jeu. 01 août 2024 11:25:31 +0200, a ecrit:

Quoted 40 lines · Samuel Thibault
Quoted 39 lines

Jürgen Groß, le jeu. 01 août 2024 10:13:07 +0200, a ecrit:

Quoted 37 lines · Samuel Thibault
Quoted 27 lines

Jürgen Groß, le jeu. 01 août 2024 07:56:36 +0200, a ecrit:

Quoted 9 lines · Samuel Thibault
Quoted 8 lines

Hello,

Juergen Gross, le mer. 31 juil. 2024 15:00:25 +0200, a ecrit:

Quoted 4 lines
(quoted diff) +2 -1
-pgentry_t *need_pgt(unsigned long va)
+static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                         pgentry_t *pte, void *par)
    {

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) )

Did you mean (*pte & _PAGE_PSE)?

No.

I don't understand: it doesn't map what I know of need_pgt and what the existing code is doing.

AIKI, the point of need_pgt is to make sure there is a L1 page table entry for a VA, and return it, so the caller can put in it at pte for a mfn or such. In the case a PSE is met, we don't go further, and it's up to the caller to decide what it wants to do (most often it's actually unexpected and asserted out). In both cases, the PRESENT bit of the pte whose address is returned does not matter, most often it's the caller which will set it.

Quoted 5 lines

The existing code for need_pgt thus always adds page table entries down to level1 (except if _PAGE_PSE is met, i.e. a large page was already set up): the termination was:

[... walk down to level 2]

You have omitted:

        ASSERT(tab[offset] & _PAGE_PRESENT);
Quoted 8 lines
Quoted 2 lines

Right, that confirms that we never set PSE without PRESENT. We probably want to keep that assertion in the PSE case.

Hmm, I'm not sure.

The hardware allows to have any bit set in the PTE when PRESENT isn't set. It will never look at any other bit in this case. Why should the software require something different here?

To document&check what we expect to be producing.

Quoted 6 lines
Quoted 2 lines

But we still want to continue walking down to level1 (or pse) even when L4/L3/L2 have PRESENT.

We do that. In this case the is_leaf flag won't be set and need_pgt_func() will return early with the return value 0, meaning that the walk will be continued.

Ah, I missed that indeed, I had not integrated that is_leaf is 1 also in the case of missing level>1 page. It'd probably be useful to emphasize in patch 1:

+ * is_leaf: true if PTE doesn't address another page table

it should explicitly say (L1 PTE, or PSE, or not present yet)

The *pte & _PAGE_PRESENT test alone still looks surprising to the reader, though: we don't return because the page is present, but because we reached the last possible level that we could fill, which is either L1, or an existing PSE. It is true that when is_leaf is 1, if it's not L1 but present, it's necessarily a PSE, but it's clearer to the reader to write what we meant: L1 or PSE, so something like:

if ( lvl == L1_FRAME || (*pte & _PAGE_PRESENT) ) {
    assert ( lvl == L1_FRAME || (*pte & _PAGE_PSE) ); // we are at last possible level
    [...]
}

Samuel

#12 lore raw

PATCH 3/3 mini-os: mm: convert set_readonly() to use walk_pt()

In reply to Juergen Gross

Instead of having another copy of a page table walk in set_readonly(), just use walk_pt().

As it will be needed later anyway, split out the TLB flushing into a dedicated function.

---
 arch/x86/mm.c | 119 +++++++++++++++++++++-----------------------------
 1 file changed, 50 insertions(+), 69 deletions(-)
arch/x86/mm.c +50 -69
index accde291..90992068 100644
@@ -397,92 +397,73 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)
  * Mark portion of the address space read only.
  */
 extern struct shared_info shared_info;
-static void set_readonly(void *text, void *etext)
-{
-    unsigned long start_address =
-        ((unsigned long) text + PAGE_SIZE - 1) & PAGE_MASK;
-    unsigned long end_address = (unsigned long) etext;
-    pgentry_t *tab = pt_base, page;
-    unsigned long mfn = pfn_to_mfn(virt_to_pfn(pt_base));
-    unsigned long offset;
-    unsigned long page_size = PAGE_SIZE;
+
+struct set_readonly_par {
+    unsigned long etext;
 #ifdef CONFIG_PARAVIRT
-    int count = 0;
-    int rc;
+    unsigned int count;
 #endif
+};
 
-    printk("setting %p-%p readonly\n", text, etext);
+static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                             pgentry_t *pte, void *par)
+{
+    struct set_readonly_par *ro = par;
 
-    while ( start_address + page_size <= end_address )
-    {
-        tab = pt_base;
-        mfn = pfn_to_mfn(virt_to_pfn(pt_base));
+    if ( !is_leaf )
+        return 0;
 
-#if defined(__x86_64__)
-        offset = l4_table_offset(start_address);
-        page = tab[offset];
-        mfn = pte_to_mfn(page);
-        tab = to_virt(mfn_to_pfn(mfn) << PAGE_SHIFT);
-#endif
-        offset = l3_table_offset(start_address);
-        page = tab[offset];
-        mfn = pte_to_mfn(page);
-        tab = to_virt(mfn_to_pfn(mfn) << PAGE_SHIFT);
-        offset = l2_table_offset(start_address);        
-        if ( !(tab[offset] & _PAGE_PSE) )
-        {
-            page = tab[offset];
-            mfn = pte_to_mfn(page);
-            tab = to_virt(mfn_to_pfn(mfn) << PAGE_SHIFT);
+    if ( va + (1UL << ptdata[lvl].shift) > ro->etext )
+        return 1;
 
-            offset = l1_table_offset(start_address);
-        }
+    if ( va == (unsigned long)&shared_info )
+    {
+        printk("skipped %lx\n", va);
+        return 0;
+    }
 
-        if ( start_address != (unsigned long)&shared_info )
-        {
 #ifdef CONFIG_PARAVIRT
-            mmu_updates[count].ptr = 
-                ((pgentry_t)mfn << PAGE_SHIFT) + sizeof(pgentry_t) * offset;
-            mmu_updates[count].val = tab[offset] & ~_PAGE_RW;
-            count++;
+    mmu_updates[ro->count].ptr = virt_to_mach(pte);
+    mmu_updates[ro->count].val = *pte & ~_PAGE_RW;
+    ro->count++;
+
+    if ( (ro->count == L1_PAGETABLE_ENTRIES ||
+          va + 2 * PAGE_SIZE > ro->etext) &&
+         HYPERVISOR_mmu_update(mmu_updates, ro->count, NULL, DOMID_SELF) < 0 )
+    {
+        printk("ERROR: set_readonly(): PTE could not be updated\n");
+        do_exit();
+    }
 #else
-            tab[offset] &= ~_PAGE_RW;
+    *pte &= ~_PAGE_RW;
 #endif
-        }
-        else
-            printk("skipped %lx\n", start_address);
 
-        start_address += page_size;
+    return 0;
+}
 
 #ifdef CONFIG_PARAVIRT
-        if ( count == L1_PAGETABLE_ENTRIES || 
-             start_address + page_size > end_address )
-        {
-            rc = HYPERVISOR_mmu_update(mmu_updates, count, NULL, DOMID_SELF);
-            if ( rc < 0 )
-            {
-                printk("ERROR: set_readonly(): PTE could not be updated\n");
-                do_exit();
-            }
-            count = 0;
-        }
-#else
-        if ( start_address == (1UL << L2_PAGETABLE_SHIFT) )
-            page_size = 1UL << L2_PAGETABLE_SHIFT;
-#endif
-    }
+static void tlb_flush(void)
+{
+    mmuext_op_t op = { .cmd = MMUEXT_TLB_FLUSH_ALL };
+    int count;
 
-#ifdef CONFIG_PARAVIRT
-    {
-        mmuext_op_t op = {
-            .cmd = MMUEXT_TLB_FLUSH_ALL,
-        };
-        int count;
-        HYPERVISOR_mmuext_op(&op, 1, &count, DOMID_SELF);
-    }
+    HYPERVISOR_mmuext_op(&op, 1, &count, DOMID_SELF);
+}
 #else
+static void tlb_flush(void)
+{
     write_cr3((unsigned long)pt_base);
+}
 #endif
+
+static void set_readonly(void *text, void *etext)
+{
+    struct set_readonly_par setro = { .etext = (unsigned long)etext };
+    unsigned long start_address = PAGE_ALIGN((unsigned long)text);
+
+    printk("setting %p-%p readonly\n", text, etext);
+    walk_pt(start_address, setro.etext, set_readonly_func, &setro);
+    tlb_flush();
 }
 
 /*
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH 3/3] mini-os: mm: convert set_readonly() to use walk_pt()

Juergen Gross, le mer. 31 juil. 2024 15:00:26 +0200, a ecrit:

Quoted 6 lines
arch/x86/mm.c +5 -0
+static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                             pgentry_t *pte, void *par)
+{
+    struct set_readonly_par *ro = par;
 
+    mmu_updates[ro->count].ptr = virt_to_mach(pte);

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

Quoted 10 lines
arch/x86/mm.c +10 -0
+    mmu_updates[ro->count].val = *pte & ~_PAGE_RW;
+    ro->count++;
+
+    if ( (ro->count == L1_PAGETABLE_ENTRIES ||
+          va + 2 * PAGE_SIZE > ro->etext) &&
+         HYPERVISOR_mmu_update(mmu_updates, ro->count, NULL, DOMID_SELF) < 0 )
+    {
+        printk("ERROR: set_readonly(): PTE could not be updated\n");
+        do_exit();
+    }

Don't we also want to set ro->count to 0?
And assert that it is 0 after calling walk_pt in set_readonly, to make sure the va + 2 * PAGE_SIZE > ro->etext test did work properly (personally I would have rather made set_readonly call a last HYPERVISOR_mmu_update in case ro->count is not 0, which looks more robust that a quite magic-looking va + 2 * PAGE_SIZE > ro->etext test)

Samuel

#14 lore raw

Re: [PATCH 3/3] mini-os: mm: convert set_readonly() to use walk_pt()

Quoted 6 lines · Samuel Thibault

Juergen Gross, le mer. 31 juil. 2024 15:00:26 +0200, a ecrit:

Quoted 5 lines
arch/x86/mm.c +4 -0
+static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_leaf,
+                             pgentry_t *pte, void *par)
+{
+    struct set_readonly_par *ro = par;
Quoted 13 lines

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

Quoted 11 lines
arch/x86/mm.c +11 -0
+    mmu_updates[ro->count].ptr = virt_to_mach(pte);
+    mmu_updates[ro->count].val = *pte & ~_PAGE_RW;
+    ro->count++;
+
+    if ( (ro->count == L1_PAGETABLE_ENTRIES ||
+          va + 2 * PAGE_SIZE > ro->etext) &&
+         HYPERVISOR_mmu_update(mmu_updates, ro->count, NULL, DOMID_SELF) < 0 )
+    {
+        printk("ERROR: set_readonly(): PTE could not be updated\n");
+        do_exit();
+    }

Don't we also want to set ro->count to 0?

Oh, indeed. Thanks for catching this.

Quoted 5 lines

And assert that it is 0 after calling walk_pt in set_readonly, to make sure the va + 2 * PAGE_SIZE > ro->etext test did work properly (personally I would have rather made set_readonly call a last HYPERVISOR_mmu_update in case ro->count is not 0, which looks more robust that a quite magic-looking va + 2 * PAGE_SIZE > ro->etext test)

I think you are right. I'll do that.

Juergen