DevAPIC.cpp revision 5ab279c951084868bab25b6bdebf86ad9aafcab7
#ifdef VBOX
/* $Id$ */
/** @file
* Advanced Programmable Interrupt Controller (APIC) Device and
* I/O Advanced Programmable Interrupt Controller (IO-APIC) Device.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
* --------------------------------------------------------------------
*
* This code is based on:
*
* apic.c revision 1.5 @@OSETODO
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_APIC
#include "Builtins2.h"
#include "vl_vbox.h"
#define MSR_IA32_APICBASE 0x1b
#ifdef VBOX
#endif
#ifndef EINVAL
# define EINVAL 1
#endif
#ifdef _MSC_VER
#endif
/** @def APIC_LOCK
* Acquires the PDM lock. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
} while (0)
/** @def APIC_LOCK_VOID
* Acquires the PDM lock and does not expect failure (i.e. ring-3 only!). */
do { \
} while (0)
/** @def APIC_UNLOCK
* Releases the PDM lock. */
#define APIC_UNLOCK(pThis) \
/** @def IOAPIC_LOCK
* Acquires the PDM lock. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
} while (0)
/** @def IOAPIC_UNLOCK
* Releases the PDM lock. */
/** @def LAPIC_BASE
* Return address of first LAPIC state. */
do { \
uint32_t i; \
{ \
{ \
code; \
} \
apic++; \
} \
} while (0)
#endif /* VBOX */
/*
* APIC support
*
* Copyright (c) 2004-2005 Fabrice Bellard
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef VBOX
#include "vl.h"
#endif
#define DEBUG_APIC
#define DEBUG_IOAPIC
/* APIC Local Vector Table */
#define APIC_LVT_TIMER 0
#define APIC_LVT_THERMAL 1
#define APIC_LVT_PERFORM 2
#define APIC_LVT_LINT0 3
#define APIC_LVT_LINT1 4
#define APIC_LVT_ERROR 5
#define APIC_LVT_NB 6
/* APIC delivery modes */
#define APIC_DM_FIXED 0
#define APIC_DM_LOWPRI 1
#define APIC_DM_SMI 2
#define APIC_DM_NMI 4
#define APIC_DM_INIT 5
#define APIC_DM_SIPI 6
#define APIC_DM_EXTINT 7
/* APIC destination mode */
#define APIC_DESTMODE_FLAT 0xf
#define APIC_DESTMODE_CLUSTER 1
#define APIC_TRIGGER_EDGE 0
#define APIC_TRIGGER_LEVEL 1
#define IOAPIC_NUM_PINS 0x18
#ifdef VBOX
#define APIC_MAX_PATCH_ATTEMPTS 100
typedef uint32_t PhysApicId;
#endif
typedef struct APICState {
#ifndef VBOX
#endif /* !VBOX */
#ifdef VBOX
/* Task priority register (interrupt level) */
/* Logical APIC id */
/* Physical APIC id */
/** @todo: is it logical or physical? Not really used anyway now. */
#else
#endif
int count_shift;
#ifdef VBOX
#endif
#ifndef VBOX
#else
/** The APIC timer - R3 Ptr. */
/** The APIC timer - R0 Ptr. */
/** The APIC timer - RC Ptr. */
/** Alignment */
#endif /* VBOX */
} APICState;
struct IOAPICState {
#ifdef VBOX
/** The device instance - R3 Ptr. */
/** The IOAPIC helpers - R3 Ptr. */
/** The device instance - R0 Ptr. */
/** The IOAPIC helpers - R0 Ptr. */
/** The device instance - RC Ptr. */
/** The IOAPIC helpers - RC Ptr. */
# ifdef VBOX_WITH_STATISTICS
# endif
#endif /* VBOX */
};
#ifdef VBOX
typedef struct IOAPICState IOAPICState;
typedef struct
{
/** The device instance - R3 Ptr. */
/** The APIC helpers - R3 Ptr. */
/** LAPICs states - R3 Ptr */
/** The device instance - R0 Ptr. */
/** The APIC helpers - R0 Ptr. */
/** LAPICs states - R0 Ptr */
/** The device instance - RC Ptr. */
/** The APIC helpers - RC Ptr. */
/** LAPICs states - RC Ptr */
/** APIC specification version in this virtual hardware configuration. */
/** Number of attempts made to optimize TPR accesses. */
/** Number of CPUs on the system (same as LAPIC count). */
# ifdef VBOX_WITH_STATISTICS
# endif
static int apic_get_arb_pri(APICState *s);
static int apic_get_ppr(APICState *s);
{
}
{
/* LAPIC's array is indexed by CPU id */
}
{
/* for now we assume LAPIC physical id == CPU id */
}
{
getCpuFromLapic(dev, s));
}
{
Log2(("apic: clear interrupt flag\n"));
getCpuFromLapic(dev, s));
}
#ifdef IN_RING3
{
getCpuFromLapic(dev, s),
vector);
}
{
Log2(("apic: send init IPI\n"));
getCpuFromLapic(dev, s));
}
#endif
{
switch (dev->enmVersion)
{
case PDMAPICVERSION_NONE:
return 0;
case PDMAPICVERSION_APIC:
return MSR_IA32_APICBASE_ENABLE;
case PDMAPICVERSION_X2APIC:
default:
return 0;
}
}
{
{
case 0:
return PDMAPICVERSION_NONE;
case 1:
default:
/* Invalid */
return PDMAPICVERSION_NONE;
case 2:
return PDMAPICVERSION_APIC;
case 3:
return PDMAPICVERSION_X2APIC;
}
}
#endif /* VBOX */
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifndef VBOX
static int apic_io_memory;
static int last_apic_id = 0;
#endif /* !VBOX */
#ifdef VBOX
PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID iCpu, uint32_t u32Reg, uint64_t u64Value);
PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID iCpu, uint32_t u32Reg, uint64_t *pu64Value);
PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
#endif /* VBOX */
#ifndef VBOX
{
#else /* VBOX */
{
#endif /* VBOX */
LogFlow(("apic_bus_deliver mask=%x mode=%x vector=%x polarity=%x trigger_mode=%x\n", deliver_bitmask, delivery_mode, vector_num, polarity, trigger_mode));
switch (delivery_mode) {
case APIC_DM_LOWPRI:
{
int d = -1;
if (deliver_bitmask)
d = ffs_bit(deliver_bitmask);
if (d >= 0)
{
}
return VINF_SUCCESS;
}
case APIC_DM_FIXED:
/* XXX: arbitration */
break;
case APIC_DM_SMI:
/** @todo: what do we really do with SMI */
return VINF_SUCCESS;
case APIC_DM_NMI:
/** @todo: what do we really do with NMI */
return VINF_SUCCESS;
case APIC_DM_INIT:
/* normal INIT IPI sent to processors */
#ifdef VBOX
#ifdef IN_RING3
return VINF_SUCCESS;
#else
/* We shall send init IPI only in R3, R0 calls should be
rescheduled to R3 */
return VINF_IOM_HC_MMIO_READ_WRITE;
#endif /* IN_RING3 */
#else
}
#endif
case APIC_DM_EXTINT:
/* handled in I/O APIC code */
break;
default:
return VINF_SUCCESS;
}
#ifdef VBOX
return VINF_SUCCESS;
#else /* VBOX */
}
#endif /* VBOX */
}
#ifndef VBOX
{
#ifdef DEBUG_APIC
#endif
/* if disabled, cannot be enabled again */
if (!(val & MSR_IA32_APICBASE_ENABLE)) {
s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
s->spurious_vec &= ~APIC_SV_ENABLE;
}
}
#else /* VBOX */
{
/** @todo: do we need to lock here ? */
/* APIC_LOCK_VOID(dev, VERR_INTERNAL_ERROR); */
/** @todo If this change is valid immediately, then we should change the MMIO registration! */
/* We cannot change if this CPU is BSP or not by writing to MSR - it's hardwired */
s->apicbase =
{
switch (newMode)
{
case PDMAPICVERSION_NONE:
{
s->spurious_vec &= ~APIC_SV_ENABLE;
/* Clear any pending APIC interrupt action flag. */
cpuClearInterrupt(dev, s);
/** @todo: why do we do that? */
break;
}
case PDMAPICVERSION_APIC:
/** @todo: map MMIO ranges, if needed */
break;
case PDMAPICVERSION_X2APIC:
/** @todo: unmap MMIO ranges of this APIC, according to the spec */
break;
default:
break;
}
}
/* APIC_UNLOCK(dev); */
}
#endif /* VBOX */
#ifndef VBOX
{
#ifdef DEBUG_APIC
#endif
return s->apicbase;
}
{
apic_update_irq(s);
}
{
return s->tpr >> 4;
}
{
unsigned int ret = 0;
#ifdef HOST_I386
return ret;
#else
if (value > 0xffff)
if (value > 0xff)
if (value > 0xf)
if (value > 0x3)
#endif
}
{
int i, mask;
i = index >> 5;
}
{
int i, mask;
i = index >> 5;
}
#else /* VBOX */
{
return s->apicbase;
}
{
}
{
return s->tpr >> 4;
}
PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t u64Value)
{
int rc = VINF_SUCCESS;
return VERR_EM_INTERPRETER;
switch (index)
{
case 0x02:
break;
case 0x03:
break;
case 0x08:
break;
case 0x09: case 0x0a:
break;
case 0x0b: /* EOI */
break;
case 0x0d:
break;
case 0x0e:
break;
case 0x0f:
break;
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28:
break;
case 0x30:
/* Here one of the differences with regular APIC: ICR is single 64-bit register */
break;
case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
{
int n = index - 0x32;
if (n == APIC_LVT_TIMER)
}
break;
case 0x38:
break;
case 0x39:
break;
case 0x3e:
{
int v;
break;
}
case 0x3f:
{
/* Self IPI, see x2APIC book 2.4.5 */
0 /* Delivery mode - fixed */,
0 /* Polarity - conform to the bus */,
0 /* Trigger mode - edge */);
break;
}
default:
break;
}
return rc;
}
PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t *pu64Value)
{
return VERR_EM_INTERPRETER;
switch (index)
{
case 0x02: /* id */
break;
case 0x03: /* version */
break;
case 0x08:
break;
case 0x09:
break;
case 0x0a:
/* ppr */
break;
case 0x0b:
val = 0;
break;
case 0x0d:
break;
case 0x0e:
/* Bottom 28 bits are always 1 */
break;
case 0x0f:
break;
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
break;
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
break;
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
break;
case 0x28:
break;
case 0x30:
/* Here one of the differences with regular APIC: ICR is single 64-bit register */
break;
case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
break;
case 0x38:
break;
case 0x39:
break;
case 0x3e:
break;
case 0x3f:
/* Self IPI register is write only */
break;
default:
val = 0;
break;
}
return VINF_SUCCESS;
}
/**
* More or less private interface between IOAPIC, only PDM is responsible
* for connecting the two devices.
*/
{
LogFlow(("apicBusDeliverCallback: pDevIns=%p u8Dest=%#x u8DestMode=%#x u8DeliveryMode=%#x iVector=%#x u8Polarity=%#x u8TriggerMode=%#x\n",
}
#endif /* VBOX */
/* return -1 if no bit is set */
{
int i;
for(i = 7; i >= 0; i--) {
if (tab[i] != 0) {
}
}
return -1;
}
static int apic_get_ppr(APICState *s)
{
if (isrv < 0)
isrv = 0;
isrv >>= 4;
else
return ppr;
}
static int apic_get_ppr_zero_tpr(APICState *s)
{
int isrv;
if (isrv < 0)
isrv = 0;
return isrv;
}
static int apic_get_arb_pri(APICState *s)
{
/* XXX: arbitration */
return 0;
}
/* signal the CPU if an irq is pending */
{
if (!(s->spurious_vec & APIC_SV_ENABLE))
#ifdef VBOX
{
/* Clear any pending APIC interrupt action flag. */
cpuClearInterrupt(dev, s);
return false;
}
#else
return false;
#endif /* VBOX */
if (irrv < 0)
return false;
ppr = apic_get_ppr(s);
return false;
#ifndef VBOX
#else
cpuSetInterrupt(dev, s);
return true;
#endif
}
#ifdef VBOX
{
if (!dev)
return false;
/*
* All our callbacks now come from single IOAPIC, thus locking
* seems to be excessive now (@todo: check)
*/
if (irrv < 0)
return false;
ppr = apic_get_ppr_zero_tpr(s);
return false;
return true;
}
{
bool fIrqIsActive = false;
bool fIrqWasActive = false;
/* If an interrupt is pending and now masked, then clear the FF flag. */
if (fIrqWasActive && !fIrqIsActive)
{
cpuClearInterrupt(dev, s);
}
}
#endif
{
LogFlow(("CPU%d: apic_set_irq vector=%x, trigger_mode=%x\n", s->phys_id, vector_num, trigger_mode));
if (trigger_mode)
else
apic_update_irq(dev, s);
}
{
int isrv;
if (isrv < 0)
return;
/* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
set the remote IRR bit for level triggered interrupts. */
apic_update_irq(dev, s);
}
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX */
{
if (dest_mode == 0)
{
if (dest == 0xff)
mask = 0xff;
else
}
else
{
uint32_t i;
/* XXX: cluster mode */
{
{
}
{
&&
{
mask |= (1 << i);
}
}
apic++;
}
}
return mask;
}
#ifdef IN_RING3
{
int i;
for(i = 0; i < APIC_LVT_NB; i++)
s->tpr = 0;
s->spurious_vec = 0xff;
s->log_dest = 0;
s->dest_mode = 0xff;
s->esr = 0;
s->divide_conf = 0;
s->count_shift = 0;
s->initial_count = 0;
s->initial_count_load_time = 0;
s->next_time = 0;
#ifdef VBOX
cpuSendInitIpi(dev, s);
#endif
}
/* send a SIPI message to the CPU to start it */
{
#ifndef VBOX
return;
0xffff, 0);
#else
#endif
}
#endif /* IN_RING3 */
{
uint32_t deliver_bitmask = 0;
#ifndef VBOX
#endif /* !VBOX */
LogFlow(("apic_deliver dest=%x dest_mode=%x dest_shorthand=%x delivery_mode=%x vector_num=%x polarity=%x trigger_mode=%x\n", dest, dest_mode, dest_shorthand, delivery_mode, vector_num, polarity, trigger_mode));
switch (dest_shorthand) {
case 0:
#ifndef VBOX
#else /* VBOX */
#endif /* !VBOX */
break;
case 1:
break;
case 2:
deliver_bitmask = 0xffffffff;
break;
case 3:
break;
}
switch (delivery_mode) {
case APIC_DM_INIT:
{
#ifndef VBOX
return;
#else
return VINF_SUCCESS;
#endif
}
}
break;
case APIC_DM_SIPI:
#ifndef VBOX
/* XXX: SMP support */
/* apic_startup(apic_iter); */
}
}
return;
#else
# ifdef IN_RING3
return VINF_SUCCESS;
# else
/* We shall send SIPI only in R3, R0 calls should be
rescheduled to R3 */
return VINF_IOM_HC_MMIO_WRITE;
# endif
#endif /* !VBOX */
}
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX */
}
{
/* if the APIC is not installed or enabled, we let the 8259 handle the
IRQs */
if (!dev)
{
Log(("apic_get_interrupt: returns -1 (!s)\n"));
return -1;
}
int intno;
if (!(s->spurious_vec & APIC_SV_ENABLE)) {
Log(("apic_get_interrupt: returns -1 (APIC_SV_ENABLE)\n"));
intno = -1;
goto done;
}
/* XXX: spurious IRQ handling */
if (intno < 0) {
Log(("apic_get_interrupt: returns -1 (irr)\n"));
intno = -1;
goto done;
}
goto done;
}
apic_update_irq(dev, s);
done:
return intno;
}
{
int64_t d;
#ifndef VBOX
s->count_shift;
#else /* VBOX */
s->count_shift;
#endif /* VBOX */
/* periodic */
} else {
if (d >= s->initial_count)
val = 0;
else
val = s->initial_count - d;
}
return val;
}
{
d = (current_time - s->initial_count_load_time) >>
s->count_shift;
} else {
if (d >= s->initial_count)
goto no_timer;
}
#ifndef VBOX
#else
#endif
} else {
#ifndef VBOX
qemu_del_timer(s->timer);
#else
#endif
}
}
#ifdef IN_RING3
#ifndef VBOX
static void apic_timer(void *opaque)
{
#else /* VBOX */
{
#endif /* VBOX */
LogFlow(("apic_timer: trigger irq\n"));
}
#ifdef VBOX
#endif
}
#endif /* IN_RING3 */
#ifndef VBOX
{
return 0;
}
{
return 0;
}
{
}
{
}
#endif /* !VBOX */
#ifndef VBOX
{
APICState *s;
#else /* VBOX */
{
#endif /* VBOX */
int index;
#ifndef VBOX
if (!env)
return 0;
s = env->apic_state;
#endif /* !VBOX */
switch(index) {
case 0x02: /* id */
break;
case 0x03: /* version */
break;
case 0x08:
break;
case 0x09:
val = apic_get_arb_pri(s);
break;
case 0x0a:
/* ppr */
val = apic_get_ppr(s);
break;
case 0x0b:
val = 0;
break;
case 0x0d:
break;
case 0x0e:
#ifdef VBOX
/* Bottom 28 bits are always 1 */
#else
#endif
break;
case 0x0f:
val = s->spurious_vec;
break;
#ifndef VBOX
case 0x10 ... 0x17:
#else /* VBOX */
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
#endif /* VBOX */
break;
#ifndef VBOX
case 0x18 ... 0x1f:
#else /* VBOX */
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
#endif /* VBOX */
break;
#ifndef VBOX
case 0x20 ... 0x27:
#else /* VBOX */
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
#endif /* VBOX */
break;
case 0x28:
break;
case 0x30:
case 0x31:
break;
#ifndef VBOX
case 0x32 ... 0x37:
#else /* VBOX */
case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
#endif /* VBOX */
break;
case 0x38:
val = s->initial_count;
break;
case 0x39:
break;
case 0x3e:
val = s->divide_conf;
break;
default:
s->esr |= ESR_ILLEGAL_ADDRESS;
val = 0;
break;
}
#ifdef DEBUG_APIC
#endif
return val;
}
#ifndef VBOX
{
APICState *s;
#else /* VBOX */
static int apic_mem_writel(APICDeviceInfo* dev, APICState *s, target_phys_addr_t addr, uint32_t val)
{
int rc = VINF_SUCCESS;
#endif /* VBOX */
int index;
#ifndef VBOX
if (!env)
return;
s = env->apic_state;
#endif /* !VBOX */
#ifdef DEBUG_APIC
#endif
switch(index) {
case 0x02:
break;
case 0x03:
Log(("apic_mem_writel: write to version register; ignored\n"));
break;
case 0x08:
#ifdef VBOX
#else
apic_update_irq(s);
#endif
break;
case 0x09:
case 0x0a:
break;
case 0x0b: /* EOI */
break;
case 0x0d:
break;
case 0x0e:
break;
case 0x0f:
apic_update_irq(dev, s);
break;
#ifndef VBOX
case 0x10 ... 0x17:
case 0x18 ... 0x1f:
case 0x20 ... 0x27:
case 0x28:
#else
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28:
#endif
break;
case 0x30:
break;
case 0x31:
break;
#ifndef VBOX
case 0x32 ... 0x37:
#else /* VBOX */
case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
#endif /* VBOX */
{
int n = index - 0x32;
if (n == APIC_LVT_TIMER)
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX*/
}
break;
case 0x38:
s->initial_count = val;
#ifndef VBOX
#else /* VBOX */
#endif /* VBOX*/
break;
case 0x39:
break;
case 0x3e:
{
int v;
}
break;
default:
s->esr |= ESR_ILLEGAL_ADDRESS;
break;
}
#ifdef VBOX
return rc;
#endif
}
#ifdef IN_RING3
{
int i;
qemu_put_be32s(f, &s->apicbase);
#ifdef VBOX
qemu_put_be32s(f, &s->id);
qemu_put_be32s(f, &s->phys_id);
qemu_put_be32s(f, &s->arb_id);
qemu_put_be32s(f, &s->tpr);
#else
qemu_put_8s(f, &s->id);
qemu_put_8s(f, &s->arb_id);
qemu_put_8s(f, &s->tpr);
#endif
qemu_put_be32s(f, &s->spurious_vec);
qemu_put_8s(f, &s->log_dest);
qemu_put_8s(f, &s->dest_mode);
for (i = 0; i < 8; i++) {
qemu_put_be32s(f, &s->isr[i]);
qemu_put_be32s(f, &s->tmr[i]);
qemu_put_be32s(f, &s->irr[i]);
}
for (i = 0; i < APIC_LVT_NB; i++) {
qemu_put_be32s(f, &s->lvt[i]);
}
qemu_put_be32s(f, &s->esr);
qemu_put_be32s(f, &s->icr[0]);
qemu_put_be32s(f, &s->divide_conf);
qemu_put_be32s(f, &s->count_shift);
qemu_put_be32s(f, &s->initial_count);
qemu_put_be64s(f, &s->initial_count_load_time);
qemu_put_be64s(f, &s->next_time);
#ifdef VBOX
#endif
}
{
int i;
#ifdef VBOX
return -EINVAL;
/* XXX: what if the base changes? (registered memory regions) */
qemu_get_be32s(f, &s->apicbase);
switch (version_id)
{
case 1:
{
qemu_get_8s(f, &val);
/* UP only in old saved states */
s->phys_id = 0;
qemu_get_8s(f, &val);
break;
}
case 2:
qemu_get_be32s(f, &s->id);
qemu_get_be32s(f, &s->phys_id);
qemu_get_be32s(f, &s->arb_id);
break;
}
qemu_get_be32s(f, &s->tpr);
#else
if (version_id != 1)
return -EINVAL;
/* XXX: what if the base changes? (registered memory regions) */
qemu_get_be32s(f, &s->apicbase);
qemu_get_8s(f, &s->id);
qemu_get_8s(f, &s->arb_id);
qemu_get_8s(f, &s->tpr);
#endif
qemu_get_be32s(f, &s->spurious_vec);
qemu_get_8s(f, &s->log_dest);
qemu_get_8s(f, &s->dest_mode);
for (i = 0; i < 8; i++) {
qemu_get_be32s(f, &s->isr[i]);
qemu_get_be32s(f, &s->tmr[i]);
qemu_get_be32s(f, &s->irr[i]);
}
for (i = 0; i < APIC_LVT_NB; i++) {
qemu_get_be32s(f, &s->lvt[i]);
}
qemu_get_be32s(f, &s->esr);
qemu_get_be32s(f, &s->icr[0]);
qemu_get_be32s(f, &s->divide_conf);
#ifdef VBOX
#endif
return VINF_SUCCESS;
}
#ifndef VBOX
static void apic_reset(void *opaque)
{
apic_init_ipi(s);
}
#endif
#endif /* IN_RING3 */
#ifndef VBOX
};
};
{
APICState *s;
s = qemu_mallocz(sizeof(APICState));
if (!s)
return -1;
env->apic_state = s;
apic_init_ipi(s);
s->id = last_apic_id++;
s->apicbase = 0xfee00000 |
/* XXX: mapping more APICs at the same memory location */
if (apic_io_memory == 0) {
/* NOTE: the APIC is directly connected to the CPU - it is not
on the global memory bus. */
}
s->next_apic = first_local_apic;
first_local_apic = s;
return 0;
}
#endif /* !VBOX */
static void ioapic_service(IOAPICState *s)
{
uint8_t i;
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
mask = 1 << i;
if (!(entry & APIC_LVT_MASKED)) {
if (trig_mode == APIC_TRIGGER_EDGE)
if (delivery_mode == APIC_DM_EXTINT)
#ifndef VBOX /* malc: i'm still not so sure about ExtINT delivery */
#else /* VBOX */
{
AssertMsgFailed(("Delivery mode ExtINT"));
}
#endif /* VBOX */
else
#ifndef VBOX
#else /* VBOX */
dest,
/* We must be sure that attempts to reschedule in R3
never get here */
#endif /* VBOX */
}
}
}
}
#ifdef VBOX
static
#endif
{
/* level triggered */
if (level) {
ioapic_service(s);
#ifdef VBOX
}
#endif
} else {
}
} else {
/* edge triggered */
if (level) {
ioapic_service(s);
}
}
}
}
{
int index;
addr &= 0xff;
if (addr == 0x00) {
} else if (addr == 0x10) {
switch (s->ioregsel) {
case 0x00:
break;
case 0x01:
break;
case 0x02:
val = 0;
break;
default:
if (s->ioregsel & 1)
else
}
}
#ifdef DEBUG_IOAPIC
#endif
}
return val;
}
{
int index;
addr &= 0xff;
if (addr == 0x00) {
return;
} else if (addr == 0x10) {
#ifdef DEBUG_IOAPIC
#endif
switch (s->ioregsel) {
case 0x00:
return;
case 0x01:
case 0x02:
return;
default:
if (s->ioregsel & 1) {
} else {
#ifdef VBOX
/* According to IOAPIC spec, vectors should be from 0x10 to 0xfe */
if ((val & APIC_LVT_MASKED) ||
{
}
else
{
/*
* Linux 2.6 kernels has pretty strange function
* unlock_ExtINT_logic() which writes
* absolutely bogus (all 0) value into the vector
* with pretty vague explanation why.
* So we just ignore such writes.
*/
}
}
#else
#endif
ioapic_service(s);
}
}
}
}
#ifdef IN_RING3
{
int i;
qemu_put_8s(f, &s->id);
qemu_put_8s(f, &s->ioregsel);
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
qemu_put_be64s(f, &s->ioredtbl[i]);
}
}
{
int i;
if (version_id != 1)
return -EINVAL;
qemu_get_8s(f, &s->id);
qemu_get_8s(f, &s->ioregsel);
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
qemu_get_be64s(f, &s->ioredtbl[i]);
}
return 0;
}
static void ioapic_reset(void *opaque)
{
#ifdef VBOX
#endif
int i;
memset(s, 0, sizeof(*s));
for(i = 0; i < IOAPIC_NUM_PINS; i++)
#ifdef VBOX
if (pDevIns)
{
}
if (pIoApicHlp)
{
s->pIoApicHlpR3 = pIoApicHlp;
}
#endif
}
#endif /* IN_RING3 */
#ifndef VBOX
};
};
IOAPICState *ioapic_init(void)
{
IOAPICState *s;
int io_memory;
s = qemu_mallocz(sizeof(IOAPICState));
if (!s)
return NULL;
ioapic_reset(s);
s->id = last_apic_id++;
ioapic_mem_write, s);
return s;
}
#endif /* !VBOX */
/* LAPIC */
PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
/** @todo: add LAPIC range validity checks (different LAPICs can theoretically have
different physical addresses, see #3092) */
switch (cb)
{
case 1:
break;
case 2:
break;
case 4:
{
#if 0 /** @note experimental */
#ifndef IN_RING3
&& ++s->ulTPRPatchAttempts < APIC_MAX_PATCH_ATTEMPTS)
{
#ifdef IN_RC
#else
pDevIns->pDevHlpR0->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, pDevIns + RT_OFFSETOF(APICState, tpr));
#endif
return VINF_PATM_HC_MMIO_PATCH_READ;
}
#endif
#endif /* experimental */
break;
}
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
/** @todo: add LAPIC range validity checks (multiple LAPICs can theoretically have
different physical addresses, see #3092) */
switch (cb)
{
case 1:
case 2:
/* ignore */
break;
case 4:
{
int rc;
return rc;
}
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
#ifdef IN_RING3
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
/* save all APICs data, @todo: is it correct? */
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) apicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
{
/* load all APICs data, @todo: is it correct? */
{
AssertFailed();
}
);
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRESET
*/
{
apic_init_ipi(dev, s);
/* malc, I've removed the initing duplicated in apic_init_ipi(). This
* arb_id was left over.. */
s->arb_id = 0;
/* Reset should re-enable the APIC. */
if (s->phys_id == 0)
s->apicbase |= MSR_IA32_APICBASE_BSP;
/* Clear any pending APIC interrupt action flag. */
cpuClearInterrupt(dev, s);
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
{
int i;
/* Mark first CPU as BSP */
if (id == 0)
for (i = 0; i < APIC_LVT_NB; i++)
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
int rc;
uint32_t i;
bool fIOAPIC;
bool fGCEnabled;
bool fR0Enabled;
/*
* Only single device instance.
*/
/*
* Validate configuration.
*/
"IOAPIC\0"
"GCEnabled\0"
"R0Enabled\0"
"NumCPUs\0"))
if (RT_FAILURE(rc))
N_("Configuration error: Failed to read \"IOAPIC\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query integer value \"NumCPUs\""));
Log(("APIC: cCpus=%d fR0Enabled=%RTbool fGCEnabled=%RTbool fIOAPIC=%RTbool\n", cCpus, fR0Enabled, fGCEnabled, fIOAPIC));
/* TODO: Current implementation is limited to 32 CPUs due to the use of 32 bits bitmasks. */
if (cCpus > 32)
N_("Configuration error: Invalid value for \"NumCPUs\""));
/*
* Init the data.
*/
/* Use PDMAPICVERSION_X2APIC to activate x2APIC mode */
/*
* We are not freeing this memory, as it's automatically released when guest exits.
*/
rc = MMHyperAlloc(pVM, cCpus*sizeof(APICState), 1, MM_TAG_PDM_DEVICE_USER, (void **)&pThis->pLapicsR3);
if (RT_FAILURE(rc))
return VERR_NO_MEMORY;
{
initApicData(apic, i);
apic++;
}
/*
* Register the APIC.
*/
if (fGCEnabled) {
} else {
}
if (fR0Enabled) {
} else {
}
if (RT_FAILURE(rc))
{
return rc;
}
/*
* The the CPUID feature bit.
*/
if (u32Eax >= 1)
{
if ( fIOAPIC /* If IOAPIC is enabled, enable Local APIC in any case */
|| ( u32Ebx == X86_CPUID_VENDOR_INTEL_EBX
|| ( u32Ebx == X86_CPUID_VENDOR_AMD_EBX
{
LogRel(("Activating Local APIC\n"));
}
}
/*
* Register the MMIO range.
*/
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled) {
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled) {
if (RT_FAILURE(rc))
return rc;
}
/*
* Create the APIC timers.
*/
{
if (RT_FAILURE(rc))
return rc;
apic++;
}
/*
* Saved state.
*/
if (RT_FAILURE(rc))
return rc;
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveIrq, STAMTYPE_COUNTER, "/PDM/APIC/Masked/ActiveIRQ", STAMUNIT_OCCURENCES, "Number of cleared irqs.");
#endif
return VINF_SUCCESS;
}
/**
* APIC device registration structure.
*/
const PDMDEVREG g_DeviceAPIC =
{
/* u32Version */
/* szDeviceName */
"apic",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"Advanced Programmable Interrupt Controller (APIC) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(APICState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
/* IOAPIC */
PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
switch (cb)
{
case 1:
break;
case 2:
break;
case 4:
break;
default:
IOAPIC_UNLOCK(s);
return VERR_INTERNAL_ERROR;
}
IOAPIC_UNLOCK(s);
return VINF_SUCCESS;
}
PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
switch (cb)
{
case 1:
case 2:
case 4:
IOAPIC_UNLOCK(s);
break;
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
{
}
#ifdef IN_RING3
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
ioapic_save(pSSMHandle, s);
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) ioapicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
{
AssertFailed();
}
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRESET
*/
{
ioapic_reset(s);
IOAPIC_UNLOCK(s);
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
bool fGCEnabled;
bool fR0Enabled;
int rc;
/*
* Validate and read the configuration.
*/
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
/*
* Initialize the state data.
*/
ioapic_reset(s);
s->id = 0;
/*
* Register the IOAPIC and get helpers.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Register MMIO callbacks and saved state.
*/
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled) {
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled) {
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in HC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in HC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in HC.");
#endif
return VINF_SUCCESS;
}
/**
* IO APIC device registration structure.
*/
const PDMDEVREG g_DeviceIOAPIC =
{
/* u32Version */
/* szDeviceName */
"ioapic",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"I/O Advanced Programmable Interrupt Controller (IO-APIC) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(IOAPICState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */