|
| 1 | +# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# pylint: disable=too-many-statements |
| 5 | + |
| 6 | +""" |
| 7 | +Check CPU features in the host vs the guest. |
| 8 | +
|
| 9 | +This test can highlight differences between the host and what the guest sees. |
| 10 | +
|
| 11 | +No CPU templates as we are interested only on what is passed through to the guest by default. |
| 12 | +For that, check test_feat_parity.py |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from framework import utils |
| 18 | +from framework.properties import global_props |
| 19 | +from framework.utils_cpuid import CPU_FEATURES_CMD, CpuModel |
| 20 | + |
| 21 | +CPU_MODEL = global_props.cpu_codename |
| 22 | + |
| 23 | +INTEL_HOST_ONLY_FEATS = { |
| 24 | + "acpi", |
| 25 | + "aperfmperf", |
| 26 | + "arch_perfmon", |
| 27 | + "art", |
| 28 | + "bts", |
| 29 | + "cat_l3", |
| 30 | + "cdp_l3", |
| 31 | + "cqm", |
| 32 | + "cqm_llc", |
| 33 | + "cqm_mbm_local", |
| 34 | + "cqm_mbm_total", |
| 35 | + "cqm_occup_llc", |
| 36 | + "dca", |
| 37 | + "ds_cpl", |
| 38 | + "dtes64", |
| 39 | + "dtherm", |
| 40 | + "dts", |
| 41 | + "epb", |
| 42 | + "ept", |
| 43 | + "ept_ad", |
| 44 | + "est", |
| 45 | + "flexpriority", |
| 46 | + "flush_l1d", |
| 47 | + "hwp", |
| 48 | + "hwp_act_window", |
| 49 | + "hwp_epp", |
| 50 | + "hwp_pkg_req", |
| 51 | + "ida", |
| 52 | + "intel_ppin", |
| 53 | + "intel_pt", |
| 54 | + "mba", |
| 55 | + "monitor", |
| 56 | + "pbe", |
| 57 | + "pdcm", |
| 58 | + "pebs", |
| 59 | + "pln", |
| 60 | + "pts", |
| 61 | + "rdt_a", |
| 62 | + "sdbg", |
| 63 | + "smx", |
| 64 | + "tm", |
| 65 | + "tm2", |
| 66 | + "tpr_shadow", |
| 67 | + "vmx", |
| 68 | + "vnmi", |
| 69 | + "vpid", |
| 70 | + "xtpr", |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +def test_host_vs_guest_cpu_features(uvm_nano): |
| 75 | + """Check CPU features host vs guest""" |
| 76 | + |
| 77 | + vm = uvm_nano |
| 78 | + vm.add_net_iface() |
| 79 | + vm.start() |
| 80 | + host_feats = set(utils.check_output(CPU_FEATURES_CMD).stdout.split()) |
| 81 | + guest_feats = set(vm.ssh.check_output(CPU_FEATURES_CMD).stdout.split()) |
| 82 | + |
| 83 | + match CPU_MODEL: |
| 84 | + case CpuModel.AMD_MILAN: |
| 85 | + host_guest_diff_5_10 = { |
| 86 | + "amd_ppin", |
| 87 | + "aperfmperf", |
| 88 | + "bpext", |
| 89 | + "cat_l3", |
| 90 | + "cdp_l3", |
| 91 | + "cpb", |
| 92 | + "cqm", |
| 93 | + "cqm_llc", |
| 94 | + "cqm_mbm_local", |
| 95 | + "cqm_mbm_total", |
| 96 | + "cqm_occup_llc", |
| 97 | + "decodeassists", |
| 98 | + "extapic", |
| 99 | + "extd_apicid", |
| 100 | + "flushbyasid", |
| 101 | + "hw_pstate", |
| 102 | + "ibs", |
| 103 | + "irperf", |
| 104 | + "lbrv", |
| 105 | + "mba", |
| 106 | + "monitor", |
| 107 | + "mwaitx", |
| 108 | + "overflow_recov", |
| 109 | + "pausefilter", |
| 110 | + "perfctr_llc", |
| 111 | + "perfctr_nb", |
| 112 | + "pfthreshold", |
| 113 | + "rdpru", |
| 114 | + "rdt_a", |
| 115 | + "sev", |
| 116 | + "sev_es", |
| 117 | + "skinit", |
| 118 | + "smca", |
| 119 | + "sme", |
| 120 | + "succor", |
| 121 | + "svm_lock", |
| 122 | + "tce", |
| 123 | + "tsc_scale", |
| 124 | + "v_vmsave_vmload", |
| 125 | + "vgif", |
| 126 | + "vmcb_clean", |
| 127 | + "wdt", |
| 128 | + } |
| 129 | + |
| 130 | + host_guest_diff_6_1 = host_guest_diff_5_10 - { |
| 131 | + "lbrv", |
| 132 | + "pausefilter", |
| 133 | + "pfthreshold", |
| 134 | + "sme", |
| 135 | + "tsc_scale", |
| 136 | + "v_vmsave_vmload", |
| 137 | + "vgif", |
| 138 | + "vmcb_clean", |
| 139 | + } | {"brs", "rapl", "v_spec_ctrl"} |
| 140 | + |
| 141 | + if global_props.host_linux_version_tpl < (6, 1): |
| 142 | + assert host_feats - guest_feats == host_guest_diff_5_10 |
| 143 | + else: |
| 144 | + assert host_feats - guest_feats == host_guest_diff_6_1 |
| 145 | + |
| 146 | + assert guest_feats - host_feats == { |
| 147 | + "hypervisor", |
| 148 | + "tsc_adjust", |
| 149 | + "tsc_deadline_timer", |
| 150 | + "tsc_known_freq", |
| 151 | + } |
| 152 | + |
| 153 | + case CpuModel.INTEL_SKYLAKE: |
| 154 | + assert host_feats - guest_feats == INTEL_HOST_ONLY_FEATS |
| 155 | + assert guest_feats - host_feats == { |
| 156 | + "hypervisor", |
| 157 | + "tsc_known_freq", |
| 158 | + "umip", |
| 159 | + } |
| 160 | + |
| 161 | + case CpuModel.INTEL_CASCADELAKE: |
| 162 | + expected_host_minus_guest = INTEL_HOST_ONLY_FEATS |
| 163 | + expected_guest_minus_host = { |
| 164 | + "hypervisor", |
| 165 | + "tsc_known_freq", |
| 166 | + "umip", |
| 167 | + } |
| 168 | + |
| 169 | + # Linux kernel v6.4+ passes through the CPUID bit for "flush_l1d" to guests. |
| 170 | + # https://github.com/torvalds/linux/commit/45cf86f26148e549c5ba4a8ab32a390e4bde216e |
| 171 | + # |
| 172 | + # Our test ubuntu host kernel is v6.8 and has the commit. |
| 173 | + if global_props.host_linux_version_tpl >= (6, 4): |
| 174 | + expected_host_minus_guest -= {"flush_l1d"} |
| 175 | + |
| 176 | + # Linux kernel v6.6+ drops the "invpcid_single" synthetic feature bit. |
| 177 | + # https://github.com/torvalds/linux/commit/54e3d9434ef61b97fd3263c141b928dc5635e50d |
| 178 | + # |
| 179 | + # Our test ubuntu host kernel is v6.8 and has the commit. |
| 180 | + host_has_invpcid_single = global_props.host_linux_version_tpl < (6, 6) |
| 181 | + guest_has_invpcid_single = vm.guest_kernel_version < (6, 6) |
| 182 | + if host_has_invpcid_single and not guest_has_invpcid_single: |
| 183 | + expected_host_minus_guest |= {"invpcid_single"} |
| 184 | + if not host_has_invpcid_single and guest_has_invpcid_single: |
| 185 | + expected_guest_minus_host |= {"invpcid_single"} |
| 186 | + |
| 187 | + assert host_feats - guest_feats == expected_host_minus_guest |
| 188 | + assert guest_feats - host_feats == expected_guest_minus_host |
| 189 | + |
| 190 | + case CpuModel.INTEL_ICELAKE: |
| 191 | + host_guest_diff_5_10 = INTEL_HOST_ONLY_FEATS - {"cdp_l3"} | { |
| 192 | + "pconfig", |
| 193 | + "tme", |
| 194 | + "split_lock_detect", |
| 195 | + } |
| 196 | + host_guest_diff_6_1 = host_guest_diff_5_10 - { |
| 197 | + "bts", |
| 198 | + "dtes64", |
| 199 | + "dts", |
| 200 | + "pebs", |
| 201 | + } |
| 202 | + |
| 203 | + if global_props.host_linux_version_tpl < (6, 1): |
| 204 | + assert host_feats - guest_feats == host_guest_diff_5_10 |
| 205 | + else: |
| 206 | + assert host_feats - guest_feats == host_guest_diff_6_1 |
| 207 | + |
| 208 | + assert guest_feats - host_feats == { |
| 209 | + "hypervisor", |
| 210 | + "tsc_known_freq", |
| 211 | + } |
| 212 | + |
| 213 | + case CpuModel.ARM_NEOVERSE_N1: |
| 214 | + expected_guest_minus_host = set() |
| 215 | + expected_host_minus_guest = set() |
| 216 | + |
| 217 | + # Upstream kernel v6.11+ hides "ssbs" from "lscpu" on Neoverse-N1 and Neoverse-V1 since |
| 218 | + # they have an errata whereby an MSR to the SSBS special-purpose register does not |
| 219 | + # affect subsequent speculative instructions, permitting speculative store bypassing for |
| 220 | + # a window of time. |
| 221 | + # https://github.com/torvalds/linux/commit/adeec61a4723fd3e39da68db4cc4d924e6d7f641 |
| 222 | + # |
| 223 | + # While Amazon Linux kernels (v5.10 and v6.1) backported the above commit, our test |
| 224 | + # ubuntu kernel (v6.8) and our guest kernels (v5.10 and v6.1) don't pick it. |
| 225 | + host_has_ssbs = global_props.host_os not in { |
| 226 | + "amzn2", |
| 227 | + "amzn2023", |
| 228 | + } and global_props.host_linux_version_tpl < (6, 11) |
| 229 | + guest_has_ssbs = vm.guest_kernel_version < (6, 11) |
| 230 | + |
| 231 | + if host_has_ssbs and not guest_has_ssbs: |
| 232 | + expected_host_minus_guest |= {"ssbs"} |
| 233 | + if not host_has_ssbs and guest_has_ssbs: |
| 234 | + expected_guest_minus_host |= {"ssbs"} |
| 235 | + |
| 236 | + assert host_feats - guest_feats == expected_host_minus_guest |
| 237 | + assert guest_feats - host_feats == expected_guest_minus_host |
| 238 | + |
| 239 | + case CpuModel.ARM_NEOVERSE_V1: |
| 240 | + expected_guest_minus_host = set() |
| 241 | + # KVM does not enable PAC or SVE features by default |
| 242 | + # and Firecracker does not enable them either. |
| 243 | + expected_host_minus_guest = {"paca", "pacg", "sve", "svebf16", "svei8mm"} |
| 244 | + |
| 245 | + # Upstream kernel v6.11+ hides "ssbs" from "lscpu" on Neoverse-N1 and Neoverse-V1 since |
| 246 | + # they have an errata whereby an MSR to the SSBS special-purpose register does not |
| 247 | + # affect subsequent speculative instructions, permitting speculative store bypassing for |
| 248 | + # a window of time. |
| 249 | + # https://github.com/torvalds/linux/commit/adeec61a4723fd3e39da68db4cc4d924e6d7f641 |
| 250 | + # |
| 251 | + # While Amazon Linux kernels (v5.10 and v6.1) backported the above commit, our test |
| 252 | + # ubuntu kernel (v6.8) and our guest kernels (v5.10 and v6.1) don't pick it. |
| 253 | + host_has_ssbs = global_props.host_os not in { |
| 254 | + "amzn2", |
| 255 | + "amzn2023", |
| 256 | + } and global_props.host_linux_version_tpl < (6, 11) |
| 257 | + guest_has_ssbs = vm.guest_kernel_version < (6, 11) |
| 258 | + |
| 259 | + if host_has_ssbs and not guest_has_ssbs: |
| 260 | + expected_host_minus_guest |= {"ssbs"} |
| 261 | + if not host_has_ssbs and guest_has_ssbs: |
| 262 | + expected_guest_minus_host |= {"ssbs"} |
| 263 | + |
| 264 | + assert host_feats - guest_feats == expected_host_minus_guest |
| 265 | + assert guest_feats - host_feats == expected_guest_minus_host |
| 266 | + |
| 267 | + case _: |
| 268 | + # only fail if running in CI |
| 269 | + if os.environ.get("BUILDKITE") is not None: |
| 270 | + assert ( |
| 271 | + guest_feats == host_feats |
| 272 | + ), f"Cpu model {CPU_MODEL} is not supported" |
0 commit comments