From patchwork Fri Apr 21 00:49:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 9691565 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 EBD116038D for ; Fri, 21 Apr 2017 00:51:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DE1EA2843F for ; Fri, 21 Apr 2017 00:51:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D30242847C; Fri, 21 Apr 2017 00:51:33 +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.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 7F3982843F for ; Fri, 21 Apr 2017 00:51:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034009AbdDUAu2 (ORCPT ); Thu, 20 Apr 2017 20:50:28 -0400 Received: from mail-io0-f173.google.com ([209.85.223.173]:36328 "EHLO mail-io0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033707AbdDUAuX (ORCPT ); Thu, 20 Apr 2017 20:50:23 -0400 Received: by mail-io0-f173.google.com with SMTP id o22so104071189iod.3 for ; Thu, 20 Apr 2017 17:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=9aKiae+lOMCw4dlOC1a9rRSvs5xUbQcKi58rQYm5U40=; b=NdyXQxPZEBmvjL16Giw5VdFWgA+vOQS5iEJvNBIDYlIjZgosGHhmnsBAw1GsOBPO6D Fep89A22Ulpe+R2RUDwY4S1CP0+/+3VcP2sgLZEhpG1j1puMy6eEC08yVsoe27hEvtCY Bi725rOq7KrxXGrg36ctCbwi97oP3dNgI5CsfumYRjwR+VSeXGs4SReMYBd7Fnahdnrj kEyGWJcLfHAX13PgQ3WX0W8OAXVs3JldximUDKtouNTx6NGktsG8Q+IN3o7eFkWLoCM1 nWf/BAzyyU2P+WJBljAcOVFWSQMBRtQ5NGTxlLhu7rc8xbGDDwl1skh2+NBLkt6B87re /uDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9aKiae+lOMCw4dlOC1a9rRSvs5xUbQcKi58rQYm5U40=; b=HOhNPmA/ZeFzS0ZP2zcMCLkP/Dl2Oh1n6IQn/WlYlYv6wTy1pPu7dynPg8L8RjsoFK e5nlUP5i6V31VSrfoISn9W380+RhqNvE11x8/FP5x/abl8RV4tDpo+nVMa6Vrjd18WAV akH+Pqblk+DDciRWVJ7ToY5isq+tXkCjjJ9soaFhJWoSDnZcfXJ6d3sCbhLrQsVpdouQ LufPKZkiyyufRFzxXNnZJptgmLhdpauYRy8eJesPedBsAPedbuHjvVajKnW1DOpuYtdC f5oAABiPwvZXr6gAMCyLZpZ06q8UtjE9fM/t62jVIcDAk6f0aDpEkl2jQKcyYKy04D64 1f/Q== X-Gm-Message-State: AN3rC/52EB3Mih7Ve70EtmRRjJ4meHYnKUuJ6BJt6PEGoArp8ySMG9kt Z50bpqVC5hHoE/vJ X-Received: by 10.99.9.66 with SMTP id 63mr9966594pgj.22.1492735821753; Thu, 20 Apr 2017 17:50:21 -0700 (PDT) Received: from dmatlack.sea.corp.google.com ([100.100.206.82]) by smtp.gmail.com with ESMTPSA id e13sm12466486pfb.30.2017.04.20.17.50.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 20 Apr 2017 17:50:21 -0700 (PDT) From: David Matlack To: kvm@vger.kernel.org Cc: Peter Feiner , David Matlack Subject: [kvm-unit-tests PATCH 14/32] x86: factor out vmx_enter_guest Date: Thu, 20 Apr 2017 17:49:46 -0700 Message-Id: <20170421005004.137260-15-dmatlack@google.com> X-Mailer: git-send-email 2.12.2.816.g2cccc81164-goog In-Reply-To: <20170421005004.137260-1-dmatlack@google.com> References: <20170421005004.137260-1-dmatlack@google.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Feiner Signed-off-by: Peter Feiner Signed-off-by: David Matlack --- x86/vmx.c | 75 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/x86/vmx.c b/x86/vmx.c index 15ed94af56fd..e6c11b013d94 100644 --- a/x86/vmx.c +++ b/x86/vmx.c @@ -1376,43 +1376,54 @@ entry_failure_handler(struct vmentry_failure *failure) return VMX_TEST_EXIT; } -static int vmx_run() +/* + * Tries to enter the guest. Returns true iff entry succeeded. Otherwise, + * populates @failure. + */ +static bool vmx_enter_guest(struct vmentry_failure *failure) { - unsigned long host_rflags; + failure->early = 0; + + asm volatile ( + "mov %[HOST_RSP], %%rdi\n\t" + "vmwrite %%rsp, %%rdi\n\t" + LOAD_GPR_C + "cmpb $0, %[launched]\n\t" + "jne 1f\n\t" + "vmlaunch\n\t" + "jmp 2f\n\t" + "1: " + "vmresume\n\t" + "2: " + SAVE_GPR_C + "pushf\n\t" + "pop %%rdi\n\t" + "mov %%rdi, %[failure_flags]\n\t" + "movl $1, %[failure_flags]\n\t" + "jmp 3f\n\t" + "vmx_return:\n\t" + SAVE_GPR_C + "3: \n\t" + : [failure_early]"+m"(failure->early), + [failure_flags]"=m"(failure->flags) + : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP) + : "rdi", "memory", "cc" + ); + + failure->vmlaunch = !launched; + failure->instr = launched ? "vmresume" : "vmlaunch"; + + return !failure->early && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE); +} +static int vmx_run() +{ while (1) { u32 ret; - u32 fail = 0; bool entered; struct vmentry_failure failure; - asm volatile ( - "mov %[HOST_RSP], %%rdi\n\t" - "vmwrite %%rsp, %%rdi\n\t" - LOAD_GPR_C - "cmpb $0, %[launched]\n\t" - "jne 1f\n\t" - "vmlaunch\n\t" - "jmp 2f\n\t" - "1: " - "vmresume\n\t" - "2: " - SAVE_GPR_C - "pushf\n\t" - "pop %%rdi\n\t" - "mov %%rdi, %[host_rflags]\n\t" - "movl $1, %[fail]\n\t" - "jmp 3f\n\t" - "vmx_return:\n\t" - SAVE_GPR_C - "3: \n\t" - : [fail]"+m"(fail), [host_rflags]"=m"(host_rflags) - : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP) - : "rdi", "memory", "cc" - - ); - - entered = !fail && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE); + entered = vmx_enter_guest(&failure); if (entered) { /* @@ -1422,10 +1433,6 @@ static int vmx_run() launched = 1; ret = exit_handler(); } else { - failure.flags = host_rflags; - failure.vmlaunch = !launched; - failure.instr = launched ? "vmresume" : "vmlaunch"; - failure.early = fail; ret = entry_failure_handler(&failure); }