From patchwork Mon Aug 14 05:08:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 9898091 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 80FB8602D9 for ; Mon, 14 Aug 2017 05:11:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 72A112858B for ; Mon, 14 Aug 2017 05:11:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 670B5285CB; Mon, 14 Aug 2017 05:11:05 +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=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A91402858B for ; Mon, 14 Aug 2017 05:11:04 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dh7cS-0003wB-Bs; Mon, 14 Aug 2017 05:08:52 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dh7cR-0003w5-Jz for xen-devel@lists.xen.org; Mon, 14 Aug 2017 05:08:51 +0000 Received: from [85.158.139.211] by server-17.bemta-5.messagelabs.com id A9/B2-18997-26031995; Mon, 14 Aug 2017 05:08:50 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrGIsWRWlGSWpSXmKPExsXiVRvspJtoMDH SoH+5mcWSj4tZHBg9ju7+zRTAGMWamZeUX5HAmvHiyxKmgpv2FS0b7BsY9+p0MXJxCAn0M0rM nbyICcRhEXjJInF81TN2EEdCoJ9VovXRbyCHE8hJktgz+QIjhF0psW/aS2YQW0hAWeLY44NsE KO+MEpM+TKHDSTBJmAosfZrIwuILSIgLXHt82VGkCJmgVmMEu82ngVLCAsYSzzt2wlmswioSi xpmAhm8wpYSuzffw9qs7zExN5pYJs5BawkDj84ygqx2VJiedsLtgmMAgsYGVYxqhenFpWlFum a6SUVZaZnlOQmZuboGhqY6uWmFhcnpqfmJCYV6yXn525iBIYWAxDsYJza4HyIUZKDSUmUN8Gn N1KILyk/pTIjsTgjvqg0J7X4EKMMB4eSBK+4/sRIIcGi1PTUirTMHGCQw6QlOHiURHg79YDSv MUFibnFmekQqVOMxhyTDmz/wsTxasL/b0xCLHn5ealS4rxyIJMEQEozSvPgBsGi7xKjrJQwLy PQaUI8BalFuZklqPKvGMU5GJWEeS+DLOTJzCuB2/cK6BQmoFP6QL7gLS5JREhJNTAGtbbWddR YrqhJPzltbcntjm0/3hZO1ApItDzUGXXgk0X4z6hZGtt2c0WdjawMVPVZ8Py9brf2OhbxcouD G3hvPrG+emKyuv5Moa3fO21kLaLzE+6sOHTyasz07iUHJc2FDa/78Sx6o1PxduXTvKWeE2/9O MfhrRzV5sSlpX5MNyXujFw+50klluKMREMt5qLiRACRJ2icuQIAAA== X-Env-Sender: boqun.feng@gmail.com X-Msg-Ref: server-6.tower-206.messagelabs.com!1502687328!104264858!1 X-Originating-IP: [74.125.83.66] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 31292 invoked from network); 14 Aug 2017 05:08:49 -0000 Received: from mail-pg0-f66.google.com (HELO mail-pg0-f66.google.com) (74.125.83.66) by server-6.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 14 Aug 2017 05:08:49 -0000 Received: by mail-pg0-f66.google.com with SMTP id 83so8768201pgb.4 for ; Sun, 13 Aug 2017 22:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=BYujaT9b+WdH7o971ta/WcBa3cu2Sph+BK70vr2n4Ck=; b=Fnz3SYcOfF5oq8mIcpK7EWTRMkNEgHEhbdTqofv26jfW5iwsiNx+b7nQeULYyoyY87 Rf2XKfE/CFSACqo7bihlUSFAioChD46XkzOH4aeNSUf47T0I1ubz8RS2dRoE6oasxX8S NvYnfE4guVYUPUHw/RRVSOdRlx5i0FEB8pEKMPZ1Dk2XNsx7+dJnjJGzD4TlPKoYZMJ0 MArJF1+GJe+BCQ7qfaezwLnjj4lzlwG/VIb6KYWo7xxbL0Ha402P3QsR7UDWN/nQnTzM MlFGh81yy1Q5Zn7KY6sLD8vLJF2Cw6NRBQYQv1vfcaEZhg0PWfPuhSgP1t4Y4xj0Vcoo UX4Q== 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=BYujaT9b+WdH7o971ta/WcBa3cu2Sph+BK70vr2n4Ck=; b=cnXTpqMK5a4ABgoRqgewEdng68Uxlkq8vDtixfU/giIBkA2H4fWQq+G+SvI0p173Xn 4vhtT+C4Bltx1ffHFZCwZIyCUTJX6J/8k1/hLzN1Kjz7SnB/5XNED2WHUGY+jIVj7vi9 zuovbBOE5VbCDsIur3ZCrd2HtrN3D6pTN3MSOZciX5kCEYqd2eQrSYP58GLcSyXCFyGR qr76UGRym5c5A7xvM4HcWx27Pt2QpR1eRj78hcEqx13OoEvUsM5ahuHUwTqvk56I76eV 1GJoW/vSmynKFErqX3EjhcIR/0iac5J+ogIJ9tJ4GvqdFjcOOJcYBM9pfSNRbCnczKJa TKVA== X-Gm-Message-State: AHYfb5jRQTxq+e2WYxxUnxnaF/SQNnP429JbWdI8890ICJZ2pNFvhFY+ b7V8VxlisE7MhZRSGjY= X-Received: by 10.84.138.129 with SMTP id 1mr27053007plp.223.1502687328036; Sun, 13 Aug 2017 22:08:48 -0700 (PDT) Received: from localhost ([134.134.139.77]) by smtp.gmail.com with ESMTPSA id x29sm12108565pff.2.2017.08.13.22.08.46 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 13 Aug 2017 22:08:46 -0700 (PDT) From: "Boqun Feng (Intel)" To: xen-devel@lists.xen.org Date: Mon, 14 Aug 2017 13:08:08 +0800 Message-Id: <20170814050808.30758-1-boqun.feng@gmail.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170720052921.31586-1-boqun.feng@gmail.com> References: <20170720052921.31586-1-boqun.feng@gmail.com> Cc: Andrew Cooper , "Boqun Feng \(Intel\)" , yu.c.zhang@linux.intel.com, Jan Beulich Subject: [Xen-devel] [PATCH XTF v2] Functional: Add a UMIP test X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a "umip" test for the User-Model Instruction Prevention. The test simply tries to run sgdt/sidt/sldt/str/smsw in guest user-mode with CR4_UMIP = 1. Signed-off-by: Boqun Feng (Intel) --- v1 --> v2: * add a new write_cr4_safe() * use %pe for exception print * refactor the code based on Andrew's guide and advice Test results: * With UMIP patch: ** boot with hvm_fep: SUCCESS ** boot without hvm_fep: SKIP, due to "FEP support not detected.." * Without UMIP patch: ** boot with hvm_fep: SKIP, due to "UMIP is not supported.." ** boot without hvm_fep: SKIP, due to "UMIP is not supported.." * With UMIP cpuid exposed but CR4 invalid: ** boot with hvm_fep: FAILURE, due to "Fail: Unable to activate UMIP.." ** boot without hvm_fep: FAILURE, due to "Fail: Unable to activate UMIP.." arch/x86/include/arch/lib.h | 13 +++ docs/all-tests.dox | 2 + tests/umip/Makefile | 9 ++ tests/umip/main.c | 219 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 tests/umip/Makefile create mode 100644 tests/umip/main.c diff --git a/arch/x86/include/arch/lib.h b/arch/x86/include/arch/lib.h index f608af9996f0..4f0d85290cf0 100644 --- a/arch/x86/include/arch/lib.h +++ b/arch/x86/include/arch/lib.h @@ -340,6 +340,19 @@ static inline void write_cr4(unsigned long cr4) asm volatile ("mov %0, %%cr4" :: "r" (cr4)); } +static inline bool write_cr4_safe(unsigned long cr4) +{ + exinfo_t fault = 0; + + asm volatile ("1: mov %1, %%cr4; 2:" + _ASM_EXTABLE_HANDLER(1b, 2b, ex_record_fault_edi) + : "+D" (fault) + : "r" (cr4), + "X" (ex_record_fault_edi)); + + return !fault; +} + static inline void write_cr8(unsigned long cr8) { asm volatile ("mov %0, %%cr8" :: "r" (cr8)); diff --git a/docs/all-tests.dox b/docs/all-tests.dox index c1b163a926cb..ef011007cf68 100644 --- a/docs/all-tests.dox +++ b/docs/all-tests.dox @@ -111,4 +111,6 @@ guest breakout. @section index-in-development In Development @subpage test-vvmx - Nested VT-x tests. + +@subpage test-umip - User-Mode Instruction Prevention */ diff --git a/tests/umip/Makefile b/tests/umip/Makefile new file mode 100644 index 000000000000..0248c8b247a0 --- /dev/null +++ b/tests/umip/Makefile @@ -0,0 +1,9 @@ +include $(ROOT)/build/common.mk + +NAME := umip +CATEGORY := functional +TEST-ENVS := hvm32 hvm64 + +obj-perenv += main.o + +include $(ROOT)/build/gen.mk diff --git a/tests/umip/main.c b/tests/umip/main.c new file mode 100644 index 000000000000..fcaba4e34570 --- /dev/null +++ b/tests/umip/main.c @@ -0,0 +1,219 @@ +/** + * @file tests/umip/main.c + * @ref test-umip + * + * @page test-umip umip + * + * @todo Docs for test-umip + * + * @see tests/umip/main.c + */ +#include +#include +#include + +const char test_title[] = "User-Mode Instruction Prevention Test"; +bool test_wants_user_mappings = true; + +static unsigned long stub_sgdt(unsigned long force) +{ + exinfo_t fault = 0; + desc_ptr tmp; + + asm volatile("test %[fep], %[fep];" + "jz 1f;" + _ASM_XEN_FEP + "1: sgdt %[tmp]; 2:" + _ASM_EXTABLE_HANDLER(1b,2b, ex_record_fault_edi) + : "+D" (fault), [tmp] "=m" (tmp) + : [fep] "q" (force), + "X" (ex_record_fault_edi)); + + return fault; +} +static unsigned long stub_sidt(unsigned long force) +{ + exinfo_t fault = 0; + desc_ptr tmp; + + asm volatile("test %[fep], %[fep];" + "jz 1f;" + _ASM_XEN_FEP + "1: sidt %[tmp]; 2:" + _ASM_EXTABLE_HANDLER(1b,2b, ex_record_fault_edi) + : "+D" (fault), [tmp] "=m" (tmp) + : [fep] "q" (force), + "X" (ex_record_fault_edi)); + + return fault; +} + +static unsigned long stub_sldt(unsigned long force) +{ + exinfo_t fault = 0; + unsigned int tmp; + + asm volatile("test %[fep], %[fep];" + "jz 1f;" + _ASM_XEN_FEP + "1: sldt %[tmp]; 2:" + _ASM_EXTABLE_HANDLER(1b,2b, ex_record_fault_edi) + : "+D" (fault), [tmp] "=r" (tmp) + : [fep] "q" (force), + "X" (ex_record_fault_edi)); + + return fault; +} + +static unsigned long stub_str(unsigned long force) +{ + exinfo_t fault = 0; + unsigned int tmp; + + asm volatile("test %[fep], %[fep];" + "jz 1f;" + _ASM_XEN_FEP + "1: str %[tmp]; 2:" + _ASM_EXTABLE_HANDLER(1b,2b, ex_record_fault_edi) + : "+D" (fault), [tmp] "=r" (tmp) + : [fep] "q" (force), + "X" (ex_record_fault_edi)); + + return fault; +} + +static unsigned long stub_smsw(unsigned long force) +{ + exinfo_t fault = 0; + unsigned int tmp; + + asm volatile("test %[fep], %[fep];" + "jz 1f;" + _ASM_XEN_FEP + "1: smsw %[tmp]; 2:" + _ASM_EXTABLE_HANDLER(1b,2b, ex_record_fault_edi) + : "+D" (fault), [tmp] "=r" (tmp) + : [fep] "q" (force), + "X" (ex_record_fault_edi)); + + return fault; +} + +static const struct stub { + unsigned long (*fn)(unsigned long); + const char *name; +} stubs[] = { + { stub_sgdt, "SGDT" }, + { stub_sidt, "SIDT" }, + { stub_sldt, "SLDT" }, + { stub_str, "STR" }, + { stub_smsw, "SMSW" }, +}; + +void test_umip(bool umip_active, bool force) +{ + unsigned int i; + bool user; + + for ( user = false; ; user = true ) + { + exinfo_t exp = user && umip_active ? EXINFO_SYM(GP, 0) : 0; + + for ( i = 0; i < ARRAY_SIZE(stubs); i++) + { + const struct stub *s = &stubs[i]; + exinfo_t ret; + + ret = user ? exec_user_param(s->fn, force) : s->fn(force); + + /* + * Tolerate the instruction emulator not understanding these + * instructions in older releases of Xen. + */ + + if ( force && ret == EXINFO_SYM(UD, 0) ) + { + static bool once; + + if ( !once ) + { + xtf_skip("Skip: Emulator doesn't implement %s\n", s->name); + once = true; + } + + continue; + } + + if ( ret != exp ) + xtf_failure("Fail: %s %s\n" + " expected %pe\n" + " got %pe\n", + user ? "user" : "supervisor", s->name, + _p(exp), _p(ret)); + } + + if ( user ) + break; + } +} + +void test_main(void) +{ + unsigned long cr4; + + /* run with UMIP inactive */ + test_umip(false, false); + + if ( !xtf_has_fep ) + xtf_skip("FEP support not detected - some tests will be skipped\n"); + else + test_umip(false, true); + + if ( !cpu_has_umip ) + { + xtf_skip("UMIP is not supported, skip the rest of test\n"); + return; + } + + cr4 = read_cr4(); + + if ( !write_cr4_safe(cr4 | X86_CR4_UMIP) ) + { + xtf_failure("Fail: Unable to activate UMIP\n"); + return; + } + + /* run with UMIP active */ + test_umip(true, false); + + if ( !xtf_has_fep ) + xtf_skip("FEP support not detected - some tests will be skipped\n"); + else + test_umip(true, true); + + if ( !write_cr4_safe(cr4) ) + { + xtf_failure("Fail: Unable to deactivate UMIP\n"); + return; + } + + /* run with UMIP inactive */ + test_umip(false, false); + + if ( !xtf_has_fep ) + xtf_skip("FEP support not detected - some tests will be skipped\n"); + else + test_umip(false, true); + + xtf_success(NULL); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */