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) mitigation sysfs status.
7
8import sys, os, re
9this_dir = os.path.dirname(os.path.realpath(__file__))
10sys.path.insert(0, this_dir + '/../../kselftest')
11import ksft
12
13from common import *
14
15bug = "indirect_target_selection"
16mitigation = get_sysfs(bug)
17
18ITS_MITIGATION_ALIGNED_THUNKS	= "Mitigation: Aligned branch/return thunks"
19ITS_MITIGATION_RETPOLINE_STUFF	= "Mitigation: Retpolines, Stuffing RSB"
20ITS_MITIGATION_VMEXIT_ONLY		= "Mitigation: Vulnerable, KVM: Not affected"
21ITS_MITIGATION_VULNERABLE       = "Vulnerable"
22
23def check_mitigation():
24    if mitigation == ITS_MITIGATION_ALIGNED_THUNKS:
25        if cmdline_has(f'{bug}=stuff') and sysfs_has("spectre_v2", "Retpolines"):
26            bug_check_fail(bug, ITS_MITIGATION_ALIGNED_THUNKS, ITS_MITIGATION_RETPOLINE_STUFF)
27            return
28        if cmdline_has(f'{bug}=vmexit') and cpuinfo_has('its_native_only'):
29            bug_check_fail(bug, ITS_MITIGATION_ALIGNED_THUNKS, ITS_MITIGATION_VMEXIT_ONLY)
30            return
31        bug_check_pass(bug, ITS_MITIGATION_ALIGNED_THUNKS)
32        return
33
34    if mitigation == ITS_MITIGATION_RETPOLINE_STUFF:
35        if cmdline_has(f'{bug}=stuff') and sysfs_has("spectre_v2", "Retpolines"):
36            bug_check_pass(bug, ITS_MITIGATION_RETPOLINE_STUFF)
37            return
38        if sysfs_has('retbleed', 'Stuffing'):
39            bug_check_pass(bug, ITS_MITIGATION_RETPOLINE_STUFF)
40            return
41        bug_check_fail(bug, ITS_MITIGATION_RETPOLINE_STUFF, ITS_MITIGATION_ALIGNED_THUNKS)
42
43    if mitigation == ITS_MITIGATION_VMEXIT_ONLY:
44        if cmdline_has(f'{bug}=vmexit') and cpuinfo_has('its_native_only'):
45            bug_check_pass(bug, ITS_MITIGATION_VMEXIT_ONLY)
46            return
47        bug_check_fail(bug, ITS_MITIGATION_VMEXIT_ONLY, ITS_MITIGATION_ALIGNED_THUNKS)
48
49    if mitigation == ITS_MITIGATION_VULNERABLE:
50        if sysfs_has("spectre_v2", "Vulnerable"):
51            bug_check_pass(bug, ITS_MITIGATION_VULNERABLE)
52        else:
53            bug_check_fail(bug, "Mitigation", ITS_MITIGATION_VULNERABLE)
54
55    bug_status_unknown(bug, mitigation)
56    return
57
58ksft.print_header()
59ksft.set_plan(1)
60ksft.print_msg(f'{bug}: {mitigation} ...')
61
62if not basic_checks_sufficient(bug, mitigation):
63    check_mitigation()
64
65ksft.finished()
66