From patchwork Wed Jun 13 18:19:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10462785 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 617AF60329 for ; Wed, 13 Jun 2018 18:20:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A1C9290F6 for ; Wed, 13 Jun 2018 18:20:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3EB192903D; Wed, 13 Jun 2018 18:20: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=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL 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 213722903D for ; Wed, 13 Jun 2018 18:20:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935725AbeFMSUE (ORCPT ); Wed, 13 Jun 2018 14:20:04 -0400 Received: from mail-ot0-f202.google.com ([74.125.82.202]:56027 "EHLO mail-ot0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935117AbeFMSUA (ORCPT ); Wed, 13 Jun 2018 14:20:00 -0400 Received: by mail-ot0-f202.google.com with SMTP id b1-v6so1985997otf.22 for ; Wed, 13 Jun 2018 11:20:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=xSOyOcBFNFOSUvSvOQa0a+VDhVvTg8+NSPrP36tyXjg=; b=l5ktpInmYAd74VaO7awc2yXKGxIEHRrsbZEmrxwM1FVgf3+zgFInixNgLnpQVM6Ady A7PaD6tsb1znjMJI6OkWDOFs+qJBCniKbDQuklY2AfubuUurRtWQ39UnoCePhB53dnI4 w4rEtQpGYO36JVjHcU+BuKTt3muSG5Ebhywfvs6q456crBps5ySM91rF4tRulQhpHHlU cvxgUAsRdoBijhHa3xKVcdpbTuhuR1YoHHGZDE6AZZdL0i4mQ37ojFM1ZHT61hKYwPMT PybMMsoP4xGEn2ywVOfo1+ZywCtl/E48c5e4ay6eTVnqgOBaj5p6694gdlRiQO3/OPEH IWIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=xSOyOcBFNFOSUvSvOQa0a+VDhVvTg8+NSPrP36tyXjg=; b=PYzUwscnqdPYMOasQ759W1UITbdyPQfWwYe1xQiFtIxZs/VMXZ3mTQHx2yQM9kG7jz p3hbupaSsOlFR9RHYVsvZiPvw2kIxr9Vo1opV0UXoHdBVdrn7DegvDi+hk7EYi9I37TD j16QMG4f+ysjdtaDAEnxVRFE5lCJUyVvQRpMNyjvNICi0n09fuwhBHQ5xoXJhWsAjlAr adPLVaUOdy7q43eEiCZlPX2l6CyRpB9XoxYLj4zDilAaG7gk/vKdXkMBOC5Deq/dMcIY 919KTFL4uXFY7cMIgb8o5/BDgqjbsLA5RuXkA/a86R3iXZKSrDyto/NWvJStLuuA93m5 QIww== X-Gm-Message-State: APt69E15n7plZ1DjB4qXE/MfttkdSLwte4Sn0TZCtlc/ksjkw8++vpti hdFoVPmlLLWABhpt9DpiXngRbxho2QNV4FG5icFAN4rEEaqaiUV261gFGPpmoZ7pg1l8TMc6Npb racR5G6HBxu+EzxNXMpOd8abyirjUn82n+fietidEPgMMmT9tznEBIA4tIyOk X-Google-Smtp-Source: ADUXVKKuHDEI6a+cJWaUxQM94e89DPa5QO921IyhyGrWRKM7GD/oe0b/shuoYU2UOGP0Kq/8A1aS7Pj80qWd X-Received: by 2002:aca:c717:: with SMTP id x23-v6mr2604589oif.12.1528913999333; Wed, 13 Jun 2018 11:19:59 -0700 (PDT) Date: Wed, 13 Jun 2018 11:19:48 -0700 Message-Id: <20180613181948.98657-1-marcorr@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.18.0.rc1.242.g61856ae69a-goog Subject: [kvm-unit-tests PATCH] x86: Add test for nested VM entry prereqs From: Marc Orr To: kvm@vger.kernel.org, jmattson@google.com, pbonzini@redhat.com, rkrcmar@redhat.com Cc: Marc Orr Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds a test for the prereq checks done as a part of a nested VM launch related to event injection. Signed-off-by: Marc Orr Reviewed-by: Jim Mattson --- lib/x86/processor.h | 8 ++ x86/vmx.h | 27 ++++++ x86/vmx_tests.c | 195 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index 247386207bb0..886170cfa163 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -15,6 +15,14 @@ # define S "4" #endif +#define DF_VECTOR 8 +#define TS_VECTOR 10 +#define NP_VECTOR 11 +#define SS_VECTOR 12 +#define GP_VECTOR 13 +#define PF_VECTOR 14 +#define AC_VECTOR 17 + #define X86_CR0_PE 0x00000001 #define X86_CR0_MP 0x00000002 #define X86_CR0_TS 0x00000008 diff --git a/x86/vmx.h b/x86/vmx.h index bdcaac0edc01..116bbd515beb 100644 --- a/x86/vmx.h +++ b/x86/vmx.h @@ -420,6 +420,15 @@ enum Intr_type { #define INTR_INFO_INTR_TYPE_SHIFT 8 +#define INTR_TYPE_EXT_INTR (0 << 8) /* external interrupt */ +#define INTR_TYPE_RESERVED (1 << 8) /* reserved */ +#define INTR_TYPE_NMI_INTR (2 << 8) /* NMI */ +#define INTR_TYPE_HARD_EXCEPTION (3 << 8) /* processor exception */ +#define INTR_TYPE_SOFT_INTR (4 << 8) /* software interrupt */ +#define INTR_TYPE_PRIV_SW_EXCEPTION (5 << 8) /* priv. software exception */ +#define INTR_TYPE_SOFT_EXCEPTION (6 << 8) /* software exception */ +#define INTR_TYPE_OTHER_EVENT (7 << 8) /* other event */ + /* * VM-instruction error numbers */ @@ -712,6 +721,24 @@ static inline bool invvpid(unsigned long type, u64 vpid, u64 gla) return ret; } +static inline int enable_unrestricted_guest(void) +{ + if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) + return -1; + + if (!(ctrl_cpu_rev[1].clr & CPU_URG)) + return -1; + + vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); + vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | CPU_URG); + return 0; +} + +static inline void disable_unrestricted_guest(void) +{ + vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) & ~CPU_URG); +} + const char *exit_reason_description(u64 reason); void print_vmexit_info(); void print_vmentry_failure_info(struct vmentry_failure *failure); diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 0c1a6952f9ea..57246905982b 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -3561,6 +3561,200 @@ static void try_tpr_threshold_and_vtpr(unsigned threshold, unsigned vtpr) report_prefix_pop(); } +static void test_invalid_event_injection(void) +{ + u32 ent_intr_info_save = vmcs_read(ENT_INTR_INFO); + u32 ent_intr_error_save = vmcs_read(ENT_INTR_ERROR); + u32 ent_inst_len_save = vmcs_read(ENT_INST_LEN); + u32 primary_save = vmcs_read(CPU_EXEC_CTRL0); + u32 secondary_save = vmcs_read(CPU_EXEC_CTRL1); + u64 guest_cr0_save = vmcs_read(GUEST_CR0); + u32 ent_intr_info_base = INTR_INFO_VALID_MASK; + u32 ent_intr_info, ent_intr_err, ent_intr_len; + u32 cnt; + + /* Setup */ + report_prefix_push("invalid event injection"); + vmcs_write(ENT_INTR_ERROR, 0x00000000); + vmcs_write(ENT_INST_LEN, 0x00000001); + + /* The field’s interruption type is not set to a reserved value. */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_RESERVED; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "RESERVED interruption type invalid", + ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* If the interruption type is NMI, the vector is 2. */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(NMI && nr != 2) invalid", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* + * If the interruption type + * is HW exception, the vector is at most 31. + */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 0x20; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(HW exception && nr > 31) invalid", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* If the interruption type is other event, the vector is 0. */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_OTHER_EVENT | 0x1; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(OTHER EVENT && nr != 0) invalid", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* + * deliver-error-code is 1 iff either + * (a) the "unrestricted guest" VM-execution control is 0 + * (b) CR0.PE is set. + */ + ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | + INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "error code <-> (!URG || prot_mode)", + ent_intr_info); + enable_unrestricted_guest(); + vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | + GP_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "error code <-> (!URG || prot_mode)", + ent_intr_info); + vmcs_write(GUEST_CR0, guest_cr0_save | X86_CR0_PE); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* deliver-error-code is 1 iff the interruption type is HW exception */ + report_prefix_push("error code <-> HW exception"); + for (cnt = 0; cnt < 8; cnt++) { + u32 exception_type_mask = cnt << 8; + u32 deliver_error_code_mask = + exception_type_mask != INTR_TYPE_HARD_EXCEPTION ? + INTR_INFO_DELIVER_CODE_MASK : 0; + + ent_intr_info = ent_intr_info_base | deliver_error_code_mask | + exception_type_mask | GP_VECTOR; + report_prefix_pushf("VM-entry intr info=0x%x", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + } + report_prefix_pop(); + + /* + * deliver-error-code is 1 iff the the vector + * indicates an exception that would normally deliver an error code + */ + report_prefix_push("error code <-> nr normally delivers an error code"); + for (cnt = 0; cnt < 32; cnt++) { + u32 deliver_error_code_mask = INTR_INFO_DELIVER_CODE_MASK; + + switch (cnt) { + case DF_VECTOR: + case TS_VECTOR: + case NP_VECTOR: + case SS_VECTOR: + case GP_VECTOR: + case PF_VECTOR: + case AC_VECTOR: + deliver_error_code_mask = 0; + } + ent_intr_info = ent_intr_info_base | deliver_error_code_mask | + INTR_TYPE_HARD_EXCEPTION | cnt; + report_prefix_pushf("VM-entry intr info=0x%x", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + } + report_prefix_pop(); + + /* Reserved bits in the field (30:12) are 0. */ + report_prefix_push("reserved bits clear"); + for (cnt = 12; cnt <= 30; cnt++) { + ent_intr_info = ent_intr_info_base | + INTR_INFO_DELIVER_CODE_MASK | + INTR_TYPE_HARD_EXCEPTION | GP_VECTOR | + (1U << cnt); + report_prefix_pushf("VM-entry intr info=0x%x", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(false, false); + report_prefix_pop(); + } + report_prefix_pop(); + + /* + * If deliver-error-code is 1 + * bits 31:15 of the VM-entry exception error-code field are 0. + */ + ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | + INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "VM-entry exception error code[31:15] clear", + ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + for (cnt = 15; cnt <= 31; cnt++) { + ent_intr_err = 1U << cnt; + report_prefix_pushf("VM-entry intr error=0x%x", ent_intr_err); + vmcs_write(ENT_INTR_ERROR, ent_intr_err); + test_vmx_controls(false, false); + report_prefix_pop(); + } + vmcs_write(ENT_INTR_ERROR, 0x00000000); + report_prefix_pop(); + + /* + * If the interruption type is software interrupt, software exception, + * or privileged software exception, the VM-entry instruction-length + * field is in the range 0–15. + */ + + ent_intr_info = ent_intr_info_base | INTR_TYPE_SOFT_EXCEPTION; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "VM-entry instruction-length check", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + + /* Instruction length set to -1 (0xFFFFFFFF) should fail */ + ent_intr_len = -1; + report_prefix_pushf("VM-entry intr length = 0x%x", ent_intr_len); + vmcs_write(ENT_INST_LEN, ent_intr_len); + test_vmx_controls(false, false); + report_prefix_pop(); + + /* Instruction length set to 16 should fail */ + ent_intr_len = 0x00000010; + report_prefix_pushf("VM-entry intr length = 0x%x", ent_intr_len); + vmcs_write(ENT_INST_LEN, 0x00000010); + test_vmx_controls(false, false); + report_prefix_pop(); + + report_prefix_pop(); + + /* Cleanup */ + vmcs_write(ENT_INTR_INFO, ent_intr_info_save); + vmcs_write(ENT_INTR_ERROR, ent_intr_error_save); + vmcs_write(ENT_INST_LEN, ent_inst_len_save); + vmcs_write(CPU_EXEC_CTRL0, primary_save); + vmcs_write(CPU_EXEC_CTRL1, secondary_save); + vmcs_write(GUEST_CR0, guest_cr0_save); + report_prefix_pop(); +} + /* * Test interesting vTPR values for a given TPR threshold. */ @@ -3807,6 +4001,7 @@ static void vmx_controls_test(void) test_apic_virt_addr(); test_tpr_threshold(); test_nmi_ctrls(); + test_invalid_event_injection(); } static bool valid_vmcs_for_vmentry(void)