summaryrefslogtreecommitdiffstats
path: root/scripts/cpu-x86-uarch-abi.py
blob: 08acc52a816fd6cb504ca74959f3a9f7c4994f43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python3
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# A script to generate a CSV file showing the x86_64 ABI
# compatibility levels for each CPU model.
#

from qemu import qmp
import sys

if len(sys.argv) != 1:
    print("syntax: %s QMP-SOCK\n\n" % __file__ +
          "Where QMP-SOCK points to a QEMU process such as\n\n" +
          " # qemu-system-x86_64 -qmp unix:/tmp/qmp,server,nowait " +
          "-display none -accel kvm", file=sys.stderr)
    sys.exit(1)

# Mandatory CPUID features for each microarch ABI level
levels = [
    [ # x86-64 baseline
        "cmov",
        "cx8",
        "fpu",
        "fxsr",
        "mmx",
        "syscall",
        "sse",
        "sse2",
    ],
    [ # x86-64-v2
        "cx16",
        "lahf-lm",
        "popcnt",
        "pni",
        "sse4.1",
        "sse4.2",
        "ssse3",
    ],
    [ # x86-64-v3
        "avx",
        "avx2",
        "bmi1",
        "bmi2",
        "f16c",
        "fma",
        "abm",
        "movbe",
    ],
    [ # x86-64-v4
        "avx512f",
        "avx512bw",
        "avx512cd",
        "avx512dq",
        "avx512vl",
    ],
]

# Assumes externally launched process such as
#
#   qemu-system-x86_64 -qmp unix:/tmp/qmp,server,nowait -display none -accel kvm
#
# Note different results will be obtained with TCG, as
# TCG masks out certain features otherwise present in
# the CPU model definitions, as does KVM.


sock = sys.argv[1]
cmd = sys.argv[2]
shell = qmp.QEMUMonitorProtocol(sock)
shell.connect()

models = shell.cmd("query-cpu-definitions")

# These QMP props don't correspond to CPUID fatures
# so ignore them
skip = [
    "family",
    "min-level",
    "min-xlevel",
    "vendor",
    "model",
    "model-id",
    "stepping",
]

names = []

for model in models["return"]:
    if "alias-of" in model:
        continue
    names.append(model["name"])

models = {}

for name in sorted(names):
    cpu = shell.cmd("query-cpu-model-expansion",
                     { "type": "static",
                       "model": { "name": name }})

    got = {}
    for (feature, present) in cpu["return"]["model"]["props"].items():
        if present and feature not in skip:
            got[feature] = True

    if name in ["host", "max", "base"]:
        continue

    models[name] = {
        # Dict of all present features in this CPU model
        "features": got,

        # Whether each x86-64 ABI level is satisfied
        "levels": [False, False, False, False],

        # Number of extra CPUID features compared to the x86-64 ABI level
        "distance":[-1, -1, -1, -1],

        # CPUID features present in model, but not in ABI level
        "delta":[[], [], [], []],

        # CPUID features in ABI level but not present in model
        "missing": [[], [], [], []],
    }


# Calculate whether the CPU models satisfy each ABI level
for name in models.keys():
    for level in range(len(levels)):
        got = set(models[name]["features"])
        want = set(levels[level])
        missing = want - got
        match = True
        if len(missing) > 0:
            match = False
        models[name]["levels"][level] = match
        models[name]["missing"][level] = missing

# Cache list of CPU models satisfying each ABI level
abi_models = [
    [],
    [],
    [],
    [],
]

for name in models.keys():
    for level in range(len(levels)):
        if models[name]["levels"][level]:
            abi_models[level].append(name)


for level in range(len(abi_models)):
    # Find the union of features in all CPU models satisfying this ABI
    allfeatures = {}
    for name in abi_models[level]:
        for feat in models[name]["features"]:
            allfeatures[feat] = True

    # Find the intersection of features in all CPU models satisfying this ABI
    commonfeatures = []
    for feat in allfeatures:
        present = True
        for name in models.keys():
            if not models[name]["levels"][level]:
                continue
            if feat not in models[name]["features"]:
                present = False
        if present:
            commonfeatures.append(feat)

    # Determine how many extra features are present compared to the lowest
    # common denominator
    for name in models.keys():
        if not models[name]["levels"][level]:
            continue

        delta = set(models[name]["features"].keys()) - set(commonfeatures)
        models[name]["distance"][level] = len(delta)
        models[name]["delta"][level] = delta

def print_uarch_abi_csv():
    print("# Automatically generated from '%s'" % __file__)
    print("Model,baseline,v2,v3,v4")
    for name in models.keys():
        print(name, end="")
        for level in range(len(levels)):
            if models[name]["levels"][level]:
                print(",✅", end="")
            else:
                print(",", end="")
        print()

print_uarch_abi_csv()