From 621b980dc5a2ce4e461ecc1e6b4f31bf2aaf51d0 Mon Sep 17 00:00:00 2001 From: Haoliang Zhu Date: Wed, 29 May 2024 16:15:20 +0800 Subject: [PATCH 1/2] instruction_check:Add instruction check tests 1. Implemented processor instruction set support detection by invoking the cpuid_check tool with the Avocado testing framework. Signed-off-by: Haoliang Zhu --- BM/instruction-check/README.md | 37 +++++++++++++++++ BM/instruction-check/feature_list.py | 49 +++++++++++++++++++++++ BM/instruction-check/instruction_check.py | 25 ++++++++++++ BM/instruction-check/setup.sh | 5 +++ 4 files changed, 116 insertions(+) create mode 100644 BM/instruction-check/README.md create mode 100755 BM/instruction-check/feature_list.py create mode 100755 BM/instruction-check/instruction_check.py create mode 100755 BM/instruction-check/setup.sh diff --git a/BM/instruction-check/README.md b/BM/instruction-check/README.md new file mode 100644 index 00000000..27c41721 --- /dev/null +++ b/BM/instruction-check/README.md @@ -0,0 +1,37 @@ +# Instruction Check + +This README provides information about the `instruction-check` project, which focuses on the CPUID instruction in computer processors. The CPUID instruction allows software programs to query and retrieve detailed information about the processor, including the manufacturer, model, features, and other characteristics. + +## CPUID Check Tool + +In the `LKVS/tools` directory, you can find a pre-existing tool for checking CPUID information. This tool supports six parameters, for example: `cpuid_check 1 0 0 0 c 25`. The first four parameters represent the input values for the EAX, EBX, ECX, and EDX registers, while `c` indicates checking the output of the ECX register, and `25` represents checking the 25th bit. + +## Preparation + +### 1. Install the Avocado Test Framework +The Instruction Check project utilizes the Avocado Test Framework, so it needs to be installed first. You can install the Avocado test framework using either of the following methods: + +1. Source code installation: + ``` + git clone git://github.com/avocado-framework/avocado.git + cd avocado + pip install . + ``` +2. Installation via pip: + ``` + pip3 install --user avocado-framework + ``` + +### 2. Install the HTML plugin +After installing the framework, you also need to install the HTML plugin for generating test results in HTML format. You can do this by running the following command: +``pip install avocado-framework-plugin-result-html`` + +## Usage + +### 1. ompile the cpuid_check tool +Run the command: +``./setuo.sh`` + +### 2. Execute instruction_check.py +Run the command: +``./ininstruction_check.py`` or ``python instruction_check.py`` \ No newline at end of file diff --git a/BM/instruction-check/feature_list.py b/BM/instruction-check/feature_list.py new file mode 100755 index 00000000..ee861af9 --- /dev/null +++ b/BM/instruction-check/feature_list.py @@ -0,0 +1,49 @@ +cpuid_info = { + "AESNI": ['1', '0', '0', '0', 'c', '25'], + "XSAVE": ['1', '0', '0', '0', 'c', '26'], + "FSGSBASE": ['7', '0', '0', '0', 'b', '0'], + "SMEP": ['7', '0', '0', '0', 'b', '7'], + "RDT_A": ['7', '0', '0', '0', 'b', '15'], + "AVX512_IFMA": ['7', '0', '0', '0', 'b', '21'], + "SHA_NI": ['7', '0', '0', '0', 'b', '29'], + "AVX512_VBMI": ['7', '0', '0', '0', 'c', '1'], + "UMIP": ['7', '0', '0', '0', 'c', '2'], + "WAITPKG": ['7', '0', '0', '0', 'c', '5'], + "AVX512_VBMI2": ['7', '0', '0', '0', 'c', '6'], + "CET_SS": ['7', '0', '0', '0', 'c', '7'], + "GFNI": ['7', '0', '0', '0', 'c', '8'], + "VAES": ['7', '0', '0', '0', 'c', '9'], + "AVX512_VNNI": ['7', '0', '0', '0', 'c', '11'], + "AVX512_BITALG": ['7', '0', '0', '0', 'c', '12'], + "RDPID": ['7', '0', '0', '0', 'c', '22'], + "KL": ['7', '0', '0', '0', 'c', '23'], + "CLDEMOTE": ['7', '0', '0', '0', 'c', '25'], + "MOVDIRI": ['7', '0', '0', '0', 'c', '27'], + "MOVDIR64B": ['7', '0', '0', '0', 'c', '28'], + "PKS": ['7', '0', '0', '0', 'c', '31'], + "UINTR": ['7', '0', '0', '0', 'd', '5'], + "AVX512_VP2INTERSECT": ['7', '0', '0', '0', 'd', '8'], + "SERIALIZE": ['7', '0', '0', '0', 'd', '14'], + "TSXLDTRK": ['7', '0', '0', '0', 'd', '16'], + "CET_IBT": ['7', '0', '0', '0', 'd', '20'], + "AMX_BF16": ['7', '0', '0', '0', 'd', '22'], + "AVX512_FP16": ['7', '0', '0', '0', 'd', '23'], + "AMX_TILE": ['7', '0', '0', '0', 'd', '24'], + "AMX_INT8": ['7', '0', '0', '0', 'd', '25'], + "AVX_VNNI": ['7', '0', '1', '0', 'a', '4'], + "FRED": ['7', '0', '1', '0', 'a', '17'], + "WRMSRNS": ['7', '0', '1', '0', 'a', '19'], + "AMX_FP16": ['7', '0', '1', '0', 'a', '21'], + "AVX_IFMA": ['7', '0', '1', '0', 'a', '23'], + "AVX_VNNI_INT8": ['7', '0', '1', '0', 'd', '4'], + "AVX_NE_CONVERT": ['7', '0', '1', '0', 'd', '5'], + "XFD": ['d', '0', '1', '0', 'a', '4'], + "KL_BITMAP0": ['19', '0', '0', '0', 'a', '0'], + "KL_BITMAP1": ['19', '0', '0', '0', 'a', '1'], + "KL_BITMAP2": ['19', '0', '0', '0', 'a', '2'], + "AESKLE": ['19', '0', '0', '0', 'b', '0'], + "AES_WIDE": ['19', '0', '0', '0', 'b', '2'], + "KL_IWKEYBACKUP": ['19', '0', '0', '0', 'b', '4'], + "KL_RANDOM_IWKEY": ['19', '0', '0', '0', 'c', '1'] + # Add more cpuid_info here +} diff --git a/BM/instruction-check/instruction_check.py b/BM/instruction-check/instruction_check.py new file mode 100755 index 00000000..ce2fb1ac --- /dev/null +++ b/BM/instruction-check/instruction_check.py @@ -0,0 +1,25 @@ +#!/usr/bin/python + +import sys +from avocado.core.job import Job +from avocado.core.nrunner.runnable import Runnable +from avocado.core.suite import TestSuite +from feature_list import cpuid_info + +def main(): + feature_name_list = cpuid_info.keys() + + suites = [] + + for feature_name in feature_name_list: + args = cpuid_info[feature_name] + runnable = Runnable("exec-test", "../tools/cpuid_check/cpuid_check", *args) + suite = TestSuite(name=feature_name, tests=[runnable]) + suites.append(suite) + + # Run the test suites + with Job(test_suites=suites) as j: + sys.exit(j.run()) + +if __name__=="__main__": + main() \ No newline at end of file diff --git a/BM/instruction-check/setup.sh b/BM/instruction-check/setup.sh new file mode 100755 index 00000000..bb1c56fb --- /dev/null +++ b/BM/instruction-check/setup.sh @@ -0,0 +1,5 @@ +#!/bin/bash +SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" +PARENT_DIR="$(dirname "$SCRIPT_DIR")" +cd "$PARENT_DIR/tools" || exit 1 +make \ No newline at end of file From a9426c375e595a18f90c514966ef95ac02f6b560 Mon Sep 17 00:00:00 2001 From: Haoliang Zhu Date: Wed, 29 May 2024 16:50:11 +0800 Subject: [PATCH 2/2] instruction-check:Add another way to use instruction-check 1. Added dynamic generation of Avocado test classes from feature_list.py into cpuid_test.py. 2. Fixed some errors in README. Signed-off-by: Haoliang Zhu --- BM/instruction-check/README.md | 23 +++++-- BM/instruction-check/auto_gen_test.py | 36 ++++++++++ BM/instruction-check/feature_list.py | 94 +++++++++++++-------------- 3 files changed, 102 insertions(+), 51 deletions(-) create mode 100755 BM/instruction-check/auto_gen_test.py diff --git a/BM/instruction-check/README.md b/BM/instruction-check/README.md index 27c41721..762e8a9d 100644 --- a/BM/instruction-check/README.md +++ b/BM/instruction-check/README.md @@ -26,12 +26,27 @@ The Instruction Check project utilizes the Avocado Test Framework, so it needs t After installing the framework, you also need to install the HTML plugin for generating test results in HTML format. You can do this by running the following command: ``pip install avocado-framework-plugin-result-html`` -## Usage +## Usage 1 -### 1. ompile the cpuid_check tool +### 1. Compile the cpuid_check tool Run the command: -``./setuo.sh`` +``./setup.sh`` ### 2. Execute instruction_check.py Run the command: -``./ininstruction_check.py`` or ``python instruction_check.py`` \ No newline at end of file +``./instruction_check.py`` or ``python instruction_check.py`` + +## Usage 2 +Usage 1 assembles each test into an Avocado job, so it can be executed directly by calling the Python interpreter. We also provide another method, which generates Avocado test classes based on the information in feature_list.py and writes them into the file cpuid_test.py. + +### 1. Compile the cpuid_check tool +Run the command: +``./setup.sh`` + +### 2. Generate Avocado tests +Run the command: +``./auto_gen_test.py`` or ``python auto_gen_test.py`` + +### 3. Use Avocado to run tests +Run the command: +``avocado run cpuid_test.py`` \ No newline at end of file diff --git a/BM/instruction-check/auto_gen_test.py b/BM/instruction-check/auto_gen_test.py new file mode 100755 index 00000000..47831874 --- /dev/null +++ b/BM/instruction-check/auto_gen_test.py @@ -0,0 +1,36 @@ +#!/usr/bin/python +import os +from feature_list import cpuid_info + +# Template string, used to generate code for each test class +class_template = """ +class {class_name}(Test): + def test(self): + cmd = os.path.join(source_dir, file_name + ' ' + ' '.join({args})) + cmd_result = process.run(cmd, ignore_status=True) + self.assertEqual(cmd_result.exit_status, expected_result) +""" + +def generate_cpuid_tests(): + with open('cpuid_test.py', 'w') as f: + script_dir = os.path.dirname(__file__) + parent_dir = os.path.dirname(script_dir) + grandparent_dir = os.path.dirname(parent_dir) + source_dir = f"{grandparent_dir}/tools/cpuid_check" + # Write the necessary imports and variable definitions + f.write(f'''#!/usr/bin/python + +import os +from avocado import Test +from avocado.utils import process + +source_dir = "{source_dir}" +file_name = "cpuid_check" +expected_result = 0 +''') + # For each feature in feature_list.py generates a test class + for feature, args in cpuid_info.items(): + class_code = class_template.format(class_name=feature, args=args) + f.write(class_code) + +generate_cpuid_tests() \ No newline at end of file diff --git a/BM/instruction-check/feature_list.py b/BM/instruction-check/feature_list.py index ee861af9..82242f38 100755 --- a/BM/instruction-check/feature_list.py +++ b/BM/instruction-check/feature_list.py @@ -1,49 +1,49 @@ cpuid_info = { - "AESNI": ['1', '0', '0', '0', 'c', '25'], - "XSAVE": ['1', '0', '0', '0', 'c', '26'], - "FSGSBASE": ['7', '0', '0', '0', 'b', '0'], - "SMEP": ['7', '0', '0', '0', 'b', '7'], - "RDT_A": ['7', '0', '0', '0', 'b', '15'], - "AVX512_IFMA": ['7', '0', '0', '0', 'b', '21'], - "SHA_NI": ['7', '0', '0', '0', 'b', '29'], - "AVX512_VBMI": ['7', '0', '0', '0', 'c', '1'], - "UMIP": ['7', '0', '0', '0', 'c', '2'], - "WAITPKG": ['7', '0', '0', '0', 'c', '5'], - "AVX512_VBMI2": ['7', '0', '0', '0', 'c', '6'], - "CET_SS": ['7', '0', '0', '0', 'c', '7'], - "GFNI": ['7', '0', '0', '0', 'c', '8'], - "VAES": ['7', '0', '0', '0', 'c', '9'], - "AVX512_VNNI": ['7', '0', '0', '0', 'c', '11'], - "AVX512_BITALG": ['7', '0', '0', '0', 'c', '12'], - "RDPID": ['7', '0', '0', '0', 'c', '22'], - "KL": ['7', '0', '0', '0', 'c', '23'], - "CLDEMOTE": ['7', '0', '0', '0', 'c', '25'], - "MOVDIRI": ['7', '0', '0', '0', 'c', '27'], - "MOVDIR64B": ['7', '0', '0', '0', 'c', '28'], - "PKS": ['7', '0', '0', '0', 'c', '31'], - "UINTR": ['7', '0', '0', '0', 'd', '5'], - "AVX512_VP2INTERSECT": ['7', '0', '0', '0', 'd', '8'], - "SERIALIZE": ['7', '0', '0', '0', 'd', '14'], - "TSXLDTRK": ['7', '0', '0', '0', 'd', '16'], - "CET_IBT": ['7', '0', '0', '0', 'd', '20'], - "AMX_BF16": ['7', '0', '0', '0', 'd', '22'], - "AVX512_FP16": ['7', '0', '0', '0', 'd', '23'], - "AMX_TILE": ['7', '0', '0', '0', 'd', '24'], - "AMX_INT8": ['7', '0', '0', '0', 'd', '25'], - "AVX_VNNI": ['7', '0', '1', '0', 'a', '4'], - "FRED": ['7', '0', '1', '0', 'a', '17'], - "WRMSRNS": ['7', '0', '1', '0', 'a', '19'], - "AMX_FP16": ['7', '0', '1', '0', 'a', '21'], - "AVX_IFMA": ['7', '0', '1', '0', 'a', '23'], - "AVX_VNNI_INT8": ['7', '0', '1', '0', 'd', '4'], - "AVX_NE_CONVERT": ['7', '0', '1', '0', 'd', '5'], - "XFD": ['d', '0', '1', '0', 'a', '4'], - "KL_BITMAP0": ['19', '0', '0', '0', 'a', '0'], - "KL_BITMAP1": ['19', '0', '0', '0', 'a', '1'], - "KL_BITMAP2": ['19', '0', '0', '0', 'a', '2'], - "AESKLE": ['19', '0', '0', '0', 'b', '0'], - "AES_WIDE": ['19', '0', '0', '0', 'b', '2'], - "KL_IWKEYBACKUP": ['19', '0', '0', '0', 'b', '4'], - "KL_RANDOM_IWKEY": ['19', '0', '0', '0', 'c', '1'] - # Add more cpuid_info here + "AESNI": ['1', '0', '0', '0', 'c', '25'], + "XSAVE": ['1', '0', '0', '0', 'c', '26'], + "FSGSBASE": ['7', '0', '0', '0', 'b', '0'], + "SMEP": ['7', '0', '0', '0', 'b', '7'], + "RDT_A": ['7', '0', '0', '0', 'b', '15'], + "AVX512_IFMA": ['7', '0', '0', '0', 'b', '21'], + "SHA_NI": ['7', '0', '0', '0', 'b', '29'], + "AVX512_VBMI": ['7', '0', '0', '0', 'c', '1'], + "UMIP": ['7', '0', '0', '0', 'c', '2'], + "WAITPKG": ['7', '0', '0', '0', 'c', '5'], + "AVX512_VBMI2": ['7', '0', '0', '0', 'c', '6'], + "CET_SS": ['7', '0', '0', '0', 'c', '7'], + "GFNI": ['7', '0', '0', '0', 'c', '8'], + "VAES": ['7', '0', '0', '0', 'c', '9'], + "AVX512_VNNI": ['7', '0', '0', '0', 'c', '11'], + "AVX512_BITALG": ['7', '0', '0', '0', 'c', '12'], + "RDPID": ['7', '0', '0', '0', 'c', '22'], + "KL": ['7', '0', '0', '0', 'c', '23'], + "CLDEMOTE": ['7', '0', '0', '0', 'c', '25'], + "MOVDIRI": ['7', '0', '0', '0', 'c', '27'], + "MOVDIR64B": ['7', '0', '0', '0', 'c', '28'], + "PKS": ['7', '0', '0', '0', 'c', '31'], + "UINTR": ['7', '0', '0', '0', 'd', '5'], + "AVX512_VP2INTERSECT": ['7', '0', '0', '0', 'd', '8'], + "SERIALIZE": ['7', '0', '0', '0', 'd', '14'], + "TSXLDTRK": ['7', '0', '0', '0', 'd', '16'], + "CET_IBT": ['7', '0', '0', '0', 'd', '20'], + "AMX_BF16": ['7', '0', '0', '0', 'd', '22'], + "AVX512_FP16": ['7', '0', '0', '0', 'd', '23'], + "AMX_TILE": ['7', '0', '0', '0', 'd', '24'], + "AMX_INT8": ['7', '0', '0', '0', 'd', '25'], + "AVX_VNNI": ['7', '0', '1', '0', 'a', '4'], + "FRED": ['7', '0', '1', '0', 'a', '17'], + "WRMSRNS": ['7', '0', '1', '0', 'a', '19'], + "AMX_FP16": ['7', '0', '1', '0', 'a', '21'], + "AVX_IFMA": ['7', '0', '1', '0', 'a', '23'], + "AVX_VNNI_INT8": ['7', '0', '1', '0', 'd', '4'], + "AVX_NE_CONVERT": ['7', '0', '1', '0', 'd', '5'], + "XFD": ['d', '0', '1', '0', 'a', '4'], + "KL_BITMAP0": ['19', '0', '0', '0', 'a', '0'], + "KL_BITMAP1": ['19', '0', '0', '0', 'a', '1'], + "KL_BITMAP2": ['19', '0', '0', '0', 'a', '2'], + "AESKLE": ['19', '0', '0', '0', 'b', '0'], + "AES_WIDE": ['19', '0', '0', '0', 'b', '2'], + "KL_IWKEYBACKUP": ['19', '0', '0', '0', 'b', '4'], + "KL_RANDOM_IWKEY": ['19', '0', '0', '0', 'c', '1'] + # Add more cpuid_info here }