From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 D32A3262FE7 for ; Wed, 25 Feb 2026 01:21:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982473; cv=none; b=IDhe+OfOu77BAJO0zSQlM3/lrirEtTLhCxDjZvz6XCwS+wjb0srO3ESf57ZzCosPTM2R6rWzUdyqQWuIlP12Wd8h6vj3tXzL0OE0ocLi0LBzqI5J0AkEXbKIpFR8UM3o3/1AjGqw0uvqr7nw6GR86iA4sppoCb2m6//tuR7rSwI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982473; c=relaxed/simple; bh=4zmfeUoFw0++ZD2R7XqZF7I3p6nxfaKrCAs/KOcytN0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DWn5dmKD6XkKpk7bU+X+PqtHFUctmjeeogPxha53nTMcv29VJ+y57qp44HVtr/FYq5Ss0pK5o9yE8vVG2S8hJW297O1eGLsLzc/j6+JnwjjJnl7oS/5LCfpx9tJDtFPdZvXvxTo+B6j7JbK72VqW6TyJR8r0Jq7E2QpT7qX2g+U= 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=O9TYJ9n7; arc=none smtp.client-ip=209.85.214.201 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="O9TYJ9n7" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2adc527eaf5so2765625ad.0 for ; Tue, 24 Feb 2026 17:21:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771982468; x=1772587268; 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=LNNapYowtT0YYRygm4WC7Vz/wQDCwBu2kBJka11VtD0=; b=O9TYJ9n7RHCsH7UGDZAJ5Ib0hBi5YppZ+tPlu+EIN8rIlN/bHFlPMI800sm+DdXgNT +JdJoPqVdHJqfMzI4wYuYRvdlAvvmnN430z0T/Xm2pRQlhN4w0IbzJhDc0ioRlx6GZLk BP9vnTrDNA0xI2ID53Q9laWvviaLXd3hYZ/JahYYgGEAwSwqkQjZZQ1Cu4Qk90u1/3AH EHiBV6HNyNvwl3lXBEr9QloNaRoqC0CrgdYHcp0eDC30W4r1TKU+kBtvNEGUE4R6wF5i 3yt50VCrGJ3gfbZwKNGYQxgzRrOg+BCjYCaunx3UJlL1bpsYjgzkHOO3ZmRJzX1j7k1L nkGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771982468; x=1772587268; 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=LNNapYowtT0YYRygm4WC7Vz/wQDCwBu2kBJka11VtD0=; b=MMpLvPf1YSzNtNLf31NyaKhkfVLG2rcprUf0yA00jm7zEtGTj3cP7YnbB5M3anT+Ga VEYoAqxt3YXzIgZImgUI88pgX4TD2YOC4vQV1wytrr8d7dbCZB6cOUy19DYI3AIwMefD opXeumFfBWpxUKGcvz/ib5gDBjaF4X1y9gfvpRLRJI1Gsv712yigQ7yONOJjUULGjUSG WSNR2upGRx8aQEWuC/BTveLTC9qrcvp2LM5NTpLDgpdESZd9eJvZWpMAidDsZTfEG6o1 CuoEdGISRUzuSBEI7Z91mSE0Zr1LhVgY8to33nqhusBNE2JuNDJ9fDwCksn3ghVdwftf pKCA== X-Forwarded-Encrypted: i=1; AJvYcCXV29duibYbqDWqsRzzgn5DiFxetgZ+uIe37+u8/Cfsd9x0Z7QtXfYndzlttrZrX04nisRKhFs3SJuca7Q=@vger.kernel.org X-Gm-Message-State: AOJu0YzhTngW19zW6gNDoe1QUy7xIL8xscXjCxrsV17xw78adaV8HCnQ vQJ1JfHtS2yuIvUeQSTAIZFN5Pm1vc4Q8o5v92gxj1alcl/yyoyjphd6e8TiF6+vfsBIyA/IWgt fcgsPwQ== X-Received: from pjbnh18.prod.google.com ([2002:a17:90b:3652:b0:358:dfd8:3150]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a8f:b0:2aa:f2ce:5ac5 with SMTP id d9443c01a7336-2ad744e0d19mr151791755ad.32.1771982468138; Tue, 24 Feb 2026 17:21:08 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Feb 2026 17:20:43 -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-9-seanjc@google.com> Subject: [PATCH 08/14] KVM: x86: Dedup kvm_sev_es_mmio_{read,write}() 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" Dedup the SEV-ES emulated MMIO code by using the read vs. write emulator ops to handle the few differences between reads and writes. Opportunistically tweak the comment about fragments to call out that KVM should verify that userspace can actually handle MMIO requests that cross page boundaries. Unlike emulated MMIO, the request is made in the GPA space, not the GVA space, i.e. emulation across page boundaries can work generically, at least in theory. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 110 +++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 65 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2db0bf738d2d..f93f0f8961af 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -14293,80 +14293,60 @@ static int complete_sev_es_emulated_mmio(struct kvm_vcpu *vcpu) return 0; } +static int kvm_sev_es_do_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned int bytes, void *data, + const struct read_write_emulator_ops *ops) +{ + struct kvm_mmio_fragment *frag; + int handled; + + if (!data || WARN_ON_ONCE(object_is_on_stack(data))) + return -EINVAL; + + handled = ops->read_write_mmio(vcpu, gpa, bytes, data); + if (handled == bytes) + return 1; + + bytes -= handled; + gpa += handled; + data += handled; + + /* + * TODO: Determine whether or not userspace plays nice with MMIO + * requests that split a page boundary. + */ + frag = vcpu->mmio_fragments; + vcpu->mmio_nr_fragments = 1; + frag->len = bytes; + frag->gpa = gpa; + frag->data = data; + + vcpu->mmio_needed = 1; + vcpu->mmio_cur_fragment = 0; + + vcpu->run->mmio.phys_addr = gpa; + vcpu->run->mmio.len = min(8u, frag->len); + vcpu->run->mmio.is_write = ops->write; + if (ops->write) + memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); + vcpu->run->exit_reason = KVM_EXIT_MMIO; + + vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; + + return 0; +} + int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, void *data) { - int handled; - struct kvm_mmio_fragment *frag; - - if (!data || WARN_ON_ONCE(object_is_on_stack(data))) - return -EINVAL; - - handled = write_emultor.read_write_mmio(vcpu, gpa, bytes, data); - if (handled == bytes) - return 1; - - bytes -= handled; - gpa += handled; - data += handled; - - /*TODO: Check if need to increment number of frags */ - frag = vcpu->mmio_fragments; - vcpu->mmio_nr_fragments = 1; - frag->len = bytes; - frag->gpa = gpa; - frag->data = data; - - vcpu->mmio_needed = 1; - vcpu->mmio_cur_fragment = 0; - - vcpu->run->mmio.phys_addr = gpa; - vcpu->run->mmio.len = min(8u, frag->len); - vcpu->run->mmio.is_write = 1; - memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); - vcpu->run->exit_reason = KVM_EXIT_MMIO; - - vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; - - return 0; + return kvm_sev_es_do_mmio(vcpu, gpa, bytes, data, &write_emultor); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_sev_es_mmio_write); int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, void *data) { - int handled; - struct kvm_mmio_fragment *frag; - - if (!data || WARN_ON_ONCE(object_is_on_stack(data))) - return -EINVAL; - - handled = read_emultor.read_write_mmio(vcpu, gpa, bytes, data); - if (handled == bytes) - return 1; - - bytes -= handled; - gpa += handled; - data += handled; - - /*TODO: Check if need to increment number of frags */ - frag = vcpu->mmio_fragments; - vcpu->mmio_nr_fragments = 1; - frag->len = bytes; - frag->gpa = gpa; - frag->data = data; - - vcpu->mmio_needed = 1; - vcpu->mmio_cur_fragment = 0; - - vcpu->run->mmio.phys_addr = gpa; - vcpu->run->mmio.len = min(8u, frag->len); - vcpu->run->mmio.is_write = 0; - vcpu->run->exit_reason = KVM_EXIT_MMIO; - - vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; - - return 0; + return kvm_sev_es_do_mmio(vcpu, gpa, bytes, data, &read_emultor); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_sev_es_mmio_read); -- 2.53.0.414.gf7e9f6c205-goog