From patchwork Tue Oct 29 21:05:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 11218549 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6F10A13BD for ; Tue, 29 Oct 2019 21:06:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4B63F21721 for ; Tue, 29 Oct 2019 21:06:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dnwnfKnb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728339AbfJ2VGN (ORCPT ); Tue, 29 Oct 2019 17:06:13 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:50580 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725840AbfJ2VGN (ORCPT ); Tue, 29 Oct 2019 17:06:13 -0400 Received: by mail-pf1-f201.google.com with SMTP id y191so12370959pfg.17 for ; Tue, 29 Oct 2019 14:06:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=W9TmDvzIpvVvMh9ub0E4eq3Yi9eArcf8ZO+zuv8WQrQ=; b=dnwnfKnb9LznXfooKlHBCD3nDnHj7i29u4P50wfU8RS1Z6IcHtJL8+pyAO0+NzVNPz h1C3ez6pqwVDSJiuIOaHvDOBXRw6MwohdqK96V7KrL3IeIERwx6OifF0bLo2D+1//iey viubex1XFg6kK3g1F0/uD3w8mSfnu95jJrxB3Jv+75njyRG2GKF/uaTDqGP/gs3U/3PL aCBtxIOtnmgiNZmxmjvFZ1h4At8bH3LI3+E81V5+/ZknVsZS60uy8sPsoyohjnG8kmqv EwPmH0o6LlQZc8JpVQJBMx2d5u/Yusw+cMXvjRG66qWtIBIRFv5pig09bIQ0Ay7P2nLm Y3Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=W9TmDvzIpvVvMh9ub0E4eq3Yi9eArcf8ZO+zuv8WQrQ=; b=LrQj8sskM5f6x/EZAmsS4sg9Ctx4V3Ap20GbJq6ngWCAYoZ+LHO3YlCLRTCVULlJdW g9+YEOHCcfsYqYE0q/CZ7JtEtwTEWpUAhzIvW+NQlMg/uRytzySwVMqUXzdz+KyGRVE5 xX3nnxFszXmVTQiMUCWrIhxfTCqPADwta2ZRNCWimbYnN00egqsyML3O6bg2agrX0QPc t5d+Eoh+lvBzPoI9T49J+St5SGN9FCHqXci2+Spa9iGruwYEh0870n1vKj3DOVS3NR/s f2YrS5ejjtfH8LKUI0iKPlmOAKjOQbJ5YR3QsoaCwH4Ll0sLTcv5pNQTR9dHMuVE8FXX tBgg== X-Gm-Message-State: APjAAAVluBEpnHgUr+kD2ePt9tT95LwF8O/qM0M/Kkh6MWU/22wUyAOR UuTz0pl/l3ZRFh0BNUt2TiifS5zqqbHRE4Hiu8ACjyBuLeks+UtN+sVYK6hNZ5LZMqiqV0HZDXf oHC3QKO/bMhEN4sUYfc5lidyFBb7pzDCyRl7QCzoH8eimb8+o/Ka/9dMOYKXgtIoHh2p7 X-Google-Smtp-Source: APXvYqw6qwdYZn2QAO9KRRcHT9+jwhmrkdZyYbz/rpIZlb5i3BWHg+po/FEv5+Zk74fdNg3wFoe5xrOWH4x+w8KG X-Received: by 2002:a63:4a50:: with SMTP id j16mr26630256pgl.308.1572383170907; Tue, 29 Oct 2019 14:06:10 -0700 (PDT) Date: Tue, 29 Oct 2019 14:05:52 -0700 In-Reply-To: <20191029210555.138393-1-aaronlewis@google.com> Message-Id: <20191029210555.138393-2-aaronlewis@google.com> Mime-Version: 1.0 References: <20191029210555.138393-1-aaronlewis@google.com> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog Subject: [PATCH 1/4] kvm: nested: Introduce read_and_check_msr_entry() From: Aaron Lewis To: kvm@vger.kernel.org Cc: Aaron Lewis Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add the function read_and_check_msr_entry() which just pulls some code out of nested_vmx_store_msr() for now, however, this is in preparation for a change later in this series were we reuse the code in read_and_check_msr_entry(). Signed-off-by: Aaron Lewis Change-Id: Iaf8787198c06674e8b0555982a962f5bd288e43f Reviewed-by: Jim Mattson --- arch/x86/kvm/vmx/nested.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index e76eb4f07f6c..7b058d7b9fcc 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -929,6 +929,26 @@ static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) return i + 1; } +static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, + struct vmx_msr_entry *e) +{ + if (kvm_vcpu_read_guest(vcpu, + gpa + i * sizeof(*e), + e, 2 * sizeof(u32))) { + pr_debug_ratelimited( + "%s cannot read MSR entry (%u, 0x%08llx)\n", + __func__, i, gpa + i * sizeof(*e)); + return false; + } + if (nested_vmx_store_msr_check(vcpu, e)) { + pr_debug_ratelimited( + "%s check failed (%u, 0x%x, 0x%x)\n", + __func__, i, e->index, e->reserved); + return false; + } + return true; +} + static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) { u64 data; @@ -940,20 +960,9 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) if (unlikely(i >= max_msr_list_size)) return -EINVAL; - if (kvm_vcpu_read_guest(vcpu, - gpa + i * sizeof(e), - &e, 2 * sizeof(u32))) { - pr_debug_ratelimited( - "%s cannot read MSR entry (%u, 0x%08llx)\n", - __func__, i, gpa + i * sizeof(e)); + if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) return -EINVAL; - } - if (nested_vmx_store_msr_check(vcpu, &e)) { - pr_debug_ratelimited( - "%s check failed (%u, 0x%x, 0x%x)\n", - __func__, i, e.index, e.reserved); - return -EINVAL; - } + if (kvm_get_msr(vcpu, e.index, &data)) { pr_debug_ratelimited( "%s cannot read MSR (%u, 0x%x)\n", From patchwork Tue Oct 29 21:05:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 11218551 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EBE2A13BD for ; Tue, 29 Oct 2019 21:06:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C910821721 for ; Tue, 29 Oct 2019 21:06:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GDuEgQa5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728575AbfJ2VGR (ORCPT ); Tue, 29 Oct 2019 17:06:17 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:43087 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728374AbfJ2VGQ (ORCPT ); Tue, 29 Oct 2019 17:06:16 -0400 Received: by mail-pg1-f202.google.com with SMTP id k7so8579398pgq.10 for ; Tue, 29 Oct 2019 14:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=s0LL3QMWsuiEqCtNWEPr0+snNyRy5wSt166tuiwJ1PM=; b=GDuEgQa5IzN1Fs+AjMXv74Y0vW8FkJDnokCMD6F3f8Mb8DoBDgfhNjG81LUUAT/Tka V7Kx4kWnD/EzCK6q3xJ53Ylv+494y5Q6C9u+w32d6jqwesx6+H98Kzk5CcGmrgOvDVuD kZGJdfrKe2xXCVQFrZiQlNQcvNtuuETinTkh4Z5/WaClVdWIxAH964RuvRWOmMQghl9P Ey4qATHzZxIYdoqCIjpW92hUIPJMppxLIN87D62fzsMH/b985eIiPkccfYMDdI/THyUL p5roOI0L7ipwo22QWUdXulyYkNku1IZfukknZIPkntBdW+1KMeb+PDdjiUZtkogyVY6U Xiiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=s0LL3QMWsuiEqCtNWEPr0+snNyRy5wSt166tuiwJ1PM=; b=VbGHLUW7BqFAz/17m+MoBCEqWtGnmW3TepMWVNeeb5yVgepTna3WvAUKR1xVGOaSgT cKvBzqyd8dvyROiO2d4YixGdVRwmn9iqQHaMw/9L/Wt3eFin4AiRQ6ti4TdHug/DwbVd fhxiQWfvSmepLJslbHXzd3MEgiHm83VikVO6MdHV92jBGM/FuUSxVn6/CWVQ++X6AwNc Q1hDss+OnGPNZRdcGjn+yXz4+nFskGM01x1ssCcrrvRIeEY2oZ8A5cV7WYq8SJ9wZ6If 6WjOMb1XazmcskMHlOJ8y7dLu6rN31dBEwbG4aZJqbKE4dulWzV1d6T+25hh7JUzHFR2 AljA== X-Gm-Message-State: APjAAAWMW/Cc1jfeGexxm/pxD3Q28LIE8JnnanhDISWHPBxy43Mzd1hA zfjog1woLzkNvc6QlaXJdIgrG3WdPRPCVP5oXlD+U9N3+r50lN398AJGteKc72COGMIVDQWvfy9 fHBgs9qWT+Z39SukKiKHrJKVoQXiBVq7SC0agQ3D1cO5I1MHA5UGhZz2PmCzzxHeOdY+w X-Google-Smtp-Source: APXvYqxo02XGwLjhsXtFXwWeHw2JstknZEPNsFIkO9Pq29AoeISRzLuLrLGI7985CNrp2QRnUkcY87qCczZFXJ1A X-Received: by 2002:a63:d802:: with SMTP id b2mr29766690pgh.414.1572383174296; Tue, 29 Oct 2019 14:06:14 -0700 (PDT) Date: Tue, 29 Oct 2019 14:05:53 -0700 In-Reply-To: <20191029210555.138393-1-aaronlewis@google.com> Message-Id: <20191029210555.138393-3-aaronlewis@google.com> Mime-Version: 1.0 References: <20191029210555.138393-1-aaronlewis@google.com> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog Subject: [PATCH 2/4] kvm: vmx: Rename NR_AUTOLOAD_MSRS to NR_MSR_ENTRIES From: Aaron Lewis To: kvm@vger.kernel.org Cc: Aaron Lewis Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Rename NR_AUTOLOAD_MSRS to NR_MSR_ENTRIES. This needs to be done due to the addition of the MSR-autostore area that will be added later in this series. After that the name AUTOLOAD will no longer make sense. Signed-off-by: Aaron Lewis Change-Id: Iafe7c3bfb90842a93d7c453a1d8c84a48d5fe7b0 Reviewed-by: Jim Mattson --- arch/x86/kvm/vmx/vmx.c | 4 ++-- arch/x86/kvm/vmx/vmx.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e7970a2e8eae..c0160ca9ddba 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -940,8 +940,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, if (!entry_only) j = find_msr(&m->host, msr); - if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) || - (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) { + if ((i < 0 && m->guest.nr == NR_MSR_ENTRIES) || + (j < 0 && m->host.nr == NR_MSR_ENTRIES)) { printk_once(KERN_WARNING "Not enough msr switch entries. " "Can't add msr %x\n", msr); return; diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index bee16687dc0b..0c6835bd6945 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -22,11 +22,11 @@ extern u32 get_umwait_control_msr(void); #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) -#define NR_AUTOLOAD_MSRS 8 +#define NR_MSR_ENTRIES 8 struct vmx_msrs { unsigned int nr; - struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; + struct vmx_msr_entry val[NR_MSR_ENTRIES]; }; struct shared_msr_entry { From patchwork Tue Oct 29 21:05:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 11218553 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B9B4E1390 for ; Tue, 29 Oct 2019 21:06:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9676D21721 for ; Tue, 29 Oct 2019 21:06:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p9TMSKZf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728309AbfJ2VGS (ORCPT ); Tue, 29 Oct 2019 17:06:18 -0400 Received: from mail-pl1-f202.google.com ([209.85.214.202]:51146 "EHLO mail-pl1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728202AbfJ2VGS (ORCPT ); Tue, 29 Oct 2019 17:06:18 -0400 Received: by mail-pl1-f202.google.com with SMTP id x8so154262plo.17 for ; Tue, 29 Oct 2019 14:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=KVJFtVpqqhp4AY3I8pC3DfiDcxLh4ZkmI6tduNedXL0=; b=p9TMSKZfgq/t5pQXP4CREpVCRzpU1NAMTSDrRNiqTsrYJWLsxN51PhLVg4RtslW4XF J5/PVAj1O25kiMMnHmzCgi0KSAgCaZINTvQOXI96evlcCYSUUblQllvFhP0mkxH7L+Du ksD/prGRjqCw81XeYczoCGtd0br9ardXDZl4A0cm4LXzQLHamXfv+M2xtFYAEWKL7zL3 M/YvrL+ziGTUe3BvO0wo+hcyA40rYca6y1vC9MkL7A8uBzrlX0fdCsVmRBKPvVYuCour W2AkGvoJKBl8og7xvHj8o0lBw4nLVOLGnenWKHHWjBoEt2OmNtcicNmQycWHr3x7HYYk +/mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KVJFtVpqqhp4AY3I8pC3DfiDcxLh4ZkmI6tduNedXL0=; b=kTx4wcxTKfx8K4Ieffc23BJ7e6PWRvUnTRQDZeFyNm8Plb5IWPjGEEwXPUkolY75K8 h7WC5FUzZ4/k/HG40f1o0jPr4NGIv0mJWxb11qKg36Dm5eWKerGKtI4mDfL2Z+XdRaxx jILWFCl7jNFtYZEvVC1tNeniR9oDjYa/ec39TPL8KOSq4V0fOcTDI/jDy0g5FKUHcg77 gwMCLsHGoyWlO1cawB8jEbO3Sk3wRE5Kngo59Lwj4jKu06cP1IFYcES539syhN3pbQLj tw+gBE7FUwOBqsFJsBuFnbz1+FW0+RQujz1BqcW0JILYqJAXOY9EWX9uXdjg96PfrRns o5lw== X-Gm-Message-State: APjAAAWWmv/zKAxbDvrV/qu2z/MFBiLG4BJggHSP9opeM9ex3Ok6nbCP iOg/EXYfoJ23BCuCUrwtFrcUsa+Tyl53moxaFQWs/k2UHWwA6zO2FW0shw6TJ/AEkl489iVI8fE DFsftgwBsgiPeQxH3PSsZdblhiiE8pqUGsHVBQU/QS+NBUozk6LBJo3jYqfZ+IUdSD79N X-Google-Smtp-Source: APXvYqyhJMGCE9atB/e6U3SiOicShlXOKi4pnyRCqFsi0ALfN7aKrQSf7f06pbjo+Tfz6hhSHSgpIrbgSgIhKmKV X-Received: by 2002:a63:ec4f:: with SMTP id r15mr29082700pgj.17.1572383177448; Tue, 29 Oct 2019 14:06:17 -0700 (PDT) Date: Tue, 29 Oct 2019 14:05:54 -0700 In-Reply-To: <20191029210555.138393-1-aaronlewis@google.com> Message-Id: <20191029210555.138393-4-aaronlewis@google.com> Mime-Version: 1.0 References: <20191029210555.138393-1-aaronlewis@google.com> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog Subject: [PATCH 3/4] kvm: vmx: Rename function find_msr() to vmx_find_msr_index() From: Aaron Lewis To: kvm@vger.kernel.org Cc: Aaron Lewis Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Rename function find_msr() to vmx_find_msr_index() to share implementations between vmx.c and nested.c in an upcoming change. Signed-off-by: Aaron Lewis Change-Id: I544afb762ceccb72e950bb62ac2e649a8b0cfec9 Reviewed-by: Jim Mattson --- arch/x86/kvm/vmx/vmx.c | 10 +++++----- arch/x86/kvm/vmx/vmx.h | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c0160ca9ddba..39c701730297 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -835,7 +835,7 @@ static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, vm_exit_controls_clearbit(vmx, exit); } -static int find_msr(struct vmx_msrs *m, unsigned int msr) +int vmx_find_msr_index(struct vmx_msrs *m, u32 msr) { unsigned int i; @@ -869,7 +869,7 @@ static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) } break; } - i = find_msr(&m->guest, msr); + i = vmx_find_msr_index(&m->guest, msr); if (i < 0) goto skip_guest; --m->guest.nr; @@ -877,7 +877,7 @@ static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); skip_guest: - i = find_msr(&m->host, msr); + i = vmx_find_msr_index(&m->host, msr); if (i < 0) return; @@ -936,9 +936,9 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, wrmsrl(MSR_IA32_PEBS_ENABLE, 0); } - i = find_msr(&m->guest, msr); + i = vmx_find_msr_index(&m->guest, msr); if (!entry_only) - j = find_msr(&m->host, msr); + j = vmx_find_msr_index(&m->host, msr); if ((i < 0 && m->guest.nr == NR_MSR_ENTRIES) || (j < 0 && m->host.nr == NR_MSR_ENTRIES)) { diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 0c6835bd6945..34b5fef603d8 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -334,6 +334,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu); struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr); void pt_update_intercept_for_msr(struct vcpu_vmx *vmx); void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp); +int vmx_find_msr_index(struct vmx_msrs *m, u32 msr); #define POSTED_INTR_ON 0 #define POSTED_INTR_SN 1 From patchwork Tue Oct 29 21:05:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 11218555 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 91EA91390 for ; Tue, 29 Oct 2019 21:06:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 667F521734 for ; Tue, 29 Oct 2019 21:06:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LvAVzqyS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728202AbfJ2VGV (ORCPT ); Tue, 29 Oct 2019 17:06:21 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:54953 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728576AbfJ2VGV (ORCPT ); Tue, 29 Oct 2019 17:06:21 -0400 Received: by mail-pf1-f202.google.com with SMTP id 2so12358110pfv.21 for ; Tue, 29 Oct 2019 14:06:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7GmrFIGIPo229824iF8tjRIB4ZcL3cegUAEOQuJCxb0=; b=LvAVzqySMh7iGPz/F+fUClWtPxleNA6Egg67HQRkVVt/so+GMqlH72hjEJaYLta5yu vMreKtFZwaD95AvcXhyPdjZeqzSaWDTsOHlWQOinn3CwC/fPsic8y32ADC4inVdB31aS 3WK+Ep1VCyu0CR7v2bnTlBwGmLPukbgHkc/qjUpFGxjf772EP6EBClaND9WfVoQQSYf+ i0eAN9Y45lpW2XdDovMFVtJcqKUnUY03DMsr24UjdJv/yuwXPGMbia7F2SP6vFpV7rPD cc5d64zymgRIuTl+hr35PFAHHgCCHiJNZikEEeEFayB82FB+5zRVz66AJuo/V4ITLshh eMPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7GmrFIGIPo229824iF8tjRIB4ZcL3cegUAEOQuJCxb0=; b=FRJq0cQ0z5Efixw39gWmoNqTOIjYxsQi9xsTwQhabAksisheJe6nL4Gy8n2TFW02YF cx7tzhd56f+0Kjv6yLs95Ny0XxzIOTQmfomEbb+xUW4Pc9NkhFODKma8jf6PCofQRLlt 4pzFNqdI6FO3YmNwO36NAlMFUsXDueOCjjub48rcZW0hrr6WsBjiyCvuMbFsfs9YT30S vPXXt1m3wAA2fV2otPzZAD4ID6K7hHb5eNqnQ76niig0WitGJg0jpdgEnNEcBigJqpRc /6PyeFDBd3Nd3fBP/70dXnD1FJk+EobThZt5nEasHJghNJbO5GTBR8JY5nID683S5zom JfMw== X-Gm-Message-State: APjAAAU8OXY6XJ+H7kvczDocKp4PLcngL7I3nxDu5+aHMNptkNk7Wfux nLJS5C171NSQG8j7Y4ADDY42brIQrXnYCCrQLjTO37hlazaxqK1ZyFvJlIxVBgJh/ye+SLM4cJQ OStgVuukIFyWC6+VQ5/t5Sb2P73I5HGbLVk/qBpn27zgBbM5P2v3kBun2NSn0/dsMwqZw X-Google-Smtp-Source: APXvYqzK3JWU+betAyFUuPjqjKDR3q1IDBXGdHPbuJrD8mkEZHrE2QX0IyA2gIuXqOOWPPg8BO1UT0h3K4JhoI/H X-Received: by 2002:a63:cd47:: with SMTP id a7mr28410809pgj.29.1572383180315; Tue, 29 Oct 2019 14:06:20 -0700 (PDT) Date: Tue, 29 Oct 2019 14:05:55 -0700 In-Reply-To: <20191029210555.138393-1-aaronlewis@google.com> Message-Id: <20191029210555.138393-5-aaronlewis@google.com> Mime-Version: 1.0 References: <20191029210555.138393-1-aaronlewis@google.com> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog Subject: [PATCH 4/4] KVM: nVMX: Add support for capturing highest observable L2 TSC From: Aaron Lewis To: kvm@vger.kernel.org Cc: Aaron Lewis Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The L1 hypervisor may include the IA32_TIME_STAMP_COUNTER MSR in the vmcs12 MSR VM-exit MSR-store area as a way of determining the highest TSC value that might have been observed by L2 prior to VM-exit. The current implementation does not capture a very tight bound on this value. To tighten the bound, add the IA32_TIME_STAMP_COUNTER MSR to the vmcs02 VM-exit MSR-store area whenever it appears in the vmcs12 VM-exit MSR-store area. When L0 processes the vmcs12 VM-exit MSR-store area during the emulation of an L2->L1 VM-exit, special-case the IA32_TIME_STAMP_COUNTER MSR, using the value stored in the vmcs02 VM-exit MSR-store area to derive the value to be stored in the vmcs12 VM-exit MSR-store area. Signed-off-by: Aaron Lewis Change-Id: I876e79d98e8f2e5439deafb070be1daa2e1a8e4a Reviewed-by: Jim Mattson --- arch/x86/kvm/vmx/nested.c | 91 ++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmx.h | 4 ++ 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 7b058d7b9fcc..19863f2a6588 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -929,6 +929,36 @@ static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) return i + 1; } +static bool nested_vmx_get_msr_value(struct kvm_vcpu *vcpu, u32 msr_index, + u64 *data) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + /* + * If the L0 hypervisor stored a more accurate value for the TSC that + * does not include the time taken for emulation of the L2->L1 + * VM-exit in L0, use the more accurate value. + */ + if (msr_index == MSR_IA32_TSC) { + int index = vmx_find_msr_index(&vmx->msr_autostore.guest, + MSR_IA32_TSC); + + if (index >= 0) { + u64 val = vmx->msr_autostore.guest.val[index].value; + + *data = kvm_read_l1_tsc(vcpu, val); + return true; + } + } + + if (kvm_get_msr(vcpu, msr_index, data)) { + pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, + msr_index); + return false; + } + return true; +} + static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, struct vmx_msr_entry *e) { @@ -963,12 +993,9 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) return -EINVAL; - if (kvm_get_msr(vcpu, e.index, &data)) { - pr_debug_ratelimited( - "%s cannot read MSR (%u, 0x%x)\n", - __func__, i, e.index); + if (!nested_vmx_get_msr_value(vcpu, e.index, &data)) return -EINVAL; - } + if (kvm_vcpu_write_guest(vcpu, gpa + i * sizeof(e) + offsetof(struct vmx_msr_entry, value), @@ -982,6 +1009,51 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) return 0; } +static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) +{ + struct vmcs12 *vmcs12 = get_vmcs12(vcpu); + u32 count = vmcs12->vm_exit_msr_store_count; + u64 gpa = vmcs12->vm_exit_msr_store_addr; + struct vmx_msr_entry e; + u32 i; + + for (i = 0; i < count; i++) { + if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) + return false; + + if (e.index == msr_index) + return true; + } + return false; +} + +static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, + u32 msr_index) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + struct vmx_msrs *autostore = &vmx->msr_autostore.guest; + int i = vmx_find_msr_index(autostore, msr_index); + bool in_autostore_list = i >= 0; + bool in_vmcs12_store_list; + int last; + + in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); + + if (in_vmcs12_store_list && !in_autostore_list) { + if (autostore->nr == NR_MSR_ENTRIES) { + pr_warn_ratelimited( + "Not enough msr entries in msr_autostore. Can't add msr %x\n", + msr_index); + return; + } + last = autostore->nr++; + autostore->val[last].index = msr_index; + } else if (!in_vmcs12_store_list && in_autostore_list) { + last = --autostore->nr; + autostore->val[i] = autostore->val[last]; + } +} + static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) { unsigned long invalid_mask; @@ -2027,7 +2099,7 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) * addresses are constant (for vmcs02), the counts can change based * on L2's behavior, e.g. switching to/from long mode. */ - vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); + vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val)); vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); @@ -2294,6 +2366,13 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); } + /* + * Make sure the msr_autostore list is up to date before we set the + * count in the vmcs02. + */ + prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); + + vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 34b5fef603d8..0ab1562287af 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -230,6 +230,10 @@ struct vcpu_vmx { struct vmx_msrs host; } msr_autoload; + struct msr_autostore { + struct vmx_msrs guest; + } msr_autostore; + struct { int vm86_active; ulong save_rflags;