diff mbox

[v2,7/8] xen/x86: Use 2M superpages for text/data/bss mappings

Message ID 1456245085-2302-8-git-send-email-andrew.cooper3@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andrew Cooper Feb. 23, 2016, 4:31 p.m. UTC
This balloons the size of Xen in memory from 4.4MB to 8MB, because of the
required alignment adjustments.

However
 * All mappings are 2M superpages.
 * .text (and .init at boot) are the only sections marked executable.
 * .text and .rodata are marked read-only.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>

v2:
 * .data and .bss are adjcent (from earlier patch), so don't require 2M
   alignment
---
 xen/arch/x86/setup.c     | 38 +++++++++++++++++++++++++++++++++++---
 xen/arch/x86/xen.lds.S   | 27 +++++++++++++++++++++++++++
 xen/include/xen/kernel.h |  7 +++++++
 3 files changed, 69 insertions(+), 3 deletions(-)

Comments

Jan Beulich Feb. 24, 2016, 1:17 p.m. UTC | #1
>>> On 23.02.16 at 17:31, <andrew.cooper3@citrix.com> wrote:
> This balloons the size of Xen in memory from 4.4MB to 8MB, because of the
> required alignment adjustments.

Interesting - on v1 it was 12Mb iirc, and aiui you folded just one
pair of 2M pages, which would yield 10M now; did you perhaps
not account for .text spanning 2 large pages, due to the 1M bias
it starts at? But anyway...

> --- a/xen/include/xen/kernel.h
> +++ b/xen/include/xen/kernel.h
> @@ -65,6 +65,13 @@
>  	1;                                      \
>  })
>  
> +#ifdef CONFIG_X86
> +extern const char __2M_text_start[], __2M_text_end[];
> +extern const char __2M_rodata_start[], __2M_rodata_end[];
> +extern char __2M_init_start[], __2M_init_end[];
> +extern char __2M_rwdata_start[], __2M_rwdata_end[];
> +#endif

I think this would better go into an x86-specific header. After all
there's no point for this patch to touch anything that's not x86-
specific anyway.

Jan
Andrew Cooper Feb. 24, 2016, 1:21 p.m. UTC | #2
On 24/02/16 13:17, Jan Beulich wrote:
>>>> On 23.02.16 at 17:31, <andrew.cooper3@citrix.com> wrote:
>> This balloons the size of Xen in memory from 4.4MB to 8MB, because of the
>> required alignment adjustments.
> Interesting - on v1 it was 12Mb iirc, and aiui you folded just one
> pair of 2M pages, which would yield 10M now; did you perhaps
> not account for .text spanning 2 large pages, due to the 1M bias
> it starts at? But anyway...

The original 12 was me just counting 6 superpages.  I forgot to account
for the 1 released back because of .init (which I suppose is now in the
following patch).  That, combined with the alignment drop results in 4
in-use superpages after boot.

~Andrew
Andrew Cooper Feb. 24, 2016, 1:23 p.m. UTC | #3
On 24/02/16 13:21, Andrew Cooper wrote:
> On 24/02/16 13:17, Jan Beulich wrote:
>>>>> On 23.02.16 at 17:31, <andrew.cooper3@citrix.com> wrote:
>>> This balloons the size of Xen in memory from 4.4MB to 8MB, because of the
>>> required alignment adjustments.
>> Interesting - on v1 it was 12Mb iirc, and aiui you folded just one
>> pair of 2M pages, which would yield 10M now; did you perhaps
>> not account for .text spanning 2 large pages, due to the 1M bias
>> it starts at? But anyway...
> The original 12 was me just counting 6 superpages.  I forgot to account
> for the 1 released back because of .init (which I suppose is now in the
> following patch).  That, combined with the alignment drop results in 4
> in-use superpages after boot.

This is perhaps more clear given the debugging shown in the cover letter.

~Andrew
diff mbox

Patch

diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index cddf954..806fa95 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -920,14 +920,46 @@  void __init noreturn __start_xen(unsigned long mbi_p)
 
             /* The only data mappings to be relocated are in the Xen area. */
             pl2e = __va(__pa(l2_xenmap));
+            /*
+             * Undo the temporary-hooking of the l1_identmap.  __2M_text_start
+             * is contained in this PTE.
+             */
             *pl2e++ = l2e_from_pfn(xen_phys_start >> PAGE_SHIFT,
-                                   PAGE_HYPERVISOR_RWX | _PAGE_PSE);
+                                   PAGE_HYPERVISOR_RX | _PAGE_PSE);
             for ( i = 1; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ )
             {
+                unsigned int flags;
+
                 if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
                     continue;
-                *pl2e = l2e_from_intpte(l2e_get_intpte(*pl2e) +
-                                        xen_phys_start);
+
+                if ( i < l2_table_offset((unsigned long)&__2M_text_end) )
+                {
+                    flags = PAGE_HYPERVISOR_RX | _PAGE_PSE;
+                }
+                else if ( i >= l2_table_offset((unsigned long)&__2M_rodata_start) &&
+                          i <  l2_table_offset((unsigned long)&__2M_rodata_end) )
+                {
+                    flags = PAGE_HYPERVISOR_RO | _PAGE_PSE;
+                }
+                else if ( i >= l2_table_offset((unsigned long)&__2M_init_start) &&
+                          i <  l2_table_offset((unsigned long)&__2M_init_end) )
+                {
+                    flags = PAGE_HYPERVISOR_RWX | _PAGE_PSE;
+                }
+                else if ( (i >= l2_table_offset((unsigned long)&__2M_rwdata_start) &&
+                           i <  l2_table_offset((unsigned long)&__2M_rwdata_end)) )
+                {
+                    flags = PAGE_HYPERVISOR_RW | _PAGE_PSE;
+                }
+                else
+                {
+                    *pl2e = l2e_empty();
+                    continue;
+                }
+
+                *pl2e = l2e_from_paddr(
+                    l2e_get_paddr(*pl2e) + xen_phys_start, flags);
             }
 
             /* Re-sync the stack and then switch to relocated pagetables. */
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index 63dbcff..aadb082 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -38,6 +38,9 @@  SECTIONS
   . = __XEN_VIRT_START;
   __image_base__ = .;
 #endif
+
+  __2M_text_start = .;         /* Start of 2M superpages, mapped RX. */
+
   . = __XEN_VIRT_START + MB(1);
   _start = .;
   .text : {
@@ -50,6 +53,10 @@  SECTIONS
        _etext = .;             /* End of text section */
   } :text = 0x9090
 
+  . = ALIGN(MB(2));
+  __2M_text_end = .;
+
+  __2M_rodata_start = .;       /* Start of 2M superpages, mapped RO. */
   .rodata : {
        /* Bug frames table */
        . = ALIGN(4);
@@ -73,6 +80,10 @@  SECTIONS
 #endif
   } :text
 
+  . = ALIGN(MB(2));
+  __2M_rodata_end = .;
+
+  __2M_init_start = .;         /* Start of 2M superpages, mapped RWX (boot only). */
   . = ALIGN(PAGE_SIZE);             /* Init code and data */
   __init_begin = .;
   .init.text : {
@@ -135,6 +146,10 @@  SECTIONS
   . = ALIGN(PAGE_SIZE);
   __init_end = .;
 
+  . = ALIGN(MB(2));
+  __2M_init_end = .;
+
+  __2M_rwdata_start = .;       /* Start of 2M superpages, mapped RW. */
   . = ALIGN(SMP_CACHE_BYTES);
   .data.read_mostly : {
        /* Exception table */
@@ -183,6 +198,9 @@  SECTIONS
   } :text
   _end = . ;
 
+  . = ALIGN(MB(2));
+  __2M_rwdata_end = .;
+
 #ifdef EFI
   . = ALIGN(4);
   .reloc : {
@@ -229,4 +247,13 @@  ASSERT(__image_base__ > XEN_VIRT_START ||
 ASSERT(kexec_reloc_size - kexec_reloc <= PAGE_SIZE, "kexec_reloc is too large")
 #endif
 
+ASSERT(IS_ALIGNED(__2M_text_start,   MB(2)), "__2M_text_start misaligned")
+ASSERT(IS_ALIGNED(__2M_text_end,     MB(2)), "__2M_text_end misaligned")
+ASSERT(IS_ALIGNED(__2M_rodata_start, MB(2)), "__2M_rodata_start misaligned")
+ASSERT(IS_ALIGNED(__2M_rodata_end,   MB(2)), "__2M_rodata_end misaligned")
+ASSERT(IS_ALIGNED(__2M_init_start,   MB(2)), "__2M_init_start misaligned")
+ASSERT(IS_ALIGNED(__2M_init_end,     MB(2)), "__2M_init_end misaligned")
+ASSERT(IS_ALIGNED(__2M_rwdata_start, MB(2)), "__2M_rwdata_start misaligned")
+ASSERT(IS_ALIGNED(__2M_rwdata_end,   MB(2)), "__2M_rwdata_end misaligned")
+
 ASSERT(IS_ALIGNED(cpu0_stack, STACK_SIZE), "cpu0_stack misaligned")
diff --git a/xen/include/xen/kernel.h b/xen/include/xen/kernel.h
index 548b64d..8a48334 100644
--- a/xen/include/xen/kernel.h
+++ b/xen/include/xen/kernel.h
@@ -65,6 +65,13 @@ 
 	1;                                      \
 })
 
+#ifdef CONFIG_X86
+extern const char __2M_text_start[], __2M_text_end[];
+extern const char __2M_rodata_start[], __2M_rodata_end[];
+extern char __2M_init_start[], __2M_init_end[];
+extern char __2M_rwdata_start[], __2M_rwdata_end[];
+#endif
+
 extern char _start[], _end[], start[];
 #define is_kernel(p) ({                         \
     char *__p = (char *)(unsigned long)(p);     \