Skip to contentSkip to conversation

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

PATCH In review Eduardo Habkost 3 participants 8 messages Dec 18, 2012

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 target-i386/cpu.c: coding style fix

In reply to Eduardo Habkost
---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-i386/cpu.c +3 -2
index 2d4fc42..fe27def 100644
@@ -1789,10 +1789,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
 /* XXX: The physical address space is limited to 42 bits in exec.c. */
             *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.7.11.7
#6 lore raw

PATCH 2/3 target-i386/cpu.c: remove last TAB character from the source

In reply to Eduardo Habkost
---
 target-i386/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-i386/cpu.c +1 -1
index fe27def..6bfc8cc 100644
@@ -1787,7 +1787,7 @@ 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) {
                 *eax = 0x00000024; /* 36 bits physical */
Signature · 1 line
1.7.11.7
#7 lore raw

PATCH 3/3 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(), and CPU feature-bit property lookup/registration).

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;
* The fact that check_features_against_host() still doesn't check all feature words as it is supposed to.

---
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

---
 hw/kvm/clock.c            |   2 +-
 linux-user/elfload.c      |   2 +-
 linux-user/main.c         |   4 +-
 target-i386/cpu.c         | 581 ++++++++++++++++++++++++----------------------
 target-i386/cpu.h         |  30 +--
 target-i386/helper.c      |   4 +-
 target-i386/kvm.c         |   5 +-
 target-i386/misc_helper.c |  14 +-
 target-i386/translate.c   |  10 +-
 9 files changed, 335 insertions(+), 317 deletions(-)
hw/kvm/clock.c +1 -1
index 824b978..11af665 100644
@@ -129,7 +129,7 @@ static 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 1d8bcb4..71156af 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 25e35cd..5e847dd 100644
@@ -3625,13 +3625,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_80000001_EDX] & CPUID_EXT2_LM)) {
         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
         exit(1);
     }
target-i386/cpu.c +297 -284

907 lines not on this page · raw message

target-i386/cpu.h +17 -13
index fbbe730..5f3d272 100644
@@ -694,6 +694,21 @@ typedef enum TPRAccess {
     TPR_ACCESS_WRITE,
 } TPRAccess;
 
+typedef enum X86CPUFeatureWord {
+    FEAT_1_EDX,         /* CPUID[1].EDX */
+    FEAT_1_ECX,         /* CPUID[1].ECX */
+    FEAT_7_0_EBX,       /* CPUID[EAX=7,ECX=0].EBX */
+    FEAT_80000001_EDX,  /* CPUID[8000_0001].EDX */
+    FEAT_80000001_ECX,  /* CPUID[8000_0001].ECX */
+    FEAT_C0000001_EDX,  /* CPUID[C000_0001].EDX */
+    FEAT_KVM,           /* CPUID[4000_0001].EAX (KVM_CPUID_FEATURES) */
+    FEAT_SVM,           /* CPUID[8000_000A].EDX */
+    FEATURE_WORDS,      /* Size of the features arrays */
+} FeatureWord;
+
+/* An array of feature words */
+typedef uint32_t X86CPUFeatureWords[FEATURE_WORDS];
+
 typedef struct CPUX86State {
     /* standard registers */
     target_ulong regs[CPU_NB_REGS];
@@ -800,24 +815,15 @@ 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;
+    X86CPUFeatureWords 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;
     int cpuid_vendor_override;
-    /* 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];
@@ -831,8 +837,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 00341c5..7453d4b 100644
@@ -449,7 +449,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;
@@ -457,7 +457,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 f669281..5b614fa 100644
@@ -438,7 +438,7 @@ int kvm_arch_init_vcpu(CPUX86State *env)
     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);
@@ -573,7 +573,8 @@ int kvm_arch_init_vcpu(CPUX86State *env)
     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(env->kvm_state, KVM_CAP_MCE) > 0) {
         uint64_t mcg_cap;
         int banks;
target-i386/misc_helper.c +7 -7
index a020379..546b8b9 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_80000001_EDX] & CPUID_EXT2_SYSCALL) {
                 update_mask |= MSR_EFER_SCE;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_LM) {
+            if (env->features[FEAT_80000001_EDX] & CPUID_EXT2_LM) {
                 update_mask |= MSR_EFER_LME;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_FFXSR) {
+            if (env->features[FEAT_80000001_EDX] & CPUID_EXT2_FFXSR) {
                 update_mask |= MSR_EFER_FFXSR;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_NX) {
+            if (env->features[FEAT_80000001_EDX] & CPUID_EXT2_NX) {
                 update_mask |= MSR_EFER_NXE;
             }
-            if (env->cpuid_ext3_features & CPUID_EXT3_SVM) {
+            if (env->features[FEAT_80000001_ECX] & CPUID_EXT3_SVM) {
                 update_mask |= MSR_EFER_SVME;
             }
-            if (env->cpuid_ext2_features & CPUID_EXT2_FFXSR) {
+            if (env->features[FEAT_80000001_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 f394ea6..8ff4af9 100644
@@ -7926,11 +7926,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_80000001_EDX];
+    dc->cpuid_ext3_features = env->features[FEAT_80000001_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.7.11.7
#0 lore raw

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

I would like to give emphasis to the following, from description of patch 3:

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;
* The fact that check_features_against_host() still doesn't check all feature words as it is supposed to.

Changes in v3:
- Removed the EXT2_PPRO_FEATURES patch
- Rebased on top of:
- afaerber's qom-cpu-next (commit cb97b7f), plus
- Igor's "[PATCH 00/20 v2] x86 CPU cleanup (wave 2)"
(but only patches 1-6 and 12)
(See git tree below)
- I tried to choose the patches that I find likely to be included soon, to try to reduce the need to rebase this patch again, but I know I probably haven't guessed correctly and a new rebase is probably going to be necessary.
- Added "target-i386/cpu.c: remove last TAB character from the source"

Changes in v2:
- Rebased on top of:
Subject: [PATCH 0/2] target-i386: move CPU object creation to cpu.c (v2)
- Wrote a sed script that can be used to reproduce exactly the same changes from patch 3:
https://gist.github.com/4271991

Git tree for testing:
git://github.com/ehabkost/qemu-hacks.git x86-cpu-feature-array.v3
https://github.com/ehabkost/qemu-hacks/tree/x86-cpu-feature-array.v3

Original description:

I was planning to implement this only after we finished the rest of the work, the changes are a bit intrusive. But now it looks like the CPUID feature bits are getting into our way (e.g. the feature word array will allow us to simplify the -cpu host and -cpu check/enforce code a lot, making it easier to convert that code to use CPU subclasses), so I decided to submit it now.

Eduardo Habkost (3):
target-i386/cpu.c: coding style fix
target-i386/cpu.c: remove last TAB character from the source
target-i386: replace cpuid_*features fields with a feature word array

 hw/kvm/clock.c            |   2 +-
 linux-user/elfload.c      |   2 +-
 linux-user/main.c         |   4 +-
 target-i386/cpu.c         | 586 ++++++++++++++++++++++++----------------------
 target-i386/cpu.h         |  30 ++-
 target-i386/helper.c      |   4 +-
 target-i386/kvm.c         |   5 +-
 target-i386/misc_helper.c |  14 +-
 target-i386/translate.c   |  10 +-
 9 files changed, 338 insertions(+), 319 deletions(-)
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] target-i386/cpu.c: coding style fix

In reply to Eduardo Habkost
Quoted 10 lines · On Tue, 18 Dec 2012 14:29:21 -0200 Eduardo Habkost <[email protected]> wrote:
---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-i386/cpu.c +0 -0
index 2d4fc42..fe27def 100644
@@ -1789,10 +1789,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t
Quoted 2 lines

index, uint32_t count, /* XXX: The physical address space is limited to 42
bits in exec.c. */ *eax = 0x00003028; /* 48 bits virtual, 40 bits

^^ you forgot TAB here

Quoted 11 lines

physical */ } else {

target-i386/cpu.c +3 -2
-            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;
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] target-i386/cpu.c: coding style fix

Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Tue, 18 Dec 2012 14:29:21 -0200 Eduardo Habkost <[email protected]> wrote:
---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 7 lines
target-i386/cpu.c +0 -0
index 2d4fc42..fe27def 100644
@@ -1789,10 +1789,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t

index, uint32_t count, /* XXX: The physical address space is limited to 42
bits in exec.c. */ *eax = 0x00003028; /* 48 bits virtual, 40 bits

^^ you forgot TAB here

See patch 2/3. :-)

Signature · 1 line
Eduardo
#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] target-i386/cpu.c: coding style fix

Am 18.12.2012 18:03, schrieb Eduardo Habkost:

Quoted 18 lines · Igor Mammedov
Quoted 8 lines
Quoted 5 lines · On Tue, 18 Dec 2012 14:29:21 -0200 Eduardo Habkost <[email protected]> wrote:
---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 7 lines
target-i386/cpu.c +0 -0
index 2d4fc42..fe27def 100644
@@ -1789,10 +1789,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t

index, uint32_t count, /* XXX: The physical address space is limited to 42
bits in exec.c. */ *eax = 0x00003028; /* 48 bits virtual, 40 bits

^^ you forgot TAB here

Quoted 1 line

See patch 2/3. :-)

Can I just squash that into this one?

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
#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] target-i386/cpu.c: coding style fix

Quoted 19 lines · Andreas Färber

Am 18.12.2012 18:03, schrieb Eduardo Habkost:

Quoted 18 lines · Igor Mammedov
Quoted 16 lines
Quoted 10 lines · On Tue, 18 Dec 2012 14:29:21 -0200 Eduardo Habkost <[email protected]> wrote:
---
 target-i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-i386/cpu.c +0 -0
index 2d4fc42..fe27def 100644
@@ -1789,10 +1789,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t
Quoted 2 lines

index, uint32_t count, /* XXX: The physical address space is limited to 42
bits in exec.c. */ *eax = 0x00003028; /* 48 bits virtual, 40 bits

^^ you forgot TAB here

Quoted 3 lines
Quoted 1 line

See patch 2/3. :-)

Can I just squash that into this one?

Yes, sure.

I was keeping both changes separate on my git tree while working/rebasing the series, but I should have squashed them before submitting (as both are just "coding style fixes").

Signature · 1 line
Eduardo