Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i386 CPUID helper functions |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <inttypes.h> |
| 23 | |
| 24 | #include "cpu.h" |
| 25 | #include "kvm.h" |
| 26 | |
| 27 | #include "qemu-option.h" |
| 28 | #include "qemu-config.h" |
| 29 | |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 30 | #include "qapi/qapi-visit-core.h" |
| 31 | |
Vadim Rozenfeld | 28f52cc | 2011-12-18 22:48:13 +0200 | [diff] [blame] | 32 | #include "hyperv.h" |
| 33 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 34 | /* feature flags taken from "Intel Processor Identification and the CPUID |
| 35 | * Instruction" and AMD's "CPUID Specification". In cases of disagreement |
| 36 | * between feature naming conventions, aliases may be added. |
| 37 | */ |
| 38 | static const char *feature_name[] = { |
| 39 | "fpu", "vme", "de", "pse", |
| 40 | "tsc", "msr", "pae", "mce", |
| 41 | "cx8", "apic", NULL, "sep", |
| 42 | "mtrr", "pge", "mca", "cmov", |
| 43 | "pat", "pse36", "pn" /* Intel psn */, "clflush" /* Intel clfsh */, |
| 44 | NULL, "ds" /* Intel dts */, "acpi", "mmx", |
| 45 | "fxsr", "sse", "sse2", "ss", |
| 46 | "ht" /* Intel htt */, "tm", "ia64", "pbe", |
| 47 | }; |
| 48 | static const char *ext_feature_name[] = { |
Eduardo Habkost | f370be3 | 2012-02-17 14:41:20 -0200 | [diff] [blame] | 49 | "pni|sse3" /* Intel,AMD sse3 */, "pclmulqdq|pclmuldq", "dtes64", "monitor", |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 50 | "ds_cpl", "vmx", "smx", "est", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 51 | "tm2", "ssse3", "cid", NULL, |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 52 | "fma", "cx16", "xtpr", "pdcm", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 53 | NULL, NULL, "dca", "sse4.1|sse4_1", |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 54 | "sse4.2|sse4_2", "x2apic", "movbe", "popcnt", |
Eduardo Habkost | eaf3f09 | 2012-03-06 15:11:30 -0300 | [diff] [blame] | 55 | "tsc-deadline", "aes", "xsave", "osxsave", |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 56 | "avx", NULL, NULL, "hypervisor", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 57 | }; |
| 58 | static const char *ext2_feature_name[] = { |
| 59 | "fpu", "vme", "de", "pse", |
| 60 | "tsc", "msr", "pae", "mce", |
| 61 | "cx8" /* AMD CMPXCHG8B */, "apic", NULL, "syscall", |
| 62 | "mtrr", "pge", "mca", "cmov", |
| 63 | "pat", "pse36", NULL, NULL /* Linux mp */, |
Eduardo Habkost | 3ac8ebf | 2012-02-17 14:41:21 -0200 | [diff] [blame] | 64 | "nx|xd", NULL, "mmxext", "mmx", |
Eduardo Habkost | f370be3 | 2012-02-17 14:41:20 -0200 | [diff] [blame] | 65 | "fxsr", "fxsr_opt|ffxsr", "pdpe1gb" /* AMD Page1GB */, "rdtscp", |
Eduardo Habkost | 3ac8ebf | 2012-02-17 14:41:21 -0200 | [diff] [blame] | 66 | NULL, "lm|i64", "3dnowext", "3dnow", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 67 | }; |
| 68 | static const char *ext3_feature_name[] = { |
| 69 | "lahf_lm" /* AMD LahfSahf */, "cmp_legacy", "svm", "extapic" /* AMD ExtApicSpace */, |
| 70 | "cr8legacy" /* AMD AltMovCr8 */, "abm", "sse4a", "misalignsse", |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 71 | "3dnowprefetch", "osvw", "ibs", "xop", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 72 | "skinit", "wdt", NULL, NULL, |
Andre Przywara | e117f77 | 2010-03-11 14:38:59 +0100 | [diff] [blame] | 73 | "fma4", NULL, "cvt16", "nodeid_msr", |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 74 | NULL, NULL, NULL, NULL, |
| 75 | NULL, NULL, NULL, NULL, |
| 76 | NULL, NULL, NULL, NULL, |
| 77 | }; |
| 78 | |
| 79 | static const char *kvm_feature_name[] = { |
Glauber Costa | 642258c | 2011-03-17 19:42:06 -0300 | [diff] [blame] | 80 | "kvmclock", "kvm_nopiodelay", "kvm_mmu", "kvmclock", "kvm_asyncpf", NULL, NULL, NULL, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 81 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 82 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 83 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 84 | }; |
| 85 | |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 86 | static const char *svm_feature_name[] = { |
| 87 | "npt", "lbrv", "svm_lock", "nrip_save", |
| 88 | "tsc_scale", "vmcb_clean", "flushbyasid", "decodeassists", |
| 89 | NULL, NULL, "pause_filter", NULL, |
| 90 | "pfthreshold", NULL, NULL, NULL, |
| 91 | NULL, NULL, NULL, NULL, |
| 92 | NULL, NULL, NULL, NULL, |
| 93 | NULL, NULL, NULL, NULL, |
| 94 | NULL, NULL, NULL, NULL, |
| 95 | }; |
| 96 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 97 | /* collects per-function cpuid data |
| 98 | */ |
| 99 | typedef struct model_features_t { |
| 100 | uint32_t *guest_feat; |
| 101 | uint32_t *host_feat; |
| 102 | uint32_t check_feat; |
| 103 | const char **flag_names; |
| 104 | uint32_t cpuid; |
| 105 | } model_features_t; |
| 106 | |
| 107 | int check_cpuid = 0; |
| 108 | int enforce_cpuid = 0; |
| 109 | |
Jan Kiszka | bb44e0d | 2011-01-21 21:48:07 +0100 | [diff] [blame] | 110 | void host_cpuid(uint32_t function, uint32_t count, |
| 111 | uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 112 | { |
| 113 | #if defined(CONFIG_KVM) |
Anthony Liguori | a1fd24a | 2011-11-27 11:13:01 -0600 | [diff] [blame] | 114 | uint32_t vec[4]; |
| 115 | |
| 116 | #ifdef __x86_64__ |
| 117 | asm volatile("cpuid" |
| 118 | : "=a"(vec[0]), "=b"(vec[1]), |
| 119 | "=c"(vec[2]), "=d"(vec[3]) |
| 120 | : "0"(function), "c"(count) : "cc"); |
| 121 | #else |
| 122 | asm volatile("pusha \n\t" |
| 123 | "cpuid \n\t" |
| 124 | "mov %%eax, 0(%2) \n\t" |
| 125 | "mov %%ebx, 4(%2) \n\t" |
| 126 | "mov %%ecx, 8(%2) \n\t" |
| 127 | "mov %%edx, 12(%2) \n\t" |
| 128 | "popa" |
| 129 | : : "a"(function), "c"(count), "S"(vec) |
| 130 | : "memory", "cc"); |
| 131 | #endif |
| 132 | |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 133 | if (eax) |
Anthony Liguori | a1fd24a | 2011-11-27 11:13:01 -0600 | [diff] [blame] | 134 | *eax = vec[0]; |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 135 | if (ebx) |
Anthony Liguori | a1fd24a | 2011-11-27 11:13:01 -0600 | [diff] [blame] | 136 | *ebx = vec[1]; |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 137 | if (ecx) |
Anthony Liguori | a1fd24a | 2011-11-27 11:13:01 -0600 | [diff] [blame] | 138 | *ecx = vec[2]; |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 139 | if (edx) |
Anthony Liguori | a1fd24a | 2011-11-27 11:13:01 -0600 | [diff] [blame] | 140 | *edx = vec[3]; |
Andre Przywara | bdde476 | 2010-03-11 14:38:58 +0100 | [diff] [blame] | 141 | #endif |
| 142 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 143 | |
| 144 | #define iswhite(c) ((c) && ((c) <= ' ' || '~' < (c))) |
| 145 | |
| 146 | /* general substring compare of *[s1..e1) and *[s2..e2). sx is start of |
| 147 | * a substring. ex if !NULL points to the first char after a substring, |
| 148 | * otherwise the string is assumed to sized by a terminating nul. |
| 149 | * Return lexical ordering of *s1:*s2. |
| 150 | */ |
| 151 | static int sstrcmp(const char *s1, const char *e1, const char *s2, |
| 152 | const char *e2) |
| 153 | { |
| 154 | for (;;) { |
| 155 | if (!*s1 || !*s2 || *s1 != *s2) |
| 156 | return (*s1 - *s2); |
| 157 | ++s1, ++s2; |
| 158 | if (s1 == e1 && s2 == e2) |
| 159 | return (0); |
| 160 | else if (s1 == e1) |
| 161 | return (*s2); |
| 162 | else if (s2 == e2) |
| 163 | return (*s1); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* compare *[s..e) to *altstr. *altstr may be a simple string or multiple |
| 168 | * '|' delimited (possibly empty) strings in which case search for a match |
| 169 | * within the alternatives proceeds left to right. Return 0 for success, |
| 170 | * non-zero otherwise. |
| 171 | */ |
| 172 | static int altcmp(const char *s, const char *e, const char *altstr) |
| 173 | { |
| 174 | const char *p, *q; |
| 175 | |
| 176 | for (q = p = altstr; ; ) { |
| 177 | while (*p && *p != '|') |
| 178 | ++p; |
| 179 | if ((q == p && !*s) || (q != p && !sstrcmp(s, e, q, p))) |
| 180 | return (0); |
| 181 | if (!*p) |
| 182 | return (1); |
| 183 | else |
| 184 | q = ++p; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* search featureset for flag *[s..e), if found set corresponding bit in |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 189 | * *pval and return true, otherwise return false |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 190 | */ |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 191 | static bool lookup_feature(uint32_t *pval, const char *s, const char *e, |
| 192 | const char **featureset) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 193 | { |
| 194 | uint32_t mask; |
| 195 | const char **ppc; |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 196 | bool found = false; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 197 | |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 198 | for (mask = 1, ppc = featureset; mask; mask <<= 1, ++ppc) { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 199 | if (*ppc && !altcmp(s, e, *ppc)) { |
| 200 | *pval |= mask; |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 201 | found = true; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 202 | } |
Jan Kiszka | e41e0fc | 2011-04-19 13:06:06 +0200 | [diff] [blame] | 203 | } |
| 204 | return found; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features, |
| 208 | uint32_t *ext_features, |
| 209 | uint32_t *ext2_features, |
| 210 | uint32_t *ext3_features, |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 211 | uint32_t *kvm_features, |
| 212 | uint32_t *svm_features) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 213 | { |
| 214 | if (!lookup_feature(features, flagname, NULL, feature_name) && |
| 215 | !lookup_feature(ext_features, flagname, NULL, ext_feature_name) && |
| 216 | !lookup_feature(ext2_features, flagname, NULL, ext2_feature_name) && |
| 217 | !lookup_feature(ext3_features, flagname, NULL, ext3_feature_name) && |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 218 | !lookup_feature(kvm_features, flagname, NULL, kvm_feature_name) && |
| 219 | !lookup_feature(svm_features, flagname, NULL, svm_feature_name)) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 220 | fprintf(stderr, "CPU feature %s not found\n", flagname); |
| 221 | } |
| 222 | |
| 223 | typedef struct x86_def_t { |
| 224 | struct x86_def_t *next; |
| 225 | const char *name; |
| 226 | uint32_t level; |
| 227 | uint32_t vendor1, vendor2, vendor3; |
| 228 | int family; |
| 229 | int model; |
| 230 | int stepping; |
Joerg Roedel | b862d1f | 2011-07-07 16:13:12 +0200 | [diff] [blame] | 231 | int tsc_khz; |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 232 | uint32_t features, ext_features, ext2_features, ext3_features; |
| 233 | uint32_t kvm_features, svm_features; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 234 | uint32_t xlevel; |
| 235 | char model_id[48]; |
| 236 | int vendor_override; |
| 237 | uint32_t flags; |
brillywu@viatech.com.cn | b3baa15 | 2011-06-01 09:59:52 +0800 | [diff] [blame] | 238 | /* Store the results of Centaur's CPUID instructions */ |
| 239 | uint32_t ext4_features; |
| 240 | uint32_t xlevel2; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 241 | } x86_def_t; |
| 242 | |
| 243 | #define I486_FEATURES (CPUID_FP87 | CPUID_VME | CPUID_PSE) |
| 244 | #define PENTIUM_FEATURES (I486_FEATURES | CPUID_DE | CPUID_TSC | \ |
| 245 | CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_MMX | CPUID_APIC) |
| 246 | #define PENTIUM2_FEATURES (PENTIUM_FEATURES | CPUID_PAE | CPUID_SEP | \ |
| 247 | CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \ |
| 248 | CPUID_PSE36 | CPUID_FXSR) |
| 249 | #define PENTIUM3_FEATURES (PENTIUM2_FEATURES | CPUID_SSE) |
| 250 | #define PPRO_FEATURES (CPUID_FP87 | CPUID_DE | CPUID_PSE | CPUID_TSC | \ |
| 251 | CPUID_MSR | CPUID_MCE | CPUID_CX8 | CPUID_PGE | CPUID_CMOV | \ |
| 252 | CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \ |
| 253 | CPUID_PAE | CPUID_SEP | CPUID_APIC) |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 254 | #define EXT2_FEATURE_MASK 0x0183F3FF |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 255 | |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 256 | #define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \ |
| 257 | CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \ |
| 258 | CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \ |
| 259 | CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \ |
| 260 | CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS) |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 261 | /* partly implemented: |
| 262 | CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64) |
| 263 | CPUID_PSE36 (needed for Solaris) */ |
| 264 | /* missing: |
| 265 | CPUID_VME, CPUID_DTS, CPUID_SS, CPUID_HT, CPUID_TM, CPUID_PBE */ |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 266 | #define TCG_EXT_FEATURES (CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | \ |
Andi Kleen | 8713f8f | 2010-06-26 22:54:21 +0200 | [diff] [blame] | 267 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT | \ |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 268 | CPUID_EXT_HYPERVISOR) |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 269 | /* missing: |
| 270 | CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_EST, |
Andi Kleen | 8713f8f | 2010-06-26 22:54:21 +0200 | [diff] [blame] | 271 | CPUID_EXT_TM2, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_XSAVE */ |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 272 | #define TCG_EXT2_FEATURES ((TCG_FEATURES & EXT2_FEATURE_MASK) | \ |
| 273 | CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \ |
| 274 | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT) |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 275 | /* missing: |
| 276 | CPUID_EXT2_PDPE1GB */ |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 277 | #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \ |
| 278 | CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A) |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 279 | #define TCG_SVM_FEATURES 0 |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 280 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 281 | /* maintains list of cpu model definitions |
| 282 | */ |
| 283 | static x86_def_t *x86_defs = {NULL}; |
| 284 | |
| 285 | /* built-in cpu model definitions (deprecated) |
| 286 | */ |
| 287 | static x86_def_t builtin_x86_defs[] = { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 288 | { |
| 289 | .name = "qemu64", |
| 290 | .level = 4, |
| 291 | .vendor1 = CPUID_VENDOR_AMD_1, |
| 292 | .vendor2 = CPUID_VENDOR_AMD_2, |
| 293 | .vendor3 = CPUID_VENDOR_AMD_3, |
| 294 | .family = 6, |
| 295 | .model = 2, |
| 296 | .stepping = 3, |
| 297 | .features = PPRO_FEATURES | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 298 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 299 | CPUID_PSE36, |
| 300 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16 | CPUID_EXT_POPCNT, |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 301 | .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 302 | CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX, |
| 303 | .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | |
| 304 | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A, |
| 305 | .xlevel = 0x8000000A, |
| 306 | .model_id = "QEMU Virtual CPU version " QEMU_VERSION, |
| 307 | }, |
| 308 | { |
| 309 | .name = "phenom", |
| 310 | .level = 5, |
| 311 | .vendor1 = CPUID_VENDOR_AMD_1, |
| 312 | .vendor2 = CPUID_VENDOR_AMD_2, |
| 313 | .vendor3 = CPUID_VENDOR_AMD_3, |
| 314 | .family = 16, |
| 315 | .model = 2, |
| 316 | .stepping = 3, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 317 | .features = PPRO_FEATURES | |
| 318 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 319 | CPUID_PSE36 | CPUID_VME | CPUID_HT, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 320 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_CX16 | |
| 321 | CPUID_EXT_POPCNT, |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 322 | .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 323 | CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX | |
| 324 | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_MMXEXT | |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 325 | CPUID_EXT2_FFXSR | CPUID_EXT2_PDPE1GB | CPUID_EXT2_RDTSCP, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 326 | /* Missing: CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC, |
| 327 | CPUID_EXT3_CR8LEG, |
| 328 | CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH, |
| 329 | CPUID_EXT3_OSVW, CPUID_EXT3_IBS */ |
| 330 | .ext3_features = CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | |
| 331 | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A, |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 332 | .svm_features = CPUID_SVM_NPT | CPUID_SVM_LBRV, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 333 | .xlevel = 0x8000001A, |
| 334 | .model_id = "AMD Phenom(tm) 9550 Quad-Core Processor" |
| 335 | }, |
| 336 | { |
| 337 | .name = "core2duo", |
| 338 | .level = 10, |
| 339 | .family = 6, |
| 340 | .model = 15, |
| 341 | .stepping = 11, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 342 | .features = PPRO_FEATURES | |
| 343 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 344 | CPUID_PSE36 | CPUID_VME | CPUID_DTS | CPUID_ACPI | CPUID_SS | |
| 345 | CPUID_HT | CPUID_TM | CPUID_PBE, |
| 346 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | |
| 347 | CPUID_EXT_DTES64 | CPUID_EXT_DSCPL | CPUID_EXT_VMX | CPUID_EXT_EST | |
| 348 | CPUID_EXT_TM2 | CPUID_EXT_CX16 | CPUID_EXT_XTPR | CPUID_EXT_PDCM, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 349 | .ext2_features = CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX, |
| 350 | .ext3_features = CPUID_EXT3_LAHF_LM, |
| 351 | .xlevel = 0x80000008, |
| 352 | .model_id = "Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz", |
| 353 | }, |
| 354 | { |
| 355 | .name = "kvm64", |
| 356 | .level = 5, |
| 357 | .vendor1 = CPUID_VENDOR_INTEL_1, |
| 358 | .vendor2 = CPUID_VENDOR_INTEL_2, |
| 359 | .vendor3 = CPUID_VENDOR_INTEL_3, |
| 360 | .family = 15, |
| 361 | .model = 6, |
| 362 | .stepping = 1, |
| 363 | /* Missing: CPUID_VME, CPUID_HT */ |
| 364 | .features = PPRO_FEATURES | |
| 365 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | |
| 366 | CPUID_PSE36, |
| 367 | /* Missing: CPUID_EXT_POPCNT, CPUID_EXT_MONITOR */ |
| 368 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_CX16, |
| 369 | /* Missing: CPUID_EXT2_PDPE1GB, CPUID_EXT2_RDTSCP */ |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 370 | .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 371 | CPUID_EXT2_LM | CPUID_EXT2_SYSCALL | CPUID_EXT2_NX, |
| 372 | /* Missing: CPUID_EXT3_LAHF_LM, CPUID_EXT3_CMP_LEG, CPUID_EXT3_EXTAPIC, |
| 373 | CPUID_EXT3_CR8LEG, CPUID_EXT3_ABM, CPUID_EXT3_SSE4A, |
| 374 | CPUID_EXT3_MISALIGNSSE, CPUID_EXT3_3DNOWPREFETCH, |
| 375 | CPUID_EXT3_OSVW, CPUID_EXT3_IBS, CPUID_EXT3_SVM */ |
| 376 | .ext3_features = 0, |
| 377 | .xlevel = 0x80000008, |
| 378 | .model_id = "Common KVM processor" |
| 379 | }, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 380 | { |
| 381 | .name = "qemu32", |
| 382 | .level = 4, |
| 383 | .family = 6, |
| 384 | .model = 3, |
| 385 | .stepping = 3, |
| 386 | .features = PPRO_FEATURES, |
| 387 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_POPCNT, |
Andre Przywara | 58012d6 | 2010-03-11 14:39:06 +0100 | [diff] [blame] | 388 | .xlevel = 0x80000004, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 389 | .model_id = "QEMU Virtual CPU version " QEMU_VERSION, |
| 390 | }, |
| 391 | { |
Andre Przywara | eafaf1e | 2010-05-21 09:50:51 +0200 | [diff] [blame] | 392 | .name = "kvm32", |
| 393 | .level = 5, |
| 394 | .family = 15, |
| 395 | .model = 6, |
| 396 | .stepping = 1, |
| 397 | .features = PPRO_FEATURES | |
| 398 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_PSE36, |
| 399 | .ext_features = CPUID_EXT_SSE3, |
| 400 | .ext2_features = PPRO_FEATURES & EXT2_FEATURE_MASK, |
| 401 | .ext3_features = 0, |
| 402 | .xlevel = 0x80000008, |
| 403 | .model_id = "Common 32-bit KVM processor" |
| 404 | }, |
| 405 | { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 406 | .name = "coreduo", |
| 407 | .level = 10, |
| 408 | .family = 6, |
| 409 | .model = 14, |
| 410 | .stepping = 8, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 411 | .features = PPRO_FEATURES | CPUID_VME | |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 412 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_DTS | CPUID_ACPI | |
| 413 | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE, |
| 414 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_VMX | |
| 415 | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR | CPUID_EXT_PDCM, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 416 | .ext2_features = CPUID_EXT2_NX, |
| 417 | .xlevel = 0x80000008, |
| 418 | .model_id = "Genuine Intel(R) CPU T2600 @ 2.16GHz", |
| 419 | }, |
| 420 | { |
| 421 | .name = "486", |
Andre Przywara | 58012d6 | 2010-03-11 14:39:06 +0100 | [diff] [blame] | 422 | .level = 1, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 423 | .family = 4, |
| 424 | .model = 0, |
| 425 | .stepping = 0, |
| 426 | .features = I486_FEATURES, |
| 427 | .xlevel = 0, |
| 428 | }, |
| 429 | { |
| 430 | .name = "pentium", |
| 431 | .level = 1, |
| 432 | .family = 5, |
| 433 | .model = 4, |
| 434 | .stepping = 3, |
| 435 | .features = PENTIUM_FEATURES, |
| 436 | .xlevel = 0, |
| 437 | }, |
| 438 | { |
| 439 | .name = "pentium2", |
| 440 | .level = 2, |
| 441 | .family = 6, |
| 442 | .model = 5, |
| 443 | .stepping = 2, |
| 444 | .features = PENTIUM2_FEATURES, |
| 445 | .xlevel = 0, |
| 446 | }, |
| 447 | { |
| 448 | .name = "pentium3", |
| 449 | .level = 2, |
| 450 | .family = 6, |
| 451 | .model = 7, |
| 452 | .stepping = 3, |
| 453 | .features = PENTIUM3_FEATURES, |
| 454 | .xlevel = 0, |
| 455 | }, |
| 456 | { |
| 457 | .name = "athlon", |
| 458 | .level = 2, |
| 459 | .vendor1 = CPUID_VENDOR_AMD_1, |
| 460 | .vendor2 = CPUID_VENDOR_AMD_2, |
| 461 | .vendor3 = CPUID_VENDOR_AMD_3, |
| 462 | .family = 6, |
| 463 | .model = 2, |
| 464 | .stepping = 3, |
| 465 | .features = PPRO_FEATURES | CPUID_PSE36 | CPUID_VME | CPUID_MTRR | CPUID_MCA, |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 466 | .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_MMXEXT | CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 467 | .xlevel = 0x80000008, |
| 468 | /* XXX: put another string ? */ |
| 469 | .model_id = "QEMU Virtual CPU version " QEMU_VERSION, |
| 470 | }, |
| 471 | { |
| 472 | .name = "n270", |
| 473 | /* original is on level 10 */ |
| 474 | .level = 5, |
| 475 | .family = 6, |
| 476 | .model = 28, |
| 477 | .stepping = 2, |
| 478 | .features = PPRO_FEATURES | |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 479 | CPUID_MTRR | CPUID_CLFLUSH | CPUID_MCA | CPUID_VME | CPUID_DTS | |
| 480 | CPUID_ACPI | CPUID_SS | CPUID_HT | CPUID_TM | CPUID_PBE, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 481 | /* Some CPUs got no CPUID_SEP */ |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 482 | .ext_features = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | |
| 483 | CPUID_EXT_DSCPL | CPUID_EXT_EST | CPUID_EXT_TM2 | CPUID_EXT_XTPR, |
Andre Przywara | 4267393 | 2010-03-11 14:38:56 +0100 | [diff] [blame] | 484 | .ext2_features = (PPRO_FEATURES & EXT2_FEATURE_MASK) | CPUID_EXT2_NX, |
Aurelien Jarno | 8560efe | 2010-03-13 16:43:15 +0100 | [diff] [blame] | 485 | .ext3_features = CPUID_EXT3_LAHF_LM, |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 486 | .xlevel = 0x8000000A, |
| 487 | .model_id = "Intel(R) Atom(TM) CPU N270 @ 1.60GHz", |
| 488 | }, |
| 489 | }; |
| 490 | |
| 491 | static int cpu_x86_fill_model_id(char *str) |
| 492 | { |
| 493 | uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0; |
| 494 | int i; |
| 495 | |
| 496 | for (i = 0; i < 3; i++) { |
| 497 | host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx); |
| 498 | memcpy(str + i * 16 + 0, &eax, 4); |
| 499 | memcpy(str + i * 16 + 4, &ebx, 4); |
| 500 | memcpy(str + i * 16 + 8, &ecx, 4); |
| 501 | memcpy(str + i * 16 + 12, &edx, 4); |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int cpu_x86_fill_host(x86_def_t *x86_cpu_def) |
| 507 | { |
| 508 | uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0; |
| 509 | |
| 510 | x86_cpu_def->name = "host"; |
| 511 | host_cpuid(0x0, 0, &eax, &ebx, &ecx, &edx); |
| 512 | x86_cpu_def->level = eax; |
| 513 | x86_cpu_def->vendor1 = ebx; |
| 514 | x86_cpu_def->vendor2 = edx; |
| 515 | x86_cpu_def->vendor3 = ecx; |
| 516 | |
| 517 | host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx); |
| 518 | x86_cpu_def->family = ((eax >> 8) & 0x0F) + ((eax >> 20) & 0xFF); |
| 519 | x86_cpu_def->model = ((eax >> 4) & 0x0F) | ((eax & 0xF0000) >> 12); |
| 520 | x86_cpu_def->stepping = eax & 0x0F; |
| 521 | x86_cpu_def->ext_features = ecx; |
| 522 | x86_cpu_def->features = edx; |
| 523 | |
| 524 | host_cpuid(0x80000000, 0, &eax, &ebx, &ecx, &edx); |
| 525 | x86_cpu_def->xlevel = eax; |
| 526 | |
| 527 | host_cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx); |
| 528 | x86_cpu_def->ext2_features = edx; |
| 529 | x86_cpu_def->ext3_features = ecx; |
| 530 | cpu_x86_fill_model_id(x86_cpu_def->model_id); |
| 531 | x86_cpu_def->vendor_override = 0; |
| 532 | |
brillywu@viatech.com.cn | b3baa15 | 2011-06-01 09:59:52 +0800 | [diff] [blame] | 533 | /* Call Centaur's CPUID instruction. */ |
| 534 | if (x86_cpu_def->vendor1 == CPUID_VENDOR_VIA_1 && |
| 535 | x86_cpu_def->vendor2 == CPUID_VENDOR_VIA_2 && |
| 536 | x86_cpu_def->vendor3 == CPUID_VENDOR_VIA_3) { |
| 537 | host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx); |
| 538 | if (eax >= 0xC0000001) { |
| 539 | /* Support VIA max extended level */ |
| 540 | x86_cpu_def->xlevel2 = eax; |
| 541 | host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx); |
| 542 | x86_cpu_def->ext4_features = edx; |
| 543 | } |
| 544 | } |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * Every SVM feature requires emulation support in KVM - so we can't just |
| 548 | * read the host features here. KVM might even support SVM features not |
| 549 | * available on the host hardware. Just set all bits and mask out the |
| 550 | * unsupported ones later. |
| 551 | */ |
| 552 | x86_cpu_def->svm_features = -1; |
| 553 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int unavailable_host_feature(struct model_features_t *f, uint32_t mask) |
| 558 | { |
| 559 | int i; |
| 560 | |
| 561 | for (i = 0; i < 32; ++i) |
| 562 | if (1 << i & mask) { |
| 563 | fprintf(stderr, "warning: host cpuid %04x_%04x lacks requested" |
| 564 | " flag '%s' [0x%08x]\n", |
| 565 | f->cpuid >> 16, f->cpuid & 0xffff, |
| 566 | f->flag_names[i] ? f->flag_names[i] : "[reserved]", mask); |
| 567 | break; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /* best effort attempt to inform user requested cpu flags aren't making |
| 573 | * their way to the guest. Note: ft[].check_feat ideally should be |
| 574 | * specified via a guest_def field to suppress report of extraneous flags. |
| 575 | */ |
| 576 | static int check_features_against_host(x86_def_t *guest_def) |
| 577 | { |
| 578 | x86_def_t host_def; |
| 579 | uint32_t mask; |
| 580 | int rv, i; |
| 581 | struct model_features_t ft[] = { |
| 582 | {&guest_def->features, &host_def.features, |
| 583 | ~0, feature_name, 0x00000000}, |
| 584 | {&guest_def->ext_features, &host_def.ext_features, |
| 585 | ~CPUID_EXT_HYPERVISOR, ext_feature_name, 0x00000001}, |
| 586 | {&guest_def->ext2_features, &host_def.ext2_features, |
| 587 | ~PPRO_FEATURES, ext2_feature_name, 0x80000000}, |
| 588 | {&guest_def->ext3_features, &host_def.ext3_features, |
| 589 | ~CPUID_EXT3_SVM, ext3_feature_name, 0x80000001}}; |
| 590 | |
| 591 | cpu_x86_fill_host(&host_def); |
Blue Swirl | 66fe09e | 2010-08-20 21:03:24 +0000 | [diff] [blame] | 592 | for (rv = 0, i = 0; i < ARRAY_SIZE(ft); ++i) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 593 | for (mask = 1; mask; mask <<= 1) |
| 594 | if (ft[i].check_feat & mask && *ft[i].guest_feat & mask && |
| 595 | !(*ft[i].host_feat & mask)) { |
| 596 | unavailable_host_feature(&ft[i], mask); |
| 597 | rv = 1; |
| 598 | } |
| 599 | return rv; |
| 600 | } |
| 601 | |
Andreas Färber | 95b8519 | 2012-04-17 14:42:22 +0200 | [diff] [blame] | 602 | static void x86_cpuid_version_get_family(Object *obj, Visitor *v, void *opaque, |
| 603 | const char *name, Error **errp) |
| 604 | { |
| 605 | X86CPU *cpu = X86_CPU(obj); |
| 606 | CPUX86State *env = &cpu->env; |
| 607 | int64_t value; |
| 608 | |
| 609 | value = (env->cpuid_version >> 8) & 0xf; |
| 610 | if (value == 0xf) { |
| 611 | value += (env->cpuid_version >> 20) & 0xff; |
| 612 | } |
| 613 | visit_type_int(v, &value, name, errp); |
| 614 | } |
| 615 | |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 616 | static void x86_cpuid_version_set_family(Object *obj, Visitor *v, void *opaque, |
| 617 | const char *name, Error **errp) |
Andreas Färber | ed5e1ec | 2012-02-17 17:46:01 +0100 | [diff] [blame] | 618 | { |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 619 | X86CPU *cpu = X86_CPU(obj); |
| 620 | CPUX86State *env = &cpu->env; |
| 621 | const int64_t min = 0; |
| 622 | const int64_t max = 0xff + 0xf; |
| 623 | int64_t value; |
| 624 | |
| 625 | visit_type_int(v, &value, name, errp); |
| 626 | if (error_is_set(errp)) { |
| 627 | return; |
| 628 | } |
| 629 | if (value < min || value > max) { |
| 630 | error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "", |
| 631 | name ? name : "null", value, min, max); |
| 632 | return; |
| 633 | } |
| 634 | |
Andreas Färber | ed5e1ec | 2012-02-17 17:46:01 +0100 | [diff] [blame] | 635 | env->cpuid_version &= ~0xff00f00; |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 636 | if (value > 0x0f) { |
| 637 | env->cpuid_version |= 0xf00 | ((value - 0x0f) << 20); |
Andreas Färber | ed5e1ec | 2012-02-17 17:46:01 +0100 | [diff] [blame] | 638 | } else { |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 639 | env->cpuid_version |= value << 8; |
Andreas Färber | ed5e1ec | 2012-02-17 17:46:01 +0100 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Andreas Färber | 67e30c8 | 2012-04-17 14:48:14 +0200 | [diff] [blame] | 643 | static void x86_cpuid_version_get_model(Object *obj, Visitor *v, void *opaque, |
| 644 | const char *name, Error **errp) |
| 645 | { |
| 646 | X86CPU *cpu = X86_CPU(obj); |
| 647 | CPUX86State *env = &cpu->env; |
| 648 | int64_t value; |
| 649 | |
| 650 | value = (env->cpuid_version >> 4) & 0xf; |
| 651 | value |= ((env->cpuid_version >> 16) & 0xf) << 4; |
| 652 | visit_type_int(v, &value, name, errp); |
| 653 | } |
| 654 | |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 655 | static void x86_cpuid_version_set_model(Object *obj, Visitor *v, void *opaque, |
| 656 | const char *name, Error **errp) |
Andreas Färber | b0704cb | 2012-02-17 17:46:02 +0100 | [diff] [blame] | 657 | { |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 658 | X86CPU *cpu = X86_CPU(obj); |
| 659 | CPUX86State *env = &cpu->env; |
| 660 | const int64_t min = 0; |
| 661 | const int64_t max = 0xff; |
| 662 | int64_t value; |
| 663 | |
| 664 | visit_type_int(v, &value, name, errp); |
| 665 | if (error_is_set(errp)) { |
| 666 | return; |
| 667 | } |
| 668 | if (value < min || value > max) { |
| 669 | error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "", |
| 670 | name ? name : "null", value, min, max); |
| 671 | return; |
| 672 | } |
| 673 | |
Andreas Färber | b0704cb | 2012-02-17 17:46:02 +0100 | [diff] [blame] | 674 | env->cpuid_version &= ~0xf00f0; |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 675 | env->cpuid_version |= ((value & 0xf) << 4) | ((value >> 4) << 16); |
Andreas Färber | b0704cb | 2012-02-17 17:46:02 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Andreas Färber | 35112e4 | 2012-04-17 14:50:53 +0200 | [diff] [blame] | 678 | static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v, |
| 679 | void *opaque, const char *name, |
| 680 | Error **errp) |
| 681 | { |
| 682 | X86CPU *cpu = X86_CPU(obj); |
| 683 | CPUX86State *env = &cpu->env; |
| 684 | int64_t value; |
| 685 | |
| 686 | value = env->cpuid_version & 0xf; |
| 687 | visit_type_int(v, &value, name, errp); |
| 688 | } |
| 689 | |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 690 | static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v, |
| 691 | void *opaque, const char *name, |
| 692 | Error **errp) |
Andreas Färber | 38c3dc4 | 2012-02-17 17:46:03 +0100 | [diff] [blame] | 693 | { |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 694 | X86CPU *cpu = X86_CPU(obj); |
| 695 | CPUX86State *env = &cpu->env; |
| 696 | const int64_t min = 0; |
| 697 | const int64_t max = 0xf; |
| 698 | int64_t value; |
| 699 | |
| 700 | visit_type_int(v, &value, name, errp); |
| 701 | if (error_is_set(errp)) { |
| 702 | return; |
| 703 | } |
| 704 | if (value < min || value > max) { |
| 705 | error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "", |
| 706 | name ? name : "null", value, min, max); |
| 707 | return; |
| 708 | } |
| 709 | |
Andreas Färber | 38c3dc4 | 2012-02-17 17:46:03 +0100 | [diff] [blame] | 710 | env->cpuid_version &= ~0xf; |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 711 | env->cpuid_version |= value & 0xf; |
Andreas Färber | 38c3dc4 | 2012-02-17 17:46:03 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Andreas Färber | 8e1898b | 2012-04-17 18:41:40 +0200 | [diff] [blame^] | 714 | static void x86_cpuid_get_level(Object *obj, Visitor *v, void *opaque, |
| 715 | const char *name, Error **errp) |
| 716 | { |
| 717 | X86CPU *cpu = X86_CPU(obj); |
| 718 | int64_t value; |
| 719 | |
| 720 | value = cpu->env.cpuid_level; |
| 721 | /* TODO Use visit_type_uint32() once available */ |
| 722 | visit_type_int(v, &value, name, errp); |
| 723 | } |
| 724 | |
| 725 | static void x86_cpuid_set_level(Object *obj, Visitor *v, void *opaque, |
| 726 | const char *name, Error **errp) |
| 727 | { |
| 728 | X86CPU *cpu = X86_CPU(obj); |
| 729 | const int64_t min = 0; |
| 730 | const int64_t max = UINT32_MAX; |
| 731 | int64_t value; |
| 732 | |
| 733 | /* TODO Use visit_type_uint32() once available */ |
| 734 | visit_type_int(v, &value, name, errp); |
| 735 | if (error_is_set(errp)) { |
| 736 | return; |
| 737 | } |
| 738 | if (value < min || value > max) { |
| 739 | error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "", |
| 740 | name ? name : "null", value, min, max); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | cpu->env.cpuid_level = value; |
| 745 | } |
| 746 | |
Andreas Färber | 63e886e | 2012-04-17 23:02:26 +0200 | [diff] [blame] | 747 | static char *x86_cpuid_get_model_id(Object *obj, Error **errp) |
| 748 | { |
| 749 | X86CPU *cpu = X86_CPU(obj); |
| 750 | CPUX86State *env = &cpu->env; |
| 751 | char *value; |
| 752 | int i; |
| 753 | |
| 754 | value = g_malloc(48 + 1); |
| 755 | for (i = 0; i < 48; i++) { |
| 756 | value[i] = env->cpuid_model[i >> 2] >> (8 * (i & 3)); |
| 757 | } |
| 758 | value[48] = '\0'; |
| 759 | return value; |
| 760 | } |
| 761 | |
Andreas Färber | 938d4c2 | 2012-04-17 15:17:27 +0200 | [diff] [blame] | 762 | static void x86_cpuid_set_model_id(Object *obj, const char *model_id, |
| 763 | Error **errp) |
Andreas Färber | dcce667 | 2012-02-17 17:46:04 +0100 | [diff] [blame] | 764 | { |
Andreas Färber | 938d4c2 | 2012-04-17 15:17:27 +0200 | [diff] [blame] | 765 | X86CPU *cpu = X86_CPU(obj); |
| 766 | CPUX86State *env = &cpu->env; |
Andreas Färber | dcce667 | 2012-02-17 17:46:04 +0100 | [diff] [blame] | 767 | int c, len, i; |
| 768 | |
| 769 | if (model_id == NULL) { |
| 770 | model_id = ""; |
| 771 | } |
| 772 | len = strlen(model_id); |
Andreas Färber | d0a6acf | 2012-04-17 18:21:52 +0200 | [diff] [blame] | 773 | memset(env->cpuid_model, 0, 48); |
Andreas Färber | dcce667 | 2012-02-17 17:46:04 +0100 | [diff] [blame] | 774 | for (i = 0; i < 48; i++) { |
| 775 | if (i >= len) { |
| 776 | c = '\0'; |
| 777 | } else { |
| 778 | c = (uint8_t)model_id[i]; |
| 779 | } |
| 780 | env->cpuid_model[i >> 2] |= c << (8 * (i & 3)); |
| 781 | } |
| 782 | } |
| 783 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 784 | static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) |
| 785 | { |
| 786 | unsigned int i; |
| 787 | x86_def_t *def; |
| 788 | |
Markus Armbruster | d3c481b | 2011-11-08 15:36:48 +0100 | [diff] [blame] | 789 | char *s = g_strdup(cpu_model); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 790 | char *featurestr, *name = strtok(s, ","); |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 791 | /* Features to be added*/ |
| 792 | uint32_t plus_features = 0, plus_ext_features = 0; |
| 793 | uint32_t plus_ext2_features = 0, plus_ext3_features = 0; |
| 794 | uint32_t plus_kvm_features = 0, plus_svm_features = 0; |
| 795 | /* Features to be removed */ |
| 796 | uint32_t minus_features = 0, minus_ext_features = 0; |
| 797 | uint32_t minus_ext2_features = 0, minus_ext3_features = 0; |
| 798 | uint32_t minus_kvm_features = 0, minus_svm_features = 0; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 799 | uint32_t numvalue; |
| 800 | |
| 801 | for (def = x86_defs; def; def = def->next) |
Markus Armbruster | 04c5b17 | 2011-11-08 15:36:50 +0100 | [diff] [blame] | 802 | if (name && !strcmp(name, def->name)) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 803 | break; |
Markus Armbruster | 04c5b17 | 2011-11-08 15:36:50 +0100 | [diff] [blame] | 804 | if (kvm_enabled() && name && strcmp(name, "host") == 0) { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 805 | cpu_x86_fill_host(x86_cpu_def); |
| 806 | } else if (!def) { |
| 807 | goto error; |
| 808 | } else { |
| 809 | memcpy(x86_cpu_def, def, sizeof(*def)); |
| 810 | } |
| 811 | |
| 812 | plus_kvm_features = ~0; /* not supported bits will be filtered out later */ |
| 813 | |
| 814 | add_flagname_to_bitmaps("hypervisor", &plus_features, |
| 815 | &plus_ext_features, &plus_ext2_features, &plus_ext3_features, |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 816 | &plus_kvm_features, &plus_svm_features); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 817 | |
| 818 | featurestr = strtok(NULL, ","); |
| 819 | |
| 820 | while (featurestr) { |
| 821 | char *val; |
| 822 | if (featurestr[0] == '+') { |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 823 | add_flagname_to_bitmaps(featurestr + 1, &plus_features, |
| 824 | &plus_ext_features, &plus_ext2_features, |
| 825 | &plus_ext3_features, &plus_kvm_features, |
| 826 | &plus_svm_features); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 827 | } else if (featurestr[0] == '-') { |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 828 | add_flagname_to_bitmaps(featurestr + 1, &minus_features, |
| 829 | &minus_ext_features, &minus_ext2_features, |
| 830 | &minus_ext3_features, &minus_kvm_features, |
| 831 | &minus_svm_features); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 832 | } else if ((val = strchr(featurestr, '='))) { |
| 833 | *val = 0; val++; |
| 834 | if (!strcmp(featurestr, "family")) { |
| 835 | char *err; |
| 836 | numvalue = strtoul(val, &err, 0); |
Andreas Färber | a88a677 | 2012-04-17 16:15:15 +0200 | [diff] [blame] | 837 | if (!*val || *err || numvalue > 0xff + 0xf) { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 838 | fprintf(stderr, "bad numerical value %s\n", val); |
| 839 | goto error; |
| 840 | } |
| 841 | x86_cpu_def->family = numvalue; |
| 842 | } else if (!strcmp(featurestr, "model")) { |
| 843 | char *err; |
| 844 | numvalue = strtoul(val, &err, 0); |
| 845 | if (!*val || *err || numvalue > 0xff) { |
| 846 | fprintf(stderr, "bad numerical value %s\n", val); |
| 847 | goto error; |
| 848 | } |
| 849 | x86_cpu_def->model = numvalue; |
| 850 | } else if (!strcmp(featurestr, "stepping")) { |
| 851 | char *err; |
| 852 | numvalue = strtoul(val, &err, 0); |
| 853 | if (!*val || *err || numvalue > 0xf) { |
| 854 | fprintf(stderr, "bad numerical value %s\n", val); |
| 855 | goto error; |
| 856 | } |
| 857 | x86_cpu_def->stepping = numvalue ; |
| 858 | } else if (!strcmp(featurestr, "level")) { |
| 859 | char *err; |
| 860 | numvalue = strtoul(val, &err, 0); |
| 861 | if (!*val || *err) { |
| 862 | fprintf(stderr, "bad numerical value %s\n", val); |
| 863 | goto error; |
| 864 | } |
| 865 | x86_cpu_def->level = numvalue; |
| 866 | } else if (!strcmp(featurestr, "xlevel")) { |
| 867 | char *err; |
| 868 | numvalue = strtoul(val, &err, 0); |
| 869 | if (!*val || *err) { |
| 870 | fprintf(stderr, "bad numerical value %s\n", val); |
| 871 | goto error; |
| 872 | } |
| 873 | if (numvalue < 0x80000000) { |
Aurelien Jarno | 2f7a21c | 2010-03-13 16:46:33 +0100 | [diff] [blame] | 874 | numvalue += 0x80000000; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 875 | } |
| 876 | x86_cpu_def->xlevel = numvalue; |
| 877 | } else if (!strcmp(featurestr, "vendor")) { |
| 878 | if (strlen(val) != 12) { |
| 879 | fprintf(stderr, "vendor string must be 12 chars long\n"); |
| 880 | goto error; |
| 881 | } |
| 882 | x86_cpu_def->vendor1 = 0; |
| 883 | x86_cpu_def->vendor2 = 0; |
| 884 | x86_cpu_def->vendor3 = 0; |
| 885 | for(i = 0; i < 4; i++) { |
| 886 | x86_cpu_def->vendor1 |= ((uint8_t)val[i ]) << (8 * i); |
| 887 | x86_cpu_def->vendor2 |= ((uint8_t)val[i + 4]) << (8 * i); |
| 888 | x86_cpu_def->vendor3 |= ((uint8_t)val[i + 8]) << (8 * i); |
| 889 | } |
| 890 | x86_cpu_def->vendor_override = 1; |
| 891 | } else if (!strcmp(featurestr, "model_id")) { |
| 892 | pstrcpy(x86_cpu_def->model_id, sizeof(x86_cpu_def->model_id), |
| 893 | val); |
Joerg Roedel | b862d1f | 2011-07-07 16:13:12 +0200 | [diff] [blame] | 894 | } else if (!strcmp(featurestr, "tsc_freq")) { |
| 895 | int64_t tsc_freq; |
| 896 | char *err; |
| 897 | |
| 898 | tsc_freq = strtosz_suffix_unit(val, &err, |
| 899 | STRTOSZ_DEFSUFFIX_B, 1000); |
Markus Armbruster | 45009a3 | 2011-11-22 09:46:04 +0100 | [diff] [blame] | 900 | if (tsc_freq < 0 || *err) { |
Joerg Roedel | b862d1f | 2011-07-07 16:13:12 +0200 | [diff] [blame] | 901 | fprintf(stderr, "bad numerical value %s\n", val); |
| 902 | goto error; |
| 903 | } |
| 904 | x86_cpu_def->tsc_khz = tsc_freq / 1000; |
Vadim Rozenfeld | 28f52cc | 2011-12-18 22:48:13 +0200 | [diff] [blame] | 905 | } else if (!strcmp(featurestr, "hv_spinlocks")) { |
| 906 | char *err; |
| 907 | numvalue = strtoul(val, &err, 0); |
| 908 | if (!*val || *err) { |
| 909 | fprintf(stderr, "bad numerical value %s\n", val); |
| 910 | goto error; |
| 911 | } |
| 912 | hyperv_set_spinlock_retries(numvalue); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 913 | } else { |
| 914 | fprintf(stderr, "unrecognized feature %s\n", featurestr); |
| 915 | goto error; |
| 916 | } |
| 917 | } else if (!strcmp(featurestr, "check")) { |
| 918 | check_cpuid = 1; |
| 919 | } else if (!strcmp(featurestr, "enforce")) { |
| 920 | check_cpuid = enforce_cpuid = 1; |
Vadim Rozenfeld | 28f52cc | 2011-12-18 22:48:13 +0200 | [diff] [blame] | 921 | } else if (!strcmp(featurestr, "hv_relaxed")) { |
| 922 | hyperv_enable_relaxed_timing(true); |
| 923 | } else if (!strcmp(featurestr, "hv_vapic")) { |
| 924 | hyperv_enable_vapic_recommended(true); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 925 | } else { |
| 926 | fprintf(stderr, "feature string `%s' not in format (+feature|-feature|feature=xyz)\n", featurestr); |
| 927 | goto error; |
| 928 | } |
| 929 | featurestr = strtok(NULL, ","); |
| 930 | } |
| 931 | x86_cpu_def->features |= plus_features; |
| 932 | x86_cpu_def->ext_features |= plus_ext_features; |
| 933 | x86_cpu_def->ext2_features |= plus_ext2_features; |
| 934 | x86_cpu_def->ext3_features |= plus_ext3_features; |
| 935 | x86_cpu_def->kvm_features |= plus_kvm_features; |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 936 | x86_cpu_def->svm_features |= plus_svm_features; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 937 | x86_cpu_def->features &= ~minus_features; |
| 938 | x86_cpu_def->ext_features &= ~minus_ext_features; |
| 939 | x86_cpu_def->ext2_features &= ~minus_ext2_features; |
| 940 | x86_cpu_def->ext3_features &= ~minus_ext3_features; |
| 941 | x86_cpu_def->kvm_features &= ~minus_kvm_features; |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 942 | x86_cpu_def->svm_features &= ~minus_svm_features; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 943 | if (check_cpuid) { |
| 944 | if (check_features_against_host(x86_cpu_def) && enforce_cpuid) |
| 945 | goto error; |
| 946 | } |
Markus Armbruster | d3c481b | 2011-11-08 15:36:48 +0100 | [diff] [blame] | 947 | g_free(s); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 948 | return 0; |
| 949 | |
| 950 | error: |
Markus Armbruster | d3c481b | 2011-11-08 15:36:48 +0100 | [diff] [blame] | 951 | g_free(s); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | /* generate a composite string into buf of all cpuid names in featureset |
| 956 | * selected by fbits. indicate truncation at bufsize in the event of overflow. |
| 957 | * if flags, suppress names undefined in featureset. |
| 958 | */ |
| 959 | static void listflags(char *buf, int bufsize, uint32_t fbits, |
| 960 | const char **featureset, uint32_t flags) |
| 961 | { |
| 962 | const char **p = &featureset[31]; |
| 963 | char *q, *b, bit; |
| 964 | int nc; |
| 965 | |
| 966 | b = 4 <= bufsize ? buf + (bufsize -= 3) - 1 : NULL; |
| 967 | *buf = '\0'; |
| 968 | for (q = buf, bit = 31; fbits && bufsize; --p, fbits &= ~(1 << bit), --bit) |
| 969 | if (fbits & 1 << bit && (*p || !flags)) { |
| 970 | if (*p) |
| 971 | nc = snprintf(q, bufsize, "%s%s", q == buf ? "" : " ", *p); |
| 972 | else |
| 973 | nc = snprintf(q, bufsize, "%s[%d]", q == buf ? "" : " ", bit); |
| 974 | if (bufsize <= nc) { |
| 975 | if (b) { |
| 976 | memcpy(b, "...", sizeof("...")); |
| 977 | } |
| 978 | return; |
| 979 | } |
| 980 | q += nc; |
| 981 | bufsize -= nc; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* generate CPU information: |
| 986 | * -? list model names |
| 987 | * -?model list model names/IDs |
| 988 | * -?dump output all model (x86_def_t) data |
| 989 | * -?cpuid list all recognized cpuid flag names |
| 990 | */ |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 991 | void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 992 | { |
| 993 | unsigned char model = !strcmp("?model", optarg); |
| 994 | unsigned char dump = !strcmp("?dump", optarg); |
| 995 | unsigned char cpuid = !strcmp("?cpuid", optarg); |
| 996 | x86_def_t *def; |
| 997 | char buf[256]; |
| 998 | |
| 999 | if (cpuid) { |
| 1000 | (*cpu_fprintf)(f, "Recognized CPUID flags:\n"); |
| 1001 | listflags(buf, sizeof (buf), (uint32_t)~0, feature_name, 1); |
| 1002 | (*cpu_fprintf)(f, " f_edx: %s\n", buf); |
| 1003 | listflags(buf, sizeof (buf), (uint32_t)~0, ext_feature_name, 1); |
| 1004 | (*cpu_fprintf)(f, " f_ecx: %s\n", buf); |
| 1005 | listflags(buf, sizeof (buf), (uint32_t)~0, ext2_feature_name, 1); |
| 1006 | (*cpu_fprintf)(f, " extf_edx: %s\n", buf); |
| 1007 | listflags(buf, sizeof (buf), (uint32_t)~0, ext3_feature_name, 1); |
| 1008 | (*cpu_fprintf)(f, " extf_ecx: %s\n", buf); |
| 1009 | return; |
| 1010 | } |
| 1011 | for (def = x86_defs; def; def = def->next) { |
| 1012 | snprintf(buf, sizeof (buf), def->flags ? "[%s]": "%s", def->name); |
| 1013 | if (model || dump) { |
| 1014 | (*cpu_fprintf)(f, "x86 %16s %-48s\n", buf, def->model_id); |
| 1015 | } else { |
| 1016 | (*cpu_fprintf)(f, "x86 %16s\n", buf); |
| 1017 | } |
| 1018 | if (dump) { |
| 1019 | memcpy(buf, &def->vendor1, sizeof (def->vendor1)); |
| 1020 | memcpy(buf + 4, &def->vendor2, sizeof (def->vendor2)); |
| 1021 | memcpy(buf + 8, &def->vendor3, sizeof (def->vendor3)); |
| 1022 | buf[12] = '\0'; |
| 1023 | (*cpu_fprintf)(f, |
| 1024 | " family %d model %d stepping %d level %d xlevel 0x%x" |
| 1025 | " vendor \"%s\"\n", |
| 1026 | def->family, def->model, def->stepping, def->level, |
| 1027 | def->xlevel, buf); |
| 1028 | listflags(buf, sizeof (buf), def->features, feature_name, 0); |
| 1029 | (*cpu_fprintf)(f, " feature_edx %08x (%s)\n", def->features, |
| 1030 | buf); |
| 1031 | listflags(buf, sizeof (buf), def->ext_features, ext_feature_name, |
| 1032 | 0); |
| 1033 | (*cpu_fprintf)(f, " feature_ecx %08x (%s)\n", def->ext_features, |
| 1034 | buf); |
| 1035 | listflags(buf, sizeof (buf), def->ext2_features, ext2_feature_name, |
| 1036 | 0); |
| 1037 | (*cpu_fprintf)(f, " extfeature_edx %08x (%s)\n", |
| 1038 | def->ext2_features, buf); |
| 1039 | listflags(buf, sizeof (buf), def->ext3_features, ext3_feature_name, |
| 1040 | 0); |
| 1041 | (*cpu_fprintf)(f, " extfeature_ecx %08x (%s)\n", |
| 1042 | def->ext3_features, buf); |
| 1043 | (*cpu_fprintf)(f, "\n"); |
| 1044 | } |
| 1045 | } |
Andre Przywara | ed2c54d | 2010-03-11 14:39:00 +0100 | [diff] [blame] | 1046 | if (kvm_enabled()) { |
| 1047 | (*cpu_fprintf)(f, "x86 %16s\n", "[host]"); |
| 1048 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Andreas Färber | 61dcd77 | 2012-04-17 12:00:51 +0200 | [diff] [blame] | 1051 | int cpu_x86_register(X86CPU *cpu, const char *cpu_model) |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1052 | { |
Andreas Färber | 61dcd77 | 2012-04-17 12:00:51 +0200 | [diff] [blame] | 1053 | CPUX86State *env = &cpu->env; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1054 | x86_def_t def1, *def = &def1; |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1055 | Error *error = NULL; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1056 | |
Joerg Roedel | db0ad1b | 2010-09-27 15:16:16 +0200 | [diff] [blame] | 1057 | memset(def, 0, sizeof(*def)); |
| 1058 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1059 | if (cpu_x86_find_by_name(def, cpu_model) < 0) |
| 1060 | return -1; |
| 1061 | if (def->vendor1) { |
| 1062 | env->cpuid_vendor1 = def->vendor1; |
| 1063 | env->cpuid_vendor2 = def->vendor2; |
| 1064 | env->cpuid_vendor3 = def->vendor3; |
| 1065 | } else { |
| 1066 | env->cpuid_vendor1 = CPUID_VENDOR_INTEL_1; |
| 1067 | env->cpuid_vendor2 = CPUID_VENDOR_INTEL_2; |
| 1068 | env->cpuid_vendor3 = CPUID_VENDOR_INTEL_3; |
| 1069 | } |
| 1070 | env->cpuid_vendor_override = def->vendor_override; |
Andreas Färber | 8e1898b | 2012-04-17 18:41:40 +0200 | [diff] [blame^] | 1071 | object_property_set_int(OBJECT(cpu), def->level, "level", &error); |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1072 | object_property_set_int(OBJECT(cpu), def->family, "family", &error); |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 1073 | object_property_set_int(OBJECT(cpu), def->model, "model", &error); |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 1074 | object_property_set_int(OBJECT(cpu), def->stepping, "stepping", &error); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1075 | env->cpuid_features = def->features; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1076 | env->cpuid_ext_features = def->ext_features; |
| 1077 | env->cpuid_ext2_features = def->ext2_features; |
Andre Przywara | 4d067ed | 2010-03-11 14:38:57 +0100 | [diff] [blame] | 1078 | env->cpuid_ext3_features = def->ext3_features; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1079 | env->cpuid_xlevel = def->xlevel; |
| 1080 | env->cpuid_kvm_features = def->kvm_features; |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 1081 | env->cpuid_svm_features = def->svm_features; |
brillywu@viatech.com.cn | b3baa15 | 2011-06-01 09:59:52 +0800 | [diff] [blame] | 1082 | env->cpuid_ext4_features = def->ext4_features; |
| 1083 | env->cpuid_xlevel2 = def->xlevel2; |
Joerg Roedel | b862d1f | 2011-07-07 16:13:12 +0200 | [diff] [blame] | 1084 | env->tsc_khz = def->tsc_khz; |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 1085 | if (!kvm_enabled()) { |
| 1086 | env->cpuid_features &= TCG_FEATURES; |
| 1087 | env->cpuid_ext_features &= TCG_EXT_FEATURES; |
| 1088 | env->cpuid_ext2_features &= (TCG_EXT2_FEATURES |
| 1089 | #ifdef TARGET_X86_64 |
| 1090 | | CPUID_EXT2_SYSCALL | CPUID_EXT2_LM |
| 1091 | #endif |
| 1092 | ); |
| 1093 | env->cpuid_ext3_features &= TCG_EXT3_FEATURES; |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 1094 | env->cpuid_svm_features &= TCG_SVM_FEATURES; |
Andre Przywara | 551a2de | 2010-03-11 14:39:03 +0100 | [diff] [blame] | 1095 | } |
Andreas Färber | 938d4c2 | 2012-04-17 15:17:27 +0200 | [diff] [blame] | 1096 | object_property_set_str(OBJECT(cpu), def->model_id, "model-id", &error); |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1097 | if (error_is_set(&error)) { |
| 1098 | error_free(error); |
| 1099 | return -1; |
| 1100 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | #if !defined(CONFIG_USER_ONLY) |
| 1105 | /* copy vendor id string to 32 bit register, nul pad as needed |
| 1106 | */ |
| 1107 | static void cpyid(const char *s, uint32_t *id) |
| 1108 | { |
| 1109 | char *d = (char *)id; |
| 1110 | char i; |
| 1111 | |
| 1112 | for (i = sizeof (*id); i--; ) |
| 1113 | *d++ = *s ? *s++ : '\0'; |
| 1114 | } |
| 1115 | |
| 1116 | /* interpret radix and convert from string to arbitrary scalar, |
| 1117 | * otherwise flag failure |
| 1118 | */ |
| 1119 | #define setscalar(pval, str, perr) \ |
| 1120 | { \ |
| 1121 | char *pend; \ |
| 1122 | unsigned long ul; \ |
| 1123 | \ |
| 1124 | ul = strtoul(str, &pend, 0); \ |
| 1125 | *str && !*pend ? (*pval = ul) : (*perr = 1); \ |
| 1126 | } |
| 1127 | |
| 1128 | /* map cpuid options to feature bits, otherwise return failure |
| 1129 | * (option tags in *str are delimited by whitespace) |
| 1130 | */ |
| 1131 | static void setfeatures(uint32_t *pval, const char *str, |
| 1132 | const char **featureset, int *perr) |
| 1133 | { |
| 1134 | const char *p, *q; |
| 1135 | |
| 1136 | for (q = p = str; *p || *q; q = p) { |
| 1137 | while (iswhite(*p)) |
| 1138 | q = ++p; |
| 1139 | while (*p && !iswhite(*p)) |
| 1140 | ++p; |
| 1141 | if (!*q && !*p) |
| 1142 | return; |
| 1143 | if (!lookup_feature(pval, q, p, featureset)) { |
| 1144 | fprintf(stderr, "error: feature \"%.*s\" not available in set\n", |
| 1145 | (int)(p - q), q); |
| 1146 | *perr = 1; |
| 1147 | return; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | /* map config file options to x86_def_t form |
| 1153 | */ |
| 1154 | static int cpudef_setfield(const char *name, const char *str, void *opaque) |
| 1155 | { |
| 1156 | x86_def_t *def = opaque; |
| 1157 | int err = 0; |
| 1158 | |
| 1159 | if (!strcmp(name, "name")) { |
Markus Armbruster | 99e1dec | 2011-11-08 15:36:49 +0100 | [diff] [blame] | 1160 | g_free((void *)def->name); |
Markus Armbruster | d3c481b | 2011-11-08 15:36:48 +0100 | [diff] [blame] | 1161 | def->name = g_strdup(str); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1162 | } else if (!strcmp(name, "model_id")) { |
| 1163 | strncpy(def->model_id, str, sizeof (def->model_id)); |
| 1164 | } else if (!strcmp(name, "level")) { |
| 1165 | setscalar(&def->level, str, &err) |
| 1166 | } else if (!strcmp(name, "vendor")) { |
| 1167 | cpyid(&str[0], &def->vendor1); |
| 1168 | cpyid(&str[4], &def->vendor2); |
| 1169 | cpyid(&str[8], &def->vendor3); |
| 1170 | } else if (!strcmp(name, "family")) { |
| 1171 | setscalar(&def->family, str, &err) |
| 1172 | } else if (!strcmp(name, "model")) { |
| 1173 | setscalar(&def->model, str, &err) |
| 1174 | } else if (!strcmp(name, "stepping")) { |
| 1175 | setscalar(&def->stepping, str, &err) |
| 1176 | } else if (!strcmp(name, "feature_edx")) { |
| 1177 | setfeatures(&def->features, str, feature_name, &err); |
| 1178 | } else if (!strcmp(name, "feature_ecx")) { |
| 1179 | setfeatures(&def->ext_features, str, ext_feature_name, &err); |
| 1180 | } else if (!strcmp(name, "extfeature_edx")) { |
| 1181 | setfeatures(&def->ext2_features, str, ext2_feature_name, &err); |
| 1182 | } else if (!strcmp(name, "extfeature_ecx")) { |
| 1183 | setfeatures(&def->ext3_features, str, ext3_feature_name, &err); |
| 1184 | } else if (!strcmp(name, "xlevel")) { |
| 1185 | setscalar(&def->xlevel, str, &err) |
| 1186 | } else { |
| 1187 | fprintf(stderr, "error: unknown option [%s = %s]\n", name, str); |
| 1188 | return (1); |
| 1189 | } |
| 1190 | if (err) { |
| 1191 | fprintf(stderr, "error: bad option value [%s = %s]\n", name, str); |
| 1192 | return (1); |
| 1193 | } |
| 1194 | return (0); |
| 1195 | } |
| 1196 | |
| 1197 | /* register config file entry as x86_def_t |
| 1198 | */ |
| 1199 | static int cpudef_register(QemuOpts *opts, void *opaque) |
| 1200 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1201 | x86_def_t *def = g_malloc0(sizeof (x86_def_t)); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1202 | |
| 1203 | qemu_opt_foreach(opts, cpudef_setfield, def, 1); |
| 1204 | def->next = x86_defs; |
| 1205 | x86_defs = def; |
| 1206 | return (0); |
| 1207 | } |
Blue Swirl | 0e26b7b | 2010-06-19 10:42:34 +0300 | [diff] [blame] | 1208 | |
| 1209 | void cpu_clear_apic_feature(CPUX86State *env) |
| 1210 | { |
| 1211 | env->cpuid_features &= ~CPUID_APIC; |
| 1212 | } |
| 1213 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1214 | #endif /* !CONFIG_USER_ONLY */ |
| 1215 | |
| 1216 | /* register "cpudef" models defined in configuration file. Here we first |
| 1217 | * preload any built-in definitions |
| 1218 | */ |
| 1219 | void x86_cpudef_setup(void) |
| 1220 | { |
| 1221 | int i; |
| 1222 | |
| 1223 | for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); ++i) { |
| 1224 | builtin_x86_defs[i].next = x86_defs; |
| 1225 | builtin_x86_defs[i].flags = 1; |
| 1226 | x86_defs = &builtin_x86_defs[i]; |
| 1227 | } |
| 1228 | #if !defined(CONFIG_USER_ONLY) |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 1229 | qemu_opts_foreach(qemu_find_opts("cpudef"), cpudef_register, NULL, 0); |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1230 | #endif |
| 1231 | } |
| 1232 | |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1233 | static void get_cpuid_vendor(CPUX86State *env, uint32_t *ebx, |
| 1234 | uint32_t *ecx, uint32_t *edx) |
| 1235 | { |
| 1236 | *ebx = env->cpuid_vendor1; |
| 1237 | *edx = env->cpuid_vendor2; |
| 1238 | *ecx = env->cpuid_vendor3; |
| 1239 | |
| 1240 | /* sysenter isn't supported on compatibility mode on AMD, syscall |
| 1241 | * isn't supported in compatibility mode on Intel. |
| 1242 | * Normally we advertise the actual cpu vendor, but you can override |
| 1243 | * this if you want to use KVM's sysenter/syscall emulation |
| 1244 | * in compatibility mode and when doing cross vendor migration |
| 1245 | */ |
Andre Przywara | 8935499 | 2010-06-02 11:57:47 +0200 | [diff] [blame] | 1246 | if (kvm_enabled() && ! env->cpuid_vendor_override) { |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1247 | host_cpuid(0, 0, NULL, ebx, ecx, edx); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, |
| 1252 | uint32_t *eax, uint32_t *ebx, |
| 1253 | uint32_t *ecx, uint32_t *edx) |
| 1254 | { |
| 1255 | /* test if maximum index reached */ |
| 1256 | if (index & 0x80000000) { |
brillywu@viatech.com.cn | b3baa15 | 2011-06-01 09:59:52 +0800 | [diff] [blame] | 1257 | if (index > env->cpuid_xlevel) { |
| 1258 | if (env->cpuid_xlevel2 > 0) { |
| 1259 | /* Handle the Centaur's CPUID instruction. */ |
| 1260 | if (index > env->cpuid_xlevel2) { |
| 1261 | index = env->cpuid_xlevel2; |
| 1262 | } else if (index < 0xC0000000) { |
| 1263 | index = env->cpuid_xlevel; |
| 1264 | } |
| 1265 | } else { |
| 1266 | index = env->cpuid_xlevel; |
| 1267 | } |
| 1268 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1269 | } else { |
| 1270 | if (index > env->cpuid_level) |
| 1271 | index = env->cpuid_level; |
| 1272 | } |
| 1273 | |
| 1274 | switch(index) { |
| 1275 | case 0: |
| 1276 | *eax = env->cpuid_level; |
| 1277 | get_cpuid_vendor(env, ebx, ecx, edx); |
| 1278 | break; |
| 1279 | case 1: |
| 1280 | *eax = env->cpuid_version; |
| 1281 | *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */ |
| 1282 | *ecx = env->cpuid_ext_features; |
| 1283 | *edx = env->cpuid_features; |
| 1284 | if (env->nr_cores * env->nr_threads > 1) { |
| 1285 | *ebx |= (env->nr_cores * env->nr_threads) << 16; |
| 1286 | *edx |= 1 << 28; /* HTT bit */ |
| 1287 | } |
| 1288 | break; |
| 1289 | case 2: |
| 1290 | /* cache info: needed for Pentium Pro compatibility */ |
| 1291 | *eax = 1; |
| 1292 | *ebx = 0; |
| 1293 | *ecx = 0; |
| 1294 | *edx = 0x2c307d; |
| 1295 | break; |
| 1296 | case 4: |
| 1297 | /* cache info: needed for Core compatibility */ |
| 1298 | if (env->nr_cores > 1) { |
Aurelien Jarno | 2f7a21c | 2010-03-13 16:46:33 +0100 | [diff] [blame] | 1299 | *eax = (env->nr_cores - 1) << 26; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1300 | } else { |
Aurelien Jarno | 2f7a21c | 2010-03-13 16:46:33 +0100 | [diff] [blame] | 1301 | *eax = 0; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1302 | } |
| 1303 | switch (count) { |
| 1304 | case 0: /* L1 dcache info */ |
| 1305 | *eax |= 0x0000121; |
| 1306 | *ebx = 0x1c0003f; |
| 1307 | *ecx = 0x000003f; |
| 1308 | *edx = 0x0000001; |
| 1309 | break; |
| 1310 | case 1: /* L1 icache info */ |
| 1311 | *eax |= 0x0000122; |
| 1312 | *ebx = 0x1c0003f; |
| 1313 | *ecx = 0x000003f; |
| 1314 | *edx = 0x0000001; |
| 1315 | break; |
| 1316 | case 2: /* L2 cache info */ |
| 1317 | *eax |= 0x0000143; |
| 1318 | if (env->nr_threads > 1) { |
| 1319 | *eax |= (env->nr_threads - 1) << 14; |
| 1320 | } |
| 1321 | *ebx = 0x3c0003f; |
| 1322 | *ecx = 0x0000fff; |
| 1323 | *edx = 0x0000001; |
| 1324 | break; |
| 1325 | default: /* end of info */ |
| 1326 | *eax = 0; |
| 1327 | *ebx = 0; |
| 1328 | *ecx = 0; |
| 1329 | *edx = 0; |
| 1330 | break; |
| 1331 | } |
| 1332 | break; |
| 1333 | case 5: |
| 1334 | /* mwait info: needed for Core compatibility */ |
| 1335 | *eax = 0; /* Smallest monitor-line size in bytes */ |
| 1336 | *ebx = 0; /* Largest monitor-line size in bytes */ |
| 1337 | *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE; |
| 1338 | *edx = 0; |
| 1339 | break; |
| 1340 | case 6: |
| 1341 | /* Thermal and Power Leaf */ |
| 1342 | *eax = 0; |
| 1343 | *ebx = 0; |
| 1344 | *ecx = 0; |
| 1345 | *edx = 0; |
| 1346 | break; |
Yang, Wei Y | f791168 | 2011-05-30 23:17:42 +0800 | [diff] [blame] | 1347 | case 7: |
| 1348 | if (kvm_enabled()) { |
Jan Kiszka | ba9bc59 | 2011-06-08 16:11:05 +0200 | [diff] [blame] | 1349 | KVMState *s = env->kvm_state; |
| 1350 | |
| 1351 | *eax = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EAX); |
| 1352 | *ebx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EBX); |
| 1353 | *ecx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_ECX); |
| 1354 | *edx = kvm_arch_get_supported_cpuid(s, 0x7, count, R_EDX); |
Yang, Wei Y | f791168 | 2011-05-30 23:17:42 +0800 | [diff] [blame] | 1355 | } else { |
| 1356 | *eax = 0; |
| 1357 | *ebx = 0; |
| 1358 | *ecx = 0; |
| 1359 | *edx = 0; |
| 1360 | } |
| 1361 | break; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1362 | case 9: |
| 1363 | /* Direct Cache Access Information Leaf */ |
| 1364 | *eax = 0; /* Bits 0-31 in DCA_CAP MSR */ |
| 1365 | *ebx = 0; |
| 1366 | *ecx = 0; |
| 1367 | *edx = 0; |
| 1368 | break; |
| 1369 | case 0xA: |
| 1370 | /* Architectural Performance Monitoring Leaf */ |
Gleb Natapov | a0fa820 | 2011-12-15 12:44:05 +0200 | [diff] [blame] | 1371 | if (kvm_enabled()) { |
| 1372 | KVMState *s = env->kvm_state; |
| 1373 | |
| 1374 | *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX); |
| 1375 | *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX); |
| 1376 | *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX); |
| 1377 | *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX); |
| 1378 | } else { |
| 1379 | *eax = 0; |
| 1380 | *ebx = 0; |
| 1381 | *ecx = 0; |
| 1382 | *edx = 0; |
| 1383 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1384 | break; |
Sheng Yang | 51e4943 | 2010-06-17 15:18:14 +0800 | [diff] [blame] | 1385 | case 0xD: |
| 1386 | /* Processor Extended State */ |
| 1387 | if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) { |
| 1388 | *eax = 0; |
| 1389 | *ebx = 0; |
| 1390 | *ecx = 0; |
| 1391 | *edx = 0; |
| 1392 | break; |
| 1393 | } |
| 1394 | if (kvm_enabled()) { |
Jan Kiszka | ba9bc59 | 2011-06-08 16:11:05 +0200 | [diff] [blame] | 1395 | KVMState *s = env->kvm_state; |
| 1396 | |
| 1397 | *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX); |
| 1398 | *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX); |
| 1399 | *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX); |
| 1400 | *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX); |
Sheng Yang | 51e4943 | 2010-06-17 15:18:14 +0800 | [diff] [blame] | 1401 | } else { |
| 1402 | *eax = 0; |
| 1403 | *ebx = 0; |
| 1404 | *ecx = 0; |
| 1405 | *edx = 0; |
| 1406 | } |
| 1407 | break; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1408 | case 0x80000000: |
| 1409 | *eax = env->cpuid_xlevel; |
| 1410 | *ebx = env->cpuid_vendor1; |
| 1411 | *edx = env->cpuid_vendor2; |
| 1412 | *ecx = env->cpuid_vendor3; |
| 1413 | break; |
| 1414 | case 0x80000001: |
| 1415 | *eax = env->cpuid_version; |
| 1416 | *ebx = 0; |
| 1417 | *ecx = env->cpuid_ext3_features; |
| 1418 | *edx = env->cpuid_ext2_features; |
| 1419 | |
| 1420 | /* The Linux kernel checks for the CMPLegacy bit and |
| 1421 | * discards multiple thread information if it is set. |
| 1422 | * So dont set it here for Intel to make Linux guests happy. |
| 1423 | */ |
| 1424 | if (env->nr_cores * env->nr_threads > 1) { |
| 1425 | uint32_t tebx, tecx, tedx; |
| 1426 | get_cpuid_vendor(env, &tebx, &tecx, &tedx); |
| 1427 | if (tebx != CPUID_VENDOR_INTEL_1 || |
| 1428 | tedx != CPUID_VENDOR_INTEL_2 || |
| 1429 | tecx != CPUID_VENDOR_INTEL_3) { |
| 1430 | *ecx |= 1 << 1; /* CmpLegacy bit */ |
| 1431 | } |
| 1432 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | case 0x80000002: |
| 1435 | case 0x80000003: |
| 1436 | case 0x80000004: |
| 1437 | *eax = env->cpuid_model[(index - 0x80000002) * 4 + 0]; |
| 1438 | *ebx = env->cpuid_model[(index - 0x80000002) * 4 + 1]; |
| 1439 | *ecx = env->cpuid_model[(index - 0x80000002) * 4 + 2]; |
| 1440 | *edx = env->cpuid_model[(index - 0x80000002) * 4 + 3]; |
| 1441 | break; |
| 1442 | case 0x80000005: |
| 1443 | /* cache info (L1 cache) */ |
| 1444 | *eax = 0x01ff01ff; |
| 1445 | *ebx = 0x01ff01ff; |
| 1446 | *ecx = 0x40020140; |
| 1447 | *edx = 0x40020140; |
| 1448 | break; |
| 1449 | case 0x80000006: |
| 1450 | /* cache info (L2 cache) */ |
| 1451 | *eax = 0; |
| 1452 | *ebx = 0x42004200; |
| 1453 | *ecx = 0x02008140; |
| 1454 | *edx = 0; |
| 1455 | break; |
| 1456 | case 0x80000008: |
| 1457 | /* virtual & phys address size in low 2 bytes. */ |
| 1458 | /* XXX: This value must match the one used in the MMU code. */ |
| 1459 | if (env->cpuid_ext2_features & CPUID_EXT2_LM) { |
| 1460 | /* 64 bit processor */ |
| 1461 | /* XXX: The physical address space is limited to 42 bits in exec.c. */ |
| 1462 | *eax = 0x00003028; /* 48 bits virtual, 40 bits physical */ |
| 1463 | } else { |
| 1464 | if (env->cpuid_features & CPUID_PSE36) |
| 1465 | *eax = 0x00000024; /* 36 bits physical */ |
| 1466 | else |
| 1467 | *eax = 0x00000020; /* 32 bits physical */ |
| 1468 | } |
| 1469 | *ebx = 0; |
| 1470 | *ecx = 0; |
| 1471 | *edx = 0; |
| 1472 | if (env->nr_cores * env->nr_threads > 1) { |
| 1473 | *ecx |= (env->nr_cores * env->nr_threads) - 1; |
| 1474 | } |
| 1475 | break; |
| 1476 | case 0x8000000A: |
Joerg Roedel | 296acb6 | 2010-09-27 15:16:17 +0200 | [diff] [blame] | 1477 | if (env->cpuid_ext3_features & CPUID_EXT3_SVM) { |
| 1478 | *eax = 0x00000001; /* SVM Revision */ |
| 1479 | *ebx = 0x00000010; /* nr of ASIDs */ |
| 1480 | *ecx = 0; |
| 1481 | *edx = env->cpuid_svm_features; /* optional features */ |
| 1482 | } else { |
| 1483 | *eax = 0; |
| 1484 | *ebx = 0; |
| 1485 | *ecx = 0; |
| 1486 | *edx = 0; |
| 1487 | } |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1488 | break; |
brillywu@viatech.com.cn | b3baa15 | 2011-06-01 09:59:52 +0800 | [diff] [blame] | 1489 | case 0xC0000000: |
| 1490 | *eax = env->cpuid_xlevel2; |
| 1491 | *ebx = 0; |
| 1492 | *ecx = 0; |
| 1493 | *edx = 0; |
| 1494 | break; |
| 1495 | case 0xC0000001: |
| 1496 | /* Support for VIA CPU's CPUID instruction */ |
| 1497 | *eax = env->cpuid_version; |
| 1498 | *ebx = 0; |
| 1499 | *ecx = 0; |
| 1500 | *edx = env->cpuid_ext4_features; |
| 1501 | break; |
| 1502 | case 0xC0000002: |
| 1503 | case 0xC0000003: |
| 1504 | case 0xC0000004: |
| 1505 | /* Reserved for the future, and now filled with zero */ |
| 1506 | *eax = 0; |
| 1507 | *ebx = 0; |
| 1508 | *ecx = 0; |
| 1509 | *edx = 0; |
| 1510 | break; |
Andre Przywara | c6dc6f6 | 2010-03-11 14:38:55 +0100 | [diff] [blame] | 1511 | default: |
| 1512 | /* reserved values: zero */ |
| 1513 | *eax = 0; |
| 1514 | *ebx = 0; |
| 1515 | *ecx = 0; |
| 1516 | *edx = 0; |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
Andreas Färber | 5fd2087 | 2012-04-02 23:20:08 +0200 | [diff] [blame] | 1520 | |
| 1521 | /* CPUClass::reset() */ |
| 1522 | static void x86_cpu_reset(CPUState *s) |
| 1523 | { |
| 1524 | X86CPU *cpu = X86_CPU(s); |
| 1525 | X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu); |
| 1526 | CPUX86State *env = &cpu->env; |
Andreas Färber | c1958ae | 2012-04-03 00:16:24 +0200 | [diff] [blame] | 1527 | int i; |
| 1528 | |
| 1529 | if (qemu_loglevel_mask(CPU_LOG_RESET)) { |
| 1530 | qemu_log("CPU Reset (CPU %d)\n", env->cpu_index); |
| 1531 | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
| 1532 | } |
Andreas Färber | 5fd2087 | 2012-04-02 23:20:08 +0200 | [diff] [blame] | 1533 | |
| 1534 | xcc->parent_reset(s); |
| 1535 | |
Andreas Färber | c1958ae | 2012-04-03 00:16:24 +0200 | [diff] [blame] | 1536 | |
| 1537 | memset(env, 0, offsetof(CPUX86State, breakpoints)); |
| 1538 | |
| 1539 | tlb_flush(env, 1); |
| 1540 | |
| 1541 | env->old_exception = -1; |
| 1542 | |
| 1543 | /* init to reset state */ |
| 1544 | |
| 1545 | #ifdef CONFIG_SOFTMMU |
| 1546 | env->hflags |= HF_SOFTMMU_MASK; |
| 1547 | #endif |
| 1548 | env->hflags2 |= HF2_GIF_MASK; |
| 1549 | |
| 1550 | cpu_x86_update_cr0(env, 0x60000010); |
| 1551 | env->a20_mask = ~0x0; |
| 1552 | env->smbase = 0x30000; |
| 1553 | |
| 1554 | env->idt.limit = 0xffff; |
| 1555 | env->gdt.limit = 0xffff; |
| 1556 | env->ldt.limit = 0xffff; |
| 1557 | env->ldt.flags = DESC_P_MASK | (2 << DESC_TYPE_SHIFT); |
| 1558 | env->tr.limit = 0xffff; |
| 1559 | env->tr.flags = DESC_P_MASK | (11 << DESC_TYPE_SHIFT); |
| 1560 | |
| 1561 | cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, |
| 1562 | DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | |
| 1563 | DESC_R_MASK | DESC_A_MASK); |
| 1564 | cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, |
| 1565 | DESC_P_MASK | DESC_S_MASK | DESC_W_MASK | |
| 1566 | DESC_A_MASK); |
| 1567 | cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, |
| 1568 | DESC_P_MASK | DESC_S_MASK | DESC_W_MASK | |
| 1569 | DESC_A_MASK); |
| 1570 | cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, |
| 1571 | DESC_P_MASK | DESC_S_MASK | DESC_W_MASK | |
| 1572 | DESC_A_MASK); |
| 1573 | cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, |
| 1574 | DESC_P_MASK | DESC_S_MASK | DESC_W_MASK | |
| 1575 | DESC_A_MASK); |
| 1576 | cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, |
| 1577 | DESC_P_MASK | DESC_S_MASK | DESC_W_MASK | |
| 1578 | DESC_A_MASK); |
| 1579 | |
| 1580 | env->eip = 0xfff0; |
| 1581 | env->regs[R_EDX] = env->cpuid_version; |
| 1582 | |
| 1583 | env->eflags = 0x2; |
| 1584 | |
| 1585 | /* FPU init */ |
| 1586 | for (i = 0; i < 8; i++) { |
| 1587 | env->fptags[i] = 1; |
| 1588 | } |
| 1589 | env->fpuc = 0x37f; |
| 1590 | |
| 1591 | env->mxcsr = 0x1f80; |
| 1592 | |
| 1593 | env->pat = 0x0007040600070406ULL; |
| 1594 | env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT; |
| 1595 | |
| 1596 | memset(env->dr, 0, sizeof(env->dr)); |
| 1597 | env->dr[6] = DR6_FIXED_1; |
| 1598 | env->dr[7] = DR7_FIXED_1; |
| 1599 | cpu_breakpoint_remove_all(env, BP_CPU); |
| 1600 | cpu_watchpoint_remove_all(env, BP_CPU); |
Andreas Färber | 5fd2087 | 2012-04-02 23:20:08 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Andreas Färber | de02481 | 2012-04-03 00:00:17 +0200 | [diff] [blame] | 1603 | static void mce_init(X86CPU *cpu) |
| 1604 | { |
| 1605 | CPUX86State *cenv = &cpu->env; |
| 1606 | unsigned int bank; |
| 1607 | |
| 1608 | if (((cenv->cpuid_version >> 8) & 0xf) >= 6 |
| 1609 | && (cenv->cpuid_features & (CPUID_MCE | CPUID_MCA)) == |
| 1610 | (CPUID_MCE | CPUID_MCA)) { |
| 1611 | cenv->mcg_cap = MCE_CAP_DEF | MCE_BANKS_DEF; |
| 1612 | cenv->mcg_ctl = ~(uint64_t)0; |
| 1613 | for (bank = 0; bank < MCE_BANKS_DEF; bank++) { |
| 1614 | cenv->mce_banks[bank * 4] = ~(uint64_t)0; |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | static void x86_cpu_initfn(Object *obj) |
| 1620 | { |
| 1621 | X86CPU *cpu = X86_CPU(obj); |
| 1622 | CPUX86State *env = &cpu->env; |
| 1623 | |
| 1624 | cpu_exec_init(env); |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1625 | |
| 1626 | object_property_add(obj, "family", "int", |
Andreas Färber | 95b8519 | 2012-04-17 14:42:22 +0200 | [diff] [blame] | 1627 | x86_cpuid_version_get_family, |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1628 | x86_cpuid_version_set_family, NULL, NULL, NULL); |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 1629 | object_property_add(obj, "model", "int", |
Andreas Färber | 67e30c8 | 2012-04-17 14:48:14 +0200 | [diff] [blame] | 1630 | x86_cpuid_version_get_model, |
Andreas Färber | c5291a4 | 2012-04-17 12:16:39 +0200 | [diff] [blame] | 1631 | x86_cpuid_version_set_model, NULL, NULL, NULL); |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 1632 | object_property_add(obj, "stepping", "int", |
Andreas Färber | 35112e4 | 2012-04-17 14:50:53 +0200 | [diff] [blame] | 1633 | x86_cpuid_version_get_stepping, |
Andreas Färber | 036e222 | 2012-04-17 14:14:18 +0200 | [diff] [blame] | 1634 | x86_cpuid_version_set_stepping, NULL, NULL, NULL); |
Andreas Färber | 8e1898b | 2012-04-17 18:41:40 +0200 | [diff] [blame^] | 1635 | object_property_add(obj, "level", "int", |
| 1636 | x86_cpuid_get_level, |
| 1637 | x86_cpuid_set_level, NULL, NULL, NULL); |
Andreas Färber | 938d4c2 | 2012-04-17 15:17:27 +0200 | [diff] [blame] | 1638 | object_property_add_str(obj, "model-id", |
Andreas Färber | 63e886e | 2012-04-17 23:02:26 +0200 | [diff] [blame] | 1639 | x86_cpuid_get_model_id, |
Andreas Färber | 938d4c2 | 2012-04-17 15:17:27 +0200 | [diff] [blame] | 1640 | x86_cpuid_set_model_id, NULL); |
Andreas Färber | 71ad61d | 2012-04-17 12:10:29 +0200 | [diff] [blame] | 1641 | |
Andreas Färber | de02481 | 2012-04-03 00:00:17 +0200 | [diff] [blame] | 1642 | env->cpuid_apic_id = env->cpu_index; |
| 1643 | mce_init(cpu); |
| 1644 | } |
| 1645 | |
Andreas Färber | 5fd2087 | 2012-04-02 23:20:08 +0200 | [diff] [blame] | 1646 | static void x86_cpu_common_class_init(ObjectClass *oc, void *data) |
| 1647 | { |
| 1648 | X86CPUClass *xcc = X86_CPU_CLASS(oc); |
| 1649 | CPUClass *cc = CPU_CLASS(oc); |
| 1650 | |
| 1651 | xcc->parent_reset = cc->reset; |
| 1652 | cc->reset = x86_cpu_reset; |
| 1653 | } |
| 1654 | |
| 1655 | static const TypeInfo x86_cpu_type_info = { |
| 1656 | .name = TYPE_X86_CPU, |
| 1657 | .parent = TYPE_CPU, |
| 1658 | .instance_size = sizeof(X86CPU), |
Andreas Färber | de02481 | 2012-04-03 00:00:17 +0200 | [diff] [blame] | 1659 | .instance_init = x86_cpu_initfn, |
Andreas Färber | 5fd2087 | 2012-04-02 23:20:08 +0200 | [diff] [blame] | 1660 | .abstract = false, |
| 1661 | .class_size = sizeof(X86CPUClass), |
| 1662 | .class_init = x86_cpu_common_class_init, |
| 1663 | }; |
| 1664 | |
| 1665 | static void x86_cpu_register_types(void) |
| 1666 | { |
| 1667 | type_register_static(&x86_cpu_type_info); |
| 1668 | } |
| 1669 | |
| 1670 | type_init(x86_cpu_register_types) |