Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1700099iog; Tue, 14 Jun 2022 11:12:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+HjPJ5oRuLRDJn44OKkL3MfMxAly+3kKhjFVyUQ6bOxKKKTcvgP7ngUhUP/eefDJAAbeG X-Received: by 2002:a17:902:f2ca:b0:167:8898:bad0 with SMTP id h10-20020a170902f2ca00b001678898bad0mr5395892plc.170.1655230334558; Tue, 14 Jun 2022 11:12:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655230334; cv=none; d=google.com; s=arc-20160816; b=klHuk4SoYl+1HvWQ8hTj0c+46Zlj3GN7Hus5QHdWqpFt5WLtMBh+1AYxxWmKI22ge4 8MifA6Uv62G7telDYTz6FefODoXdvNOmqAeOPEklcXdg7HNPbEFfUBYc8Bh6XnRxg91F eITBHPjoVbAnySKwY2JVooTOydsfYVjlqEfUyd/VjItCsl4tLaRs7yqSB6l8XiMHk+hc 6MyHsEEHOCvYPgyMWOYXqnFi6769kVSJlUvn0K/IMTXdwaZ6yBy7NAWtdKzdAx4RtZfK mVO/cQJghGK4JGfN8R4PTQG8kEv2ZCQNRdHZkepqpEV9Fq2+i7zIOMIt72+M7XsbHm/n h+hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=C78v/CAN8C2z22MVaDl0QxTyyJoCfPguMq8MOK3tOJc=; b=YOE5EATGQYOEZvrKoKHsALExE6D9usvDDcb2tQMYz1zJ7M4qwdp1ReBLk4WaAz8hDZ jnr0Ld/amanwyoMMhAYVO/B1FTj9bnxPMoH63s/6NfgLRm8GP+ufHHMVtQPXNTvafmB2 ISgbH5g3zvgLWDyvx4r+Ptafs5EFXhByu2CR7jVfad4Sic5/tglHembaVLhSwYrkjdcv vqWWll0mtWA9ZpR+Ms6bXfqwPz9XBQfPyfIfkeVRUVAmAw9oDPwJkDT6S3UaS8/RCH4A h8OUsnvtYmSX3D0ScYPenE6qmXbXqe4iYkodPLdnU9jlRPrRc3vocwwz1XXSgRGJTG6m 9aTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KPQRP+y1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay10-20020a056a00300a00b0051c34f76e7esi1036840pfb.46.2022.06.14.11.12.02; Tue, 14 Jun 2022 11:12:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KPQRP+y1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245423AbiFNSIb (ORCPT + 99 others); Tue, 14 Jun 2022 14:08:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234330AbiFNSI3 (ORCPT ); Tue, 14 Jun 2022 14:08:29 -0400 Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD5EB34B93 for ; Tue, 14 Jun 2022 11:08:28 -0700 (PDT) Received: by mail-yb1-xb2d.google.com with SMTP id e184so16473550ybf.8 for ; Tue, 14 Jun 2022 11:08:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=C78v/CAN8C2z22MVaDl0QxTyyJoCfPguMq8MOK3tOJc=; b=KPQRP+y1/9biwSKOdmOrA7l+Ng8mYs2ZKqEsk1EcYmkGcOWMD3eVJOVUvBbrZYLqLa XxmEyIa3+nMLYvLo8wsYQD6u2h6vJ69PlvvEWBU4hwT/ftXEt28sTdms2tNq2oqAwkJ2 xCAMYyeEgKvWmnPoO7td7+2N+gClf9AyQwwCLajgaaQu89TWxl4Z3nrdDmLCzj2nvrDt iEDgjcTO60gIUOVeCKdV4ZZl/HvoU7NaI0kNlP+lV1x3XSzN1cMxBhRoG5rEWorAbSrH Bc+DXxtEPmMLZj2PPf9qEivHmu0mikMk2gU1zLrWRRh2zuFLQceSL6wcyYVB3HSoZP1/ nNTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=C78v/CAN8C2z22MVaDl0QxTyyJoCfPguMq8MOK3tOJc=; b=pCq4qGIJPl5+RSRUvt2p/o3e0z2Y0KIBSCyZEMUIB2elLt5x/4JQzXs5+1CrVxl0e2 2l2gFwmlou8X0u2W6oieqHwRQLOw5EEkhZqvkHcTorpVe5HrgElv6zeidwfxgOtriHeR 6Q2tfGs4w+A07lK3mz+p63gUmN/reURCrvC1iYrVYWkW+ofFcuYWkdMjqfFjtdiCDt2K /xM7wuFqMZ0OwF/KBfRHJT8T7/PIc+NmspMcub4bnmQCY+dcKWK5NQEOuUVeROQh4ZrJ TTSK25De071ShT9T/BW/bCwm12z7T88pq+XHAKkc7oBB1muFWb89TXqbeTNbcfoyd8Yg dXuQ== X-Gm-Message-State: AJIora8QvKqbpusARNcdGWTp5NHaBZGMXx0WL/bSPA2KzR4XJZFbRLWf B+Shpw/g33k5T0cqZuc4ZFMOYQc+jcUw/bs7n+03Fw== X-Received: by 2002:a25:5d0d:0:b0:633:25c8:380 with SMTP id r13-20020a255d0d000000b0063325c80380mr5966062ybb.167.1655230107606; Tue, 14 Jun 2022 11:08:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Sagi Shahar Date: Tue, 14 Jun 2022 11:08:16 -0700 Message-ID: Subject: Re: [RFC PATCH v6 093/104] KVM: TDX: Handle TDX PV MMIO hypercall To: "Yamahata, Isaku" Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , Erdem Aktas , Sean Christopherson Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 5, 2022 at 11:16 AM wrote: > > From: Sean Christopherson > > Export kvm_io_bus_read and kvm_mmio tracepoint and wire up TDX PV MMIO > hypercall to the KVM backend functions. > > kvm_io_bus_read/write() searches KVM device emulated in kernel of the given > MMIO address and emulates the MMIO. As TDX PV MMIO also needs it, export > kvm_io_bus_read(). kvm_io_bus_write() is already exported. TDX PV MMIO > emulates some of MMIO itself. To add trace point consistently with x86 > kvm, export kvm_mmio tracepoint. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > Reviewed-by: Paolo Bonzini > --- > arch/x86/kvm/vmx/tdx.c | 114 +++++++++++++++++++++++++++++++++++++++++ > arch/x86/kvm/x86.c | 1 + > virt/kvm/kvm_main.c | 2 + > 3 files changed, 117 insertions(+) > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > index ee0cf5336ade..6ab4a52fc9e9 100644 > --- a/arch/x86/kvm/vmx/tdx.c > +++ b/arch/x86/kvm/vmx/tdx.c > @@ -1057,6 +1057,118 @@ static int tdx_emulate_io(struct kvm_vcpu *vcpu) > return ret; > } > > +static int tdx_complete_mmio(struct kvm_vcpu *vcpu) > +{ > + unsigned long val = 0; > + gpa_t gpa; > + int size; > + > + WARN_ON(vcpu->mmio_needed != 1); > + vcpu->mmio_needed = 0; > + > + if (!vcpu->mmio_is_write) { > + gpa = vcpu->mmio_fragments[0].gpa; > + size = vcpu->mmio_fragments[0].len; > + > + memcpy(&val, vcpu->run->mmio.data, size); > + tdvmcall_set_return_val(vcpu, val); > + trace_kvm_mmio(KVM_TRACE_MMIO_READ, size, gpa, &val); > + } > + return 1; > +} > + > +static inline int tdx_mmio_write(struct kvm_vcpu *vcpu, gpa_t gpa, int size, > + unsigned long val) > +{ > + if (kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && > + kvm_io_bus_write(vcpu, KVM_MMIO_BUS, gpa, size, &val)) > + return -EOPNOTSUPP; > + > + trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, size, gpa, &val); > + return 0; > +} > + > +static inline int tdx_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, int size) > +{ > + unsigned long val; > + > + if (kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && > + kvm_io_bus_read(vcpu, KVM_MMIO_BUS, gpa, size, &val)) > + return -EOPNOTSUPP; > + > + tdvmcall_set_return_val(vcpu, val); > + trace_kvm_mmio(KVM_TRACE_MMIO_READ, size, gpa, &val); > + return 0; > +} > + > +static int tdx_emulate_mmio(struct kvm_vcpu *vcpu) > +{ > + struct kvm_memory_slot *slot; > + int size, write, r; > + unsigned long val; > + gpa_t gpa; > + > + WARN_ON(vcpu->mmio_needed); > + > + size = tdvmcall_a0_read(vcpu); > + write = tdvmcall_a1_read(vcpu); > + gpa = tdvmcall_a2_read(vcpu); > + val = write ? tdvmcall_a3_read(vcpu) : 0; > + > + if (size != 1 && size != 2 && size != 4 && size != 8) > + goto error; > + if (write != 0 && write != 1) > + goto error; > + > + /* Strip the shared bit, allow MMIO with and without it set. */ > + gpa = gpa & ~gfn_to_gpa(kvm_gfn_shared_mask(vcpu->kvm)); > + > + if (size > 8u || ((gpa + size - 1) ^ gpa) & PAGE_MASK) > + goto error; > + > + slot = kvm_vcpu_gfn_to_memslot(vcpu, gpa_to_gfn(gpa)); > + if (slot && !(slot->flags & KVM_MEMSLOT_INVALID)) > + goto error; > + > + if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { > + trace_kvm_fast_mmio(gpa); > + return 1; > + } > + > + if (write) > + r = tdx_mmio_write(vcpu, gpa, size, val); > + else > + r = tdx_mmio_read(vcpu, gpa, size); > + if (!r) { > + /* Kernel completed device emulation. */ > + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); > + return 1; > + } > + > + /* Request the device emulation to userspace device model. */ > + vcpu->mmio_needed = 1; > + vcpu->mmio_is_write = write; > + vcpu->arch.complete_userspace_io = tdx_complete_mmio; > + > + vcpu->run->mmio.phys_addr = gpa; > + vcpu->run->mmio.len = size; > + vcpu->run->mmio.is_write = write; > + vcpu->run->exit_reason = KVM_EXIT_MMIO; > + > + if (write) { > + memcpy(vcpu->run->mmio.data, &val, size); > + } else { > + vcpu->mmio_fragments[0].gpa = gpa; > + vcpu->mmio_fragments[0].len = size; > + trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, size, gpa, NULL); > + } > + return 0; > + > +error: > + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); We should return an error code here. > + return 1; > +} > + > static int handle_tdvmcall(struct kvm_vcpu *vcpu) > { > if (tdvmcall_exit_type(vcpu)) > @@ -1069,6 +1181,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) > return tdx_emulate_hlt(vcpu); > case EXIT_REASON_IO_INSTRUCTION: > return tdx_emulate_io(vcpu); > + case EXIT_REASON_EPT_VIOLATION: > + return tdx_emulate_mmio(vcpu); > default: > break; > } > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 5f291470a6f6..f367d0dcef97 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -13166,6 +13166,7 @@ bool kvm_arch_dirty_log_supported(struct kvm *kvm) > > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); > +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_mmio); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 4bf7178e42bd..7f01131666de 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -2294,6 +2294,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn > > return NULL; > } > +EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot); > > bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) > { > @@ -5169,6 +5170,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, > r = __kvm_io_bus_read(vcpu, bus, &range, val); > return r < 0 ? r : 0; > } > +EXPORT_SYMBOL_GPL(kvm_io_bus_read); > > /* Caller must hold slots_lock. */ > int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, > -- > 2.25.1 > Sagi