Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp859209imd; Thu, 1 Nov 2018 06:44:20 -0700 (PDT) X-Google-Smtp-Source: AJdET5eX9px3Yo7TSmnpFd+ljN97h/ilZjfr7eyCuCz/9dW3CgDN9pc3T3ZFNyT8i+ZxrioNeace X-Received: by 2002:a17:902:e81:: with SMTP id 1-v6mr7730915plx.48.1541079860374; Thu, 01 Nov 2018 06:44:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541079860; cv=none; d=google.com; s=arc-20160816; b=aEQyVp236bWCxz/4wyhEtc5+yt4f5j8ub311QEFk/61YCdf9i6mZ0Va/qmL3hv2dje gYl7p4gzZLEFLWaxEOZq9t+DG2nvXnkECPEy+kEBaYf3IgmdzMAVqaOkv0tjPGFEhEDQ xaOLRNcOF1URi4zm54MUgm/K4Yvhd2NKGGjl+GIIlTFoqXUx96pKYV+Bf2/rqWZKkan4 OPhjWoBsbpE7VLepCjjPyLBR4YH4CNkLz4hNQcefVzM1dWoYyhHyrz4PaLNu7uGOuoLH JqCvC5xAqVVNBY4YfKwwjcWdPWmMvrOJOHoTxh5jsVyCAYligOpBhU2E4jIhTynW8aO3 8JyA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=YEPsFPbjB1UlGbv7nVUMRcnG6kWtRYAVCKeChzRjd00=; b=L6CeCjNf4GDR61UXJe9k6hItY4EzhDJ8kQKopTvu61ZZcojP5KO97TijzjxLymQXLR lUzsR2nk+M1Osk+Td2HoLUWSVOnCQgdeIHboTHokwYNPtO/Ci2CD12c6LL4uPhdrFCzh rAnz+W3KoaXXrtvKGU3O7MqsUF2D+DLEP+4DFQLBZ7iaz+oJn9w9mf3qXrLAp9aHUsxs FIB4Sh7XUQCIPDdGIuFXRXwc9dA+37zcb5pZjppuzeonlFgUmHfEQGTDU/hxmuX38sx3 jQwcCURtSNYp7jRw6/WZWiXjniphe+bqJoAiWr4bDkVx2UGaWUsV9o3yQTW6Ij0f08x5 YDMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=Mg1mxkDF; 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 o12-v6si33169770pfh.9.2018.11.01.06.44.02; Thu, 01 Nov 2018 06:44:20 -0700 (PDT) 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=Mg1mxkDF; 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 S1728743AbeKAWq2 (ORCPT + 99 others); Thu, 1 Nov 2018 18:46:28 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:1226 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728514AbeKAWq2 (ORCPT ); Thu, 1 Nov 2018 18:46:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1541079807; x=1572615807; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YEPsFPbjB1UlGbv7nVUMRcnG6kWtRYAVCKeChzRjd00=; b=Mg1mxkDF5gl0p0q8zv3w3W+8zcgBgvy61E0xC/FZmY6QKoZTsLtTkCSS vF4kMgXyg8hfiYgfBS/90FeCx7R9qndMrDoxhXms8oMTKmZJvLae2nLSH vmfXVtqNKNaoHTlDXtuQThVdrJnJ34d3NtTtN6zqJLXjpYe0XCfB/BBrI A=; X-IronPort-AV: E=Sophos;i="5.54,452,1534809600"; d="scan'208";a="639833343" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-1a-67b371d8.us-east-1.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 01 Nov 2018 13:43:24 +0000 Received: from u54ee758033e858cfa736.ant.amazon.com (iad7-ws-svc-lb50-vlan3.amazon.com [10.0.93.214]) by email-inbound-relay-1a-67b371d8.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id wA1DhHdZ053102 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 1 Nov 2018 13:43:19 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 wA1DhG9a004815; Thu, 1 Nov 2018 14:43:16 +0100 Received: (from jsteckli@localhost) by u54ee758033e858cfa736.ant.amazon.com (8.15.2/8.15.2/Submit) id wA1DhF9m004814; Thu, 1 Nov 2018 14:43:15 +0100 From: Julian Stecklina To: kvm@vger.kernel.org, Paolo Bonzini Cc: Julian Stecklina , Julian Stecklina , linux-kernel@vger.kernel.org Subject: [PATCH v3 1/3] kvm, vmx: move CR2 context switch out of assembly path Date: Thu, 1 Nov 2018 14:42:47 +0100 Message-Id: <0b290ddce11ab78951592b9435469cd16773629c.1541079752.git.jsteckli@amazon.de> X-Mailer: git-send-email 2.7.4 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 ccc6a01..a6e5a5c 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.7.4