From patchwork Wed Dec 14 19:59:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9474695 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8D54D60823 for ; Wed, 14 Dec 2016 20:15:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7D7F328716 for ; Wed, 14 Dec 2016 20:15:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7076528743; Wed, 14 Dec 2016 20:15:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B81228716 for ; Wed, 14 Dec 2016 20:15:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752522AbcLNUPT (ORCPT ); Wed, 14 Dec 2016 15:15:19 -0500 Received: from mail-bn3nam01on0049.outbound.protection.outlook.com ([104.47.33.49]:6446 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750991AbcLNUPR (ORCPT ); Wed, 14 Dec 2016 15:15:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=V8dfpVvrGOs11ghR93QLMURBFHqzljYUvgiR8a4zPq0=; b=UeD8IOoxtfLam20ibWL7Gw+zenWWxGYJlyL46vhsNS8NPjt8FSNZpTtMLiDb58YWtjOxaNp+6pfMzNtBrEm0/CE4eKM7W21VDY93IQetBhgbRE6gwksGAeosJkXsrRbaE8eqryVGZHq+itdLY9P5aeD7voWR0sua3+bm5prIrp4= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from [127.0.1.1] (165.204.77.1) by BLUPR12MB0658.namprd12.prod.outlook.com (10.163.217.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.771.8; Wed, 14 Dec 2016 19:59:30 +0000 Subject: [PATCH v3] kvm: svm: Use the hardware provided GPA instead of page walk From: Brijesh Singh To: CC: , , , , , , , , , Date: Wed, 14 Dec 2016 14:59:23 -0500 Message-ID: <148174556351.13485.18084576305677228092.stgit@brijesh-build-machine> In-Reply-To: <148174555164.13485.11526818510637119259.stgit@brijesh-build-machine> References: <148174555164.13485.11526818510637119259.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: MWHPR22CA0068.namprd22.prod.outlook.com (10.171.142.30) To BLUPR12MB0658.namprd12.prod.outlook.com (10.163.217.28) X-MS-Office365-Filtering-Correlation-Id: fdaa0d0d-4350-4a2e-5724-08d4245bb875 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:BLUPR12MB0658; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0658; 3:8Qq5ljXDEDEdQt6AMj9pAMyW0dNw2LHMzCJQ7iPOrw3rkDZWk85uDYoBxvp3vtB1OZjugcwLW0/DPXo4D6XMxh7OOhnUnAofIGy3+nJ+eUdioXXCgXCMmnzEPqATbJxLX2isIfP5bYLKuuGa32Ce2hxxaLqZ2w++9JrZMMv5bjWmxAe5gLUSvEDMTcBn0Wjm8tSfkEIEEp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VkDTn1VO2S4Dby6IkqAPCY8IxBmhnJWqe+reobzrAHTZdw/5tyrOJU4QmtDEIqnV0sHThOY+tnU9qj+0VACPObdxY3GQifiCm5kD/L4zzKg6uyrFbgCqBhlEhqju33OVI8Xne+rS9ekt1hXrroi4E5hST+TKCtscDXNcuIS7rj0MkeyaotNvIPGouju+sjDiG9RWVEP8Ew51LOIFKcxqviLTPU8W0yzh2ZMdeglZL0jRL/SJacocPj66naswNXjoJsosUFRBdfbcHEUsC3zKUwX3UYHntyPE9LqaoRc61bxPqH79P4Om6+T6OrAK X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026)(6041248)(20161123555025)(20161123560025)(20161123564025)(20161123562025)(6072148); SRVR:BLUPR12MB0658; BCL:0; PCL:0; RULEID:; SRVR:BLUPR12MB0658; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0658; 4:bjdzJSAPelVor4OiQAXcKA9LZhQMHc4qVK6lERiC3Buk6Gp9bLpgarD85w07w8g2ZCnueP0i3h0PwdrSbFUDEDeZ+63xuhA5JfuM8reNB1VpMmasulyPOfVm3xWh2c2kejyRlZuBZdv29i6JSDIXZ03EA9Kv1EzAO+hXd0Ysn+HEqV4Tity6psxSODOzWCqpQ+amjimEyH6N1i9NLGjwdCe1BrWrIopGqmc7BMi4PFgIOVdnNOWOz6rI25b2XOpC6XOHOidshLPkrGlNxSpwabf20Pd7UnwjRHPlE4OVv94iOLPO9PyqZEJdRzHfALYq3bVVTBLqzyrliXxduXvjt3cB1sI/FicvWdGfKABvovGFVCVCCteI9jCl0dfyQcy9TaPRtQnHz+kT03lECFvjF3DC1FFtDiyAjQHcpKzQwTvWWPfhGI3DdgzryeCJJlQmZlWXdSf4Vk/yABMZApVnpk/E6CLV+9deQolPXsh03XL30z5KXtcdjDbMNp0V5rEAKpyZjnhL7piZQWULhujP09x4CPmWjfMCjLDKlAaFwx15keIgXLdBkGWCbak8Twf7CS91Qgvco1xXw2nnAkIeATgKySyLt6SRl+NyZ4y1zJ0QWwZjtrQPJpERkazs4i9C X-Forefront-PRVS: 01565FED4C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(39840400002)(39450400003)(39850400002)(39410400002)(39860400002)(189002)(199003)(90366009)(77096006)(6486002)(50986999)(54356999)(103116003)(76176999)(86362001)(101416001)(9686002)(81166006)(81156014)(68736007)(8676002)(7736002)(305945005)(38730400001)(4001350100001)(5660300001)(33716001)(50466002)(4326007)(7416002)(6116002)(3846002)(189998001)(110136003)(2950100002)(47776003)(97736004)(6916009)(66066001)(6666003)(2351001)(2906002)(106356001)(230700001)(105586002)(33646002)(92566002)(23676002)(42186005)(83506001)(25786008); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR12MB0658; H:[127.0.1.1]; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTFVQUjEyTUIwNjU4OzIzOlZRZUhFNkJIZTFJQzZOZnNkYVFyWnBxaDRz?= =?utf-8?B?YUloQTMxdHJScnNDcHphRlVua0FHeGpVRGVnTEpJTmRBTEVZMnY2MDVCVW5o?= =?utf-8?B?S1hRMEpaQUFwRFdOdnNGbUw2cXJ0WC9UUEVhalJrc01sMW1mdlorWEU0cW1M?= =?utf-8?B?cXVPL3hPaVJoaTFuUWVPSE9NQlJoNGdBVVI3K0JMekJtY2g5WXpzUUN4dUdj?= =?utf-8?B?NVQ0Yk94V096cG9ob3dCd0lyejhLWTRqdUtTbzNXa0tFL2JvQi9jT2txV2ZK?= =?utf-8?B?VDMyWlh1YzZWc2x4OG9uLzFSSFVKTkpuOGkvL1hYRWpncDBCb09mSlQ5a2R3?= =?utf-8?B?WGF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+jSJrEPxntGmaZTk70JRci+TVc4LN6VHLlEZKJ92RRrJeyuxThKhM4l6Zsca35yVrSuBmMEDkDjqNjA377X9ROeTQFPihOQ1yUtTdh555sihSlmTA4j7V/7i61Ry/rMvXg/Ua31wo2g==; 5:uLrGGldELDxZl9/RTHSSU8XRJ3LQEMNDrdLoVdTHbG3fyXnKb5Vm5obR4AAX/l3Ono29oEHFkUG2acqT4HMFW9hof04A/mdbqenCKdYobubY6h9qswy/s5Ma81sRqHAz1ezedc17wu7DppuXPuYxgA==; 24:mNoinjI/pcQeuIOyOICPdbWqPDW7WTLW7wwKXpwhXlXI7L7fJJ0664gyVp1NRq+JPIXPedR3QMPuAd436HPaP+/o5G67oMeQ+IdOXEZERRs= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0658; 7:sraI6vU47zkROGD1mQtH0lB/IR47MfK49cwWQlTL3AgzxG+FyGwUI3SdvnMyGcvoLQtVXxnmPgvAOHgPmMy3m1euu0iOspEoy5KX2oJZ2qNjjktq7/tdK7cqUuZ4ycEN4naPxNTP0Su5wcZCJfpWTfFPNZda6yTaOyleZwq7f7YahjRD6pHyXHqeKh7g6jpEoZmx3anglXqBfQFRQSDph/3Dsnd1tPwY9dXlWQgPTU7CJ/FjOBGd6lfH+K7E54y1fYk+Byb8k5myAji1QsfyzEOajEly91t8tjsSUHugYKVBaT1PerhLivmtlzyaCWpSczoRn+iMMrBE1qPnwkzB2Z//IjsJ8L81UBPmdGkunahaPn+WwPboh9ahzHk+5fpR8UPjCkCZaiSRap+f7dY4EiMoQN9sGTGH19uoxJHZTdNfPtP4EjDyfpj5hQLt4ynAi2nHBv04BJhG7KXmAp+Nzw==; 20:5ppVTqDmFCqwPJiUfjTuJ6OQzHLxVHSL3cdAhXEISt66qpqObTbTB6w3+da9Sz0s4MSP2JwLLZjsRQau/ZzM6EGTugHgOFayccj9jwo0tmd8qNEYXi6SPwBLTMpWS7VpN1Q5nV7FhlvL4XTcwkyksA1jX0rC0T3GzH4Gtk4xYtf2pQufCCSQ5bV0yqZPZhcX4wjwYw2ucznPw1BvM2yF94UdLrJCYkGInl85vw3cjhKucaVJTcY0m8wacImy71Rn X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2016 19:59:30.5909 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR12MB0658 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Tom Lendacky When a guest causes a NPF which requires emulation, KVM sometimes walks the guest page tables to translate the GVA to a GPA. This is unnecessary most of the time on AMD hardware since the hardware provides the GPA in EXITINFO2. The only exception cases involve string operations involving rep or operations that use two memory locations. With rep, the GPA will only be the value of the initial NPF and with dual memory locations we won't know which memory address was translated into EXITINFO2. Signed-off-by: Tom Lendacky Reviewed-by: Borislav Petkov Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/emulate.c | 20 +++++++++++++--- arch/x86/kvm/svm.c | 2 ++ arch/x86/kvm/x86.c | 45 ++++++++++++++++++++++++++++-------- 5 files changed, 57 insertions(+), 14 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h index e9cd7be..3e8c287 100644 --- a/arch/x86/include/asm/kvm_emulate.h +++ b/arch/x86/include/asm/kvm_emulate.h @@ -441,5 +441,6 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq); void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt); void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt); +bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt); #endif /* _ASM_X86_KVM_X86_EMULATE_H */ diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 77cb3f9..fd5b1c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -668,6 +668,9 @@ struct kvm_vcpu_arch { int pending_ioapic_eoi; int pending_external_vector; + + /* GPA available (AMD only) */ + bool gpa_available; }; struct kvm_lpage_info { diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index a3ce9d2..1b9bb01 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -171,6 +171,7 @@ #define NearBranch ((u64)1 << 52) /* Near branches */ #define No16 ((u64)1 << 53) /* No 16 bit operand */ #define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */ +#define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */ #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) @@ -4104,7 +4105,7 @@ static const struct opcode group1[] = { }; static const struct opcode group1A[] = { - I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N, + I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N, }; static const struct opcode group2[] = { @@ -4142,7 +4143,7 @@ static const struct opcode group5[] = { I(SrcMemFAddr | ImplicitOps, em_call_far), I(SrcMem | NearBranch, em_jmp_abs), I(SrcMemFAddr | ImplicitOps, em_jmp_far), - I(SrcMem | Stack, em_push), D(Undefined), + I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined), }; static const struct opcode group6[] = { @@ -4360,8 +4361,8 @@ static const struct opcode opcode_table[256] = { /* 0xA0 - 0xA7 */ I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), - I2bv(SrcSI | DstDI | Mov | String, em_mov), - F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), + I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov), + F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r), /* 0xA8 - 0xAF */ F2bv(DstAcc | SrcImm | NoWrite, em_test), I2bv(SrcAcc | DstDI | Mov | String, em_mov), @@ -5483,3 +5484,14 @@ void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) { writeback_registers(ctxt); } + +bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt) +{ + if (ctxt->rep_prefix && (ctxt->d & String)) + return false; + + if (ctxt->d & TwoMemOp) + return false; + + return true; +} diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 5e64e656..e9b3555 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -4188,6 +4188,8 @@ static int handle_exit(struct kvm_vcpu *vcpu) trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); + vcpu->arch.gpa_available = (exit_code == SVM_EXIT_NPF); + if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) vcpu->arch.cr0 = svm->vmcb->save.cr0; if (npt_enabled) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c30f62dc..26c8b93 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4420,6 +4420,21 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, } EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); +static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, + gpa_t gpa, bool write) +{ + /* For APIC access vmexit */ + if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) + return 1; + + if (vcpu_match_mmio_gpa(vcpu, gpa)) { + trace_vcpu_match_mmio(gva, gpa, write, true); + return 1; + } + + return 0; +} + static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, gpa_t *gpa, struct x86_exception *exception, bool write) @@ -4446,16 +4461,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, if (*gpa == UNMAPPED_GVA) return -1; - /* For APIC access vmexit */ - if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) - return 1; - - if (vcpu_match_mmio_gpa(vcpu, *gpa)) { - trace_vcpu_match_mmio(gva, *gpa, write, true); - return 1; - } - - return 0; + return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); } int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, @@ -4552,6 +4558,22 @@ static int emulator_read_write_onepage(unsigned long addr, void *val, int handled, ret; bool write = ops->write; struct kvm_mmio_fragment *frag; + struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; + + /* + * If the exit was due to a NPF we may already have a GPA. + * If the GPA is present, use it to avoid the GVA to GPA table walk. + * Note, this cannot be used on string operations since string + * operation using rep will only have the initial GPA from the NPF + * occurred. + */ + if (vcpu->arch.gpa_available && + emulator_can_use_gpa(ctxt) && + vcpu_is_mmio_gpa(vcpu, addr, exception->address, write) && + (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK)) { + gpa = exception->address; + goto mmio; + } ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); @@ -5563,6 +5585,9 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, } restart: + /* Save the faulting GPA (cr2) in the address field */ + ctxt->exception.address = cr2; + r = x86_emulate_insn(ctxt); if (r == EMULATION_INTERCEPTED)