From owner-svn-src-all@freebsd.org Thu Nov 12 00:46:53 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id BABAB46B7C6; Thu, 12 Nov 2020 00:46:53 +0000 (UTC) (envelope-from kib@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CWjbd4yJ5z4WDX; Thu, 12 Nov 2020 00:46:53 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9D1B91DC76; Thu, 12 Nov 2020 00:46:53 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AC0krJT049661; Thu, 12 Nov 2020 00:46:53 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AC0krBs049660; Thu, 12 Nov 2020 00:46:53 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202011120046.0AC0krBs049660@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Thu, 12 Nov 2020 00:46:53 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367606 - head/usr.sbin/bhyve X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/usr.sbin/bhyve X-SVN-Commit-Revision: 367606 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Nov 2020 00:46:53 -0000 Author: kib Date: Thu Nov 12 00:46:53 2020 New Revision: 367606 URL: https://svnweb.freebsd.org/changeset/base/367606 Log: bhyve: avoid allocating BARs above the end of supported physical addresses. Read CPUID leaf 0x8000008 to determine max supported phys address and create BAR region right below it, reserving 1/4 of the supported guest physical address space to the 64bit BARs mappings. PR: 250802 (although the issue from PR is not fixed by the change) Noted and reviewed by: grehan Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D27095 Modified: head/usr.sbin/bhyve/pci_emul.c Modified: head/usr.sbin/bhyve/pci_emul.c ============================================================================== --- head/usr.sbin/bhyve/pci_emul.c Thu Nov 12 00:29:23 2020 (r367605) +++ head/usr.sbin/bhyve/pci_emul.c Thu Nov 12 00:46:53 2020 (r367606) @@ -33,6 +33,9 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include +#include #include #include @@ -46,6 +49,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include #include #include "acpi.h" @@ -98,6 +103,7 @@ SET_DECLARE(pci_devemu_set, struct pci_devemu); static uint64_t pci_emul_iobase; static uint64_t pci_emul_membase32; static uint64_t pci_emul_membase64; +static uint64_t pci_emul_memlim64; #define PCI_EMUL_IOBASE 0x2000 #define PCI_EMUL_IOLIMIT 0x10000 @@ -108,9 +114,6 @@ SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE); #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE -#define PCI_EMUL_MEMBASE64 0xD000000000UL -#define PCI_EMUL_MEMLIMIT64 0xFD00000000UL - static struct pci_devemu *pci_emul_finddev(char *name); static void pci_lintr_route(struct pci_devinst *pi); static void pci_lintr_update(struct pci_devinst *pi); @@ -632,7 +635,7 @@ pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, baseptr = &hostbase; else baseptr = &pci_emul_membase64; - limit = PCI_EMUL_MEMLIMIT64; + limit = pci_emul_memlim64; mask = PCIM_BAR_MEM_BASE; lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 | PCIM_BAR_MEM_PREFETCH; @@ -1101,12 +1104,25 @@ init_pci(struct vmctx *ctx) struct slotinfo *si; struct funcinfo *fi; size_t lowmem; - int bus, slot, func; - int error; + uint64_t cpu_maxphysaddr, pci_emul_memresv64; + u_int regs[4]; + int bus, slot, func, error; pci_emul_iobase = PCI_EMUL_IOBASE; pci_emul_membase32 = vm_get_lowmem_limit(ctx); - pci_emul_membase64 = PCI_EMUL_MEMBASE64; + + do_cpuid(0x80000008, regs); + cpu_maxphysaddr = 1ULL << (regs[0] & 0xff); + if (cpu_maxphysaddr > VM_MAXUSER_ADDRESS_LA48) + cpu_maxphysaddr = VM_MAXUSER_ADDRESS_LA48; + pci_emul_memresv64 = cpu_maxphysaddr / 4; + /* + * Max power of 2 that is less then + * cpu_maxphysaddr - pci_emul_memresv64. + */ + pci_emul_membase64 = 1ULL << (flsl(cpu_maxphysaddr - + pci_emul_memresv64) - 1); + pci_emul_memlim64 = cpu_maxphysaddr; for (bus = 0; bus < MAXBUSES; bus++) { if ((bi = pci_businfo[bus]) == NULL)