From: "Jan Beulich" Subject: Re: [v5][PATCH 03/10] xen:x86: define a new hypercall to get RMRR mappings Date: Wed, 03 Sep 2014 13:54:13 +0100 Message-ID: <54072B9502000078000304D0@mail.emea.novell.com> References: <1409050980-21933-1-git-send-email-tiejun.chen@intel.com> <1409050980-21933-4-git-send-email-tiejun.chen@intel.com> <53FC774D.8000501@citrix.com> <53FC9BB1020000780002D986@mail.emea.novell.com> <53FD3669.3070705@intel.com> <53FD9C03020000780002DEFF@mail.emea.novell.com> <53FD86E9.4020205@intel.com> <53FE92C6.6050605@intel.com> <53FEED4F020000780002E76C@mail.emea.novell.com> <53FED5AF.7080803@intel.com> <53FED802.7080707@intel.com> <53FEF9F8020000780002E7E2@mail.emea.novell.com> <53FFED5D.7040406@intel.com> <54006196020000780002EFC6@mail.emea.novell.com> <54043FEA.6080100@intel.com> <540466A8020000780002F656@mail.emea.novell.com> <540594F2.4000406@intel.com> <5405B4E1020000780002FCBC@mail.emea.novell.com> <5405A597.9040103@intel.com> <5405DF00020000780002FD52@mail.emea.novell.com> <540672A7.9050000@intel.com> <5406D1E1.3050601@intel.com> <5406F0470200007800030190@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part51640F65.3__=" Return-path: In-Reply-To: <5406F0470200007800030190@mail.emea.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Tiejun Chen Cc: kevin.tian@intel.com, ian.campbell@citrix.com, stefano.stabellini@eu.citrix.com, Andrew Cooper , ian.jackson@eu.citrix.com, xen-devel@lists.xen.org, yang.z.zhang@intel.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part51640F65.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline >>> On 03.09.14 at 10:41, wrote: > I'm afraid I have to give up and instead go and implement this for > you (which already by now would clearly have been the much less > time consuming thing at least on my end). So here's something to try (only compile tested). Jan introduce XENMEM_reserved_device_memory_map This is a prerequisite for punching holes into HVM and PVH guests' P2M to allow passing through devices that are associated with (on VT-d) RMRRs. Signed-off-by: Jan Beulich --- a/xen/common/compat/memory.c +++ b/xen/common/compat/memory.c @@ -15,6 +15,35 @@ CHECK_TYPE(domid); =20 CHECK_mem_access_op; =20 +struct get_reserved_device_memory { + struct compat_mem_reserved_device_memory_map map; + unsigned int used_entries; +}; + +static int get_reserved_device_memory(xen_pfn_t start, + xen_ulong_t nr, void *ctxt) +{ + struct get_reserved_device_memory *grdm =3D ctxt; + + if ( grdm->used_entries < grdm->map.nr_entries ) + { + struct compat_mem_reserved_device_memory rdm =3D { + .start_pfn =3D start, .nr_pages =3D nr + }; + + if ( rdm.start_pfn !=3D start || rdm.nr_pages !=3D nr ) + return -ERANGE; + + if ( __copy_to_compat_offset(grdm->map.buffer, grdm->used_entries,= + &rdm, 1) ) + return -EFAULT; + } + + ++grdm->used_entries; + + return 0; +} + int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) = compat) { int split, op =3D cmd & MEMOP_CMD_MASK; @@ -272,6 +301,29 @@ int compat_memory_op(unsigned int cmd, X break; } =20 +#ifdef HAS_PASSTHROUGH + case XENMEM_reserved_device_memory_map: + { + struct get_reserved_device_memory grdm; + + if ( copy_from_guest(&grdm.map, compat, 1) || + !compat_handle_okay(grdm.map.buffer, grdm.map.nr_entries)= ) + return -EFAULT; + + grdm.used_entries =3D 0; + rc =3D iommu_get_reserved_device_memory(get_reserved_device_me= mory, + &grdm); + + if ( !rc && grdm.map.nr_entries < grdm.used_entries ) + rc =3D -ENOBUFS; + grdm.map.nr_entries =3D grdm.used_entries; + if ( __copy_to_guest(compat, &grdm.map, 1) ) + rc =3D -EFAULT; + + return rc; + } +#endif + default: return compat_arch_memory_op(cmd, compat); } --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -695,6 +695,32 @@ out: return rc; } =20 +struct get_reserved_device_memory { + struct xen_mem_reserved_device_memory_map map; + unsigned int used_entries; +}; + +static int get_reserved_device_memory(xen_pfn_t start, + xen_ulong_t nr, void *ctxt) +{ + struct get_reserved_device_memory *grdm =3D ctxt; + + if ( grdm->used_entries < grdm->map.nr_entries ) + { + struct xen_mem_reserved_device_memory rdm =3D { + .start_pfn =3D start, .nr_pages =3D nr + }; + + if ( __copy_to_guest_offset(grdm->map.buffer, grdm->used_entries, + &rdm, 1) ) + return -EFAULT; + } + + ++grdm->used_entries; + + return 0; +} + long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) { struct domain *d; @@ -969,6 +995,29 @@ long do_memory_op(unsigned long cmd, XEN =20 break; =20 +#ifdef HAS_PASSTHROUGH + case XENMEM_reserved_device_memory_map: + { + struct get_reserved_device_memory grdm; + + if ( copy_from_guest(&grdm.map, arg, 1) || + !guest_handle_okay(grdm.map.buffer, grdm.map.nr_entries) ) + return -EFAULT; + + grdm.used_entries =3D 0; + rc =3D iommu_get_reserved_device_memory(get_reserved_device_memory= , + &grdm); + + if ( !rc && grdm.map.nr_entries < grdm.used_entries ) + rc =3D -ENOBUFS; + grdm.map.nr_entries =3D grdm.used_entries; + if ( __copy_to_guest(arg, &grdm.map, 1) ) + rc =3D -EFAULT; + + break; + } +#endif + default: rc =3D arch_memory_op(cmd, arg); break; --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -344,6 +344,16 @@ void iommu_crash_shutdown(void) iommu_enabled =3D iommu_intremap =3D 0; } =20 +int iommu_get_reserved_device_memory(iommu_grdm_t *func, void *ctxt) +{ + const struct iommu_ops *ops =3D iommu_get_ops(); + + if ( !iommu_enabled || !ops->get_reserved_device_memory ) + return 0; + + return ops->get_reserved_device_memory(func, ctxt); +} + bool_t iommu_has_feature(struct domain *d, enum iommu_feature feature) { const struct hvm_iommu *hd =3D domain_hvm_iommu(d); --- a/xen/drivers/passthrough/vtd/dmar.c +++ b/xen/drivers/passthrough/vtd/dmar.c @@ -893,3 +893,20 @@ int platform_supports_x2apic(void) unsigned int mask =3D ACPI_DMAR_INTR_REMAP | ACPI_DMAR_X2APIC_OPT_OUT;= return cpu_has_x2apic && ((dmar_flags & mask) =3D=3D ACPI_DMAR_INTR_RE= MAP); } + +int intel_iommu_get_reserved_device_memory(iommu_grdm_t *func, void = *ctxt) +{ + struct acpi_rmrr_unit *rmrr; + int rc =3D 0; + + list_for_each_entry(rmrr, &acpi_rmrr_units, list) + { + rc =3D func(PFN_DOWN(rmrr->base_address), + PFN_UP(rmrr->end_address) - PFN_DOWN(rmrr->base_address)= , + ctxt); + if ( rc ) + break; + } + + return rc; +} --- a/xen/drivers/passthrough/vtd/extern.h +++ b/xen/drivers/passthrough/vtd/extern.h @@ -75,6 +75,7 @@ int domain_context_mapping_one(struct do u8 bus, u8 devfn, const struct pci_dev *); int domain_context_unmap_one(struct domain *domain, struct iommu *iommu, u8 bus, u8 devfn); +int intel_iommu_get_reserved_device_memory(iommu_grdm_t *func, void = *ctxt); =20 unsigned int io_apic_read_remap_rte(unsigned int apic, unsigned int reg); void io_apic_write_remap_rte(unsigned int apic, --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2485,6 +2485,7 @@ const struct iommu_ops intel_iommu_ops =3D .crash_shutdown =3D vtd_crash_shutdown, .iotlb_flush =3D intel_iommu_iotlb_flush, .iotlb_flush_all =3D intel_iommu_iotlb_flush_all, + .get_reserved_device_memory =3D intel_iommu_get_reserved_device_memory= , .dump_p2m_table =3D vtd_dump_p2m_table, }; =20 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -120,6 +120,8 @@ void iommu_dt_domain_destroy(struct doma =20 struct page_info; =20 +typedef int iommu_grdm_t(xen_pfn_t start, xen_ulong_t nr, void *ctxt); + struct iommu_ops { int (*init)(struct domain *d); void (*hwdom_init)(struct domain *d); @@ -156,12 +158,14 @@ struct iommu_ops { void (*crash_shutdown)(void); void (*iotlb_flush)(struct domain *d, unsigned long gfn, unsigned int = page_count); void (*iotlb_flush_all)(struct domain *d); + int (*get_reserved_device_memory)(iommu_grdm_t *, void *); void (*dump_p2m_table)(struct domain *d); }; =20 void iommu_suspend(void); void iommu_resume(void); void iommu_crash_shutdown(void); +int iommu_get_reserved_device_memory(iommu_grdm_t *, void *); =20 void iommu_share_p2m_table(struct domain *d); =20 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -523,7 +523,29 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_ =20 #endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ =20 -/* Next available subop number is 26 */ +/* + * For legacy reasons, some devices must be configured with special = memory + * regions to function correctly. The guest must avoid using any of = these + * regions. + */ +#define XENMEM_reserved_device_memory_map 26 +struct xen_mem_reserved_device_memory { + xen_pfn_t start_pfn; + xen_ulong_t nr_pages; +}; +typedef struct xen_mem_reserved_device_memory xen_mem_reserved_device_memo= ry_t; +DEFINE_XEN_GUEST_HANDLE(xen_mem_reserved_device_memory_t); + +struct xen_mem_reserved_device_memory_map { + /* IN/OUT */ + unsigned int nr_entries; + /* OUT */ + XEN_GUEST_HANDLE(xen_mem_reserved_device_memory_t) buffer; +}; +typedef struct xen_mem_reserved_device_memory_map xen_mem_reserved_device_= memory_map_t; +DEFINE_XEN_GUEST_HANDLE(xen_mem_reserved_device_memory_map_t); + +/* Next available subop number is 27 */ =20 #endif /* __XEN_PUBLIC_MEMORY_H__ */ =20 --- a/xen/include/xlat.lst +++ b/xen/include/xlat.lst @@ -60,7 +60,8 @@ ! memory_exchange memory.h ! memory_map memory.h ! memory_reservation memory.h -? mem_access_op memory.h +? mem_access_op memory.h +! mem_reserved_device_memory_map memory.h ! pod_target memory.h ! remove_from_physmap memory.h ? physdev_eoi physdev.h --=__Part51640F65.3__= Content-Type: text/plain; name="get-reserved-device-memory.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="get-reserved-device-memory.patch" introduce XENMEM_reserved_device_memory_map=0A=0AThis is a prerequisite = for punching holes into HVM and PVH guests' P2M=0Ato allow passing through = devices that are associated with (on VT-d)=0ARMRRs.=0A=0ASigned-off-by: = Jan Beulich =0A=0A--- a/xen/common/compat/memory.c=0A+++= b/xen/common/compat/memory.c=0A@@ -15,6 +15,35 @@ CHECK_TYPE(domid);=0A = =0A CHECK_mem_access_op;=0A =0A+struct get_reserved_device_memory {=0A+ = struct compat_mem_reserved_device_memory_map map;=0A+ unsigned int = used_entries;=0A+};=0A+=0A+static int get_reserved_device_memory(xen_pfn_t = start,=0A+ xen_ulong_t nr, void = *ctxt)=0A+{=0A+ struct get_reserved_device_memory *grdm =3D ctxt;=0A+=0A= + if ( grdm->used_entries < grdm->map.nr_entries )=0A+ {=0A+ = struct compat_mem_reserved_device_memory rdm =3D {=0A+ = .start_pfn =3D start, .nr_pages =3D nr=0A+ };=0A+=0A+ if ( = rdm.start_pfn !=3D start || rdm.nr_pages !=3D nr )=0A+ return = -ERANGE;=0A+=0A+ if ( __copy_to_compat_offset(grdm->map.buffer, = grdm->used_entries,=0A+ &rdm, 1) )=0A+ = return -EFAULT;=0A+ }=0A+=0A+ ++grdm->used_entries;=0A+=0A= + return 0;=0A+}=0A+=0A int compat_memory_op(unsigned int cmd, = XEN_GUEST_HANDLE_PARAM(void) compat)=0A {=0A int split, op =3D cmd & = MEMOP_CMD_MASK;=0A@@ -272,6 +301,29 @@ int compat_memory_op(unsigned int = cmd, X=0A break;=0A }=0A =0A+#ifdef HAS_PASSTHROUGH=0A+= case XENMEM_reserved_device_memory_map:=0A+ {=0A+ = struct get_reserved_device_memory grdm;=0A+=0A+ if ( copy_from_= guest(&grdm.map, compat, 1) ||=0A+ !compat_handle_okay(grdm= .map.buffer, grdm.map.nr_entries) )=0A+ return -EFAULT;=0A+= =0A+ grdm.used_entries =3D 0;=0A+ rc =3D iommu_get_re= served_device_memory(get_reserved_device_memory,=0A+ = &grdm);=0A+=0A+ if ( !rc && = grdm.map.nr_entries < grdm.used_entries )=0A+ rc =3D = -ENOBUFS;=0A+ grdm.map.nr_entries =3D grdm.used_entries;=0A+ = if ( __copy_to_guest(compat, &grdm.map, 1) )=0A+ rc = =3D -EFAULT;=0A+=0A+ return rc;=0A+ }=0A+#endif=0A+=0A = default:=0A return compat_arch_memory_op(cmd, compat);=0A= }=0A--- a/xen/common/memory.c=0A+++ b/xen/common/memory.c=0A@@ = -695,6 +695,32 @@ out:=0A return rc;=0A }=0A =0A+struct get_reserved_de= vice_memory {=0A+ struct xen_mem_reserved_device_memory_map map;=0A+ = unsigned int used_entries;=0A+};=0A+=0A+static int get_reserved_device_memo= ry(xen_pfn_t start,=0A+ xen_ulong_t = nr, void *ctxt)=0A+{=0A+ struct get_reserved_device_memory *grdm =3D = ctxt;=0A+=0A+ if ( grdm->used_entries < grdm->map.nr_entries )=0A+ = {=0A+ struct xen_mem_reserved_device_memory rdm =3D {=0A+ = .start_pfn =3D start, .nr_pages =3D nr=0A+ };=0A+=0A+ if ( = __copy_to_guest_offset(grdm->map.buffer, grdm->used_entries,=0A+ = &rdm, 1) )=0A+ return -EFAULT;=0A+ = }=0A+=0A+ ++grdm->used_entries;=0A+=0A+ return 0;=0A+}=0A+=0A long = do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)=0A {=0A = struct domain *d;=0A@@ -969,6 +995,29 @@ long do_memory_op(unsigned = long cmd, XEN=0A =0A break;=0A =0A+#ifdef HAS_PASSTHROUGH=0A+ = case XENMEM_reserved_device_memory_map:=0A+ {=0A+ struct = get_reserved_device_memory grdm;=0A+=0A+ if ( copy_from_guest(&grdm.= map, arg, 1) ||=0A+ !guest_handle_okay(grdm.map.buffer, = grdm.map.nr_entries) )=0A+ return -EFAULT;=0A+=0A+ = grdm.used_entries =3D 0;=0A+ rc =3D iommu_get_reserved_device_memory= (get_reserved_device_memory,=0A+ = &grdm);=0A+=0A+ if ( !rc && grdm.map.nr_entries < grdm.used_entri= es )=0A+ rc =3D -ENOBUFS;=0A+ grdm.map.nr_entries =3D = grdm.used_entries;=0A+ if ( __copy_to_guest(arg, &grdm.map, 1) = )=0A+ rc =3D -EFAULT;=0A+=0A+ break;=0A+ }=0A+#endif= =0A+=0A default:=0A rc =3D arch_memory_op(cmd, arg);=0A = break;=0A--- a/xen/drivers/passthrough/iommu.c=0A+++ b/xen/drivers/passthr= ough/iommu.c=0A@@ -344,6 +344,16 @@ void iommu_crash_shutdown(void)=0A = iommu_enabled =3D iommu_intremap =3D 0;=0A }=0A =0A+int iommu_get_reserved_= device_memory(iommu_grdm_t *func, void *ctxt)=0A+{=0A+ const struct = iommu_ops *ops =3D iommu_get_ops();=0A+=0A+ if ( !iommu_enabled || = !ops->get_reserved_device_memory )=0A+ return 0;=0A+=0A+ return = ops->get_reserved_device_memory(func, ctxt);=0A+}=0A+=0A bool_t iommu_has_f= eature(struct domain *d, enum iommu_feature feature)=0A {=0A const = struct hvm_iommu *hd =3D domain_hvm_iommu(d);=0A--- a/xen/drivers/passthrou= gh/vtd/dmar.c=0A+++ b/xen/drivers/passthrough/vtd/dmar.c=0A@@ -893,3 = +893,20 @@ int platform_supports_x2apic(void)=0A unsigned int mask =3D = ACPI_DMAR_INTR_REMAP | ACPI_DMAR_X2APIC_OPT_OUT;=0A return cpu_has_x2ap= ic && ((dmar_flags & mask) =3D=3D ACPI_DMAR_INTR_REMAP);=0A }=0A+=0A+int = intel_iommu_get_reserved_device_memory(iommu_grdm_t *func, void *ctxt)=0A+{= =0A+ struct acpi_rmrr_unit *rmrr;=0A+ int rc =3D 0;=0A+=0A+ = list_for_each_entry(rmrr, &acpi_rmrr_units, list)=0A+ {=0A+ rc = =3D func(PFN_DOWN(rmrr->base_address),=0A+ PFN_UP(rmrr->en= d_address) - PFN_DOWN(rmrr->base_address),=0A+ ctxt);=0A+ = if ( rc )=0A+ break;=0A+ }=0A+=0A+ return = rc;=0A+}=0A--- a/xen/drivers/passthrough/vtd/extern.h=0A+++ b/xen/drivers/p= assthrough/vtd/extern.h=0A@@ -75,6 +75,7 @@ int domain_context_mapping_one(= struct do=0A u8 bus, u8 devfn, const struct = pci_dev *);=0A int domain_context_unmap_one(struct domain *domain, struct = iommu *iommu,=0A u8 bus, u8 devfn);=0A+int = intel_iommu_get_reserved_device_memory(iommu_grdm_t *func, void *ctxt);=0A = =0A unsigned int io_apic_read_remap_rte(unsigned int apic, unsigned int = reg);=0A void io_apic_write_remap_rte(unsigned int apic,=0A--- a/xen/driver= s/passthrough/vtd/iommu.c=0A+++ b/xen/drivers/passthrough/vtd/iommu.c=0A@@ = -2485,6 +2485,7 @@ const struct iommu_ops intel_iommu_ops =3D=0A = .crash_shutdown =3D vtd_crash_shutdown,=0A .iotlb_flush =3D intel_iommu= _iotlb_flush,=0A .iotlb_flush_all =3D intel_iommu_iotlb_flush_all,=0A+ = .get_reserved_device_memory =3D intel_iommu_get_reserved_device_memory,= =0A .dump_p2m_table =3D vtd_dump_p2m_table,=0A };=0A =0A--- a/xen/inclu= de/xen/iommu.h=0A+++ b/xen/include/xen/iommu.h=0A@@ -120,6 +120,8 @@ void = iommu_dt_domain_destroy(struct doma=0A =0A struct page_info;=0A =0A+typedef= int iommu_grdm_t(xen_pfn_t start, xen_ulong_t nr, void *ctxt);=0A+=0A = struct iommu_ops {=0A int (*init)(struct domain *d);=0A void = (*hwdom_init)(struct domain *d);=0A@@ -156,12 +158,14 @@ struct iommu_ops = {=0A void (*crash_shutdown)(void);=0A void (*iotlb_flush)(struct = domain *d, unsigned long gfn, unsigned int page_count);=0A void = (*iotlb_flush_all)(struct domain *d);=0A+ int (*get_reserved_device_memo= ry)(iommu_grdm_t *, void *);=0A void (*dump_p2m_table)(struct domain = *d);=0A };=0A =0A void iommu_suspend(void);=0A void iommu_resume(void);=0A = void iommu_crash_shutdown(void);=0A+int iommu_get_reserved_device_memory(io= mmu_grdm_t *, void *);=0A =0A void iommu_share_p2m_table(struct domain = *d);=0A =0A--- a/xen/include/public/memory.h=0A+++ b/xen/include/public/mem= ory.h=0A@@ -523,7 +523,29 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_=0A = =0A #endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */=0A =0A-/* Next = available subop number is 26 */=0A+/*=0A+ * For legacy reasons, some = devices must be configured with special memory=0A+ * regions to function = correctly. The guest must avoid using any of these=0A+ * regions.=0A+ = */=0A+#define XENMEM_reserved_device_memory_map 26=0A+struct xen_mem_rese= rved_device_memory {=0A+ xen_pfn_t start_pfn;=0A+ xen_ulong_t = nr_pages;=0A+};=0A+typedef struct xen_mem_reserved_device_memory xen_mem_re= served_device_memory_t;=0A+DEFINE_XEN_GUEST_HANDLE(xen_mem_reserved_device_= memory_t);=0A+=0A+struct xen_mem_reserved_device_memory_map {=0A+ /* = IN/OUT */=0A+ unsigned int nr_entries;=0A+ /* OUT */=0A+ = XEN_GUEST_HANDLE(xen_mem_reserved_device_memory_t) buffer;=0A+};=0A+typedef= struct xen_mem_reserved_device_memory_map xen_mem_reserved_device_memory_m= ap_t;=0A+DEFINE_XEN_GUEST_HANDLE(xen_mem_reserved_device_memory_map_t);=0A+= =0A+/* Next available subop number is 27 */=0A =0A #endif /* __XEN_PUBLIC_M= EMORY_H__ */=0A =0A--- a/xen/include/xlat.lst=0A+++ b/xen/include/xlat.lst= =0A@@ -60,7 +60,8 @@=0A ! memory_exchange memory.h=0A= ! memory_map memory.h=0A ! memory_reservation = memory.h=0A-? mem_access_op memory.h=0A+? mem_access_= op memory.h=0A+! mem_reserved_device_memory_map = memory.h=0A ! pod_target memory.h=0A ! remove_from= _physmap memory.h=0A ? physdev_eoi = physdev.h=0A --=__Part51640F65.3__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part51640F65.3__=--