Skip to contentSkip to conversation

target-i386: Increase CPUID level/xlevel/xlevel2 automatically

PATCH Applied Eduardo Habkost 2 participants 9 messages Sep 22, 2016 – Sep 27, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover target-i386: Increase CPUID level/xlevel/xlevel2 automatically

This series fixes the inconsistency between CPUID[7].EBX features and all the rest of the configurable CPU features. This ensures that level/xlevel/xlevel2 will be set to appropriate values depending on the set of features enabled in a CPU model or in the command-line.

Changes v1 -> v2:
* Coding style changes (split long lines)
* Simplify compat logic: instead of 3 max_level* fields and 2 cpuid_auto_level_* fields, add a single full_cpuid_auto_level boolean field
* If the user set level/xlevel/xlevel2 to 0 explicitly, do what was asked for (use UINT32_MAX as default value)

Eduardo Habkost (6):
target-i386: Remove unused X86CPUDefinition::xlevel2 field
target-i386: Add a marker to end of the region zeroed on reset
tests: Add test code for CPUID level/xlevel handling
tests: Test CPUID level handling for old machines
target-i386: Automatically set level/xlevel/xlevel2 when needed
target-i386: Enable CPUID[0x8000000A] if SVM is enabled

 include/hw/i386/pc.h          |   7 +-
 target-i386/cpu.c             |  92 +++++++++++++++++++----
 target-i386/cpu.h             |  13 +++-
 tests/Makefile.include        |   2 +
 tests/test-x86-cpuid-compat.c | 171 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 268 insertions(+), 17 deletions(-)
 create mode 100644 tests/test-x86-cpuid-compat.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/6 target-i386: Remove unused X86CPUDefinition::xlevel2 field

No CPU model in builtin_x86_defs has xlevel2 set, so it is always zero. Delete the field.

Note that this is not an user-visible change. It doesn't remove the ability to set xlevel2 on the command-line, it just removes an unused field in builtin_x86_defs.

---
 target-i386/cpu.c | 2 --
 1 file changed, 2 deletions(-)
target-i386/cpu.c +0 -2
index db12728..920b78f 100644
@@ -761,7 +761,6 @@ struct X86CPUDefinition {
     const char *name;
     uint32_t level;
     uint32_t xlevel;
-    uint32_t xlevel2;
     /* vendor is zero-terminated, 12 character ASCII string */
     char vendor[CPUID_VENDOR_SZ + 1];
     int family;
@@ -2214,7 +2213,6 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
     object_property_set_int(OBJECT(cpu), def->model, "model", errp);
     object_property_set_int(OBJECT(cpu), def->stepping, "stepping", errp);
     object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", errp);
-    object_property_set_int(OBJECT(cpu), def->xlevel2, "xlevel2", errp);
     object_property_set_str(OBJECT(cpu), def->model_id, "model-id", errp);
     for (w = 0; w < FEATURE_WORDS; w++) {
         env->features[w] = def->features[w];
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/6 target-i386: Add a marker to end of the region zeroed on reset

In reply to Eduardo Habkost

Instead of using cpuid_level, use an empty struct as a marker (like we already did with {start,end}_init_save). This will avoid accidentaly resetting the wrong fields if we change the field ordering on CPUX86State.

---
 target-i386/cpu.c | 2 +-
 target-i386/cpu.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
target-i386/cpu.c +1 -1
index 920b78f..26f0e59 100644
@@ -2714,7 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
     xcc->parent_reset(s);
 
-    memset(env, 0, offsetof(CPUX86State, cpuid_level));
+    memset(env, 0, offsetof(CPUX86State, end_reset_fields));
 
     tlb_flush(s, 1);
 
target-i386/cpu.h +1 -0
index 27af9c3..604d591 100644
@@ -1108,6 +1108,7 @@ typedef struct CPUX86State {
     CPU_COMMON
 
     /* Fields from here on are preserved across CPU reset. */
+    struct {} end_reset_fields;
 
     /* processor features (e.g. for CPUID insn) */
     uint32_t cpuid_level;
Signature · 1 line
2.7.4
#3 lore raw

PATCH v2 3/6 tests: Add test code for CPUID level/xlevel handling

In reply to Eduardo Habkost

Add test code that will check if the automatic CPUID level changes are working as expected.

---
Changes v1 -> v2:
* Coding style changes (split long lines)

---
 tests/Makefile.include        |   2 +
 tests/test-x86-cpuid-compat.c | 108 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 tests/test-x86-cpuid-compat.c
tests/Makefile.include +2 -0
index 6052a38..5a98962 100644
@@ -242,6 +242,7 @@ check-qtest-i386-y += tests/test-netfilter$(EXESUF)
 check-qtest-i386-y += tests/test-filter-mirror$(EXESUF)
 check-qtest-i386-y += tests/test-filter-redirector$(EXESUF)
 check-qtest-i386-y += tests/postcopy-test$(EXESUF)
+check-qtest-i386-y += tests/test-x86-cpuid-compat$(EXESUF)
 check-qtest-x86_64-y += $(check-qtest-i386-y)
 gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c
 gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -656,6 +657,7 @@ tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-o
 tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y)
 tests/test-filter-mirror$(EXESUF): tests/test-filter-mirror.o $(qtest-obj-y)
 tests/test-filter-redirector$(EXESUF): tests/test-filter-redirector.o $(qtest-obj-y)
+tests/test-x86-cpuid-compat$(EXESUF): tests/test-x86-cpuid-compat.o $(qtest-obj-y)
 tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o contrib/ivshmem-server/ivshmem-server.o $(libqos-pc-obj-y)
 tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o
 
tests/test-x86-cpuid-compat.c new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.7.4
#4 lore raw

PATCH v2 4/6 tests: Test CPUID level handling for old machines

In reply to Eduardo Habkost

We're going to change the way level/xlevel/xlevel2 are handled when enabling features, but we need to keep the old behavior on existing machine types. Add test cases for that.

---
Changes v1 -> v2:
* Coding style changes (split long lines)

---
 tests/test-x86-cpuid-compat.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
tests/test-x86-cpuid-compat.c +13 -0
index b81cfeb..f7003ee 100644
@@ -104,5 +104,18 @@ int main(int argc, char **argv)
                    "-cpu 486,xlevel2=0xC0000002,+xstore",
                    "xlevel2", 0xC0000002);
 
+    /* Check compatibility of old machine-types that didn't
+     * auto-increase level/xlevel/xlevel2: */
+
+    add_cpuid_test("x86/cpuid/auto-level/pc-2.7",
+                   "-machine pc-i440fx-2.7 -cpu 486,+arat,+avx512vbmi,+xsaveopt",
+                   "level", 1);
+    add_cpuid_test("x86/cpuid/auto-xlevel/pc-2.7",
+                   "-machine pc-i440fx-2.7 -cpu 486,+3dnow,+sse4a,+invtsc,+npt",
+                   "xlevel", 0);
+    add_cpuid_test("x86/cpuid/auto-xlevel2/pc-2.7",
+                   "-machine pc-i440fx-2.7 -cpu 486,+xstore",
+                   "xlevel2", 0);
+
     return g_test_run();
 }
Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 5/6 target-i386: Automatically set level/xlevel/xlevel2 when needed

In reply to Eduardo Habkost

Instead of requiring users and management software to be aware of required CPUID level/xlevel/xlevel2 values for each feature, automatically increase those values when features need them.

This was already done for CPUID[7].EBX, and is now made generic for all CPUID feature flags. Unit test included, to make sure we don't break ABI on older machine-types and don't mess with the CPUID level values if they are explicitly set by the user.

---
Changes v1 -> v2:
* Coding style changes (split long lines)
* Simplify compat logic: instead of 3 max_level* fields and 2 cpuid_auto_level_* fields, add a single full_cpuid_auto_level boolean field
* If the user set level/xlevel/xlevel2 to 0 explicitly, do what was asked for (use UINT32_MAX as default value)

---
 include/hw/i386/pc.h          |  7 +++-
 target-i386/cpu.c             | 84 +++++++++++++++++++++++++++++++++++++------
 target-i386/cpu.h             | 12 +++++--
 tests/test-x86-cpuid-compat.c | 45 +++++++++++++++++++++++
 4 files changed, 134 insertions(+), 14 deletions(-)
include/hw/i386/pc.h +6 -1
index ab8e319..0a5b16a 100644
@@ -376,7 +376,12 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
 
 #define PC_COMPAT_2_7 \
     PC_COMPAT_2_8 \
-    HW_COMPAT_2_7
+    HW_COMPAT_2_7 \
+    {\
+        .driver   = TYPE_X86_CPU,\
+        .property = "full-cpuid-auto-level",\
+        .value    = "off",\
+    },
 
 #define PC_COMPAT_2_6 \
     HW_COMPAT_2_6 \
target-i386/cpu.c +74 -10

117 lines not on this page · raw message

target-i386/cpu.h +9 -3
index 604d591..aaa45f0 100644
@@ -1111,9 +1111,12 @@ typedef struct CPUX86State {
     struct {} end_reset_fields;
 
     /* processor features (e.g. for CPUID insn) */
-    uint32_t cpuid_level;
-    uint32_t cpuid_xlevel;
-    uint32_t cpuid_xlevel2;
+    /* Minimum level/xlevel/xlevel2, based on CPU model + features */
+    uint32_t cpuid_min_level, cpuid_min_xlevel, cpuid_min_xlevel2;
+    /* Maximum level/xlevel/xlevel2 value for auto-assignment: */
+    uint32_t cpuid_max_level, cpuid_max_xlevel, cpuid_max_xlevel2;
+    /* Actual level/xlevel/xlevel2 value: */
+    uint32_t cpuid_level, cpuid_xlevel, cpuid_xlevel2;
     uint32_t cpuid_vendor1;
     uint32_t cpuid_vendor2;
     uint32_t cpuid_vendor3;
@@ -1218,6 +1221,9 @@ struct X86CPU {
     /* Compatibility bits for old machine types: */
     bool enable_cpuid_0xb;
 
+    /* Enable auto level-increase for all CPUID leaves */
+    bool full_cpuid_auto_level;
+
     /* if true fill the top bits of the MTRR_PHYSMASKn variable range */
     bool fill_mtrr_mask;
 
tests/test-x86-cpuid-compat.c +45 -0

64 lines not on this page · raw message

Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 6/6 target-i386: Enable CPUID[0x8000000A] if SVM is enabled

In reply to Eduardo Habkost

SVM needs CPUID[0x8000000A] to be available. So if SVM is enabled in a CPU model or explicitly in the command-line, adjust CPUID xlevel to expose the CPUID[0x8000000A] leaf.

---
Changes v1 -> v2:
* Coding style changes (split long lines)
* Refresh after cpuid_max_xlevel field was removed and full_cpuid_auto_level field was added

---
 target-i386/cpu.c             |  4 ++++
 tests/test-x86-cpuid-compat.c | 15 ++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)
target-i386/cpu.c +4 -0
index 24893d2..7a5da99 100644
@@ -3047,6 +3047,10 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         x86_cpu_adjust_feat_level(cpu, FEAT_C000_0001_EDX);
         x86_cpu_adjust_feat_level(cpu, FEAT_SVM);
         x86_cpu_adjust_feat_level(cpu, FEAT_XSAVE);
+        /* SVM requires CPUID[0x8000000A] */
+        if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
+            x86_cpu_adjust_level(cpu, &env->cpuid_min_xlevel, 0x8000000A);
+        }
     }
 
     /* Set cpuid_*level* based on cpuid_min_*level, if not explicitly set */
tests/test-x86-cpuid-compat.c +10 -5
index 83f1c80..83162a4 100644
@@ -83,6 +83,8 @@ int main(int argc, char **argv)
                    "-cpu core2duo", "xlevel", 0x80000008);
     add_cpuid_test("x86/cpuid/phenom/xlevel",
                    "-cpu phenom", "xlevel", 0x8000001A);
+    add_cpuid_test("x86/cpuid/athlon/xlevel",
+                   "-cpu athlon", "xlevel", 0x80000008);
 
     /* If level is not large enough, it should increase automatically: */
     /* CPUID[6].EAX: */
@@ -112,6 +114,9 @@ int main(int argc, char **argv)
     /* CPUID[C000_0001].EDX: */
     add_cpuid_test("x86/cpuid/auto-xlevel2/phenom/xstore",
                    "-cpu phenom,+xstore", "xlevel2", 0xC0000001);
+    /* SVM needs CPUID[0x8000000A] */
+    add_cpuid_test("x86/cpuid/auto-xlevel/athlon/svm",
+                   "-cpu athlon,+svm", "xlevel", 0x8000000A);
 
 
     /* If level is already large enough, it shouldn't change: */
@@ -131,17 +136,17 @@ int main(int argc, char **argv)
 
     /* if xlevel is already large enough, it shouldn't change: */
     add_cpuid_test("x86/cpuid/auto-xlevel/phenom/3dnow",
-                   "-cpu phenom,+3dnow,+sse4a,+invtsc,+npt",
+                   "-cpu phenom,+3dnow,+sse4a,+invtsc,+npt,+svm",
                    "xlevel", 0x8000001A);
     /* If xlevel is explicitly set, it shouldn't change: */
     add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/80000002",
-                   "-cpu 486,xlevel=0x80000002,+3dnow,+sse4a,+invtsc,+npt",
+                   "-cpu 486,xlevel=0x80000002,+3dnow,+sse4a,+invtsc,+npt,+svm",
                    "xlevel", 0x80000002);
     add_cpuid_test("x86/cpuid/auto-xlevel/486/fixed/8000001A",
-                   "-cpu 486,xlevel=0x8000001A,+3dnow,+sse4a,+invtsc,+npt",
+                   "-cpu 486,xlevel=0x8000001A,+3dnow,+sse4a,+invtsc,+npt,+svm",
                    "xlevel", 0x8000001A);
     add_cpuid_test("x86/cpuid/auto-xlevel/phenom/fixed/0",
-                   "-cpu 486,xlevel=0,+3dnow,+sse4a,+invtsc,+npt",
+                   "-cpu 486,xlevel=0,+3dnow,+sse4a,+invtsc,+npt,+svm",
                    "xlevel", 0);
 
     /* if xlevel2 is already large enough, it shouldn't change: */
@@ -156,7 +161,7 @@ int main(int argc, char **argv)
                    "-machine pc-i440fx-2.7 -cpu 486,+arat,+avx512vbmi,+xsaveopt",
                    "level", 1);
     add_cpuid_test("x86/cpuid/auto-xlevel/pc-2.7",
-                   "-machine pc-i440fx-2.7 -cpu 486,+3dnow,+sse4a,+invtsc,+npt",
+                   "-machine pc-i440fx-2.7 -cpu 486,+3dnow,+sse4a,+invtsc,+npt,+svm",
                    "xlevel", 0);
     add_cpuid_test("x86/cpuid/auto-xlevel2/pc-2.7",
                    "-machine pc-i440fx-2.7 -cpu 486,+xstore",
Signature · 1 line
2.7.4
Reviewed-by on the v2 series
#7 lore raw
In reply to Eduardo Habkost
Quoted 7 lines · Eduardo Habkost

Eduardo Habkost (6):
target-i386: Remove unused X86CPUDefinition::xlevel2 field
target-i386: Add a marker to end of the region zeroed on reset
tests: Add test code for CPUID level/xlevel handling
tests: Test CPUID level handling for old machines
target-i386: Automatically set level/xlevel/xlevel2 when needed
target-i386: Enable CPUID[0x8000000A] if SVM is enabled

r~

author Applied
#8 lore raw
In reply to Eduardo Habkost
Quoted 14 lines · Eduardo Habkost

This series fixes the inconsistency between CPUID[7].EBX features and all the rest of the configurable CPU features. This ensures that level/xlevel/xlevel2 will be set to appropriate values depending on the set of features enabled in a CPU model or in the command-line.

Changes v1 -> v2:
* Coding style changes (split long lines)
* Simplify compat logic: instead of 3 max_level* fields and 2 cpuid_auto_level_* fields, add a single full_cpuid_auto_level boolean field
* If the user set level/xlevel/xlevel2 to 0 explicitly, do what was asked for (use UINT32_MAX as default value)

Quoted 7 lines

Eduardo Habkost (6):
target-i386: Remove unused X86CPUDefinition::xlevel2 field
target-i386: Add a marker to end of the region zeroed on reset
tests: Add test code for CPUID level/xlevel handling
tests: Test CPUID level handling for old machines
target-i386: Automatically set level/xlevel/xlevel2 when needed
target-i386: Enable CPUID[0x8000000A] if SVM is enabled

Queued on x86-next.

Signature · 1 line
Eduardo