xen/x86: Remap text/data/bss with appropriate permissions
PATCH 1/1 xen/x86: Remap text/data/bss with appropriate permissions
c/s cf39362 "x86: use 2M superpages for text/data/bss mappings" served two purposes; to map the primary code and data with appropriate pagetable permissions (rather than unilaterally RWX), and to reduce the TLB pressure.
The extra alignment exposed a SYSLinux issue, and was partly reverted by c/s 0b8a172 "x86: partially revert use of 2M mappings for hypervisor image".
This change reinstates the pagetable permission improvements while avoiding the 2M alignment issue.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/setup.c | 24 ++++++++++++++++++++++++ xen/arch/x86/xen.lds.S | 16 ++++++++++++++++ 2 files changed, 40 insertions(+)
xen/arch/x86/setup.c +24 -0
@@ -529,9 +529,33 @@ static void noinline init_done(void) }else { +/* Mark .text as RX (avoiding the first 2M superpage). */ +map_pages_to_xen (XEN_VIRT_START +MB (2 ), +PFN_DOWN (__pa (XEN_VIRT_START +MB (2 ))), +PFN_DOWN (__2M_text_end - + (const char *)(XEN_VIRT_START +MB (2 ))), +PAGE_HYPERVISOR_RX ); + +/* Mark .rodata as RO. */ +map_pages_to_xen ((unsigned long )&__2M_rodata_start, +PFN_DOWN (__pa (__2M_rodata_start)), +PFN_DOWN (__2M_rodata_end - __2M_rodata_start), +PAGE_HYPERVISOR_RO ); + +/* Free and reuse .init. */ destroy_xen_mappings ((unsigned long )&__init_begin, (unsigned long )&__init_end);init_xenheap_pages (__pa (__init_begin),__pa (__init_end)); + +/* Mark .data and .bss as RW. */ +map_pages_to_xen ((unsigned long )&__2M_rwdata_start, +PFN_DOWN (__pa (__2M_rwdata_start)), +PFN_DOWN (__2M_rwdata_end - __2M_rwdata_start), +PAGE_HYPERVISOR_RW ); + +/* Drop the remaining mappings in the shattered superpage. */ +destroy_xen_mappings ((unsigned long )&__2M_rwdata_end, +ROUNDUP ((unsigned long )&__2M_rwdata_end,MB (2 ))); }printk ("Freed %ldkB init memory.\n" , (long )(__init_end-__init_begin)>>10 );
xen/arch/x86/xen.lds.S +16 -0
@@ -55,6 +55,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_text_end = .;
@@ -98,6 +100,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_rodata_end = .;
@@ -167,6 +171,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_init_end = .;
@@ -211,6 +217,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_rwdata_end = .;
@@ -269,6 +277,14 @@ 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") +#else +ASSERT(IS_ALIGNED(__2M_text_end, PAGE_SIZE), "__2M_text_end misaligned") +ASSERT(IS_ALIGNED(__2M_rodata_start, PAGE_SIZE), "__2M_rodata_start misaligned") +ASSERT(IS_ALIGNED(__2M_rodata_end, PAGE_SIZE), "__2M_rodata_end misaligned") +ASSERT(IS_ALIGNED(__2M_init_start, PAGE_SIZE), "__2M_init_start misaligned") +ASSERT(IS_ALIGNED(__2M_init_end, PAGE_SIZE), "__2M_init_end misaligned") +ASSERT(IS_ALIGNED(__2M_rwdata_start, PAGE_SIZE), "__2M_rwdata_start misaligned") +ASSERT(IS_ALIGNED(__2M_rwdata_end, PAGE_SIZE), "__2M_rwdata_end misaligned") #endif ASSERT(IS_ALIGNED(cpu0_stack, STACK_SIZE), "cpu0_stack misaligned")
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 1/1 DO NOT APPLY - debug keys for inspecting Xen mappings
--- xen/arch/x86/mm.c | 15 ++++++ xen/arch/x86/traps.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+)
xen/arch/x86/mm.c +15 -0
@@ -123,6 +123,7 @@#include <asm/fixmap.h> #include <asm/io_apic.h> #include <asm/pci.h> +#include <xen/console.h> /* Mapping of the fixmap space needed early. */ l1_pgentry_t __section(".bss.page_aligned") l1_fixmap[L1_PAGETABLE_ENTRIES];
@@ -5635,6 +5636,20 @@ int map_pages_to_xen(l1_pgentry_t *pl1e, ol1e;unsigned int i; +if (XEN_VIRT_START >= virt && +XEN_VIRT_START <= (virt + (nr_mfns *PAGE_SIZE )) ) + { +console_start_sync (); + +printk ("*** %s(%p, %p, %p, %#x)\n" , + __func__,_p (virt),_p (mfn),_p (nr_mfns), flags); +printk ("*** Called from %p %pS\n" , +__builtin_return_address (0 ), +__builtin_return_address (0 )); + +console_end_sync (); + } +#define flush_flags (oldf) do { \ unsigned int o_ = (oldf); \ if ( (o_) & _PAGE_GLOBAL ) \
xen/arch/x86/traps.c +148 -0
154 lines not on this page · raw message
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 13:43, <[email protected]> wrote:
xen/arch/x86/setup.c +15
-0@@ -529,9 +529,33 @@ static void noinline init_done(void) }else { +/* Mark .text as RX (avoiding the first 2M superpage). */ +map_pages_to_xen (XEN_VIRT_START +MB (2 ), +PFN_DOWN (__pa (XEN_VIRT_START +MB (2 ))), +PFN_DOWN (__2M_text_end - + (const char *)(XEN_VIRT_START +MB (2 ))), +PAGE_HYPERVISOR_RX ); + +/* Mark .rodata as RO. */ +map_pages_to_xen ((unsigned long )&__2M_rodata_start, +PFN_DOWN (__pa (__2M_rodata_start)), +PFN_DOWN (__2M_rodata_end - __2M_rodata_start), +PAGE_HYPERVISOR_RO ); + +/* Free and reuse .init. */ destroy_xen_mappings ((unsigned long )&__init_begin, (unsigned long )&__init_end);init_xenheap_pages (__pa (__init_begin),__pa (__init_end)); +
xen/arch/x86/setup.c static void noinline init_done(void) · line 559
Quoted 10 lines
xen/arch/x86/setup.c +9
-0+/* Mark .data and .bss as RW. */ +map_pages_to_xen ((unsigned long )&__2M_rwdata_start, +PFN_DOWN (__pa (__2M_rwdata_start)), +PFN_DOWN (__2M_rwdata_end - __2M_rwdata_start), +PAGE_HYPERVISOR_RW ); + +/* Drop the remaining mappings in the shattered superpage. */ +destroy_xen_mappings ((unsigned long )&__2M_rwdata_end, +ROUNDUP ((unsigned long )&__2M_rwdata_end,MB (2 ))); }
I think this would be more appropriate to add to some __init function (which the discarding of .init.* naturally can't live in).
Also - do we really want to make this code dependent on map_pages_to_xen() not intermediately zapping the mappings being changed?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 13:43, <[email protected]> wrote:
xen/arch/x86/setup.c +19
-0@@ -529,9 +529,33 @@ static void noinline init_done(void) }else { +/* Mark .text as RX (avoiding the first 2M superpage). */ +map_pages_to_xen (XEN_VIRT_START +MB (2 ), +PFN_DOWN (__pa (XEN_VIRT_START +MB (2 ))), +PFN_DOWN (__2M_text_end - + (const char *)(XEN_VIRT_START +MB (2 ))), +PAGE_HYPERVISOR_RX ); + +/* Mark .rodata as RO. */ +map_pages_to_xen ((unsigned long )&__2M_rodata_start, +PFN_DOWN (__pa (__2M_rodata_start)), +PFN_DOWN (__2M_rodata_end - __2M_rodata_start), +PAGE_HYPERVISOR_RO ); + +/* Free and reuse .init. */ destroy_xen_mappings ((unsigned long )&__init_begin, (unsigned long )&__init_end);init_xenheap_pages (__pa (__init_begin),__pa (__init_end)); + +/* Mark .data and .bss as RW. */ + map_pages_to_xen((unsigned long)&__2M_rwdata_start, + PFN_DOWN(__pa(__2M_rwdata_start)), + PFN_DOWN(__2M_rwdata_end - __2M_rwdata_start),
Quoted 8 lines
xen/arch/x86/setup.c static void noinline init_done(void) · line 559
Quoted 6 lines
xen/arch/x86/setup.c +5
-0+ PAGE_HYPERVISOR_RW); + +/* Drop the remaining mappings in the shattered superpage. */ +destroy_xen_mappings ((unsigned long )&__2M_rwdata_end, +ROUNDUP ((unsigned long )&__2M_rwdata_end,MB (2 ))); }I think this would be more appropriate to add to some
__initfunction (which the discarding of .init.* naturally can't live in).
I will see if I can pull it forwards to just after the relocation, to be as close to the permissions change in the 2M case as possible.
Quoted 4 lines
Also - do we really want to make this code dependent on
map_pages_to_xen()not intermediately zapping the mappings being changed?
Do you mean "immediately"?
As far as I can tell, it is guaranteed to be safe, even when remapping the code section. Updates to the live pagetables are using atomic writes, and I didn't spot a point which would end up with a transient non-present mapping.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 15:44, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Also - do we really want to make this code dependent on
map_pages_to_xen()not intermediately zapping the mappings being changed?Do you mean "immediately"?
No.
Quoted 4 lines
As far as I can tell, it is guaranteed to be safe, even when remapping the code section. Updates to the live pagetables are using atomic writes, and I didn't spot a point which would end up with a transient non-present mapping.
But we may, at some point and for whatever reason, come to make the function zap the mapping (i.e. clear the present bit), flush, and only the re-establish the new mapping.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 8 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 15:44, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Also - do we really want to make this code dependent on
map_pages_to_xen()not intermediately zapping the mappings being changed?Do you mean "immediately"?
No.
Quoted 7 lines
Quoted 4 lines
As far as I can tell, it is guaranteed to be safe, even when remapping the code section. Updates to the live pagetables are using atomic writes, and I didn't spot a point which would end up with a transient non-present mapping.
But we may, at some point and for whatever reason, come to make the function zap the mapping (i.e. clear the present bit), flush, and only the re-establish the new mapping.
This change is temporary until I can fix the legacy boot issue and reintroduce the proper 2M functionality.
If someone in the future wants to change the behaviour of map_pages_to_xen() then we can reconsider. However, I think it is unlikely that this will actually happen at all, and if it ever does, I hope to have already fixed the 2M alignment and deleted this change.
This change is a big security improvement, and absolutely should be taken, especially as the current implementation of map_pages_to_xen() is safe.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 17:15, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.03.16 at 15:44, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Also - do we really want to make this code dependent on
map_pages_to_xen()not intermediately zapping the mappings being changed?Do you mean "immediately"?
No.
Quoted 7 lines
Quoted 4 lines
As far as I can tell, it is guaranteed to be safe, even when remapping the code section. Updates to the live pagetables are using atomic writes, and I didn't spot a point which would end up with a transient non-present mapping.
But we may, at some point and for whatever reason, come to make the function zap the mapping (i.e. clear the present bit), flush, and only the re-establish the new mapping.
This change is temporary until I can fix the legacy boot issue and reintroduce the proper 2M functionality.
Quoted 8 lines
If someone in the future wants to change the behaviour of
map_pages_to_xen()then we can reconsider. However, I think it is unlikely that this will actually happen at all, and if it ever does, I hope to have already fixed the 2M alignment and deleted this change.This change is a big security improvement, and absolutely should be taken, especially as the current implementation of
map_pages_to_xen()is safe.
I by no means intend to reject this change just because of this aspect - I merely wanted to make the slight concern explicit.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v2 1/1 xen/x86: Remap text/data/bss with appropriate permissions
In reply to Jan Beulichc/s cf39362 "x86: use 2M superpages for text/data/bss mappings" served two purposes; to map the primary code and data with appropriate pagetable permissions (rather than unilaterally RWX), and to reduce the TLB pressure.
The extra alignment exposed a SYSLinux issue, and was partly reverted by c/s 0b8a172 "x86: partially revert use of 2M mappings for hypervisor image".
This change reinstates the pagetable permission improvements while avoiding the 2M alignment issue.
---
CC: Jan Beulich <[email protected]>
v2:
Move the remapping earlier in the boot sequence. This is where memguart_init() used to reside, which itself used map_pages_to_xen() on the full virtual region.
--- xen/arch/x86/setup.c | 31 +++++++++++++++++++++++++++++++ xen/arch/x86/xen.lds.S | 16 ++++++++++++++++ 2 files changed, 47 insertions(+)
xen/arch/x86/setup.c +31 -0
@@ -1205,6 +1205,37 @@ void __init noreturn __start_xen(unsigned long mbi_p) ~((1UL <<L2_PAGETABLE_SHIFT ) -1 );destroy_xen_mappings (xen_virt_end,XEN_VIRT_START +BOOTSTRAP_MAP_BASE ); +/* +* If not using 2M mappings to gain suitable pagetable permissions +* directly from the relocation above, remap the code/data +* sections with decreased permissions. +*/ +if ( !using_2M_mapping () ) + { +/* Mark .text as RX (avoiding the first 2M superpage). */ +map_pages_to_xen (XEN_VIRT_START +MB (2 ), +PFN_DOWN (__pa (XEN_VIRT_START +MB (2 ))), +PFN_DOWN (__2M_text_end - + (const char *)(XEN_VIRT_START +MB (2 ))), +PAGE_HYPERVISOR_RX ); + +/* Mark .rodata as RO. */ +map_pages_to_xen ((unsigned long )&__2M_rodata_start, +PFN_DOWN (__pa (__2M_rodata_start)), +PFN_DOWN (__2M_rodata_end - __2M_rodata_start), +PAGE_HYPERVISOR_RO ); + +/* Mark .data and .bss as RW. */ +map_pages_to_xen ((unsigned long )&__2M_rwdata_start, +PFN_DOWN (__pa (__2M_rwdata_start)), +PFN_DOWN (__2M_rwdata_end - __2M_rwdata_start), +PAGE_HYPERVISOR_RW ); + +/* Drop the remaining mappings in the shattered superpage. */ +destroy_xen_mappings ((unsigned long )&__2M_rwdata_end, +ROUNDUP ((unsigned long )&__2M_rwdata_end,MB (2 ))); + } + nr_pages =0 ;for ( i =0 ; i < e820.nr_map; i++ )if ( e820.map[i].type ==E820_RAM )
xen/arch/x86/xen.lds.S +16 -0
@@ -56,6 +56,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_text_end = .;
@@ -99,6 +101,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_rodata_end = .;
@@ -168,6 +172,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_init_end = .;
@@ -212,6 +218,8 @@ SECTIONS #ifdef EFI . = ALIGN(MB(2)); +#else + . = ALIGN(PAGE_SIZE); #endif __2M_rwdata_end = .;
@@ -270,6 +278,14 @@ 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") +#else +ASSERT(IS_ALIGNED(__2M_text_end, PAGE_SIZE), "__2M_text_end misaligned") +ASSERT(IS_ALIGNED(__2M_rodata_start, PAGE_SIZE), "__2M_rodata_start misaligned") +ASSERT(IS_ALIGNED(__2M_rodata_end, PAGE_SIZE), "__2M_rodata_end misaligned") +ASSERT(IS_ALIGNED(__2M_init_start, PAGE_SIZE), "__2M_init_start misaligned") +ASSERT(IS_ALIGNED(__2M_init_end, PAGE_SIZE), "__2M_init_end misaligned") +ASSERT(IS_ALIGNED(__2M_rwdata_start, PAGE_SIZE), "__2M_rwdata_start misaligned") +ASSERT(IS_ALIGNED(__2M_rwdata_end, PAGE_SIZE), "__2M_rwdata_end misaligned") #endif ASSERT(IS_ALIGNED(cpu0_stack, STACK_SIZE), "cpu0_stack misaligned")
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel