x86/pvh: copy data from low 1MB to Dom0 physmap instead of mapping it
PATCH 1/1 x86/pvh: copy data from low 1MB to Dom0 physmap instead of mapping it
Identity mapping RAM regions on the low 1MB for Dom0 is not ideal, since there's data there that could be used by Xen during runtime (like the AP trampoline), so instead of identity mapping the low 1MB into the Dom0 physmap populate those RAM regions and copy the data.
Note that this allows to remove unshare_xen_page_with_guest since the only caller was the PVH Dom0 builder.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
--- xen/arch/x86/hvm/dom0_build.c | 51 +++++++++++------------------------ xen/arch/x86/mm.c | 16 ----------- xen/include/xen/mm.h | 1 - 3 files changed, 16 insertions(+), 52 deletions(-)
xen/arch/x86/hvm/dom0_build.c +16 -35
@@ -278,33 +278,6 @@ static int __init pvh_setup_vmx_realmode_helpers(struct domain *d)return 0 ; } -/* Assign the low 1MB to Dom0. */ -static void __init pvh_steal_low_ram (struct domain *d,unsigned long start, -unsigned long nr_pages) -{ -unsigned long mfn; - -ASSERT (start + nr_pages <=PFN_DOWN (MB (1 ))); - -for ( mfn = start; mfn < start + nr_pages; mfn++ ) - { -struct page_info *pg =mfn_to_page (_mfn (mfn)); -int rc; - - rc =unshare_xen_page_with_guest (pg, dom_io); -if ( rc ) - { -printk ("Unable to unshare Xen mfn %#lx: %d\n" , mfn, rc); -continue ; - } - -share_xen_page_with_guest (pg, d, SHARE_rw); - rc =guest_physmap_add_entry (d,_gfn (mfn),_mfn (mfn),0 , p2m_ram_rw); -if ( rc ) -printk ("Unable to add mfn %#lx to p2m: %d\n" , mfn, rc); - } -} -static __init void pvh_setup_e820 (struct domain *d,unsigned long nr_pages) {struct e820entry *entry, *entry_guest;
@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d) addr =PFN_DOWN (d->arch.e820[i].addr); size =PFN_DOWN (d->arch.e820[i].size); -if ( addr >=MB1_PAGES ) - rc =pvh_populate_memory_range (d, addr, size); -else - { -ASSERT (addr + size <MB1_PAGES ); -pvh_steal_low_ram (d, addr, size); - } - + rc =pvh_populate_memory_range (d, addr, size);if ( rc )return rc; + +if ( addr <MB1_PAGES ) + { +enum hvm_translation_result res = +hvm_copy_to_guest_phys (mfn_to_maddr (_mfn (addr)), +mfn_to_virt (addr), size <<PAGE_SHIFT , + v); + +if ( res != HVMTRANS_okay ) + { +printk ("Failed to copy [%#lx, %#lx): %d\n" , + addr, addr + size, res); +return -EFAULT ; + } + } }if ( cpu_has_vmx &&paging_mode_hap (d) && !vmx_unrestricted_guest (v) )
xen/arch/x86/mm.c +0 -16
@@ -511,22 +511,6 @@ void share_xen_page_with_guest(struct page_info *page, struct domain *d,spin_unlock (&d->page_alloc_lock); } -int __init unshare_xen_page_with_guest (struct page_info *page, -struct domain *d) -{ -if (page_get_owner (page) != d || !is_xen_heap_page (page) ) -return -EINVAL ; - -if (test_and_clear_bit (_PGC_allocated, &page->count_info) ) -put_page (page); - -/* Remove the owner and clear the flags. */ - page->u.inuse.type_info =0 ; -page_set_owner (page,NULL ); - -return 0 ; -} -void free_shared_domheap_page (struct page_info *page) {if (test_and_clear_bit (_PGC_allocated, &page->count_info) )
xen/include/xen/mm.h +0 -1
@@ -645,7 +645,6 @@ enum XENSHARE_flags { };void share_xen_page_with_guest (struct page_info *page,struct domain *d,enum XENSHARE_flags flags); -int unshare_xen_page_with_guest (struct page_info *page,struct domain *d);static inline void share_xen_page_with_privileged_guests (struct page_info *page,enum XENSHARE_flags flags)
Signature · 7 lines
2.18.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines · Roger Pau Monne
Identity mapping RAM regions on the low 1MB for Dom0 is not ideal, since there's data there that could be used by Xen during runtime (like the AP trampoline), so instead of identity mapping the low 1MB into the Dom0 physmap populate those RAM regions and copy the data.
I assume you encountered some real issues or is it just precaution?
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d)
Quoted 60 lines
Note that this allows to remove
unshare_xen_page_with_guestsince the only caller was the PVH Dom0 builder.Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>--- xen/arch/x86/hvm/dom0_build.c | 51 +++++++++++------------------------ xen/arch/x86/mm.c | 16 ----------- xen/include/xen/mm.h | 1 - 3 files changed, 16 insertions(+), 52 deletions(-)xen/arch/x86/hvm/dom0_build.c +0
-27@@ -278,33 +278,6 @@ static int __init pvh_setup_vmx_realmode_helpers(struct domain *d)return 0 ; } -/* Assign the low 1MB to Dom0. */ -static void __init pvh_steal_low_ram (struct domain *d,unsigned long start, -unsigned long nr_pages) -{ -unsigned long mfn; - -ASSERT (start + nr_pages <=PFN_DOWN (MB (1 ))); - -for ( mfn = start; mfn < start + nr_pages; mfn++ ) - { -struct page_info *pg =mfn_to_page (_mfn (mfn)); -int rc; - - rc =unshare_xen_page_with_guest (pg, dom_io); -if ( rc ) - { -printk ("Unable to unshare Xen mfn %#lx: %d\n" , mfn, rc); -continue ; - } - -share_xen_page_with_guest (pg, d, SHARE_rw); - rc =guest_physmap_add_entry (d,_gfn (mfn),_mfn (mfn),0 , p2m_ram_rw); -if ( rc ) -printk ("Unable to add mfn %#lx to p2m: %d\n" , mfn, rc); - } -} -static __init void pvh_setup_e820 (struct domain *d,unsigned long nr_pages) {struct e820entry *entry, *entry_guest;@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d)
Somewhere above the hunk you modified, there is a comment saying "Memory below 1MB is identity mapped". Don't you need to change that as well? Otherwise first 1MB still 1:1 maps to 1MB machine memory in guest p2m.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 7 lines · Wei Liu
Quoted 4 lines · Roger Pau Monne
Identity mapping RAM regions on the low 1MB for Dom0 is not ideal, since there's data there that could be used by Xen during runtime (like the AP trampoline), so instead of identity mapping the low 1MB into the Dom0 physmap populate those RAM regions and copy the data.
I assume you encountered some real issues or is it just precaution?
It's simpler, and allowing Dom0 to modify the Xen AP trampoline code is not a good idea.
Quoted 57 lines
Quoted 57 lines
Note that this allows to remove
unshare_xen_page_with_guestsince the only caller was the PVH Dom0 builder.Signed-off-by Roger Pau Monné <[email protected]>---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>--- xen/arch/x86/hvm/dom0_build.c | 51 +++++++++++------------------------ xen/arch/x86/mm.c | 16 ----------- xen/include/xen/mm.h | 1 - 3 files changed, 16 insertions(+), 52 deletions(-)xen/arch/x86/hvm/dom0_build.c +0
-27@@ -278,33 +278,6 @@ static int __init pvh_setup_vmx_realmode_helpers(struct domain *d)return 0 ; } -/* Assign the low 1MB to Dom0. */ -static void __init pvh_steal_low_ram (struct domain *d,unsigned long start, -unsigned long nr_pages) -{ -unsigned long mfn; - -ASSERT (start + nr_pages <=PFN_DOWN (MB (1 ))); - -for ( mfn = start; mfn < start + nr_pages; mfn++ ) - { -struct page_info *pg =mfn_to_page (_mfn (mfn)); -int rc; - - rc =unshare_xen_page_with_guest (pg, dom_io); -if ( rc ) - { -printk ("Unable to unshare Xen mfn %#lx: %d\n" , mfn, rc); -continue ; - } - -share_xen_page_with_guest (pg, d, SHARE_rw); - rc =guest_physmap_add_entry (d,_gfn (mfn),_mfn (mfn),0 , p2m_ram_rw); -if ( rc ) -printk ("Unable to add mfn %#lx to p2m: %d\n" , mfn, rc); - } -} -static __init void pvh_setup_e820 (struct domain *d,unsigned long nr_pages)
Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d)
Quoted 3 lines
xen/arch/x86/hvm/dom0_build.c +0
-0{struct e820entry *entry, *entry_guest;@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d)Somewhere above the hunk you modified, there is a comment saying "Memory below 1MB is identity mapped". Don't you need to change that as well? Otherwise first 1MB still 1:1 maps to 1MB machine memory in guest p2m.
Non-RAM regions are still identity mapped.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Wei Liu
Reviewed-byOn Mon, Sep 17, 2018 at 12:34:19PM +0200, Roger Pau Monné wrote:
[...]
Quoted 11 lines
Quoted 9 lines
Review static int __init pvh_setup_p2m(struct domain *d)
Quoted 4 lines
static
__initvoidpvh_setup_e820(struct domain *d, unsigned long nr_pages){ struct e820entry *entry, *entry_guest;(quoted diff) +0
-0@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d)Somewhere above the hunk you modified, there is a comment saying "Memory below 1MB is identity mapped". Don't you need to change that as well? Otherwise first 1MB still 1:1 maps to 1MB machine memory in guest p2m.
Non-RAM regions are still identity mapped.
I see. Got confused because it said "memory".
May I suggest you update the comment a bit to be more precise, something like:
/*
* Region below 1MB is identity mapped here, but RAM below 1MB will be
* populated later when processing E820 map.
*/
In any case, the code looks fine:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines · Roger Pau Monne
Identity mapping RAM regions on the low 1MB for Dom0 is not ideal, since there's data there that could be used by Xen during runtime (like the AP trampoline), so instead of identity mapping the low 1MB into the Dom0 physmap populate those RAM regions and copy the data.
Note that this allows to remove
unshare_xen_page_with_guestsince the only caller was the PVH Dom0 builder.Signed-off-by Roger Pau Monné <[email protected]>
And you seemed to have made a typo in your own email address.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines · Roger Pau Monne
Identity mapping RAM regions on the low 1MB for Dom0 is not ideal, since there's data there that could be used by Xen during runtime (like the AP trampoline), so instead of identity mapping the low 1MB into the Dom0 physmap populate those RAM regions and copy the data.
Note that this allows to remove
unshare_xen_page_with_guestsince the only caller was the PVH Dom0 builder.Signed-off-by Roger Pau Monné <[email protected]>
Removing unshare_xen_page_with_guest() once it's not used:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 14.09.18 at 13:16, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +7
-8@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d) addr =PFN_DOWN (d->arch.e820[i].addr); size =PFN_DOWN (d->arch.e820[i].size); -if ( addr >=MB1_PAGES ) - rc =pvh_populate_memory_range (d, addr, size); -else - { -ASSERT (addr + size <MB1_PAGES ); -pvh_steal_low_ram (d, addr, size); - } - + rc =pvh_populate_memory_range (d, addr, size);if ( rc )return rc; + +if ( addr <MB1_PAGES ) + { +enum hvm_translation_result res = + hvm_copy_to_guest_phys(mfn_to_maddr(_mfn(addr)), + mfn_to_virt(addr), size << PAGE_SHIFT,
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d) · line 414
Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +9
-0+ v); + +if ( res != HVMTRANS_okay ) + { +printk ("Failed to copy [%#lx, %#lx): %d\n" , + addr, addr + size, res); +return -EFAULT ; + } + } }
Is there any guarantee (in particular on, but not limited to EFI systems) for E820_RAM regions to never span the 1Mb boundary? If not, you may end up copying memory above 1Mb here.
Furthermore, what about RAM / non-RAM boundaries in the middle of a page (which is quite common a situation for the first Mb)?
I also wonder whether it wouldn't be worthwhile to avoid calling modify_identity_mmio() for RAM ranges (which are now going to be re-mapped anyway).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 14.09.18 at 13:16, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +13
-8@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d) addr =PFN_DOWN (d->arch.e820[i].addr); size =PFN_DOWN (d->arch.e820[i].size); -if ( addr >=MB1_PAGES ) - rc =pvh_populate_memory_range (d, addr, size); -else - { -ASSERT (addr + size <MB1_PAGES ); -pvh_steal_low_ram (d, addr, size); - } - + rc =pvh_populate_memory_range (d, addr, size);if ( rc )return rc; + +if ( addr <MB1_PAGES ) + { +enum hvm_translation_result res = +hvm_copy_to_guest_phys (mfn_to_maddr (_mfn (addr)), +mfn_to_virt (addr), size <<PAGE_SHIFT , + v); + +if ( res != HVMTRANS_okay ) + { +printk ("Failed to copy [%#lx, %#lx): %d\n" , + addr, addr + size, res);
Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d) · line 414
Quoted 4 lines
xen/arch/x86/hvm/dom0_build.c +3
-0+return -EFAULT ; + } + } }Is there any guarantee (in particular on, but not limited to EFI systems) for
E820_RAMregions to never span the 1Mb boundary? If not, you may end up copying memory above 1Mb here.
Right, I guess I could do something like:
end = min(MB(1), d->arch.e820[i].addr + d->arch.e820[i].size);
And calculate the size based on the 'end' value.
Quoted 2 lines
Furthermore, what about RAM / non-RAM boundaries in the middle of a page (which is quite common a situation for the first Mb)?
There are no such RAM ranges in the guest memory map because pvh_setup_e820 aligns the RAM regions start/end to page boundaries. This is not ideal, so if you want I can do the following:
hvm_copy_to_guest_phys(d->arch.e820[i].addr, d->arch.e820[i].size, v);
And if pvh_setup_e820 is improved so that RAM regions are no longer aligned to page boundaries the copy will work without issues.
Quoted 3 lines
I also wonder whether it wouldn't be worthwhile to avoid calling
modify_identity_mmio()for RAM ranges (which are now going to be re-mapped anyway).
I think it's easier (code-wise) to identity map the whole area and then just populate the RAM regions as needed.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 17.09.18 at 15:37, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 14.09.18 at 13:16, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +1
-8@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d) addr =PFN_DOWN (d->arch.e820[i].addr); size =PFN_DOWN (d->arch.e820[i].size); -if ( addr >=MB1_PAGES ) - rc =pvh_populate_memory_range (d, addr, size); -else - { -ASSERT (addr + size <MB1_PAGES ); -pvh_steal_low_ram (d, addr, size); - } - + rc =pvh_populate_memory_range (d, addr, size);Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d)
Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c +6
-0if ( rc )return rc; + +if ( addr <MB1_PAGES ) + { +enum hvm_translation_result res = + hvm_copy_to_guest_phys(mfn_to_maddr(_mfn(addr)), + mfn_to_virt(addr), size <<
PAGE_SHIFT,Quoted 11 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d) · line 414
Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +9
-0+ v); + +if ( res != HVMTRANS_okay ) + { +printk ("Failed to copy [%#lx, %#lx): %d\n" , + addr, addr + size, res); +return -EFAULT ; + } + } }Quoted 3 lines
Is there any guarantee (in particular on, but not limited to EFI systems) for
E820_RAMregions to never span the 1Mb boundary? If not, you may end up copying memory above 1Mb here.
Quoted 5 lines
Right, I guess I could do something like:
end =
min(MB(1), d->arch.e820[i].addr + d->arch.e820[i].size);And calculate the size based on the 'end' value.
Right.
Quoted 5 lines
Quoted 2 lines
Furthermore, what about RAM / non-RAM boundaries in the middle of a page (which is quite common a situation for the first Mb)?
There are no such RAM ranges in the guest memory map because
pvh_setup_e820aligns the RAM regions start/end to page boundaries.
Oh, right.
Quoted 6 lines
This is not ideal, so if you want I can do the following:
hvm_copy_to_guest_phys(d->arch.e820[i].addr, d->arch.e820[i].size, v);And if
pvh_setup_e820is improved so that RAM regions are no longer aligned to page boundaries the copy will work without issues.
Hmm, I guess I'm having difficulty understanding what you think the goal ought to be: Would a page part of which is RAM be copied or mapped in your opinion? I think only mapping can possibly work reliably, and with your remark about pvh_setup_e820() I then think no change would be needed.
Quoted 6 lines
Quoted 3 lines
I also wonder whether it wouldn't be worthwhile to avoid calling
modify_identity_mmio()for RAM ranges (which are now going to be re-mapped anyway).I think it's easier (code-wise) to identity map the whole area and then just populate the RAM regions as needed.
Well, okay then.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 40 lines · Jan Beulich
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 17.09.18 at 15:37, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 14.09.18 at 13:16, <[email protected]> wrote:
xen/arch/x86/hvm/dom0_build.c +1
-8@@ -420,16 +393,24 @@ static int __init pvh_setup_p2m(struct domain *d) addr =PFN_DOWN (d->arch.e820[i].addr); size =PFN_DOWN (d->arch.e820[i].size); -if ( addr >=MB1_PAGES ) - rc =pvh_populate_memory_range (d, addr, size); -else - { -ASSERT (addr + size <MB1_PAGES ); -pvh_steal_low_ram (d, addr, size); - } - + rc =pvh_populate_memory_range (d, addr, size);Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d)
Quoted 8 lines
xen/arch/x86/hvm/dom0_build.c +6
-0if ( rc )return rc; + +if ( addr <MB1_PAGES ) + { +enum hvm_translation_result res = + hvm_copy_to_guest_phys(mfn_to_maddr(_mfn(addr)), + mfn_to_virt(addr), size <<
PAGE_SHIFT,Quoted 14 lines
xen/arch/x86/hvm/dom0_build.c static int __init pvh_setup_p2m(struct domain *d) · line 414
Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +9
-0+ v); + +if ( res != HVMTRANS_okay ) + { +printk ("Failed to copy [%#lx, %#lx): %d\n" , + addr, addr + size, res); +return -EFAULT ; + } + } }Is there any guarantee (in particular on, but not limited to EFI systems) for
E820_RAMregions to never span the 1Mb boundary? If not, you may end up copying memory above 1Mb here.
Quoted 7 lines
Quoted 5 lines
Right, I guess I could do something like:
end =
min(MB(1), d->arch.e820[i].addr + d->arch.e820[i].size);And calculate the size based on the 'end' value.
Right.
Let me fix this and resend.
Quoted 11 lines
Quoted 6 lines
Quoted 3 lines
Furthermore, what about RAM / non-RAM boundaries in the middle of a page (which is quite common a situation for the first Mb)?
There are no such RAM ranges in the guest memory map because
pvh_setup_e820aligns the RAM regions start/end to page boundaries.Oh, right.
Quoted 2 lines
This is not ideal, so if you want I can do the following:
Quoted 8 lines
Quoted 4 lines
hvm_copy_to_guest_phys(d->arch.e820[i].addr, d->arch.e820[i].size, v);And if
pvh_setup_e820is improved so that RAM regions are no longer aligned to page boundaries the copy will work without issues.Hmm, I guess I'm having difficulty understanding what you think the goal ought to be: Would a page part of which is RAM be copied or mapped in your opinion?
IMO start of RAM ranges should be ceiled, and the end should be floored so that the resulting range is the same size or smaller, but never greater than the original RAM range.
For example a RAM range like [0x10, 0x2001) will end up as [0x1000, 0x2000), so a page that's only partially RAM loses the RAM part in pvh_setup_e820.
Quoted 3 lines
I think only mapping can possibly work
reliably, and with your remark aboutpvh_setup_e820()I then think no change would be needed.
I agree, only identity mapping can work with such pages.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel