Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Nov 2020 07:29:28 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r368049 - stable/12/usr.sbin/bhyve
Message-ID:  <202011260729.0AQ7TSb8034554@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Thu Nov 26 07:29:28 2020
New Revision: 368049
URL: https://svnweb.freebsd.org/changeset/base/368049

Log:
  MFC r367606:
  bhyve: avoid allocating BARs above the end of supported physical addresses.

Modified:
  stable/12/usr.sbin/bhyve/pci_emul.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/usr.sbin/bhyve/pci_emul.c
==============================================================================
--- stable/12/usr.sbin/bhyve/pci_emul.c	Thu Nov 26 06:59:27 2020	(r368048)
+++ stable/12/usr.sbin/bhyve/pci_emul.c	Thu Nov 26 07:29:28 2020	(r368049)
@@ -33,6 +33,9 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/linker_set.h>
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/pmap.h>
 
 #include <ctype.h>
 #include <errno.h>
@@ -45,6 +48,8 @@ __FBSDID("$FreeBSD$");
 #include <stdbool.h>
 
 #include <machine/vmm.h>
+#include <machine/cpufunc.h>
+#include <machine/specialreg.h>
 #include <vmmapi.h>
 
 #include "acpi.h"
@@ -97,6 +102,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
@@ -107,9 +113,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);
@@ -631,7 +634,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;
@@ -1100,12 +1103,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)
+		cpu_maxphysaddr = VM_MAXUSER_ADDRESS;
+	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)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202011260729.0AQ7TSb8034554>