All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Grall <julien@xen.org>
To: xen-devel@lists.xenproject.org
Cc: julien@xen.org, Julien Grall <julien.grall@arm.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Bertrand Marquis <bertrand.marquis@arm.com>,
	Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>,
	Julien Grall <jgrall@amazon.com>
Subject: [PATCH v3 18/19] xen/arm: mm: Rework setup_xenheap_mappings()
Date: Mon, 21 Feb 2022 10:22:17 +0000	[thread overview]
Message-ID: <20220221102218.33785-19-julien@xen.org> (raw)
In-Reply-To: <20220221102218.33785-1-julien@xen.org>

From: Julien Grall <julien.grall@arm.com>

The current implementation of setup_xenheap_mappings() is using 1GB
mappings. This can lead to unexpected result because the mapping
may alias a non-cachable region (such as device or reserved regions).
For more details see B2.8 in ARM DDI 0487H.a.

map_pages_to_xen() was recently reworked to allow superpage mappings,
support contiguous mapping and deal with the use of pagge-tables before
they are mapped.

Most of the code in setup_xenheap_mappings() is now replaced with a
single call to map_pages_to_xen().

Signed-off-by: Julien Grall <julien.grall@arm.com>
Signed-off-by: Julien Grall <jgrall@amazon.com>

---
    Changes in v3:
        - Don't use 1GB mapping
        - Re-order code in setup_mm() in a separate patch

    Changes in v2:
        - New patch
---
 xen/arch/arm/mm.c | 87 ++++++++++-------------------------------------
 1 file changed, 18 insertions(+), 69 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index 11b6b60a2bc1..4af59375d998 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -138,17 +138,6 @@ static DEFINE_PAGE_TABLE(cpu0_pgtable);
 static DEFINE_PAGE_TABLES(cpu0_dommap, DOMHEAP_SECOND_PAGES);
 #endif
 
-#ifdef CONFIG_ARM_64
-/* The first page of the first level mapping of the xenheap. The
- * subsequent xenheap first level pages are dynamically allocated, but
- * we need this one to bootstrap ourselves. */
-static DEFINE_PAGE_TABLE(xenheap_first_first);
-/* The zeroeth level slot which uses xenheap_first_first. Used because
- * setup_xenheap_mappings otherwise relies on mfn_to_virt which isn't
- * valid for a non-xenheap mapping. */
-static __initdata int xenheap_first_first_slot = -1;
-#endif
-
 /* Common pagetable leaves */
 /* Second level page tables.
  *
@@ -815,77 +804,37 @@ void __init setup_xenheap_mappings(unsigned long base_mfn,
 void __init setup_xenheap_mappings(unsigned long base_mfn,
                                    unsigned long nr_mfns)
 {
-    lpae_t *first, pte;
-    unsigned long mfn, end_mfn;
-    vaddr_t vaddr;
-
-    /* Align to previous 1GB boundary */
-    mfn = base_mfn & ~((FIRST_SIZE>>PAGE_SHIFT)-1);
+    int rc;
 
     /* First call sets the xenheap physical and virtual offset. */
     if ( mfn_eq(xenheap_mfn_start, INVALID_MFN) )
     {
+        unsigned long mfn_gb = base_mfn & ~((FIRST_SIZE >> PAGE_SHIFT) - 1);
+
         xenheap_mfn_start = _mfn(base_mfn);
         xenheap_base_pdx = mfn_to_pdx(_mfn(base_mfn));
+        /*
+         * The base address may not be aligned to the first level
+         * size (e.g. 1GB when using 4KB pages). This would prevent
+         * superpage mappings for all the regions because the virtual
+         * address and machine address should both be suitably aligned.
+         *
+         * Prevent that by offsetting the start of the xenheap virtual
+         * address.
+         */
         xenheap_virt_start = DIRECTMAP_VIRT_START +
-            (base_mfn - mfn) * PAGE_SIZE;
+            (base_mfn - mfn_gb) * PAGE_SIZE;
     }
 
     if ( base_mfn < mfn_x(xenheap_mfn_start) )
         panic("cannot add xenheap mapping at %lx below heap start %lx\n",
               base_mfn, mfn_x(xenheap_mfn_start));
 
-    end_mfn = base_mfn + nr_mfns;
-
-    /*
-     * Virtual address aligned to previous 1GB to match physical
-     * address alignment done above.
-     */
-    vaddr = (vaddr_t)__mfn_to_virt(base_mfn) & FIRST_MASK;
-
-    while ( mfn < end_mfn )
-    {
-        int slot = zeroeth_table_offset(vaddr);
-        lpae_t *p = &xen_pgtable[slot];
-
-        if ( p->pt.valid )
-        {
-            /* mfn_to_virt is not valid on the 1st 1st mfn, since it
-             * is not within the xenheap. */
-            first = slot == xenheap_first_first_slot ?
-                xenheap_first_first : mfn_to_virt(lpae_get_mfn(*p));
-        }
-        else if ( xenheap_first_first_slot == -1)
-        {
-            /* Use xenheap_first_first to bootstrap the mappings */
-            first = xenheap_first_first;
-
-            pte = pte_of_xenaddr((vaddr_t)xenheap_first_first);
-            pte.pt.table = 1;
-            write_pte(p, pte);
-
-            xenheap_first_first_slot = slot;
-        }
-        else
-        {
-            mfn_t first_mfn = alloc_boot_pages(1, 1);
-
-            clear_page(mfn_to_virt(first_mfn));
-            pte = mfn_to_xen_entry(first_mfn, MT_NORMAL);
-            pte.pt.table = 1;
-            write_pte(p, pte);
-            first = mfn_to_virt(first_mfn);
-        }
-
-        pte = mfn_to_xen_entry(_mfn(mfn), MT_NORMAL);
-        /* TODO: Set pte.pt.contig when appropriate. */
-        write_pte(&first[first_table_offset(vaddr)], pte);
-
-        mfn += FIRST_SIZE>>PAGE_SHIFT;
-        vaddr += FIRST_SIZE;
-    }
-
-    flush_xen_tlb_local();
+    rc = map_pages_to_xen((vaddr_t)__mfn_to_virt(base_mfn),
+                          _mfn(base_mfn), nr_mfns,
+                          PAGE_HYPERVISOR_RW | _PAGE_BLOCK);
+    if ( rc )
+        panic("Unable to setup the xenheap mappings.\n");
 }
 #endif
 
-- 
2.32.0



  parent reply	other threads:[~2022-02-21 10:51 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 10:21 [PATCH v3 00/19] xen/arm: mm: Remove open-coding mappings Julien Grall
2022-02-21 10:22 ` [PATCH v3 01/19] xen/arm: lpae: Rename LPAE_ENTRIES_MASK_GS to LPAE_ENTRY_MASK_GS Julien Grall
2022-02-22 13:30   ` Michal Orzel
2022-02-24 22:19     ` Julien Grall
2022-02-22 15:21   ` Bertrand Marquis
2022-02-21 10:22 ` [PATCH v3 02/19] xen/arm: lpae: Use the generic helpers to defined the Xen PT helpers Julien Grall
2022-02-22 14:26   ` Michal Orzel
2022-02-22 15:38   ` Bertrand Marquis
2022-02-24 22:24     ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 03/19] xen/arm: p2m: Replace level_{orders, masks} arrays with XEN_PT_LEVEL_{ORDER, MASK} Julien Grall
2022-02-22 15:55   ` Bertrand Marquis
2022-02-24 22:41     ` Julien Grall
2022-02-25  8:27       ` Bertrand Marquis
2022-02-25 16:41         ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 04/19] xen/arm: mm: Allow other mapping size in xen_pt_update_entry() Julien Grall
2022-04-01 23:35   ` Stefano Stabellini
2022-04-02 15:59     ` Julien Grall
2022-04-05 20:46       ` Stefano Stabellini
2022-04-10 12:17         ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 05/19] xen/arm: mm: Add support for the contiguous bit Julien Grall
2022-02-26 19:30   ` Julien Grall
2022-03-21  5:46     ` Hongda Deng
2022-04-01 23:53   ` Stefano Stabellini
2022-04-02 16:18     ` Julien Grall
2022-04-05 20:47       ` Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 06/19] xen/arm: mm: Avoid flushing the TLBs when mapping are inserted Julien Grall
2022-04-02  0:00   ` Stefano Stabellini
2022-04-02 16:38     ` Julien Grall
2022-04-05 20:49       ` Stefano Stabellini
2022-04-10 12:30         ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 07/19] xen/arm: mm: Don't open-code Xen PT update in remove_early_mappings() Julien Grall
2022-04-02  0:04   ` Stefano Stabellini
2022-04-02 16:47     ` Julien Grall
2022-04-05 20:51       ` Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 08/19] xen/arm: mm: Re-implement early_fdt_map() using map_pages_to_xen() Julien Grall
2022-03-18  7:36   ` Hongda Deng
2022-03-18 19:44     ` Julien Grall
2022-04-02  0:10   ` Stefano Stabellini
2022-04-02 17:02     ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 09/19] xen/arm32: mm: Check if the virtual address is shared before updating it Julien Grall
2022-03-18 10:44   ` Hongda Deng
2022-03-18 22:15     ` Julien Grall
2022-03-19 15:59   ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 10/19] xen/arm32: mm: Re-implement setup_xenheap_mappings() using map_pages_to_xen() Julien Grall
2022-04-02  0:11   ` Stefano Stabellini
2022-05-14  9:42   ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 11/19] xen/arm: mm: Allocate xen page tables in domheap rather than xenheap Julien Grall
2022-02-21 10:22 ` [PATCH v3 12/19] xen/arm: mm: Allow page-table allocation from the boot allocator Julien Grall
2022-04-05 20:58   ` Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 13/19] xen/arm: Move fixmap definitions in a separate header Julien Grall
2022-02-22 15:10   ` Jan Beulich
2022-04-05 21:12   ` Stefano Stabellini
2022-04-05 21:47     ` Julien Grall
2022-04-06  0:10       ` Stefano Stabellini
2022-04-06  8:24         ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 14/19] xen/arm: add Persistent Map (PMAP) infrastructure Julien Grall
2022-02-22 15:22   ` Jan Beulich
2022-02-28  9:55     ` Julien Grall
2022-02-28 10:10       ` Jan Beulich
2022-02-28 10:20         ` Julien Grall
2022-02-28 10:30           ` Jan Beulich
2022-02-28 10:52             ` Julien Grall
2022-04-05 21:27   ` Stefano Stabellini
2022-05-14 10:17     ` Julien Grall
2022-02-21 10:22 ` [PATCH v3 15/19] xen/arm: mm: Clean-up the includes and order them Julien Grall
2022-04-05 21:29   ` Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 16/19] xen/arm: mm: Use the PMAP helpers in xen_{,un}map_table() Julien Grall
2022-04-05 21:36   ` Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 17/19] xen/arm64: mm: Add memory to the boot allocator first Julien Grall
2022-04-05 21:50   ` Stefano Stabellini
2022-04-05 22:12     ` Julien Grall
2022-04-06  0:02       ` Stefano Stabellini
2022-02-21 10:22 ` Julien Grall [this message]
2022-04-05 23:57   ` [PATCH v3 18/19] xen/arm: mm: Rework setup_xenheap_mappings() Stefano Stabellini
2022-02-21 10:22 ` [PATCH v3 19/19] xen/arm: mm: Re-implement setup_frame_table_mappings() with map_pages_to_xen() Julien Grall
2022-03-16  6:10   ` Hongda Deng
2022-04-06  0:01   ` Stefano Stabellini
2022-05-14 10:02     ` Julien Grall
2022-02-27 19:25 ` [PATCH v3 00/19] xen/arm: mm: Remove open-coding mappings Julien Grall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220221102218.33785-19-julien@xen.org \
    --to=julien@xen.org \
    --cc=Volodymyr_Babchuk@epam.com \
    --cc=bertrand.marquis@arm.com \
    --cc=jgrall@amazon.com \
    --cc=julien.grall@arm.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.