From nobody Sat Apr 11 23:04:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1773038720; cv=none; d=zohomail.com; s=zohoarc; b=A4JRNSyjRE5w67AjrxQiXJEAx/BgzeZdYgLAGsuYb4+dH0NwQc+ciSkEANolupuyOnbZyLGYR6k9S85PYjcfs8wU5wyPjHytKwKmXjB+51esGuCCDN9G25mXIUNbNdrYg8f81aEwYPnchbUK4cdiw8pWkY5G3OU5ltlpEVmaHPM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773038720; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=OcNC0yip2XrDmFo80yDxRtX2oleYkrUeo7InlE7zv9I=; b=NJsluYDYD3xyB21e55JGHD8EVIZsCvJpEksZOa5Q1bOSfQAjQ3/x7wfyh3/+31bUteyIfBWXS+H79MoZmXwr3tyLcDgfLy3WRumYMJcPZw3TZL69prXPXfWC0yzzTQogXUDERYGbqkB3eD+TmIb3Ukqz7XD7geucafOvyy/ifVE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1773038720335634.3292999587145; Sun, 8 Mar 2026 23:45:20 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1249077.1546619 (Exim 4.92) (envelope-from ) id 1vzUMM-0006XK-85; Mon, 09 Mar 2026 06:44:58 +0000 Received: by outflank-mailman (output) from mailman id 1249077.1546619; Mon, 09 Mar 2026 06:44:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vzUMM-0006X6-39; Mon, 09 Mar 2026 06:44:58 +0000 Received: by outflank-mailman (input) for mailman id 1249077; Mon, 09 Mar 2026 06:44:56 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vzUMK-0005KI-Ot for xen-devel@lists.xenproject.org; Mon, 09 Mar 2026 06:44:56 +0000 Received: from mail-vk1-xa2a.google.com (mail-vk1-xa2a.google.com [2607:f8b0:4864:20::a2a]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7bbb3518-1b83-11f1-b164-2bf370ae4941; Mon, 09 Mar 2026 07:44:56 +0100 (CET) Received: by mail-vk1-xa2a.google.com with SMTP id 71dfb90a1353d-56b17c0223bso628832e0c.1 for ; Sun, 08 Mar 2026 23:44:56 -0700 (PDT) Received: from gmail.com (ip190-5-140-138.intercom.com.sv. [190.5.140.138]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-56b0f889ed2sm7456276e0c.13.2026.03.08.23.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2026 23:44:54 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7bbb3518-1b83-11f1-b164-2bf370ae4941 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773038695; x=1773643495; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OcNC0yip2XrDmFo80yDxRtX2oleYkrUeo7InlE7zv9I=; b=LOU2YxLCDKLnQ2zAkCvHD/pWl1L/yiN5wh/KZrOhuosRRV6Dlk06GcFyFuKxi9uY4G BtEd4+CCF/iCRgqR6//5255EdBWolK3+U+JefJ2xd+z1u5qAlt7kBE4BLTxzNWaXf1UR +aCVzd7YKJTb4dxSG0ELron77q58brXjkThM1/c6z1p7zKvlIGbjI1LP/cF8IwuZ8u1Y VNiFzwuYLY/MBLMjDi3SKMBoqMj17EsTVr2grb/v02t0yHOItL1NmHwHgu9tzngk/mOj ZaQ1mcxFxJ64rNRRPteW6vNs9igfsagsYHr6hXYhb+TEc8l6wggexPJwLzGDz04UhXAm LhHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773038695; x=1773643495; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OcNC0yip2XrDmFo80yDxRtX2oleYkrUeo7InlE7zv9I=; b=lFycrzStSpHtu6QESel7ijcs69+6UobIFBI9FRRMrGDKIkO/2tafy/e/QqPmd4yOEA 2KAV8okzNuZkz01SnYIdEe2xOENADMcy2EBaSIc12WSsbz5NR8HzS1Y2mrDCGnjoeNht 0FAw3RSVenQd3gGj+HveYxM9shNDZ974bJn5W4So+aqYQ1ctntFVYG0bZEHeckhbA8aM TkybhhOUOn2EaHTMvt9w1kh78RVh1BRTx0PrS+OLgqhFvnNEnOmjvVUBSxOUi65FkAZD dddyvjNt9NOGt63Xa2PHjJxmDyQUHfbLIBmZyWpytnPJdij4E+PpHxMkedMD1eAWBGuF AHhA== X-Forwarded-Encrypted: i=1; AJvYcCV5tqO9ECmP7BG5DFNlRKMP7Ewg9W9nz9kQflEVfNom8DvqjPFtDjW7sb8NkYg7ng6y7saOzbamtX0=@lists.xenproject.org X-Gm-Message-State: AOJu0Yz+GwncYEgp9VPlIxwkm10wTTP8Nz0Zn9RVaUF0sBDGje4lzcsi ZHt37xGSjicPiff7AjrrmKD8qJcDMyVwjNPWSxe+UOzbAQCk9cGiQtnhv63/akyJ X-Gm-Gg: ATEYQzxZmSMI3j8BrIi3r6zbT77SuvXDGuF+teBXjYj8Pf/33t9gBQqkiiPUQ5jOuei omrGDh/EaLxLc3WkKYI0m8gJhyRi3MSWUIRPyjKuX4uur4zOSObeJbo+fPyGWUSRY72p4pAW9mV SX2jKsY/5TSU2br6d+MhjB++IIaWwYwsOlSHaXzfRl3pFzcTE2a2fbXDH+orbZ0PSAzhYCQ+KC5 BpnYttgCBP2+3B7mk/JpMkzVcLhZzw/eGeBkV01bjLH2wmv4f/r7LwrUzl0JTRbcD4DwmCqMd0b ADMg4Z+o03hsUdrs1gWdma/GvWrPrhds0w7UdyAIxalExCKoWPY0R8Akft+Xz8chbIPHmHHsH6M rBFyqgnUHQ7Vj7CmZmQiEPnJbqsyOD8F3V8QDhApkhlVpbHL0LF2VTyHb5DJey3G96NVynVNbHO 5BZnecX9UnvG9afU3pmIv+gq5/0iXxt/s4ixOemZVa2yyz6FUHyxPcdQrLTw== X-Received: by 2002:a05:6122:1c0a:b0:567:4722:66a3 with SMTP id 71dfb90a1353d-56b07da5535mr3763057e0c.8.1773038695019; Sun, 08 Mar 2026 23:44:55 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org, Stefano Stabellini , Anthony PERARD , "Edgar E. Iglesias" Cc: edgar.iglesias@amd.com, xen-devel@lists.xenproject.org Subject: [PULL v1 5/5] hw/xen: xenpvh: Add prop to enable/disable the mapcache Date: Mon, 9 Mar 2026 07:44:35 +0100 Message-ID: <20260309064436.51860-6-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260309064436.51860-1-edgar.iglesias@gmail.com> References: <20260309064436.51860-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1773038722813154100 Content-Type: text/plain; charset="utf-8" From: "Edgar E. Iglesias" Add a machine property to enable/disable the mapcache. Default it to enabled for backwards compatibility. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini --- hw/xen/xen-pvh-common.c | 133 +++++++++++++++++++++++++++++--- include/hw/xen/xen-pvh-common.h | 2 + 2 files changed, 124 insertions(+), 11 deletions(-) diff --git a/hw/xen/xen-pvh-common.c b/hw/xen/xen-pvh-common.c index 4fade017bb..cca37202ff 100644 --- a/hw/xen/xen-pvh-common.c +++ b/hw/xen/xen-pvh-common.c @@ -29,6 +29,69 @@ static const MemoryListener xen_memory_listener =3D { .priority =3D MEMORY_LISTENER_PRIORITY_ACCEL, }; =20 +/* + * Map foreign RAM in bounded chunks so we don't build a PFN array for the + * entire guest size (which can be huge for large guests). We reserve a VA + * range once and then MAP_FIXED each chunk into place. + */ +#define XEN_PVH_MAP_CHUNK_PAGES 65535 + +static void *xen_map_guest_ram(XenPVHMachineState *s, + uint64_t addr, uint64_t size) +{ + size_t total_pages =3D size >> XC_PAGE_SHIFT; + size_t chunk_pages =3D MIN(XEN_PVH_MAP_CHUNK_PAGES, total_pages); + g_autofree xen_pfn_t *pfns =3D NULL; + void *base =3D NULL; + size_t offset; + + if (!total_pages) { + goto done; + } + + base =3D mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, = 0); + if (base =3D=3D MAP_FAILED) { + base =3D NULL; + goto done; + } + + pfns =3D g_new0(xen_pfn_t, chunk_pages); + if (!pfns) { + munmap(base, size); + base =3D NULL; + goto done; + } + + for (offset =3D 0; offset < total_pages; offset +=3D chunk_pages) { + size_t num_pages =3D MIN(chunk_pages, total_pages - offset); + void *mapped; + size_t i; + + for (i =3D 0; i < num_pages; i++) { + pfns[i] =3D (addr >> XC_PAGE_SHIFT) + offset + i; + } + + mapped =3D xenforeignmemory_map2( + xen_fmem, xen_domid, + (uint8_t *)base + (offset << XC_PAGE_SHIFT), + PROT_READ | PROT_WRITE, MAP_FIXED, + num_pages, pfns, NULL); + if (!mapped) { + munmap(base, size); + base =3D NULL; + goto done; + } + } +done: + if (!base) { + /* We can't recover from this. */ + error_report("FATAL: Failed to foreign-map %" PRIx64 " - %" PRIx64, + addr, addr + size); + exit(EXIT_FAILURE); + } + return base; +} + static void xen_pvh_init_ram(XenPVHMachineState *s, MemoryRegion *sysmem) { @@ -45,22 +108,42 @@ static void xen_pvh_init_ram(XenPVHMachineState *s, block_len =3D s->cfg.ram_high.base + ram_size[1]; } =20 - memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len, - &error_fatal); + if (s->cfg.mapcache) { + memory_region_init_ram(&xen_memory, NULL, "xen.ram", + block_len, &error_fatal); + memory_region_init_alias(&s->ram.low, NULL, "xen.ram.lo", &xen_mem= ory, + s->cfg.ram_low.base, ram_size[0]); + if (ram_size[1] > 0) { + memory_region_init_alias(&s->ram.high, NULL, "xen.ram.hi", + &xen_memory, + s->cfg.ram_high.base, ram_size[1]); + } + } else { + void *p; + + p =3D xen_map_guest_ram(s, s->cfg.ram_low.base, ram_size[0]); + memory_region_init_ram_ptr(&s->ram.low, NULL, "xen.ram.lo", + ram_size[0], p); + if (ram_size[1] > 0) { + p =3D xen_map_guest_ram(s, s->cfg.ram_high.base, ram_size[1]); + memory_region_init_ram_ptr(&s->ram.high, NULL, "xen.ram.hi", + ram_size[1], p); + } + } =20 - memory_region_init_alias(&s->ram.low, NULL, "xen.ram.lo", &xen_memory, - s->cfg.ram_low.base, ram_size[0]); + /* Map them onto QEMU's address-space. */ memory_region_add_subregion(sysmem, s->cfg.ram_low.base, &s->ram.low); if (ram_size[1] > 0) { - memory_region_init_alias(&s->ram.high, NULL, "xen.ram.hi", &xen_me= mory, - s->cfg.ram_high.base, ram_size[1]); memory_region_add_subregion(sysmem, s->cfg.ram_high.base, &s->ram.= high); } =20 - /* Setup support for grants. */ - memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len, - &error_fatal); - memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grants); + /* Grants are only supported when the mapcache is on. */ + if (s->cfg.mapcache) { + /* Setup support for grants. */ + memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len, + &error_fatal); + memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grant= s); + } } =20 static void xen_set_irq(void *opaque, int irq, int level) @@ -203,7 +286,7 @@ static void xen_pvh_init(MachineState *ms) xen_register_ioreq(&s->ioreq, ms->smp.max_cpus, xpc->handle_bufioreq, &xen_memory_listener, - true); + s->cfg.mapcache); =20 if (s->cfg.virtio_mmio_num) { xen_create_virtio_mmio_devices(s); @@ -285,6 +368,20 @@ XEN_PVH_PROP_MEMMAP(pci_ecam) XEN_PVH_PROP_MEMMAP(pci_mmio) XEN_PVH_PROP_MEMMAP(pci_mmio_high) =20 +static void xen_pvh_set_mapcache(Object *obj, bool value, Error **errp) +{ + XenPVHMachineState *xp =3D XEN_PVH_MACHINE(obj); + + xp->cfg.mapcache =3D value; +} + +static bool xen_pvh_get_mapcache(Object *obj, Error **errp) +{ + XenPVHMachineState *xp =3D XEN_PVH_MACHINE(obj); + + return xp->cfg.mapcache; +} + static void xen_pvh_set_pci_intx_irq_base(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) @@ -338,6 +435,12 @@ do { = \ OC_MEMMAP_PROP_SIZE(c, prop_name, name); \ } while (0) =20 + object_class_property_add_bool(oc, "mapcache", xen_pvh_get_mapcache, + xen_pvh_set_mapcache); + object_class_property_set_description(oc, "mapcache", + "Set on/off to enable/disable the " + "mapcache"); + /* * We provide memmap properties to allow Xen to move things to other * addresses for example when users need to accomodate the memory-map @@ -377,6 +480,13 @@ do { = \ #endif } =20 +static void xen_pvh_instance_init(Object *obj) +{ + XenPVHMachineState *xp =3D XEN_PVH_MACHINE(obj); + + xp->cfg.mapcache =3D true; +} + static void xen_pvh_class_init(ObjectClass *oc, const void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -395,6 +505,7 @@ static const TypeInfo xen_pvh_info =3D { .parent =3D TYPE_MACHINE, .abstract =3D true, .instance_size =3D sizeof(XenPVHMachineState), + .instance_init =3D xen_pvh_instance_init, .class_size =3D sizeof(XenPVHMachineClass), .class_init =3D xen_pvh_class_init, }; diff --git a/include/hw/xen/xen-pvh-common.h b/include/hw/xen/xen-pvh-commo= n.h index 0ed07c5694..0209b798f3 100644 --- a/include/hw/xen/xen-pvh-common.h +++ b/include/hw/xen/xen-pvh-common.h @@ -84,6 +84,8 @@ struct XenPVHMachineState { /* PCI */ MemMapEntry pci_ecam, pci_mmio, pci_mmio_high; uint32_t pci_intx_irq_base; + + bool mapcache; } cfg; }; =20 --=20 2.43.0