Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3405633iog; Mon, 27 Jun 2022 15:39:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sOEdf5hBImwTVPi0v6+ugOX+HACYpGDod6d/LnuEUW8PmDOs1E/7Qdy8g1pIoQ6o/3ziMQ X-Received: by 2002:a17:906:246:b0:722:dffd:39f4 with SMTP id 6-20020a170906024600b00722dffd39f4mr15375843ejl.477.1656369542501; Mon, 27 Jun 2022 15:39:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656369542; cv=none; d=google.com; s=arc-20160816; b=embE4IDbwSN8ENtbBJuC6+y1CHQrxZqd95VcKOnxwxz6u+aoOs9eD4NFn720k7uVFm I3N5zCLQAV+oGX2zXa3INo/fZ81HzB1cO+fsAV7HvMRGP8yjkqAI14BFRXIM1XOlVQen dwBRZ1am1Tin5DLlfaKvI1dP6xNGcigkAgMm55X3s1zn75txpyZhmR3y6j64+uNpGc9E ruyRkGMKKSTYNDbskxDDsh6dxb46kNGXhDBP7kqwA1TYrmU5y3TmYUkfMmzcKcrn2fGH Om+mm7fLc/67/xWzzLB5svCQAmHuQ8usZBDYU6W4qfVevsdCL52DHSFdyC39YqIAOthr Hbcw== 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=eYqHi/z8HS6z+Tq6fz+C8HciW1/lXUpylhf8T9f6EIY=; b=H18LW1C5MCaafhT5vcpUciKTM/rxWMNNHAjuOlpVH2veJf7dZQVQIVNjklMEFFEjb3 XvE7VUXhvWsUgL47ljckC3oIbCXhenc/l4PKJWskdP9l+hc5RVpv0rnwLL4m95SM0R7t vkw3vyFp0ltlE+xRISe7oTU3oEl4T0//6sQ2X4rV+wdSaaQzR9R07PEGdOT76FOvhdYd 0a72yT0ar4Y4l3JRi2nbMAYX2SH53SnXz4Diq20OG8/WB+t8Kc8qhN03yYL9mOmo04Uj OGYd3hseQhJb5qvs4JlqgR3CCm72KSTbLNpunXSHY220e7Hzk6gs4wpM+zQRa+0RrDOL zFsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IMlxUjfl; 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 a20-20020a50c314000000b004355a6f9844si14486279edb.206.2022.06.27.15.38.37; Mon, 27 Jun 2022 15:39:02 -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=IMlxUjfl; 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 S241981AbiF0V6i (ORCPT + 99 others); Mon, 27 Jun 2022 17:58:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241434AbiF0VzL (ORCPT ); Mon, 27 Jun 2022 17:55:11 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47B5C62F0; Mon, 27 Jun 2022 14:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656366899; x=1687902899; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qzQhFHWv4FWYQyKVZkGxMlOgIzg1BMJpqEbI3M/bXtM=; b=IMlxUjflgcLRF5COzpq2F77mmxtJ/0eMepfQ7iPQ4z9FXsLvTKx+7N9W 3LBnJu6j2F2EBLLzF+eZgLk7phrJgBnJrosU6karrxQRk2FTNopN/9/o3 CIKaioShm1wQz9JqkIQ402nhZVhHBFm4JOGfxFYXNehzvrApvAQx9trNf aBFmBU1YhxhPtVCOd5ukC1aKFrg/Dzp4Z77HOvzqHKqA6noqwrtrOG84s NusPA0P5Uk2YafMHqUfgUbDbW81WlgOr3ckyV+b1ZtOJuDhQR+NnkR0ZO /NuVlPaBhSgBAuTvS7BeEua+8EZtVFhy9vHFfh3D+J/xBSiuM61NnICGO g==; X-IronPort-AV: E=McAfee;i="6400,9594,10391"; a="279116101" X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="279116101" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:57 -0700 X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="657863640" 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; 27 Jun 2022 14:54:57 -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 Subject: [PATCH v7 063/102] KVM: TDX: Add helper assembly function to TDX vcpu Date: Mon, 27 Jun 2022 14:53:55 -0700 Message-Id: <3165b105e44e13155735fb6252827b570c7a7649.1656366338.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=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 TDX defines an API to run TDX vcpu with its own ABI. Define an assembly helper function to run TDX vcpu to hide the special ABI so that C code can call it with function call ABI. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/vmenter.S | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/arch/x86/kvm/vmx/vmenter.S b/arch/x86/kvm/vmx/vmenter.S index 435c187927c4..f58ea3c97ccf 100644 --- a/arch/x86/kvm/vmx/vmenter.S +++ b/arch/x86/kvm/vmx/vmenter.S @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,13 @@ #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE #endif +#ifdef CONFIG_INTEL_TDX_HOST +#define TDENTER 0 +#define EXIT_REASON_TDCALL 77 +#define TDENTER_ERROR_BIT 63 +#define seamcall .byte 0x66,0x0f,0x01,0xcf +#endif + .section .noinstr.text, "ax" /** @@ -328,3 +336,141 @@ SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff) pop %_ASM_BP RET SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff) + +#ifdef CONFIG_INTEL_TDX_HOST + +.pushsection .noinstr.text, "ax" + +/** + * __tdx_vcpu_run - Call SEAMCALL(TDENTER) to run a TD vcpu + * @tdvpr: physical address of TDVPR + * @regs: void * (to registers of TDVCPU) + * @gpr_mask: non-zero if guest registers need to be loaded prior to TDENTER + * + * Returns: + * TD-Exit Reason + * + * Note: KVM doesn't support using XMM in its hypercalls, it's the HyperV + * code's responsibility to save/restore XMM registers on TDVMCALL. + */ +SYM_FUNC_START(__tdx_vcpu_run) + push %rbp + mov %rsp, %rbp + + push %r15 + push %r14 + push %r13 + push %r12 + push %rbx + + /* Save @regs, which is needed after TDENTER to capture output. */ + push %rsi + + /* Load @tdvpr to RCX */ + mov %rdi, %rcx + + /* No need to load guest GPRs if the last exit wasn't a TDVMCALL. */ + test %dx, %dx + je 1f + + /* Load @regs to RAX, which will be clobbered with $TDENTER anyways. */ + mov %rsi, %rax + + mov VCPU_RBX(%rax), %rbx + mov VCPU_RDX(%rax), %rdx + mov VCPU_RBP(%rax), %rbp + mov VCPU_RSI(%rax), %rsi + mov VCPU_RDI(%rax), %rdi + + mov VCPU_R8 (%rax), %r8 + mov VCPU_R9 (%rax), %r9 + mov VCPU_R10(%rax), %r10 + mov VCPU_R11(%rax), %r11 + mov VCPU_R12(%rax), %r12 + mov VCPU_R13(%rax), %r13 + mov VCPU_R14(%rax), %r14 + mov VCPU_R15(%rax), %r15 + + /* Load TDENTER to RAX. This kills the @regs pointer! */ +1: mov $TDENTER, %rax + +2: seamcall + + /* Skip to the exit path if TDENTER failed. */ + bt $TDENTER_ERROR_BIT, %rax + jc 4f + + /* Temporarily save the TD-Exit reason. */ + push %rax + + /* check if TD-exit due to TDVMCALL */ + cmp $EXIT_REASON_TDCALL, %ax + + /* Reload @regs to RAX. */ + mov 8(%rsp), %rax + + /* Jump on non-TDVMCALL */ + jne 3f + + /* Save all output from SEAMCALL(TDENTER) */ + mov %rbx, VCPU_RBX(%rax) + mov %rbp, VCPU_RBP(%rax) + mov %rsi, VCPU_RSI(%rax) + mov %rdi, VCPU_RDI(%rax) + mov %r10, VCPU_R10(%rax) + mov %r11, VCPU_R11(%rax) + mov %r12, VCPU_R12(%rax) + mov %r13, VCPU_R13(%rax) + mov %r14, VCPU_R14(%rax) + mov %r15, VCPU_R15(%rax) + +3: mov %rcx, VCPU_RCX(%rax) + mov %rdx, VCPU_RDX(%rax) + mov %r8, VCPU_R8 (%rax) + mov %r9, VCPU_R9 (%rax) + + /* + * Clear all general purpose registers except RSP and RAX to prevent + * speculative use of the guest's values. + */ + xor %rbx, %rbx + xor %rcx, %rcx + xor %rdx, %rdx + xor %rsi, %rsi + xor %rdi, %rdi + xor %rbp, %rbp + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + + /* Restore the TD-Exit reason to RAX for return. */ + pop %rax + + /* "POP" @regs. */ +4: add $8, %rsp + pop %rbx + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + pop %rbp + RET + +5: cmpb $0, kvm_rebooting + je 6f + mov $-EFAULT, %rax + jmp 4b +6: ud2 + _ASM_EXTABLE(2b, 5b) + +SYM_FUNC_END(__tdx_vcpu_run) + +.popsection + +#endif -- 2.25.1