Fix xen crash when starting HVM guest due to missing io handler
cover Fix xen crash when starting HVM guest due to missing io handler
From: Suravee Suthikulpanit <[email protected]>
Hi All,
On systems with iommu v2 enabled, the hypervisor crashes when trying to start up an HVM guest.
Investigating shows that the guest_iommu_init() is called before the HVM domain is initialized. It then tries to register_mmio_handler() causing the hvm_next_io_handler() to increment the io_handler_count. However, the registration fails silently and left the I/O handler uninitialized.
At later time, hvm_find_io_handler() is called and iterate through the registered handlered, but then resulting in referencing NULL pointers.
This patch series proposes fix for this issue.
NOTE: For patch 2, since guest IOMMU emulation is still incompleted, this change is tentative and will be verified in the future. Alterantively, I can just simply remove the guest_iommu_init()/destroy() for now. I will be also looking at re-enabling this feature in Xen.
Thanks,
Suravee
Suravee Suthikulpanit (2):
x86/hvm: Add check when register io handler
svm: iommu: Only call guest_iommu_init() after initialized HVM domain
xen/arch/x86/hvm/intercept.c | 8 ++++++-- xen/arch/x86/hvm/svm/svm.c | 10 ++++++++++ xen/drivers/passthrough/amd/iommu_guest.c | 6 ++++++ xen/drivers/passthrough/amd/pci_amd_iommu.c | 4 ---- 4 files changed, 22 insertions(+), 6 deletions(-)
Signature · 7 lines
1.9.1 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 1/2 x86/hvm: Add check when register io handler
From: Suravee Suthikulpanit <[email protected]>
At the time of registering HVM I/O handler, the HVM domain might not have been initialized, which means the hvm_domain.io_handler would be NULL. In the hvm_next_io_handler(), this should be checked before returning and referencing the array. Also, the io_handler_count should only be incremented on success.
So, this patch adds error handling in hvm_next_io_handler.
--- xen/arch/x86/hvm/intercept.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/intercept.c +6 -2
@@ -248,14 +248,18 @@ int hvm_io_intercept(ioreq_t *p)struct hvm_io_handler *hvm_next_io_handler (struct domain *d) { -unsigned int i = d->arch.hvm_domain.io_handler_count++; +unsigned int i = d->arch.hvm_domain.io_handler_count; -if ( i ==NR_IO_HANDLERS ) +if ( !d->arch.hvm_domain.io_handler ) +return NULL ; + +if ( i ==NR_IO_HANDLERS -1 ) {domain_crash (d);return NULL ; } + d->arch.hvm_domain.io_handler_count++;return &d->arch.hvm_domain.io_handler[i]; }
Signature · 7 lines
1.9.1 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH 2/2 svm: iommu: Only call guest_iommu_init() after initialized HVM domain
In reply to [email protected]From: Suravee Suthikulpanit <[email protected]>
The guest_iommu_init() is currently called by the following code path:
arch/x86/domain.c: arch_domain_create()
]- drivers/passthrough/iommu.c: iommu_domain_init()
|- drivers/passthrough/amd/pci_amd_iommu.c: amd_iommu_domain_init();
|- drivers/passthrough/amd/iommu_guest.c: guest_iommu_init()
At this point, the hvm_domain_initialised() has not been called. So register_mmio_handler(), in guest_iommu_init(), silently fails. This patch moves the call to guest_iommu_init/destroy() into the svm_domain_intialise/_destroy() instead.
--- xen/arch/x86/hvm/svm/svm.c | 10 ++++++++++ xen/drivers/passthrough/amd/iommu_guest.c | 6 ++++++ xen/drivers/passthrough/amd/pci_amd_iommu.c | 4 ---- 3 files changed, 16 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/svm/svm.c +10 -0
@@ -45,6 +45,7 @@#include <asm/hvm/support.h> #include <asm/hvm/io.h> #include <asm/hvm/emulate.h> +#include <asm/hvm/svm/amd-iommu-proto.h> #include <asm/hvm/svm/asid.h> #include <asm/hvm/svm/svm.h> #include <asm/hvm/svm/vmcb.h>
@@ -1176,11 +1177,20 @@ void svm_host_osvw_init()static int svm_domain_initialise (struct domain *d) { +if (is_hvm_domain (d) ) +/* +* This requires the hvm domain to be +* initialized first. +*/ +return guest_iommu_init (d); +return 0 ; }static void svm_domain_destroy (struct domain *d) { +if (is_hvm_domain (d) ) +guest_iommu_destroy (d); }static int svm_vcpu_initialise (struct vcpu *v)
xen/drivers/passthrough/amd/iommu_guest.c +6 -0
@@ -891,6 +891,12 @@ int guest_iommu_init(struct domain* d) !has_viommu(d) )return 0 ; +if ( d->arch.hvm_domain.io_handler ==NULL ) + { +AMD_IOMMU_DEBUG ("Error: uninitalized hvm io handler\n" ); +return 1 ; + } + iommu = xzalloc(struct guest_iommu);if ( !iommu ) {
xen/drivers/passthrough/amd/pci_amd_iommu.c +0 -4
@@ -274,9 +274,6 @@ static int amd_iommu_domain_init(struct domain *d) hd->arch.paging_mode =is_hvm_domain (d) ?IOMMU_PAGING_MODE_LEVEL_2 :get_paging_mode (max_page); - -guest_iommu_init (d); -return 0 ; }
@@ -476,7 +473,6 @@ static void deallocate_iommu_page_tables(struct domain *d)static void amd_iommu_domain_destroy (struct domain *d) { -guest_iommu_destroy (d);deallocate_iommu_page_tables (d);amd_iommu_flush_all_pages (d); }
Signature · 7 lines
1.9.1 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel