Guest magic region allocation for 11 Dom0less domUs - Take two
v2 cover Guest magic region allocation for 11 Dom0less domUs - Take two
Hi all,
This series is trying to fix the reported guest magic region allocation issue for 11 Dom0less domUs, an error message can seen from the init-dom0less application on 1:1 direct-mapped Dom0less DomUs:
``` Allocating magic pages memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1 Error on alloc magic pages ```
This is because populate_physmap() automatically assumes gfn == mfn for direct mapped domains. This cannot be true for the magic pages that are allocated later for 1:1 Dom0less DomUs from the init-dom0less helper application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.
In [1] I've tried to fix this issue by the domctl approach, and discussions in [2] and [3] indicates that a domctl is not really necessary, as we can simplify the issue to "allocate the Dom0less guest magic regions at the Dom0less DomU build time and pass the region base PFN to init-dom0less application". Therefore, the first patch in this series will allocate magic pages for Dom0less DomUs, the second patch will store the allocated region base PFN to HVMOP params like HVM_PARAM_CALLBACK_IRQ, and the third patch uses the HVMOP to get the stored guest magic region base PFN to avoid hardcoding GUEST_MAGIC_BASE. The last patch will update documentation.
Gitlab CI for this series can be found in [4].
[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2404251508470.3940@ubuntu-linux-20-04-desktop/
[4] https://gitlab.com/xen-project/people/henryw/xen/-/pipelines/1285727622
Henry Wang (4):
xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less
DomUs
xen/arm: Add new HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} keys in HVMOP tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE docs/features/dom0less: Update the late XenStore init protocol
docs/features/dom0less.pandoc | 8 ++++--- tools/helpers/init-dom0less.c | 40 ++++++++++++++------------------- tools/libs/guest/xg_dom_arm.c | 6 ----- xen/arch/arm/dom0less-build.c | 35 +++++++++++++++++++++++++++++ xen/arch/arm/hvm.c | 2 ++ xen/include/public/arch-arm.h | 6 +++++ xen/include/public/hvm/params.h | 11 ++++++++- 7 files changed, 75 insertions(+), 33 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/4 xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.
An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:
``` Allocating magic pages memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1 Error on alloc magic pages ```
The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.
To solve above issue, this commit allocates hypervisor reserved pages (currently used as the magic pages) for Arm Dom0less DomUs at the domain construction time. The base address/PFN of the region will be noted and communicated to the init-dom0less application in Dom0.
---
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)
--- tools/libs/guest/xg_dom_arm.c | 6 ------ xen/arch/arm/dom0less-build.c | 32 ++++++++++++++++++++++++++++++++ xen/include/public/arch-arm.h | 6 ++++++ 3 files changed, 38 insertions(+), 6 deletions(-)
tools/libs/guest/xg_dom_arm.c +0 -6
@@ -25,12 +25,6 @@#include "xg_private.h" -#define NR_MAGIC_PAGES 4 -#define CONSOLE_PFN_OFFSET 0 -#define XENSTORE_PFN_OFFSET 1 -#define MEMACCESS_PFN_OFFSET 2 -#define VUART_PFN_OFFSET 3 -#define LPAE_SHIFT 9#define PFN_4K_SHIFT (0)
xen/arch/arm/dom0less-build.c +32 -0
@@ -739,6 +739,34 @@ static int __init alloc_xenstore_evtchn(struct domain *d)return 0 ; } +static int __init alloc_magic_pages (struct domain *d) +{ +struct page_info *magic_pg; +mfn_t mfn; +gfn_t gfn; +int rc; + + d->max_pages +=NR_MAGIC_PAGES ; + magic_pg =alloc_domheap_pages (d,get_order_from_pages (NR_MAGIC_PAGES ),0 ); +if ( magic_pg ==NULL ) +return -ENOMEM ; + + mfn =page_to_mfn (magic_pg); +if ( !is_domain_direct_mapped (d) ) + gfn =gaddr_to_gfn (GUEST_MAGIC_BASE ); +else + gfn =gaddr_to_gfn (mfn_to_maddr (mfn)); + + rc =guest_physmap_add_pages (d, gfn, mfn,NR_MAGIC_PAGES ); +if ( rc ) + { +free_domheap_pages (magic_pg,get_order_from_pages (NR_MAGIC_PAGES )); +return rc; + } + +return 0 ; +} +static int __init construct_domU (struct domain *d,const struct dt_device_node *node) {
@@ -840,6 +868,10 @@ static int __init construct_domU(struct domain *d,if ( rc <0 )return rc; d->arch.hvm.params[HVM_PARAM_STORE_PFN ] = ~0ULL ; + + rc =alloc_magic_pages (d); +if ( rc <0 ) +return rc; }return rc;
xen/include/public/arch-arm.h +6 -0
@@ -476,6 +476,12 @@ typedef uint64_t xen_callback_t;#define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000)#define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000) +#define NR_MAGIC_PAGES 4 +#define CONSOLE_PFN_OFFSET 0 +#define XENSTORE_PFN_OFFSET 1 +#define MEMACCESS_PFN_OFFSET 2 +#define VUART_PFN_OFFSET 3 +#define GUEST_RAM_BANKS 2/*
Signature · 1 line
2.34.1
Re: [PATCH v2 1/4] xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
Hi Henry,
Quoted 67 lines · Henry Wang
There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.
An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:
``` Allocating magic pages memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1 Error on alloc magic pages ```The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through
populate_physmap(), andpopulate_physmap()automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.To solve above issue, this commit allocates hypervisor reserved pages (currently used as the magic pages) for Arm Dom0less DomUs at the domain construction time. The base address/PFN of the region will be noted and communicated to the init-dom0less application in Dom0.
Reported-by Alec Kwapis <[email protected]>Suggested-by Daniel P. Smith <[email protected]>Signed-off-by Henry Wang <[email protected]>---
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)--- tools/libs/guest/xg_dom_arm.c | 6 ------ xen/arch/arm/dom0less-build.c | 32 ++++++++++++++++++++++++++++++++ xen/include/public/arch-arm.h | 6 ++++++ 3 files changed, 38 insertions(+), 6 deletions(-)tools/libs/guest/xg_dom_arm.c +0
-6@@ -25,12 +25,6 @@#include "xg_private.h" -#define NR_MAGIC_PAGES 4 -#define CONSOLE_PFN_OFFSET 0 -#define XENSTORE_PFN_OFFSET 1 -#define MEMACCESS_PFN_OFFSET 2 -#define VUART_PFN_OFFSET 3 -#define LPAE_SHIFT 9#define PFN_4K_SHIFT (0)xen/arch/arm/dom0less-build.c +0
-0@@ -739,6 +739,34 @@ static int __init alloc_xenstore_evtchn(struct domain *d)return 0 ;
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 749
Quoted 10 lines
xen/arch/arm/dom0less-build.c +8
-0} +static int __init alloc_magic_pages (struct domain *d) +{ +struct page_info *magic_pg; +mfn_t mfn; +gfn_t gfn; +int rc; + + d->max_pages +=NR_MAGIC_PAGES ;
Here you bump d->max_mages by NR_MAGIC_PAGES but...
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 750
Quoted 1 line
xen/arch/arm/dom0less-build.c +1
-0+ magic_pg =alloc_domheap_pages (d,get_order_from_pages (NR_MAGIC_PAGES ),0 );
... here you will allocate using a power-of-two. Which may end up to fail as there is nothing guaranteeing that NR_MAGIC_PAGES is suitably aligned.
For now NR_MAGIC_PAGES seems suitably aligned, so it BUILD_BUG_ON() woudl be ok.
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 758
Quoted 8 lines
xen/arch/arm/dom0less-build.c +8
-0+if ( magic_pg ==NULL ) +return -ENOMEM ; + + mfn =page_to_mfn (magic_pg); +if ( !is_domain_direct_mapped (d) ) + gfn =gaddr_to_gfn (GUEST_MAGIC_BASE ); +else + gfn =gaddr_to_gfn (mfn_to_maddr (mfn));
Allocating the magic pages contiguously is only necessary for direct mapped domain. For the other it might be preferable to allocate page by page. That said, NR_MAGIC_PAGES is not big enough. So it would be okay.
Quoted 15 lines
xen/arch/arm/dom0less-build.c +11
-0+ + rc =guest_physmap_add_pages (d, gfn, mfn,NR_MAGIC_PAGES ); +if ( rc ) + { +free_domheap_pages (magic_pg,get_order_from_pages (NR_MAGIC_PAGES )); +return rc; + } + +return 0 ; +} +static int __init construct_domU (struct domain *d,const struct dt_device_node *node) {
xen/arch/arm/dom0less-build.c static int __init construct_domU(struct domain *d, · line 874
Quoted 7 lines
xen/arch/arm/dom0less-build.c +4
-0@@ -840,6 +868,10 @@ static int __init construct_domU(struct domain *d,if ( rc <0 )return rc; d->arch.hvm.params[HVM_PARAM_STORE_PFN ] = ~0ULL ; + + rc =alloc_magic_pages (d); +if ( rc <0 ) +return rc;
This will only be allocated xenstore is enabled. But I don't think some of the magic pages really require xenstore to work. In the future we may need some more fine graine choice (see my comment in patch #2 as well).
xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 479
Quoted 12 lines
xen/arch/arm/dom0less-build.c +0
-0}return rc;xen/include/public/arch-arm.h +1
-0@@ -476,6 +476,12 @@ typedef uint64_t xen_callback_t;#define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000)#define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000) +#define NR_MAGIC_PAGES 4
This name and the other below are far too generic to be part of the shared header. For NR_MAGIC_PAGES, can you explain why GUEST_MAGIC_SIZE cannot be used? Is it because it is "too" big?
For the rest below...
xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 483
Quoted 4 lines
xen/include/public/arch-arm.h +4
-0+#define CONSOLE_PFN_OFFSET 0 +#define XENSTORE_PFN_OFFSET 1 +#define MEMACCESS_PFN_OFFSET 2 +#define VUART_PFN_OFFSET 3
... the order we allocate the magic pages is purely an internal decision of the toolstack. For the rest of the system, they need to access HVM_PARAM_*. So it doesn't feel like they should be part of the shared headers.
If there is a strong reason to include them, then I think they all should be prefixed with GUEST_MAGIC_*.
Cheers,
Quoted 4 lines
xen/include/public/arch-arm.h +1
-0+#define GUEST_RAM_BANKS 2/*
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/4] xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
Hi Julien,
Quoted 80 lines · Julien Grall
Hi Henry,
Quoted 64 lines · Henry Wang
There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.
An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:
``` Allocating magic pages memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1 Error on alloc magic pages ```The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through
populate_physmap(), andpopulate_physmap()automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.To solve above issue, this commit allocates hypervisor reserved pages (currently used as the magic pages) for Arm Dom0less DomUs at the domain construction time. The base address/PFN of the region will be noted and communicated to the init-dom0less application in Dom0.
Reported-by Alec Kwapis <[email protected]>Suggested-by Daniel P. Smith <[email protected]>Signed-off-by Henry Wang <[email protected]>---
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)
---tools/libs/guest/xg_dom_arm.c | 6 ------ xen/arch/arm/dom0less-build.c | 32 ++++++++++++++++++++++++++++++++ xen/include/public/arch-arm.h | 6 ++++++ 3 files changed, 38 insertions(+), 6 deletions(-)tools/libs/guest/xg_dom_arm.c +0
-0@@ -25,12 +25,6 @@ #include "xg_private.h"-#define
NR_MAGIC_PAGES4tools/libs/guest/xg_dom_arm.c +0
-5-#define CONSOLE_PFN_OFFSET 0 -#define XENSTORE_PFN_OFFSET 1 -#define MEMACCESS_PFN_OFFSET 2 -#define VUART_PFN_OFFSET 3 -#define
LPAE_SHIFT9
#define PFN_4K_SHIFT (0)xen/arch/arm/dom0less-build.c +0
-0@@ -739,6 +739,34 @@ static int __init alloc_xenstore_evtchn(struct domain *d)xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct · line 749
Quoted 10 lines
xen/arch/arm/dom0less-build.c +7
-0return 0; } +static int __init alloc_magic_pages(struct domain *d) +{ + struct page_info *magic_pg; + mfn_t mfn; + gfn_t gfn; + int rc; + + d->max_pages += NR_MAGIC_PAGES;Here you bump
d->max_magesbyNR_MAGIC_PAGESbut...
Quoted 6 lines
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct · line 751
Quoted 2 lines
xen/arch/arm/dom0less-build.c +1
-0+ magic_pg = alloc_domheap_pages(d,get_order_from_pages (NR_MAGIC_PAGES ),0 );... here you will allocate using a power-of-two. Which may end up to fail as there is nothing guaranteeing that
NR_MAGIC_PAGESis suitably aligned.For now
NR_MAGIC_PAGESseems suitably aligned, so itBUILD_BUG_ON()woudl be ok.
Great catch! I will add BUILD_BUG_ON(NR_MAGIC_PAGES & (NR_MAGIC_PAGES - 1)); Thanks.
Quoted 28 lines
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 758
Quoted 8 lines
xen/arch/arm/dom0less-build.c +8
-0+ if ( magic_pg == NULL ) + return -ENOMEM; + + mfn = page_to_mfn(magic_pg); + if ( !is_domain_direct_mapped(d) ) + gfn = gaddr_to_gfn(GUEST_MAGIC_BASE); + else + gfn = gaddr_to_gfn(mfn_to_maddr(mfn));Allocating the magic pages contiguously is only necessary for direct mapped domain. For the other it might be preferable to allocate page by page. That said,
NR_MAGIC_PAGESis not big enough. So it would be okay.Quoted 17 lines
xen/arch/arm/dom0less-build.c +11
-0+ + rc = guest_physmap_add_pages(d, gfn, mfn, NR_MAGIC_PAGES); + if ( rc ) + { + free_domheap_pages(magic_pg,get_order_from_pages (NR_MAGIC_PAGES )); + return rc; + } + + return 0; +} +static int
__initconstruct_domU(struct domain *d,
const structdt_device_node*node) {xen/arch/arm/dom0less-build.c +0
-0@@ -840,6 +868,10 @@ static int __init construct_domU(struct domain *d, if ( rc < 0 )
Quoted 8 lines
xen/arch/arm/dom0less-build.c static int __init construct_domU(struct domain *d, · line 874
Quoted 6 lines
xen/arch/arm/dom0less-build.c +4
-0return rc; d->arch.hvm.params[HVM_PARAM_STORE_PFN] = ~0ULL; + + rc = alloc_magic_pages(d); + if ( rc < 0 ) + return rc;This will only be allocated xenstore is enabled. But I don't think some of the magic pages really require xenstore to work. In the future we may need some more fine graine choice (see my comment in patch #2 as well).
Sorry, but it seems that by the time that I am writing this reply, I didn't get the email for patch #2 comment. I will reply both together when I see it.
Quoted 11 lines
Quoted 10 lines
xen/arch/arm/dom0less-build.c +0
-0} return rc;xen/include/public/arch-arm.h +0
-0@@ -476,6 +476,12 @@ typedef uint64_t xen_callback_t; #define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000) #define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000)+#define
NR_MAGIC_PAGES4This name and the other below are far too generic to be part of the shared header. For
NR_MAGIC_PAGES, can you explain whyGUEST_MAGIC_SIZEcannot be used? Is it because it is "too" big?
Yes, I don't really want to allocate whole 16MB when we merely need 4 pages.
Quoted 10 lines
For the rest below...
xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 483
Quoted 4 lines
xen/include/public/arch-arm.h +4
-0+#define CONSOLE_PFN_OFFSET 0 +#define XENSTORE_PFN_OFFSET 1 +#define MEMACCESS_PFN_OFFSET 2 +#define VUART_PFN_OFFSET 3... the order we allocate the magic pages is purely an internal decision of the toolstack. For the rest of the system, they need to access HVM_PARAM_*. So it doesn't feel like they should be part of the shared headers.
If there is a strong reason to include them, then I think they all should be prefixed with GUEST_MAGIC_*.
One of the benefits as Michal pointed out in comments for v1 [1] would be this would also allow init-dom0less.h not to re-define XENSTORE_PFN_OFFSET. I will rename them as suggested if both of you are ok with moving them to the arch header.
[1] https://lore.kernel.org/xen-devel/[email protected]/
Kind regards,
Henry
Re: [PATCH v2 1/4] xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
Hi Henry,
Quoted 26 lines · Henry Wang
Quoted 17 lines
Quoted 17 lines
+
+ rc =guest_physmap_add_pages(d, gfn, mfn, NR_MAGIC_PAGES); + if ( rc )
+ {
+free_domheap_pages(magic_pg, get_order_from_pages(NR_MAGIC_PAGES));
+ return rc;
+ }xen/arch/arm/dom0less-build.c +4
-0+ + return 0; +} +static int
__initconstruct_domU(struct domain *d,
const structdt_device_node*node) {xen/arch/arm/dom0less-build.c +0
-0@@ -840,6 +868,10 @@ static int __init construct_domU(struct domain *d, if ( rc < 0 )Quoted 8 lines
xen/arch/arm/dom0less-build.c static int __init construct_domU(struct domain *d, · line 874
Quoted 6 lines
xen/arch/arm/dom0less-build.c +4
-0return rc; d->arch.hvm.params[HVM_PARAM_STORE_PFN] = ~0ULL; + + rc = alloc_magic_pages(d); + if ( rc < 0 ) + return rc;This will only be allocated xenstore is enabled. But I don't think some of the magic pages really require xenstore to work. In the future we may need some more fine graine choice (see my comment in patch #2 as well).
Quoted 1 line
Sorry, but it seems that by the time that I am writing this reply, I didn't get the email for patch #2 comment. I will reply both together when I see it.
That was expected, I knew what I wanted to mention about patch #2 but the e-mail was not fully written. You should have it in your inbox now.
Quoted 13 lines
Quoted 12 lines
Quoted 11 lines
xen/arch/arm/dom0less-build.c +0
-0} return rc;xen/include/public/arch-arm.h +0
-0@@ -476,6 +476,12 @@ typedef uint64_t xen_callback_t; #define GUEST_MAGIC_BASE xen_mk_ullong(0x39000000) #define GUEST_MAGIC_SIZE xen_mk_ullong(0x01000000)+#define
NR_MAGIC_PAGES4This name and the other below are far too generic to be part of the shared header. For
NR_MAGIC_PAGES, can you explain whyGUEST_MAGIC_SIZEcannot be used? Is it because it is "too" big?
Quoted 1 line
Yes, I don't really want to allocate whole 16MB when we merely need 4 pages.
What about updating GUEST_MAGIC_SIZE to 0x4000? It doesn't make any sense to have two define which have pretty much the same meaning.
Then on the toolstack side, you can check that NR_MAGIC_PAGES is smaller or equal to GUEST_MAGIC_SIZE.
Quoted 10 lines
Quoted 10 lines
For the rest below...
xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 483
Quoted 4 lines
xen/include/public/arch-arm.h +4
-0+#define CONSOLE_PFN_OFFSET 0 +#define XENSTORE_PFN_OFFSET 1 +#define MEMACCESS_PFN_OFFSET 2 +#define VUART_PFN_OFFSET 3... the order we allocate the magic pages is purely an internal decision of the toolstack. For the rest of the system, they need to access HVM_PARAM_*. So it doesn't feel like they should be part of the shared headers.
Quoted 3 lines
Quoted 1 line
If there is a strong reason to include them, then I think they all should be prefixed with GUEST_MAGIC_*.
One of the benefits as Michal pointed out in comments for v1 [1] would be this would also allow
init-dom0less.hnot to re-defineXENSTORE_PFN_OFFSET.
At the moment, yes they have the same values. But with series, XENSTORE_PFN_OFFSET should technically be 0 in init-dom0less.
This is because Xen may only allocate one page (you don't need 4) for the reserved area. So...
Quoted 1 line
I will rename them as suggested if both of you are ok with moving them to the arch header.
... I don't think they should be shared.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/4] xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
In reply to Henry WangHi Henry,
Quoted 76 lines · Henry Wang
There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.
An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:
``` Allocating magic pages memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1 Error on alloc magic pages ```The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through
populate_physmap(), andpopulate_physmap()automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.To solve above issue, this commit allocates hypervisor reserved pages (currently used as the magic pages) for Arm Dom0less DomUs at the domain construction time. The base address/PFN of the region will be noted and communicated to the init-dom0less application in Dom0.
Reported-by Alec Kwapis <[email protected]>Suggested-by Daniel P. Smith <[email protected]>Signed-off-by Henry Wang <[email protected]>---
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)--- tools/libs/guest/xg_dom_arm.c | 6 ------ xen/arch/arm/dom0less-build.c | 32 ++++++++++++++++++++++++++++++++ xen/include/public/arch-arm.h | 6 ++++++ 3 files changed, 38 insertions(+), 6 deletions(-)tools/libs/guest/xg_dom_arm.c +0
-6@@ -25,12 +25,6 @@#include "xg_private.h" -#define NR_MAGIC_PAGES 4 -#define CONSOLE_PFN_OFFSET 0 -#define XENSTORE_PFN_OFFSET 1 -#define MEMACCESS_PFN_OFFSET 2 -#define VUART_PFN_OFFSET 3 -#define LPAE_SHIFT 9#define PFN_4K_SHIFT (0)xen/arch/arm/dom0less-build.c +7
-0@@ -739,6 +739,34 @@ static int __init alloc_xenstore_evtchn(struct domain *d)return 0 ; } +static int __init alloc_magic_pages (struct domain *d) +{ +struct page_info *magic_pg; +mfn_t mfn; +gfn_t gfn; +int rc; +
xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 758
Quoted 10 lines
xen/arch/arm/dom0less-build.c +10
-0+ d->max_pages +=NR_MAGIC_PAGES ; + magic_pg =alloc_domheap_pages (d,get_order_from_pages (NR_MAGIC_PAGES ),0 ); +if ( magic_pg ==NULL ) +return -ENOMEM ; + + mfn =page_to_mfn (magic_pg); +if ( !is_domain_direct_mapped (d) ) + gfn =gaddr_to_gfn (GUEST_MAGIC_BASE ); +else + gfn =gaddr_to_gfn (mfn_to_maddr (mfn));
Summarizing the discussion we had on Matrix. Regions like the extend area and shared memory may not be direct mapped. So unfortunately, I think it is possible that the GFN could clash with one of those.
At least in the shared memory case, the user can provide the address. But as you use the domheap allocator, the address returned could easily change if you tweak your setup.
I am not entirely sure what's the best solution. We could ask the user to provide the information for reserved region. But it feels like we are exposing a bit too much to the user.
So possibly we would want to use the same approach as extended regions. Once we processed all the mappings, find some space for the hypervisor regions.
Any other suggestions?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/4] xen/arm: Alloc hypervisor reserved pages as magic pages for Dom0less DomUs
Hi Julien,
Quoted 24 lines · Julien Grall
Hi Henry,
Quoted 10 lines · Henry Wang
+static int
__initalloc_magic_pages(struct domain *d)xen/arch/arm/dom0less-build.c +8
-0+{ + struct page_info *magic_pg; + mfn_t mfn; + gfn_t gfn; + int rc; + + d->max_pages += NR_MAGIC_PAGES; + magic_pg = alloc_domheap_pages(d,
get_order_from_pages(NR_MAGIC_PAGES), 0);Quoted 8 lines
+ if (
magic_pg== NULL )
+ return-ENOMEM;
+
+ mfn =page_to_mfn(magic_pg);
+ if ( !is_domain_direct_mapped(d) )
+ gfn =gaddr_to_gfn(GUEST_MAGIC_BASE);
+ else
+ gfn = gaddr_to_gfn(mfn_to_maddr(mfn));Summarizing the discussion we had on Matrix. Regions like the extend area and shared memory may not be direct mapped. So unfortunately, I think it is possible that the GFN could clash with one of those.
Quoted 5 lines
At least in the shared memory case, the user can provide the address. But as you use the domheap allocator, the address returned could easily change if you tweak your setup.
I am not entirely sure what's the best solution. We could ask the user to provide the information for reserved region. But it feels like we are exposing a bit too much to the user.
So possibly we would want to use the same approach as extended regions. Once we processed all the mappings, find some space for the hypervisor regions.
One thing that I noticed when I re-visit the extended region finding code from the hypervisor side is:
When the domain is direct-mapped, when we find extended region for the domain, we either use find_unallocated_memory() or find_memory_holes(). It looks like the removal of shared memory regions in both functions uses the paddr parsed from the device tree to remove the regions, which indicates there is an assumption that when a domain is direct-mapped, the shared memory should also be direct-mapped. I might be wrong, but otherwise I don't think the extended region finding logic will carve out the correct shared memory region gpaddr range for guests.
So I think we are missing the documentation (and the corresponding checking when we parse the device tree) for above assumption for the static shared memory, i.e., when the domain is direct-mapped, the static shared memory should also be direct-mapped, and user should make sure this is satisfied in the device tree otherwise Xen should complain.
If we add this assumption and related checking code, I think your concern of clashing with static shared memory can be addressed. Do you agree?
Kind regards,
Henry
Quoted 5 lines
Any other suggestions?
Cheers,
PATCH v2 2/4 xen/arm: Add new HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} keys in HVMOP
In reply to Henry WangFor use cases such as Dom0less PV drivers, a mechanism to communicate Dom0less DomU's static data with the runtime control plane (Dom0) is needed. Since on Arm HVMOP is already the existing approach to address such use cases (for example the allocation of HVM_PARAM_CALLBACK_IRQ), add new HVMOP keys HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} for storing the hypervisor reserved pages region base PFN and size.
Currently, the hypervisor reserved pages region is used as the Arm Dom0less DomU guest magic pages region. Therefore protect the HVMOP keys with "#if defined(__arm__) || defined(__aarch64__)". The values will be set at Dom0less DomU construction time after Dom0less DomU's magic pages region has been allocated.
---
v2:
- Rename the HVMOP keys to HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE}. (Daniel)
- Add comment on top of HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} to describe its usage. Protect them with #ifdef. (Daniel, Jan)
--- xen/arch/arm/dom0less-build.c | 3 +++ xen/arch/arm/hvm.c | 2 ++ xen/include/public/hvm/params.h | 11 ++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-)
xen/arch/arm/dom0less-build.c +3 -0
@@ -764,6 +764,9 @@ static int __init alloc_magic_pages(struct domain *d)return rc; } + d->arch.hvm.params[HVM_PARAM_HV_RSRV_BASE_PFN ] =gfn_x (gfn); + d->arch.hvm.params[HVM_PARAM_HV_RSRV_SIZE ] =NR_MAGIC_PAGES ; +return 0 ; }
xen/arch/arm/hvm.c +2 -0
@@ -55,6 +55,8 @@ static int hvm_allow_get_param(const struct domain *d, unsigned int param)case HVM_PARAM_STORE_EVTCHN :case HVM_PARAM_CONSOLE_PFN :case HVM_PARAM_CONSOLE_EVTCHN : +case HVM_PARAM_HV_RSRV_BASE_PFN : +case HVM_PARAM_HV_RSRV_SIZE :return 0 ;/*
xen/include/public/hvm/params.h +10 -1
@@ -296,6 +296,15 @@#define XEN_HVM_MCA_CAP_LMCE (xen_mk_ullong(1) << 0)#define XEN_HVM_MCA_CAP_MASK XEN_HVM_MCA_CAP_LMCE -#define HVM_NR_PARAMS 39 +/* +* Base PFN and number of pages of the hypervisor reserved pages region. +* Currently only used on Arm for Dom0less DomUs as guest magic pages region. +*/ +#if defined(__arm__) || defined(__aarch64__) +#define HVM_PARAM_HV_RSRV_BASE_PFN 39 +#define HVM_PARAM_HV_RSRV_SIZE 40 +#endif + +#define HVM_NR_PARAMS 41#endif /* __XEN_PUBLIC_HVM_PARAMS_H__ */
Signature · 1 line
2.34.1
Re: [PATCH v2 2/4] xen/arm: Add new HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} keys in HVMOP
Hi Henry,
Quoted 45 lines · Henry Wang
For use cases such as Dom0less PV drivers, a mechanism to communicate Dom0less DomU's static data with the runtime control plane (Dom0) is needed. Since on Arm HVMOP is already the existing approach to address such use cases (for example the allocation of
HVM_PARAM_CALLBACK_IRQ), add new HVMOP keys HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} for storing the hypervisor reserved pages region base PFN and size.Currently, the hypervisor reserved pages region is used as the Arm Dom0less DomU guest magic pages region. Therefore protect the HVMOP keys with "#if
defined(__arm__)||defined(__aarch64__)". The values will be set at Dom0less DomU construction time after Dom0less DomU's magic pages region has been allocated.Reported-by Alec Kwapis <[email protected]>Signed-off-by Henry Wang <[email protected]>---
v2:
- Rename the HVMOP keys to HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE}. (Daniel)
- Add comment on top of HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} to describe its usage. Protect them with #ifdef. (Daniel, Jan)--- xen/arch/arm/dom0less-build.c | 3 +++ xen/arch/arm/hvm.c | 2 ++ xen/include/public/hvm/params.h | 11 ++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-)xen/arch/arm/dom0less-build.c +3
-0@@ -764,6 +764,9 @@ static int __init alloc_magic_pages(struct domain *d)return rc; } + d->arch.hvm.params[HVM_PARAM_HV_RSRV_BASE_PFN ] =gfn_x (gfn); + d->arch.hvm.params[HVM_PARAM_HV_RSRV_SIZE ] =NR_MAGIC_PAGES ; +return 0 ; }xen/arch/arm/hvm.c +0
-0
xen/arch/arm/hvm.c static int hvm_allow_get_param(const struct domain *d, unsigned int param) · line 59
Quoted 5 lines
xen/arch/arm/hvm.c +2
-0@@ -55,6 +55,8 @@ static int hvm_allow_get_param(const struct domain *d, unsigned int param)case HVM_PARAM_STORE_EVTCHN :case HVM_PARAM_CONSOLE_PFN :case HVM_PARAM_CONSOLE_EVTCHN : +case HVM_PARAM_HV_RSRV_BASE_PFN : +case HVM_PARAM_HV_RSRV_SIZE :
We should not allow the guest to read HVM_PARAM_HV_*. So this would need to be handled like HVM_PARAM_HV_*.
But I have some reservation with using HVM params. It means we are setting in stone how we communicate/allocate the reserved pages.
I can see a few issues that may bite back in the future.
We always pre-allocate the reserved pages. But most of the feature may not be used which ends up to be a waste of memory. For instance, you only seem to one page for dom0less, but allocate 4. I understand that libxenguest is doing the same today, however we have the flexibility to change it by just not allocating the page.
Similarly, we expect the hypervisor to provide enough reserved pages for the toolstack to work. Yet, I believe some of the features (e.g. memaccess) doesn't need to be known in advance.
The number of pages are tiny at the moment (4) on Xen. But given the name, I could see someone asking for setting aside more reserved spaces and a single range may start to be a problem for direct mapped domain (unless we don't need to map the magic pages 1:1).
Overall, if we want to make it part of the stable ABI. Then it would be better to provide a (large) reserved space that will be allocated on demand (IOW there is no associated physical memory). If it doesn't work for direct mapped domain, then another solution is to have a toolstack hypercall to allocate a GFN (they don't need to be contiguous) and the backing memory.
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 3/4 tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE
In reply to Henry WangCurrently the GUEST_MAGIC_BASE in the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs.
Since the guest magic region is now allocated from the hypervisor, instead of hardcoding the guest magic pages region, use xc_hvm_param_get() to get the guest magic region PFN, and based on that the XenStore PFN can be calculated. Also, we don't need to set the max mem anymore, so drop the call to xc_domain_setmaxmem(). Rename the alloc_xs_page() to get_xs_page() to reflect the changes.
Take the opportunity to do some coding style improvements when possible.
---
v2:
- Update HVMOP keys name.
--- tools/helpers/init-dom0less.c | 40 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
tools/helpers/init-dom0less.c +17 -23
@@ -19,24 +19,20 @@#define XENSTORE_PFN_OFFSET 1#define STR_MAX_LENGTH 128 -static int alloc_xs_page (struct xc_interface_core *xch, -libxl_dominfo *info, -uint64_t *xenstore_pfn) +static int get_xs_page (struct xc_interface_core *xch,libxl_dominfo *info, +uint64_t *xenstore_pfn) {int rc; -const xen_pfn_t base =GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ; -xen_pfn_t p2m = (GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ) +XENSTORE_PFN_OFFSET ; +xen_pfn_t magic_base_pfn; - rc =xc_domain_setmaxmem (xch, info->domid, - info->max_memkb + (XC_PAGE_SIZE /1024 )); -if (rc <0 ) -return rc; - - rc =xc_domain_populate_physmap_exact (xch, info->domid,1 ,0 ,0 , &p2m); -if (rc <0 ) -return rc; + rc =xc_hvm_param_get (xch, info->domid,HVM_PARAM_HV_RSRV_BASE_PFN , + &magic_base_pfn); +if (rc <0 ) { +printf ("Failed to get HVM_PARAM_HV_RSRV_BASE_PFN\n" ); +return 1 ; + } - *xenstore_pfn = base +XENSTORE_PFN_OFFSET ; + *xenstore_pfn = magic_base_pfn +XENSTORE_PFN_OFFSET ; rc =xc_clear_domain_page (xch, info->domid, *xenstore_pfn);if (rc <0 )return rc;
@@ -100,6 +96,7 @@ static bool do_xs_write_vm(struct xs_handle *xsh, xs_transaction_t t,*/ static int create_xenstore (struct xs_handle *xsh,libxl_dominfo *info,libxl_uuid uuid, +xen_pfn_t xenstore_pfn,evtchn_port_t xenstore_port) {domid_t domid;
@@ -145,8 +142,7 @@ static int create_xenstore(struct xs_handle *xsh, rc =snprintf (target_memkb_str,STR_MAX_LENGTH ,"%" PRIu64, info->current_memkb);if (rc <0 || rc >=STR_MAX_LENGTH )return rc; - rc =snprintf (ring_ref_str,STR_MAX_LENGTH ,"%lld" , - (GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ) +XENSTORE_PFN_OFFSET ); + rc =snprintf (ring_ref_str,STR_MAX_LENGTH ,"%" PRIu_xen_pfn, xenstore_pfn);if (rc <0 || rc >=STR_MAX_LENGTH )return rc; rc =snprintf (xenstore_port_str,STR_MAX_LENGTH ,"%u" , xenstore_port);
@@ -245,9 +241,9 @@ static int init_domain(struct xs_handle *xsh,if (!xenstore_evtchn)return 0 ; -/* Alloc xenstore page */ -if (alloc_xs_page (xch, info, &xenstore_pfn) !=0 ) { -printf ("Error on alloc magic pages\n" ); +/* Get xenstore page */ +if (get_xs_page (xch, info, &xenstore_pfn) !=0 ) { +printf ("Error on getting xenstore page\n" );return 1 ; }
@@ -278,13 +274,11 @@ static int init_domain(struct xs_handle *xsh,if (rc <0 )return rc; - rc =create_xenstore (xsh, info, uuid, xenstore_evtchn); + rc =create_xenstore (xsh, info, uuid, xenstore_pfn, xenstore_evtchn);if (rc)err (1 ,"writing to xenstore" ); - rc =xs_introduce_domain (xsh, info->domid, - (GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ) +XENSTORE_PFN_OFFSET , - xenstore_evtchn); + rc =xs_introduce_domain (xsh, info->domid, xenstore_pfn, xenstore_evtchn);if (!rc)err (1 ,"xs_introduce_domain" );return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH v2 3/4] tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE
Hi Henry,
Quoted 27 lines · Henry Wang
Currently the
GUEST_MAGIC_BASEin the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs.Since the guest magic region is now allocated from the hypervisor, instead of hardcoding the guest magic pages region, use
xc_hvm_param_get()to get the guest magic region PFN, and based on that the XenStore PFN can be calculated. Also, we don't need to set the max mem anymore, so drop the call toxc_domain_setmaxmem(). Rename thealloc_xs_page()toget_xs_page()to reflect the changes.Take the opportunity to do some coding style improvements when possible.
Reported-by Alec Kwapis <[email protected]>Signed-off-by Henry Wang <[email protected]>---
v2:
- Update HVMOP keys name.--- tools/helpers/init-dom0less.c | 40 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 23 deletions(-)tools/helpers/init-dom0less.c +0
-0
tools/helpers/init-dom0less.c line 19
Quoted 1 line
tools/helpers/init-dom0less.c +0
-0@@ -19,24 +19,20 @@#define XENSTORE_PFN_OFFSET 1
Based on previous discussion, I think this wants to be 0 so the hypervisor only need to allocate one page (rather than 2 with the current offset).
Quoted 18 lines
tools/helpers/init-dom0less.c +3
-10#define STR_MAX_LENGTH 128 -static int alloc_xs_page (struct xc_interface_core *xch, -libxl_dominfo *info, -uint64_t *xenstore_pfn) +static int get_xs_page (struct xc_interface_core *xch,libxl_dominfo *info, +uint64_t *xenstore_pfn) {int rc; -const xen_pfn_t base =GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ; -xen_pfn_t p2m = (GUEST_MAGIC_BASE >>XC_PAGE_SHIFT ) +XENSTORE_PFN_OFFSET ; +xen_pfn_t magic_base_pfn; - rc =xc_domain_setmaxmem (xch, info->domid, - info->max_memkb + (XC_PAGE_SIZE /1024 )); -if (rc <0 ) -return rc; -
tools/helpers/init-dom0less.c line 34
Quoted 10 lines
tools/helpers/init-dom0less.c +6
-3- rc =xc_domain_populate_physmap_exact (xch, info->domid,1 ,0 ,0 , &p2m); -if (rc <0 ) -return rc; + rc =xc_hvm_param_get (xch, info->domid,HVM_PARAM_HV_RSRV_BASE_PFN , + &magic_base_pfn); +if (rc <0 ) { +printf ("Failed to get HVM_PARAM_HV_RSRV_BASE_PFN\n" ); +return 1 ; + }
If we go down the route of using HVM_PARAM... then we should avoid to rely on the hypervisor to provide a large enough region. So we should check the offset against the size.
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 4/4 docs/features/dom0less: Update the late XenStore init protocol
In reply to Henry WangWith the new allocation strategy of Dom0less DomUs magic page region, update the documentation of the late XenStore init protocol accordingly.
---
v2:
- New patch.
--- docs/features/dom0less.pandoc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
docs/features/dom0less.pandoc +5 -3
@@ -110,8 +110,9 @@ hotplug PV drivers to dom0less guests. E.g. xl network-attach domU. The implementation works as follows: - Xen allocates the xenstore event channel for each dom0less domU that has the "xen,enhanced" property, and sets HVM_PARAM_STORE_EVTCHN -- Xen does *not* allocate the xenstore page and sets HVM_PARAM_STORE_PFN - to ~0ULL (invalid) +- Xen allocates the hypervisor reserved pages region (the xenstore page + is part of it) and sets HVM_PARAM_HV_RSRV_{BASE_PFN,SIZE} accordingly. + Xen sets HVM_PARAM_STORE_PFN to ~0ULL (invalid). - Dom0less domU kernels check that HVM_PARAM_STORE_PFN is set to invalid - Old kernels will continue without xenstore support (Note: some old buggy kernels might crash because they don't check the validity of
@@ -121,7 +122,8 @@ The implementation works as follows: channel (HVM_PARAM_STORE_EVTCHN) before continuing with the initialization - Once dom0 is booted, init-dom0less is executed: - - it allocates the xenstore shared page and sets HVM_PARAM_STORE_PFN + - it gets the xenstore shared page from HVM_PARAM_HV_RSRV_BASE_PFN + and sets HVM_PARAM_STORE_PFN - it calls xs_introduce_domain - Xenstored notices the new domain, initializes interfaces as usual, and sends an event channel notification to the domain using the xenstore
Signature · 1 line
2.34.1