From nobody Tue Jul 30 07:57:12 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WY6yK2Srqz5RT7d; Tue, 30 Jul 2024 07:57:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WY6yK0zVxz44Q6; Tue, 30 Jul 2024 07:57:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722326233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=D3d4IdtL8P3PFKtUjWCjI9l31Y7+DY+jkQg04RraFlo=; b=q1Umu3h0qEzDaMVSis7KbMmJJo/4Du/jiDxyxiJMM+K4j3a92wNA9V3VCNN48es72dUnzB o8N4ubeeh/OVGZUCJNBS2oLH04uWB0z9YOvLK9kERQki5i42bhnLGKieiKHj2l+rJk/3bF j3KCl41PuT+BhgVKNnVw5pn1FV6bpVYLm9LKkqRlhmk228RXfv+yd7WjelfhRBWY9hPsD+ UG4IQQxWaxkhWk0xOM57v+ivI4VWClbktct5mspT3tdA7M2QnJtiklwgYYGlx+HKJnZ36B KWU6I1Thm8Tv9XcNIV9Bo/UG8G9PVYluj3wSGsuFpj4fZBuBWgNp62eq5fTMYw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722326233; a=rsa-sha256; cv=none; b=W5NX9MtQG2sdZ4OsMnfCeBNXuCW1MUIGXmQ6S9V+P3zaiT5GRaNy5Vz5uuDUBWRVKPk9Vl ZLJcEed/AxHZoqXF9bGfSRiVpxcV+LFHw/48KWXiO+7y0lmabscZZmVSmOEtH4Vp9lsPZZ 6kKhoeP2OGUfqyFsK2YKZuWuQXX+SmuHrFwoMf6CTxIzDMO1NwtQVsXsDk0bDuZDvAzPxW OJcFF1KlisF0WAEPvGHTt1UVkqmOW/L6Uzszt1X5YcyLYmpb2jxGSby2mp+lM7r19uE2RR yKPku6WgFK6iO/oSHto9z7oQU8cxvA5HIWFkdZX2Ko8t+L8hfg+GiPYGmZQoWA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722326233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=D3d4IdtL8P3PFKtUjWCjI9l31Y7+DY+jkQg04RraFlo=; b=ux2UzQB9DlB3s0GwX6K3BLI/GfG01tvwUwgCw5UwfLVeiq8C5qrbkF0xf6AICuNaPgN5gn vNYPABPKQ93n9fKaHFmk0sy/aZImlvvSnXN7eT1Vvyz7wVjQSLc8vaFYa4XvwZPlP6v/va xAlFTqSG4IFCV31CXPp3pnifNQ7rTtjCEOqKHTUwif0ne2eDhaAyYBZ6AhLoPh17cORn74 6W+FbfL6CnaxMfEfXRxIB5u9zYa8VoZ9bk1LxQJlXdm7X08noTxT14QKDshmU/lTFpicQs 7T9p6J29WO813ZJWrcs0UNRgmtk97FEwLcFto6RGIflAT72gRd9zj03FIbtGdA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WY6yK0Zqqz18V2; Tue, 30 Jul 2024 07:57:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46U7vCKI066472; Tue, 30 Jul 2024 07:57:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46U7vCDn066469; Tue, 30 Jul 2024 07:57:12 GMT (envelope-from git) Date: Tue, 30 Jul 2024 07:57:12 GMT Message-Id: <202407300757.46U7vCDn066469@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Roger Pau =?utf-8?Q?Monn=C3=A9?= Subject: git: 69c47485b5f9 - main - x86/xen: use UNUSABLE e820 regions for external mappings List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: royger X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 69c47485b5f9609c32269523a704d1ad0c638a30 Auto-Submitted: auto-generated The branch main has been updated by royger: URL: https://cgit.FreeBSD.org/src/commit/?id=69c47485b5f9609c32269523a704d1ad0c638a30 commit 69c47485b5f9609c32269523a704d1ad0c638a30 Author: Roger Pau Monné AuthorDate: 2024-02-15 10:42:17 +0000 Commit: Roger Pau Monné CommitDate: 2024-07-30 07:41:17 +0000 x86/xen: use UNUSABLE e820 regions for external mappings Using holes in the memory map to create external mappings (for example grants or foreign maps) can be dangerous, as the guest might not have enough knowledge of whether such holes are maybe MMIO of some devices. Workaround this by using UNUSABLE e820 ranges as scratch space for external mappings. The e820 memory map provided to dom0 is based on the native one, but since PVH dom0 uses second stage translation, the UNUSABLE ranges on the host memory map doesn't affect it, and we can also guarantee no device MMIO uses those. Additionally, any RAM in the e820 not available to dom0 because dom0 memory has been limited on the command line, or because those are in use by Xen, are converted to UNUSABLE in the dom0 memory map. Note that domU don't usually have UNUSABLE ranges in their memory maps, and hence the logic introduced here won't help in avoiding conflicts with MMIO regions. However the usage of external mappings on domUs is in general limited, and the logic introduced here simply won't be helpful to them, but won't regress the current state. Sponsored by: Cloud Software Group MFC after: 1 week Differential revision: https://reviews.freebsd.org/D46123 --- sys/x86/xen/hvm.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/sys/x86/xen/hvm.c b/sys/x86/xen/hvm.c index 22f070213f9b..d2ab1c32b0a2 100644 --- a/sys/x86/xen/hvm.c +++ b/sys/x86/xen/hvm.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -548,3 +549,52 @@ xen_has_iommu_maps(void) return (regs[0] & XEN_HVM_CPUID_IOMMU_MAPPINGS); } + +int +xen_arch_init_physmem(device_t dev, struct rman *mem) +{ + static struct bios_smap smap[128]; + struct xen_memory_map memmap = { + .nr_entries = nitems(smap), + }; + unsigned int i; + int error; + + set_xen_guest_handle(memmap.buffer, smap); + error = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); + if (error != 0) + return (error); + + /* + * Fill with UNUSABLE regions, as it's always fine to use those for + * foreign mappings, they will never be populated. + */ + for (i = 0; i < memmap.nr_entries; i++) { + const vm_paddr_t max_phys = cpu_getmaxphyaddr(); + vm_paddr_t start, end; + + if (smap[i].type != SMAP_TYPE_ACPI_ERROR) + continue; + + start = round_page(smap[i].base); + /* In 32bit mode we possibly need to truncate the addresses. */ + end = MIN(trunc_page(smap[i].base + smap[i].length) - 1, + max_phys); + + if (start >= end) + continue; + + if (bootverbose != 0) + device_printf(dev, + "scratch mapping region @ [%016jx, %016jx]\n", + start, end); + + error = rman_manage_region(mem, start, end); + if (error != 0) + device_printf(dev, + "unable to add scratch region [%016jx, %016jx]: %d\n", + start, end, error); + } + + return (0); +}