Index: /trunk/include/VBox/pdmdev.h
===================================================================
--- /trunk/include/VBox/pdmdev.h	(revision 12666)
+++ /trunk/include/VBox/pdmdev.h	(revision 12667)
@@ -1056,5 +1056,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLRCCALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns));
+    DECLRCCALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
@@ -1063,5 +1063,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLRCCALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns));
+    DECLRCCALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
@@ -1122,5 +1122,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLR0CALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns));
+    DECLR0CALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
@@ -1129,5 +1129,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLR0CALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns));
+    DECLR0CALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
@@ -1187,5 +1187,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLR3CALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns));
+    DECLR3CALLBACKMEMBER(void, pfnSetInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
@@ -1194,5 +1194,5 @@
      * @param   pDevIns         Device instance of the APIC.
      */
-    DECLR3CALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns));
+    DECLR3CALLBACKMEMBER(void, pfnClearInterruptFF,(PPDMDEVINS pDevIns, VMCPUID cpuid));
 
     /**
Index: /trunk/include/VBox/vmm.h
===================================================================
--- /trunk/include/VBox/vmm.h	(revision 12666)
+++ /trunk/include/VBox/vmm.h	(revision 12667)
@@ -130,5 +130,5 @@
 RTHCPTR VMMGetHCStack(PVM pVM);
 
-uint32_t VMMGetCpuId(PVM pVM);
+VMCPUID VMMGetCpuId(PVM pVM);
 
 VMMDECL(uint32_t) VMMGetSvnRev(void);
Index: /trunk/src/VBox/Devices/PC/DevAPIC.cpp
===================================================================
--- /trunk/src/VBox/Devices/PC/DevAPIC.cpp	(revision 12666)
+++ /trunk/src/VBox/Devices/PC/DevAPIC.cpp	(revision 12667)
@@ -309,18 +309,28 @@
 DECLINLINE(APICState*) getLapic(APICDeviceInfo* dev)
 {
-    uint32_t id = dev->CTX_SUFF(pApicHlp)->pfnGetCpuId(dev->CTX_SUFF(pDevIns));
+    /* LAPIC's array is indexed by CPU id */
+    VMCPUID id = dev->CTX_SUFF(pApicHlp)->pfnGetCpuId(dev->CTX_SUFF(pDevIns));
     return LAPIC_BASE(dev) + id;
 }
 
-
-DECLINLINE(void) cpuSetInterrupt(APICDeviceInfo* dev, uint32_t cpuid)
-{
-    dev->CTX_SUFF(pApicHlp)->pfnSetInterruptFF(dev->CTX_SUFF(pDevIns));
-}
-
-DECLINLINE(void) cpuClearInterrupt(APICDeviceInfo* dev, uint32_t cpuid)
-{
-    dev->CTX_SUFF(pApicHlp)->pfnClearInterruptFF(dev->CTX_SUFF(pDevIns));
-}
+DECLINLINE(VMCPUID) getCpuFromLapic(APICDeviceInfo* dev, APICState *s)
+{
+    /* for now we assume LAPIC id == CPU id */
+    return VMCPUID(s->id);
+}
+
+DECLINLINE(void) cpuSetInterrupt(APICDeviceInfo* dev, APICState *s)
+{
+    dev->CTX_SUFF(pApicHlp)->pfnSetInterruptFF(dev->CTX_SUFF(pDevIns), 
+                                               getCpuFromLapic(dev, s));
+}
+
+DECLINLINE(void) cpuClearInterrupt(APICDeviceInfo* dev, APICState *s)
+{
+    dev->CTX_SUFF(pApicHlp)->pfnClearInterruptFF(dev->CTX_SUFF(pDevIns), 
+                                                 getCpuFromLapic(dev, s));
+}
+
+
 #endif /* VBOX */
 
@@ -393,5 +403,5 @@
             /** @todo: what do we really do with SMI */
             foreach_apic(dev, deliver_bitmask,
-                         cpuSetInterrupt(dev, apic->id));
+                         cpuSetInterrupt(dev, apic));
             return;
 
@@ -399,5 +409,5 @@
             /** @todo: what do we really do with NMI */
             foreach_apic(dev, deliver_bitmask,
-                         cpuSetInterrupt(dev, apic->id));
+                         cpuSetInterrupt(dev, apic));
             return;
 
@@ -470,5 +480,5 @@
 
         /* Clear any pending APIC interrupt action flag. */
-        cpuClearInterrupt(dev, s->id);
+        cpuClearInterrupt(dev, s);
         dev->CTX_SUFF(pApicHlp)->pfnChangeFeature(pDevIns, false);
     }
@@ -632,5 +642,5 @@
     {
         /* Clear any pending APIC interrupt action flag. */
-        cpuClearInterrupt(dev, s->id);
+        cpuClearInterrupt(dev, s);
         return false;
     }
@@ -647,5 +657,5 @@
     cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
 #else
-    cpuSetInterrupt(dev, s->id);
+    cpuSetInterrupt(dev, s);
     return true;
 #endif
@@ -693,5 +703,5 @@
         Log(("apic_update_tpr: deactivate interrupt that was masked by the TPR update (%x)\n", val));
         STAM_COUNTER_INC(&dev->StatClearedActiveIrq);
-        cpuClearInterrupt(dev, s->id);
+        cpuClearInterrupt(dev, s);
     }
 }
@@ -1804,5 +1814,5 @@
     dev->pApicHlpR3->pfnChangeFeature(dev->pDevInsR3, true);
     /* Clear any pending APIC interrupt action flag. */
-    cpuClearInterrupt(dev, s->id);
+    cpuClearInterrupt(dev, s);
     APIC_UNLOCK(dev);
 }
Index: /trunk/src/VBox/VMM/PDMDevice.cpp
===================================================================
--- /trunk/src/VBox/VMM/PDMDevice.cpp	(revision 12666)
+++ /trunk/src/VBox/VMM/PDMDevice.cpp	(revision 12667)
@@ -230,10 +230,10 @@
  * @{
  */
-static DECLCALLBACK(void) pdmR3ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns);
-static DECLCALLBACK(void) pdmR3ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns);
+static DECLCALLBACK(void) pdmR3ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
+static DECLCALLBACK(void) pdmR3ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
 static DECLCALLBACK(void) pdmR3ApicHlp_ChangeFeature(PPDMDEVINS pDevIns, bool fEnabled);
 static DECLCALLBACK(int) pdmR3ApicHlp_Lock(PPDMDEVINS pDevIns, int rc);
 static DECLCALLBACK(void) pdmR3ApicHlp_Unlock(PPDMDEVINS pDevIns);
-static DECLCALLBACK(uint32_t) pdmR3ApicHlp_GetCpuId(PPDMDEVINS pDevIns);
+static DECLCALLBACK(VMCPUID) pdmR3ApicHlp_GetCpuId(PPDMDEVINS pDevIns);
 static DECLCALLBACK(PCPDMAPICHLPRC) pdmR3ApicHlp_GetRCHelpers(PPDMDEVINS pDevIns);
 static DECLCALLBACK(PCPDMAPICHLPR0) pdmR3ApicHlp_GetR0Helpers(PPDMDEVINS pDevIns);
@@ -4073,5 +4073,5 @@
 
 /** @copydoc PDMAPICHLPR3::pfnSetInterruptFF */
-static DECLCALLBACK(void) pdmR3ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmR3ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -4086,5 +4086,5 @@
 
 /** @copydoc PDMAPICHLPR3::pfnClearInterruptFF */
-static DECLCALLBACK(void) pdmR3ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmR3ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -4100,5 +4100,5 @@
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
-    LogFlow(("pdmR3ApicHlp_ClearInterruptFF: caller='%s'/%d: fEnabled=%RTbool\n",
+    LogFlow(("pdmR3ApicHlp_ChangeFeature: caller='%s'/%d: fEnabled=%RTbool\n",
              pDevIns->pDevReg->szDeviceName, pDevIns->iInstance, fEnabled));
     if (fEnabled)
@@ -4125,5 +4125,5 @@
 
 /** @copydoc PDMAPICHLPR3::pfnGetCpuId */
-static DECLCALLBACK(uint32_t) pdmR3ApicHlp_GetCpuId(PPDMDEVINS pDevIns)
+static DECLCALLBACK(VMCPUID) pdmR3ApicHlp_GetCpuId(PPDMDEVINS pDevIns)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
Index: /trunk/src/VBox/VMM/VMMAll/VMMAll.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMAll/VMMAll.cpp	(revision 12666)
+++ /trunk/src/VBox/VMM/VMMAll/VMMAll.cpp	(revision 12667)
@@ -67,7 +67,10 @@
  * @thread  EMT
  */
-uint32_t VMMGetCpuId(PVM pVM)
+VMCPUID VMMGetCpuId(PVM pVM)
 {
-#ifdef VBOX_WITH_GUEST_SMPT
+#ifdef VBOX_WITH_SMP_GUESTS
+    /* Only emulation thread(s) allowed to ask for CPU id */
+    VM_ASSERT_EMT(pVM);
+
 # if defined(IN_GC)
     /* There is only one CPU if we're in GC. */
Index: /trunk/src/VBox/VMM/VMMGC/PDMGCDevice.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMGC/PDMGCDevice.cpp	(revision 12666)
+++ /trunk/src/VBox/VMM/VMMGC/PDMGCDevice.cpp	(revision 12667)
@@ -99,10 +99,10 @@
  * @{
  */
-static DECLCALLBACK(void) pdmRCApicHlp_SetInterruptFF(PPDMDEVINS pDevIns);
-static DECLCALLBACK(void) pdmRCApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns);
+static DECLCALLBACK(void) pdmRCApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
+static DECLCALLBACK(void) pdmRCApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
 static DECLCALLBACK(void) pdmRCApicHlp_ChangeFeature(PPDMDEVINS pDevIns, bool fEnabled);
 static DECLCALLBACK(int) pdmRCApicHlp_Lock(PPDMDEVINS pDevIns, int rc);
 static DECLCALLBACK(void) pdmRCApicHlp_Unlock(PPDMDEVINS pDevIns);
-static DECLCALLBACK(uint32_t) pdmRCApicHlp_GetCpuId(PPDMDEVINS pDevIns);
+static DECLCALLBACK(VMCPUID) pdmRCApicHlp_GetCpuId(PPDMDEVINS pDevIns);
 /** @} */
 
@@ -393,5 +393,5 @@
 
 /** @copydoc PDMAPICHLPRC::pfnSetInterruptFF */
-static DECLCALLBACK(void) pdmRCApicHlp_SetInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmRCApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -403,5 +403,5 @@
 
 /** @copydoc PDMAPICHLPRC::pfnClearInterruptFF */
-static DECLCALLBACK(void) pdmRCApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmRCApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -441,12 +441,9 @@
 
 /** @copydoc PDMAPICHLPRC::pfnGetCpuId */
-static DECLCALLBACK(uint32_t) pdmRCApicHlp_GetCpuId(PPDMDEVINS pDevIns)
+static DECLCALLBACK(VMCPUID) pdmRCApicHlp_GetCpuId(PPDMDEVINS pDevIns)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
     return VMMGetCpuId(pDevIns->Internal.s.pVMGC);
 }
-
-
-
 
 /** @copydoc PDMIOAPICHLPRC::pfnApicBusDeliver */
Index: /trunk/src/VBox/VMM/VMMR0/PDMR0Device.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/PDMR0Device.cpp	(revision 12666)
+++ /trunk/src/VBox/VMM/VMMR0/PDMR0Device.cpp	(revision 12667)
@@ -99,10 +99,10 @@
  * @{
  */
-static DECLCALLBACK(void) pdmR0ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns);
-static DECLCALLBACK(void) pdmR0ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns);
+static DECLCALLBACK(void) pdmR0ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
+static DECLCALLBACK(void) pdmR0ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid);
 static DECLCALLBACK(void) pdmR0ApicHlp_ChangeFeature(PPDMDEVINS pDevIns, bool fEnabled);
 static DECLCALLBACK(int) pdmR0ApicHlp_Lock(PPDMDEVINS pDevIns, int rc);
 static DECLCALLBACK(void) pdmR0ApicHlp_Unlock(PPDMDEVINS pDevIns);
-static DECLCALLBACK(uint32_t) pdmR0ApicHlp_GetCpuId(PPDMDEVINS pDevIns);
+static DECLCALLBACK(VMCPUID) pdmR0ApicHlp_GetCpuId(PPDMDEVINS pDevIns);
 /** @} */
 
@@ -397,5 +397,5 @@
 
 /** @copydoc PDMAPICHLPR0::pfnSetInterruptFF */
-static DECLCALLBACK(void) pdmR0ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmR0ApicHlp_SetInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -407,5 +407,5 @@
 
 /** @copydoc PDMAPICHLPR0::pfnClearInterruptFF */
-static DECLCALLBACK(void) pdmR0ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns)
+static DECLCALLBACK(void) pdmR0ApicHlp_ClearInterruptFF(PPDMDEVINS pDevIns, VMCPUID cpuid)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
@@ -445,11 +445,9 @@
 
 /** @copydoc PDMAPICHLPR0::pfnGetCpuId */
-static DECLCALLBACK(uint32_t) pdmR0ApicHlp_GetCpuId(PPDMDEVINS pDevIns)
+static DECLCALLBACK(VMCPUID) pdmR0ApicHlp_GetCpuId(PPDMDEVINS pDevIns)
 {
     PDMDEV_ASSERT_DEVINS(pDevIns);
     return VMMGetCpuId(pDevIns->Internal.s.pVMHC);
 }
-
-
 
 
