Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3845494pxy; Mon, 26 Apr 2021 11:06:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzohdig5kvIU4IdMP7wq0/ik1BHG8J8b3bnyFnExWQ51OMg8g/AiZ+76VJCAuUJ1Ok6fFbw X-Received: by 2002:a17:906:5949:: with SMTP id g9mr19957705ejr.356.1619460360991; Mon, 26 Apr 2021 11:06:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619460360; cv=none; d=google.com; s=arc-20160816; b=lDzE171q6vfVft3qLeAvl7bah8HqkkRwOL6Hsd5fxiT0XXxQspsLILD3jD4shdbbLE QghTHP6dXNRc8MCnKG9CsxC/o3ew2/EKuBKcTISURpkEu+qDatMFb6oPEeApQRZ5Q3mO vSF+rYg4nbLsJxxx/SPDSTQl7DL6+3tCE0PpRyYqyswKuc6fNbb1AGm++fdOKO8HBxRA ILlRzOuxsnTlk+/4IgrlrcaZl9DVYA8h1krpLq+Aw0EgDpoaVJpLPz9vBKc1fgdQoD0N YoixOGxd2/+vqpg+IdZ51tzkeAOQpWDjrdzWlQInPH9aUrc9Rc+z90nrVLGBmIkBGyJQ ip3A== 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 :ironport-sdr:ironport-sdr; bh=xrV1iWwpEaAAVyGiUcJEqiAR9TiYGd02tmJRa0MPQ3o=; b=WLkdEjYTZ6O8XL2GAyuBzQG9bX3earqGICFThLTyCxvNZXgd3HISwuLP0D/appFb6E 9JksxpYBosyk8S4UTDieLPOIYD/rvy59cGsBKGZ/WXRpgJTomSsrneEClhMiWI3gcfht ZK5jutsFDfk+hHfIYv8SKcR+DykL/zu4vRNwmZSIwUJZ4sfvH6Ag0j2zz/YrezuBRivX 5jHkEeEUwn65x+rY3nVCsBQY+dRvf0zB6wPH1EwuVcWTqaeV4CSR94vpZnFz7O2hPtPT RaeKRh/qFcmex2h4xk30CoIpyFlYPKHQ5amzarT3ODj970jnqQIUSK81oS2LhHXUa4i+ 7shg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e7si485647edk.255.2021.04.26.11.05.37; Mon, 26 Apr 2021 11:06:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234864AbhDZSEf (ORCPT + 99 others); Mon, 26 Apr 2021 14:04:35 -0400 Received: from mga04.intel.com ([192.55.52.120]:22346 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234343AbhDZSDe (ORCPT ); Mon, 26 Apr 2021 14:03:34 -0400 IronPort-SDR: JXKp49xNFJmyM+l6yrxqwyWoQ1l6Kr5SFNQBHFqvxEjs6kkKomXJ0mRr41T9EXKAOHdTCdCfal rzIYQ0wtb0iA== X-IronPort-AV: E=McAfee;i="6200,9189,9966"; a="194263256" X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="194263256" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:49 -0700 IronPort-SDR: mlGVjO1wrTFdncFOqXW/bWAUFNx5U0EPmvS4OlFPWORQwqmGUUzVxkN3kph/rC8Y73uH803fp/ wRNLLrvrqzqQ== X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="447353395" Received: from ssumanpx-mobl.amr.corp.intel.com (HELO skuppusw-mobl5.amr.corp.intel.com) ([10.254.34.197]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:49 -0700 From: Kuppuswamy Sathyanarayanan To: Peter Zijlstra , Andy Lutomirski , Dave Hansen , Dan Williams , Tony Luck Cc: Andi Kleen , Kirill Shutemov , Kuppuswamy Sathyanarayanan , Raj Ashok , Sean Christopherson , linux-kernel@vger.kernel.org, Kuppuswamy Sathyanarayanan Subject: [RFC v2 15/32] x86/tdx: Handle in-kernel MMIO Date: Mon, 26 Apr 2021 11:01:42 -0700 Message-Id: <38eb0c5dce2e2ff6bdab4dffbead09780ec86e50.1619458733.git.sathyanarayanan.kuppuswamy@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Kirill A. Shutemov" Handle #VE due to MMIO operations. MMIO triggers #VE with EPT_VIOLATION exit reason. For now we only handle subset of instruction that kernel uses for MMIO oerations. User-space access triggers SIGBUS. Signed-off-by: Kirill A. Shutemov Reviewed-by: Andi Kleen Signed-off-by: Kuppuswamy Sathyanarayanan --- arch/x86/kernel/tdx.c | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c index ec61f2f06c98..3fe617978fc4 100644 --- a/arch/x86/kernel/tdx.c +++ b/arch/x86/kernel/tdx.c @@ -5,6 +5,8 @@ #include #include +#include +#include /* force_sig_fault() */ #include @@ -219,6 +221,101 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio(int size, bool write, unsigned long addr, + unsigned long val) +{ + return tdvmcall_out_r11(EXIT_REASON_EPT_VIOLATION, size, + write, addr, val); +} + +static inline void *get_reg_ptr(struct pt_regs *regs, struct insn *insn) +{ + static const int regoff[] = { + offsetof(struct pt_regs, ax), + offsetof(struct pt_regs, cx), + offsetof(struct pt_regs, dx), + offsetof(struct pt_regs, bx), + offsetof(struct pt_regs, sp), + offsetof(struct pt_regs, bp), + offsetof(struct pt_regs, si), + offsetof(struct pt_regs, di), + offsetof(struct pt_regs, r8), + offsetof(struct pt_regs, r9), + offsetof(struct pt_regs, r10), + offsetof(struct pt_regs, r11), + offsetof(struct pt_regs, r12), + offsetof(struct pt_regs, r13), + offsetof(struct pt_regs, r14), + offsetof(struct pt_regs, r15), + }; + int regno; + + regno = X86_MODRM_REG(insn->modrm.value); + if (X86_REX_R(insn->rex_prefix.value)) + regno += 8; + + return (void *)regs + regoff[regno]; +} + +static int tdg_handle_mmio(struct pt_regs *regs, struct ve_info *ve) +{ + int size; + bool write; + unsigned long *reg; + struct insn insn; + unsigned long val = 0; + + /* + * User mode would mean the kernel exposed a device directly + * to ring3, which shouldn't happen except for things like + * DPDK. + */ + if (user_mode(regs)) { + pr_err("Unexpected user-mode MMIO access.\n"); + force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) ve->gla); + return 0; + } + + kernel_insn_init(&insn, (void *) regs->ip, MAX_INSN_SIZE); + insn_get_length(&insn); + insn_get_opcode(&insn); + + write = ve->exit_qual & 0x2; + + size = insn.opnd_bytes; + switch (insn.opcode.bytes[0]) { + /* MOV r/m8 r8 */ + case 0x88: + /* MOV r8 r/m8 */ + case 0x8A: + /* MOV r/m8 imm8 */ + case 0xC6: + size = 1; + break; + } + + if (inat_has_immediate(insn.attr)) { + BUG_ON(!write); + val = insn.immediate.value; + tdg_mmio(size, write, ve->gpa, val); + return insn.length; + } + + BUG_ON(!inat_has_modrm(insn.attr)); + + reg = get_reg_ptr(regs, &insn); + + if (write) { + memcpy(&val, reg, size); + tdg_mmio(size, write, ve->gpa, val); + } else { + val = tdg_mmio(size, write, ve->gpa, val); + memset(reg, 0, size); + memcpy(reg, &val, size); + } + return insn.length; +} + unsigned long tdg_get_ve_info(struct ve_info *ve) { u64 ret; @@ -271,6 +368,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs, case EXIT_REASON_IO_INSTRUCTION: tdg_handle_io(regs, ve->exit_qual); break; + case EXIT_REASON_EPT_VIOLATION: + ve->instr_len = tdg_handle_mmio(regs, ve); + break; default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return -EFAULT; -- 2.25.1