| 1 | /* |
| 2 | * Utility to read CPUIDs from x86 processors |
| 3 | * Copyright (c) 2013, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <ctype.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <stdint.h> |
| 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | struct cpuidcxx . . $ cpuid# { |
| 31 | uint32_tcxx . . $ uint32_t# eaxcxx . . $ cpuid#eax., ebxcxx . . $ cpuid#ebx., ecxcxx . . $ cpuid#ecx., edxcxx . . $ cpuid#edx.; |
| 32 | }; |
| 33 | |
| 34 | static int cpuidcxx . . $ cpuid(32548f96ea69e99b).(int cpu, uint32_tcxx . . $ uint32_t# leaf, uint32_tcxx . . $ uint32_t# subleaf, struct cpuidcxx . . $ cpuid# *data) |
| 35 | { |
| 36 | static int fd = -1; |
| 37 | static int last_cpu; |
| 38 | off_tcxx . . $ off_t# offset = leaf + ((off_tcxx . . $ off_t#) subleaf << 32); |
| 39 | |
| 40 | if (fd < 0 || last_cpu != cpu) { |
| 41 | char devstr[64]; |
| 42 | if (fd >= 0) |
| 43 | closecxx . . $ close(7864480464b09eea).(fd); |
| 44 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(devstr, sizeof devstr, "/dev/cpu/%d/cpuid", cpu); |
| 45 | fd = opencxx . . $ open(e156e3aa1e5c4ad1).(devstr, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!); |
| 46 | if (fd < 0) { |
| 47 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENXIOcxx . . $ `<external>/8987895939074593727/errno-base.h:10:9`!) { |
| 48 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "cpuid: No CPU %d\n", cpu); |
| 49 | exitcxx . . $ exit(3ca292c338df9bb3).(2); |
| 50 | } else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!) { |
| 51 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, |
| 52 | "cpuid: CPU %d doesn't support cpuid\n", |
| 53 | cpu); |
| 54 | exitcxx . . $ exit(3ca292c338df9bb3).(3); |
| 55 | } else { |
| 56 | perrorcxx . . $ perror(85c52e162fed56f9).("cpuid: open"); |
| 57 | exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 58 | } |
| 59 | } |
| 60 | last_cpu = cpu; |
| 61 | } |
| 62 | return preadcxx . . $ pread(b119fdc0c07358b4).(fd, data, sizeof(*data), offset) == sizeof(*data) ? 0 : -1; |
| 63 | } |
| 64 | |
| 65 | static char *make_stringcxx . . $ make_string(f109d7e0a0ca42b3).(uint32_tcxx . . $ uint32_t# val) |
| 66 | { |
| 67 | static char string[5] = "xxxx"; |
| 68 | int i, ch; |
| 69 | |
| 70 | for (i = 0; i < 4; i++) { |
| 71 | ch = val & 0xff; |
| 72 | string[i] = isprintcxx . . $ `<external>/17904084882347303253/ctype.h:195:10`!(ch) ? ch : '.'; |
| 73 | val >>= 8; |
| 74 | } |
| 75 | |
| 76 | return string; |
| 77 | } |
| 78 | |
| 79 | static void print_cpuid_levelcxx . . $ print_cpuid_level(c24e9442732259d5).(uint32_tcxx . . $ uint32_t# leaf, uint32_tcxx . . $ uint32_t# subleaf, |
| 80 | struct cpuidcxx . . $ cpuid# *lvl) |
| 81 | { |
| 82 | printfcxx . . $ printf(581fddd72a8db8fc).("%08x %08x: ", leaf, subleaf); |
| 83 | printfcxx . . $ printf(581fddd72a8db8fc).("%08x %s ", lvl->eaxcxx . . $ cpuid#eax., make_stringcxx . . $ make_string(f109d7e0a0ca42b3).(lvl->eaxcxx . . $ cpuid#eax.)); |
| 84 | printfcxx . . $ printf(581fddd72a8db8fc).("%08x %s ", lvl->ebxcxx . . $ cpuid#ebx., make_stringcxx . . $ make_string(f109d7e0a0ca42b3).(lvl->ebxcxx . . $ cpuid#ebx.)); |
| 85 | printfcxx . . $ printf(581fddd72a8db8fc).("%08x %s ", lvl->ecxcxx . . $ cpuid#ecx., make_stringcxx . . $ make_string(f109d7e0a0ca42b3).(lvl->ecxcxx . . $ cpuid#ecx.)); |
| 86 | printfcxx . . $ printf(581fddd72a8db8fc).("%08x %s\n", lvl->edxcxx . . $ cpuid#edx., make_stringcxx . . $ make_string(f109d7e0a0ca42b3).(lvl->edxcxx . . $ cpuid#edx.)); |
| 87 | } |
| 88 | |
| 89 | static void dump_cpuid_leafcxx . . $ dump_cpuid_leaf(c8dafc4a64bf4a1c).(int cpu, uint32_tcxx . . $ uint32_t# leaf) |
| 90 | { |
| 91 | struct cpuidcxx . . $ cpuid# lvl, lastlvl, lvl0; |
| 92 | uint32_tcxx . . $ uint32_t# subleaf; |
| 93 | |
| 94 | cpuidcxx . . $ cpuid(32548f96ea69e99b).(cpu, leaf, 0, &lvl0); |
| 95 | print_cpuid_levelcxx . . $ print_cpuid_level(c24e9442732259d5).(leaf, 0, &lvl0); |
| 96 | |
| 97 | /* |
| 98 | * There is no standard mechanism for enumerating the number of |
| 99 | * subleaves, this is a heuristic... |
| 100 | */ |
| 101 | lastlvl = lvl0; |
| 102 | |
| 103 | for (subleaf = 1; subleaf != 0; subleaf++) { |
| 104 | if (cpuidcxx . . $ cpuid(32548f96ea69e99b).(cpu, leaf, subleaf, &lvl)) |
| 105 | return; |
| 106 | |
| 107 | switch (leaf) { |
| 108 | case 4: |
| 109 | if ((lvl.eaxcxx . . $ cpuid#eax. & 0x1f) == 0 |
| 110 | || !memcmpcxx . . $ memcmp(36f949851efa3055).(&lvl, &lastlvl, sizeof lvl)) |
| 111 | return; |
| 112 | break; |
| 113 | |
| 114 | case 7: |
| 115 | if (subleaf >= lvl0.eaxcxx . . $ cpuid#eax.) |
| 116 | return; |
| 117 | break; |
| 118 | |
| 119 | case 0xb: |
| 120 | if ((lvl.ecxcxx . . $ cpuid#ecx. & ~0xff) == 0) |
| 121 | return; |
| 122 | |
| 123 | case 0xd: |
| 124 | if ((lvl.eaxcxx . . $ cpuid#eax. | lvl.ebxcxx . . $ cpuid#ebx. | lvl.ecxcxx . . $ cpuid#ecx. | lvl.edxcxx . . $ cpuid#edx.) == 0) |
| 125 | return; |
| 126 | |
| 127 | default: |
| 128 | /* Generic, anticipatory rules */ |
| 129 | /* Exclude ecx here for levels which return the initial ecx value */ |
| 130 | if ((lvl.eaxcxx . . $ cpuid#eax. | lvl.ebxcxx . . $ cpuid#ebx. | lvl.ecxcxx . . $ cpuid#ecx. | lvl.edxcxx . . $ cpuid#edx.) == 0) |
| 131 | return; |
| 132 | |
| 133 | if (!memcmpcxx . . $ memcmp(36f949851efa3055).(&lvl, &lvl0, sizeof lvl)) |
| 134 | return; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | print_cpuid_levelcxx . . $ print_cpuid_level(c24e9442732259d5).(leaf, subleaf, &lvl); |
| 139 | |
| 140 | lastlvl = lvl; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void dump_levelscxx . . $ dump_levels(c8dafc4a64bf4a1c).(int cpu, uint32_tcxx . . $ uint32_t# region) |
| 145 | { |
| 146 | static struct cpuidcxx . . $ cpuid# invalid_leaf; |
| 147 | struct cpuidcxx . . $ cpuid# max; |
| 148 | uint32_tcxx . . $ uint32_t# n; |
| 149 | |
| 150 | if (cpuidcxx . . $ cpuid(32548f96ea69e99b).(cpu, region, 0, &max)) |
| 151 | return; |
| 152 | |
| 153 | /* |
| 154 | * Intel processors may return the last group 0 CPUID leaf instead |
| 155 | * all zero for a not-present level |
| 156 | */ |
| 157 | if (region == 0) { |
| 158 | cpuidcxx . . $ cpuid(32548f96ea69e99b).(cpu, max.eaxcxx . . $ cpuid#eax. + 1, 0, &invalid_leaf); |
| 159 | } else { |
| 160 | if (!memcmpcxx . . $ memcmp(36f949851efa3055).(&max, &invalid_leaf, sizeof(struct cpuidcxx . . $ cpuid#))) |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if ((max.eaxcxx . . $ cpuid#eax. & 0xffff0000) == region) { |
| 165 | for (n = region; n <= max.eaxcxx . . $ cpuid#eax.; n++) { |
| 166 | dump_cpuid_leafcxx . . $ dump_cpuid_leaf(c8dafc4a64bf4a1c).(cpu, n); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void usagecxx . . $ usage(49f6e7a06ebc5aa8).(void) |
| 172 | { |
| 173 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Usage: cpuid [processor # (default 0)]\n"); |
| 174 | } |
| 175 | |
| 176 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 177 | { |
| 178 | int cpu = 0; |
| 179 | uint32_tcxx . . $ uint32_t# n; |
| 180 | char *endptr; |
| 181 | |
| 182 | if (argc > 2) { |
| 183 | usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 184 | exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 185 | } else if (argc == 2) { |
| 186 | cpu = strtoulcxx . . $ strtoul(31746e8d6ec49860).(argv[1], &endptr, 0); |
| 187 | if (*endptr || cpu > 5119) { |
| 188 | usagecxx . . $ usage(49f6e7a06ebc5aa8).(); |
| 189 | exitcxx . . $ exit(3ca292c338df9bb3).(127); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | printfcxx . . $ printf(581fddd72a8db8fc). |
| 194 | ("Leaf Subleaf EAX EBX ECX EDX \n"); |
| 195 | |
| 196 | for (n = 0; n <= 0xffff; n++) { |
| 197 | dump_levelscxx . . $ dump_levels(c8dafc4a64bf4a1c).(cpu, n << 16); |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |