Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp550814rwb; Thu, 12 Jan 2023 09:13:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXtMxkGK/o/oYU4dMMO6sIy7LahL6vG+qP+SBR58fS00zHhyobZ6oAHKFZKYgDlO9gZr7spj X-Received: by 2002:a17:906:2403:b0:84d:3e5b:7c02 with SMTP id z3-20020a170906240300b0084d3e5b7c02mr161945eja.22.1673543580613; Thu, 12 Jan 2023 09:13:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543580; cv=none; d=google.com; s=arc-20160816; b=fcbIzpHYpWQqz/vgHlyz7jVWkN3iQwXEvRiz1MlI7xHsaEKwtNJb4VCa/l4muU1NtY oi1TGfK5oWW8EsplTsbDsL65IxCsGstttwx11eQ7+4zebL5Lk2NsuC+MSxv0zjtknQUO K9ATsE9kW9SEpNSeQiA0YHE8v70FboD5cr0yADyT/yVPXd0rd0U+WgtKRPBYIssLrYcb wVJVELNpdmPEi/jSfuZY1zDU20hv8VGWYbLPAhr3yDwrxI8qoJruciUwhz41FdvtAPM/ Kt0YvdCcGkCB2egUfB1y94JAu4O71DFkGrKh8TZ4oJoVBFXOmwi99mv4RGQvYO6CWBdU SC1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aytG8BIcuv1eTwAVuZCLZuZGeAQLFxdIAcgLUyAk9No=; b=S9OUTAPCLY3houRzhOojCdRa1C2GJ6RLuGuim07Wn3XmIMyhnKNK9h4vRB2Az62c7i rlkMomNw6ER5DlRrJGg2PKpNautFHJsSGHcs33ZsBAAGFsRS0imBmWe/V2HMPMwzkez7 M3k6FfMAVEj6UI+lz/r3Q6/jJDuOj20qNX2Am2wnRrRcluaIxDzj4JVK3clXoMrHmM+v /FAGBtgrcFEScRVLqKEVMAuup7lPY2bWCPlwyTZTFC6ibajViGHCHL5eT5ZVaTyytwc6 m7rDJQh2hB9eDBXGE3XOnonsz5URG1/ixV1GGBcfBM9K0UudH2HUSCPIRE7AZxQ0xWlG My8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YLScHOjS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw3-20020a1709066a0300b0084d11b32f2esi19081465ejc.980.2023.01.12.09.12.48; Thu, 12 Jan 2023 09:13:00 -0800 (PST) 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=@intel.com header.s=Intel header.b=YLScHOjS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240437AbjALQkD (ORCPT + 51 others); Thu, 12 Jan 2023 11:40:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231174AbjALQhZ (ORCPT ); Thu, 12 Jan 2023 11:37:25 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 498FFDEB1; Thu, 12 Jan 2023 08:33:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541225; x=1705077225; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UP1WpdheYeJvVdvirp+Vpfoecm7bqXd9sD8L7PqyirY=; b=YLScHOjSc7C6dMAQixuafQnIpGh0tWRqaIGj7ITxlpWqljJ4AXtA+yxF MmU69M/ZrTvVSu2lOt2ihb1YAOdAx1STwmxxkXMRhdHMF2SD559jXoqd5 ySdJsp4qodzgRu/WCJw0KaiEyrRjo7m2sknOnw1UaIsfOdYa1LkeOoXja HFh9z52BWsmfXiFZNJV3jrMuhJqN0+d+lfT/4Yl+1+C806pcRANMX6jHe H+4Sc+Od5DrTg3aGIY1DhK7Sf4n0dTGBf3ay/ifFpgtBPDycJ+Iy2vZrw Dwx93t93CRRO9CxeG3YTGNITzkhzRc0Jpu5hYuklSPhAU66Bw5qUv1xiJ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="386089734" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="386089734" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:36 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="726372565" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="726372565" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:36 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack Subject: [PATCH v11 094/113] KVM: TDX: Handle TDX PV port io hypercall Date: Thu, 12 Jan 2023 08:32:42 -0800 Message-Id: <97ba08e8c45eec34d9b283f4a5f8de3767397422.1673539699.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 From: Isaku Yamahata Wire up TDX PV port IO hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini --- arch/x86/kvm/vmx/tdx.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 3dcfbdd37579..88318a80e6de 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -979,6 +979,61 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu) return kvm_emulate_halt_noskip(vcpu); } +static int tdx_complete_pio_in(struct kvm_vcpu *vcpu) +{ + struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; + unsigned long val = 0; + int ret; + + WARN_ON_ONCE(vcpu->arch.pio.count != 1); + + ret = ctxt->ops->pio_in_emulated(ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val, 1); + WARN_ON_ONCE(!ret); + + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + tdvmcall_set_return_val(vcpu, val); + + return 1; +} + +static int tdx_emulate_io(struct kvm_vcpu *vcpu) +{ + struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; + unsigned long val = 0; + unsigned int port; + int size, ret; + bool write; + + ++vcpu->stat.io_exits; + + size = tdvmcall_a0_read(vcpu); + write = tdvmcall_a1_read(vcpu); + port = tdvmcall_a2_read(vcpu); + + if (size != 1 && size != 2 && size != 4) { + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INVALID_OPERAND); + return 1; + } + + if (write) { + val = tdvmcall_a3_read(vcpu); + ret = ctxt->ops->pio_out_emulated(ctxt, size, port, &val, 1); + + /* No need for a complete_userspace_io callback. */ + vcpu->arch.pio.count = 0; + } else { + ret = ctxt->ops->pio_in_emulated(ctxt, size, port, &val, 1); + if (!ret) + vcpu->arch.complete_userspace_io = tdx_complete_pio_in; + else + tdvmcall_set_return_val(vcpu, val); + } + if (ret) + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + return ret; +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -989,6 +1044,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) return tdx_emulate_cpuid(vcpu); case EXIT_REASON_HLT: return tdx_emulate_hlt(vcpu); + case EXIT_REASON_IO_INSTRUCTION: + return tdx_emulate_io(vcpu); default: break; } -- 2.25.1