Skip to contentSkip to conversation

target-i386: clear bsp bit when designating bsp

Qemu-devel Nadav Amit 5 participants 18 messages Apr 1, 2015 – Apr 7, 2015

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

Since the BSP bit is writable on real hardware, during reset all the CPUs which were not chosen to be the BSP should have their BSP bit cleared. This fix is required for KVM to work correctly when it changes the BSP bit.

An additional fix is required for QEMU tcg to allow software to change the BSP bit.

---
 hw/intc/apic_common.c  | 8 ++++++--
 include/hw/i386/apic.h | 2 +-
 target-i386/cpu.c      | 4 +---
 3 files changed, 8 insertions(+), 6 deletions(-)
hw/intc/apic_common.c +6 -2
index 0858b45..042e960 100644
@@ -215,14 +215,18 @@ void apic_init_reset(DeviceState *dev)
     }
 }
 
-void apic_designate_bsp(DeviceState *dev)
+void apic_designate_bsp(DeviceState *dev, bool bsp)
 {
     if (dev == NULL) {
         return;
     }
 
     APICCommonState *s = APIC_COMMON(dev);
-    s->apicbase |= MSR_IA32_APICBASE_BSP;
+    if (bsp) {
+        s->apicbase |= MSR_IA32_APICBASE_BSP;
+    } else {
+        s->apicbase &= ~MSR_IA32_APICBASE_BSP;
+    }
 }
 
 static void apic_reset_common(DeviceState *dev)
include/hw/i386/apic.h +1 -1
index 1d48e02..51eb6d3 100644
@@ -21,7 +21,7 @@ void apic_sipi(DeviceState *s);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
 void apic_poll_irq(DeviceState *d);
-void apic_designate_bsp(DeviceState *d);
+void apic_designate_bsp(DeviceState *d, bool bsp);
 
 /* pc.c */
 DeviceState *cpu_get_current_apic(void);
target-i386/cpu.c +1 -3
index b2d1c95..03b33cf 100644
@@ -2714,9 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
 #if !defined(CONFIG_USER_ONLY)
     /* We hard-wire the BSP to the first CPU. */
-    if (s->cpu_index == 0) {
-        apic_designate_bsp(cpu->apic_state);
-    }
+    apic_designate_bsp(cpu->apic_state, s->cpu_index == 0);
 
     s->halted = !cpu_is_bsp(cpu);
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 58 lines · Nadav Amit

Since the BSP bit is writable on real hardware, during reset all the CPUs which were not chosen to be the BSP should have their BSP bit cleared. This fix is required for KVM to work correctly when it changes the BSP bit.

An additional fix is required for QEMU tcg to allow software to change the BSP bit.

---
 hw/intc/apic_common.c  | 8 ++++++--
 include/hw/i386/apic.h | 2 +-
 target-i386/cpu.c      | 4 +---
 3 files changed, 8 insertions(+), 6 deletions(-)
hw/intc/apic_common.c +6 -2
index 0858b45..042e960 100644
@@ -215,14 +215,18 @@ void apic_init_reset(DeviceState *dev)
     }
 }
 
-void apic_designate_bsp(DeviceState *dev)
+void apic_designate_bsp(DeviceState *dev, bool bsp)
 {
     if (dev == NULL) {
         return;
     }
 
     APICCommonState *s = APIC_COMMON(dev);
-    s->apicbase |= MSR_IA32_APICBASE_BSP;
+    if (bsp) {
+        s->apicbase |= MSR_IA32_APICBASE_BSP;
+    } else {
+        s->apicbase &= ~MSR_IA32_APICBASE_BSP;
+    }
 }
 
 static void apic_reset_common(DeviceState *dev)
include/hw/i386/apic.h +1 -1
index 1d48e02..51eb6d3 100644
@@ -21,7 +21,7 @@ void apic_sipi(DeviceState *s);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
 void apic_poll_irq(DeviceState *d);
-void apic_designate_bsp(DeviceState *d);
+void apic_designate_bsp(DeviceState *d, bool bsp);
 
 /* pc.c */
 DeviceState *cpu_get_current_apic(void);
target-i386/cpu.c +0 -0
index b2d1c95..03b33cf 100644

target-i386/cpu.c static void x86_cpu_reset(CPUState *s) · line 2720

Quoted 10 lines
target-i386/cpu.c +1 -3
@@ -2714,9 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
 #if !defined(CONFIG_USER_ONLY)
     /* We hard-wire the BSP to the first CPU. */
-    if (s->cpu_index == 0) {
-        apic_designate_bsp(cpu->apic_state);
-    }
+    apic_designate_bsp(cpu->apic_state, s->cpu_index == 0);
 
     s->halted = !cpu_is_bsp(cpu);
 

Thanks, applied locally.

Paolo

#2 lore raw

+Eduardo as target-i386 maintainer

Am 02.04.2015 um 14:34 schrieb Paolo Bonzini:

Quoted 68 lines · Nadav Amit
Quoted 65 lines

Since the BSP bit is writable on real hardware, during reset all the CPUs which were not chosen to be the BSP should have their BSP bit cleared. This fix is required for KVM to work correctly when it changes the BSP bit.

An additional fix is required for QEMU tcg to allow software to change the BSP bit.

---
 hw/intc/apic_common.c  | 8 ++++++--
 include/hw/i386/apic.h | 2 +-
 target-i386/cpu.c      | 4 +---
 3 files changed, 8 insertions(+), 6 deletions(-)
hw/intc/apic_common.c +6 -2
index 0858b45..042e960 100644
@@ -215,14 +215,18 @@ void apic_init_reset(DeviceState *dev)
     }
 }
 
-void apic_designate_bsp(DeviceState *dev)
+void apic_designate_bsp(DeviceState *dev, bool bsp)
 {
     if (dev == NULL) {
         return;
     }
 
     APICCommonState *s = APIC_COMMON(dev);
-    s->apicbase |= MSR_IA32_APICBASE_BSP;
+    if (bsp) {
+        s->apicbase |= MSR_IA32_APICBASE_BSP;
+    } else {
+        s->apicbase &= ~MSR_IA32_APICBASE_BSP;
+    }
 }
 
 static void apic_reset_common(DeviceState *dev)
include/hw/i386/apic.h +1 -1
index 1d48e02..51eb6d3 100644
@@ -21,7 +21,7 @@ void apic_sipi(DeviceState *s);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
 void apic_poll_irq(DeviceState *d);
-void apic_designate_bsp(DeviceState *d);
+void apic_designate_bsp(DeviceState *d, bool bsp);
 
 /* pc.c */
 DeviceState *cpu_get_current_apic(void);
target-i386/cpu.c +1 -3
index b2d1c95..03b33cf 100644
@@ -2714,9 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
 #if !defined(CONFIG_USER_ONLY)
     /* We hard-wire the BSP to the first CPU. */
-    if (s->cpu_index == 0) {
-        apic_designate_bsp(cpu->apic_state);
-    }
+    apic_designate_bsp(cpu->apic_state, s->cpu_index == 0);
 
Quoted 5 lines

target-i386/cpu.c static void x86_cpu_reset(CPUState *s) · line 2720

Quoted 3 lines
target-i386/cpu.c +0 -0
     s->halted = !cpu_is_bsp(cpu);
 

Thanks, applied locally.

I don't understand why this is necessary: The cpu_index doesn't change, therefore the BSP designation won't change either. Is this a hot-unplug preparation? Or is a KVM-specific call missing here? Either way, the commit message could use some clarification.

The API change itself looks okay.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#3 lore raw
Quoted 13 lines · Andreas Färber
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
target-i386/cpu.c +1 -3
index b2d1c95..03b33cf 100644
@@ -2714,9 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
 #if !defined(CONFIG_USER_ONLY)
     /* We hard-wire the BSP to the first CPU. */
-    if (s->cpu_index == 0) {
-        apic_designate_bsp(cpu->apic_state);
-    }
+    apic_designate_bsp(cpu->apic_state, s->cpu_index == 0);
 
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

target-i386/cpu.c static void x86_cpu_reset(CPUState *s) · line 2720

Quoted 3 lines
target-i386/cpu.c +0 -0
     s->halted = !cpu_is_bsp(cpu);
 

Thanks, applied locally.

I don't understand why this is necessary: The cpu_index doesn't change, therefore the BSP designation won't change either.

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Paolo

Is this a hot-unplug

Quoted 2 lines

preparation? Or is a KVM-specific call missing here? Either way, the commit message could use some clarification.

#4 lore raw

Am 07.04.2015 um 12:34 schrieb Paolo Bonzini:

Quoted 19 lines · Andreas Färber
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 13 lines
Quoted 13 lines
target-i386/cpu.c +1 -3
index b2d1c95..03b33cf 100644
@@ -2714,9 +2714,7 @@ static void x86_cpu_reset(CPUState *s)
 
 #if !defined(CONFIG_USER_ONLY)
     /* We hard-wire the BSP to the first CPU. */
-    if (s->cpu_index == 0) {
-        apic_designate_bsp(cpu->apic_state);
-    }
+    apic_designate_bsp(cpu->apic_state, s->cpu_index == 0);
 
Quoted 3 lines

target-i386/cpu.c static void x86_cpu_reset(CPUState *s) · line 2720

Quoted 3 lines
target-i386/cpu.c +0 -0
     s->halted = !cpu_is_bsp(cpu);
 

Thanks, applied locally.

Quoted 4 lines
Quoted 2 lines

I don't understand why this is necessary: The cpu_index doesn't change, therefore the BSP designation won't change either.

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#5 lore raw
Quoted 4 lines · Andreas Färber
Quoted 1 line
Quoted 1 line

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Not cpu_index, s->apicbase's MSR_IA32_APICBASE_BSP bit can change with KVM in-kernel LAPIC. It cannot change with QEMU's userspace LAPIC.

Because it can change, you have to call apic_designate_bsp for all CPUs and not only on CPU 0.

Paolo

#6 lore raw

Am 07.04.2015 um 12:54 schrieb Paolo Bonzini:

Quoted 11 lines · Andreas Färber
Quoted 4 lines
Quoted 1 line
Quoted 1 line

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Not cpu_index, s->apicbase's MSR_IA32_APICBASE_BSP bit can change with KVM in-kernel LAPIC. It cannot change with QEMU's userspace LAPIC.

Because it can change, you have to call apic_designate_bsp for all CPUs and not only on CPU 0.

Now I'm even more confused. Surely CPUState is initially zero-initialized. Then we designate one as BSP on reset. That should be the same result as designating all non-BSP CPUs, no? The only way that would change is then cpu_index == 0 goes away (hot-unplug, not implemented yet), and in that case it would be about designating a different CPU, not about un-designating one.

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#7 lore raw

Am 07.04.2015 um 13:09 schrieb Andreas Färber:

Quoted 20 lines

Am 07.04.2015 um 12:54 schrieb Paolo Bonzini:

Quoted 11 lines · Andreas Färber
Quoted 4 lines
Quoted 1 line
Quoted 1 line

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Not cpu_index, s->apicbase's MSR_IA32_APICBASE_BSP bit can change with KVM in-kernel LAPIC. It cannot change with QEMU's userspace LAPIC.

Because it can change, you have to call apic_designate_bsp for all CPUs and not only on CPU 0.

Now I'm even more confused. Surely CPUState is initially zero-initialized. Then we designate one as BSP on reset. That should be the same result as designating all non-BSP CPUs, no? The only way that would change is then cpu_index == 0 goes away (hot-unplug, not implemented yet), and in that case it would be about designating a different CPU, not about un-designating one.

Quoted 2 lines

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

    bsp = cpu_is_bsp(s->cpu);
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

    bsp = cpu_get_apic_base(s->cpu->apic_state) & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

    bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

In which case we already relied on s->cpu and could thus simply change this to something like:

    bsp = CPU(s->cpu)->cpu_index == 0;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Then the apicbase manipulation would be nicely encapsulated in the APIC rather than the APIC reset retaining it and the CPU reset meddling with its state.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#8 lore raw
Quoted 25 lines · On Tue, 07 Apr 2015 13:57:34 +0200 Andreas Färber <[email protected]> wrote:

Am 07.04.2015 um 13:09 schrieb Andreas Färber:

Quoted 24 lines

Am 07.04.2015 um 12:54 schrieb Paolo Bonzini:

Quoted 7 lines · Andreas Färber
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Quoted 6 lines

Not cpu_index, s->apicbase's MSR_IA32_APICBASE_BSP bit can change with KVM in-kernel LAPIC. It cannot change with QEMU's userspace LAPIC.

Because it can change, you have to call apic_designate_bsp for all CPUs and not only on CPU 0.

Now I'm even more confused. Surely CPUState is initially zero-initialized. Then we designate one as BSP on reset. That should be the same result as designating all non-BSP CPUs, no? The only way that would change is then cpu_index == 0 goes away (hot-unplug, not implemented yet), and in that case it would be about designating a different CPU, not about un-designating one.

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

guest can set BSP flag in apicbase of non the first CPU and then o reset on KVM exit it will be propagated into QEMU's state kvm_arch_post_run() -> cpu_set_apic_base()

as result with current code after reset we will have the first CPU with BSP bit and another one since nobody bothered to clear it.

That's what this patch does.

[...]

Quoted 11 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

    bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

In which case we already relied on s->cpu and could thus simply change this to something like:

    bsp = CPU(s->cpu)->cpu_index == 0;

^^^ shouldn't be part of APIC code, APIC shouldn't have anything to do with cpu_index.

we do above in CPU code currently

    /* We hard-wire the BSP to the first CPU. */
    if (s->cpu_index == 0) {
        apic_designate_bsp(cpu->apic_state);
    }
Quoted 9 lines
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Then the apicbase manipulation would be nicely encapsulated in the APIC rather than the APIC reset retaining it and the CPU reset meddling with its state.

Andreas

#9 lore raw

Am 07.04.2015 um 15:14 schrieb Igor Mammedov:

Quoted 49 lines
Quoted 25 lines · On Tue, 07 Apr 2015 13:57:34 +0200 Andreas Färber <[email protected]> wrote:

Am 07.04.2015 um 13:09 schrieb Andreas Färber:

Quoted 24 lines

Am 07.04.2015 um 12:54 schrieb Paolo Bonzini:

Quoted 7 lines · Andreas Färber
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

It can change at runtime, though, if you're using the KVM in-kernel LAPIC.

Got a pointer? A quick git-grep doesn't show anything in hw/ or kvm-all.c or target-i386/ assigning cpu_index, so it'll always have the initial value.

Quoted 6 lines

Not cpu_index, s->apicbase's MSR_IA32_APICBASE_BSP bit can change with KVM in-kernel LAPIC. It cannot change with QEMU's userspace LAPIC.

Because it can change, you have to call apic_designate_bsp for all CPUs and not only on CPU 0.

Now I'm even more confused. Surely CPUState is initially zero-initialized. Then we designate one as BSP on reset. That should be the same result as designating all non-BSP CPUs, no? The only way that would change is then cpu_index == 0 goes away (hot-unplug, not implemented yet), and in that case it would be about designating a different CPU, not about un-designating one.

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

guest can set BSP flag in apicbase of non the first CPU and then o reset on KVM exit it will be propagated into QEMU's state kvm_arch_post_run() -> cpu_set_apic_base()

as result with current code after reset we will have the first CPU with BSP bit and another one since nobody bothered to clear it.

That's what this patch does.

[...]

Quoted 11 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

    bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

In which case we already relied on s->cpu and could thus simply change this to something like:

    bsp = CPU(s->cpu)->cpu_index == 0;
Quoted 8 lines

^^^ shouldn't be part of APIC code, APIC shouldn't have anything to do with cpu_index.

we do above in CPU code currently

    /* We hard-wire the BSP to the first CPU. */
    if (s->cpu_index == 0) {
        apic_designate_bsp(cpu->apic_state);
    }

I know, that's what this patch is changing, and I am saying that by the same logic the CPU has no business fiddling with the APIC's apicbase field when the APIC's reset is touching that very same field.

And I do remember that we arrived at this solution to get to a halfway simple generic reset semantics, moving an external BSP designation into the reset handling. However, this patch is making the twisted logic worse IMO.

Andreas

Quoted 12 lines
Quoted 11 lines

    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Then the apicbase manipulation would be nicely encapsulated in the APIC rather than the APIC reset retaining it and the CPU reset meddling with its state.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#10 lore raw
Quoted 7 lines · Andreas Färber
Quoted 4 lines
Quoted 4 lines
    /* We hard-wire the BSP to the first CPU. */
    if (s->cpu_index == 0) {
        apic_designate_bsp(cpu->apic_state);
    }

I know, that's what this patch is changing, and I am saying that by the same logic the CPU has no business fiddling with the APIC's apicbase field when the APIC's reset is touching that very same field.

That's exactly what a real CPU does on power up or #RESET, though.

Paolo

#11 lore raw

Am 07.04.2015 um 15:29 schrieb Paolo Bonzini:

Quoted 10 lines · Andreas Färber
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
    /* We hard-wire the BSP to the first CPU. */
    if (s->cpu_index == 0) {
        apic_designate_bsp(cpu->apic_state);
    }

I know, that's what this patch is changing, and I am saying that by the same logic the CPU has no business fiddling with the APIC's apicbase field when the APIC's reset is touching that very same field.

That's exactly what a real CPU does on power up or #RESET, though.

Does the APIC retain its BSP bit value on #RESET though? I doubt it. It feels we're awkwardly working around qdev reset semantics here...

If you say the CPU must be in charge, then we should assure that the APIC is reset before the CPU designates it and not have the APIC reset callback retain such bits.

Admittedly, if this were for-2.3 (as which it is not marked) then this patch may be the least intrusive. But it isn't and I've been preparing to refactor the CPU-APIC relationship, so I really want to get it right long-term.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#12 lore raw
Quoted 6 lines · Andreas Färber

Am 07.04.2015 um 15:29 schrieb Paolo Bonzini:

Quoted 5 lines · Andreas Färber
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
    /* We hard-wire the BSP to the first CPU. */
    if (s->cpu_index == 0) {
        apic_designate_bsp(cpu->apic_state);
    }
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I know, that's what this patch is changing, and I am saying that by the same logic the CPU has no business fiddling with the APIC's apicbase field when the APIC's reset is touching that very same field.

That's exactly what a real CPU does on power up or #RESET, though.

Does the APIC retain its BSP bit value on #RESET though? I doubt it.

You cannot tell, since the MP protocol reruns immediately after a full reset. I think we do this in apic_cpu_reset to avoid mess with the initialization order of the APIC and CPU.

Quoted 5 lines

It feels we're awkwardly working around qdev reset semantics here...

If you say the CPU must be in charge, then we should assure that the APIC is reset before the CPU designates it and not have the APIC reset callback retain such bits.

Yes, I agree, but as you know very well the propagation of signals (be it "reset" or "realize") is a mess.

Even if you make the APIC a QOM child of the CPU, this doesn't mean that qdev reset (which is post-order) propagates to the APIC before propagating to the CPU.

Quoted 4 lines

Admittedly, if this were for-2.3 (as which it is not marked) then this patch may be the least intrusive. But it isn't and I've been preparing to refactor the CPU-APIC relationship, so I really want to get it right long-term.

Well, actually I did post it for inclusion in 2.3 since it affected only KVM and it would be ugly to have 4.0 fail kvm-unit-tests with all existing QEMU releases.

Paolo

#13 lore raw
In reply to Andreas Färber
Quoted 14 lines · Andreas Färber
Quoted 2 lines
Quoted 2 lines

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

    bsp = cpu_is_bsp(s->cpu);
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

    bsp = cpu_get_apic_base(s->cpu->apic_state) & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
Quoted 6 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

    bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Yes, this is more readable.

Quoted 10 lines

In which case we already relied on s->cpu and could thus simply change this to something like:

    bsp = CPU(s->cpu)->cpu_index == 0;
    s->apicbase = APIC_DEFAULT_ADDRESS |
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Then the apicbase manipulation would be nicely encapsulated in the APIC rather than the APIC reset retaining it and the CPU reset meddling with its state.

... but I find this worse. apic_designate_bsp corresponds to hardware executing the MP initialization protocol.

Paolo

#14 lore raw
Quoted 17 lines · Paolo Bonzini
Quoted 14 lines · Andreas Färber
Quoted 2 lines
Quoted 2 lines

If this is some issue with sync'ing state back and forth before QEMU and KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

     bsp = cpu_is_bsp(s->cpu);
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

     bsp = cpu_get_apic_base(s->cpu->apic_state) & MSR_IA32_APICBASE_BSP;
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
Quoted 8 lines
Quoted 6 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

     bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Yes, this is more readable.

just $0.02 :)

why don't

        bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
        s->apicbase =
		APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;

in this case. This looks the same from the textual point of view.

Den

#15 lore raw
Quoted 29 lines · Denis V. Lunev
Quoted 19 lines · Paolo Bonzini
Quoted 16 lines · Andreas Färber
Quoted 3 lines
Quoted 3 lines

If this is some issue with sync'ing state back and forth before QEMU and
KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

     bsp = cpu_is_bsp(s->cpu);
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

     bsp = cpu_get_apic_base(s->cpu->apic_state) &
MSR_IA32_APICBASE_BSP;
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
Quoted 8 lines
Quoted 6 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

     bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
     s->apicbase = APIC_DEFAULT_ADDRESS |
         (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Yes, this is more readable.

Quoted 7 lines

just $0.02 :)

why don't

        bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
        s->apicbase =
        APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;

in this case. This looks the same from the textual point of view.

Yes. Would you like to send a patch?

Paolo

#16 lore raw
Quoted 40 lines · Paolo Bonzini
Quoted 29 lines · Denis V. Lunev
Quoted 19 lines · Paolo Bonzini
Quoted 16 lines · Andreas Färber
Quoted 3 lines
Quoted 3 lines

If this is some issue with sync'ing state back and forth before QEMU and
KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

      bsp = cpu_is_bsp(s->cpu);
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

      bsp = cpu_get_apic_base(s->cpu->apic_state) &
MSR_IA32_APICBASE_BSP;
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
Quoted 8 lines
Quoted 6 lines

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

      bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;

Yes, this is more readable.

Quoted 7 lines

just $0.02 :)

why don't

         bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
         s->apicbase =
         APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;

in this case. This looks the same from the textual point of view.

Quoted 4 lines

Yes. Would you like to send a patch?

Paolo

no prob, just give me 2 minutes. Side note, bsp will become uint32_t and we will loose tracepoint inside cpu_get_apic_base() on this path...

Den

#17 lore raw

Am 07.04.2015 um 15:44 schrieb Denis V. Lunev:

Quoted 44 lines · Paolo Bonzini
Quoted 43 lines · Denis V. Lunev
Quoted 39 lines · Paolo Bonzini
Quoted 24 lines · Andreas Färber
Quoted 21 lines
Quoted 3 lines
Quoted 3 lines

If this is some issue with sync'ing state back and forth before QEMU and
KVM then the real issue has not been explained.

Hm, hw/intc/apic_common.c:apic_reset_common() has:

      bsp = cpu_is_bsp(s->cpu);
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) |

MSR_IA32_APICBASE_ENABLE;

What this is doing is really:

      bsp = cpu_get_apic_base(s->cpu->apic_state) &
MSR_IA32_APICBASE_BSP;
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) |

MSR_IA32_APICBASE_ENABLE;

Unless I'm missing something, since we are in the APIC device's reset function, this is effectively a twisted way of writing:

Quoted 6 lines
Quoted 4 lines
      bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
      s->apicbase = APIC_DEFAULT_ADDRESS |
          (bsp ? MSR_IA32_APICBASE_BSP : 0) |

MSR_IA32_APICBASE_ENABLE;

Yes, this is more readable.

just $0.02 :)

why don't

         bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
         s->apicbase =
         APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;

in this case. This looks the same from the textual point of view.

Quoted 8 lines
Quoted 4 lines

Yes. Would you like to send a patch?

Paolo

no prob, just give me 2 minutes. Side note, bsp will become uint32_t and we will loose tracepoint inside cpu_get_apic_base() on this path...

Yes, I intentionally left it out above as I don't think we need to trace the local usage here. Worth mentioning in the commit message though.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)