Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4073315pxv; Mon, 26 Jul 2021 20:36:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxw7WgwBxQb27W7+Q9liy5fZBdGuLyPfIoMl4kYZTGVCwT+UUnHEiq/xmdCtEOzNK2YFWZU X-Received: by 2002:a05:6602:26d2:: with SMTP id g18mr16983842ioo.194.1627356983897; Mon, 26 Jul 2021 20:36:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627356983; cv=none; d=google.com; s=arc-20160816; b=C7NGHF8hJrMJEOtF3KEoMyyyXCwDB2hF3jMN+UIuaLStQucnn3zlmN8b/ARNXP9hWA qV7rr8IDG5vfakFPNER+EvIWqCS602EC5/Ec+YQmbYdAmzXAzayzX3n7g8TZRgbKBlm4 TSO0RiGYiAhcBjndPQJyLzqrZGvXLoPXILwYm03aQnHXRz9XlDcAc8BPbvRGns7kbORZ Kg+OhVk/OBDh/qbgzzkj8cWxuu20YyTxj7GfkAeCIUPFznMlYluH6MJucHOhXQ3V5owi SL5YIGtrvOIWkJBEqX2uONreNKQpBy6xwEcvG3i31l8H+bSpbIK5yLz/nseC8W3jYSv5 LPzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=/DmDVxGSp0uK/5TLS3FIWd+O/4w/lCKQya2Vmo2RHd0=; b=a4nnjqa5LLHJ4WT4oMKO3ui/1PMLeFFobXpZkq+XBUOxfXAVn0MSrWm607cj+8WrZn Q0GZVWr/44S9Mj5Qz9xV66me2ui7SqETZfOO8K2QxfAxcv52rdi/Bcj+Kx3Uf84Jkx/W UD7IvB0VKCLtxDkASgV+LhOyyOLdHqWqx686G0MjRfRZrsjukb+FQYlW1TQcyDK/OEKL AzsKPHj6rH8K+pbtwirlQxf4A+v5bCwp1ysTw9ohjmLoWHQNpVwtdTeTpOYYwVy/tdC9 b5t1poyybZe3PVf7y1w+Wtn0Gpo2TVwl2sItlYxvOVbAf67QdVeuEwTQjDqjLAhiqhAZ qkzQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q14si1998681ils.147.2021.07.26.20.36.12; Mon, 26 Jul 2021 20:36:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235089AbhG0Cy7 (ORCPT + 99 others); Mon, 26 Jul 2021 22:54:59 -0400 Received: from mga18.intel.com ([134.134.136.126]:58530 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235052AbhG0Cy6 (ORCPT ); Mon, 26 Jul 2021 22:54:58 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10057"; a="199586977" X-IronPort-AV: E=Sophos;i="5.84,272,1620716400"; d="scan'208";a="199586977" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2021 20:35:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,272,1620716400"; d="scan'208";a="505307699" Received: from fedora29.sh.intel.com ([10.239.182.87]) by FMSMGA003.fm.intel.com with ESMTP; 26 Jul 2021 20:35:22 -0700 From: Pengfei Xu To: Shuah Khan , linux-kselftest , linux-kernel Cc: Pengfei Xu , Heng Su , Yu Yu-cheng , Yu Fenghua , Hansen Dave , Luck Tony , Mehta Sohil , Chen Yu C , kernel test robot Subject: [RFC PATCH v1 1/2] selftests/xsave: test basic XSAVE architecture functionality Date: Tue, 27 Jul 2021 11:34:43 +0800 Message-Id: <5cfbc30dcc6e684a307ac4bb90e34affdfaa9d56.1627355565.git.pengfei.xu@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The XSAVE feature set supports the saving and restoring of state components such as FPU, which is used for process context switching. In order to ensure that XSAVE feature works correctly, this case tests the execution of xsave/xrstor in user space: show and tests the content change for xsave; the contents of xrstor and xsave should same on x86 platforms. This case is the basis for XSAVE feature testing. Signed-off-by: Pengfei Xu Reported-by: kernel test robot # compile issues during review --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/xsave/.gitignore | 2 + tools/testing/selftests/xsave/Makefile | 6 + tools/testing/selftests/xsave/xsave_common.h | 246 ++++++++++++++++++ .../selftests/xsave/xsave_instruction.c | 83 ++++++ 5 files changed, 338 insertions(+) create mode 100644 tools/testing/selftests/xsave/.gitignore create mode 100644 tools/testing/selftests/xsave/Makefile create mode 100644 tools/testing/selftests/xsave/xsave_common.h create mode 100644 tools/testing/selftests/xsave/xsave_instruction.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index fb010a35d61a..4c0ddb8228e1 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -73,6 +73,7 @@ TARGETS += user TARGETS += vDSO TARGETS += vm TARGETS += x86 +TARGETS += xsave TARGETS += zram #Please keep the TARGETS list alphabetically sorted # Run "make quicktest=1 run_tests" or diff --git a/tools/testing/selftests/xsave/.gitignore b/tools/testing/selftests/xsave/.gitignore new file mode 100644 index 000000000000..00b9970360c4 --- /dev/null +++ b/tools/testing/selftests/xsave/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +xsave_instruction diff --git a/tools/testing/selftests/xsave/Makefile b/tools/testing/selftests/xsave/Makefile new file mode 100644 index 000000000000..dafdb0abdeb3 --- /dev/null +++ b/tools/testing/selftests/xsave/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-only +CFLAGS := -g -Wall -mxsave -O2 + +TEST_GEN_PROGS := xsave_instruction + +include ../lib.mk diff --git a/tools/testing/selftests/xsave/xsave_common.h b/tools/testing/selftests/xsave/xsave_common.h new file mode 100644 index 000000000000..c555f9d289a3 --- /dev/null +++ b/tools/testing/selftests/xsave/xsave_common.h @@ -0,0 +1,246 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" + +#ifdef __i386__ +#define XSAVE _xsave +#else +#define XSAVE _xsave64 +#endif + +#ifdef __i386__ +#define XRSTOR _xrstor +#else +#define XRSTOR _xrstor64 +#endif + +#define SAVE_MASK 0xffffffffffffffff +#define RESULT_PASS 0 +#define RESULT_FAIL 1 +#define RESULT_ERROR 3 +#define CHANGE 10 +#define NO_CHANGE 11 + +/* Copied from Linux kernel */ +static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, + unsigned int *ecx, unsigned int *edx) +{ + /* ecx is often an input as well as an output. */ + asm volatile("cpuid" + : "=a" (*eax), + "=b" (*ebx), + "=c" (*ecx), + "=d" (*edx) + : "0" (*eax), "2" (*ecx) + : "memory"); +} + +void execution_failed(char *reason) +{ + ksft_test_result_xfail("%s", reason); + ksft_exit_fail(); +} + +int get_xsave_size(void) +{ + unsigned int eax, ebx, ecx, edx; + + eax = 0x0d; + ebx = 0; + ecx = 0; + edx = 0; + native_cpuid(&eax, &ebx, &ecx, &edx); + + return (int)ecx; +} + +void dump_buffer(unsigned char *buf, int size) +{ + int i, j; + + printf("xsave size = %d (%03xh)\n", size, size); + + for (i = 0; i < size; i += 16) { + printf("%04x: ", i); + + for (j = i; ((j < i + 16) && (j < size)); j++) + printf("%02x ", buf[j]); + printf("\n"); + } +} + +void show_part_buf(unsigned char *buf0, unsigned char *buf1, int start, + int size) +{ + int c; + + printf("%04x: ", start); + for (c = start; ((c < start + 16) && (c < size)); c++) + printf("%02x ", buf0[c]); + printf(" -> "); + for (c = start; ((c < start + 16) && (c < size)); c++) + printf("%02x ", buf1[c]); + printf("\n"); +} + +int show_buf_diff(unsigned char *buf0, unsigned char *buf1, int size) +{ + int a, b, result_buf = RESULT_PASS; + + for (a = 0; a < size; a += 16) { + /* SDM "XSAVE Area": XSAVE feature set does not use bytes 511:416 */ + if ((a >= 416) && (a <= 511)) + continue; + + for (b = a; ((b < a + 16) && (b < size)); b++) { + if (buf0[b] != buf1[b]) { + show_part_buf(buf0, buf1, a, size); + result_buf = RESULT_FAIL; + break; + } + } + } + + return result_buf; +} + +int check_xsave_reserved_header(unsigned char *buf0, + unsigned char *buf1, int size, const char *test_name) +{ + int a, b, result_resv_header = RESULT_PASS; + + /* SDM "Form of XRSTOR": Bytes 63:16 of the XSAVE header should 0 */ + for (a = 528; a < 576 ; a += 16) { + for (b = a; ((b < a + 16) && (b < size)); b++) { + if ((buf0[b] != 0) || (buf1[b] != 0)) { + ksft_print_msg("%s FAIL: buf0[%d]:%d or buf1[%d]:%d not 0\n", + test_name, b, buf0[b], b, buf1[b]); + show_part_buf(buf0, buf1, a, size); + result_resv_header = RESULT_FAIL; + break; + } + } + } + + return result_resv_header; +} + +int check_xsave_buf(unsigned char *buf0, unsigned char *buf1, + int size, const char *test_name, int change) +{ + int result_buf = RESULT_ERROR, result_resv_header = RESULT_ERROR; + + switch (change) { + case CHANGE: + if (show_buf_diff(buf0, buf1, size)) + result_buf = RESULT_PASS; + else { + ksft_print_msg("%s FAIL: xsave content was same\n", test_name); + result_buf = RESULT_FAIL; + } + break; + case NO_CHANGE: + if (show_buf_diff(buf0, buf1, size)) { + ksft_print_msg("%s FAIL: xsave content changed\n", test_name); + show_buf_diff(buf0, buf1, size); + result_buf = RESULT_FAIL; + } else + result_buf = RESULT_PASS; + break; + default: + ksft_test_result_error("%s ERROR: invalid change:%d\n", test_name, + change); + break; + } + + result_resv_header = check_xsave_reserved_header(buf0, buf1, size, + test_name); + + return (result_buf || result_resv_header); +} + +void check_result(int result, const char *test_name) +{ + switch (result) { + case RESULT_PASS: + ksft_test_result_pass("%s PASS\n", test_name); + break; + case RESULT_FAIL: + ksft_test_result_fail("%s FAIL\n", test_name); + break; + case RESULT_ERROR: + ksft_test_result_fail("%s ERROR\n", test_name); + break; + default: + ksft_test_result_error("%s ERROR: invalid result:%c\n", + test_name, result); + break; + } +} + +void populate_fpu_regs(void) +{ + uint32_t ui32; + uint64_t ui64; + + ui32 = 1; + ui64 = 0xBAB00500FAB7; + + /* Initialize FPU and push different values onto FPU register stack: */ + asm volatile ("finit"); + asm volatile ("fldl %0" : : "m" (ui64)); + asm volatile ("flds %0" : : "m" (ui32)); + ui64 += 0x93ABE13; + asm volatile ("fldl %0" : : "m" (ui64)); + ui64 += 0x93; + asm volatile ("fldl %0" : : "m" (ui64)); + asm volatile ("flds %0" : : "m" (ui32)); + asm volatile ("fldl %0" : : "m" (ui64)); + ui64 -= 0x21; + asm volatile ("fldl %0" : : "m" (ui64)); + asm volatile ("flds %0" : : "m" (ui32)); + asm volatile ("fldl %0" : : "m" (ui64)); + + /* Fill each remaining YMM register with a different value: */ + asm volatile ("vbroadcastss %0, %%ymm0" : : "m" (ui32)); + ui32 = 0xFAFBABAF; + asm volatile ("vbroadcastss %0, %%ymm1" : : "m" (ui32)); + ui32 -= 0xA; + asm volatile ("vbroadcastss %0, %%ymm2" : : "m" (ui32)); + ui32 -= 0xB; + asm volatile ("vbroadcastss %0, %%ymm3" : : "m" (ui32)); + ui32 -= 0x3; + asm volatile ("vbroadcastss %0, %%ymm4" : : "m" (ui32)); + ui32 += 0xA; + asm volatile ("vbroadcastss %0, %%ymm5" : : "m" (ui32)); + ui32 -= 0x7; + asm volatile ("vbroadcastss %0, %%ymm6" : : "m" (ui32)); + ui32 -= 0xABABA; + asm volatile ("vbroadcastss %0, %%ymm7" : : "m" (ui32)); + + #ifndef __i386__ + ui32 += 0xF7; + asm volatile ("vbroadcastss %0, %%ymm8" : : "m" (ui32)); + ui32 -= 0x7; + asm volatile ("vbroadcastss %0, %%ymm9" : : "m" (ui32)); + ui32 += 0x2; + asm volatile ("vbroadcastss %0, %%ymm10" : : "m" (ui32)); + ui32 += 0xD; + asm volatile ("vbroadcastss %0, %%ymm11" : : "m" (ui32)); + ui32 -= 0x4; + asm volatile ("vbroadcastss %0, %%ymm12" : : "m" (ui32)); + ui32 -= 0xDD; + asm volatile ("vbroadcastss %0, %%ymm13" : : "m" (ui32)); + ui32 -= 0xABD; + asm volatile ("vbroadcastss %0, %%ymm14" : : "m" (ui32)); + ui32 += 0xBEBABF456; + asm volatile ("vbroadcastss %0, %%ymm15" : : "m" (ui32)); + #endif +} diff --git a/tools/testing/selftests/xsave/xsave_instruction.c b/tools/testing/selftests/xsave/xsave_instruction.c new file mode 100644 index 000000000000..2b610101511d --- /dev/null +++ b/tools/testing/selftests/xsave/xsave_instruction.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test kernel support for XSAVE-managed features. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "xsave_common.h" + +static unsigned char *xsave_buf0, *xsave_buf1; + +static void set_ymm0_reg(uint32_t ui32) +{ + asm volatile ("vbroadcastss %0, %%ymm0" : : "m" (ui32)); +} + +static void dump_xsave_content(int xsave_size) +{ + XSAVE(xsave_buf0, SAVE_MASK); + dump_buffer(xsave_buf0, xsave_size); + ksft_print_msg("Entire contents of XSAVE is as above\n"); +} + +static void test_xsave_ymm_change(int xsave_size) +{ + const char *test_name = "xsave test after ymm change"; + uint32_t ui32_set = 0x1234, ui32_change = 0x5678; + int result = RESULT_ERROR; + + set_ymm0_reg(ui32_set); + XSAVE(xsave_buf0, SAVE_MASK); + set_ymm0_reg(ui32_change); + XSAVE(xsave_buf1, SAVE_MASK); + result = check_xsave_buf(xsave_buf0, xsave_buf1, xsave_size, test_name, + CHANGE); + check_result(result, test_name); +} + +static void test_xsave_xrstor(int xsave_size) +{ + const char *test_name = "xsave after xrstor test"; + int result = RESULT_ERROR; + + XSAVE(xsave_buf0, SAVE_MASK); + XRSTOR(xsave_buf0, SAVE_MASK); + XSAVE(xsave_buf1, SAVE_MASK); + result = check_xsave_buf(xsave_buf0, xsave_buf1, xsave_size, test_name, + NO_CHANGE); + check_result(result, test_name); +} + +int main(void) +{ + int xsave_size; + + ksft_print_header(); + ksft_set_plan(2); + + xsave_size = get_xsave_size(); + /* SDM XSAVE: misalignment to a 64-byte boundary will result in #GP */ + xsave_buf0 = aligned_alloc(64, xsave_size); + if (!xsave_buf0) + execution_failed("aligned_alloc xsave_buf0 failed\n"); + xsave_buf1 = aligned_alloc(64, xsave_size); + if (!xsave_buf1) + execution_failed("aligned_alloc xsave_buf1 failed\n"); + + populate_fpu_regs(); + /* Show the entire contents of xsave for issue debug */ + dump_xsave_content(xsave_size); + + test_xsave_ymm_change(xsave_size); + test_xsave_xrstor(xsave_size); + + ksft_exit(!ksft_get_fail_cnt()); +} -- 2.20.1