Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3008887imu; Fri, 23 Nov 2018 19:15:30 -0800 (PST) X-Google-Smtp-Source: AJdET5fPimlEloPTFRtkDSmJKA0+5tkSpy50PsTJUtUkalR8K9wUBW1pxWaz34z+id8esnxf+v60 X-Received: by 2002:a62:8893:: with SMTP id l141mr18381023pfd.1.1543029330519; Fri, 23 Nov 2018 19:15:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543029330; cv=none; d=google.com; s=arc-20160816; b=WVe8orxzCCmfoH+ECRTfxcXKCyEu2ERN6X+aoIHkqGFXo9Y7EQaRIyTX3GCsXzpkX2 OfhzzIqs+lJWpw3m8cx8P7h9n2ijSeKVDtdZeNlLMuNqIS9QIKqtJNSp6m5QBzhOmEoO 1aPQZxdfGobzmOyGpuJuz5PDvozsioGCvODyWdHFyxP3ZO+lF9AqJg+wHIS9gAtMLxRH DtOB7VsYq2h4VqIzghE6hpYuQezFYXunAdNHlWr+ko3bMfBm/IdHrjuaHuUJZ1qjlB8S FHd4LGyIUsYOQJfJiMC3GbHY5M7qd4qnnfLlTuEUv0D4RWVqyBYdDS7zzQrJW+IMqcqT bHGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=Y0vjKPL8ZuhXE0bqa/fSxAgmugYhdHShYA3isBMlp0k=; b=pL5djFgjGrH4MWITB3QDLccJ/k9Xh4cqdw+f7FxxHjt+SNGNBh7hi9V88UZ8nCNK2e 6L1sk5OWOleJeGeL3Qvlpm1PHgmd7vfjlplUuuLFZ8Mdl8WSLeWRC/WVinV+4SnqEqW6 dG/Cg1TM1+FTl5oUr9f4N2a/cm7tEEesBSq8eXZMXg0EyghsISbah+7wU6MmJcAZ92Nz gveFvwiT5Ytx/oR/i75ofi5++0/77y7l/bU54/u2TpOWwbkgQ8Vj7uNZFpJiHz741QW5 3tBezPU1ziDmBDgBO9PmocdWAjQuBelTLKMBW9il9YsLBZUZls4PNrnWElu4doZS4af+ mN/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=O3gyUy70; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d34si51078840pgb.43.2018.11.23.19.15.16; Fri, 23 Nov 2018 19:15:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=O3gyUy70; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404276AbeKWDaQ (ORCPT + 99 others); Thu, 22 Nov 2018 22:30:16 -0500 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:39541 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732770AbeKWDaQ (ORCPT ); Thu, 22 Nov 2018 22:30:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1542905405; x=1574441405; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Y0vjKPL8ZuhXE0bqa/fSxAgmugYhdHShYA3isBMlp0k=; b=O3gyUy70hPEtg7P7Y1oxbyAB+jd7iSQ3GFHCp75ZwvX+FKfLD3jVT7Q9 Z7U6zRbkRDZkbImmEMVi9am4N4KNHJ8lYHgjoxEIRaK6J7wK5Kofuko3B 9/lKtPMeYVgR8eM54kdLwuBmEmgUq+8F4XceTYCJcCK4HIze8VBtyJR5g c=; X-IronPort-AV: E=Sophos;i="5.56,253,1539648000"; d="scan'208";a="771919547" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-2b-4ff6265a.us-west-2.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 22 Nov 2018 16:50:01 +0000 Received: from u54ee758033e858cfa736.ant.amazon.com (pdx2-ws-svc-lb17-vlan2.amazon.com [10.247.140.66]) by email-inbound-relay-2b-4ff6265a.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id wAMGnujS037306 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 22 Nov 2018 16:49:58 GMT Received: from u54ee758033e858cfa736.ant.amazon.com (localhost [127.0.0.1]) by u54ee758033e858cfa736.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id wAMGnt4t008106; Thu, 22 Nov 2018 17:49:55 +0100 Received: (from jsteckli@localhost) by u54ee758033e858cfa736.ant.amazon.com (8.15.2/8.15.2/Submit) id wAMGnsT4008105; Thu, 22 Nov 2018 17:49:54 +0100 From: Julian Stecklina To: kernel-hardening@lists.openwall.com Cc: Julian Stecklina , Liran Alon , Tycho Andersen , Jonathan Adams , David Woodhouse , LKML Subject: [RFC RESEND PATCH 1/6] kvm, vmx: move CR2 context switch out of assembly path Date: Thu, 22 Nov 2018 17:49:34 +0100 Message-Id: <3c182809f9f1cffbaada408b2b291c0f5eb2a465.1542905228.git.jsteckli@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The VM entry/exit path is a giant inline assembly statement. Simplify it by doing CR2 context switching in plain C. Move CR2 restore behind IBRS clearing, so we reduce the amount of code we execute with IBRS on. Using {read,write}_cr2() means KVM will use pv_mmu_ops instead of open coding native_{read,write}_cr2(). The CR2 code has been done in assembly since KVM's genesis[1], which predates the addition of the paravirt ops[2], i.e. KVM isn't deliberately avoiding the paravirt ops. [1] Commit 6aa8b732ca01 ("[PATCH] kvm: userspace interface") [2] Commit d3561b7fa0fb ("[PATCH] paravirt: header and stubs for paravirtualisation") Signed-off-by: Julian Stecklina Reviewed-by: Jan H. Schönherr Reviewed-by: Konrad Jan Miller Reviewed-by: Jim Mattson Reviewed-by: Sean Christopherson --- arch/x86/kvm/vmx.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index ccc6a01eb4f4..a6e5a5cd8f14 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11212,6 +11212,9 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? (unsigned long)¤t_evmcs->host_rsp : 0; + if (read_cr2() != vcpu->arch.cr2) + write_cr2(vcpu->arch.cr2); + if (static_branch_unlikely(&vmx_l1d_should_flush)) vmx_l1d_flush(vcpu); @@ -11231,13 +11234,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) "2: \n\t" __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t" "1: \n\t" - /* Reload cr2 if changed */ - "mov %c[cr2](%0), %%" _ASM_AX " \n\t" - "mov %%cr2, %%" _ASM_DX " \n\t" - "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" - "je 3f \n\t" - "mov %%" _ASM_AX", %%cr2 \n\t" - "3: \n\t" /* Check if vmlaunch of vmresume is needed */ "cmpl $0, %c[launched](%0) \n\t" /* Load guest registers. Don't clobber flags. */ @@ -11298,8 +11294,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) "xor %%r14d, %%r14d \n\t" "xor %%r15d, %%r15d \n\t" #endif - "mov %%cr2, %%" _ASM_AX " \n\t" - "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" "xor %%eax, %%eax \n\t" "xor %%ebx, %%ebx \n\t" @@ -11331,7 +11325,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), #endif - [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), [wordsize]"i"(sizeof(ulong)) : "cc", "memory" #ifdef CONFIG_X86_64 @@ -11365,6 +11358,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) /* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB(); + vcpu->arch.cr2 = read_cr2(); + /* All fields are clean at this point */ if (static_branch_unlikely(&enable_evmcs)) current_evmcs->hv_clean_fields |= -- 2.17.1