From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 795CB26E708 for ; Wed, 25 Feb 2026 01:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982477; cv=none; b=AchLQLNKn7QRbzXDwiAG12EDybOQkM1esmdGsiXdgQhB0a+UhgGTgj8QAT5MrvlX5sBDoaXeC7zyhqo6u/6Gl5NMzzSYvjGsUNs2QYQe2alWuFz8Agep3UEAoFAWwg3xH6HaWwXLWJwdK+b/mNuS7NgDpXU6XbNjFQVhkj6eKJg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982477; c=relaxed/simple; bh=85i39OWQhVtkn+KmWvGw+H0S5qjkbaqht3+QjSVkoLI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aWVCXuPwuLUWBR6i58TxqHFWB1mNNfkhq1s4QQ2XrwISQTTIibTf6uvDPrkqRa2rJdwoufHx236jp7dVakCT998m2urmPi3r3gcczmL9E/h0Pc5vEtADQgohcEXwNFYvjwLsdu2ADIKvLXX+bLX1VZnRPDtaU8w5O9LQUlwQjo4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WxnGNGx9; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WxnGNGx9" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-358dc09b43eso1361279a91.2 for ; Tue, 24 Feb 2026 17:21:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771982475; x=1772587275; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=PEtXjeylvLkO6uPQdY2VXz1BpnUVZoay6R5c8Z2Fy64=; b=WxnGNGx9MHeq6Vnm9AblAYlxLtsfYWdaxUYa51xS2cIYozLVBka5m3nTHIYUOwfeMF e0e6LBhGb0UHaY1JDiWeCSDeox/awWImdp49hc9f//17RYa97DWlCPAq0hRw+R8U5q5X 9lt6KCyq4G68ZLtNKoQC0yXclx9CF7Hnya2VrZj1EB0N0lI0M1LngyV9wnH1YkSbb0W5 2osq3hozdRJAgImHkNE6NXHyEOqR6dYZVXZK3HRJAhzacHEDsAurB9+lnxJuOTZLr7Z0 gcq+J/PVrMKqZqYoQzh5yRQ6zkXVaKDC/4K6xc4k9Pg89EFa8dEZEhU2KTwBvPVVXzzI daBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771982475; x=1772587275; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PEtXjeylvLkO6uPQdY2VXz1BpnUVZoay6R5c8Z2Fy64=; b=KY3+kkBNtHU2tvU52pD6Z1FS6koMj0URFLAgo3oaV2V6O8YCYCJDO1nW6b2Y3KEKC0 el0VZ7Wx0SCmwBwYUnFlyX5pZpouLrgVbRLn2GFadSd4gnHmKYYT3KNbaWBZ2ylxco1v xz4jK6Sc1SpqdLA/OGaWWVoxqiRP18U62YqGlBzieWvJ/b2XkyegtTltuF52N0cKpkFO wrt2giuj+vnD8zKvzJvmfbtPp4p9MUpeDNv8Rqrm2XmjvvdQFNvgybtcbEk1fqrdDYKr Ah5emKhDaUERaAs9O8vpWsV2kEysggTiRHdtPmTXlDVyLn1YqFe/Xff+prs0xjlbYnt2 /IGg== X-Forwarded-Encrypted: i=1; AJvYcCVRMbNb7EE/LImsi4olyemDhRGOm2ZmRmnH1V4QtiufFnVWzfX4c+jZi6ykGwCkm6StrKFtYCbFR+EdErM=@vger.kernel.org X-Gm-Message-State: AOJu0YzDR85WoHhlwEzEwD0IhSkHMCs7rChnrHho0yTzZbgCADxMc7lq BW1cB2v0JCSJziSkppbZiN78758zo5YK74TobPipccHtC0t4HespY3sY6a55ZLm83WBU9FixdMc TxrSdCA== X-Received: from pgbdw17.prod.google.com ([2002:a05:6a02:4491:b0:c6e:2e2c:d4a2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:cf85:b0:395:9d89:db69 with SMTP id adf61e73a8af0-3959f270843mr632443637.7.1771982474849; Tue, 24 Feb 2026 17:21:14 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Feb 2026 17:20:47 -0800 In-Reply-To: <20260225012049.920665-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260225012049.920665-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260225012049.920665-13-seanjc@google.com> Subject: [PATCH 12/14] KVM: x86: Rename .read_write_emulate() to .read_write_guest() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Kiryl Shutsemau Cc: kvm@vger.kernel.org, x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Yashu Zhang , Rick Edgecombe , Binbin Wu , Xiaoyao Li , Tom Lendacky , Michael Roth Content-Type: text/plain; charset="UTF-8" Rename the ops and helpers to read/write guest memory to clarify that they do exactly that, i.e. aren't generic emulation flows and don't do anything related to emulated MMIO. Opportunistically add comments to explain the flow, e.g. it's not exactly obvious that KVM deliberately treats "failed" accesses to guest memory as emulated MMIO. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 5a3ba161db7b..f3e2ec7e1828 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8103,21 +8103,21 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, } struct read_write_emulator_ops { - int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes); + int (*read_write_guest)(struct kvm_vcpu *vcpu, gpa_t gpa, + void *val, int bytes); int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val); bool write; }; -static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes) +static int emulator_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, + void *val, int bytes) { return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); } -static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes) +static int emulator_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, + void *val, int bytes) { int ret; @@ -8157,11 +8157,22 @@ static int emulator_read_write_onepage(unsigned long addr, void *val, return X86EMUL_PROPAGATE_FAULT; } - if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) + /* + * If the memory is not _known_ to be emulated MMIO, attempt to access + * guest memory. If accessing guest memory fails, e.g. because there's + * no memslot, then handle the access as MMIO. Note, treating the + * access as emulated MMIO is technically wrong if there is a memslot, + * i.e. if accessing host user memory failed, but this has been KVM's + * historical ABI for decades. + */ + if (!ret && ops->read_write_guest(vcpu, gpa, val, bytes)) return X86EMUL_CONTINUE; /* - * Is this MMIO handled locally? + * Attempt to handle emulated MMIO within the kernel, e.g. for accesses + * to an in-kernel local or I/O APIC, or to an ioeventfd range attached + * to MMIO bus. If the access isn't fully resolved, insert an MMIO + * fragment with the relevant details. */ handled = ops->read_write_mmio(vcpu, gpa, bytes, val); if (handled == bytes) @@ -8182,6 +8193,13 @@ static int emulator_read_write_onepage(unsigned long addr, void *val, frag->data = val; } frag->len = bytes; + + /* + * Continue emulating, even though KVM needs to (eventually) do an MMIO + * exit to userspace. If the access splits multiple pages, then KVM + * needs to exit to userspace only after emulating both parts of the + * access. + */ return X86EMUL_CONTINUE; } @@ -8279,7 +8297,7 @@ static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, struct x86_exception *exception) { static const struct read_write_emulator_ops ops = { - .read_write_emulate = read_emulate, + .read_write_guest = emulator_read_guest, .read_write_mmio = vcpu_mmio_read, .write = false, }; @@ -8294,7 +8312,7 @@ static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, struct x86_exception *exception) { static const struct read_write_emulator_ops ops = { - .read_write_emulate = write_emulate, + .read_write_guest = emulator_write_guest, .read_write_mmio = vcpu_mmio_write, .write = true, }; -- 2.53.0.414.gf7e9f6c205-goog