VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevAPIC.cpp@ 40907

Last change on this file since 40907 was 40907, checked in by vboxsync, 12 years ago

Working on tracking IRQs for tracing and logging purposes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 88.9 KB
Line 
1/* $Id: DevAPIC.cpp 40907 2012-04-13 20:50:14Z vboxsync $ */
2/** @file
3 * Advanced Programmable Interrupt Controller (APIC) Device.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on:
19 *
20 * apic.c revision 1.5 @@OSETODO
21 *
22 * APIC support
23 *
24 * Copyright (c) 2004-2005 Fabrice Bellard
25 *
26 * This library is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU Lesser General Public
28 * License as published by the Free Software Foundation; either
29 * version 2 of the License, or (at your option) any later version.
30 *
31 * This library is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * Lesser General Public License for more details.
35 *
36 * You should have received a copy of the GNU Lesser General Public
37 * License along with this library; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 */
40
41/*******************************************************************************
42* Header Files *
43*******************************************************************************/
44#define LOG_GROUP LOG_GROUP_DEV_APIC
45#include <VBox/vmm/pdmdev.h>
46
47#include <VBox/log.h>
48#include <VBox/vmm/stam.h>
49#include <VBox/vmm/vmcpuset.h>
50#include <iprt/asm.h>
51#include <iprt/assert.h>
52
53#include <VBox/msi.h>
54
55#include "VBoxDD2.h"
56#include "DevApic.h"
57
58
59/*******************************************************************************
60* Defined Constants And Macros *
61*******************************************************************************/
62#define MSR_IA32_APICBASE 0x1b
63#define MSR_IA32_APICBASE_BSP (1<<8)
64#define MSR_IA32_APICBASE_ENABLE (1<<11)
65#define MSR_IA32_APICBASE_X2ENABLE (1<<10)
66#define MSR_IA32_APICBASE_BASE (0xfffff<<12)
67
68#ifdef _MSC_VER
69# pragma warning(disable:4244)
70#endif
71
72/** The current saved state version.*/
73#define APIC_SAVED_STATE_VERSION 3
74/** The saved state version used by VirtualBox v3 and earlier.
75 * This does not include the config. */
76#define APIC_SAVED_STATE_VERSION_VBOX_30 2
77/** Some ancient version... */
78#define APIC_SAVED_STATE_VERSION_ANCIENT 1
79
80/* version 0x14: Pentium 4, Xeon; LVT count depends on that */
81#define APIC_HW_VERSION 0x14
82
83/** @def APIC_LOCK
84 * Acquires the PDM lock. */
85#define APIC_LOCK(a_pDev, rcBusy) \
86 do { \
87 int rc2 = PDMCritSectEnter((a_pDev)->CTX_SUFF(pCritSect), (rcBusy)); \
88 if (rc2 != VINF_SUCCESS) \
89 return rc2; \
90 } while (0)
91
92/** @def APIC_LOCK_VOID
93 * Acquires the PDM lock and does not expect failure (i.e. ring-3 only!). */
94#define APIC_LOCK_VOID(a_pDev, rcBusy) \
95 do { \
96 int rc2 = PDMCritSectEnter((a_pDev)->CTX_SUFF(pCritSect), (rcBusy)); \
97 AssertLogRelRCReturnVoid(rc2); \
98 } while (0)
99
100/** @def APIC_UNLOCK
101 * Releases the PDM lock. */
102#define APIC_UNLOCK(a_pDev) \
103 PDMCritSectLeave((a_pDev)->CTX_SUFF(pCritSect))
104
105/** @def APIC_AND_TM_LOCK
106 * Acquires the virtual sync clock lock as well as the PDM lock. */
107#define APIC_AND_TM_LOCK(a_pDev, a_pAcpi, rcBusy) \
108 do { \
109 int rc2 = TMTimerLock((a_pAcpi)->CTX_SUFF(pTimer), (rcBusy)); \
110 if (rc2 != VINF_SUCCESS) \
111 return rc2; \
112 rc2 = PDMCritSectEnter((a_pDev)->CTX_SUFF(pCritSect), (rcBusy)); \
113 if (rc2 != VINF_SUCCESS) \
114 { \
115 TMTimerUnlock((a_pAcpi)->CTX_SUFF(pTimer)); \
116 return rc2; \
117 } \
118 } while (0)
119
120/** @def APIC_AND_TM_UNLOCK
121 * Releases the PDM lock as well as the TM virtual sync clock lock. */
122#define APIC_AND_TM_UNLOCK(a_pDev, a_pAcpi) \
123 do { \
124 TMTimerUnlock((a_pAcpi)->CTX_SUFF(pTimer)); \
125 PDMCritSectLeave((a_pDev)->CTX_SUFF(pCritSect)); \
126 } while (0)
127
128/**
129 * Begins an APIC enumeration block.
130 *
131 * Code placed between this and the APIC_FOREACH_END macro will be executed for
132 * each APIC instance present in the system.
133 *
134 * @param a_pDev The APIC device.
135 */
136#define APIC_FOREACH_BEGIN(a_pDev) \
137 do { \
138 VMCPUID const cApics = (a_pDev)->cCpus; \
139 APICState *pCurApic = (a_pDev)->CTX_SUFF(paLapics); \
140 for (VMCPUID iCurApic = 0; iCurApic < cApics; iCurApic++, pCurApic++) \
141 { \
142 do { } while (0)
143
144/**
145 * Begins an APIC enumeration block, given a destination set.
146 *
147 * Code placed between this and the APIC_FOREACH_END macro will be executed for
148 * each APIC instance present in @a a_pDstSet.
149 *
150 * @param a_pDev The APIC device.
151 * @param a_pDstSet The destination set.
152 */
153#define APIC_FOREACH_IN_SET_BEGIN(a_pDev, a_pDstSet) \
154 APIC_FOREACH_BEGIN(a_pDev); \
155 if (!VMCPUSET_IS_PRESENT((a_pDstSet), iCurApic)) \
156 continue; \
157 do { } while (0)
158
159
160/** Counterpart to APIC_FOREACH_IN_SET_BEGIN and APIC_FOREACH_BEGIN. */
161#define APIC_FOREACH_END() \
162 } \
163 } while (0)
164
165#define DEBUG_APIC
166
167/* APIC Local Vector Table */
168#define APIC_LVT_TIMER 0
169#define APIC_LVT_THERMAL 1
170#define APIC_LVT_PERFORM 2
171#define APIC_LVT_LINT0 3
172#define APIC_LVT_LINT1 4
173#define APIC_LVT_ERROR 5
174#define APIC_LVT_NB 6
175
176/* APIC delivery modes */
177#define APIC_DM_FIXED 0
178#define APIC_DM_LOWPRI 1
179#define APIC_DM_SMI 2
180#define APIC_DM_NMI 4
181#define APIC_DM_INIT 5
182#define APIC_DM_SIPI 6
183#define APIC_DM_EXTINT 7
184
185/* APIC destination mode */
186#define APIC_DESTMODE_FLAT 0xf
187#define APIC_DESTMODE_CLUSTER 0x0
188
189#define APIC_TRIGGER_EDGE 0
190#define APIC_TRIGGER_LEVEL 1
191
192#define APIC_LVT_TIMER_PERIODIC (1<<17)
193#define APIC_LVT_MASKED (1<<16)
194#define APIC_LVT_LEVEL_TRIGGER (1<<15)
195#define APIC_LVT_REMOTE_IRR (1<<14)
196#define APIC_INPUT_POLARITY (1<<13)
197#define APIC_SEND_PENDING (1<<12)
198
199#define ESR_ILLEGAL_ADDRESS (1 << 7)
200
201#define APIC_SV_ENABLE (1 << 8)
202
203#define APIC_MAX_PATCH_ATTEMPTS 100
204
205typedef uint32_t PhysApicId;
206typedef uint32_t LogApicId;
207
208
209/*******************************************************************************
210* Structures and Typedefs *
211*******************************************************************************/
212typedef struct APIC256BITREG
213{
214 /** The bitmap data. */
215 uint32_t au32Bitmap[8 /*256/32*/];
216} APIC256BITREG;
217typedef APIC256BITREG *PAPIC256BITREG;
218typedef APIC256BITREG const *PCAPIC256BITREG;
219
220/**
221 * Tests if a bit in the 256-bit APIC register is set.
222 *
223 * @returns true if set, false if clear.
224 *
225 * @param pReg The register.
226 * @param iBit The bit to test for.
227 */
228DECLINLINE(bool) Apic256BitReg_IsBitSet(PCAPIC256BITREG pReg, unsigned iBit)
229{
230 Assert(iBit < 256);
231 return ASMBitTest(&pReg->au32Bitmap[0], iBit);
232}
233
234
235/**
236 * Sets a bit in the 256-bit APIC register is set.
237 *
238 * @param pReg The register.
239 * @param iBit The bit to set.
240 */
241DECLINLINE(void) Apic256BitReg_SetBit(PAPIC256BITREG pReg, unsigned iBit)
242{
243 Assert(iBit < 256);
244 return ASMBitSet(&pReg->au32Bitmap[0], iBit);
245}
246
247
248/**
249 * Clears a bit in the 256-bit APIC register is set.
250 *
251 * @param pReg The register.
252 * @param iBit The bit to clear.
253 */
254DECLINLINE(void) Apic256BitReg_ClearBit(PAPIC256BITREG pReg, unsigned iBit)
255{
256 Assert(iBit < 256);
257 return ASMBitClear(&pReg->au32Bitmap[0], iBit);
258}
259
260/**
261 * Clears all bits in the 256-bit APIC register set.
262 *
263 * @param pReg The register.
264 */
265DECLINLINE(void) Apic256BitReg_Empty(PAPIC256BITREG pReg)
266{
267 memset(&pReg->au32Bitmap[0], 0, sizeof(pReg->au32Bitmap));
268}
269
270/**
271 * Finds the last bit set in the register, i.e. the highest priority interrupt.
272 *
273 * @returns The index of the found bit, @a iRetAllClear if none was found.
274 *
275 * @param pReg The register.
276 * @param iRetAllClear What to return if all bits are clear.
277 */
278static int Apic256BitReg_FindLastSetBit(PCAPIC256BITREG pReg, int iRetAllClear)
279{
280 uint32_t i = RT_ELEMENTS(pReg->au32Bitmap);
281 while (i-- > 0)
282 {
283 uint32_t u = pReg->au32Bitmap[i];
284 if (u)
285 {
286 u = ASMBitLastSetU32(u);
287 u--;
288 u |= i << 5;
289 return (int)u;
290 }
291 }
292 return iRetAllClear;
293}
294
295
296typedef struct APICState
297{
298 uint32_t apicbase;
299 /* Task priority register (interrupt level) */
300 uint32_t tpr;
301 /* Logical APIC id - user programmable */
302 LogApicId id;
303 /* Physical APIC id - not visible to user, constant */
304 PhysApicId phys_id;
305 /** @todo: is it logical or physical? Not really used anyway now. */
306 PhysApicId arb_id;
307 uint32_t spurious_vec;
308 uint8_t log_dest;
309 uint8_t dest_mode;
310 APIC256BITREG isr; /**< in service register */
311 APIC256BITREG tmr; /**< trigger mode register */
312 APIC256BITREG irr; /**< interrupt request register */
313 uint32_t lvt[APIC_LVT_NB];
314 uint32_t esr; /* error register */
315 uint32_t icr[2];
316 uint32_t divide_conf;
317 int count_shift;
318 uint32_t initial_count;
319 uint32_t Alignment0;
320
321 /** The time stamp of the initial_count load, i.e. when it was started. */
322 uint64_t initial_count_load_time;
323 /** The time stamp of the next timer callback. */
324 uint64_t next_time;
325 /** The APIC timer - R3 Ptr. */
326 PTMTIMERR3 pTimerR3;
327 /** The APIC timer - R0 Ptr. */
328 PTMTIMERR0 pTimerR0;
329 /** The APIC timer - RC Ptr. */
330 PTMTIMERRC pTimerRC;
331 /** Whether the timer is armed or not */
332 bool fTimerArmed;
333 /** Alignment */
334 bool afAlignment[3];
335 /** The initial_count value used for the current frequency hint. */
336 uint32_t uHintedInitialCount;
337 /** The count_shift value used for the current frequency hint. */
338 uint32_t uHintedCountShift;
339 /** Timer description timer. */
340 R3PTRTYPE(char *) pszDesc;
341
342 /** The IRQ tags and source IDs for each (tracing purposes). */
343 uint32_t auTags[256];
344
345# ifdef VBOX_WITH_STATISTICS
346# if HC_ARCH_BITS == 32
347 uint32_t u32Alignment0;
348# endif
349 STAMCOUNTER StatTimerSetInitialCount;
350 STAMCOUNTER StatTimerSetInitialCountArm;
351 STAMCOUNTER StatTimerSetInitialCountDisarm;
352 STAMCOUNTER StatTimerSetLvt;
353 STAMCOUNTER StatTimerSetLvtClearPeriodic;
354 STAMCOUNTER StatTimerSetLvtPostponed;
355 STAMCOUNTER StatTimerSetLvtArmed;
356 STAMCOUNTER StatTimerSetLvtArm;
357 STAMCOUNTER StatTimerSetLvtArmRetries;
358 STAMCOUNTER StatTimerSetLvtNoRelevantChange;
359# endif
360
361} APICState;
362
363AssertCompileMemberAlignment(APICState, initial_count_load_time, 8);
364# ifdef VBOX_WITH_STATISTICS
365AssertCompileMemberAlignment(APICState, StatTimerSetInitialCount, 8);
366# endif
367
368typedef struct
369{
370 /** The device instance - R3 Ptr. */
371 PPDMDEVINSR3 pDevInsR3;
372 /** The APIC helpers - R3 Ptr. */
373 PCPDMAPICHLPR3 pApicHlpR3;
374 /** LAPICs states - R3 Ptr */
375 R3PTRTYPE(APICState *) paLapicsR3;
376 /** The critical section - R3 Ptr. */
377 R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
378
379 /** The device instance - R0 Ptr. */
380 PPDMDEVINSR0 pDevInsR0;
381 /** The APIC helpers - R0 Ptr. */
382 PCPDMAPICHLPR0 pApicHlpR0;
383 /** LAPICs states - R0 Ptr */
384 R0PTRTYPE(APICState *) paLapicsR0;
385 /** The critical section - R3 Ptr. */
386 R0PTRTYPE(PPDMCRITSECT) pCritSectR0;
387
388 /** The device instance - RC Ptr. */
389 PPDMDEVINSRC pDevInsRC;
390 /** The APIC helpers - RC Ptr. */
391 PCPDMAPICHLPRC pApicHlpRC;
392 /** LAPICs states - RC Ptr */
393 RCPTRTYPE(APICState *) paLapicsRC;
394 /** The critical section - R3 Ptr. */
395 RCPTRTYPE(PPDMCRITSECT) pCritSectRC;
396
397 /** APIC specification version in this virtual hardware configuration. */
398 PDMAPICVERSION enmVersion;
399
400 /** Number of attempts made to optimize TPR accesses. */
401 uint32_t cTPRPatchAttempts;
402
403 /** Number of CPUs on the system (same as LAPIC count). */
404 uint32_t cCpus;
405 /** Whether we've got an IO APIC or not. */
406 bool fIoApic;
407 /** Alignment padding. */
408 bool afPadding[3];
409
410# ifdef VBOX_WITH_STATISTICS
411 STAMCOUNTER StatMMIOReadGC;
412 STAMCOUNTER StatMMIOReadHC;
413 STAMCOUNTER StatMMIOWriteGC;
414 STAMCOUNTER StatMMIOWriteHC;
415 STAMCOUNTER StatClearedActiveIrq;
416# endif
417} APICDeviceInfo;
418# ifdef VBOX_WITH_STATISTICS
419AssertCompileMemberAlignment(APICDeviceInfo, StatMMIOReadGC, 8);
420# endif
421
422#ifndef VBOX_DEVICE_STRUCT_TESTCASE
423
424/*******************************************************************************
425* Internal Functions *
426*******************************************************************************/
427static void apic_update_tpr(APICDeviceInfo *pDev, APICState* s, uint32_t val);
428
429static void apic_eoi(APICDeviceInfo *pDev, APICState* s); /* */
430static PVMCPUSET apic_get_delivery_bitmask(APICDeviceInfo* pDev, uint8_t dest, uint8_t dest_mode, PVMCPUSET pDstSet);
431static int apic_deliver(APICDeviceInfo* pDev, APICState *s,
432 uint8_t dest, uint8_t dest_mode,
433 uint8_t delivery_mode, uint8_t vector_num,
434 uint8_t polarity, uint8_t trigger_mode);
435static int apic_get_arb_pri(APICState const *s);
436static int apic_get_ppr(APICState const *s);
437static uint32_t apic_get_current_count(APICDeviceInfo const *pDev, APICState const *s);
438static void apicTimerSetInitialCount(APICDeviceInfo *pDev, APICState *s, uint32_t initial_count);
439static void apicTimerSetLvt(APICDeviceInfo *pDev, APICState *pApic, uint32_t fNew);
440static void apicSendInitIpi(APICDeviceInfo* pDev, APICState *s);
441
442static void apic_init_ipi(APICDeviceInfo* pDev, APICState *s);
443static void apic_set_irq(APICDeviceInfo* pDev, APICState *s, int vector_num, int trigger_mode, uint32_t uTagSrc);
444static bool apic_update_irq(APICDeviceInfo* pDev, APICState *s);
445
446
447DECLINLINE(APICState*) getLapicById(APICDeviceInfo *pDev, VMCPUID id)
448{
449 AssertFatalMsg(id < pDev->cCpus, ("CPU id %d out of range\n", id));
450 return &pDev->CTX_SUFF(paLapics)[id];
451}
452
453DECLINLINE(APICState*) getLapic(APICDeviceInfo* pDev)
454{
455 /* LAPIC's array is indexed by CPU id */
456 VMCPUID id = pDev->CTX_SUFF(pApicHlp)->pfnGetCpuId(pDev->CTX_SUFF(pDevIns));
457 return getLapicById(pDev, id);
458}
459
460DECLINLINE(VMCPUID) getCpuFromLapic(APICDeviceInfo* pDev, APICState *s)
461{
462 /* for now we assume LAPIC physical id == CPU id */
463 return VMCPUID(s->phys_id);
464}
465
466DECLINLINE(void) cpuSetInterrupt(APICDeviceInfo* pDev, APICState *s, PDMAPICIRQ enmType = PDMAPICIRQ_HARDWARE)
467{
468 LogFlow(("apic: setting interrupt flag for cpu %d\n", getCpuFromLapic(pDev, s)));
469 pDev->CTX_SUFF(pApicHlp)->pfnSetInterruptFF(pDev->CTX_SUFF(pDevIns), enmType,
470 getCpuFromLapic(pDev, s));
471}
472
473DECLINLINE(void) cpuClearInterrupt(APICDeviceInfo* pDev, APICState *s, PDMAPICIRQ enmType = PDMAPICIRQ_HARDWARE)
474{
475 LogFlow(("apic: clear interrupt flag\n"));
476 pDev->CTX_SUFF(pApicHlp)->pfnClearInterruptFF(pDev->CTX_SUFF(pDevIns), enmType,
477 getCpuFromLapic(pDev, s));
478}
479
480# ifdef IN_RING3
481
482DECLINLINE(void) cpuSendSipi(APICDeviceInfo* pDev, APICState *s, int vector)
483{
484 Log2(("apic: send SIPI vector=%d\n", vector));
485
486 pDev->pApicHlpR3->pfnSendSipi(pDev->pDevInsR3,
487 getCpuFromLapic(pDev, s),
488 vector);
489}
490
491DECLINLINE(void) cpuSendInitIpi(APICDeviceInfo* pDev, APICState *s)
492{
493 Log2(("apic: send init IPI\n"));
494
495 pDev->pApicHlpR3->pfnSendInitIpi(pDev->pDevInsR3,
496 getCpuFromLapic(pDev, s));
497}
498
499# endif /* IN_RING3 */
500
501DECLINLINE(uint32_t) getApicEnableBits(APICDeviceInfo* pDev)
502{
503 switch (pDev->enmVersion)
504 {
505 case PDMAPICVERSION_NONE:
506 return 0;
507 case PDMAPICVERSION_APIC:
508 return MSR_IA32_APICBASE_ENABLE;
509 case PDMAPICVERSION_X2APIC:
510 return MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_X2ENABLE ;
511 default:
512 AssertMsgFailed(("Unsupported APIC version %d\n", pDev->enmVersion));
513 return 0;
514 }
515}
516
517DECLINLINE(PDMAPICVERSION) getApicMode(APICState *apic)
518{
519 switch (((apic->apicbase) >> 10) & 0x3)
520 {
521 case 0:
522 return PDMAPICVERSION_NONE;
523 case 1:
524 default:
525 /* Invalid */
526 return PDMAPICVERSION_NONE;
527 case 2:
528 return PDMAPICVERSION_APIC;
529 case 3:
530 return PDMAPICVERSION_X2APIC;
531 }
532}
533
534static int apic_bus_deliver(APICDeviceInfo* pDev,
535 PCVMCPUSET pDstSet, uint8_t delivery_mode,
536 uint8_t vector_num, uint8_t polarity,
537 uint8_t trigger_mode, uint32_t uTagSrc)
538{
539 LogFlow(("apic_bus_deliver mask=%R[vmcpuset] mode=%x vector=%x polarity=%x trigger_mode=%x uTagSrc=%#x\n",
540 pDstSet, delivery_mode, vector_num, polarity, trigger_mode, uTagSrc));
541
542 switch (delivery_mode)
543 {
544 case APIC_DM_LOWPRI:
545 {
546 VMCPUID idDstCpu = VMCPUSET_FIND_FIRST_PRESENT(pDstSet);
547 if (idDstCpu != NIL_VMCPUID)
548 {
549 APICState *pApic = getLapicById(pDev, idDstCpu);
550 apic_set_irq(pDev, pApic, vector_num, trigger_mode, uTagSrc);
551 }
552 return VINF_SUCCESS;
553 }
554
555 case APIC_DM_FIXED:
556 /** @todo XXX: arbitration */
557 break;
558
559 case APIC_DM_SMI:
560 APIC_FOREACH_IN_SET_BEGIN(pDev, pDstSet);
561 cpuSetInterrupt(pDev, pCurApic, PDMAPICIRQ_SMI);
562 APIC_FOREACH_END();
563 return VINF_SUCCESS;
564
565 case APIC_DM_NMI:
566 APIC_FOREACH_IN_SET_BEGIN(pDev, pDstSet);
567 cpuSetInterrupt(pDev, pCurApic, PDMAPICIRQ_NMI);
568 APIC_FOREACH_END();
569 return VINF_SUCCESS;
570
571 case APIC_DM_INIT:
572 /* normal INIT IPI sent to processors */
573#ifdef IN_RING3
574 APIC_FOREACH_IN_SET_BEGIN(pDev, pDstSet);
575 apicSendInitIpi(pDev, pCurApic);
576 APIC_FOREACH_END();
577 return VINF_SUCCESS;
578#else
579 /* We shall send init IPI only in R3. */
580 return VINF_IOM_R3_MMIO_READ_WRITE;
581#endif /* IN_RING3 */
582
583 case APIC_DM_EXTINT:
584 /* handled in I/O APIC code */
585 break;
586
587 default:
588 return VINF_SUCCESS;
589 }
590
591 APIC_FOREACH_IN_SET_BEGIN(pDev, pDstSet);
592 apic_set_irq(pDev, pCurApic, vector_num, trigger_mode, uTagSrc);
593 APIC_FOREACH_END();
594 return VINF_SUCCESS;
595}
596
597
598PDMBOTHCBDECL(void) apicSetBase(PPDMDEVINS pDevIns, uint64_t val)
599{
600 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
601 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
602 APICState *s = getLapic(pDev); /** @todo fix interface */
603 Log(("apicSetBase: %016RX64\n", val));
604
605 /** @todo: do we need to lock here ? */
606 /* APIC_LOCK_VOID(pDev, VERR_INTERNAL_ERROR); */
607 /** @todo If this change is valid immediately, then we should change the MMIO registration! */
608 /* We cannot change if this CPU is BSP or not by writing to MSR - it's hardwired */
609 PDMAPICVERSION oldMode = getApicMode(s);
610 s->apicbase =
611 (val & 0xfffff000) | /* base */
612 (val & getApicEnableBits(pDev)) | /* mode */
613 (s->apicbase & MSR_IA32_APICBASE_BSP) /* keep BSP bit */;
614 PDMAPICVERSION newMode = getApicMode(s);
615
616 if (oldMode != newMode)
617 {
618 switch (newMode)
619 {
620 case PDMAPICVERSION_NONE:
621 {
622 s->spurious_vec &= ~APIC_SV_ENABLE;
623 /* Clear any pending APIC interrupt action flag. */
624 cpuClearInterrupt(pDev, s);
625 /** @todo: why do we do that? */
626 pDev->CTX_SUFF(pApicHlp)->pfnChangeFeature(pDevIns, PDMAPICVERSION_NONE);
627 break;
628 }
629 case PDMAPICVERSION_APIC:
630 /** @todo: map MMIO ranges, if needed */
631 break;
632 case PDMAPICVERSION_X2APIC:
633 /** @todo: unmap MMIO ranges of this APIC, according to the spec */
634 break;
635 default:
636 break;
637 }
638 }
639 /* APIC_UNLOCK(pDev); */
640}
641
642PDMBOTHCBDECL(uint64_t) apicGetBase(PPDMDEVINS pDevIns)
643{
644 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
645 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
646 APICState *s = getLapic(pDev); /** @todo fix interface */
647 LogFlow(("apicGetBase: %016llx\n", (uint64_t)s->apicbase));
648 return s->apicbase;
649}
650
651PDMBOTHCBDECL(void) apicSetTPR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint8_t val)
652{
653 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
654 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
655 APICState *s = getLapicById(pDev, idCpu);
656 LogFlow(("apicSetTPR: val=%#x (trp %#x -> %#x)\n", val, s->tpr, val));
657 apic_update_tpr(pDev, s, val);
658}
659
660PDMBOTHCBDECL(uint8_t) apicGetTPR(PPDMDEVINS pDevIns, VMCPUID idCpu)
661{
662 /* We don't perform any locking here as that would cause a lot of contention for VT-x/AMD-V. */
663 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
664 APICState *s = getLapicById(pDev, idCpu);
665 Log2(("apicGetTPR: returns %#x\n", s->tpr));
666 return s->tpr;
667}
668
669
670/**
671 * apicWriteRegister helper for dealing with invalid register access.
672 *
673 * @returns Strict VBox status code.
674 * @param pDev The PDM device instance.
675 * @param pApic The APIC being written to.
676 * @param iReg The APIC register index.
677 * @param u64Value The value being written.
678 * @param rcBusy The busy return code to employ. See
679 * PDMCritSectEnter for a description.
680 * @param fMsr Set if called via MSR, clear if MMIO.
681 */
682static int apicWriteRegisterInvalid(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t u64Value,
683 int rcBusy, bool fMsr)
684{
685 Log(("apicWriteRegisterInvalid/%u: iReg=%#x fMsr=%RTbool u64Value=%#llx\n", pApic->phys_id, iReg, fMsr, u64Value));
686 int rc = PDMDevHlpDBGFStop(pDev->CTX_SUFF(pDevIns), RT_SRC_POS,
687 "iReg=%#x fMsr=%RTbool u64Value=%#llx id=%u\n", iReg, fMsr, u64Value, pApic->phys_id);
688 APIC_LOCK(pDev, rcBusy);
689 pApic->esr |= ESR_ILLEGAL_ADDRESS;
690 APIC_UNLOCK(pDev);
691 return rc;
692}
693
694
695
696/**
697 * Writes to an APIC register via MMIO or MSR.
698 *
699 * @returns Strict VBox status code.
700 * @param pDev The PDM device instance.
701 * @param pApic The APIC being written to.
702 * @param iReg The APIC register index.
703 * @param u64Value The value being written.
704 * @param rcBusy The busy return code to employ. See
705 * PDMCritSectEnter for a description.
706 * @param fMsr Set if called via MSR, clear if MMIO.
707 */
708static int apicWriteRegister(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t u64Value,
709 int rcBusy, bool fMsr)
710{
711 Assert(!PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
712
713 int rc = VINF_SUCCESS;
714 switch (iReg)
715 {
716 case 0x02:
717 APIC_LOCK(pDev, rcBusy);
718 pApic->id = (u64Value >> 24); /** @todo r=bird: Is the range supposed to be 40 bits??? */
719 APIC_UNLOCK(pDev);
720 break;
721
722 case 0x03:
723 /* read only, ignore write. */
724 break;
725
726 case 0x08:
727 APIC_LOCK(pDev, rcBusy);
728 apic_update_tpr(pDev, pApic, u64Value);
729 APIC_UNLOCK(pDev);
730 break;
731
732 case 0x09: case 0x0a:
733 Log(("apicWriteRegister: write to read-only register %d ignored\n", iReg));
734 break;
735
736 case 0x0b: /* EOI */
737 APIC_LOCK(pDev, rcBusy);
738 apic_eoi(pDev, pApic);
739 APIC_UNLOCK(pDev);
740 break;
741
742 case 0x0d:
743 APIC_LOCK(pDev, rcBusy);
744 pApic->log_dest = (u64Value >> 24) & 0xff;
745 APIC_UNLOCK(pDev);
746 break;
747
748 case 0x0e:
749 APIC_LOCK(pDev, rcBusy);
750 pApic->dest_mode = u64Value >> 28; /** @todo r=bird: range? This used to be 32-bit before morphed into an MSR handler. */
751 APIC_UNLOCK(pDev);
752 break;
753
754 case 0x0f:
755 APIC_LOCK(pDev, rcBusy);
756 pApic->spurious_vec = u64Value & 0x1ff;
757 apic_update_irq(pDev, pApic);
758 APIC_UNLOCK(pDev);
759 break;
760
761 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
762 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
763 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
764 case 0x28:
765 Log(("apicWriteRegister: write to read-only register %d ignored\n", iReg));
766 break;
767
768 case 0x30:
769 APIC_LOCK(pDev, rcBusy);
770 pApic->icr[0] = (uint32_t)u64Value;
771 if (fMsr) /* Here one of the differences with regular APIC: ICR is single 64-bit register */
772 pApic->icr[1] = (uint32_t)(u64Value >> 32);
773 rc = apic_deliver(pDev, pApic, (pApic->icr[1] >> 24) & 0xff, (pApic->icr[0] >> 11) & 1,
774 (pApic->icr[0] >> 8) & 7, (pApic->icr[0] & 0xff),
775 (pApic->icr[0] >> 14) & 1, (pApic->icr[0] >> 15) & 1);
776 APIC_UNLOCK(pDev);
777 break;
778
779 case 0x31:
780 if (!fMsr)
781 {
782 APIC_LOCK(pDev, rcBusy);
783 pApic->icr[1] = (uint64_t)u64Value;
784 APIC_UNLOCK(pDev);
785 }
786 else
787 rc = apicWriteRegisterInvalid(pDev, pApic, iReg, u64Value, rcBusy, fMsr);
788 break;
789
790 case 0x32 + APIC_LVT_TIMER:
791 AssertCompile(APIC_LVT_TIMER == 0);
792 APIC_AND_TM_LOCK(pDev, pApic, rcBusy);
793 apicTimerSetLvt(pDev, pApic, u64Value);
794 APIC_AND_TM_UNLOCK(pDev, pApic);
795 break;
796
797 case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
798 APIC_LOCK(pDev, rcBusy);
799 pApic->lvt[iReg - 0x32] = u64Value;
800 APIC_UNLOCK(pDev);
801 break;
802
803 case 0x38:
804 APIC_AND_TM_LOCK(pDev, pApic, rcBusy);
805 apicTimerSetInitialCount(pDev, pApic, u64Value);
806 APIC_AND_TM_UNLOCK(pDev, pApic);
807 break;
808
809 case 0x39:
810 Log(("apicWriteRegister: write to read-only register %d ignored\n", iReg));
811 break;
812
813 case 0x3e:
814 {
815 APIC_LOCK(pDev, rcBusy);
816 pApic->divide_conf = u64Value & 0xb;
817 int v = (pApic->divide_conf & 3) | ((pApic->divide_conf >> 1) & 4);
818 pApic->count_shift = (v + 1) & 7;
819 APIC_UNLOCK(pDev);
820 break;
821 }
822
823 case 0x3f:
824 if (fMsr)
825 {
826 /* Self IPI, see x2APIC book 2.4.5 */
827 APIC_LOCK(pDev, rcBusy);
828 int vector = u64Value & 0xff;
829 VMCPUSET SelfSet;
830 VMCPUSET_EMPTY(&SelfSet);
831 VMCPUSET_ADD(&SelfSet, pApic->id);
832 rc = apic_bus_deliver(pDev,
833 &SelfSet,
834 0 /* Delivery mode - fixed */,
835 vector,
836 0 /* Polarity - conform to the bus */,
837 0 /* Trigger mode - edge */,
838 0 /*uTagSrc*/);
839 APIC_UNLOCK(pDev);
840 break;
841 }
842 /* else: fall thru */
843
844 default:
845 rc = apicWriteRegisterInvalid(pDev, pApic, iReg, u64Value, rcBusy, fMsr);
846 break;
847 }
848
849 return rc;
850}
851
852
853/**
854 * apicReadRegister helper for dealing with invalid register access.
855 *
856 * @returns Strict VBox status code.
857 * @param pDev The PDM device instance.
858 * @param pApic The APIC being read to.
859 * @param iReg The APIC register index.
860 * @param pu64Value Where to store the value we've read.
861 * @param rcBusy The busy return code to employ. See
862 * PDMCritSectEnter for a description.
863 * @param fMsr Set if called via MSR, clear if MMIO.
864 */
865static int apicReadRegisterInvalid(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t *pu64Value,
866 int rcBusy, bool fMsr)
867{
868 Log(("apicReadRegisterInvalid/%u: iReg=%#x fMsr=%RTbool\n", pApic->phys_id, iReg, fMsr));
869 int rc = PDMDevHlpDBGFStop(pDev->CTX_SUFF(pDevIns), RT_SRC_POS,
870 "iReg=%#x fMsr=%RTbool id=%u\n", iReg, fMsr, pApic->phys_id);
871 APIC_LOCK(pDev, rcBusy);
872 pApic->esr |= ESR_ILLEGAL_ADDRESS;
873 APIC_UNLOCK(pDev);
874 *pu64Value = 0;
875 return rc;
876}
877
878
879/**
880 * Read from an APIC register via MMIO or MSR.
881 *
882 * @returns Strict VBox status code.
883 * @param pDev The PDM device instance.
884 * @param pApic The APIC being read to.
885 * @param iReg The APIC register index.
886 * @param pu64Value Where to store the value we've read.
887 * @param rcBusy The busy return code to employ. See
888 * PDMCritSectEnter for a description.
889 * @param fMsr Set if called via MSR, clear if MMIO.
890 */
891static int apicReadRegister(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t *pu64Value,
892 int rcBusy, bool fMsr)
893{
894 Assert(!PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
895
896 int rc = VINF_SUCCESS;
897 switch (iReg)
898 {
899 case 0x02: /* id */
900 APIC_LOCK(pDev, rcBusy);
901 *pu64Value = pApic->id << 24;
902 APIC_UNLOCK(pDev);
903 break;
904
905 case 0x03: /* version */
906 APIC_LOCK(pDev, rcBusy);
907 *pu64Value = APIC_HW_VERSION
908 | ((APIC_LVT_NB - 1) << 16) /* Max LVT index */
909#if 0
910 | (0 << 24) /* Support for EOI broadcast suppression */
911#endif
912 ;
913 APIC_UNLOCK(pDev);
914 break;
915
916 case 0x08:
917 APIC_LOCK(pDev, rcBusy);
918 *pu64Value = pApic->tpr;
919 APIC_UNLOCK(pDev);
920 break;
921
922 case 0x09:
923 *pu64Value = apic_get_arb_pri(pApic);
924 break;
925
926 case 0x0a:
927 /* ppr */
928 APIC_LOCK(pDev, rcBusy);
929 *pu64Value = apic_get_ppr(pApic);
930 APIC_UNLOCK(pDev);
931 break;
932
933 case 0x0b:
934 Log(("apicReadRegister: %x -> write only returning 0\n", iReg));
935 *pu64Value = 0;
936 break;
937
938 case 0x0d:
939 APIC_LOCK(pDev, rcBusy);
940 *pu64Value = (uint64_t)pApic->log_dest << 24;
941 APIC_UNLOCK(pDev);
942 break;
943
944 case 0x0e:
945 /* Bottom 28 bits are always 1 */
946 APIC_LOCK(pDev, rcBusy);
947 *pu64Value = ((uint64_t)pApic->dest_mode << 28) | UINT32_C(0xfffffff);
948 APIC_UNLOCK(pDev);
949 break;
950
951 case 0x0f:
952 APIC_LOCK(pDev, rcBusy);
953 *pu64Value = pApic->spurious_vec;
954 APIC_UNLOCK(pDev);
955 break;
956
957 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
958 APIC_LOCK(pDev, rcBusy);
959 *pu64Value = pApic->isr.au32Bitmap[iReg & 7];
960 APIC_UNLOCK(pDev);
961 break;
962
963 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
964 APIC_LOCK(pDev, rcBusy);
965 *pu64Value = pApic->tmr.au32Bitmap[iReg & 7];
966 APIC_UNLOCK(pDev);
967 break;
968
969 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
970 APIC_LOCK(pDev, rcBusy);
971 *pu64Value = pApic->irr.au32Bitmap[iReg & 7];
972 APIC_UNLOCK(pDev);
973 break;
974
975 case 0x28:
976 APIC_LOCK(pDev, rcBusy);
977 *pu64Value = pApic->esr;
978 APIC_UNLOCK(pDev);
979 break;
980
981 case 0x30:
982 /* Here one of the differences with regular APIC: ICR is single 64-bit register */
983 APIC_LOCK(pDev, rcBusy);
984 if (fMsr)
985 *pu64Value = RT_MAKE_U64(pApic->icr[0], pApic->icr[1]);
986 else
987 *pu64Value = pApic->icr[0];
988 APIC_UNLOCK(pDev);
989 break;
990
991 case 0x31:
992 if (fMsr)
993 rc = apicReadRegisterInvalid(pDev, pApic, iReg, pu64Value, rcBusy, fMsr);
994 else
995 {
996 APIC_LOCK(pDev, rcBusy);
997 *pu64Value = pApic->icr[1];
998 APIC_UNLOCK(pDev);
999 }
1000 break;
1001
1002 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1003 APIC_LOCK(pDev, rcBusy);
1004 *pu64Value = pApic->lvt[iReg - 0x32];
1005 APIC_UNLOCK(pDev);
1006 break;
1007
1008 case 0x38:
1009 APIC_LOCK(pDev, rcBusy);
1010 *pu64Value = pApic->initial_count;
1011 APIC_UNLOCK(pDev);
1012 break;
1013
1014 case 0x39:
1015 APIC_AND_TM_LOCK(pDev, pApic, rcBusy);
1016 *pu64Value = apic_get_current_count(pDev, pApic);
1017 APIC_AND_TM_UNLOCK(pDev, pApic);
1018 break;
1019
1020 case 0x3e:
1021 APIC_LOCK(pDev, rcBusy);
1022 *pu64Value = pApic->divide_conf;
1023 APIC_UNLOCK(pDev);
1024 break;
1025
1026 case 0x3f:
1027 if (fMsr)
1028 {
1029 /* Self IPI register is write only */
1030 Log(("apicReadMSR: read from write-only register %d ignored\n", iReg));
1031 *pu64Value = 0;
1032 }
1033 else
1034 rc = apicReadRegisterInvalid(pDev, pApic, iReg, pu64Value, rcBusy, fMsr);
1035 break;
1036 case 0x2f: /** @todo Correctable machine check exception vector, implement me! */
1037 default:
1038 /**
1039 * @todo: according to spec when APIC writes to ESR it msut raise error interrupt,
1040 * i.e. LVT[5]
1041 */
1042 rc = apicReadRegisterInvalid(pDev, pApic, iReg, pu64Value, rcBusy, fMsr);
1043 break;
1044 }
1045 return rc;
1046}
1047
1048/**
1049 * @interface_method_impl{PDMAPICREG,pfnWriteMSRR3}
1050 */
1051PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t u64Value)
1052{
1053 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1054 if (pDev->enmVersion < PDMAPICVERSION_X2APIC)
1055 return VERR_EM_INTERPRETER; /** @todo tell the caller to raise hell (\#GP(0)). */
1056
1057 APICState *pApic = getLapicById(pDev, idCpu);
1058 uint32_t iReg = (u32Reg - MSR_IA32_APIC_START) & 0xff;
1059 return apicWriteRegister(pDev, pApic, iReg, u64Value, VINF_SUCCESS /*rcBusy*/, true /*fMsr*/);
1060}
1061
1062
1063/**
1064 * @interface_method_impl{PDMAPICREG,pfnReadMSRR3}
1065 */
1066PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t *pu64Value)
1067{
1068 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1069 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
1070
1071 if (pDev->enmVersion < PDMAPICVERSION_X2APIC)
1072 return VERR_EM_INTERPRETER;
1073
1074 APICState *pApic = getLapicById(pDev, idCpu);
1075 uint32_t iReg = (u32Reg - MSR_IA32_APIC_START) & 0xff;
1076 return apicReadRegister(pDev, pApic, iReg, pu64Value, VINF_SUCCESS /*rcBusy*/, true /*fMsr*/);
1077}
1078
1079/**
1080 * More or less private interface between IOAPIC, only PDM is responsible
1081 * for connecting the two devices.
1082 */
1083PDMBOTHCBDECL(int) apicBusDeliverCallback(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
1084 uint8_t u8DeliveryMode, uint8_t iVector, uint8_t u8Polarity,
1085 uint8_t u8TriggerMode, uint32_t uTagSrc)
1086{
1087 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1088 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
1089 LogFlow(("apicBusDeliverCallback: pDevIns=%p u8Dest=%#x u8DestMode=%#x u8DeliveryMode=%#x iVector=%#x u8Polarity=%#x u8TriggerMode=%#x uTagSrc=%#x\n",
1090 pDevIns, u8Dest, u8DestMode, u8DeliveryMode, iVector, u8Polarity, u8TriggerMode, uTagSrc));
1091 VMCPUSET DstSet;
1092 return apic_bus_deliver(pDev, apic_get_delivery_bitmask(pDev, u8Dest, u8DestMode, &DstSet),
1093 u8DeliveryMode, iVector, u8Polarity, u8TriggerMode, uTagSrc);
1094}
1095
1096/**
1097 * Local interrupt delivery, for devices attached to the CPU's LINT0/LINT1 pin.
1098 * Normally used for 8259A PIC and NMI.
1099 */
1100PDMBOTHCBDECL(int) apicLocalInterrupt(PPDMDEVINS pDevIns, uint8_t u8Pin, uint8_t u8Level)
1101{
1102 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1103 APICState *s = getLapicById(pDev, 0);
1104
1105 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
1106 LogFlow(("apicLocalInterrupt: pDevIns=%p u8Pin=%x u8Level=%x\n", pDevIns, u8Pin, u8Level));
1107
1108 /* If LAPIC is disabled, go straight to the CPU. */
1109 if (!(s->spurious_vec & APIC_SV_ENABLE))
1110 {
1111 LogFlow(("apicLocalInterrupt: LAPIC disabled, delivering directly to CPU core.\n"));
1112 if (u8Level)
1113 cpuSetInterrupt(pDev, s, PDMAPICIRQ_EXTINT);
1114 else
1115 cpuClearInterrupt(pDev, s, PDMAPICIRQ_EXTINT);
1116
1117 return VINF_SUCCESS;
1118 }
1119
1120 /* If LAPIC is enabled, interrupts are subject to LVT programming. */
1121
1122 /* There are only two local interrupt pins. */
1123 AssertMsgReturn(u8Pin <= 1, ("Invalid LAPIC pin %d\n", u8Pin), VERR_INVALID_PARAMETER);
1124
1125 /* NB: We currently only deliver local interrupts to the first CPU. In theory they
1126 * should be delivered to all CPUs and it is the guest's responsibility to ensure
1127 * no more than one CPU has the interrupt unmasked.
1128 */
1129 uint32_t u32Lvec;
1130
1131 u32Lvec = s->lvt[APIC_LVT_LINT0 + u8Pin]; /* Fetch corresponding LVT entry. */
1132 /* Drop int if entry is masked. May not be correct for level-triggered interrupts. */
1133 if (!(u32Lvec & APIC_LVT_MASKED))
1134 { uint8_t u8Delivery;
1135 PDMAPICIRQ enmType;
1136
1137 u8Delivery = (u32Lvec >> 8) & 7;
1138 switch (u8Delivery)
1139 {
1140 case APIC_DM_EXTINT:
1141 Assert(u8Pin == 0); /* PIC should be wired to LINT0. */
1142 enmType = PDMAPICIRQ_EXTINT;
1143 /* ExtINT can be both set and cleared, NMI/SMI/INIT can only be set. */
1144 LogFlow(("apicLocalInterrupt: %s ExtINT interrupt\n", u8Level ? "setting" : "clearing"));
1145 if (u8Level)
1146 cpuSetInterrupt(pDev, s, enmType);
1147 else
1148 cpuClearInterrupt(pDev, s, enmType);
1149 return VINF_SUCCESS;
1150 case APIC_DM_NMI:
1151 /* External NMI should be wired to LINT1, but Linux sometimes programs
1152 * LVT0 to NMI delivery mode as well.
1153 */
1154 enmType = PDMAPICIRQ_NMI;
1155 /* Currently delivering NMIs through here causes problems with NMI watchdogs
1156 * on certain Linux kernels, e.g. 64-bit CentOS 5.3. Disable NMIs for now.
1157 */
1158 return VINF_SUCCESS;
1159 case APIC_DM_SMI:
1160 enmType = PDMAPICIRQ_SMI;
1161 break;
1162 case APIC_DM_FIXED:
1163 {
1164 /** @todo implement APIC_DM_FIXED! */
1165 static unsigned s_c = 0;
1166 if (s_c++ < 5)
1167 LogRel(("delivery type APIC_DM_FIXED not implemented. u8Pin=%d u8Level=%d\n", u8Pin, u8Level));
1168 return VINF_SUCCESS;
1169 }
1170 case APIC_DM_INIT:
1171 /** @todo implement APIC_DM_INIT? */
1172 default:
1173 {
1174 static unsigned s_c = 0;
1175 if (s_c++ < 100)
1176 AssertLogRelMsgFailed(("delivery type %d not implemented. u8Pin=%d u8Level=%d\n", u8Delivery, u8Pin, u8Level));
1177 return VERR_INTERNAL_ERROR_4;
1178 }
1179 }
1180 LogFlow(("apicLocalInterrupt: setting local interrupt type %d\n", enmType));
1181 cpuSetInterrupt(pDev, s, enmType);
1182 }
1183 return VINF_SUCCESS;
1184}
1185
1186static int apic_get_ppr(APICState const *s)
1187{
1188 int ppr;
1189
1190 int tpr = (s->tpr >> 4);
1191 int isrv = Apic256BitReg_FindLastSetBit(&s->isr, 0);
1192 isrv >>= 4;
1193 if (tpr >= isrv)
1194 ppr = s->tpr;
1195 else
1196 ppr = isrv << 4;
1197 return ppr;
1198}
1199
1200static int apic_get_ppr_zero_tpr(APICState *s)
1201{
1202 return Apic256BitReg_FindLastSetBit(&s->isr, 0);
1203}
1204
1205static int apic_get_arb_pri(APICState const *s)
1206{
1207 /** @todo XXX: arbitration */
1208 return 0;
1209}
1210
1211/* signal the CPU if an irq is pending */
1212static bool apic_update_irq(APICDeviceInfo *pDev, APICState* s)
1213{
1214 if (!(s->spurious_vec & APIC_SV_ENABLE))
1215 {
1216 /* Clear any pending APIC interrupt action flag. */
1217 cpuClearInterrupt(pDev, s);
1218 return false;
1219 }
1220
1221 int irrv = Apic256BitReg_FindLastSetBit(&s->irr, -1);
1222 if (irrv < 0)
1223 return false;
1224 int ppr = apic_get_ppr(s);
1225 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
1226 return false;
1227 cpuSetInterrupt(pDev, s);
1228 return true;
1229}
1230
1231/* Check if the APIC has a pending interrupt/if a TPR change would active one. */
1232PDMBOTHCBDECL(bool) apicHasPendingIrq(PPDMDEVINS pDevIns)
1233{
1234 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1235 if (!pDev)
1236 return false;
1237
1238 /* We don't perform any locking here as that would cause a lot of contention for VT-x/AMD-V. */
1239
1240 APICState *s = getLapic(pDev); /** @todo fix interface */
1241
1242 /*
1243 * All our callbacks now come from single IOAPIC, thus locking
1244 * seems to be excessive now
1245 */
1246 /** @todo check excessive locking whatever... */
1247 int irrv = Apic256BitReg_FindLastSetBit(&s->irr, -1);
1248 if (irrv < 0)
1249 return false;
1250
1251 int ppr = apic_get_ppr_zero_tpr(s);
1252
1253 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
1254 return false;
1255
1256 return true;
1257}
1258
1259static void apic_update_tpr(APICDeviceInfo *pDev, APICState* s, uint32_t val)
1260{
1261 bool fIrqIsActive = false;
1262 bool fIrqWasActive = false;
1263
1264 fIrqWasActive = apic_update_irq(pDev, s);
1265 s->tpr = val;
1266 fIrqIsActive = apic_update_irq(pDev, s);
1267
1268 /* If an interrupt is pending and now masked, then clear the FF flag. */
1269 if (fIrqWasActive && !fIrqIsActive)
1270 {
1271 Log(("apic_update_tpr: deactivate interrupt that was masked by the TPR update (%x)\n", val));
1272 STAM_COUNTER_INC(&pDev->StatClearedActiveIrq);
1273 cpuClearInterrupt(pDev, s);
1274 }
1275}
1276
1277static void apic_set_irq(APICDeviceInfo *pDev, APICState* s, int vector_num, int trigger_mode, uint32_t uTagSrc)
1278{
1279 LogFlow(("CPU%d: apic_set_irq vector=%x trigger_mode=%x uTagSrc=%#x\n", s->phys_id, vector_num, trigger_mode, uTagSrc));
1280
1281 Apic256BitReg_SetBit(&s->irr, vector_num);
1282 if (trigger_mode)
1283 Apic256BitReg_SetBit(&s->tmr, vector_num);
1284 else
1285 Apic256BitReg_ClearBit(&s->tmr, vector_num);
1286
1287 if (!s->auTags[vector_num])
1288 s->auTags[vector_num] = uTagSrc;
1289 else
1290 s->auTags[vector_num] |= RT_BIT_32(31);
1291
1292 apic_update_irq(pDev, s);
1293}
1294
1295static void apic_eoi(APICDeviceInfo *pDev, APICState* s)
1296{
1297 int isrv = Apic256BitReg_FindLastSetBit(&s->isr, -1);
1298 if (isrv < 0)
1299 return;
1300 Apic256BitReg_ClearBit(&s->isr, isrv);
1301 LogFlow(("CPU%d: apic_eoi isrv=%x\n", s->phys_id, isrv));
1302 /** @todo XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
1303 * set the remote IRR bit for level triggered interrupts. */
1304 apic_update_irq(pDev, s);
1305}
1306
1307static PVMCPUSET apic_get_delivery_bitmask(APICDeviceInfo *pDev, uint8_t dest, uint8_t dest_mode, PVMCPUSET pDstSet)
1308{
1309 VMCPUSET_EMPTY(pDstSet);
1310
1311 if (dest_mode == 0)
1312 {
1313 if (dest == 0xff) /* The broadcast ID. */
1314 VMCPUSET_FILL(pDstSet);
1315 else
1316 VMCPUSET_ADD(pDstSet, dest);
1317 }
1318 else
1319 {
1320 /** @todo XXX: cluster mode */
1321 APIC_FOREACH_BEGIN(pDev);
1322 if (pCurApic->dest_mode == APIC_DESTMODE_FLAT)
1323 {
1324 if (dest & pCurApic->log_dest)
1325 VMCPUSET_ADD(pDstSet, iCurApic);
1326 }
1327 else if (pCurApic->dest_mode == APIC_DESTMODE_CLUSTER)
1328 {
1329 if ( (dest & 0xf0) == (pCurApic->log_dest & 0xf0)
1330 && (dest & pCurApic->log_dest & 0x0f))
1331 VMCPUSET_ADD(pDstSet, iCurApic);
1332 }
1333 APIC_FOREACH_END();
1334 }
1335
1336 return pDstSet;
1337}
1338
1339#ifdef IN_RING3
1340static void apic_init_ipi(APICDeviceInfo* pDev, APICState *s)
1341{
1342 int i;
1343
1344 for(i = 0; i < APIC_LVT_NB; i++)
1345 s->lvt[i] = 1 << 16; /* mask LVT */
1346 s->tpr = 0;
1347 s->spurious_vec = 0xff;
1348 s->log_dest = 0;
1349 s->dest_mode = 0xff; /** @todo 0xff???? */
1350 Apic256BitReg_Empty(&s->isr);
1351 Apic256BitReg_Empty(&s->tmr);
1352 Apic256BitReg_Empty(&s->irr);
1353 s->esr = 0;
1354 memset(s->icr, 0, sizeof(s->icr));
1355 s->divide_conf = 0;
1356 s->count_shift = 1;
1357 s->initial_count = 0;
1358 s->initial_count_load_time = 0;
1359 s->next_time = 0;
1360}
1361
1362
1363static void apicSendInitIpi(APICDeviceInfo* pDev, APICState *s)
1364{
1365 apic_init_ipi(pDev, s);
1366 cpuSendInitIpi(pDev, s);
1367}
1368
1369/* send a SIPI message to the CPU to start it */
1370static void apic_startup(APICDeviceInfo* pDev, APICState *s, int vector_num)
1371{
1372 Log(("[SMP] apic_startup: %d on CPUs %d\n", vector_num, s->phys_id));
1373 cpuSendSipi(pDev, s, vector_num);
1374}
1375#endif /* IN_RING3 */
1376
1377static int apic_deliver(APICDeviceInfo *pDev, APICState *s,
1378 uint8_t dest, uint8_t dest_mode,
1379 uint8_t delivery_mode, uint8_t vector_num,
1380 uint8_t polarity, uint8_t trigger_mode)
1381{
1382 int dest_shorthand = (s->icr[0] >> 18) & 3;
1383 LogFlow(("apic_deliver dest=%x dest_mode=%x dest_shorthand=%x delivery_mode=%x vector_num=%x polarity=%x trigger_mode=%x uTagSrc=%#x\n", dest, dest_mode, dest_shorthand, delivery_mode, vector_num, polarity, trigger_mode));
1384
1385 VMCPUSET DstSet;
1386 switch (dest_shorthand)
1387 {
1388 case 0:
1389 apic_get_delivery_bitmask(pDev, dest, dest_mode, &DstSet);
1390 break;
1391 case 1:
1392 VMCPUSET_EMPTY(&DstSet);
1393 VMCPUSET_ADD(&DstSet, s->id);
1394 break;
1395 case 2:
1396 VMCPUSET_FILL(&DstSet);
1397 break;
1398 case 3:
1399 VMCPUSET_FILL(&DstSet);
1400 VMCPUSET_DEL(&DstSet, s->id);
1401 break;
1402 }
1403
1404 switch (delivery_mode)
1405 {
1406 case APIC_DM_INIT:
1407 {
1408 uint32_t const trig_mode = (s->icr[0] >> 15) & 1;
1409 uint32_t const level = (s->icr[0] >> 14) & 1;
1410 if (level == 0 && trig_mode == 1)
1411 {
1412 APIC_FOREACH_IN_SET_BEGIN(pDev, &DstSet);
1413 pCurApic->arb_id = pCurApic->id;
1414 APIC_FOREACH_END();
1415 Log(("CPU%d: APIC_DM_INIT arbitration id(s) set\n", s->phys_id));
1416 return VINF_SUCCESS;
1417 }
1418 break;
1419 }
1420
1421 case APIC_DM_SIPI:
1422# ifdef IN_RING3
1423 APIC_FOREACH_IN_SET_BEGIN(pDev, &DstSet);
1424 apic_startup(pDev, pCurApic, vector_num);
1425 APIC_FOREACH_END();
1426 return VINF_SUCCESS;
1427# else
1428 /* We shall send SIPI only in R3, R0 calls should be
1429 rescheduled to R3 */
1430 return VINF_IOM_R3_MMIO_WRITE;
1431# endif
1432 }
1433
1434 return apic_bus_deliver(pDev, &DstSet, delivery_mode, vector_num,
1435 polarity, trigger_mode, 0 /* uTagSrc*/);
1436}
1437
1438
1439PDMBOTHCBDECL(int) apicGetInterrupt(PPDMDEVINS pDevIns, uint32_t *puTagSrc)
1440{
1441 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1442 /* if the APIC is not installed or enabled, we let the 8259 handle the
1443 IRQs */
1444 if (!pDev)
1445 {
1446 Log(("apic_get_interrupt: returns -1 (!s)\n"));
1447 return -1;
1448 }
1449
1450 Assert(PDMCritSectIsOwner(pDev->CTX_SUFF(pCritSect)));
1451
1452 APICState *s = getLapic(pDev); /** @todo fix interface */
1453
1454 if (!(s->spurious_vec & APIC_SV_ENABLE))
1455 {
1456 Log(("CPU%d: apic_get_interrupt: returns -1 (APIC_SV_ENABLE)\n", s->phys_id));
1457 return -1;
1458 }
1459
1460 /** @todo XXX: spurious IRQ handling */
1461 int intno = Apic256BitReg_FindLastSetBit(&s->irr, -1);
1462 if (intno < 0)
1463 {
1464 Log(("CPU%d: apic_get_interrupt: returns -1 (irr)\n", s->phys_id));
1465 return -1;
1466 }
1467
1468 if (s->tpr && (uint32_t)intno <= s->tpr)
1469 {
1470 *puTagSrc = 0;
1471 Log(("apic_get_interrupt: returns %d (sp)\n", s->spurious_vec & 0xff));
1472 return s->spurious_vec & 0xff;
1473 }
1474
1475 Apic256BitReg_ClearBit(&s->irr, intno);
1476 Apic256BitReg_SetBit(&s->isr, intno);
1477
1478 *puTagSrc = s->auTags[intno];
1479 s->auTags[intno] = 0;
1480
1481 apic_update_irq(pDev, s);
1482
1483 LogFlow(("CPU%d: apic_get_interrupt: returns %d / %#x\n", s->phys_id, intno, *puTagSrc));
1484 return intno;
1485}
1486
1487/**
1488 * @remarks Caller (apicReadRegister) takes both the TM and APIC locks before
1489 * calling this function.
1490 */
1491static uint32_t apic_get_current_count(APICDeviceInfo const *pDev, APICState const *pApic)
1492{
1493 int64_t d = (TMTimerGet(pApic->CTX_SUFF(pTimer)) - pApic->initial_count_load_time)
1494 >> pApic->count_shift;
1495
1496 uint32_t val;
1497 if (pApic->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC)
1498 /* periodic */
1499 val = pApic->initial_count - (d % ((uint64_t)pApic->initial_count + 1));
1500 else if (d >= pApic->initial_count)
1501 val = 0;
1502 else
1503 val = pApic->initial_count - d;
1504
1505 return val;
1506}
1507
1508/**
1509 * Does the frequency hinting and logging.
1510 *
1511 * @param pApic The device state.
1512 */
1513DECLINLINE(void) apicDoFrequencyHinting(APICState *pApic)
1514{
1515 if ( pApic->uHintedInitialCount != pApic->initial_count
1516 || pApic->uHintedCountShift != (uint32_t)pApic->count_shift)
1517 {
1518 pApic->uHintedInitialCount = pApic->initial_count;
1519 pApic->uHintedCountShift = pApic->count_shift;
1520
1521 uint32_t uHz;
1522 if (pApic->initial_count > 0)
1523 {
1524 Assert((unsigned)pApic->count_shift < 30);
1525 uint64_t cTickPerPeriod = ((uint64_t)pApic->initial_count + 1) << pApic->count_shift;
1526 uHz = TMTimerGetFreq(pApic->CTX_SUFF(pTimer)) / cTickPerPeriod;
1527 }
1528 else
1529 uHz = 0;
1530 TMTimerSetFrequencyHint(pApic->CTX_SUFF(pTimer), uHz);
1531 Log(("apic: %u Hz\n", uHz));
1532 }
1533}
1534
1535/**
1536 * Implementation of the 0380h access: Timer reset + new initial count.
1537 *
1538 * @param pDev The device state.
1539 * @param pApic The APIC sub-device state.
1540 * @param u32NewInitialCount The new initial count for the timer.
1541 */
1542static void apicTimerSetInitialCount(APICDeviceInfo *pDev, APICState *pApic, uint32_t u32NewInitialCount)
1543{
1544 STAM_COUNTER_INC(&pApic->StatTimerSetInitialCount);
1545 pApic->initial_count = u32NewInitialCount;
1546
1547 /*
1548 * Don't (re-)arm the timer if the it's masked or if it's
1549 * a zero length one-shot timer.
1550 */
1551 if ( !(pApic->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)
1552 && u32NewInitialCount > 0)
1553 {
1554 /*
1555 * Calculate the relative next time and perform a combined timer get/set
1556 * operation. This avoids racing the clock between get and set.
1557 */
1558 uint64_t cTicksNext = u32NewInitialCount;
1559 cTicksNext += 1;
1560 cTicksNext <<= pApic->count_shift;
1561 TMTimerSetRelative(pApic->CTX_SUFF(pTimer), cTicksNext, &pApic->initial_count_load_time);
1562 pApic->next_time = pApic->initial_count_load_time + cTicksNext;
1563 pApic->fTimerArmed = true;
1564 apicDoFrequencyHinting(pApic);
1565 STAM_COUNTER_INC(&pApic->StatTimerSetInitialCountArm);
1566 Log(("apicTimerSetInitialCount: cTicksNext=%'llu (%#llx) ic=%#x sh=%#x nxt=%#llx\n",
1567 cTicksNext, cTicksNext, u32NewInitialCount, pApic->count_shift, pApic->next_time));
1568 }
1569 else
1570 {
1571 /* Stop it if necessary and record the load time for unmasking. */
1572 if (pApic->fTimerArmed)
1573 {
1574 STAM_COUNTER_INC(&pApic->StatTimerSetInitialCountDisarm);
1575 TMTimerStop(pApic->CTX_SUFF(pTimer));
1576 pApic->fTimerArmed = false;
1577 pApic->uHintedCountShift = pApic->uHintedInitialCount = 0;
1578 }
1579 pApic->initial_count_load_time = TMTimerGet(pApic->CTX_SUFF(pTimer));
1580 Log(("apicTimerSetInitialCount: ic=%#x sh=%#x iclt=%#llx\n", u32NewInitialCount, pApic->count_shift, pApic->initial_count_load_time));
1581 }
1582}
1583
1584/**
1585 * Implementation of the 0320h access: change the LVT flags.
1586 *
1587 * @param pDev The device state.
1588 * @param pApic The APIC sub-device state to operate on.
1589 * @param fNew The new flags.
1590 */
1591static void apicTimerSetLvt(APICDeviceInfo *pDev, APICState *pApic, uint32_t fNew)
1592{
1593 STAM_COUNTER_INC(&pApic->StatTimerSetLvt);
1594
1595 /*
1596 * Make the flag change, saving the old ones so we can avoid
1597 * unnecessary work.
1598 */
1599 uint32_t const fOld = pApic->lvt[APIC_LVT_TIMER];
1600 pApic->lvt[APIC_LVT_TIMER] = fNew;
1601
1602 /* Only the masked and peridic bits are relevant (see apic_timer_update). */
1603 if ( (fOld & (APIC_LVT_MASKED | APIC_LVT_TIMER_PERIODIC))
1604 != (fNew & (APIC_LVT_MASKED | APIC_LVT_TIMER_PERIODIC)))
1605 {
1606 /*
1607 * If changed to one-shot from periodic, stop the timer if we're not
1608 * in the first period.
1609 */
1610 /** @todo check how clearing the periodic flag really should behave when not
1611 * in period 1. The current code just mirrors the behavior of the
1612 * original implementation. */
1613 if ( (fOld & APIC_LVT_TIMER_PERIODIC)
1614 && !(fNew & APIC_LVT_TIMER_PERIODIC))
1615 {
1616 STAM_COUNTER_INC(&pApic->StatTimerSetLvtClearPeriodic);
1617 uint64_t cTicks = (pApic->next_time - pApic->initial_count_load_time) >> pApic->count_shift;
1618 if (cTicks >= pApic->initial_count)
1619 {
1620 /* not first period, stop it. */
1621 TMTimerStop(pApic->CTX_SUFF(pTimer));
1622 pApic->fTimerArmed = false;
1623 pApic->uHintedCountShift = pApic->uHintedInitialCount = 0;
1624 }
1625 /* else: first period, let it fire normally. */
1626 }
1627
1628 /*
1629 * We postpone stopping the timer when it's masked, this way we can
1630 * avoid some timer work when the guest temporarily masks the timer.
1631 * (apicR3TimerCallback will stop it if still masked.)
1632 */
1633 if (fNew & APIC_LVT_MASKED)
1634 STAM_COUNTER_INC(&pApic->StatTimerSetLvtPostponed);
1635 else if (pApic->fTimerArmed)
1636 STAM_COUNTER_INC(&pApic->StatTimerSetLvtArmed);
1637 /*
1638 * If unmasked, not armed and with a valid initial count value (according
1639 * to our interpretation of the spec), we will have to rearm the timer so
1640 * it will fire at the end of the current period.
1641 *
1642 * N.B. This is code is currently RACING the virtual sync clock!
1643 */
1644 else if ( (fOld & APIC_LVT_MASKED)
1645 && pApic->initial_count > 0)
1646 {
1647 STAM_COUNTER_INC(&pApic->StatTimerSetLvtArm);
1648 for (unsigned cTries = 0; ; cTries++)
1649 {
1650 uint64_t NextTS;
1651 uint64_t cTicks = (TMTimerGet(pApic->CTX_SUFF(pTimer)) - pApic->initial_count_load_time) >> pApic->count_shift;
1652 if (fNew & APIC_LVT_TIMER_PERIODIC)
1653 NextTS = ((cTicks / ((uint64_t)pApic->initial_count + 1)) + 1) * ((uint64_t)pApic->initial_count + 1);
1654 else
1655 {
1656 if (cTicks >= pApic->initial_count)
1657 break;
1658 NextTS = (uint64_t)pApic->initial_count + 1;
1659 }
1660 NextTS <<= pApic->count_shift;
1661 NextTS += pApic->initial_count_load_time;
1662
1663 /* Try avoid the assertion in TM.cpp... this isn't perfect! */
1664 if ( NextTS > TMTimerGet(pApic->CTX_SUFF(pTimer))
1665 || cTries > 10)
1666 {
1667 TMTimerSet(pApic->CTX_SUFF(pTimer), NextTS);
1668 pApic->next_time = NextTS;
1669 pApic->fTimerArmed = true;
1670 apicDoFrequencyHinting(pApic);
1671 Log(("apicTimerSetLvt: ic=%#x sh=%#x nxt=%#llx\n", pApic->initial_count, pApic->count_shift, pApic->next_time));
1672 break;
1673 }
1674 STAM_COUNTER_INC(&pApic->StatTimerSetLvtArmRetries);
1675 }
1676 }
1677 }
1678 else
1679 STAM_COUNTER_INC(&pApic->StatTimerSetLvtNoRelevantChange);
1680}
1681
1682# ifdef IN_RING3
1683/**
1684 * Timer callback function.
1685 *
1686 * @param pDevIns The device state.
1687 * @param pTimer The timer handle.
1688 * @param pvUser User argument pointing to the APIC instance.
1689 */
1690static DECLCALLBACK(void) apicR3TimerCallback(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1691{
1692 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1693 APICState *pApic = (APICState *)pvUser;
1694 Assert(pApic->pTimerR3 == pTimer);
1695 Assert(pApic->fTimerArmed);
1696 Assert(PDMCritSectIsOwner(pDev->pCritSectR3));
1697 Assert(TMTimerIsLockOwner(pTimer));
1698
1699 if (!(pApic->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
1700 LogFlow(("apic_timer: trigger irq\n"));
1701 apic_set_irq(pDev, pApic, pApic->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE, 0 /*uTagSrc*/);
1702
1703 if ( (pApic->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC)
1704 && pApic->initial_count > 0) {
1705 /* new interval. */
1706 pApic->next_time += (((uint64_t)pApic->initial_count + 1) << pApic->count_shift);
1707 TMTimerSet(pApic->CTX_SUFF(pTimer), pApic->next_time);
1708 pApic->fTimerArmed = true;
1709 apicDoFrequencyHinting(pApic);
1710 Log2(("apicR3TimerCallback: ic=%#x sh=%#x nxt=%#llx\n", pApic->initial_count, pApic->count_shift, pApic->next_time));
1711 } else {
1712 /* single shot or disabled. */
1713 pApic->fTimerArmed = false;
1714 pApic->uHintedCountShift = pApic->uHintedInitialCount = 0;
1715 }
1716 } else {
1717 /* masked, do not rearm. */
1718 pApic->fTimerArmed = false;
1719 pApic->uHintedCountShift = pApic->uHintedInitialCount = 0;
1720 }
1721}
1722
1723static void apic_save(SSMHANDLE* f, void *opaque)
1724{
1725 APICState *s = (APICState*)opaque;
1726 int i;
1727
1728 SSMR3PutU32(f, s->apicbase);
1729 SSMR3PutU32(f, s->id);
1730 SSMR3PutU32(f, s->phys_id);
1731 SSMR3PutU32(f, s->arb_id);
1732 SSMR3PutU32(f, s->tpr);
1733 SSMR3PutU32(f, s->spurious_vec);
1734 SSMR3PutU8(f, s->log_dest);
1735 SSMR3PutU8(f, s->dest_mode);
1736 for (i = 0; i < 8; i++) {
1737 SSMR3PutU32(f, s->isr.au32Bitmap[i]);
1738 SSMR3PutU32(f, s->tmr.au32Bitmap[i]);
1739 SSMR3PutU32(f, s->irr.au32Bitmap[i]);
1740 }
1741 for (i = 0; i < APIC_LVT_NB; i++) {
1742 SSMR3PutU32(f, s->lvt[i]);
1743 }
1744 SSMR3PutU32(f, s->esr);
1745 SSMR3PutU32(f, s->icr[0]);
1746 SSMR3PutU32(f, s->icr[1]);
1747 SSMR3PutU32(f, s->divide_conf);
1748 SSMR3PutU32(f, s->count_shift);
1749 SSMR3PutU32(f, s->initial_count);
1750 SSMR3PutU64(f, s->initial_count_load_time);
1751 SSMR3PutU64(f, s->next_time);
1752
1753 TMR3TimerSave(s->CTX_SUFF(pTimer), f);
1754}
1755
1756static int apic_load(SSMHANDLE *f, void *opaque, int version_id)
1757{
1758 APICState *s = (APICState*)opaque;
1759 int i;
1760
1761 /** @todo XXX: what if the base changes? (registered memory regions) */
1762 SSMR3GetU32(f, &s->apicbase);
1763
1764 switch (version_id)
1765 {
1766 case APIC_SAVED_STATE_VERSION_ANCIENT:
1767 {
1768 uint8_t val = 0;
1769 SSMR3GetU8(f, &val);
1770 s->id = val;
1771 /* UP only in old saved states */
1772 s->phys_id = 0;
1773 SSMR3GetU8(f, &val);
1774 s->arb_id = val;
1775 break;
1776 }
1777 case APIC_SAVED_STATE_VERSION:
1778 case APIC_SAVED_STATE_VERSION_VBOX_30:
1779 SSMR3GetU32(f, &s->id);
1780 SSMR3GetU32(f, &s->phys_id);
1781 SSMR3GetU32(f, &s->arb_id);
1782 break;
1783 default:
1784 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1785 }
1786 SSMR3GetU32(f, &s->tpr);
1787 SSMR3GetU32(f, &s->spurious_vec);
1788 SSMR3GetU8(f, &s->log_dest);
1789 SSMR3GetU8(f, &s->dest_mode);
1790 for (i = 0; i < 8; i++) {
1791 SSMR3GetU32(f, &s->isr.au32Bitmap[i]);
1792 SSMR3GetU32(f, &s->tmr.au32Bitmap[i]);
1793 SSMR3GetU32(f, &s->irr.au32Bitmap[i]);
1794 }
1795 for (i = 0; i < APIC_LVT_NB; i++) {
1796 SSMR3GetU32(f, &s->lvt[i]);
1797 }
1798 SSMR3GetU32(f, &s->esr);
1799 SSMR3GetU32(f, &s->icr[0]);
1800 SSMR3GetU32(f, &s->icr[1]);
1801 SSMR3GetU32(f, &s->divide_conf);
1802 SSMR3GetU32(f, (uint32_t *)&s->count_shift);
1803 SSMR3GetU32(f, (uint32_t *)&s->initial_count);
1804 SSMR3GetU64(f, (uint64_t *)&s->initial_count_load_time);
1805 SSMR3GetU64(f, (uint64_t *)&s->next_time);
1806
1807 int rc = TMR3TimerLoad(s->CTX_SUFF(pTimer), f);
1808 AssertRCReturn(rc, rc);
1809 s->uHintedCountShift = s->uHintedInitialCount = 0;
1810 s->fTimerArmed = TMTimerIsActive(s->CTX_SUFF(pTimer));
1811 if (s->fTimerArmed)
1812 apicDoFrequencyHinting(s);
1813
1814 return VINF_SUCCESS; /** @todo darn mess! */
1815}
1816
1817#endif /* IN_RING3 */
1818
1819/* LAPIC */
1820PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1821{
1822 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1823 APICState *s = getLapic(pDev);
1824
1825 Log(("CPU%d: apicMMIORead at %llx\n", s->phys_id, (uint64_t)GCPhysAddr));
1826
1827 /** @todo add LAPIC range validity checks (different LAPICs can
1828 * theoretically have different physical addresses, see #3092) */
1829
1830 STAM_COUNTER_INC(&CTXSUFF(pDev->StatMMIORead));
1831 switch (cb)
1832 {
1833 case 1:
1834 /** @todo this is not how recent APIC behave! We will fix
1835 * this via the IOM. */
1836 *(uint8_t *)pv = 0;
1837 break;
1838
1839 case 2:
1840 /** @todo this is not how recent APIC behave! */
1841 *(uint16_t *)pv = 0;
1842 break;
1843
1844 case 4:
1845 {
1846#if 0 /** @note experimental */
1847#ifndef IN_RING3
1848 uint32_t index = (GCPhysAddr >> 4) & 0xff;
1849
1850 if ( index == 0x08 /* TPR */
1851 && ++s->cTPRPatchAttempts < APIC_MAX_PATCH_ATTEMPTS)
1852 {
1853#ifdef IN_RC
1854 pDevIns->pDevHlpGC->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, &s->tpr);
1855#else
1856 RTGCPTR pDevInsGC = PDMINS2DATA_GCPTR(pDevIns);
1857 pDevIns->pHlpR0->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, pDevIns + RT_OFFSETOF(APICState, tpr));
1858#endif
1859 return VINF_PATM_HC_MMIO_PATCH_READ;
1860 }
1861#endif
1862#endif /* experimental */
1863
1864 /* It does its own locking. */
1865 uint64_t u64Value = 0;
1866 int rc = apicReadRegister(pDev, s, (GCPhysAddr >> 4) & 0xff, &u64Value,
1867 VINF_IOM_R3_MMIO_READ, false /*fMsr*/);
1868 *(uint32_t *)pv = (uint32_t)u64Value;
1869 return rc;
1870 }
1871
1872 default:
1873 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1874 return VERR_INTERNAL_ERROR;
1875 }
1876 return VINF_SUCCESS;
1877}
1878
1879PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
1880{
1881 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1882 APICState *s = getLapic(pDev);
1883
1884 Log(("CPU%d: apicMMIOWrite at %llx\n", s->phys_id, (uint64_t)GCPhysAddr));
1885
1886 /** @todo: add LAPIC range validity checks (multiple LAPICs can theoretically have
1887 different physical addresses, see #3092) */
1888
1889 STAM_COUNTER_INC(&CTXSUFF(pDev->StatMMIOWrite));
1890 switch (cb)
1891 {
1892 case 1:
1893 case 2:
1894 /* ignore */
1895 break;
1896
1897 case 4:
1898 /* It does its own locking. */
1899 return apicWriteRegister(pDev, s, (GCPhysAddr >> 4) & 0xff, *(uint32_t const *)pv,
1900 VINF_IOM_R3_MMIO_WRITE, false /*fMsr*/);
1901
1902 default:
1903 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1904 return VERR_INTERNAL_ERROR;
1905 }
1906 return VINF_SUCCESS;
1907}
1908
1909#ifdef IN_RING3
1910
1911/**
1912 * Wrapper around apicReadRegister.
1913 *
1914 * @returns 64-bit register value.
1915 * @param pDev The PDM device instance.
1916 * @param pApic The Local APIC in question.
1917 * @param iReg The APIC register index.
1918 */
1919static uint64_t apicR3InfoReadReg(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg)
1920{
1921 uint64_t u64Value;
1922 int rc = apicReadRegister(pDev, pApic, iReg, &u64Value, VINF_SUCCESS, true /*fMsr*/);
1923 AssertRCReturn(rc, UINT64_MAX);
1924 return u64Value;
1925}
1926
1927
1928/**
1929 * Print a 8-DWORD Local APIC bit map (256 bits).
1930 *
1931 * @param pDev The PDM device instance.
1932 * @param pApic The Local APIC in question.
1933 * @param pHlp The output helper.
1934 * @param iStartReg The register to start at.
1935 */
1936static void apicR3DumpVec(APICDeviceInfo *pDev, APICState *pApic, PCDBGFINFOHLP pHlp, uint32_t iStartReg)
1937{
1938 for (uint32_t i = 0; i < 8; i++)
1939 pHlp->pfnPrintf(pHlp, "%08x", apicR3InfoReadReg(pDev, pApic, iStartReg + i));
1940 pHlp->pfnPrintf(pHlp, "\n");
1941}
1942
1943/**
1944 * Print basic Local APIC state.
1945 *
1946 * @param pDev The PDM device instance.
1947 * @param pApic The Local APIC in question.
1948 * @param pHlp The output helper.
1949 */
1950static void apicR3InfoBasic(APICDeviceInfo *pDev, APICState *pApic, PCDBGFINFOHLP pHlp)
1951{
1952 uint64_t u64;
1953
1954 pHlp->pfnPrintf(pHlp, "Local APIC at %08llx:\n", pApic->apicbase);
1955 u64 = apicR3InfoReadReg(pDev, pApic, 0x2);
1956 pHlp->pfnPrintf(pHlp, " LAPIC ID : %08llx\n", u64);
1957 pHlp->pfnPrintf(pHlp, " APIC ID = %02llx\n", (u64 >> 24) & 0xff);
1958 u64 = apicR3InfoReadReg(pDev, pApic, 0x3);
1959 pHlp->pfnPrintf(pHlp, " APIC VER : %08llx\n", u64);
1960 pHlp->pfnPrintf(pHlp, " version = %02x\n", (int)RT_BYTE1(u64));
1961 pHlp->pfnPrintf(pHlp, " lvts = %d\n", (int)RT_BYTE3(u64) + 1);
1962 u64 = apicR3InfoReadReg(pDev, pApic, 0x8);
1963 pHlp->pfnPrintf(pHlp, " TPR : %08llx\n", u64);
1964 pHlp->pfnPrintf(pHlp, " task pri = %lld/%lld\n", (u64 >> 4) & 0xf, u64 & 0xf);
1965 u64 = apicR3InfoReadReg(pDev, pApic, 0xA);
1966 pHlp->pfnPrintf(pHlp, " PPR : %08llx\n", u64);
1967 pHlp->pfnPrintf(pHlp, " cpu pri = %lld/%lld\n", (u64 >> 4) & 0xf, u64 & 0xf);
1968 u64 = apicR3InfoReadReg(pDev, pApic, 0xD);
1969 pHlp->pfnPrintf(pHlp, " LDR : %08llx\n", u64);
1970 pHlp->pfnPrintf(pHlp, " log id = %02llx\n", (u64 >> 24) & 0xff);
1971 pHlp->pfnPrintf(pHlp, " DFR : %08llx\n", apicR3InfoReadReg(pDev, pApic, 0xE));
1972 u64 = apicR3InfoReadReg(pDev, pApic, 0xF);
1973 pHlp->pfnPrintf(pHlp, " SVR : %08llx\n", u64);
1974 pHlp->pfnPrintf(pHlp, " focus = %s\n", u64 & RT_BIT(9) ? "check off" : "check on");
1975 pHlp->pfnPrintf(pHlp, " lapic = %s\n", u64 & RT_BIT(8) ? "ENABLED" : "DISABLED");
1976 pHlp->pfnPrintf(pHlp, " vector = %02x\n", (unsigned)RT_BYTE1(u64));
1977 pHlp->pfnPrintf(pHlp, " ISR : ");
1978 apicR3DumpVec(pDev, pApic, pHlp, 0x10);
1979 int iMax = Apic256BitReg_FindLastSetBit(&pApic->isr, -1);
1980 pHlp->pfnPrintf(pHlp, " highest = %02x\n", iMax == -1 ? 0 : iMax);
1981 pHlp->pfnPrintf(pHlp, " IRR : ");
1982 apicR3DumpVec(pDev, pApic, pHlp, 0x20);
1983 iMax = Apic256BitReg_FindLastSetBit(&pApic->irr, -1);
1984 pHlp->pfnPrintf(pHlp, " highest = %02X\n", iMax == -1 ? 0 : iMax);
1985}
1986
1987
1988/**
1989 * Print the more interesting Local APIC LVT entries.
1990 *
1991 * @param pDev The PDM device instance.
1992 * @param pApic The Local APIC in question.
1993 * @param pHlp The output helper.
1994 */
1995static void apicR3InfoLVT(APICDeviceInfo *pDev, APICState *pApic, PCDBGFINFOHLP pHlp)
1996{
1997 static const char * const s_apszDeliveryModes[] =
1998 {
1999 "Fixed ", "Reserved", "SMI", "Reserved", "NMI", "INIT", "Reserved", "ExtINT"
2000 };
2001 uint64_t u64;
2002
2003 u64 = apicR3InfoReadReg(pDev, pApic, 0x32);
2004 pHlp->pfnPrintf(pHlp, " LVT Timer : %08llx\n", u64);
2005 pHlp->pfnPrintf(pHlp, " mode = %s\n", u64 & RT_BIT(17) ? "periodic" : "one-shot");
2006 pHlp->pfnPrintf(pHlp, " mask = %llu\n", (u64 >> 16) & 1);
2007 pHlp->pfnPrintf(pHlp, " status = %s\n", u64 & RT_BIT(12) ? "pending" : "idle");
2008 pHlp->pfnPrintf(pHlp, " vector = %02llx\n", u64 & 0xff);
2009 u64 = apicR3InfoReadReg(pDev, pApic, 0x35);
2010 pHlp->pfnPrintf(pHlp, " LVT LINT0 : %08llx\n", u64);
2011 pHlp->pfnPrintf(pHlp, " mask = %llu\n", (u64 >> 16) & 1);
2012 pHlp->pfnPrintf(pHlp, " trigger = %s\n", u64 & RT_BIT(15) ? "level" : "edge");
2013 pHlp->pfnPrintf(pHlp, " rem irr = %llu\n", (u64 >> 14) & 1);
2014 pHlp->pfnPrintf(pHlp, " polarty = %llu\n", (u64 >> 13) & 1);
2015 pHlp->pfnPrintf(pHlp, " status = %s\n", u64 & RT_BIT(12) ? "pending" : "idle");
2016 pHlp->pfnPrintf(pHlp, " delivry = %s\n", s_apszDeliveryModes[(u64 >> 8) & 7]);
2017 pHlp->pfnPrintf(pHlp, " vector = %02llx\n", u64 & 0xff);
2018 u64 = apicR3InfoReadReg(pDev, pApic, 0x36);
2019 pHlp->pfnPrintf(pHlp, " LVT LINT1 : %08llx\n", u64);
2020 pHlp->pfnPrintf(pHlp, " mask = %llu\n", (u64 >> 16) & 1);
2021 pHlp->pfnPrintf(pHlp, " trigger = %s\n", u64 & RT_BIT(15) ? "level" : "edge");
2022 pHlp->pfnPrintf(pHlp, " rem irr = %lld\n", (u64 >> 14) & 1);
2023 pHlp->pfnPrintf(pHlp, " polarty = %lld\n", (u64 >> 13) & 1);
2024 pHlp->pfnPrintf(pHlp, " status = %s\n", u64 & RT_BIT(12) ? "pending" : "idle");
2025 pHlp->pfnPrintf(pHlp, " delivry = %s\n", s_apszDeliveryModes[(u64 >> 8) & 7]);
2026 pHlp->pfnPrintf(pHlp, " vector = %02llx\n", u64 & 0xff);
2027}
2028
2029
2030/**
2031 * Print LAPIC timer state.
2032 *
2033 * @param pDev The PDM device instance.
2034 * @param pApic The Local APIC in question.
2035 * @param pHlp The output helper.
2036 */
2037static void apicR3InfoTimer(APICDeviceInfo *pDev, APICState *pApic, PCDBGFINFOHLP pHlp)
2038{
2039 pHlp->pfnPrintf(pHlp, "Local APIC timer:\n");
2040 pHlp->pfnPrintf(pHlp, " Initial count : %08llx\n", apicR3InfoReadReg(pDev, pApic, 0x38));
2041 pHlp->pfnPrintf(pHlp, " Current count : %08llx\n", apicR3InfoReadReg(pDev, pApic, 0x39));
2042 uint64_t u64 = apicR3InfoReadReg(pDev, pApic, 0x3e);
2043 pHlp->pfnPrintf(pHlp, " Divide config : %08llx\n", u64);
2044 unsigned uDivider = ((u64 >> 1) & 0x04) | (u64 & 0x03);
2045 pHlp->pfnPrintf(pHlp, " divider = %u\n", uDivider == 7 ? 1 : 2 << uDivider);
2046}
2047
2048
2049/**
2050 * @callback_method_impl{FNDBGFHANDLERDEV,
2051 * Dumps the Local APIC state according to given argument.}
2052 */
2053static DECLCALLBACK(void) apicR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
2054{
2055 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2056 APICState *pApic = getLapic(pDev);
2057
2058 if (pszArgs == NULL || !strcmp(pszArgs, "basic"))
2059 apicR3InfoBasic(pDev, pApic, pHlp);
2060 else if (!strcmp(pszArgs, "lvt"))
2061 apicR3InfoLVT(pDev, pApic, pHlp);
2062 else if (!strcmp(pszArgs, "timer"))
2063 apicR3InfoTimer(pDev, pApic, pHlp);
2064 else
2065 pHlp->pfnPrintf(pHlp, "Invalid argument. Recognized arguments are 'basic', 'lvt', 'timer'.\n");
2066}
2067
2068
2069/**
2070 * @copydoc FNSSMDEVLIVEEXEC
2071 */
2072static DECLCALLBACK(int) apicR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
2073{
2074 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2075
2076 SSMR3PutU32( pSSM, pDev->cCpus);
2077 SSMR3PutBool(pSSM, pDev->fIoApic);
2078 SSMR3PutU32( pSSM, pDev->enmVersion);
2079 AssertCompile(PDMAPICVERSION_APIC == 2);
2080
2081 return VINF_SSM_DONT_CALL_AGAIN;
2082}
2083
2084
2085/**
2086 * @copydoc FNSSMDEVSAVEEXEC
2087 */
2088static DECLCALLBACK(int) apicR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2089{
2090 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2091
2092 /* config */
2093 apicR3LiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
2094
2095 /* save all APICs data */ /** @todo: is it correct? */
2096 APIC_FOREACH_BEGIN(pDev);
2097 apic_save(pSSM, pCurApic);
2098 APIC_FOREACH_END();
2099
2100 return VINF_SUCCESS;
2101}
2102
2103/**
2104 * @copydoc FNSSMDEVLOADEXEC
2105 */
2106static DECLCALLBACK(int) apicR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2107{
2108 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2109
2110 if ( uVersion != APIC_SAVED_STATE_VERSION
2111 && uVersion != APIC_SAVED_STATE_VERSION_VBOX_30
2112 && uVersion != APIC_SAVED_STATE_VERSION_ANCIENT)
2113 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2114
2115 /* config */
2116 if (uVersion > APIC_SAVED_STATE_VERSION_VBOX_30)
2117 {
2118 uint32_t cCpus;
2119 int rc = SSMR3GetU32(pSSM, &cCpus); AssertRCReturn(rc, rc);
2120 if (cCpus != pDev->cCpus)
2121 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - cCpus: saved=%#x config=%#x"), cCpus, pDev->cCpus);
2122
2123 bool fIoApic;
2124 rc = SSMR3GetBool(pSSM, &fIoApic); AssertRCReturn(rc, rc);
2125 if (fIoApic != pDev->fIoApic)
2126 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fIoApic: saved=%RTbool config=%RTbool"), fIoApic, pDev->fIoApic);
2127
2128 uint32_t uApicVersion;
2129 rc = SSMR3GetU32(pSSM, &uApicVersion); AssertRCReturn(rc, rc);
2130 if (uApicVersion != (uint32_t)pDev->enmVersion)
2131 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - uApicVersion: saved=%#x config=%#x"), uApicVersion, pDev->enmVersion);
2132 }
2133
2134 if (uPass != SSM_PASS_FINAL)
2135 return VINF_SUCCESS;
2136
2137 /* load all APICs data */ /** @todo: is it correct? */
2138 APIC_LOCK(pDev, VERR_INTERNAL_ERROR_3);
2139
2140 int rc = VINF_SUCCESS;
2141 APIC_FOREACH_BEGIN(pDev);
2142 rc = apic_load(pSSM, pCurApic, uVersion);
2143 if (RT_FAILURE(rc))
2144 break;
2145 APIC_FOREACH_END();
2146
2147 APIC_UNLOCK(pDev);
2148 return rc;
2149}
2150
2151/**
2152 * @copydoc FNPDMDEVRESET
2153 */
2154static DECLCALLBACK(void) apicR3Reset(PPDMDEVINS pDevIns)
2155{
2156 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2157 TMTimerLock(pDev->paLapicsR3[0].pTimerR3, VERR_IGNORED);
2158 APIC_LOCK_VOID(pDev, VERR_IGNORED);
2159
2160 /* Reset all APICs. */
2161 for (VMCPUID i = 0; i < pDev->cCpus; i++)
2162 {
2163 APICState *pApic = &pDev->CTX_SUFF(paLapics)[i];
2164 TMTimerStop(pApic->CTX_SUFF(pTimer));
2165
2166 /* Clear LAPIC state as if an INIT IPI was sent. */
2167 apic_init_ipi(pDev, pApic);
2168
2169 /* The IDs are not touched by apic_init_ipi() and must be reset now. */
2170 pApic->arb_id = pApic->id = i;
2171 Assert(pApic->id == pApic->phys_id); /* The two should match again. */
2172
2173 /* Reset should re-enable the APIC, see comment in msi.h */
2174 pApic->apicbase = VBOX_MSI_ADDR_BASE | MSR_IA32_APICBASE_ENABLE;
2175 if (pApic->phys_id == 0)
2176 pApic->apicbase |= MSR_IA32_APICBASE_BSP;
2177
2178 /* Clear any pending APIC interrupt action flag. */
2179 cpuClearInterrupt(pDev, pApic);
2180 }
2181 /** @todo r=bird: Why is this done everytime, while the constructor first
2182 * checks the CPUID? Who is right? */
2183 pDev->pApicHlpR3->pfnChangeFeature(pDev->pDevInsR3, pDev->enmVersion);
2184
2185 APIC_UNLOCK(pDev);
2186 TMTimerUnlock(pDev->paLapicsR3[0].pTimerR3);
2187}
2188
2189
2190/**
2191 * @copydoc FNPDMDEVRELOCATE
2192 */
2193static DECLCALLBACK(void) apicR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2194{
2195 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2196 pDev->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2197 pDev->pApicHlpRC = pDev->pApicHlpR3->pfnGetRCHelpers(pDevIns);
2198 pDev->paLapicsRC = MMHyperR3ToRC(PDMDevHlpGetVM(pDevIns), pDev->paLapicsR3);
2199 pDev->pCritSectRC = pDev->pApicHlpR3->pfnGetRCCritSect(pDevIns);
2200 for (uint32_t i = 0; i < pDev->cCpus; i++)
2201 pDev->paLapicsR3[i].pTimerRC = TMTimerRCPtr(pDev->paLapicsR3[i].pTimerR3);
2202}
2203
2204
2205/**
2206 * Initializes the state of one local APIC.
2207 *
2208 * @param pApic The Local APIC state to init.
2209 * @param id The Local APIC ID.
2210 */
2211DECLINLINE(void) initApicData(APICState *pApic, uint8_t id)
2212{
2213 memset(pApic, 0, sizeof(*pApic));
2214
2215 /* See comment in msi.h for LAPIC base info. */
2216 pApic->apicbase = VBOX_MSI_ADDR_BASE | MSR_IA32_APICBASE_ENABLE;
2217 if (id == 0) /* Mark first CPU as BSP. */
2218 pApic->apicbase |= MSR_IA32_APICBASE_BSP;
2219
2220 for (int i = 0; i < APIC_LVT_NB; i++)
2221 pApic->lvt[i] = RT_BIT_32(16); /* mask LVT */
2222
2223 pApic->spurious_vec = 0xff;
2224 pApic->phys_id = id;
2225 pApic->id = id;
2226}
2227
2228
2229/**
2230 * @copydoc FNPDMDEVCONSTRUCT
2231 */
2232static DECLCALLBACK(int) apicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2233{
2234 APICDeviceInfo *pDev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2235 uint32_t i;
2236
2237 /*
2238 * Only single device instance.
2239 */
2240 Assert(iInstance == 0);
2241
2242 /*
2243 * Validate configuration.
2244 */
2245 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "IOAPIC|RZEnabled|NumCPUs", "");
2246
2247 bool fIoApic;
2248 int rc = CFGMR3QueryBoolDef(pCfg, "IOAPIC", &fIoApic, true);
2249 if (RT_FAILURE(rc))
2250 return PDMDEV_SET_ERROR(pDevIns, rc,
2251 N_("Configuration error: Failed to read \"IOAPIC\""));
2252
2253 bool fRZEnabled;
2254 rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &fRZEnabled, true);
2255 if (RT_FAILURE(rc))
2256 return PDMDEV_SET_ERROR(pDevIns, rc,
2257 N_("Configuration error: Failed to query boolean value \"RZEnabled\""));
2258
2259 uint32_t cCpus;
2260 rc = CFGMR3QueryU32Def(pCfg, "NumCPUs", &cCpus, 1);
2261 if (RT_FAILURE(rc))
2262 return PDMDEV_SET_ERROR(pDevIns, rc,
2263 N_("Configuration error: Failed to query integer value \"NumCPUs\""));
2264
2265 Log(("APIC: cCpus=%d fRZEnabled=%RTbool fIoApic=%RTbool\n", cCpus, fRZEnabled, fIoApic));
2266 if (cCpus > 255)
2267 return PDMDEV_SET_ERROR(pDevIns, rc,
2268 N_("Configuration error: Invalid value for \"NumCPUs\""));
2269
2270 /*
2271 * Init the data.
2272 */
2273 pDev->pDevInsR3 = pDevIns;
2274 pDev->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2275 pDev->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2276 pDev->cCpus = cCpus;
2277 pDev->fIoApic = fIoApic;
2278 /* Use PDMAPICVERSION_X2APIC to activate x2APIC mode */
2279 pDev->enmVersion = PDMAPICVERSION_APIC;
2280
2281 /* Disable locking in this device. */
2282 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2283 AssertRCReturn(rc, rc);
2284
2285 PVM pVM = PDMDevHlpGetVM(pDevIns);
2286
2287 /*
2288 * We are not freeing this memory, as it's automatically released when guest exits.
2289 */
2290 rc = MMHyperAlloc(pVM, cCpus * sizeof(APICState), 1, MM_TAG_PDM_DEVICE_USER, (void **)&pDev->paLapicsR3);
2291 if (RT_FAILURE(rc))
2292 return VERR_NO_MEMORY;
2293 pDev->paLapicsR0 = MMHyperR3ToR0(pVM, pDev->paLapicsR3);
2294 pDev->paLapicsRC = MMHyperR3ToRC(pVM, pDev->paLapicsR3);
2295
2296 for (i = 0; i < cCpus; i++)
2297 initApicData(&pDev->paLapicsR3[i], i);
2298
2299 /*
2300 * Register the APIC.
2301 */
2302 PDMAPICREG ApicReg;
2303 ApicReg.u32Version = PDM_APICREG_VERSION;
2304 ApicReg.pfnGetInterruptR3 = apicGetInterrupt;
2305 ApicReg.pfnHasPendingIrqR3 = apicHasPendingIrq;
2306 ApicReg.pfnSetBaseR3 = apicSetBase;
2307 ApicReg.pfnGetBaseR3 = apicGetBase;
2308 ApicReg.pfnSetTPRR3 = apicSetTPR;
2309 ApicReg.pfnGetTPRR3 = apicGetTPR;
2310 ApicReg.pfnWriteMSRR3 = apicWriteMSR;
2311 ApicReg.pfnReadMSRR3 = apicReadMSR;
2312 ApicReg.pfnBusDeliverR3 = apicBusDeliverCallback;
2313 ApicReg.pfnLocalInterruptR3 = apicLocalInterrupt;
2314 if (fRZEnabled)
2315 {
2316 ApicReg.pszGetInterruptRC = "apicGetInterrupt";
2317 ApicReg.pszHasPendingIrqRC = "apicHasPendingIrq";
2318 ApicReg.pszSetBaseRC = "apicSetBase";
2319 ApicReg.pszGetBaseRC = "apicGetBase";
2320 ApicReg.pszSetTPRRC = "apicSetTPR";
2321 ApicReg.pszGetTPRRC = "apicGetTPR";
2322 ApicReg.pszWriteMSRRC = "apicWriteMSR";
2323 ApicReg.pszReadMSRRC = "apicReadMSR";
2324 ApicReg.pszBusDeliverRC = "apicBusDeliverCallback";
2325 ApicReg.pszLocalInterruptRC = "apicLocalInterrupt";
2326
2327 ApicReg.pszGetInterruptR0 = "apicGetInterrupt";
2328 ApicReg.pszHasPendingIrqR0 = "apicHasPendingIrq";
2329 ApicReg.pszSetBaseR0 = "apicSetBase";
2330 ApicReg.pszGetBaseR0 = "apicGetBase";
2331 ApicReg.pszSetTPRR0 = "apicSetTPR";
2332 ApicReg.pszGetTPRR0 = "apicGetTPR";
2333 ApicReg.pszWriteMSRR0 = "apicWriteMSR";
2334 ApicReg.pszReadMSRR0 = "apicReadMSR";
2335 ApicReg.pszBusDeliverR0 = "apicBusDeliverCallback";
2336 ApicReg.pszLocalInterruptR0 = "apicLocalInterrupt";
2337 }
2338 else
2339 {
2340 ApicReg.pszGetInterruptRC = NULL;
2341 ApicReg.pszHasPendingIrqRC = NULL;
2342 ApicReg.pszSetBaseRC = NULL;
2343 ApicReg.pszGetBaseRC = NULL;
2344 ApicReg.pszSetTPRRC = NULL;
2345 ApicReg.pszGetTPRRC = NULL;
2346 ApicReg.pszWriteMSRRC = NULL;
2347 ApicReg.pszReadMSRRC = NULL;
2348 ApicReg.pszBusDeliverRC = NULL;
2349 ApicReg.pszLocalInterruptRC = NULL;
2350
2351 ApicReg.pszGetInterruptR0 = NULL;
2352 ApicReg.pszHasPendingIrqR0 = NULL;
2353 ApicReg.pszSetBaseR0 = NULL;
2354 ApicReg.pszGetBaseR0 = NULL;
2355 ApicReg.pszSetTPRR0 = NULL;
2356 ApicReg.pszGetTPRR0 = NULL;
2357 ApicReg.pszWriteMSRR0 = NULL;
2358 ApicReg.pszReadMSRR0 = NULL;
2359 ApicReg.pszBusDeliverR0 = NULL;
2360 ApicReg.pszLocalInterruptR0 = NULL;
2361 }
2362
2363 rc = PDMDevHlpAPICRegister(pDevIns, &ApicReg, &pDev->pApicHlpR3);
2364 AssertLogRelRCReturn(rc, rc);
2365 pDev->pCritSectR3 = pDev->pApicHlpR3->pfnGetR3CritSect(pDevIns);
2366
2367 /*
2368 * The the CPUID feature bit.
2369 */
2370 /** @todo r=bird: See remark in the apicR3Reset. */
2371 uint32_t u32Eax, u32Ebx, u32Ecx, u32Edx;
2372 PDMDevHlpGetCpuId(pDevIns, 0, &u32Eax, &u32Ebx, &u32Ecx, &u32Edx);
2373 if (u32Eax >= 1)
2374 {
2375 if ( fIoApic /* If IOAPIC is enabled, enable Local APIC in any case */
2376 || ( u32Ebx == X86_CPUID_VENDOR_INTEL_EBX
2377 && u32Ecx == X86_CPUID_VENDOR_INTEL_ECX
2378 && u32Edx == X86_CPUID_VENDOR_INTEL_EDX /* GenuineIntel */)
2379 || ( u32Ebx == X86_CPUID_VENDOR_AMD_EBX
2380 && u32Ecx == X86_CPUID_VENDOR_AMD_ECX
2381 && u32Edx == X86_CPUID_VENDOR_AMD_EDX /* AuthenticAMD */))
2382 {
2383 LogRel(("Activating Local APIC\n"));
2384 pDev->pApicHlpR3->pfnChangeFeature(pDevIns, pDev->enmVersion);
2385 }
2386 }
2387
2388 /*
2389 * Register the MMIO range.
2390 */
2391 /** @todo: shall reregister, if base changes. */
2392 uint32_t ApicBase = pDev->paLapicsR3[0].apicbase & ~0xfff;
2393 rc = PDMDevHlpMMIORegister(pDevIns, ApicBase, 0x1000, pDev,
2394 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
2395 apicMMIOWrite, apicMMIORead, "APIC Memory");
2396 if (RT_FAILURE(rc))
2397 return rc;
2398
2399 if (fRZEnabled)
2400 {
2401 pDev->pApicHlpRC = pDev->pApicHlpR3->pfnGetRCHelpers(pDevIns);
2402 pDev->pCritSectRC = pDev->pApicHlpR3->pfnGetRCCritSect(pDevIns);
2403 rc = PDMDevHlpMMIORegisterRC(pDevIns, ApicBase, 0x1000, NIL_RTRCPTR /*pvUser*/, "apicMMIOWrite", "apicMMIORead");
2404 if (RT_FAILURE(rc))
2405 return rc;
2406
2407 pDev->pApicHlpR0 = pDev->pApicHlpR3->pfnGetR0Helpers(pDevIns);
2408 pDev->pCritSectR0 = pDev->pApicHlpR3->pfnGetR0CritSect(pDevIns);
2409 rc = PDMDevHlpMMIORegisterR0(pDevIns, ApicBase, 0x1000, NIL_RTR0PTR /*pvUser*/, "apicMMIOWrite", "apicMMIORead");
2410 if (RT_FAILURE(rc))
2411 return rc;
2412 }
2413
2414 /*
2415 * Create the APIC timers.
2416 */
2417 for (i = 0; i < cCpus; i++)
2418 {
2419 APICState *pApic = &pDev->paLapicsR3[i];
2420 pApic->pszDesc = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DEVICE_USER, "APIC Timer #%u", i);
2421 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, apicR3TimerCallback, pApic,
2422 TMTIMER_FLAGS_NO_CRIT_SECT, pApic->pszDesc, &pApic->pTimerR3);
2423 if (RT_FAILURE(rc))
2424 return rc;
2425 pApic->pTimerR0 = TMTimerR0Ptr(pApic->pTimerR3);
2426 pApic->pTimerRC = TMTimerRCPtr(pApic->pTimerR3);
2427 TMR3TimerSetCritSect(pApic->pTimerR3, pDev->pCritSectR3);
2428 }
2429
2430 /*
2431 * Saved state.
2432 */
2433 rc = PDMDevHlpSSMRegister3(pDevIns, APIC_SAVED_STATE_VERSION, sizeof(*pDev),
2434 apicR3LiveExec, apicR3SaveExec, apicR3LoadExec);
2435 if (RT_FAILURE(rc))
2436 return rc;
2437
2438 /*
2439 * Register debugger info callback.
2440 */
2441 PDMDevHlpDBGFInfoRegister(pDevIns, "apic", "Display Local APIC state for current CPU. "
2442 "Recognizes 'basic', 'lvt', 'timer' as arguments, defaulting to 'basic'.", apicR3Info);
2443
2444#ifdef VBOX_WITH_STATISTICS
2445 /*
2446 * Statistics.
2447 */
2448 PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOReadGC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in GC.");
2449 PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOReadHC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in HC.");
2450 PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOWriteGC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in GC.");
2451 PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOWriteHC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in HC.");
2452 PDMDevHlpSTAMRegister(pDevIns, &pDev->StatClearedActiveIrq,STAMTYPE_COUNTER, "/Devices/APIC/MaskedActiveIRQ", STAMUNIT_OCCURENCES, "Number of cleared irqs.");
2453 for (i = 0; i < cCpus; i++)
2454 {
2455 APICState *pApic = &pDev->paLapicsR3[i];
2456 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCount, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Calls to apicTimerSetInitialCount.", "/Devices/APIC/%u/TimerSetInitialCount", i);
2457 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCountArm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSetRelative calls.", "/Devices/APIC/%u/TimerSetInitialCount/Arm", i);
2458 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCountDisarm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerStop calls.", "/Devices/APIC/%u/TimerSetInitialCount/Disasm", i);
2459 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Calls to apicTimerSetLvt.", "/Devices/APIC/%u/TimerSetLvt", i);
2460 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtClearPeriodic, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Clearing APIC_LVT_TIMER_PERIODIC.", "/Devices/APIC/%u/TimerSetLvt/ClearPeriodic", i);
2461 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtPostponed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerStop postponed.", "/Devices/APIC/%u/TimerSetLvt/Postponed", i);
2462 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArmed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet avoided.", "/Devices/APIC/%u/TimerSetLvt/Armed", i);
2463 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet necessary.", "/Devices/APIC/%u/TimerSetLvt/Arm", i);
2464 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArmRetries, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet retries.", "/Devices/APIC/%u/TimerSetLvt/ArmRetries", i);
2465 PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtNoRelevantChange,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "No relevant flags changed.", "/Devices/APIC/%u/TimerSetLvt/NoRelevantChange", i);
2466 }
2467#endif
2468
2469 return VINF_SUCCESS;
2470}
2471
2472
2473/**
2474 * APIC device registration structure.
2475 */
2476const PDMDEVREG g_DeviceAPIC =
2477{
2478 /* u32Version */
2479 PDM_DEVREG_VERSION,
2480 /* szName */
2481 "apic",
2482 /* szRCMod */
2483 "VBoxDD2GC.gc",
2484 /* szR0Mod */
2485 "VBoxDD2R0.r0",
2486 /* pszDescription */
2487 "Advanced Programmable Interrupt Controller (APIC) Device",
2488 /* fFlags */
2489 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,
2490 /* fClass */
2491 PDM_DEVREG_CLASS_PIC,
2492 /* cMaxInstances */
2493 1,
2494 /* cbInstance */
2495 sizeof(APICState),
2496 /* pfnConstruct */
2497 apicR3Construct,
2498 /* pfnDestruct */
2499 NULL,
2500 /* pfnRelocate */
2501 apicR3Relocate,
2502 /* pfnIOCtl */
2503 NULL,
2504 /* pfnPowerOn */
2505 NULL,
2506 /* pfnReset */
2507 apicR3Reset,
2508 /* pfnSuspend */
2509 NULL,
2510 /* pfnResume */
2511 NULL,
2512 /* pfnAttach */
2513 NULL,
2514 /* pfnDetach */
2515 NULL,
2516 /* pfnQueryInterface. */
2517 NULL,
2518 /* pfnInitComplete */
2519 NULL,
2520 /* pfnPowerOff */
2521 NULL,
2522 /* pfnSoftReset */
2523 NULL,
2524 /* u32VersionEnd */
2525 PDM_DEVREG_VERSION
2526};
2527
2528#endif /* IN_RING3 */
2529#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
2530
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use