its_permutations.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright (c) 2025 Intel Corporation
  5. #
  6. # Test for indirect target selection (ITS) cmdline permutations with other bugs
  7. # like spectre_v2 and retbleed.
  8. import os, sys, subprocess, itertools, re, shutil
  9. test_dir = os.path.dirname(os.path.realpath(__file__))
  10. sys.path.insert(0, test_dir + '/../../kselftest')
  11. import ksft
  12. import common as c
  13. bug = "indirect_target_selection"
  14. mitigation = c.get_sysfs(bug)
  15. if not mitigation or "Not affected" in mitigation:
  16. ksft.test_result_skip("Skipping its_permutations.py: not applicable")
  17. ksft.finished()
  18. if shutil.which('vng') is None:
  19. ksft.test_result_skip("Skipping its_permutations.py: virtme-ng ('vng') not found in PATH.")
  20. ksft.finished()
  21. TEST = f"{test_dir}/its_sysfs.py"
  22. default_kparam = ['clearcpuid=hypervisor', 'panic=5', 'panic_on_warn=1', 'oops=panic', 'nmi_watchdog=1', 'hung_task_panic=1']
  23. DEBUG = " -v "
  24. # Install dependencies
  25. # https://github.com/arighi/virtme-ng
  26. # apt install virtme-ng
  27. BOOT_CMD = f"vng --run {test_dir}/../../../../../arch/x86/boot/bzImage "
  28. #BOOT_CMD += DEBUG
  29. bug = "indirect_target_selection"
  30. input_options = {
  31. 'indirect_target_selection' : ['off', 'on', 'stuff', 'vmexit'],
  32. 'retbleed' : ['off', 'stuff', 'auto'],
  33. 'spectre_v2' : ['off', 'on', 'eibrs', 'retpoline', 'ibrs', 'eibrs,retpoline'],
  34. }
  35. def pretty_print(output):
  36. OKBLUE = '\033[94m'
  37. OKGREEN = '\033[92m'
  38. WARNING = '\033[93m'
  39. FAIL = '\033[91m'
  40. ENDC = '\033[0m'
  41. BOLD = '\033[1m'
  42. # Define patterns and their corresponding colors
  43. patterns = {
  44. r"^ok \d+": OKGREEN,
  45. r"^not ok \d+": FAIL,
  46. r"^# Testing .*": OKBLUE,
  47. r"^# Found: .*": WARNING,
  48. r"^# Totals: .*": BOLD,
  49. r"pass:([1-9]\d*)": OKGREEN,
  50. r"fail:([1-9]\d*)": FAIL,
  51. r"skip:([1-9]\d*)": WARNING,
  52. }
  53. # Apply colors based on patterns
  54. for pattern, color in patterns.items():
  55. output = re.sub(pattern, lambda match: f"{color}{match.group(0)}{ENDC}", output, flags=re.MULTILINE)
  56. print(output)
  57. combinations = list(itertools.product(*input_options.values()))
  58. ksft.print_header()
  59. ksft.set_plan(len(combinations))
  60. logs = ""
  61. for combination in combinations:
  62. append = ""
  63. log = ""
  64. for p in default_kparam:
  65. append += f' --append={p}'
  66. command = BOOT_CMD + append
  67. test_params = ""
  68. for i, key in enumerate(input_options.keys()):
  69. param = f'{key}={combination[i]}'
  70. test_params += f' {param}'
  71. command += f" --append={param}"
  72. command += f" -- {TEST}"
  73. test_name = f"{bug} {test_params}"
  74. pretty_print(f'# Testing {test_name}')
  75. t = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  76. t.wait()
  77. output, _ = t.communicate()
  78. if t.returncode == 0:
  79. ksft.test_result_pass(test_name)
  80. else:
  81. ksft.test_result_fail(test_name)
  82. output = output.decode()
  83. log += f" {output}"
  84. pretty_print(log)
  85. logs += output + "\n"
  86. # Optionally use tappy to parse the output
  87. # apt install python3-tappy
  88. with open("logs.txt", "w") as f:
  89. f.write(logs)
  90. ksft.finished()