cpu-cpuid.py 4.4 KB
Newer Older
1
#!/usr/bin/env python3
2

3
import os
4 5
import sys
import json
6
import xmltodict
7

8
def checkCPUIDFeature(cpuData, feature):
9 10
    eax_in = feature["eax_in"]
    ecx_in = feature["ecx_in"]
11 12 13 14 15
    eax = feature["eax"]
    ebx = feature["ebx"]
    ecx = feature["ecx"]
    edx = feature["edx"]

16 17 18 19
    if "cpuid" not in cpuData:
        return False

    cpuid = cpuData["cpuid"]
20
    if eax_in not in cpuid or ecx_in not in cpuid[eax_in]:
21
        return False
22

23
    leaf = cpuid[eax_in][ecx_in]
24 25 26 27
    return ((eax > 0 and leaf["eax"] & eax == eax) or
            (ebx > 0 and leaf["ebx"] & ebx == ebx) or
            (ecx > 0 and leaf["ecx"] & ecx == ecx) or
            (edx > 0 and leaf["edx"] & edx == edx))
28 29


30 31 32 33 34 35 36 37 38 39 40 41
def checkFeature(cpuData, feature):
    if feature["type"] == "cpuid":
        return checkCPUIDFeature(cpuData, feature)

    return False


def addCPUIDFeature(cpuData, feature):
    if "cpuid" not in cpuData:
        cpuData["cpuid"] = {}
    cpuid = cpuData["cpuid"]

42 43 44
    if feature["eax_in"] not in cpuid:
        cpuid[feature["eax_in"]] = {}
    leaf = cpuid[feature["eax_in"]]
45

46 47 48
    if feature["ecx_in"] not in leaf:
        leaf[feature["ecx_in"]] = {"eax": 0, "ebx": 0, "ecx": 0, "edx": 0}
    leaf = leaf[feature["ecx_in"]]
49

50 51 52 53
    for reg in ["eax", "ebx", "ecx", "edx"]:
        leaf[reg] |= feature[reg]


54 55 56 57 58
def addFeature(cpuData, feature):
    if feature["type"] == "cpuid":
        addCPUIDFeature(cpuData, feature)


59
def parseQemu(path, features):
60
    cpuData = {}
61
    with open(path, "r") as f:
62
        data, pos = json.JSONDecoder().raw_decode(f.read())
63

64
    for (prop, val) in data["return"]["model"]["props"].items():
65
        if val and prop in features:
66
            addFeature(cpuData, features[prop])
67

68
    return cpuData
69 70


71 72
def parseCPUData(path):
    cpuData = {}
73
    with open(path, "rb") as f:
74 75 76
        data = xmltodict.parse(f)

    for leaf in data["cpudata"]["cpuid"]:
77
        feature = {"type": "cpuid"}
78 79
        feature["eax_in"] = int(leaf["@eax_in"], 0)
        feature["ecx_in"] = int(leaf["@ecx_in"], 0)
80 81 82
        for reg in ["eax", "ebx", "ecx", "edx"]:
            feature[reg] = int(leaf["@" + reg], 0)

83
        addFeature(cpuData, feature)
84

85
    return cpuData
86 87


88
def parseMapFeature(data):
89
    cpuid = {"type": "cpuid"}
90 91
    for reg in ["eax_in", "ecx_in", "eax", "ebx", "ecx", "edx"]:
        attr = "@%s" % reg
92 93 94 95 96 97 98 99 100 101 102

        if attr in data:
            cpuid[reg] = int(data[attr], 0)
        else:
            cpuid[reg] = 0

    return cpuid


def parseMap():
    path = os.path.dirname(sys.argv[0])
103
    path = os.path.join(path, "..", "..", "src", "cpu_map", "x86_features.xml")
104
    with open(path, "rb") as f:
105 106 107
        data = xmltodict.parse(f)

    cpuMap = {}
108
    for feature in data["cpus"]["feature"]:
109
        if "cpuid" in feature:
110
            cpuMap[feature["@name"]] = parseMapFeature(feature["cpuid"])
111 112 113 114

    return cpuMap


115
def formatCPUData(cpuData, path, comment):
116
    print(path)
117 118 119
    with open(path, "w") as f:
        f.write("<!-- " + comment + " -->\n")
        f.write("<cpudata arch='x86'>\n")
120 121

        cpuid = cpuData["cpuid"]
122 123 124
        for eax_in in sorted(cpuid.keys()):
            for ecx_in in sorted(cpuid[eax_in].keys()):
                leaf = cpuid[eax_in][ecx_in]
125 126 127
                line = ("  <cpuid eax_in='0x%08x' ecx_in='0x%02x' "
                        "eax='0x%08x' ebx='0x%08x' "
                        "ecx='0x%08x' edx='0x%08x'/>\n")
128
                f.write(line % (
129
                        eax_in, ecx_in,
130 131 132 133
                        leaf["eax"], leaf["ebx"], leaf["ecx"], leaf["edx"]))
        f.write("</cpudata>\n")


134
def diff(cpuMap, path):
135 136
    base = path.replace(".json", "")
    jsonFile = path
137
    cpuDataFile = base + ".xml"
138 139 140
    enabledFile = base + "-enabled.xml"
    disabledFile = base + "-disabled.xml"

141
    cpuData = parseCPUData(cpuDataFile)
142
    qemu = parseQemu(jsonFile, cpuMap)
143

144 145
    enabled = {"cpuid": {}}
    disabled = {"cpuid": {}}
146
    for feature in cpuMap.values():
147 148 149 150
        if checkFeature(qemu, feature):
            addFeature(enabled, feature)
        elif checkFeature(cpuData, feature):
            addFeature(disabled, feature)
151

152 153
    formatCPUData(enabled, enabledFile, "Features enabled by QEMU")
    formatCPUData(disabled, disabledFile, "Features disabled by QEMU")
154 155


156
if len(sys.argv) < 3:
157
    print("Usage: %s diff json_file..." % sys.argv[0])
158 159 160 161 162
    sys.exit(1)

action = sys.argv[1]
args = sys.argv[2:]

163
if action == "diff":
164
    cpuMap = parseMap()
165
    for path in args:
166
        diff(cpuMap, path)
167
else:
168
    print("Unknown action: %s" % action)
169
    sys.exit(1)