From nobody Thu May 22 03:25:46 2025 X-Original-To: dev-commits-src-all@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 4b2twV2slPz5wysS; Thu, 22 May 2025 03:25:46 +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 4b2twV1rmzz3Gw5; Thu, 22 May 2025 03:25:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747884346; 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=0lcRx0VrcFkAluQrlNWQYI1KVdZf1mj+wdecaNKFL9Q=; b=Pvw1TQpLgx54DWyFbCqKoXckNxcQ5IGwi5lkv84xn4DiEOzw6y4w/2Hsvi3zveDoADPVMO 9akPkYehxCz46M8cTaeU+aAD4oy+C0cYROXg0RQQlvQSFdniMnm8n72l+vUnSSbp8bgIfs 9jb2DzG1/4gADGkkeADWdCMcv5ywqbVetBaCuzxlgrkcKQ7EL2+55yEeARYZTWbXVRGN27 s9Vv8Sqek2OxyTDjhJXMG3uRHtogRmcBABfUkYimUceyiGu09n0cj0ABDz8x7DNPKrWkwu RbJ7WPslga4LEinyH7P/xlZ6W5QgMitWTHF45P9sAiGTuG49A7ROsu+KbgnPBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747884346; 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=0lcRx0VrcFkAluQrlNWQYI1KVdZf1mj+wdecaNKFL9Q=; b=ww1tJkg9jffGjHM4TtDxX4OuYXO3YXErxTmXmlntDignYbaNX3G4GFYFGJlfBmotS3R+Bw FWR9WhjJ5HzBhQ60DCtqB4zpZgFbkKurDu9O6JxKAIWYg9TfxnLoPGc5dh+W4/LIPak6Oy njAx+35kQachQjVy3/IaptaMI+PaOIjUHSeKHFKaazr0toO59sYWVgUCWMWxSlG13EDtVj YtI1OVedFmfF/naYBsIK8TOy+d2AVqHBbDBf87PJm3f09SBHdHv7GKuEb/phPpnkM23e/G aB61zROzzlEf3xxwLBBFCvGBPlF36AFRCt9RWceaeoIVOgEegvhkb8d4P4iHYg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1747884346; a=rsa-sha256; cv=none; b=sKs2jySkaZvjP0LhTxPfnadOcLBtT9x5yXVAvf82bLMjMH9nEI/KFazI9yQQ+BWeWwDo/u foUUd87VBIkw/WYSdeEi6ip8vfdiDF3qyweRx+NgP0TDmuYO4TcGwiGOxrYaZ3uGKTNEwS 7NqumwESbHbNWKOR/nUO+Jb3cVTDiO2JVljyOIK7y2Uf6I9sHa3pDSorCt1HPufj+W1exm R2z1kRXeFukrznJdpsbctLyLDfbFq7fng3Ch0+syxM8r65i2AlcEhDFN8fMUdkb3SXULcQ iKGceqY4I0WHziouB1G7f1UXGlsNFiKS2IAdb9iPvK/ACipGyBOHPfoM7O/pqw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4b2twV1Lp6z13Q9; Thu, 22 May 2025 03:25:46 +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 54M3PkVG064414; Thu, 22 May 2025 03:25:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 54M3PkdC064411; Thu, 22 May 2025 03:25:46 GMT (envelope-from git) Date: Thu, 22 May 2025 03:25:46 GMT Message-Id: <202505220325.54M3PkdC064411@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: e877ef51e2b3 - stable/14 - bhyve: make code to handle non-backed memory accesses MD List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: e877ef51e2b3ff351d77e50810413983ec277785 Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=e877ef51e2b3ff351d77e50810413983ec277785 commit e877ef51e2b3ff351d77e50810413983ec277785 Author: Konstantin Belousov AuthorDate: 2025-05-01 07:11:00 +0000 Commit: Konstantin Belousov CommitDate: 2025-05-22 03:25:30 +0000 bhyve: make code to handle non-backed memory accesses MD PR: 286467 (cherry picked from commit e8b9d839b1fae2913dc861341b9363d41cd45173) --- usr.sbin/bhyve/amd64/Makefile.inc | 1 + usr.sbin/bhyve/mem.c | 26 ++++++++++++++++------- usr.sbin/bhyve/mem.h | 2 ++ usr.sbin/bhyve/mem_md.c | 44 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/usr.sbin/bhyve/amd64/Makefile.inc b/usr.sbin/bhyve/amd64/Makefile.inc index bc0ffa720d31..f8103ae1be32 100644 --- a/usr.sbin/bhyve/amd64/Makefile.inc +++ b/usr.sbin/bhyve/amd64/Makefile.inc @@ -7,6 +7,7 @@ SRCS+= \ inout.c \ ioapic.c \ kernemu_dev.c \ + mem_md.c \ mptbl.c \ pci_ahci.c \ pci_e82545.c \ diff --git a/usr.sbin/bhyve/mem.c b/usr.sbin/bhyve/mem.c index b4cb93a1cea4..e32d7bd571a8 100644 --- a/usr.sbin/bhyve/mem.c +++ b/usr.sbin/bhyve/mem.c @@ -34,6 +34,7 @@ #include #include +#define _WANT_KERNEL_ERRNO 1 #include #include #include @@ -168,10 +169,13 @@ static int 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 */ @@ -186,14 +190,22 @@ access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg) 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 @@ -206,13 +218,13 @@ access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg) * 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.h b/usr.sbin/bhyve/mem.h index 90172d1c9124..d7544a6bce30 100644 --- a/usr.sbin/bhyve/mem.h +++ b/usr.sbin/bhyve/mem.h @@ -53,6 +53,8 @@ struct mem_range { 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_md.c b/usr.sbin/bhyve/mem_md.c new file mode 100644 index 000000000000..0e29ec0a9de9 --- /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 + * 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 +#include +#include +#include +#include + +#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); +}