From patchwork Tue Jun 26 01:48:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10487847 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 17C9B6023A for ; Tue, 26 Jun 2018 01:48:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F116C26E97 for ; Tue, 26 Jun 2018 01:48:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E2DA627CEA; Tue, 26 Jun 2018 01:48:31 +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 F3F9826E97 for ; Tue, 26 Jun 2018 01:48:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964874AbeFZBsY (ORCPT ); Mon, 25 Jun 2018 21:48:24 -0400 Received: from mail-qt0-f201.google.com ([209.85.216.201]:56064 "EHLO mail-qt0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964830AbeFZBsX (ORCPT ); Mon, 25 Jun 2018 21:48:23 -0400 Received: by mail-qt0-f201.google.com with SMTP id f8-v6so14328345qtj.22 for ; Mon, 25 Jun 2018 18:48:23 -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=vWmPpim0Ul0xIJCOkv50sV36BYq7X902LEG1uHm7C8o=; b=b29280AJ4k6qdQAr3u7hMJbPH8dxFesEl3z09dqbjVFjZ1RpPIqBDMUCbHuPmVgGHd Z1k6HBmLdM8cvC1oCvIrsLpqQHnj4WROXdoObSGim4dYsISGBoqhSDr6zhsWzPtDz7Tp uw0QN8rFRv2H3elszhY7PJaJIH2iPsyXI++YBlz9mxUYYM2iNvQFPPNDk/5VFdCr6RS4 +KusPyjvcH3hIjZjQk7ekMFs+8LbwEpjhaaqDzckEmWCgIwX2ILmzpnf+UqaNIr5VstB VhkQVPUg8K1EPSlyJN/oPiErDwSp4GTSwcvo6ZMlpLtCgTFQzf5KX+1u+Rc+tCTnS0y2 hAnw== 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=vWmPpim0Ul0xIJCOkv50sV36BYq7X902LEG1uHm7C8o=; b=NSbp1WKKSds9n49kbyS3lRkAmXlNEX7ofEbmpvll0kp0hhdxoQb+rCaIkNrD/81pxK iRR1byaPqUhn7R9cKZ/0lrLcjEdut/u+Ps/auhNTYzB5/OK/p/IVPbE0kep5eLJvJFHj uI+rqbMUnYTX3+UzuKmhpUeE1OB4IvTNKJTzinnbkwRJ5/k8dVtO1RQyNgWcpNbJ5jfZ tWzHoSNYWzuJL7pfj+fWKs+wlcd9Kaqi/QcTQAyoPBto2q+mGu9d7lb7q7yQRvRSMOV6 IOMzLtnp2PPfYwln/Pa5EyDzyFSqychgP5QY7UkVIGo9xzFRtjSuiDFtDl0fdu1ZzjtB YLYw== X-Gm-Message-State: APt69E0Y7aX842ilwgxSu78/EY4CHDWc3Edv6oSrzBeNJQ3kXtuGmTQo iE34zWQ4QO7sb2tLT8/k9BY7vZQp+bYBCOqQIhkwt/bKNSess6Z7LpdFI8eCIGZGmw3NXLhTO39 NCvEf+Z9gMozTHyUQDAroIT/XvPYdZ4+UCDPPKGg16HeVjMS5ErjN26oG2gwc X-Google-Smtp-Source: AAOMgpfbmEj6MWs0BRMwma5r5ZbFZHbqxwvZQqj1DfxirajVL/MYWf1MdFhCwjrZSkD+307lISW0wC5U6N92 X-Received: by 2002:a37:4851:: with SMTP id v78-v6mr8637153qka.14.1529977702850; Mon, 25 Jun 2018 18:48:22 -0700 (PDT) Date: Mon, 25 Jun 2018 18:48:18 -0700 Message-Id: <20180626014818.26541-1-marcorr@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.18.0.rc2.346.g013aa6912e-goog Subject: [kvm-unit-tests PATCH v3] x86: Add test for nested VM entry prereqs From: Marc Orr To: kvm@vger.kernel.org Cc: krish.sadhukhan@oracle.com, jmattson@google.com, pbonzini@redhat.com, rkrcmar@redhat.com, pshier@google.com, 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 --- Changelog since v2: - Changed positive test case for "RESERVED interruption type invalid" to use INTR_TYPE_EXT_INTR interruption type. - Added a test case for "error code <-> (!URG || prot_mode)", where unrestricted guest is disabled. - Added tests for INTR_TYPE_PRIV_SW_EXCEPTION and INTR_TYPE_SOFT_INTR. - Annotated logs with [-] for negative test case, where VM-entry should fail and [+] for positive test case, where VM-entry should succeed. Changelog since v1: - Add positive test case for interrupt type != INTR_TYPE_RESERVED - Renamed nr to vector - Add positive test case for interrupt type == INTR_TYPE_NMI_INTR - Explicitly encode 0 vecotr (i.e., DE_VECTOR) - Updated deliver errorcode checks to test postive cases - Moved test for INTR_TYPE_OTHER_EVENT after test for INTR_TYPE_RESERVED lib/x86/processor.h | 8 ++ x86/types.h | 1 + x86/vmx.h | 27 +++++ x86/vmx_tests.c | 276 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 312 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/types.h b/x86/types.h index fd22743990f1..047556e854d6 100644 --- a/x86/types.h +++ b/x86/types.h @@ -3,6 +3,7 @@ #define DE_VECTOR 0 #define DB_VECTOR 1 +#define NMI_VECTOR 2 #define BP_VECTOR 3 #define OF_VECTOR 4 #define BR_VECTOR 5 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..4eb8fefb9bbf 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -3561,6 +3561,281 @@ 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 | DE_VECTOR; + 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(); + + ent_intr_info = ent_intr_info_base | INTR_TYPE_EXT_INTR | + DE_VECTOR; + 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(true, 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 | DB_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(OTHER EVENT && vector != 0) 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 (negative case). */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | DE_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(NMI && vector != 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 NMI, the vector is 2 (positive case). */ + ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | NMI_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "(NMI && vector == 2) valid [+]", ent_intr_info); + vmcs_write(ENT_INTR_INFO, ent_intr_info); + test_vmx_controls(true, 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 && vector > 31) 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_TYPE_HARD_EXCEPTION | + GP_VECTOR; + report_prefix_pushf("%s, VM-entry intr info=0x%x", + "error code <-> (!URG || prot_mode) [-]", + ent_intr_info); + disable_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_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); + disable_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(true, false); + report_prefix_pop(); + + 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 <-> vector delivers error code"); + for (cnt = 0; cnt < 32; cnt++) { + bool has_error_code = false; + u32 deliver_error_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: + has_error_code = true; + } + + /* Negative case */ + deliver_error_code_mask = has_error_code ? + 0 : + INTR_INFO_DELIVER_CODE_MASK; + 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(); + + /* Positive case */ + deliver_error_code_mask = has_error_code ? + INTR_INFO_DELIVER_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(true, 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. + */ + + for (cnt = 0; cnt < 3; cnt++) { + switch (cnt) { + case 0: + ent_intr_info = ent_intr_info_base | + INTR_TYPE_SOFT_INTR; + break; + case 1: + ent_intr_info = ent_intr_info_base | + INTR_TYPE_SOFT_EXCEPTION; + break; + case 2: + ent_intr_info = ent_intr_info_base | + INTR_TYPE_PRIV_SW_EXCEPTION; + break; + } + 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 +4082,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)