From ddd75790ffd7f53f022c2470359be8146e751a93 Mon Sep 17 00:00:00 2001 From: nanliu Date: Tue, 9 Jun 2020 17:13:15 +0800 Subject: [PATCH] new case: check cpu flag pku Signed-off-by: Nana Liu --- qemu/tests/cfg/x86_cpu_flags.cfg | 16 +++++++++++ qemu/tests/x86_cpu_flags.py | 46 ++++++++++++++++++++++++++++++++ qemu/tests/x86_cpu_model.py | 43 ++++++++++++++++++++--------- 3 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 qemu/tests/cfg/x86_cpu_flags.cfg create mode 100644 qemu/tests/x86_cpu_flags.py diff --git a/qemu/tests/cfg/x86_cpu_flags.cfg b/qemu/tests/cfg/x86_cpu_flags.cfg new file mode 100644 index 00000000..9e4b1c94 --- /dev/null +++ b/qemu/tests/cfg/x86_cpu_flags.cfg @@ -0,0 +1,16 @@ +- x86_cpu_flags: + type = x86_cpu_flags + cpu_model_flags += ",enforce,-mpx" + kill_vm_on_error = yes + start_vm = no + check_host_flags = yes + reboot_method = "shell" + only i386, x86_64 + check_flag_cmd = "lscpu | grep Flags | awk -F ':' '{print $2}'" + variants: + - intel: + vendor_id = 'intel' + variants: + - memory_protection_key: + cpu_model_flags += ",+pku,check" + flags = "pku ospke" diff --git a/qemu/tests/x86_cpu_flags.py b/qemu/tests/x86_cpu_flags.py new file mode 100644 index 00000000..f72ded59 --- /dev/null +++ b/qemu/tests/x86_cpu_flags.py @@ -0,0 +1,46 @@ +import logging + +from avocado.utils import cpu +from virttest import error_context, env_process +from qemu.tests.x86_cpu_model import check_flags + + +@error_context.context_aware +def run(test, params, env): + """ + Test cpu flags. + 1) Check if current flags are in the supported lists, if no, cancel test + 2) Otherwise, boot guest with the cpu flags + 3) Check cpu flags inside guest(only for linux guest) + 4) Reboot guest + + :param test: QEMU test object + :param params: Dictionary with the test parameters + :param env: Dictionary with test environment. + """ + vendor_id = params.get("vendor_id", "") + if vendor_id: + if vendor_id != cpu.get_vendor(): + test.cancel("Need host vendor %s to support this test case" % vendor_id) + + flags = params["flags"] + check_host_flags = params.get_boolean("check_host_flags") + if check_host_flags: + check_flags(params, flags, test) + + params["start_vm"] = "yes" + vm_name = params['main_vm'] + env_process.preprocess_vm(test, params, env, vm_name) + + vm = env.get_vm(vm_name) + error_context.context("Try to log into guest", logging.info) + session = vm.wait_for_login() + if params["os_type"] == "linux": + check_flags(params, flags, test, session) + + if params.get("reboot_method"): + error_context.context("Reboot guest '%s'." % vm.name, logging.info) + session = vm.reboot(session=session) + + vm.verify_kernel_crash() + session.close() diff --git a/qemu/tests/x86_cpu_model.py b/qemu/tests/x86_cpu_model.py index c90d1336..6578fabe 100644 --- a/qemu/tests/x86_cpu_model.py +++ b/qemu/tests/x86_cpu_model.py @@ -6,6 +6,35 @@ from avocado.utils import cpu, process from virttest import error_context, utils_misc, env_process +def check_flags(params, flags, test, session=None): + """ + Check cpu flags on host or guest.(only for Linux now) + :param params: Dictionary with the test parameters + :param flags: checked flags + :param test: QEMU test object + :param session: guest session + """ + cmd = params["check_flag_cmd"] + func = process.getoutput + if session: + func = session.cmd_output + out = func(cmd).split() + missing = [f for f in flags.split() if f not in out] + if session: + error_context.context("Check cpu flags inside guest", logging.info) + if missing: + test.fail("Flag %s not in guest" % missing) + no_flags = params.get("no_flags") + if no_flags: + err_flags = [f for f in no_flags.split() if f in out] + if err_flags: + test.fail("Flag %s should not be present in guest" % err_flags) + else: + error_context.context("Check cpu flags on host", logging.info) + if missing: + test.cancel("This host doesn't support flag %s" % missing) + + @error_context.context_aware def run(test, params, env): """ @@ -72,21 +101,11 @@ def run(test, params, env): test.fail("Guest cpu model is not right") if params["os_type"] == "linux": - error_context.context("Check cpu flags inside guest", logging.info) - cmd = params["check_flag_cmd"] - out = session.cmd_output(cmd).split() - missing = [f for f in flags.split() if f not in out] - if missing: - test.fail("Flag %s not in guest" % missing) - no_flags = params.get("no_flags") - if no_flags: - err_flags = [f for f in no_flags.split() if f in out] - if err_flags: - test.fail("Flag %s should not be present in guest" % err_flags) + check_flags(params, flags, test, session) if params.get("reboot_method"): error_context.context("Reboot guest '%s'." % vm.name, logging.info) - vm.reboot(session=session) + session = vm.reboot(session=session) vm.verify_kernel_crash() session.close() -- GitLab