Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F144350202
D50116.id155180.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
10 KB
Referenced Files
None
Subscribers
None
D50116.id155180.diff
View Options
diff --git a/usr.sbin/bhyve/aarch64/Makefile.inc b/usr.sbin/bhyve/aarch64/Makefile.inc
--- a/usr.sbin/bhyve/aarch64/Makefile.inc
+++ b/usr.sbin/bhyve/aarch64/Makefile.inc
@@ -1,5 +1,6 @@
SRCS+= \
fdt.c \
+ mem_aarch64.c \
rtc_pl031.c \
uart_pl011.c
diff --git a/usr.sbin/bhyve/aarch64/mem_aarch64.c b/usr.sbin/bhyve/aarch64/mem_aarch64.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/aarch64/mem_aarch64.c
@@ -0,0 +1,58 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#define _WANT_KERNEL_ERRNO 1
+#include <sys/errno.h>
+#include <sys/tree.h>
+#include <machine/armreg.h>
+#include <machine/vmm.h>
+#include <machine/vmm_instruction_emul.h>
+#include <vmmapi.h>
+
+#include "mem.h"
+
+int
+mmio_handle_non_backed_mem(struct vcpu *vcpu, uint64_t paddr,
+ struct mem_range **mr_paramp __unused)
+{
+ int err;
+ uint64_t spsr, esr;
+
+ if (vm_get_register(vcpu, VM_REG_GUEST_CPSR, &spsr) == -1)
+ return (errno);
+ if ((spsr & PSR_M_MASK) == PSR_M_EL0t)
+ esr = EXCP_DATA_ABORT_L << ESR_ELx_EC_SHIFT;
+ else
+ esr = EXCP_DATA_ABORT << ESR_ELx_EC_SHIFT;
+ esr |= ESR_ELx_IL | ISS_DATA_DFSC_EXT;
+ err = vm_inject_exception(vcpu, esr, paddr);
+ return (err != 0 ? err : EJUSTRETURN);
+}
diff --git a/usr.sbin/bhyve/amd64/Makefile.inc b/usr.sbin/bhyve/amd64/Makefile.inc
--- a/usr.sbin/bhyve/amd64/Makefile.inc
+++ b/usr.sbin/bhyve/amd64/Makefile.inc
@@ -7,6 +7,7 @@
inout.c \
ioapic.c \
kernemu_dev.c \
+ mem_x86.c \
mptbl.c \
pci_fbuf.c \
pci_gvt-d.c \
diff --git a/usr.sbin/bhyve/amd64/mem_x86.c b/usr.sbin/bhyve/amd64/mem_x86.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/amd64/mem_x86.c
@@ -0,0 +1,82 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <sys/tree.h>
+#include <machine/vmm.h>
+
+#include <stdio.h>
+
+#include "debug.h"
+#include "mem.h"
+
+static int
+no_mem_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr __unused,
+ int size, uint64_t *val, void *arg1 __unused, long arg2 __unused)
+{
+ if (dir == MEM_F_READ) {
+ switch (size) {
+ case 1:
+ *val = 0xff;
+ break;
+ case 2:
+ *val = 0xffff;
+ break;
+ case 4:
+ *val = 0xffffffff;
+ break;
+ case 8:
+ *val = 0xffffffffffffffff;
+ break;
+ }
+ }
+ return (0);
+}
+
+static struct mem_range fb_entry = {
+ .handler = no_mem_handler,
+ .base = 0,
+ .size = 0xffffffffffffffff,
+};
+
+/*
+ * x86 hardware ignores writes without receiver, and returns all 1's
+ * from reads without response to transaction.
+ */
+int
+mmio_handle_non_backed_mem(struct vcpu *vcpu __unused, uint64_t paddr,
+ struct mem_range **mr_paramp)
+{
+ *mr_paramp = &fb_entry;
+ EPRINTLN("Emulating access to non-existent address to %#lx\n",
+ paddr);
+ return (0);
+}
diff --git a/usr.sbin/bhyve/mem.h b/usr.sbin/bhyve/mem.h
--- a/usr.sbin/bhyve/mem.h
+++ b/usr.sbin/bhyve/mem.h
@@ -53,6 +53,8 @@
void init_mem(int ncpu);
int emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie,
struct vm_guest_paging *paging);
+int mmio_handle_non_backed_mem(struct vcpu *vcpu __unused, uint64_t paddr,
+ struct mem_range **mr_paramp);
int read_mem(struct vcpu *vpu, uint64_t gpa, uint64_t *rval, int size);
int register_mem(struct mem_range *memp);
diff --git a/usr.sbin/bhyve/mem.c b/usr.sbin/bhyve/mem.c
--- a/usr.sbin/bhyve/mem.c
+++ b/usr.sbin/bhyve/mem.c
@@ -33,6 +33,7 @@
*/
#include <sys/types.h>
+#define _WANT_KERNEL_ERRNO 1
#include <sys/errno.h>
#include <sys/tree.h>
#include <machine/vmm.h>
@@ -167,10 +168,13 @@
access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg)
{
struct mmio_rb_range *entry;
+ struct mem_range *mr;
int err, perror, immutable, vcpuid;
vcpuid = vcpu_id(vcpu);
+ mr = NULL;
pthread_rwlock_rdlock(&mmio_rwlock);
+
/*
* First check the per-vCPU cache
*/
@@ -185,14 +189,22 @@
if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
/* Update the per-vCPU cache */
mmio_hint[vcpuid] = entry;
- } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
- perror = pthread_rwlock_unlock(&mmio_rwlock);
- assert(perror == 0);
- return (ESRCH);
+ } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr,
+ &entry) == 0) {
+ } else {
+ err = mmio_handle_non_backed_mem(vcpu, paddr, &mr);
+ if (err != 0) {
+ perror = pthread_rwlock_unlock(&mmio_rwlock);
+ assert(perror == 0);
+ return (err == EJUSTRETURN ? 0 : err);
+ }
}
}
- assert(entry != NULL);
+ if (mr == NULL) {
+ assert(entry != NULL);
+ mr = &entry->mr_param;
+ }
/*
* An 'immutable' memory range is guaranteed to be never removed
@@ -205,13 +217,13 @@
* deadlock on 'mmio_rwlock'. However by registering the extended
* config space window as 'immutable' the deadlock can be avoided.
*/
- immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
+ immutable = (mr->flags & MEM_F_IMMUTABLE) != 0;
if (immutable) {
perror = pthread_rwlock_unlock(&mmio_rwlock);
assert(perror == 0);
}
- err = cb(vcpu, paddr, &entry->mr_param, arg);
+ err = cb(vcpu, paddr, mr, arg);
if (!immutable) {
perror = pthread_rwlock_unlock(&mmio_rwlock);
diff --git a/usr.sbin/bhyve/mem_md.c b/usr.sbin/bhyve/mem_md.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/mem_md.c
@@ -0,0 +1,44 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <sys/tree.h>
+#include <machine/vmm.h>
+#include <machine/vmm_instruction_emul.h>
+
+#include "mem.h"
+
+int
+mmio_handle_non_backed_mem(struct vcpu *vcpu __unused, uint64_t paddr __unused,
+ struct mem_range **mr_paramp __unused)
+{
+ return (ESRCH);
+}
diff --git a/usr.sbin/bhyve/riscv/Makefile.inc b/usr.sbin/bhyve/riscv/Makefile.inc
--- a/usr.sbin/bhyve/riscv/Makefile.inc
+++ b/usr.sbin/bhyve/riscv/Makefile.inc
@@ -1,5 +1,6 @@
SRCS+= \
- fdt.c
+ fdt.c \
+ mem_md.c
.PATH: ${BHYVE_SYSDIR}/sys/riscv/vmm
SRCS+= vmm_instruction_emul.c
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Feb 9, 12:01 AM (16 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28512915
Default Alt Text
D50116.id155180.diff (10 KB)
Attached To
Mode
D50116: bhyve: when accessing non-backed gpa, emulate hw
Attached
Detach File
Event Timeline
Log In to Comment