Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp1461229rwj; Sun, 30 Oct 2022 00:49:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4UK0+QMBIAi1kzGhFBhn+w//GaXaFE5Kseyx2OoTD2PRPlz9VqqMq1zvQwq41e4XnIittX X-Received: by 2002:a63:2547:0:b0:46f:793f:197b with SMTP id l68-20020a632547000000b0046f793f197bmr7397462pgl.35.1667116160125; Sun, 30 Oct 2022 00:49:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667116160; cv=none; d=google.com; s=arc-20160816; b=TQFsxsToFitZRN0S/q3hqCLOPXIp36WAwwMO1uq8/iWsdPvPpoohOSTXqpInYUWDA6 MK66hTUb3GVi/E91VDyIpsiSRPybpoD1tlhksrO8xPRMmNJVUEgK1I5q+7DAW+FqAWBO RL6xSaOA1M/3KkU8jW+kRdioa5n53CD9qfuacmsIggHwWVQQMJwVVqepXPqaYntfq1BS jl9FV5omIByQs4zuX1J6/SdBlyOqUVT2G9At4s75OVsB8cSN/v+viQBU4GHAgflcCteX RxFR8PCgNnqZYaaM+fJfX/xD3pSAaByIAWFULxhbd+GvNRnmrmb9KKGyMm41ABW8xFgL ZOmA== 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=xhvsN4DKyYTYyCr62IO7iptcab7oqDj6VGdS8rkUjXs=; b=YbKkBS0FI5pJIFJjIHjhevjp25Kui9hfGGx4XxfmYCfLb6R2Jp+84c9RXVI6sKzF/C rrzonX5IV1M+JBMjIhNLd/cLhvqod9SelsbCTgF+1orXL52ERas2Yj/zaEb+p9E///Xm XB0lUpoaF/6Ijt5vT6QLbxsfr9Fn0R/hF2sR0sTGObLwDGfAiHq6JCxAULb4cpMupsRv hIceip+ZF8a2iR+b4bDWnkO+ietaURJVjnz17r//tdz6kxSs/4cpaEbRqZgZDDKRl71Y h1shMdiZgRUuUP+39SbDT6m011XiUmBFUEU3kmTGPlqIEQ8r3NpHp2kcQiGV3/JKOee9 drHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KSBAc0WL; 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 w19-20020a170902a71300b00176a1183050si4106665plq.80.2022.10.30.00.49.08; Sun, 30 Oct 2022 00:49:20 -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=@intel.com header.s=Intel header.b=KSBAc0WL; 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 S231467AbiJ3GdJ (ORCPT + 99 others); Sun, 30 Oct 2022 02:33:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231450AbiJ3Gb6 (ORCPT ); Sun, 30 Oct 2022 02:31:58 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F07A8BA7; Sat, 29 Oct 2022 23:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667111102; x=1698647102; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dUD7oj5Uxf9yB14+vLG/PkVhuzTk5E78JzXU65V215o=; b=KSBAc0WLYyVXRtBApcIp6Uc5GFiJax/nknIlym89ayr118furRQtOZuE pgvvKsFKetCALWVEEPLwfKPLbpS5SrHz1sMFuByNcwPWS05znr74chUJy BsdMU9H+js0p9zq4bwxxbT4zeq7Nrd+g/q5olyvPSHd6ZiLxPhumn96V8 JVi28q3kuOB3VUQ7xWEVXfz9J93ndS+WPqevT2e1GqdQe+aX+LrCfcscu Uo/EnqSLJg/KZtcUm04MfC4x0ohSosWJXZObMHAIDHZKTt8LTnbl3kKFW JTFYzBCJ03V3KFiV1l2cMz/DOanmnMeL0nkGKJTCayQKZBARxgtDqhiCj w==; X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="395037214" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="395037214" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:13 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="878393149" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="878393149" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:13 -0700 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 v10 096/108] KVM: TDX: Handle TDX PV port io hypercall Date: Sat, 29 Oct 2022 23:23:37 -0700 Message-Id: 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.9 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 03b08b3f1ff6..69a3e7007e83 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -866,6 +866,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)) @@ -876,6 +931,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