Skip to contentSkip to conversation

replace cpuid_*features fields with a featue word array (v7)

PATCH Applied Eduardo Habkost 3 participants 8 messages Apr 11, 2013 – Apr 15, 2013

lore
Share & tools
lore
#0 lore raw

cover replace cpuid_*features fields with a featue word array (v7)

Yet Another rebase.

Changes v7:
- Rebase on top qom-cpu-next
(commit 3755f0a9d48da07258f4a0ef5e883272799e47b9)

Changes v6:
- Rebase on top of Andreas' qom-cpu tree (commit 9260944307077b93a66bf861a467107af986fe47)
- Break lines on kvm_check_features_against_host()
- Break the lines on builtin_x86_defs just after the "=". This way the feature lists stay on separate lines, this patch gets easier to review, and future patches that touches the code around builtin_x86_defs will be even easier to review (as they won't need to touch the lines containing the fature lists again)

Eduardo Habkost (2):
target-i386/cpu.c: Coding style fixes
target-i386: Replace cpuid_*features fields with a feature word array

 bsd-user/elfload.c        |   2 +-
 bsd-user/main.c           |   4 +-
 hw/i386/kvm/clock.c       |   2 +-
 linux-user/elfload.c      |   2 +-
 linux-user/main.c         |   4 +-
 target-i386/cpu.c         | 412 ++++++++++++++++++++++++++++------------------
 target-i386/cpu.h         |  15 +-
 target-i386/helper.c      |   4 +-
 target-i386/kvm.c         |   5 +-
 target-i386/misc_helper.c |  14 +-
 target-i386/translate.c   |  10 +-
 11 files changed, 274 insertions(+), 200 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/2 target-i386/cpu.c: Coding style fixes

* Add braces to 'if' statements;
* Remove last TAB character from the source.

---
 target-i386/cpu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target-i386/cpu.c +4 -3
index f1ccc72..c2e02fe 100644
@@ -1870,12 +1870,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
             /* 64 bit processor */
 /* XXX: The physical address space is limited to 42 bits in exec.c. */
-            *eax = 0x00003028;	/* 48 bits virtual, 40 bits physical */
+            *eax = 0x00003028;  /* 48 bits virtual, 40 bits physical */
         } else {
-            if (env->cpuid_features & CPUID_PSE36)
+            if (env->cpuid_features & CPUID_PSE36) {
                 *eax = 0x00000024; /* 36 bits physical */
-            else
+            } else {
                 *eax = 0x00000020; /* 32 bits physical */
+            }
         }
         *ebx = 0;
         *ecx = 0;
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH qom-cpu-next 1/2] target-i386/cpu.c: Coding style fixes

Quoted 13 lines · On Thu, 11 Apr 2013 17:07:23 -0300 Eduardo Habkost <[email protected]> wrote:

* Add braces to 'if' statements;
* Remove last TAB character from the source.

---
 target-i386/cpu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target-i386/cpu.c +0 -0
index f1ccc72..c2e02fe 100644

target-i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t · line 1874

Quoted 6 lines
target-i386/cpu.c +1 -1
@@ -1870,12 +1870,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t
 index, uint32_t count, if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
             /* 64 bit processor */
 /* XXX: The physical address space is limited to 42 bits in exec.c. */
-            *eax = 0x00003028;	/* 48 bits virtual, 40 bits physical
 */
+            *eax = 0x00003028;  /* 48 bits virtual, 40 bits physical */

^^ 2 spaces when in other places only one

Quoted 11 lines
target-i386/cpu.c +3 -2
         } else {
-            if (env->cpuid_features & CPUID_PSE36)
+            if (env->cpuid_features & CPUID_PSE36) {
                 *eax = 0x00000024; /* 36 bits physical */
-            else
+            } else {
                 *eax = 0x00000020; /* 32 bits physical */
+            }
         }
         *ebx = 0;
         *ecx = 0;

with fix above Reviewed-By: Igor Mammedov <[email protected]>

#3 lore raw

Re: [Qemu-devel] [PATCH qom-cpu-next 1/2] target-i386/cpu.c: Coding style fixes

Am 15.04.2013 10:27, schrieb Igor Mammedov:

Quoted 36 lines
Quoted 13 lines · On Thu, 11 Apr 2013 17:07:23 -0300 Eduardo Habkost <[email protected]> wrote:

* Add braces to 'if' statements;
* Remove last TAB character from the source.

---
 target-i386/cpu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target-i386/cpu.c +0 -0
index f1ccc72..c2e02fe 100644

target-i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t · line 1874

Quoted 6 lines
target-i386/cpu.c +1 -1
@@ -1870,12 +1870,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t
 index, uint32_t count, if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
             /* 64 bit processor */
 /* XXX: The physical address space is limited to 42 bits in exec.c. */
-            *eax = 0x00003028;	/* 48 bits virtual, 40 bits physical
 */
+            *eax = 0x00003028;  /* 48 bits virtual, 40 bits physical */

^^ 2 spaces when in other places only one

Quoted 11 lines
target-i386/cpu.c +3 -2
         } else {
-            if (env->cpuid_features & CPUID_PSE36)
+            if (env->cpuid_features & CPUID_PSE36) {
                 *eax = 0x00000024; /* 36 bits physical */
-            else
+            } else {
                 *eax = 0x00000020; /* 32 bits physical */
+            }
         }
         *ebx = 0;
         *ecx = 0;
Quoted 1 line

with fix above Reviewed-By: Igor Mammedov <[email protected]>

Thanks, applied to qom-cpu (with suggested change to spare resends):
https://github.com/afaerber/qemu-cpu/commits/qom-cpu

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw

PATCH 2/2 target-i386: Replace cpuid_*features fields with a feature word array

In reply to Eduardo Habkost

This replaces the feature-bit fields on both X86CPU and x86_def_t structs with an array.

With this, we will be able to simplify code that simply does the same operation on all feature words (e.g. kvm_check_features_against_host(), filter_features_for_kvm(), add_flagname_to_bitmaps(), CPU feature-bit property lookup/registration, and the proposed "feature-words" property)

This should also help avoid bugs like the ones introduced when we added cpuid_7_0_ebx_features. Today, adding a new feature word to the code requires chaning 5 or 6 different places in the code, and it's very easy to miss a problem when we forget to update one of those parts. See, for example:

* The bug solved by commit ffa8c11f0bbf47e1b7a3a62f97bc1da591c6734a; (CPUID 7 leaf was not being filtered based on host capabilities)
* The bug solved by commit 07ca59450c9a0c5df65665ce46aa8487af59a1dd (check/enforce flags were not checking all feature flags)

---
This patch was created solely using a sed script and no manual changes, to try to avoid mistakes while converting the code, and make it easier to rebase if necessary. The sed script can be seen at:
https://gist.github.com/4271991

Changes v7:
- Rebase on top qom-cpu-next
(commit 3755f0a9d48da07258f4a0ef5e883272799e47b9)

Changes v6:
- Rebase on top of Andreas' qom-cpu tree (commit 9260944307077b93a66bf861a467107af986fe47)
- Break lines on kvm_check_features_against_host()
- Break the lines on builtin_x86_defs just after the "=". This way the feature lists stay on separate lines, this patch gets easier to review, and future patches that touches the code around builtin_x86_defs will be even easier to review (as they won't need to touch the lines containing the fature lists again)

---
 bsd-user/elfload.c        |   2 +-
 bsd-user/main.c           |   4 +-
 hw/i386/kvm/clock.c       |   2 +-
 linux-user/elfload.c      |   2 +-
 linux-user/main.c         |   4 +-
 target-i386/cpu.c         | 407 ++++++++++++++++++++++++++++------------------
 target-i386/cpu.h         |  15 +-
 target-i386/helper.c      |   4 +-
 target-i386/kvm.c         |   5 +-
 target-i386/misc_helper.c |  14 +-
 target-i386/translate.c   |  10 +-
 11 files changed, 271 insertions(+), 198 deletions(-)
bsd-user/elfload.c +1 -1
index a6cd3ab..44e1568 100644
@@ -110,7 +110,7 @@ static const char *get_elf_platform(void)
 
 static uint32_t get_elf_hwcap(void)
 {
-  return thread_env->cpuid_features;
+  return thread_env->features[FEAT_1_EDX];
 }
 
 #ifdef TARGET_X86_64
bsd-user/main.c +2 -2
index cc84981..0da3ab9 100644
@@ -1004,13 +1004,13 @@ int main(int argc, char **argv)
 
     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
     env->hflags |= HF_PE_MASK;
-    if (env->cpuid_features & CPUID_SSE) {
+    if (env->features[FEAT_1_EDX] & CPUID_SSE) {
         env->cr[4] |= CR4_OSFXSR_MASK;
         env->hflags |= HF_OSFXSR_MASK;
     }
 #ifndef TARGET_ABI32
     /* enable 64 bit mode if possible */
-    if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
+    if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
         exit(1);
     }
hw/i386/kvm/clock.c +1 -1
index fa40e28..87d4d0f 100644
@@ -129,7 +129,7 @@ static const TypeInfo kvmclock_info = {
 void kvmclock_create(void)
 {
     if (kvm_enabled() &&
-        first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
+        first_cpu->features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
                                          (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
         sysbus_create_simple("kvmclock", -1, NULL);
     }
linux-user/elfload.c +1 -1
index 89db49c..04755de 100644
@@ -130,7 +130,7 @@ static const char *get_elf_platform(void)
 
 static uint32_t get_elf_hwcap(void)
 {
-    return thread_env->cpuid_features;
+    return thread_env->features[FEAT_1_EDX];
 }
 
 #ifdef TARGET_X86_64
linux-user/main.c +2 -2
index 4e92a0b..b97b8cf 100644
@@ -3764,13 +3764,13 @@ int main(int argc, char **argv, char **envp)
 
     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
     env->hflags |= HF_PE_MASK;
-    if (env->cpuid_features & CPUID_SSE) {
+    if (env->features[FEAT_1_EDX] & CPUID_SSE) {
         env->cr[4] |= CR4_OSFXSR_MASK;
         env->hflags |= HF_OSFXSR_MASK;
     }
 #ifndef TARGET_ABI32
     /* enable 64 bit mode if possible */
-    if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
+    if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
         exit(1);
     }
target-i386/cpu.c +245 -162

849 lines not on this page · raw message

target-i386/cpu.h +2 -13
index 2b4e319..299a793 100644
@@ -834,23 +834,14 @@ typedef struct CPUX86State {
     uint64_t pat;
 
     /* processor features (e.g. for CPUID insn) */
-    uint32_t cpuid_level;
+    uint32_t cpuid_level, cpuid_xlevel, cpuid_xlevel2;
+    FeatureWordArray features;
     uint32_t cpuid_vendor1;
     uint32_t cpuid_vendor2;
     uint32_t cpuid_vendor3;
     uint32_t cpuid_version;
-    uint32_t cpuid_features;
-    uint32_t cpuid_ext_features;
-    uint32_t cpuid_xlevel;
     uint32_t cpuid_model[12];
-    uint32_t cpuid_ext2_features;
-    uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
-    /* Store the results of Centaur's CPUID instructions */
-    uint32_t cpuid_xlevel2;
-    uint32_t cpuid_ext4_features;
-    /* Flags from CPUID[EAX=7,ECX=0].EBX */
-    uint32_t cpuid_7_0_ebx_features;
 
     /* MTRRs */
     uint64_t mtrr_fixed[11];
@@ -864,8 +855,6 @@ typedef struct CPUX86State {
     uint8_t soft_interrupt;
     uint8_t has_error_code;
     uint32_t sipi_vector;
-    uint32_t cpuid_kvm_features;
-    uint32_t cpuid_svm_features;
     bool tsc_valid;
     int tsc_khz;
     void *kvm_xsave_buf;
target-i386/helper.c +2 -2
index 282494f..158710a 100644
@@ -463,7 +463,7 @@ void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
         tlb_flush(env, 1);
     }
     /* SSE handling */
-    if (!(env->cpuid_features & CPUID_SSE)) {
+    if (!(env->features[FEAT_1_EDX] & CPUID_SSE)) {
         new_cr4 &= ~CR4_OSFXSR_MASK;
     }
     env->hflags &= ~HF_OSFXSR_MASK;
@@ -471,7 +471,7 @@ void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
         env->hflags |= HF_OSFXSR_MASK;
     }
 
-    if (!(env->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)) {
+    if (!(env->features[FEAT_7_0_EBX] & CPUID_7_0_EBX_SMAP)) {
         new_cr4 &= ~CR4_SMAP_MASK;
     }
     env->hflags &= ~HF_SMAP_MASK;
target-i386/kvm.c +3 -2
index 397afeb..b1df6a3 100644
@@ -453,7 +453,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     c = &cpuid_data.entries[cpuid_i++];
     memset(c, 0, sizeof(*c));
     c->function = KVM_CPUID_FEATURES;
-    c->eax = env->cpuid_kvm_features;
+    c->eax = env->features[FEAT_KVM];
 
     if (hyperv_enabled()) {
         memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
@@ -610,7 +610,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
     cpuid_data.cpuid.nent = cpuid_i;
 
     if (((env->cpuid_version >> 8)&0xF) >= 6
-        && (env->cpuid_features&(CPUID_MCE|CPUID_MCA)) == (CPUID_MCE|CPUID_MCA)
+        && (env->features[FEAT_1_EDX] & (CPUID_MCE|CPUID_MCA)) ==
+            (CPUID_MCE|CPUID_MCA)
         && kvm_check_extension(cs->kvm_state, KVM_CAP_MCE) > 0) {
         uint64_t mcg_cap;
         int banks;
target-i386/misc_helper.c +7 -7
index dfbc07b..ec834fc 100644
@@ -291,22 +291,22 @@ void helper_wrmsr(CPUX86State *env)
             uint64_t update_mask;
 
             update_mask = 0;
-            if (env->cpuid_ext2_features & CPUID_EXT2_SYSCALL) {
+            if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_SYSCALL) {
                 update_mask |= MSR_EFER_SCE;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
+            if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
                 update_mask |= MSR_EFER_LME;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_FFXSR) {
+            if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
                 update_mask |= MSR_EFER_FFXSR;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_NX) {
+            if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_NX) {
                 update_mask |= MSR_EFER_NXE;
             }
-            if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
+            if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
                 update_mask |= MSR_EFER_SVME;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_FFXSR) {
+            if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
                 update_mask |= MSR_EFER_FFXSR;
             }
             cpu_load_efer(env, (env->efer & ~update_mask) |
@@ -513,7 +513,7 @@ void helper_rdmsr(CPUX86State *env)
         val = env->mtrr_deftype;
         break;
     case MSR_MTRRcap:
-        if (env->cpuid_features & CPUID_MTRR) {
+        if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
             val = MSR_MTRRcap_VCNT | MSR_MTRRcap_FIXRANGE_SUPPORT |
                 MSR_MTRRcap_WC_SUPPORTED;
         } else {
target-i386/translate.c +5 -5
index 7596a90..d340aab 100644
@@ -8279,11 +8279,11 @@ static inline void gen_intermediate_code_internal(CPUX86State *env,
     if (flags & HF_SOFTMMU_MASK) {
         dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
     }
-    dc->cpuid_features = env->cpuid_features;
-    dc->cpuid_ext_features = env->cpuid_ext_features;
-    dc->cpuid_ext2_features = env->cpuid_ext2_features;
-    dc->cpuid_ext3_features = env->cpuid_ext3_features;
-    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
+    dc->cpuid_features = env->features[FEAT_1_EDX];
+    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
+    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
+    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
+    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
 #ifdef TARGET_X86_64
     dc->lma = (flags >> HF_LMA_SHIFT) & 1;
     dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
Signature · 1 line
1.8.1.4
#5 lore raw

Re: [Qemu-devel] [PATCH qom-cpu-next 2/2] target-i386: Replace cpuid_*features fields with a feature word array

Quoted 19 lines · On Thu, 11 Apr 2013 17:07:24 -0300 Eduardo Habkost <[email protected]> wrote:

This replaces the feature-bit fields on both X86CPU and x86_def_t structs with an array.

With this, we will be able to simplify code that simply does the same operation on all feature words (e.g. kvm_check_features_against_host(), filter_features_for_kvm(), add_flagname_to_bitmaps(), CPU feature-bit property lookup/registration, and the proposed "feature-words" property)

This should also help avoid bugs like the ones introduced when we added cpuid_7_0_ebx_features. Today, adding a new feature word to the code requires chaning 5 or 6 different places in the code, and it's very easy to miss a problem when we forget to update one of those parts. See, for example:

* The bug solved by commit ffa8c11f0bbf47e1b7a3a62f97bc1da591c6734a; (CPUID 7 leaf was not being filtered based on host capabilities)
* The bug solved by commit 07ca59450c9a0c5df65665ce46aa8487af59a1dd (check/enforce flags were not checking all feature flags)

Quoted 6 lines

---
This patch was created solely using a sed script and no manual changes, to try to avoid mistakes while converting the code, and make it easier to rebase if necessary. The sed script can be seen at:
https://gist.github.com/4271991

It doesn't apply anymore.

Quoted 4 lines

Changes v7:
- Rebase on top qom-cpu-next
(commit 3755f0a9d48da07258f4a0ef5e883272799e47b9)

commit IDs are often useless on staging tree, since they are changing, pls use commit's subj.

Quoted 12 lines

Changes v6:
- Rebase on top of Andreas' qom-cpu tree (commit 9260944307077b93a66bf861a467107af986fe47)
- Break lines on kvm_check_features_against_host()
- Break the lines on builtin_x86_defs just after the "=". This way the feature lists stay on separate lines, this patch gets easier to review, and future patches that touches the code around builtin_x86_defs will be even easier to review (as they won't need to touch the lines containing the fature lists again)

---

[...]

target-i386/cpu.c static void add_flagname_to_bitmaps(const char · line 351

Quoted 10 lines
target-i386/cpu.c +1 -1
index c2e02fe..e506d12 100644
@@ -348,21 +348,14 @@ static void add_flagname_to_bitmaps(const char
 *flagname, 
 typedef struct x86_def_t {
     const char *name;
-    uint32_t level;
+    uint32_t level, xlevel, xlevel2;

it's not cpuid_*features that patch re-factors, pls move to separate patch if this movement necessary at all. I don't see any gain in touch it.

Quoted 3 lines
target-i386/cpu.c +1 -0
+    FeatureWordArray features;
     /* vendor is zero-terminated, 12 character ASCII string */
     char vendor[CPUID_VENDOR_SZ + 1];

[...]

target-i386/cpu.h typedef struct CPUX86State { · line 837

Quoted 10 lines
target-i386/cpu.h +1 -1
index 2b4e319..299a793 100644
@@ -834,23 +834,14 @@ typedef struct CPUX86State {
     uint64_t pat;
 
     /* processor features (e.g. for CPUID insn) */
-    uint32_t cpuid_level;
+    uint32_t cpuid_level, cpuid_xlevel, cpuid_xlevel2;ditto

ditto

Quoted 29 lines
target-i386/cpu.h +1 -12
+    FeatureWordArray features;
     uint32_t cpuid_vendor1;
     uint32_t cpuid_vendor2;
     uint32_t cpuid_vendor3;
     uint32_t cpuid_version;
-    uint32_t cpuid_features;
-    uint32_t cpuid_ext_features;
-    uint32_t cpuid_xlevel;
     uint32_t cpuid_model[12];
-    uint32_t cpuid_ext2_features;
-    uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
-    /* Store the results of Centaur's CPUID instructions */
-    uint32_t cpuid_xlevel2;
-    uint32_t cpuid_ext4_features;
-    /* Flags from CPUID[EAX=7,ECX=0].EBX */
-    uint32_t cpuid_7_0_ebx_features;
 
     /* MTRRs */
     uint64_t mtrr_fixed[11];
@@ -864,8 +855,6 @@ typedef struct CPUX86State {
     uint8_t soft_interrupt;
     uint8_t has_error_code;
     uint32_t sipi_vector;
-    uint32_t cpuid_kvm_features;
-    uint32_t cpuid_svm_features;
     bool tsc_valid;
     int tsc_khz;
     void *kvm_xsave_buf;

[...]

Quoted 8 lines
target-i386/translate.c +0 -0
index 7596a90..d340aab 100644
@@ -8279,11 +8279,11 @@ static inline void

gen_intermediate_code_internal(CPUX86State *env, if (flags &

HF_SOFTMMU_MASK) { dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
     }

target-i386/translate.c static inline void gen_intermediate_code_internal(CPUX86State *env, · line 8286

Quoted 10 lines
target-i386/translate.c +5 -5
-    dc->cpuid_features = env->cpuid_features;
-    dc->cpuid_ext_features = env->cpuid_ext_features;
-    dc->cpuid_ext2_features = env->cpuid_ext2_features;
-    dc->cpuid_ext3_features = env->cpuid_ext3_features;
-    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
+    dc->cpuid_features = env->features[FEAT_1_EDX];
+    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
+    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
+    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
+    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];

why leaving cpuid_*features here, it's not much extra code on the first glance, so a consistent re-factoring would be justified.

Quoted 3 lines
 #ifdef TARGET_X86_64
     dc->lma = (flags >> HF_LMA_SHIFT) & 1;
     dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#6 lore raw

Re: [Qemu-devel] [PATCH qom-cpu-next 2/2] target-i386: Replace cpuid_*features fields with a feature word array

Quoted 22 lines · Igor Mammedov
Quoted 19 lines · On Thu, 11 Apr 2013 17:07:24 -0300 Eduardo Habkost <[email protected]> wrote:

This replaces the feature-bit fields on both X86CPU and x86_def_t structs with an array.

With this, we will be able to simplify code that simply does the same operation on all feature words (e.g. kvm_check_features_against_host(), filter_features_for_kvm(), add_flagname_to_bitmaps(), CPU feature-bit property lookup/registration, and the proposed "feature-words" property)

This should also help avoid bugs like the ones introduced when we added cpuid_7_0_ebx_features. Today, adding a new feature word to the code requires chaning 5 or 6 different places in the code, and it's very easy to miss a problem when we forget to update one of those parts. See, for example:

* The bug solved by commit ffa8c11f0bbf47e1b7a3a62f97bc1da591c6734a; (CPUID 7 leaf was not being filtered based on host capabilities)
* The bug solved by commit 07ca59450c9a0c5df65665ce46aa8487af59a1dd (check/enforce flags were not checking all feature flags)

Quoted 7 lines
Quoted 6 lines

---
This patch was created solely using a sed script and no manual changes, to try to avoid mistakes while converting the code, and make it easier to rebase if necessary. The sed script can be seen at:
https://gist.github.com/4271991

It doesn't apply anymore.

I'll redo and resend. Thanks.

Quoted 6 lines
Quoted 4 lines

Changes v7:
- Rebase on top qom-cpu-next
(commit 3755f0a9d48da07258f4a0ef5e883272799e47b9)

commit IDs are often useless on staging tree, since they are changing, pls use commit's subj.

I'll do it next time.

Quoted 14 lines
Quoted 13 lines

Changes v6:
- Rebase on top of Andreas' qom-cpu tree (commit 9260944307077b93a66bf861a467107af986fe47)
- Break lines on kvm_check_features_against_host()
- Break the lines on builtin_x86_defs just after the "=". This way the feature lists stay on separate lines, this patch gets easier to review, and future patches that touches the code around builtin_x86_defs will be even easier to review (as they won't need to touch the lines containing the fature lists again)

---

[...]

Quoted 12 lines

target-i386/cpu.c static void add_flagname_to_bitmaps(const char · line 351

Quoted 10 lines
target-i386/cpu.c +1 -1
index c2e02fe..e506d12 100644
@@ -348,21 +348,14 @@ static void add_flagname_to_bitmaps(const char
 *flagname, 
 typedef struct x86_def_t {
     const char *name;
-    uint32_t level;
+    uint32_t level, xlevel, xlevel2;

it's not cpuid_*features that patch re-factors, pls move to separate patch if this movement necessary at all. I don't see any gain in touch it.

Before applying this patch, xlevel was close to cpuid_ext[23]_features and xlevel2 was close to cpuid_ext4_features. Leaving them at completely random places in the struct after removing the cpuid_*_features field looks confusing to me.

At the same time, moving them without removing/moving the cpuid_*_features at the same time wouldn't make sense to me either. So I decided to move them in the same patch.

Quoted 59 lines
Quoted 4 lines
target-i386/cpu.c +1 -0
+    FeatureWordArray features;
     /* vendor is zero-terminated, 12 character ASCII string */
     char vendor[CPUID_VENDOR_SZ + 1];

[...]

target-i386/cpu.h typedef struct CPUX86State { · line 837

Quoted 10 lines
target-i386/cpu.h +1 -1
index 2b4e319..299a793 100644
@@ -834,23 +834,14 @@ typedef struct CPUX86State {
     uint64_t pat;
 
     /* processor features (e.g. for CPUID insn) */
-    uint32_t cpuid_level;
+    uint32_t cpuid_level, cpuid_xlevel, cpuid_xlevel2;ditto

ditto

Quoted 29 lines
target-i386/cpu.h +1 -12
+    FeatureWordArray features;
     uint32_t cpuid_vendor1;
     uint32_t cpuid_vendor2;
     uint32_t cpuid_vendor3;
     uint32_t cpuid_version;
-    uint32_t cpuid_features;
-    uint32_t cpuid_ext_features;
-    uint32_t cpuid_xlevel;
     uint32_t cpuid_model[12];
-    uint32_t cpuid_ext2_features;
-    uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
-    /* Store the results of Centaur's CPUID instructions */
-    uint32_t cpuid_xlevel2;
-    uint32_t cpuid_ext4_features;
-    /* Flags from CPUID[EAX=7,ECX=0].EBX */
-    uint32_t cpuid_7_0_ebx_features;
 
     /* MTRRs */
     uint64_t mtrr_fixed[11];
@@ -864,8 +855,6 @@ typedef struct CPUX86State {
     uint8_t soft_interrupt;
     uint8_t has_error_code;
     uint32_t sipi_vector;
-    uint32_t cpuid_kvm_features;
-    uint32_t cpuid_svm_features;
     bool tsc_valid;
     int tsc_khz;
     void *kvm_xsave_buf;

[...]

Quoted 12 lines
target-i386/translate.c +0 -0
index 7596a90..d340aab 100644
@@ -8279,11 +8279,11 @@ static inline void

gen_intermediate_code_internal(CPUX86State *env, if (flags &

HF_SOFTMMU_MASK) { dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
     }
target-i386/translate.c +0 -4
-    dc->cpuid_features = env->cpuid_features;
-    dc->cpuid_ext_features = env->cpuid_ext_features;
-    dc->cpuid_ext2_features = env->cpuid_ext2_features;
-    dc->cpuid_ext3_features = env->cpuid_ext3_features;
Quoted 8 lines

target-i386/translate.c static inline void gen_intermediate_code_internal(CPUX86State *env, · line 8286

Quoted 6 lines
target-i386/translate.c +5 -1
-    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
+    dc->cpuid_features = env->features[FEAT_1_EDX];
+    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
+    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
+    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
+    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];

why leaving cpuid_*features here, it's not much extra code on the first glance, so a consistent re-factoring would be justified.

struct DisasContext doesn't have all feature words (SVM, KVM, and C000_0001_EDX are not present), so it doesn't need the full feature array. I also don't know if making the struct larger would impact TCG performance, so I prefer to not touch that part of the TCG code myself.

Quoted 5 lines
Quoted 4 lines
 #ifdef TARGET_X86_64
     dc->lma = (flags >> HF_LMA_SHIFT) & 1;
     dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
Signature · 1 line
Eduardo
#7 lore raw

Re: [Qemu-devel] [PATCH qom-cpu-next 2/2] target-i386: Replace cpuid_*features fields with a feature word array

Quoted 70 lines · On Mon, 15 Apr 2013 10:40:36 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 22 lines · Igor Mammedov
Quoted 19 lines · On Thu, 11 Apr 2013 17:07:24 -0300 Eduardo Habkost <[email protected]> wrote:

This replaces the feature-bit fields on both X86CPU and x86_def_t structs with an array.

With this, we will be able to simplify code that simply does the same operation on all feature words (e.g. kvm_check_features_against_host(), filter_features_for_kvm(), add_flagname_to_bitmaps(), CPU feature-bit property lookup/registration, and the proposed "feature-words" property)

This should also help avoid bugs like the ones introduced when we added cpuid_7_0_ebx_features. Today, adding a new feature word to the code requires chaning 5 or 6 different places in the code, and it's very easy to miss a problem when we forget to update one of those parts. See, for example:

* The bug solved by commit ffa8c11f0bbf47e1b7a3a62f97bc1da591c6734a; (CPUID 7 leaf was not being filtered based on host capabilities)
* The bug solved by commit 07ca59450c9a0c5df65665ce46aa8487af59a1dd (check/enforce flags were not checking all feature flags)

Quoted 7 lines
Quoted 6 lines

---
This patch was created solely using a sed script and no manual changes, to try to avoid mistakes while converting the code, and make it easier to rebase if necessary. The sed script can be seen at:
https://gist.github.com/4271991

It doesn't apply anymore.

I'll redo and resend. Thanks.

Quoted 6 lines
Quoted 4 lines

Changes v7:
- Rebase on top qom-cpu-next
(commit 3755f0a9d48da07258f4a0ef5e883272799e47b9)

commit IDs are often useless on staging tree, since they are changing, pls use commit's subj.

I'll do it next time.

Quoted 14 lines
Quoted 13 lines

Changes v6:
- Rebase on top of Andreas' qom-cpu tree (commit 9260944307077b93a66bf861a467107af986fe47)
- Break lines on kvm_check_features_against_host()
- Break the lines on builtin_x86_defs just after the "=". This way the feature lists stay on separate lines, this patch gets easier to review, and future patches that touches the code around builtin_x86_defs will be even easier to review (as they won't need to touch the lines containing the fature lists again)

---

[...]

Quoted 12 lines

target-i386/cpu.c static void add_flagname_to_bitmaps(const char · line 351

Quoted 10 lines
target-i386/cpu.c +1 -1
index c2e02fe..e506d12 100644
@@ -348,21 +348,14 @@ static void add_flagname_to_bitmaps(const char
 *flagname, 
 typedef struct x86_def_t {
     const char *name;
-    uint32_t level;
+    uint32_t level, xlevel, xlevel2;

it's not cpuid_*features that patch re-factors, pls move to separate patch if this movement necessary at all. I don't see any gain in touch it.

Quoted 4 lines

Before applying this patch, xlevel was close to cpuid_ext[23]_features and xlevel2 was close to cpuid_ext4_features. Leaving them at completely random places in the struct after removing the cpuid_*_features field looks confusing to me.

then do it ask separate clean up patch.
and it would be nicer with one field per line, instead of ^^^.

Quoted 73 lines

At the same time, moving them without removing/moving the cpuid_*_features at the same time wouldn't make sense to me either. So I decided to move them in the same patch.

Quoted 59 lines
Quoted 4 lines
target-i386/cpu.c +1 -0
+    FeatureWordArray features;
     /* vendor is zero-terminated, 12 character ASCII string */
     char vendor[CPUID_VENDOR_SZ + 1];

[...]

target-i386/cpu.h typedef struct CPUX86State { · line 837

Quoted 10 lines
target-i386/cpu.h +1 -1
index 2b4e319..299a793 100644
@@ -834,23 +834,14 @@ typedef struct CPUX86State {
     uint64_t pat;
 
     /* processor features (e.g. for CPUID insn) */
-    uint32_t cpuid_level;
+    uint32_t cpuid_level, cpuid_xlevel, cpuid_xlevel2;ditto

ditto

Quoted 29 lines
target-i386/cpu.h +1 -12
+    FeatureWordArray features;
     uint32_t cpuid_vendor1;
     uint32_t cpuid_vendor2;
     uint32_t cpuid_vendor3;
     uint32_t cpuid_version;
-    uint32_t cpuid_features;
-    uint32_t cpuid_ext_features;
-    uint32_t cpuid_xlevel;
     uint32_t cpuid_model[12];
-    uint32_t cpuid_ext2_features;
-    uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
-    /* Store the results of Centaur's CPUID instructions */
-    uint32_t cpuid_xlevel2;
-    uint32_t cpuid_ext4_features;
-    /* Flags from CPUID[EAX=7,ECX=0].EBX */
-    uint32_t cpuid_7_0_ebx_features;
 
     /* MTRRs */
     uint64_t mtrr_fixed[11];
@@ -864,8 +855,6 @@ typedef struct CPUX86State {
     uint8_t soft_interrupt;
     uint8_t has_error_code;
     uint32_t sipi_vector;
-    uint32_t cpuid_kvm_features;
-    uint32_t cpuid_svm_features;
     bool tsc_valid;
     int tsc_khz;
     void *kvm_xsave_buf;

[...]

Quoted 12 lines
target-i386/translate.c +0 -0
index 7596a90..d340aab 100644
@@ -8279,11 +8279,11 @@ static inline void

gen_intermediate_code_internal(CPUX86State *env, if (flags &

HF_SOFTMMU_MASK) { dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
     }
target-i386/translate.c +0 -4
-    dc->cpuid_features = env->cpuid_features;
-    dc->cpuid_ext_features = env->cpuid_ext_features;
-    dc->cpuid_ext2_features = env->cpuid_ext2_features;
-    dc->cpuid_ext3_features = env->cpuid_ext3_features;
Quoted 8 lines

target-i386/translate.c static inline void gen_intermediate_code_internal(CPUX86State *env, · line 8286

Quoted 6 lines
target-i386/translate.c +5 -1
-    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
+    dc->cpuid_features = env->features[FEAT_1_EDX];
+    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
+    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
+    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
+    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];

why leaving cpuid_*features here, it's not much extra code on the first glance, so a consistent re-factoring would be justified.

Quoted 4 lines

struct DisasContext doesn't have all feature words (SVM, KVM, and C000_0001_EDX are not present), so it doesn't need the full feature array. I also don't know if making the struct larger would impact TCG performance, so I prefer to not touch that part of the TCG code myself.

agreed

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

 #ifdef TARGET_X86_64
     dc->lma = (flags >> HF_LMA_SHIFT) & 1;
     dc->code64 = (flags >> HF_CS64_SHIFT) & 1;