VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/APICAll.cpp@ 78298

Last change on this file since 78298 was 78208, checked in by vboxsync, 6 years ago

PDM,APIC,PIC: Don't use AssertRelease on VMCPU_FF_INTERRUPT_PIC, VMCPU_FF_INTERRUPT_APIC and friends during restore. Instead added debug assertions in the relevant functions causing these to be set by misbehaving device state loaders. ticketref:18331 ticketref:18265

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 123.5 KB
Line 
1/* $Id: APICAll.cpp 78208 2019-04-18 15:54:40Z vboxsync $ */
2/** @file
3 * APIC - Advanced Programmable Interrupt Controller - All Contexts.
4 */
5
6/*
7 * Copyright (C) 2016-2019 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_APIC
23#include "APICInternal.h"
24#include <VBox/vmm/pdmdev.h>
25#include <VBox/vmm/pdmapi.h>
26#include <VBox/vmm/rem.h>
27#include <VBox/vmm/vm.h>
28#include <VBox/vmm/vmm.h>
29#include <VBox/vmm/vmcpuset.h>
30
31
32/*********************************************************************************************************************************
33* Internal Functions *
34*********************************************************************************************************************************/
35static void apicSetInterruptFF(PVMCPU pVCpu, PDMAPICIRQ enmType);
36static void apicStopTimer(PVMCPU pVCpu);
37
38
39/*********************************************************************************************************************************
40* Global Variables *
41*********************************************************************************************************************************/
42#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
43/** An ordered array of valid LVT masks. */
44static const uint32_t g_au32LvtValidMasks[] =
45{
46 XAPIC_LVT_TIMER_VALID,
47 XAPIC_LVT_THERMAL_VALID,
48 XAPIC_LVT_PERF_VALID,
49 XAPIC_LVT_LINT_VALID, /* LINT0 */
50 XAPIC_LVT_LINT_VALID, /* LINT1 */
51 XAPIC_LVT_ERROR_VALID
52};
53#endif
54
55#if 0
56/** @todo CMCI */
57static const uint32_t g_au32LvtExtValidMask[] =
58{
59 XAPIC_LVT_CMCI_VALID
60};
61#endif
62
63
64/**
65 * Checks if a vector is set in an APIC 256-bit sparse register.
66 *
67 * @returns true if the specified vector is set, false otherwise.
68 * @param pApicReg The APIC 256-bit spare register.
69 * @param uVector The vector to check if set.
70 */
71DECLINLINE(bool) apicTestVectorInReg(const volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
72{
73 const volatile uint8_t *pbBitmap = (const volatile uint8_t *)&pApicReg->u[0];
74 return ASMBitTest(pbBitmap + XAPIC_REG256_VECTOR_OFF(uVector), XAPIC_REG256_VECTOR_BIT(uVector));
75}
76
77
78/**
79 * Sets the vector in an APIC 256-bit sparse register.
80 *
81 * @param pApicReg The APIC 256-bit spare register.
82 * @param uVector The vector to set.
83 */
84DECLINLINE(void) apicSetVectorInReg(volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
85{
86 volatile uint8_t *pbBitmap = (volatile uint8_t *)&pApicReg->u[0];
87 ASMAtomicBitSet(pbBitmap + XAPIC_REG256_VECTOR_OFF(uVector), XAPIC_REG256_VECTOR_BIT(uVector));
88}
89
90
91/**
92 * Clears the vector in an APIC 256-bit sparse register.
93 *
94 * @param pApicReg The APIC 256-bit spare register.
95 * @param uVector The vector to clear.
96 */
97DECLINLINE(void) apicClearVectorInReg(volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
98{
99 volatile uint8_t *pbBitmap = (volatile uint8_t *)&pApicReg->u[0];
100 ASMAtomicBitClear(pbBitmap + XAPIC_REG256_VECTOR_OFF(uVector), XAPIC_REG256_VECTOR_BIT(uVector));
101}
102
103
104#if 0 /* unused */
105/**
106 * Checks if a vector is set in an APIC Pending-Interrupt Bitmap (PIB).
107 *
108 * @returns true if the specified vector is set, false otherwise.
109 * @param pvPib Opaque pointer to the PIB.
110 * @param uVector The vector to check if set.
111 */
112DECLINLINE(bool) apicTestVectorInPib(volatile void *pvPib, uint8_t uVector)
113{
114 return ASMBitTest(pvPib, uVector);
115}
116#endif /* unused */
117
118
119/**
120 * Atomically sets the PIB notification bit.
121 *
122 * @returns non-zero if the bit was already set, 0 otherwise.
123 * @param pApicPib Pointer to the PIB.
124 */
125DECLINLINE(uint32_t) apicSetNotificationBitInPib(PAPICPIB pApicPib)
126{
127 return ASMAtomicXchgU32(&pApicPib->fOutstandingNotification, RT_BIT_32(31));
128}
129
130
131/**
132 * Atomically tests and clears the PIB notification bit.
133 *
134 * @returns non-zero if the bit was already set, 0 otherwise.
135 * @param pApicPib Pointer to the PIB.
136 */
137DECLINLINE(uint32_t) apicClearNotificationBitInPib(PAPICPIB pApicPib)
138{
139 return ASMAtomicXchgU32(&pApicPib->fOutstandingNotification, UINT32_C(0));
140}
141
142
143/**
144 * Sets the vector in an APIC Pending-Interrupt Bitmap (PIB).
145 *
146 * @param pvPib Opaque pointer to the PIB.
147 * @param uVector The vector to set.
148 */
149DECLINLINE(void) apicSetVectorInPib(volatile void *pvPib, uint8_t uVector)
150{
151 ASMAtomicBitSet(pvPib, uVector);
152}
153
154#if 0 /* unused */
155/**
156 * Clears the vector in an APIC Pending-Interrupt Bitmap (PIB).
157 *
158 * @param pvPib Opaque pointer to the PIB.
159 * @param uVector The vector to clear.
160 */
161DECLINLINE(void) apicClearVectorInPib(volatile void *pvPib, uint8_t uVector)
162{
163 ASMAtomicBitClear(pvPib, uVector);
164}
165#endif /* unused */
166
167#if 0 /* unused */
168/**
169 * Atomically OR's a fragment (32 vectors) into an APIC 256-bit sparse
170 * register.
171 *
172 * @param pApicReg The APIC 256-bit spare register.
173 * @param idxFragment The index of the 32-bit fragment in @a
174 * pApicReg.
175 * @param u32Fragment The 32-bit vector fragment to OR.
176 */
177DECLINLINE(void) apicOrVectorsToReg(volatile XAPIC256BITREG *pApicReg, size_t idxFragment, uint32_t u32Fragment)
178{
179 Assert(idxFragment < RT_ELEMENTS(pApicReg->u));
180 ASMAtomicOrU32(&pApicReg->u[idxFragment].u32Reg, u32Fragment);
181}
182#endif /* unused */
183
184
185#if 0 /* unused */
186/**
187 * Atomically AND's a fragment (32 vectors) into an APIC
188 * 256-bit sparse register.
189 *
190 * @param pApicReg The APIC 256-bit spare register.
191 * @param idxFragment The index of the 32-bit fragment in @a
192 * pApicReg.
193 * @param u32Fragment The 32-bit vector fragment to AND.
194 */
195DECLINLINE(void) apicAndVectorsToReg(volatile XAPIC256BITREG *pApicReg, size_t idxFragment, uint32_t u32Fragment)
196{
197 Assert(idxFragment < RT_ELEMENTS(pApicReg->u));
198 ASMAtomicAndU32(&pApicReg->u[idxFragment].u32Reg, u32Fragment);
199}
200#endif /* unused */
201
202
203/**
204 * Reports and returns appropriate error code for invalid MSR accesses.
205 *
206 * @returns VERR_CPUM_RAISE_GP_0
207 *
208 * @param pVCpu The cross context virtual CPU structure.
209 * @param u32Reg The MSR being accessed.
210 * @param enmAccess The invalid-access type.
211 */
212static int apicMsrAccessError(PVMCPU pVCpu, uint32_t u32Reg, APICMSRACCESS enmAccess)
213{
214 static struct
215 {
216 const char *pszBefore; /* The error message before printing the MSR index */
217 const char *pszAfter; /* The error message after printing the MSR index */
218 } const s_aAccess[] =
219 {
220 /* enmAccess pszBefore pszAfter */
221 /* 0 */ { "read MSR", " while not in x2APIC mode" },
222 /* 1 */ { "write MSR", " while not in x2APIC mode" },
223 /* 2 */ { "read reserved/unknown MSR", "" },
224 /* 3 */ { "write reserved/unknown MSR", "" },
225 /* 4 */ { "read write-only MSR", "" },
226 /* 5 */ { "write read-only MSR", "" },
227 /* 6 */ { "read reserved bits of MSR", "" },
228 /* 7 */ { "write reserved bits of MSR", "" },
229 /* 8 */ { "write an invalid value to MSR", "" },
230 /* 9 */ { "write MSR", " disallowed by configuration" },
231 /* 10 */ { "read MSR", " disallowed by configuration" },
232 };
233 AssertCompile(RT_ELEMENTS(s_aAccess) == APICMSRACCESS_COUNT);
234
235 size_t const i = enmAccess;
236 Assert(i < RT_ELEMENTS(s_aAccess));
237 if (pVCpu->apic.s.cLogMaxAccessError++ < 5)
238 LogRel(("APIC%u: Attempt to %s (%#x)%s -> #GP(0)\n", pVCpu->idCpu, s_aAccess[i].pszBefore, u32Reg, s_aAccess[i].pszAfter));
239 return VERR_CPUM_RAISE_GP_0;
240}
241
242
243/**
244 * Gets the descriptive APIC mode.
245 *
246 * @returns The name.
247 * @param enmMode The xAPIC mode.
248 */
249const char *apicGetModeName(APICMODE enmMode)
250{
251 switch (enmMode)
252 {
253 case APICMODE_DISABLED: return "Disabled";
254 case APICMODE_XAPIC: return "xAPIC";
255 case APICMODE_X2APIC: return "x2APIC";
256 default: break;
257 }
258 return "Invalid";
259}
260
261
262/**
263 * Gets the descriptive destination format name.
264 *
265 * @returns The destination format name.
266 * @param enmDestFormat The destination format.
267 */
268const char *apicGetDestFormatName(XAPICDESTFORMAT enmDestFormat)
269{
270 switch (enmDestFormat)
271 {
272 case XAPICDESTFORMAT_FLAT: return "Flat";
273 case XAPICDESTFORMAT_CLUSTER: return "Cluster";
274 default: break;
275 }
276 return "Invalid";
277}
278
279
280/**
281 * Gets the descriptive delivery mode name.
282 *
283 * @returns The delivery mode name.
284 * @param enmDeliveryMode The delivery mode.
285 */
286const char *apicGetDeliveryModeName(XAPICDELIVERYMODE enmDeliveryMode)
287{
288 switch (enmDeliveryMode)
289 {
290 case XAPICDELIVERYMODE_FIXED: return "Fixed";
291 case XAPICDELIVERYMODE_LOWEST_PRIO: return "Lowest-priority";
292 case XAPICDELIVERYMODE_SMI: return "SMI";
293 case XAPICDELIVERYMODE_NMI: return "NMI";
294 case XAPICDELIVERYMODE_INIT: return "INIT";
295 case XAPICDELIVERYMODE_STARTUP: return "SIPI";
296 case XAPICDELIVERYMODE_EXTINT: return "ExtINT";
297 default: break;
298 }
299 return "Invalid";
300}
301
302
303/**
304 * Gets the descriptive destination mode name.
305 *
306 * @returns The destination mode name.
307 * @param enmDestMode The destination mode.
308 */
309const char *apicGetDestModeName(XAPICDESTMODE enmDestMode)
310{
311 switch (enmDestMode)
312 {
313 case XAPICDESTMODE_PHYSICAL: return "Physical";
314 case XAPICDESTMODE_LOGICAL: return "Logical";
315 default: break;
316 }
317 return "Invalid";
318}
319
320
321/**
322 * Gets the descriptive trigger mode name.
323 *
324 * @returns The trigger mode name.
325 * @param enmTriggerMode The trigger mode.
326 */
327const char *apicGetTriggerModeName(XAPICTRIGGERMODE enmTriggerMode)
328{
329 switch (enmTriggerMode)
330 {
331 case XAPICTRIGGERMODE_EDGE: return "Edge";
332 case XAPICTRIGGERMODE_LEVEL: return "Level";
333 default: break;
334 }
335 return "Invalid";
336}
337
338
339/**
340 * Gets the destination shorthand name.
341 *
342 * @returns The destination shorthand name.
343 * @param enmDestShorthand The destination shorthand.
344 */
345const char *apicGetDestShorthandName(XAPICDESTSHORTHAND enmDestShorthand)
346{
347 switch (enmDestShorthand)
348 {
349 case XAPICDESTSHORTHAND_NONE: return "None";
350 case XAPICDESTSHORTHAND_SELF: return "Self";
351 case XAPIDDESTSHORTHAND_ALL_INCL_SELF: return "All including self";
352 case XAPICDESTSHORTHAND_ALL_EXCL_SELF: return "All excluding self";
353 default: break;
354 }
355 return "Invalid";
356}
357
358
359/**
360 * Gets the timer mode name.
361 *
362 * @returns The timer mode name.
363 * @param enmTimerMode The timer mode.
364 */
365const char *apicGetTimerModeName(XAPICTIMERMODE enmTimerMode)
366{
367 switch (enmTimerMode)
368 {
369 case XAPICTIMERMODE_ONESHOT: return "One-shot";
370 case XAPICTIMERMODE_PERIODIC: return "Periodic";
371 case XAPICTIMERMODE_TSC_DEADLINE: return "TSC deadline";
372 default: break;
373 }
374 return "Invalid";
375}
376
377
378/**
379 * Gets the APIC mode given the base MSR value.
380 *
381 * @returns The APIC mode.
382 * @param uApicBaseMsr The APIC Base MSR value.
383 */
384APICMODE apicGetMode(uint64_t uApicBaseMsr)
385{
386 uint32_t const uMode = (uApicBaseMsr >> 10) & UINT64_C(3);
387 APICMODE const enmMode = (APICMODE)uMode;
388#ifdef VBOX_STRICT
389 /* Paranoia. */
390 switch (uMode)
391 {
392 case APICMODE_DISABLED:
393 case APICMODE_INVALID:
394 case APICMODE_XAPIC:
395 case APICMODE_X2APIC:
396 break;
397 default:
398 AssertMsgFailed(("Invalid mode"));
399 }
400#endif
401 return enmMode;
402}
403
404
405/**
406 * Returns whether the APIC is hardware enabled or not.
407 *
408 * @returns true if enabled, false otherwise.
409 * @param pVCpu The cross context virtual CPU structure.
410 */
411VMM_INT_DECL(bool) APICIsEnabled(PVMCPU pVCpu)
412{
413 PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
414 return RT_BOOL(pApicCpu->uApicBaseMsr & MSR_IA32_APICBASE_EN);
415}
416
417
418/**
419 * Finds the most significant set bit in an APIC 256-bit sparse register.
420 *
421 * @returns @a rcNotFound if no bit was set, 0-255 otherwise.
422 * @param pReg The APIC 256-bit sparse register.
423 * @param rcNotFound What to return when no bit is set.
424 */
425static int apicGetHighestSetBitInReg(volatile const XAPIC256BITREG *pReg, int rcNotFound)
426{
427 ssize_t const cFragments = RT_ELEMENTS(pReg->u);
428 unsigned const uFragmentShift = 5;
429 AssertCompile(1 << uFragmentShift == sizeof(pReg->u[0].u32Reg) * 8);
430 for (ssize_t i = cFragments - 1; i >= 0; i--)
431 {
432 uint32_t const uFragment = pReg->u[i].u32Reg;
433 if (uFragment)
434 {
435 unsigned idxSetBit = ASMBitLastSetU32(uFragment);
436 --idxSetBit;
437 idxSetBit |= i << uFragmentShift;
438 return idxSetBit;
439 }
440 }
441 return rcNotFound;
442}
443
444
445/**
446 * Reads a 32-bit register at a specified offset.
447 *
448 * @returns The value at the specified offset.
449 * @param pXApicPage The xAPIC page.
450 * @param offReg The offset of the register being read.
451 */
452DECLINLINE(uint32_t) apicReadRaw32(PCXAPICPAGE pXApicPage, uint16_t offReg)
453{
454 Assert(offReg < sizeof(*pXApicPage) - sizeof(uint32_t));
455 uint8_t const *pbXApic = (const uint8_t *)pXApicPage;
456 uint32_t const uValue = *(const uint32_t *)(pbXApic + offReg);
457 return uValue;
458}
459
460
461/**
462 * Writes a 32-bit register at a specified offset.
463 *
464 * @param pXApicPage The xAPIC page.
465 * @param offReg The offset of the register being written.
466 * @param uReg The value of the register.
467 */
468DECLINLINE(void) apicWriteRaw32(PXAPICPAGE pXApicPage, uint16_t offReg, uint32_t uReg)
469{
470 Assert(offReg < sizeof(*pXApicPage) - sizeof(uint32_t));
471 uint8_t *pbXApic = (uint8_t *)pXApicPage;
472 *(uint32_t *)(pbXApic + offReg) = uReg;
473}
474
475
476/**
477 * Sets an error in the internal ESR of the specified APIC.
478 *
479 * @param pVCpu The cross context virtual CPU structure.
480 * @param uError The error.
481 * @thread Any.
482 */
483DECLINLINE(void) apicSetError(PVMCPU pVCpu, uint32_t uError)
484{
485 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
486 ASMAtomicOrU32(&pApicCpu->uEsrInternal, uError);
487}
488
489
490/**
491 * Clears all errors in the internal ESR.
492 *
493 * @returns The value of the internal ESR before clearing.
494 * @param pVCpu The cross context virtual CPU structure.
495 */
496DECLINLINE(uint32_t) apicClearAllErrors(PVMCPU pVCpu)
497{
498 VMCPU_ASSERT_EMT(pVCpu);
499 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
500 return ASMAtomicXchgU32(&pApicCpu->uEsrInternal, 0);
501}
502
503
504/**
505 * Signals the guest if a pending interrupt is ready to be serviced.
506 *
507 * @param pVCpu The cross context virtual CPU structure.
508 */
509static void apicSignalNextPendingIntr(PVMCPU pVCpu)
510{
511 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
512
513 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
514 if (pXApicPage->svr.u.fApicSoftwareEnable)
515 {
516 int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1 /* rcNotFound */);
517 if (irrv >= 0)
518 {
519 Assert(irrv <= (int)UINT8_MAX);
520 uint8_t const uVector = irrv;
521 uint8_t const uPpr = pXApicPage->ppr.u8Ppr;
522 if ( !uPpr
523 || XAPIC_PPR_GET_PP(uVector) > XAPIC_PPR_GET_PP(uPpr))
524 {
525 Log2(("APIC%u: apicSignalNextPendingIntr: Signaling pending interrupt. uVector=%#x\n", pVCpu->idCpu, uVector));
526 apicSetInterruptFF(pVCpu, PDMAPICIRQ_HARDWARE);
527 }
528 else
529 {
530 Log2(("APIC%u: apicSignalNextPendingIntr: Nothing to signal. uVector=%#x uPpr=%#x uTpr=%#x\n", pVCpu->idCpu,
531 uVector, uPpr, pXApicPage->tpr.u8Tpr));
532 }
533 }
534 }
535 else
536 {
537 Log2(("APIC%u: apicSignalNextPendingIntr: APIC software-disabled, clearing pending interrupt\n", pVCpu->idCpu));
538 apicClearInterruptFF(pVCpu, PDMAPICIRQ_HARDWARE);
539 }
540}
541
542
543/**
544 * Sets the Spurious-Interrupt Vector Register (SVR).
545 *
546 * @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
547 * @param pVCpu The cross context virtual CPU structure.
548 * @param uSvr The SVR value.
549 */
550static int apicSetSvr(PVMCPU pVCpu, uint32_t uSvr)
551{
552 VMCPU_ASSERT_EMT(pVCpu);
553
554 uint32_t uValidMask = XAPIC_SVR_VALID;
555 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
556 if (pXApicPage->version.u.fEoiBroadcastSupression)
557 uValidMask |= XAPIC_SVR_SUPRESS_EOI_BROADCAST;
558
559 if ( XAPIC_IN_X2APIC_MODE(pVCpu)
560 && (uSvr & ~uValidMask))
561 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_SVR, APICMSRACCESS_WRITE_RSVD_BITS);
562
563 Log2(("APIC%u: apicSetSvr: uSvr=%#RX32\n", pVCpu->idCpu, uSvr));
564 apicWriteRaw32(pXApicPage, XAPIC_OFF_SVR, uSvr);
565 if (!pXApicPage->svr.u.fApicSoftwareEnable)
566 {
567 /** @todo CMCI. */
568 pXApicPage->lvt_timer.u.u1Mask = 1;
569#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
570 pXApicPage->lvt_thermal.u.u1Mask = 1;
571#endif
572 pXApicPage->lvt_perf.u.u1Mask = 1;
573 pXApicPage->lvt_lint0.u.u1Mask = 1;
574 pXApicPage->lvt_lint1.u.u1Mask = 1;
575 pXApicPage->lvt_error.u.u1Mask = 1;
576 }
577
578 apicSignalNextPendingIntr(pVCpu);
579 return VINF_SUCCESS;
580}
581
582
583/**
584 * Sends an interrupt to one or more APICs.
585 *
586 * @returns Strict VBox status code.
587 * @param pVM The cross context VM structure.
588 * @param pVCpu The cross context virtual CPU structure, can be
589 * NULL if the source of the interrupt is not an
590 * APIC (for e.g. a bus).
591 * @param uVector The interrupt vector.
592 * @param enmTriggerMode The trigger mode.
593 * @param enmDeliveryMode The delivery mode.
594 * @param pDestCpuSet The destination CPU set.
595 * @param pfIntrAccepted Where to store whether this interrupt was
596 * accepted by the target APIC(s) or not.
597 * Optional, can be NULL.
598 * @param uSrcTag The interrupt source tag (debugging).
599 * @param rcRZ The return code if the operation cannot be
600 * performed in the current context.
601 */
602static VBOXSTRICTRC apicSendIntr(PVM pVM, PVMCPU pVCpu, uint8_t uVector, XAPICTRIGGERMODE enmTriggerMode,
603 XAPICDELIVERYMODE enmDeliveryMode, PCVMCPUSET pDestCpuSet, bool *pfIntrAccepted,
604 uint32_t uSrcTag, int rcRZ)
605{
606 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
607 VMCPUID const cCpus = pVM->cCpus;
608 bool fAccepted = false;
609 switch (enmDeliveryMode)
610 {
611 case XAPICDELIVERYMODE_FIXED:
612 {
613 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
614 {
615 if ( VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu)
616 && APICIsEnabled(&pVM->aCpus[idCpu]))
617 fAccepted = apicPostInterrupt(&pVM->aCpus[idCpu], uVector, enmTriggerMode, uSrcTag);
618 }
619 break;
620 }
621
622 case XAPICDELIVERYMODE_LOWEST_PRIO:
623 {
624 VMCPUID const idCpu = VMCPUSET_FIND_FIRST_PRESENT(pDestCpuSet);
625 if ( idCpu < pVM->cCpus
626 && APICIsEnabled(&pVM->aCpus[idCpu]))
627 fAccepted = apicPostInterrupt(&pVM->aCpus[idCpu], uVector, enmTriggerMode, uSrcTag);
628 else
629 AssertMsgFailed(("APIC: apicSendIntr: No CPU found for lowest-priority delivery mode! idCpu=%u\n", idCpu));
630 break;
631 }
632
633 case XAPICDELIVERYMODE_SMI:
634 {
635 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
636 {
637 if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
638 {
639 Log2(("APIC: apicSendIntr: Raising SMI on VCPU%u\n", idCpu));
640 apicSetInterruptFF(&pVM->aCpus[idCpu], PDMAPICIRQ_SMI);
641 fAccepted = true;
642 }
643 }
644 break;
645 }
646
647 case XAPICDELIVERYMODE_NMI:
648 {
649 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
650 {
651 if ( VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu)
652 && APICIsEnabled(&pVM->aCpus[idCpu]))
653 {
654 Log2(("APIC: apicSendIntr: Raising NMI on VCPU%u\n", idCpu));
655 apicSetInterruptFF(&pVM->aCpus[idCpu], PDMAPICIRQ_NMI);
656 fAccepted = true;
657 }
658 }
659 break;
660 }
661
662 case XAPICDELIVERYMODE_INIT:
663 {
664#ifdef IN_RING3
665 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
666 if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
667 {
668 Log2(("APIC: apicSendIntr: Issuing INIT to VCPU%u\n", idCpu));
669 VMMR3SendInitIpi(pVM, idCpu);
670 fAccepted = true;
671 }
672#else
673 /* We need to return to ring-3 to deliver the INIT. */
674 rcStrict = rcRZ;
675 fAccepted = true;
676#endif
677 break;
678 }
679
680 case XAPICDELIVERYMODE_STARTUP:
681 {
682#ifdef IN_RING3
683 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
684 if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
685 {
686 Log2(("APIC: apicSendIntr: Issuing SIPI to VCPU%u\n", idCpu));
687 VMMR3SendStartupIpi(pVM, idCpu, uVector);
688 fAccepted = true;
689 }
690#else
691 /* We need to return to ring-3 to deliver the SIPI. */
692 rcStrict = rcRZ;
693 fAccepted = true;
694 Log2(("APIC: apicSendIntr: SIPI issued, returning to RZ. rc=%Rrc\n", rcRZ));
695#endif
696 break;
697 }
698
699 case XAPICDELIVERYMODE_EXTINT:
700 {
701 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
702 if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
703 {
704 Log2(("APIC: apicSendIntr: Raising EXTINT on VCPU%u\n", idCpu));
705 apicSetInterruptFF(&pVM->aCpus[idCpu], PDMAPICIRQ_EXTINT);
706 fAccepted = true;
707 }
708 break;
709 }
710
711 default:
712 {
713 AssertMsgFailed(("APIC: apicSendIntr: Unsupported delivery mode %#x (%s)\n", enmDeliveryMode,
714 apicGetDeliveryModeName(enmDeliveryMode)));
715 break;
716 }
717 }
718
719 /*
720 * If an illegal vector is programmed, set the 'send illegal vector' error here if the
721 * interrupt is being sent by an APIC.
722 *
723 * The 'receive illegal vector' will be set on the target APIC when the interrupt
724 * gets generated, see apicPostInterrupt().
725 *
726 * See Intel spec. 10.5.3 "Error Handling".
727 */
728 if ( rcStrict != rcRZ
729 && pVCpu)
730 {
731 /*
732 * Flag only errors when the delivery mode is fixed and not others.
733 *
734 * Ubuntu 10.04-3 amd64 live CD with 2 VCPUs gets upset as it sends an SIPI to the
735 * 2nd VCPU with vector 6 and checks the ESR for no errors, see @bugref{8245#c86}.
736 */
737 /** @todo The spec says this for LVT, but not explcitly for ICR-lo
738 * but it probably is true. */
739 if (enmDeliveryMode == XAPICDELIVERYMODE_FIXED)
740 {
741 if (RT_UNLIKELY(uVector <= XAPIC_ILLEGAL_VECTOR_END))
742 apicSetError(pVCpu, XAPIC_ESR_SEND_ILLEGAL_VECTOR);
743 }
744 }
745
746 if (pfIntrAccepted)
747 *pfIntrAccepted = fAccepted;
748
749 return rcStrict;
750}
751
752
753/**
754 * Checks if this APIC belongs to a logical destination.
755 *
756 * @returns true if the APIC belongs to the logical
757 * destination, false otherwise.
758 * @param pVCpu The cross context virtual CPU structure.
759 * @param fDest The destination mask.
760 *
761 * @thread Any.
762 */
763static bool apicIsLogicalDest(PVMCPU pVCpu, uint32_t fDest)
764{
765 if (XAPIC_IN_X2APIC_MODE(pVCpu))
766 {
767 /*
768 * Flat logical mode is not supported in x2APIC mode.
769 * In clustered logical mode, the 32-bit logical ID in the LDR is interpreted as follows:
770 * - High 16 bits is the cluster ID.
771 * - Low 16 bits: each bit represents a unique APIC within the cluster.
772 */
773 PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpu);
774 uint32_t const u32Ldr = pX2ApicPage->ldr.u32LogicalApicId;
775 if (X2APIC_LDR_GET_CLUSTER_ID(u32Ldr) == (fDest & X2APIC_LDR_CLUSTER_ID))
776 return RT_BOOL(u32Ldr & fDest & X2APIC_LDR_LOGICAL_ID);
777 return false;
778 }
779
780#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
781 /*
782 * In both flat and clustered logical mode, a destination mask of all set bits indicates a broadcast.
783 * See AMD spec. 16.6.1 "Receiving System and IPI Interrupts".
784 */
785 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
786 if ((fDest & XAPIC_LDR_FLAT_LOGICAL_ID) == XAPIC_LDR_FLAT_LOGICAL_ID)
787 return true;
788
789 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
790 XAPICDESTFORMAT enmDestFormat = (XAPICDESTFORMAT)pXApicPage->dfr.u.u4Model;
791 if (enmDestFormat == XAPICDESTFORMAT_FLAT)
792 {
793 /* The destination mask is interpreted as a bitmap of 8 unique logical APIC IDs. */
794 uint8_t const u8Ldr = pXApicPage->ldr.u.u8LogicalApicId;
795 return RT_BOOL(u8Ldr & fDest & XAPIC_LDR_FLAT_LOGICAL_ID);
796 }
797
798 /*
799 * In clustered logical mode, the 8-bit logical ID in the LDR is interpreted as follows:
800 * - High 4 bits is the cluster ID.
801 * - Low 4 bits: each bit represents a unique APIC within the cluster.
802 */
803 Assert(enmDestFormat == XAPICDESTFORMAT_CLUSTER);
804 uint8_t const u8Ldr = pXApicPage->ldr.u.u8LogicalApicId;
805 if (XAPIC_LDR_CLUSTERED_GET_CLUSTER_ID(u8Ldr) == (fDest & XAPIC_LDR_CLUSTERED_CLUSTER_ID))
806 return RT_BOOL(u8Ldr & fDest & XAPIC_LDR_CLUSTERED_LOGICAL_ID);
807 return false;
808#else
809# error "Implement Pentium and P6 family APIC architectures"
810#endif
811}
812
813
814/**
815 * Figures out the set of destination CPUs for a given destination mode, format
816 * and delivery mode setting.
817 *
818 * @param pVM The cross context VM structure.
819 * @param fDestMask The destination mask.
820 * @param fBroadcastMask The broadcast mask.
821 * @param enmDestMode The destination mode.
822 * @param enmDeliveryMode The delivery mode.
823 * @param pDestCpuSet The destination CPU set to update.
824 */
825static void apicGetDestCpuSet(PVM pVM, uint32_t fDestMask, uint32_t fBroadcastMask, XAPICDESTMODE enmDestMode,
826 XAPICDELIVERYMODE enmDeliveryMode, PVMCPUSET pDestCpuSet)
827{
828 VMCPUSET_EMPTY(pDestCpuSet);
829
830 /*
831 * Physical destination mode only supports either a broadcast or a single target.
832 * - Broadcast with lowest-priority delivery mode is not supported[1], we deliver it
833 * as a regular broadcast like in fixed delivery mode.
834 * - For a single target, lowest-priority delivery mode makes no sense. We deliver
835 * to the target like in fixed delivery mode.
836 *
837 * [1] See Intel spec. 10.6.2.1 "Physical Destination Mode".
838 */
839 if ( enmDestMode == XAPICDESTMODE_PHYSICAL
840 && enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO)
841 {
842 AssertMsgFailed(("APIC: Lowest-priority delivery using physical destination mode!"));
843 enmDeliveryMode = XAPICDELIVERYMODE_FIXED;
844 }
845
846 uint32_t const cCpus = pVM->cCpus;
847 if (enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO)
848 {
849 Assert(enmDestMode == XAPICDESTMODE_LOGICAL);
850#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
851 VMCPUID idCpuLowestTpr = NIL_VMCPUID;
852 uint8_t u8LowestTpr = UINT8_C(0xff);
853 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
854 {
855 PVMCPU pVCpuDest = &pVM->aCpus[idCpu];
856 if (apicIsLogicalDest(pVCpuDest, fDestMask))
857 {
858 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpuDest);
859 uint8_t const u8Tpr = pXApicPage->tpr.u8Tpr; /* PAV */
860
861 /*
862 * If there is a tie for lowest priority, the local APIC with the highest ID is chosen.
863 * Hence the use of "<=" in the check below.
864 * See AMD spec. 16.6.2 "Lowest Priority Messages and Arbitration".
865 */
866 if (u8Tpr <= u8LowestTpr)
867 {
868 u8LowestTpr = u8Tpr;
869 idCpuLowestTpr = idCpu;
870 }
871 }
872 }
873 if (idCpuLowestTpr != NIL_VMCPUID)
874 VMCPUSET_ADD(pDestCpuSet, idCpuLowestTpr);
875#else
876# error "Implement Pentium and P6 family APIC architectures"
877#endif
878 return;
879 }
880
881 /*
882 * x2APIC:
883 * - In both physical and logical destination mode, a destination mask of 0xffffffff implies a broadcast[1].
884 * xAPIC:
885 * - In physical destination mode, a destination mask of 0xff implies a broadcast[2].
886 * - In both flat and clustered logical mode, a destination mask of 0xff implies a broadcast[3].
887 *
888 * [1] See Intel spec. 10.12.9 "ICR Operation in x2APIC Mode".
889 * [2] See Intel spec. 10.6.2.1 "Physical Destination Mode".
890 * [2] See AMD spec. 16.6.1 "Receiving System and IPI Interrupts".
891 */
892 if ((fDestMask & fBroadcastMask) == fBroadcastMask)
893 {
894 VMCPUSET_FILL(pDestCpuSet);
895 return;
896 }
897
898 if (enmDestMode == XAPICDESTMODE_PHYSICAL)
899 {
900 /* The destination mask is interpreted as the physical APIC ID of a single target. */
901#if 1
902 /* Since our physical APIC ID is read-only to software, set the corresponding bit in the CPU set. */
903 if (RT_LIKELY(fDestMask < cCpus))
904 VMCPUSET_ADD(pDestCpuSet, fDestMask);
905#else
906 /* The physical APIC ID may not match our VCPU ID, search through the list of targets. */
907 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
908 {
909 PVMCPU pVCpuDest = &pVM->aCpus[idCpu];
910 if (XAPIC_IN_X2APIC_MODE(pVCpuDest))
911 {
912 PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpuDest);
913 if (pX2ApicPage->id.u32ApicId == fDestMask)
914 VMCPUSET_ADD(pDestCpuSet, pVCpuDest->idCpu);
915 }
916 else
917 {
918 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpuDest);
919 if (pXApicPage->id.u8ApicId == (uint8_t)fDestMask)
920 VMCPUSET_ADD(pDestCpuSet, pVCpuDest->idCpu);
921 }
922 }
923#endif
924 }
925 else
926 {
927 Assert(enmDestMode == XAPICDESTMODE_LOGICAL);
928
929 /* A destination mask of all 0's implies no target APICs (since it's interpreted as a bitmap or partial bitmap). */
930 if (RT_UNLIKELY(!fDestMask))
931 return;
932
933 /* The destination mask is interpreted as a bitmap of software-programmable logical APIC ID of the target APICs. */
934 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
935 {
936 PVMCPU pVCpuDest = &pVM->aCpus[idCpu];
937 if (apicIsLogicalDest(pVCpuDest, fDestMask))
938 VMCPUSET_ADD(pDestCpuSet, pVCpuDest->idCpu);
939 }
940 }
941}
942
943
944/**
945 * Sends an Interprocessor Interrupt (IPI) using values from the Interrupt
946 * Command Register (ICR).
947 *
948 * @returns VBox status code.
949 * @param pVCpu The cross context virtual CPU structure.
950 * @param rcRZ The return code if the operation cannot be
951 * performed in the current context.
952 */
953DECLINLINE(VBOXSTRICTRC) apicSendIpi(PVMCPU pVCpu, int rcRZ)
954{
955 VMCPU_ASSERT_EMT(pVCpu);
956
957 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
958 XAPICDELIVERYMODE const enmDeliveryMode = (XAPICDELIVERYMODE)pXApicPage->icr_lo.u.u3DeliveryMode;
959 XAPICDESTMODE const enmDestMode = (XAPICDESTMODE)pXApicPage->icr_lo.u.u1DestMode;
960 XAPICINITLEVEL const enmInitLevel = (XAPICINITLEVEL)pXApicPage->icr_lo.u.u1Level;
961 XAPICTRIGGERMODE const enmTriggerMode = (XAPICTRIGGERMODE)pXApicPage->icr_lo.u.u1TriggerMode;
962 XAPICDESTSHORTHAND const enmDestShorthand = (XAPICDESTSHORTHAND)pXApicPage->icr_lo.u.u2DestShorthand;
963 uint8_t const uVector = pXApicPage->icr_lo.u.u8Vector;
964
965 PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
966 uint32_t const fDest = XAPIC_IN_X2APIC_MODE(pVCpu) ? pX2ApicPage->icr_hi.u32IcrHi : pXApicPage->icr_hi.u.u8Dest;
967
968#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
969 /*
970 * INIT Level De-assert is not support on Pentium 4 and Xeon processors.
971 * Apparently, this also applies to NMI, SMI, lowest-priority and fixed delivery modes,
972 * see @bugref{8245#c116}.
973 *
974 * See AMD spec. 16.5 "Interprocessor Interrupts (IPI)" for a table of valid ICR combinations.
975 */
976 if ( enmTriggerMode == XAPICTRIGGERMODE_LEVEL
977 && enmInitLevel == XAPICINITLEVEL_DEASSERT
978 && ( enmDeliveryMode == XAPICDELIVERYMODE_FIXED
979 || enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO
980 || enmDeliveryMode == XAPICDELIVERYMODE_SMI
981 || enmDeliveryMode == XAPICDELIVERYMODE_NMI
982 || enmDeliveryMode == XAPICDELIVERYMODE_INIT))
983 {
984 Log2(("APIC%u: %s level de-assert unsupported, ignoring!\n", pVCpu->idCpu, apicGetDeliveryModeName(enmDeliveryMode)));
985 return VINF_SUCCESS;
986 }
987#else
988# error "Implement Pentium and P6 family APIC architectures"
989#endif
990
991 /*
992 * The destination and delivery modes are ignored/by-passed when a destination shorthand is specified.
993 * See Intel spec. 10.6.2.3 "Broadcast/Self Delivery Mode".
994 */
995 VMCPUSET DestCpuSet;
996 switch (enmDestShorthand)
997 {
998 case XAPICDESTSHORTHAND_NONE:
999 {
1000 PVM pVM = pVCpu->CTX_SUFF(pVM);
1001 uint32_t const fBroadcastMask = XAPIC_IN_X2APIC_MODE(pVCpu) ? X2APIC_ID_BROADCAST_MASK : XAPIC_ID_BROADCAST_MASK;
1002 apicGetDestCpuSet(pVM, fDest, fBroadcastMask, enmDestMode, enmDeliveryMode, &DestCpuSet);
1003 break;
1004 }
1005
1006 case XAPICDESTSHORTHAND_SELF:
1007 {
1008 VMCPUSET_EMPTY(&DestCpuSet);
1009 VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
1010 break;
1011 }
1012
1013 case XAPIDDESTSHORTHAND_ALL_INCL_SELF:
1014 {
1015 VMCPUSET_FILL(&DestCpuSet);
1016 break;
1017 }
1018
1019 case XAPICDESTSHORTHAND_ALL_EXCL_SELF:
1020 {
1021 VMCPUSET_FILL(&DestCpuSet);
1022 VMCPUSET_DEL(&DestCpuSet, pVCpu->idCpu);
1023 break;
1024 }
1025 }
1026
1027 return apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
1028 NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
1029}
1030
1031
1032/**
1033 * Sets the Interrupt Command Register (ICR) high dword.
1034 *
1035 * @returns Strict VBox status code.
1036 * @param pVCpu The cross context virtual CPU structure.
1037 * @param uIcrHi The ICR high dword.
1038 */
1039static VBOXSTRICTRC apicSetIcrHi(PVMCPU pVCpu, uint32_t uIcrHi)
1040{
1041 VMCPU_ASSERT_EMT(pVCpu);
1042 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1043
1044 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1045 pXApicPage->icr_hi.all.u32IcrHi = uIcrHi & XAPIC_ICR_HI_DEST;
1046 STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrHiWrite);
1047 Log2(("APIC%u: apicSetIcrHi: uIcrHi=%#RX32\n", pVCpu->idCpu, pXApicPage->icr_hi.all.u32IcrHi));
1048
1049 return VINF_SUCCESS;
1050}
1051
1052
1053/**
1054 * Sets the Interrupt Command Register (ICR) low dword.
1055 *
1056 * @returns Strict VBox status code.
1057 * @param pVCpu The cross context virtual CPU structure.
1058 * @param uIcrLo The ICR low dword.
1059 * @param rcRZ The return code if the operation cannot be performed
1060 * in the current context.
1061 * @param fUpdateStat Whether to update the ICR low write statistics
1062 * counter.
1063 */
1064static VBOXSTRICTRC apicSetIcrLo(PVMCPU pVCpu, uint32_t uIcrLo, int rcRZ, bool fUpdateStat)
1065{
1066 VMCPU_ASSERT_EMT(pVCpu);
1067
1068 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1069 pXApicPage->icr_lo.all.u32IcrLo = uIcrLo & XAPIC_ICR_LO_WR_VALID;
1070 Log2(("APIC%u: apicSetIcrLo: uIcrLo=%#RX32\n", pVCpu->idCpu, pXApicPage->icr_lo.all.u32IcrLo));
1071
1072 if (fUpdateStat)
1073 STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrLoWrite);
1074 RT_NOREF(fUpdateStat);
1075
1076 return apicSendIpi(pVCpu, rcRZ);
1077}
1078
1079
1080/**
1081 * Sets the Interrupt Command Register (ICR).
1082 *
1083 * @returns Strict VBox status code.
1084 * @param pVCpu The cross context virtual CPU structure.
1085 * @param u64Icr The ICR (High and Low combined).
1086 * @param rcRZ The return code if the operation cannot be performed
1087 * in the current context.
1088 *
1089 * @remarks This function is used by both x2APIC interface and the Hyper-V
1090 * interface, see APICHvSetIcr. The Hyper-V spec isn't clear what
1091 * happens when invalid bits are set. For the time being, it will
1092 * \#GP like a regular x2APIC access.
1093 */
1094static VBOXSTRICTRC apicSetIcr(PVMCPU pVCpu, uint64_t u64Icr, int rcRZ)
1095{
1096 VMCPU_ASSERT_EMT(pVCpu);
1097
1098 /* Validate. */
1099 uint32_t const uLo = RT_LO_U32(u64Icr);
1100 if (RT_LIKELY(!(uLo & ~XAPIC_ICR_LO_WR_VALID)))
1101 {
1102 /* Update high dword first, then update the low dword which sends the IPI. */
1103 PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
1104 pX2ApicPage->icr_hi.u32IcrHi = RT_HI_U32(u64Icr);
1105 STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrFullWrite);
1106 return apicSetIcrLo(pVCpu, uLo, rcRZ, false /* fUpdateStat */);
1107 }
1108 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_ICR, APICMSRACCESS_WRITE_RSVD_BITS);
1109}
1110
1111
1112/**
1113 * Sets the Error Status Register (ESR).
1114 *
1115 * @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
1116 * @param pVCpu The cross context virtual CPU structure.
1117 * @param uEsr The ESR value.
1118 */
1119static int apicSetEsr(PVMCPU pVCpu, uint32_t uEsr)
1120{
1121 VMCPU_ASSERT_EMT(pVCpu);
1122
1123 Log2(("APIC%u: apicSetEsr: uEsr=%#RX32\n", pVCpu->idCpu, uEsr));
1124
1125 if ( XAPIC_IN_X2APIC_MODE(pVCpu)
1126 && (uEsr & ~XAPIC_ESR_WO_VALID))
1127 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_ESR, APICMSRACCESS_WRITE_RSVD_BITS);
1128
1129 /*
1130 * Writes to the ESR causes the internal state to be updated in the register,
1131 * clearing the original state. See AMD spec. 16.4.6 "APIC Error Interrupts".
1132 */
1133 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1134 pXApicPage->esr.all.u32Errors = apicClearAllErrors(pVCpu);
1135 return VINF_SUCCESS;
1136}
1137
1138
1139/**
1140 * Updates the Processor Priority Register (PPR).
1141 *
1142 * @param pVCpu The cross context virtual CPU structure.
1143 */
1144static void apicUpdatePpr(PVMCPU pVCpu)
1145{
1146 VMCPU_ASSERT_EMT(pVCpu);
1147
1148 /* See Intel spec 10.8.3.1 "Task and Processor Priorities". */
1149 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1150 uint8_t const uIsrv = apicGetHighestSetBitInReg(&pXApicPage->isr, 0 /* rcNotFound */);
1151 uint8_t uPpr;
1152 if (XAPIC_TPR_GET_TP(pXApicPage->tpr.u8Tpr) >= XAPIC_PPR_GET_PP(uIsrv))
1153 uPpr = pXApicPage->tpr.u8Tpr;
1154 else
1155 uPpr = XAPIC_PPR_GET_PP(uIsrv);
1156 pXApicPage->ppr.u8Ppr = uPpr;
1157}
1158
1159
1160/**
1161 * Gets the Processor Priority Register (PPR).
1162 *
1163 * @returns The PPR value.
1164 * @param pVCpu The cross context virtual CPU structure.
1165 */
1166static uint8_t apicGetPpr(PVMCPU pVCpu)
1167{
1168 VMCPU_ASSERT_EMT(pVCpu);
1169 STAM_COUNTER_INC(&pVCpu->apic.s.StatTprRead);
1170
1171 /*
1172 * With virtualized APIC registers or with TPR virtualization, the hardware may
1173 * update ISR/TPR transparently. We thus re-calculate the PPR which may be out of sync.
1174 * See Intel spec. 29.2.2 "Virtual-Interrupt Delivery".
1175 *
1176 * In all other instances, whenever the TPR or ISR changes, we need to update the PPR
1177 * as well (e.g. like we do manually in apicR3InitIpi and by calling apicUpdatePpr).
1178 */
1179 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
1180 if (pApic->fVirtApicRegsEnabled) /** @todo re-think this */
1181 apicUpdatePpr(pVCpu);
1182 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
1183 return pXApicPage->ppr.u8Ppr;
1184}
1185
1186
1187/**
1188 * Sets the Task Priority Register (TPR).
1189 *
1190 * @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
1191 * @param pVCpu The cross context virtual CPU structure.
1192 * @param uTpr The TPR value.
1193 * @param fForceX2ApicBehaviour Pretend the APIC is in x2APIC mode during
1194 * this write.
1195 */
1196static int apicSetTprEx(PVMCPU pVCpu, uint32_t uTpr, bool fForceX2ApicBehaviour)
1197{
1198 VMCPU_ASSERT_EMT(pVCpu);
1199
1200 Log2(("APIC%u: apicSetTprEx: uTpr=%#RX32\n", pVCpu->idCpu, uTpr));
1201 STAM_COUNTER_INC(&pVCpu->apic.s.StatTprWrite);
1202
1203 bool const fX2ApicMode = XAPIC_IN_X2APIC_MODE(pVCpu) || fForceX2ApicBehaviour;
1204 if ( fX2ApicMode
1205 && (uTpr & ~XAPIC_TPR_VALID))
1206 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_TPR, APICMSRACCESS_WRITE_RSVD_BITS);
1207
1208 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1209 pXApicPage->tpr.u8Tpr = uTpr;
1210 apicUpdatePpr(pVCpu);
1211 apicSignalNextPendingIntr(pVCpu);
1212 return VINF_SUCCESS;
1213}
1214
1215
1216/**
1217 * Sets the End-Of-Interrupt (EOI) register.
1218 *
1219 * @returns Strict VBox status code.
1220 * @param pVCpu The cross context virtual CPU structure.
1221 * @param uEoi The EOI value.
1222 * @param rcBusy The busy return code when the write cannot
1223 * be completed successfully in this context.
1224 * @param fForceX2ApicBehaviour Pretend the APIC is in x2APIC mode during
1225 * this write.
1226 */
1227static VBOXSTRICTRC apicSetEoi(PVMCPU pVCpu, uint32_t uEoi, int rcBusy, bool fForceX2ApicBehaviour)
1228{
1229 VMCPU_ASSERT_EMT(pVCpu);
1230
1231 Log2(("APIC%u: apicSetEoi: uEoi=%#RX32\n", pVCpu->idCpu, uEoi));
1232 STAM_COUNTER_INC(&pVCpu->apic.s.StatEoiWrite);
1233
1234 bool const fX2ApicMode = XAPIC_IN_X2APIC_MODE(pVCpu) || fForceX2ApicBehaviour;
1235 if ( fX2ApicMode
1236 && (uEoi & ~XAPIC_EOI_WO_VALID))
1237 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_EOI, APICMSRACCESS_WRITE_RSVD_BITS);
1238
1239 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1240 int isrv = apicGetHighestSetBitInReg(&pXApicPage->isr, -1 /* rcNotFound */);
1241 if (isrv >= 0)
1242 {
1243 /*
1244 * Broadcast the EOI to the I/O APIC(s).
1245 *
1246 * We'll handle the EOI broadcast first as there is tiny chance we get rescheduled to
1247 * ring-3 due to contention on the I/O APIC lock. This way we don't mess with the rest
1248 * of the APIC state and simply restart the EOI write operation from ring-3.
1249 */
1250 Assert(isrv <= (int)UINT8_MAX);
1251 uint8_t const uVector = isrv;
1252 bool const fLevelTriggered = apicTestVectorInReg(&pXApicPage->tmr, uVector);
1253 if (fLevelTriggered)
1254 {
1255 int rc = PDMIoApicBroadcastEoi(pVCpu->CTX_SUFF(pVM), uVector);
1256 if (rc == VINF_SUCCESS)
1257 { /* likely */ }
1258 else
1259 return rcBusy;
1260
1261 /*
1262 * Clear the vector from the TMR.
1263 *
1264 * The broadcast to I/O APIC can re-trigger new interrupts to arrive via the bus. However,
1265 * APICUpdatePendingInterrupts() which updates TMR can only be done from EMT which we
1266 * currently are on, so no possibility of concurrent updates.
1267 */
1268 apicClearVectorInReg(&pXApicPage->tmr, uVector);
1269
1270 /*
1271 * Clear the remote IRR bit for level-triggered, fixed mode LINT0 interrupt.
1272 * The LINT1 pin does not support level-triggered interrupts.
1273 * See Intel spec. 10.5.1 "Local Vector Table".
1274 */
1275 uint32_t const uLvtLint0 = pXApicPage->lvt_lint0.all.u32LvtLint0;
1276 if ( XAPIC_LVT_GET_REMOTE_IRR(uLvtLint0)
1277 && XAPIC_LVT_GET_VECTOR(uLvtLint0) == uVector
1278 && XAPIC_LVT_GET_DELIVERY_MODE(uLvtLint0) == XAPICDELIVERYMODE_FIXED)
1279 {
1280 ASMAtomicAndU32((volatile uint32_t *)&pXApicPage->lvt_lint0.all.u32LvtLint0, ~XAPIC_LVT_REMOTE_IRR);
1281 Log2(("APIC%u: apicSetEoi: Cleared remote-IRR for LINT0. uVector=%#x\n", pVCpu->idCpu, uVector));
1282 }
1283
1284 Log2(("APIC%u: apicSetEoi: Cleared level triggered interrupt from TMR. uVector=%#x\n", pVCpu->idCpu, uVector));
1285 }
1286
1287 /*
1288 * Mark interrupt as serviced, update the PPR and signal pending interrupts.
1289 */
1290 Log2(("APIC%u: apicSetEoi: Clearing interrupt from ISR. uVector=%#x\n", pVCpu->idCpu, uVector));
1291 apicClearVectorInReg(&pXApicPage->isr, uVector);
1292 apicUpdatePpr(pVCpu);
1293 apicSignalNextPendingIntr(pVCpu);
1294 }
1295 else
1296 {
1297#ifdef DEBUG_ramshankar
1298 /** @todo Figure out if this is done intentionally by guests or is a bug
1299 * in our emulation. Happened with Win10 SMP VM during reboot after
1300 * installation of guest additions with 3D support. */
1301 AssertMsgFailed(("APIC%u: apicSetEoi: Failed to find any ISR bit\n", pVCpu->idCpu));
1302#endif
1303 }
1304
1305 return VINF_SUCCESS;
1306}
1307
1308
1309/**
1310 * Sets the Logical Destination Register (LDR).
1311 *
1312 * @returns Strict VBox status code.
1313 * @param pVCpu The cross context virtual CPU structure.
1314 * @param uLdr The LDR value.
1315 *
1316 * @remarks LDR is read-only in x2APIC mode.
1317 */
1318static VBOXSTRICTRC apicSetLdr(PVMCPU pVCpu, uint32_t uLdr)
1319{
1320 VMCPU_ASSERT_EMT(pVCpu);
1321 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
1322 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu) || pApic->fHyperVCompatMode); RT_NOREF_PV(pApic);
1323
1324 Log2(("APIC%u: apicSetLdr: uLdr=%#RX32\n", pVCpu->idCpu, uLdr));
1325
1326 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1327 apicWriteRaw32(pXApicPage, XAPIC_OFF_LDR, uLdr & XAPIC_LDR_VALID);
1328 return VINF_SUCCESS;
1329}
1330
1331
1332/**
1333 * Sets the Destination Format Register (DFR).
1334 *
1335 * @returns Strict VBox status code.
1336 * @param pVCpu The cross context virtual CPU structure.
1337 * @param uDfr The DFR value.
1338 *
1339 * @remarks DFR is not available in x2APIC mode.
1340 */
1341static VBOXSTRICTRC apicSetDfr(PVMCPU pVCpu, uint32_t uDfr)
1342{
1343 VMCPU_ASSERT_EMT(pVCpu);
1344 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1345
1346 uDfr &= XAPIC_DFR_VALID;
1347 uDfr |= XAPIC_DFR_RSVD_MB1;
1348
1349 Log2(("APIC%u: apicSetDfr: uDfr=%#RX32\n", pVCpu->idCpu, uDfr));
1350
1351 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1352 apicWriteRaw32(pXApicPage, XAPIC_OFF_DFR, uDfr);
1353 return VINF_SUCCESS;
1354}
1355
1356
1357/**
1358 * Sets the Timer Divide Configuration Register (DCR).
1359 *
1360 * @returns Strict VBox status code.
1361 * @param pVCpu The cross context virtual CPU structure.
1362 * @param uTimerDcr The timer DCR value.
1363 */
1364static VBOXSTRICTRC apicSetTimerDcr(PVMCPU pVCpu, uint32_t uTimerDcr)
1365{
1366 VMCPU_ASSERT_EMT(pVCpu);
1367 if ( XAPIC_IN_X2APIC_MODE(pVCpu)
1368 && (uTimerDcr & ~XAPIC_TIMER_DCR_VALID))
1369 return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_TIMER_DCR, APICMSRACCESS_WRITE_RSVD_BITS);
1370
1371 Log2(("APIC%u: apicSetTimerDcr: uTimerDcr=%#RX32\n", pVCpu->idCpu, uTimerDcr));
1372
1373 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1374 apicWriteRaw32(pXApicPage, XAPIC_OFF_TIMER_DCR, uTimerDcr);
1375 return VINF_SUCCESS;
1376}
1377
1378
1379/**
1380 * Gets the timer's Current Count Register (CCR).
1381 *
1382 * @returns VBox status code.
1383 * @param pVCpu The cross context virtual CPU structure.
1384 * @param rcBusy The busy return code for the timer critical section.
1385 * @param puValue Where to store the LVT timer CCR.
1386 */
1387static VBOXSTRICTRC apicGetTimerCcr(PVMCPU pVCpu, int rcBusy, uint32_t *puValue)
1388{
1389 VMCPU_ASSERT_EMT(pVCpu);
1390 Assert(puValue);
1391
1392 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
1393 *puValue = 0;
1394
1395 /* In TSC-deadline mode, CCR returns 0, see Intel spec. 10.5.4.1 "TSC-Deadline Mode". */
1396 if (pXApicPage->lvt_timer.u.u2TimerMode == XAPIC_TIMER_MODE_TSC_DEADLINE)
1397 return VINF_SUCCESS;
1398
1399 /* If the initial-count register is 0, CCR returns 0 as it cannot exceed the ICR. */
1400 uint32_t const uInitialCount = pXApicPage->timer_icr.u32InitialCount;
1401 if (!uInitialCount)
1402 return VINF_SUCCESS;
1403
1404 /*
1405 * Reading the virtual-sync clock requires locking its timer because it's not
1406 * a simple atomic operation, see tmVirtualSyncGetEx().
1407 *
1408 * We also need to lock before reading the timer CCR, see apicR3TimerCallback().
1409 */
1410 PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
1411 PTMTIMER pTimer = pApicCpu->CTX_SUFF(pTimer);
1412
1413 int rc = TMTimerLock(pTimer, rcBusy);
1414 if (rc == VINF_SUCCESS)
1415 {
1416 /* If the current-count register is 0, it implies the timer expired. */
1417 uint32_t const uCurrentCount = pXApicPage->timer_ccr.u32CurrentCount;
1418 if (uCurrentCount)
1419 {
1420 uint64_t const cTicksElapsed = TMTimerGet(pApicCpu->CTX_SUFF(pTimer)) - pApicCpu->u64TimerInitial;
1421 TMTimerUnlock(pTimer);
1422 uint8_t const uTimerShift = apicGetTimerShift(pXApicPage);
1423 uint64_t const uDelta = cTicksElapsed >> uTimerShift;
1424 if (uInitialCount > uDelta)
1425 *puValue = uInitialCount - uDelta;
1426 }
1427 else
1428 TMTimerUnlock(pTimer);
1429 }
1430 return rc;
1431}
1432
1433
1434/**
1435 * Sets the timer's Initial-Count Register (ICR).
1436 *
1437 * @returns Strict VBox status code.
1438 * @param pVCpu The cross context virtual CPU structure.
1439 * @param rcBusy The busy return code for the timer critical section.
1440 * @param uInitialCount The timer ICR.
1441 */
1442static VBOXSTRICTRC apicSetTimerIcr(PVMCPU pVCpu, int rcBusy, uint32_t uInitialCount)
1443{
1444 VMCPU_ASSERT_EMT(pVCpu);
1445
1446 PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
1447 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
1448 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1449 PTMTIMER pTimer = pApicCpu->CTX_SUFF(pTimer);
1450
1451 Log2(("APIC%u: apicSetTimerIcr: uInitialCount=%#RX32\n", pVCpu->idCpu, uInitialCount));
1452 STAM_COUNTER_INC(&pApicCpu->StatTimerIcrWrite);
1453
1454 /* In TSC-deadline mode, timer ICR writes are ignored, see Intel spec. 10.5.4.1 "TSC-Deadline Mode". */
1455 if ( pApic->fSupportsTscDeadline
1456 && pXApicPage->lvt_timer.u.u2TimerMode == XAPIC_TIMER_MODE_TSC_DEADLINE)
1457 return VINF_SUCCESS;
1458
1459 /*
1460 * The timer CCR may be modified by apicR3TimerCallback() in parallel,
1461 * so obtain the lock -before- updating it here to be consistent with the
1462 * timer ICR. We rely on CCR being consistent in apicGetTimerCcr().
1463 */
1464 int rc = TMTimerLock(pTimer, rcBusy);
1465 if (rc == VINF_SUCCESS)
1466 {
1467 pXApicPage->timer_icr.u32InitialCount = uInitialCount;
1468 pXApicPage->timer_ccr.u32CurrentCount = uInitialCount;
1469 if (uInitialCount)
1470 apicStartTimer(pVCpu, uInitialCount);
1471 else
1472 apicStopTimer(pVCpu);
1473 TMTimerUnlock(pTimer);
1474 }
1475 return rc;
1476}
1477
1478
1479/**
1480 * Sets an LVT entry.
1481 *
1482 * @returns Strict VBox status code.
1483 * @param pVCpu The cross context virtual CPU structure.
1484 * @param offLvt The LVT entry offset in the xAPIC page.
1485 * @param uLvt The LVT value to set.
1486 */
1487static VBOXSTRICTRC apicSetLvtEntry(PVMCPU pVCpu, uint16_t offLvt, uint32_t uLvt)
1488{
1489 VMCPU_ASSERT_EMT(pVCpu);
1490
1491#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
1492 AssertMsg( offLvt == XAPIC_OFF_LVT_TIMER
1493 || offLvt == XAPIC_OFF_LVT_THERMAL
1494 || offLvt == XAPIC_OFF_LVT_PERF
1495 || offLvt == XAPIC_OFF_LVT_LINT0
1496 || offLvt == XAPIC_OFF_LVT_LINT1
1497 || offLvt == XAPIC_OFF_LVT_ERROR,
1498 ("APIC%u: apicSetLvtEntry: invalid offset, offLvt=%#RX16, uLvt=%#RX32\n", pVCpu->idCpu, offLvt, uLvt));
1499
1500 /*
1501 * If TSC-deadline mode isn't support, ignore the bit in xAPIC mode
1502 * and raise #GP(0) in x2APIC mode.
1503 */
1504 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
1505 if (offLvt == XAPIC_OFF_LVT_TIMER)
1506 {
1507 if ( !pApic->fSupportsTscDeadline
1508 && (uLvt & XAPIC_LVT_TIMER_TSCDEADLINE))
1509 {
1510 if (XAPIC_IN_X2APIC_MODE(pVCpu))
1511 return apicMsrAccessError(pVCpu, XAPIC_GET_X2APIC_MSR(offLvt), APICMSRACCESS_WRITE_RSVD_BITS);
1512 uLvt &= ~XAPIC_LVT_TIMER_TSCDEADLINE;
1513 /** @todo TSC-deadline timer mode transition */
1514 }
1515 }
1516
1517 /*
1518 * Validate rest of the LVT bits.
1519 */
1520 uint16_t const idxLvt = (offLvt - XAPIC_OFF_LVT_START) >> 4;
1521 AssertReturn(idxLvt < RT_ELEMENTS(g_au32LvtValidMasks), VERR_OUT_OF_RANGE);
1522
1523 /*
1524 * For x2APIC, disallow setting of invalid/reserved bits.
1525 * For xAPIC, mask out invalid/reserved bits (i.e. ignore them).
1526 */
1527 if ( XAPIC_IN_X2APIC_MODE(pVCpu)
1528 && (uLvt & ~g_au32LvtValidMasks[idxLvt]))
1529 return apicMsrAccessError(pVCpu, XAPIC_GET_X2APIC_MSR(offLvt), APICMSRACCESS_WRITE_RSVD_BITS);
1530
1531 uLvt &= g_au32LvtValidMasks[idxLvt];
1532
1533 /*
1534 * In the software-disabled state, LVT mask-bit must remain set and attempts to clear the mask
1535 * bit must be ignored. See Intel spec. 10.4.7.2 "Local APIC State After It Has Been Software Disabled".
1536 */
1537 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1538 if (!pXApicPage->svr.u.fApicSoftwareEnable)
1539 uLvt |= XAPIC_LVT_MASK;
1540
1541 /*
1542 * It is unclear whether we should signal a 'send illegal vector' error here and ignore updating
1543 * the LVT entry when the delivery mode is 'fixed'[1] or update it in addition to signaling the
1544 * error or not signal the error at all. For now, we'll allow setting illegal vectors into the LVT
1545 * but set the 'send illegal vector' error here. The 'receive illegal vector' error will be set if
1546 * the interrupt for the vector happens to be generated, see apicPostInterrupt().
1547 *
1548 * [1] See Intel spec. 10.5.2 "Valid Interrupt Vectors".
1549 */
1550 if (RT_UNLIKELY( XAPIC_LVT_GET_VECTOR(uLvt) <= XAPIC_ILLEGAL_VECTOR_END
1551 && XAPIC_LVT_GET_DELIVERY_MODE(uLvt) == XAPICDELIVERYMODE_FIXED))
1552 apicSetError(pVCpu, XAPIC_ESR_SEND_ILLEGAL_VECTOR);
1553
1554 Log2(("APIC%u: apicSetLvtEntry: offLvt=%#RX16 uLvt=%#RX32\n", pVCpu->idCpu, offLvt, uLvt));
1555
1556 apicWriteRaw32(pXApicPage, offLvt, uLvt);
1557 return VINF_SUCCESS;
1558#else
1559# error "Implement Pentium and P6 family APIC architectures"
1560#endif /* XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4 */
1561}
1562
1563
1564#if 0
1565/**
1566 * Sets an LVT entry in the extended LVT range.
1567 *
1568 * @returns VBox status code.
1569 * @param pVCpu The cross context virtual CPU structure.
1570 * @param offLvt The LVT entry offset in the xAPIC page.
1571 * @param uValue The LVT value to set.
1572 */
1573static int apicSetLvtExtEntry(PVMCPU pVCpu, uint16_t offLvt, uint32_t uLvt)
1574{
1575 VMCPU_ASSERT_EMT(pVCpu);
1576 AssertMsg(offLvt == XAPIC_OFF_CMCI, ("APIC%u: apicSetLvt1Entry: invalid offset %#RX16\n", pVCpu->idCpu, offLvt));
1577
1578 /** @todo support CMCI. */
1579 return VERR_NOT_IMPLEMENTED;
1580}
1581#endif
1582
1583
1584/**
1585 * Hints TM about the APIC timer frequency.
1586 *
1587 * @param pApicCpu The APIC CPU state.
1588 * @param uInitialCount The new initial count.
1589 * @param uTimerShift The new timer shift.
1590 * @thread Any.
1591 */
1592void apicHintTimerFreq(PAPICCPU pApicCpu, uint32_t uInitialCount, uint8_t uTimerShift)
1593{
1594 Assert(pApicCpu);
1595
1596 if ( pApicCpu->uHintedTimerInitialCount != uInitialCount
1597 || pApicCpu->uHintedTimerShift != uTimerShift)
1598 {
1599 uint32_t uHz;
1600 if (uInitialCount)
1601 {
1602 uint64_t cTicksPerPeriod = (uint64_t)uInitialCount << uTimerShift;
1603 uHz = TMTimerGetFreq(pApicCpu->CTX_SUFF(pTimer)) / cTicksPerPeriod;
1604 }
1605 else
1606 uHz = 0;
1607
1608 TMTimerSetFrequencyHint(pApicCpu->CTX_SUFF(pTimer), uHz);
1609 pApicCpu->uHintedTimerInitialCount = uInitialCount;
1610 pApicCpu->uHintedTimerShift = uTimerShift;
1611 }
1612}
1613
1614
1615/**
1616 * Gets the Interrupt Command Register (ICR), without performing any interface
1617 * checks.
1618 *
1619 * @returns The ICR value.
1620 * @param pVCpu The cross context virtual CPU structure.
1621 */
1622DECLINLINE(uint64_t) apicGetIcrNoCheck(PVMCPU pVCpu)
1623{
1624 PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpu);
1625 uint64_t const uHi = pX2ApicPage->icr_hi.u32IcrHi;
1626 uint64_t const uLo = pX2ApicPage->icr_lo.all.u32IcrLo;
1627 uint64_t const uIcr = RT_MAKE_U64(uLo, uHi);
1628 return uIcr;
1629}
1630
1631
1632/**
1633 * Reads an APIC register.
1634 *
1635 * @returns VBox status code.
1636 * @param pApicDev The APIC device instance.
1637 * @param pVCpu The cross context virtual CPU structure.
1638 * @param offReg The offset of the register being read.
1639 * @param puValue Where to store the register value.
1640 */
1641DECLINLINE(VBOXSTRICTRC) apicReadRegister(PAPICDEV pApicDev, PVMCPU pVCpu, uint16_t offReg, uint32_t *puValue)
1642{
1643 VMCPU_ASSERT_EMT(pVCpu);
1644 Assert(offReg <= XAPIC_OFF_MAX_VALID);
1645
1646 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1647 uint32_t uValue = 0;
1648 VBOXSTRICTRC rc = VINF_SUCCESS;
1649 switch (offReg)
1650 {
1651 case XAPIC_OFF_ID:
1652 case XAPIC_OFF_VERSION:
1653 case XAPIC_OFF_TPR:
1654 case XAPIC_OFF_EOI:
1655 case XAPIC_OFF_RRD:
1656 case XAPIC_OFF_LDR:
1657 case XAPIC_OFF_DFR:
1658 case XAPIC_OFF_SVR:
1659 case XAPIC_OFF_ISR0: case XAPIC_OFF_ISR1: case XAPIC_OFF_ISR2: case XAPIC_OFF_ISR3:
1660 case XAPIC_OFF_ISR4: case XAPIC_OFF_ISR5: case XAPIC_OFF_ISR6: case XAPIC_OFF_ISR7:
1661 case XAPIC_OFF_TMR0: case XAPIC_OFF_TMR1: case XAPIC_OFF_TMR2: case XAPIC_OFF_TMR3:
1662 case XAPIC_OFF_TMR4: case XAPIC_OFF_TMR5: case XAPIC_OFF_TMR6: case XAPIC_OFF_TMR7:
1663 case XAPIC_OFF_IRR0: case XAPIC_OFF_IRR1: case XAPIC_OFF_IRR2: case XAPIC_OFF_IRR3:
1664 case XAPIC_OFF_IRR4: case XAPIC_OFF_IRR5: case XAPIC_OFF_IRR6: case XAPIC_OFF_IRR7:
1665 case XAPIC_OFF_ESR:
1666 case XAPIC_OFF_ICR_LO:
1667 case XAPIC_OFF_ICR_HI:
1668 case XAPIC_OFF_LVT_TIMER:
1669#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
1670 case XAPIC_OFF_LVT_THERMAL:
1671#endif
1672 case XAPIC_OFF_LVT_PERF:
1673 case XAPIC_OFF_LVT_LINT0:
1674 case XAPIC_OFF_LVT_LINT1:
1675 case XAPIC_OFF_LVT_ERROR:
1676 case XAPIC_OFF_TIMER_ICR:
1677 case XAPIC_OFF_TIMER_DCR:
1678 {
1679 Assert( !XAPIC_IN_X2APIC_MODE(pVCpu)
1680 || ( offReg != XAPIC_OFF_DFR
1681 && offReg != XAPIC_OFF_ICR_HI
1682 && offReg != XAPIC_OFF_EOI));
1683 uValue = apicReadRaw32(pXApicPage, offReg);
1684 Log2(("APIC%u: apicReadRegister: offReg=%#x uValue=%#x\n", pVCpu->idCpu, offReg, uValue));
1685 break;
1686 }
1687
1688 case XAPIC_OFF_PPR:
1689 {
1690 uValue = apicGetPpr(pVCpu);
1691 break;
1692 }
1693
1694 case XAPIC_OFF_TIMER_CCR:
1695 {
1696 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1697 rc = apicGetTimerCcr(pVCpu, VINF_IOM_R3_MMIO_READ, &uValue);
1698 break;
1699 }
1700
1701 case XAPIC_OFF_APR:
1702 {
1703#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
1704 /* Unsupported on Pentium 4 and Xeon CPUs, invalid in x2APIC mode. */
1705 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1706#else
1707# error "Implement Pentium and P6 family APIC architectures"
1708#endif
1709 break;
1710 }
1711
1712 default:
1713 {
1714 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1715 rc = PDMDevHlpDBGFStop(pApicDev->CTX_SUFF(pDevIns), RT_SRC_POS, "VCPU[%u]: offReg=%#RX16\n", pVCpu->idCpu,
1716 offReg);
1717 apicSetError(pVCpu, XAPIC_ESR_ILLEGAL_REG_ADDRESS);
1718 break;
1719 }
1720 }
1721
1722 *puValue = uValue;
1723 return rc;
1724}
1725
1726
1727/**
1728 * Writes an APIC register.
1729 *
1730 * @returns Strict VBox status code.
1731 * @param pApicDev The APIC device instance.
1732 * @param pVCpu The cross context virtual CPU structure.
1733 * @param offReg The offset of the register being written.
1734 * @param uValue The register value.
1735 */
1736DECLINLINE(VBOXSTRICTRC) apicWriteRegister(PAPICDEV pApicDev, PVMCPU pVCpu, uint16_t offReg, uint32_t uValue)
1737{
1738 VMCPU_ASSERT_EMT(pVCpu);
1739 Assert(offReg <= XAPIC_OFF_MAX_VALID);
1740 Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
1741
1742 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1743 switch (offReg)
1744 {
1745 case XAPIC_OFF_TPR:
1746 {
1747 rcStrict = apicSetTprEx(pVCpu, uValue, false /* fForceX2ApicBehaviour */);
1748 break;
1749 }
1750
1751 case XAPIC_OFF_LVT_TIMER:
1752#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
1753 case XAPIC_OFF_LVT_THERMAL:
1754#endif
1755 case XAPIC_OFF_LVT_PERF:
1756 case XAPIC_OFF_LVT_LINT0:
1757 case XAPIC_OFF_LVT_LINT1:
1758 case XAPIC_OFF_LVT_ERROR:
1759 {
1760 rcStrict = apicSetLvtEntry(pVCpu, offReg, uValue);
1761 break;
1762 }
1763
1764 case XAPIC_OFF_TIMER_ICR:
1765 {
1766 rcStrict = apicSetTimerIcr(pVCpu, VINF_IOM_R3_MMIO_WRITE, uValue);
1767 break;
1768 }
1769
1770 case XAPIC_OFF_EOI:
1771 {
1772 rcStrict = apicSetEoi(pVCpu, uValue, VINF_IOM_R3_MMIO_WRITE, false /* fForceX2ApicBehaviour */);
1773 break;
1774 }
1775
1776 case XAPIC_OFF_LDR:
1777 {
1778 rcStrict = apicSetLdr(pVCpu, uValue);
1779 break;
1780 }
1781
1782 case XAPIC_OFF_DFR:
1783 {
1784 rcStrict = apicSetDfr(pVCpu, uValue);
1785 break;
1786 }
1787
1788 case XAPIC_OFF_SVR:
1789 {
1790 rcStrict = apicSetSvr(pVCpu, uValue);
1791 break;
1792 }
1793
1794 case XAPIC_OFF_ICR_LO:
1795 {
1796 rcStrict = apicSetIcrLo(pVCpu, uValue, VINF_IOM_R3_MMIO_WRITE, true /* fUpdateStat */);
1797 break;
1798 }
1799
1800 case XAPIC_OFF_ICR_HI:
1801 {
1802 rcStrict = apicSetIcrHi(pVCpu, uValue);
1803 break;
1804 }
1805
1806 case XAPIC_OFF_TIMER_DCR:
1807 {
1808 rcStrict = apicSetTimerDcr(pVCpu, uValue);
1809 break;
1810 }
1811
1812 case XAPIC_OFF_ESR:
1813 {
1814 rcStrict = apicSetEsr(pVCpu, uValue);
1815 break;
1816 }
1817
1818 case XAPIC_OFF_APR:
1819 case XAPIC_OFF_RRD:
1820 {
1821#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
1822 /* Unsupported on Pentium 4 and Xeon CPUs but writes do -not- set an illegal register access error. */
1823#else
1824# error "Implement Pentium and P6 family APIC architectures"
1825#endif
1826 break;
1827 }
1828
1829 /* Read-only, write ignored: */
1830 case XAPIC_OFF_VERSION:
1831 case XAPIC_OFF_ID:
1832 break;
1833
1834 /* Unavailable/reserved in xAPIC mode: */
1835 case X2APIC_OFF_SELF_IPI:
1836 /* Read-only registers: */
1837 case XAPIC_OFF_PPR:
1838 case XAPIC_OFF_ISR0: case XAPIC_OFF_ISR1: case XAPIC_OFF_ISR2: case XAPIC_OFF_ISR3:
1839 case XAPIC_OFF_ISR4: case XAPIC_OFF_ISR5: case XAPIC_OFF_ISR6: case XAPIC_OFF_ISR7:
1840 case XAPIC_OFF_TMR0: case XAPIC_OFF_TMR1: case XAPIC_OFF_TMR2: case XAPIC_OFF_TMR3:
1841 case XAPIC_OFF_TMR4: case XAPIC_OFF_TMR5: case XAPIC_OFF_TMR6: case XAPIC_OFF_TMR7:
1842 case XAPIC_OFF_IRR0: case XAPIC_OFF_IRR1: case XAPIC_OFF_IRR2: case XAPIC_OFF_IRR3:
1843 case XAPIC_OFF_IRR4: case XAPIC_OFF_IRR5: case XAPIC_OFF_IRR6: case XAPIC_OFF_IRR7:
1844 case XAPIC_OFF_TIMER_CCR:
1845 default:
1846 {
1847 rcStrict = PDMDevHlpDBGFStop(pApicDev->CTX_SUFF(pDevIns), RT_SRC_POS, "APIC%u: offReg=%#RX16\n", pVCpu->idCpu,
1848 offReg);
1849 apicSetError(pVCpu, XAPIC_ESR_ILLEGAL_REG_ADDRESS);
1850 break;
1851 }
1852 }
1853
1854 return rcStrict;
1855}
1856
1857
1858/**
1859 * Reads an APIC MSR.
1860 *
1861 * @returns Strict VBox status code.
1862 * @param pVCpu The cross context virtual CPU structure.
1863 * @param u32Reg The MSR being read.
1864 * @param pu64Value Where to store the read value.
1865 */
1866VMM_INT_DECL(VBOXSTRICTRC) APICReadMsr(PVMCPU pVCpu, uint32_t u32Reg, uint64_t *pu64Value)
1867{
1868 /*
1869 * Validate.
1870 */
1871 VMCPU_ASSERT_EMT(pVCpu);
1872 Assert(u32Reg >= MSR_IA32_X2APIC_ID && u32Reg <= MSR_IA32_X2APIC_SELF_IPI);
1873 Assert(pu64Value);
1874
1875 /*
1876 * Is the APIC enabled?
1877 */
1878 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
1879 if (APICIsEnabled(pVCpu))
1880 { /* likely */ }
1881 else
1882 {
1883 return apicMsrAccessError(pVCpu, u32Reg, pApic->enmMaxMode == PDMAPICMODE_NONE ?
1884 APICMSRACCESS_READ_DISALLOWED_CONFIG : APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
1885 }
1886
1887#ifndef IN_RING3
1888 if (pApic->fRZEnabled)
1889 { /* likely */}
1890 else
1891 return VINF_CPUM_R3_MSR_READ;
1892#endif
1893
1894 STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMsrRead));
1895
1896 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1897 if (RT_LIKELY( XAPIC_IN_X2APIC_MODE(pVCpu)
1898 || pApic->fHyperVCompatMode))
1899 {
1900 switch (u32Reg)
1901 {
1902 /* Special handling for x2APIC: */
1903 case MSR_IA32_X2APIC_ICR:
1904 {
1905 *pu64Value = apicGetIcrNoCheck(pVCpu);
1906 break;
1907 }
1908
1909 /* Special handling, compatible with xAPIC: */
1910 case MSR_IA32_X2APIC_TIMER_CCR:
1911 {
1912 uint32_t uValue;
1913 rcStrict = apicGetTimerCcr(pVCpu, VINF_CPUM_R3_MSR_READ, &uValue);
1914 *pu64Value = uValue;
1915 break;
1916 }
1917
1918 /* Special handling, compatible with xAPIC: */
1919 case MSR_IA32_X2APIC_PPR:
1920 {
1921 *pu64Value = apicGetPpr(pVCpu);
1922 break;
1923 }
1924
1925 /* Raw read, compatible with xAPIC: */
1926 case MSR_IA32_X2APIC_ID:
1927 case MSR_IA32_X2APIC_VERSION:
1928 case MSR_IA32_X2APIC_TPR:
1929 case MSR_IA32_X2APIC_LDR:
1930 case MSR_IA32_X2APIC_SVR:
1931 case MSR_IA32_X2APIC_ISR0: case MSR_IA32_X2APIC_ISR1: case MSR_IA32_X2APIC_ISR2: case MSR_IA32_X2APIC_ISR3:
1932 case MSR_IA32_X2APIC_ISR4: case MSR_IA32_X2APIC_ISR5: case MSR_IA32_X2APIC_ISR6: case MSR_IA32_X2APIC_ISR7:
1933 case MSR_IA32_X2APIC_TMR0: case MSR_IA32_X2APIC_TMR1: case MSR_IA32_X2APIC_TMR2: case MSR_IA32_X2APIC_TMR3:
1934 case MSR_IA32_X2APIC_TMR4: case MSR_IA32_X2APIC_TMR5: case MSR_IA32_X2APIC_TMR6: case MSR_IA32_X2APIC_TMR7:
1935 case MSR_IA32_X2APIC_IRR0: case MSR_IA32_X2APIC_IRR1: case MSR_IA32_X2APIC_IRR2: case MSR_IA32_X2APIC_IRR3:
1936 case MSR_IA32_X2APIC_IRR4: case MSR_IA32_X2APIC_IRR5: case MSR_IA32_X2APIC_IRR6: case MSR_IA32_X2APIC_IRR7:
1937 case MSR_IA32_X2APIC_ESR:
1938 case MSR_IA32_X2APIC_LVT_TIMER:
1939 case MSR_IA32_X2APIC_LVT_THERMAL:
1940 case MSR_IA32_X2APIC_LVT_PERF:
1941 case MSR_IA32_X2APIC_LVT_LINT0:
1942 case MSR_IA32_X2APIC_LVT_LINT1:
1943 case MSR_IA32_X2APIC_LVT_ERROR:
1944 case MSR_IA32_X2APIC_TIMER_ICR:
1945 case MSR_IA32_X2APIC_TIMER_DCR:
1946 {
1947 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
1948 uint16_t const offReg = X2APIC_GET_XAPIC_OFF(u32Reg);
1949 *pu64Value = apicReadRaw32(pXApicPage, offReg);
1950 break;
1951 }
1952
1953 /* Write-only MSRs: */
1954 case MSR_IA32_X2APIC_SELF_IPI:
1955 case MSR_IA32_X2APIC_EOI:
1956 {
1957 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_WRITE_ONLY);
1958 break;
1959 }
1960
1961 /*
1962 * Windows guest using Hyper-V x2APIC MSR compatibility mode tries to read the "high"
1963 * LDR bits, which is quite absurd (as it's a 32-bit register) using this invalid MSR
1964 * index (0x80E), see @bugref{8382#c175}.
1965 */
1966 case MSR_IA32_X2APIC_LDR + 1:
1967 {
1968 if (pApic->fHyperVCompatMode)
1969 *pu64Value = 0;
1970 else
1971 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
1972 break;
1973 }
1974
1975 /* Reserved MSRs: */
1976 case MSR_IA32_X2APIC_LVT_CMCI:
1977 default:
1978 {
1979 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
1980 break;
1981 }
1982 }
1983 }
1984 else
1985 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_INVALID_READ_MODE);
1986
1987 return rcStrict;
1988}
1989
1990
1991/**
1992 * Writes an APIC MSR.
1993 *
1994 * @returns Strict VBox status code.
1995 * @param pVCpu The cross context virtual CPU structure.
1996 * @param u32Reg The MSR being written.
1997 * @param u64Value The value to write.
1998 */
1999VMM_INT_DECL(VBOXSTRICTRC) APICWriteMsr(PVMCPU pVCpu, uint32_t u32Reg, uint64_t u64Value)
2000{
2001 /*
2002 * Validate.
2003 */
2004 VMCPU_ASSERT_EMT(pVCpu);
2005 Assert(u32Reg >= MSR_IA32_X2APIC_ID && u32Reg <= MSR_IA32_X2APIC_SELF_IPI);
2006
2007 /*
2008 * Is the APIC enabled?
2009 */
2010 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
2011 if (APICIsEnabled(pVCpu))
2012 { /* likely */ }
2013 else
2014 {
2015 return apicMsrAccessError(pVCpu, u32Reg, pApic->enmMaxMode == PDMAPICMODE_NONE ?
2016 APICMSRACCESS_WRITE_DISALLOWED_CONFIG : APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
2017 }
2018
2019#ifndef IN_RING3
2020 if (pApic->fRZEnabled)
2021 { /* likely */ }
2022 else
2023 return VINF_CPUM_R3_MSR_WRITE;
2024#endif
2025
2026 STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMsrWrite));
2027
2028 /*
2029 * In x2APIC mode, we need to raise #GP(0) for writes to reserved bits, unlike MMIO
2030 * accesses where they are ignored. Hence, we need to validate each register before
2031 * invoking the generic/xAPIC write functions.
2032 *
2033 * Bits 63:32 of all registers except the ICR are reserved, we'll handle this common
2034 * case first and handle validating the remaining bits on a per-register basis.
2035 * See Intel spec. 10.12.1.2 "x2APIC Register Address Space".
2036 */
2037 if ( u32Reg != MSR_IA32_X2APIC_ICR
2038 && RT_HI_U32(u64Value))
2039 return apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_BITS);
2040
2041 uint32_t u32Value = RT_LO_U32(u64Value);
2042 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
2043 if (RT_LIKELY( XAPIC_IN_X2APIC_MODE(pVCpu)
2044 || pApic->fHyperVCompatMode))
2045 {
2046 switch (u32Reg)
2047 {
2048 case MSR_IA32_X2APIC_TPR:
2049 {
2050 rcStrict = apicSetTprEx(pVCpu, u32Value, false /* fForceX2ApicBehaviour */);
2051 break;
2052 }
2053
2054 case MSR_IA32_X2APIC_ICR:
2055 {
2056 rcStrict = apicSetIcr(pVCpu, u64Value, VINF_CPUM_R3_MSR_WRITE);
2057 break;
2058 }
2059
2060 case MSR_IA32_X2APIC_SVR:
2061 {
2062 rcStrict = apicSetSvr(pVCpu, u32Value);
2063 break;
2064 }
2065
2066 case MSR_IA32_X2APIC_ESR:
2067 {
2068 rcStrict = apicSetEsr(pVCpu, u32Value);
2069 break;
2070 }
2071
2072 case MSR_IA32_X2APIC_TIMER_DCR:
2073 {
2074 rcStrict = apicSetTimerDcr(pVCpu, u32Value);
2075 break;
2076 }
2077
2078 case MSR_IA32_X2APIC_LVT_TIMER:
2079 case MSR_IA32_X2APIC_LVT_THERMAL:
2080 case MSR_IA32_X2APIC_LVT_PERF:
2081 case MSR_IA32_X2APIC_LVT_LINT0:
2082 case MSR_IA32_X2APIC_LVT_LINT1:
2083 case MSR_IA32_X2APIC_LVT_ERROR:
2084 {
2085 rcStrict = apicSetLvtEntry(pVCpu, X2APIC_GET_XAPIC_OFF(u32Reg), u32Value);
2086 break;
2087 }
2088
2089 case MSR_IA32_X2APIC_TIMER_ICR:
2090 {
2091 rcStrict = apicSetTimerIcr(pVCpu, VINF_CPUM_R3_MSR_WRITE, u32Value);
2092 break;
2093 }
2094
2095 /* Write-only MSRs: */
2096 case MSR_IA32_X2APIC_SELF_IPI:
2097 {
2098 uint8_t const uVector = XAPIC_SELF_IPI_GET_VECTOR(u32Value);
2099 apicPostInterrupt(pVCpu, uVector, XAPICTRIGGERMODE_EDGE, 0 /* uSrcTag */);
2100 rcStrict = VINF_SUCCESS;
2101 break;
2102 }
2103
2104 case MSR_IA32_X2APIC_EOI:
2105 {
2106 rcStrict = apicSetEoi(pVCpu, u32Value, VINF_CPUM_R3_MSR_WRITE, false /* fForceX2ApicBehaviour */);
2107 break;
2108 }
2109
2110 /*
2111 * Windows guest using Hyper-V x2APIC MSR compatibility mode tries to write the "high"
2112 * LDR bits, which is quite absurd (as it's a 32-bit register) using this invalid MSR
2113 * index (0x80E). The write value was 0xffffffff on a Windows 8.1 64-bit guest. We can
2114 * safely ignore this nonsense, See @bugref{8382#c7}.
2115 */
2116 case MSR_IA32_X2APIC_LDR + 1:
2117 {
2118 if (pApic->fHyperVCompatMode)
2119 rcStrict = VINF_SUCCESS;
2120 else
2121 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
2122 break;
2123 }
2124
2125 /* Special-treament (read-only normally, but not with Hyper-V) */
2126 case MSR_IA32_X2APIC_LDR:
2127 {
2128 if (pApic->fHyperVCompatMode)
2129 {
2130 rcStrict = apicSetLdr(pVCpu, u32Value);
2131 break;
2132 }
2133 }
2134 RT_FALL_THRU();
2135 /* Read-only MSRs: */
2136 case MSR_IA32_X2APIC_ID:
2137 case MSR_IA32_X2APIC_VERSION:
2138 case MSR_IA32_X2APIC_PPR:
2139 case MSR_IA32_X2APIC_ISR0: case MSR_IA32_X2APIC_ISR1: case MSR_IA32_X2APIC_ISR2: case MSR_IA32_X2APIC_ISR3:
2140 case MSR_IA32_X2APIC_ISR4: case MSR_IA32_X2APIC_ISR5: case MSR_IA32_X2APIC_ISR6: case MSR_IA32_X2APIC_ISR7:
2141 case MSR_IA32_X2APIC_TMR0: case MSR_IA32_X2APIC_TMR1: case MSR_IA32_X2APIC_TMR2: case MSR_IA32_X2APIC_TMR3:
2142 case MSR_IA32_X2APIC_TMR4: case MSR_IA32_X2APIC_TMR5: case MSR_IA32_X2APIC_TMR6: case MSR_IA32_X2APIC_TMR7:
2143 case MSR_IA32_X2APIC_IRR0: case MSR_IA32_X2APIC_IRR1: case MSR_IA32_X2APIC_IRR2: case MSR_IA32_X2APIC_IRR3:
2144 case MSR_IA32_X2APIC_IRR4: case MSR_IA32_X2APIC_IRR5: case MSR_IA32_X2APIC_IRR6: case MSR_IA32_X2APIC_IRR7:
2145 case MSR_IA32_X2APIC_TIMER_CCR:
2146 {
2147 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_READ_ONLY);
2148 break;
2149 }
2150
2151 /* Reserved MSRs: */
2152 case MSR_IA32_X2APIC_LVT_CMCI:
2153 default:
2154 {
2155 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
2156 break;
2157 }
2158 }
2159 }
2160 else
2161 rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_INVALID_WRITE_MODE);
2162
2163 return rcStrict;
2164}
2165
2166
2167/**
2168 * Resets the APIC base MSR.
2169 *
2170 * @param pVCpu The cross context virtual CPU structure.
2171 */
2172static void apicResetBaseMsr(PVMCPU pVCpu)
2173{
2174 /*
2175 * Initialize the APIC base MSR. The APIC enable-bit is set upon power-up or reset[1].
2176 *
2177 * A Reset (in xAPIC and x2APIC mode) brings up the local APIC in xAPIC mode.
2178 * An INIT IPI does -not- cause a transition between xAPIC and x2APIC mode[2].
2179 *
2180 * [1] See AMD spec. 14.1.3 "Processor Initialization State"
2181 * [2] See Intel spec. 10.12.5.1 "x2APIC States".
2182 */
2183 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2184
2185 /* Construct. */
2186 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2187 PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
2188 uint64_t uApicBaseMsr = MSR_IA32_APICBASE_ADDR;
2189 if (pVCpu->idCpu == 0)
2190 uApicBaseMsr |= MSR_IA32_APICBASE_BSP;
2191
2192 /* If the VM was configured with no APIC, don't enable xAPIC mode, obviously. */
2193 if (pApic->enmMaxMode != PDMAPICMODE_NONE)
2194 {
2195 uApicBaseMsr |= MSR_IA32_APICBASE_EN;
2196
2197 /*
2198 * While coming out of a reset the APIC is enabled and in xAPIC mode. If software had previously
2199 * disabled the APIC (which results in the CPUID bit being cleared as well) we re-enable it here.
2200 * See Intel spec. 10.12.5.1 "x2APIC States".
2201 */
2202 if (CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, true /*fVisible*/) == false)
2203 LogRel(("APIC%u: Resetting mode to xAPIC\n", pVCpu->idCpu));
2204 }
2205
2206 /* Commit. */
2207 ASMAtomicWriteU64(&pApicCpu->uApicBaseMsr, uApicBaseMsr);
2208}
2209
2210
2211/**
2212 * Initializes per-VCPU APIC to the state following an INIT reset
2213 * ("Wait-for-SIPI" state).
2214 *
2215 * @param pVCpu The cross context virtual CPU structure.
2216 */
2217void apicInitIpi(PVMCPU pVCpu)
2218{
2219 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2220 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
2221
2222 /*
2223 * See Intel spec. 10.4.7.3 "Local APIC State After an INIT Reset (Wait-for-SIPI State)"
2224 * and AMD spec 16.3.2 "APIC Registers".
2225 *
2226 * The reason we don't simply zero out the entire APIC page and only set the non-zero members
2227 * is because there are some registers that are not touched by the INIT IPI (e.g. version)
2228 * operation and this function is only a subset of the reset operation.
2229 */
2230 RT_ZERO(pXApicPage->irr);
2231 RT_ZERO(pXApicPage->irr);
2232 RT_ZERO(pXApicPage->isr);
2233 RT_ZERO(pXApicPage->tmr);
2234 RT_ZERO(pXApicPage->icr_hi);
2235 RT_ZERO(pXApicPage->icr_lo);
2236 RT_ZERO(pXApicPage->ldr);
2237 RT_ZERO(pXApicPage->tpr);
2238 RT_ZERO(pXApicPage->ppr);
2239 RT_ZERO(pXApicPage->timer_icr);
2240 RT_ZERO(pXApicPage->timer_ccr);
2241 RT_ZERO(pXApicPage->timer_dcr);
2242
2243 pXApicPage->dfr.u.u4Model = XAPICDESTFORMAT_FLAT;
2244 pXApicPage->dfr.u.u28ReservedMb1 = UINT32_C(0xfffffff);
2245
2246 /** @todo CMCI. */
2247
2248 RT_ZERO(pXApicPage->lvt_timer);
2249 pXApicPage->lvt_timer.u.u1Mask = 1;
2250
2251#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
2252 RT_ZERO(pXApicPage->lvt_thermal);
2253 pXApicPage->lvt_thermal.u.u1Mask = 1;
2254#endif
2255
2256 RT_ZERO(pXApicPage->lvt_perf);
2257 pXApicPage->lvt_perf.u.u1Mask = 1;
2258
2259 RT_ZERO(pXApicPage->lvt_lint0);
2260 pXApicPage->lvt_lint0.u.u1Mask = 1;
2261
2262 RT_ZERO(pXApicPage->lvt_lint1);
2263 pXApicPage->lvt_lint1.u.u1Mask = 1;
2264
2265 RT_ZERO(pXApicPage->lvt_error);
2266 pXApicPage->lvt_error.u.u1Mask = 1;
2267
2268 RT_ZERO(pXApicPage->svr);
2269 pXApicPage->svr.u.u8SpuriousVector = 0xff;
2270
2271 /* The self-IPI register is reset to 0. See Intel spec. 10.12.5.1 "x2APIC States" */
2272 PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
2273 RT_ZERO(pX2ApicPage->self_ipi);
2274
2275 /* Clear the pending-interrupt bitmaps. */
2276 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2277 RT_BZERO(&pApicCpu->ApicPibLevel, sizeof(APICPIB));
2278 RT_BZERO(pApicCpu->CTX_SUFF(pvApicPib), sizeof(APICPIB));
2279
2280 /* Clear the interrupt line states for LINT0 and LINT1 pins. */
2281 pApicCpu->fActiveLint0 = false;
2282 pApicCpu->fActiveLint1 = false;
2283}
2284
2285
2286/**
2287 * Initializes per-VCPU APIC to the state following a power-up or hardware
2288 * reset.
2289 *
2290 * @param pVCpu The cross context virtual CPU structure.
2291 * @param fResetApicBaseMsr Whether to reset the APIC base MSR.
2292 */
2293void apicResetCpu(PVMCPU pVCpu, bool fResetApicBaseMsr)
2294{
2295 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2296
2297 LogFlow(("APIC%u: apicR3ResetCpu: fResetApicBaseMsr=%RTbool\n", pVCpu->idCpu, fResetApicBaseMsr));
2298
2299#ifdef VBOX_STRICT
2300 /* Verify that the initial APIC ID reported via CPUID matches our VMCPU ID assumption. */
2301 uint32_t uEax, uEbx, uEcx, uEdx;
2302 uEax = uEbx = uEcx = uEdx = UINT32_MAX;
2303 CPUMGetGuestCpuId(pVCpu, 1, 0, &uEax, &uEbx, &uEcx, &uEdx);
2304 Assert(((uEbx >> 24) & 0xff) == pVCpu->idCpu);
2305#endif
2306
2307 /*
2308 * The state following a power-up or reset is a superset of the INIT state.
2309 * See Intel spec. 10.4.7.3 "Local APIC State After an INIT Reset ('Wait-for-SIPI' State)"
2310 */
2311 apicInitIpi(pVCpu);
2312
2313 /*
2314 * The APIC version register is read-only, so just initialize it here.
2315 * It is not clear from the specs, where exactly it is initialized.
2316 * The version determines the number of LVT entries and size of the APIC ID (8 bits for P4).
2317 */
2318 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
2319#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
2320 pXApicPage->version.u.u8MaxLvtEntry = XAPIC_MAX_LVT_ENTRIES_P4 - 1;
2321 pXApicPage->version.u.u8Version = XAPIC_HARDWARE_VERSION_P4;
2322 AssertCompile(sizeof(pXApicPage->id.u8ApicId) >= XAPIC_APIC_ID_BIT_COUNT_P4 / 8);
2323#else
2324# error "Implement Pentium and P6 family APIC architectures"
2325#endif
2326
2327 /** @todo It isn't clear in the spec. where exactly the default base address
2328 * is (re)initialized, atm we do it here in Reset. */
2329 if (fResetApicBaseMsr)
2330 apicResetBaseMsr(pVCpu);
2331
2332 /*
2333 * Initialize the APIC ID register to xAPIC format.
2334 */
2335 ASMMemZero32(&pXApicPage->id, sizeof(pXApicPage->id));
2336 pXApicPage->id.u8ApicId = pVCpu->idCpu;
2337}
2338
2339
2340/**
2341 * Sets the APIC base MSR.
2342 *
2343 * @returns VBox status code - no informational ones, esp. not
2344 * VINF_CPUM_R3_MSR_WRITE. Only the following two:
2345 * @retval VINF_SUCCESS
2346 * @retval VERR_CPUM_RAISE_GP_0
2347 *
2348 * @param pVCpu The cross context virtual CPU structure.
2349 * @param u64BaseMsr The value to set.
2350 */
2351VMM_INT_DECL(int) APICSetBaseMsr(PVMCPU pVCpu, uint64_t u64BaseMsr)
2352{
2353 Assert(pVCpu);
2354
2355 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2356 PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
2357 APICMODE enmOldMode = apicGetMode(pApicCpu->uApicBaseMsr);
2358 APICMODE enmNewMode = apicGetMode(u64BaseMsr);
2359 uint64_t uBaseMsr = pApicCpu->uApicBaseMsr;
2360
2361 Log2(("APIC%u: ApicSetBaseMsr: u64BaseMsr=%#RX64 enmNewMode=%s enmOldMode=%s\n", pVCpu->idCpu, u64BaseMsr,
2362 apicGetModeName(enmNewMode), apicGetModeName(enmOldMode)));
2363
2364 /*
2365 * We do not support re-mapping the APIC base address because:
2366 * - We'll have to manage all the mappings ourselves in the APIC (reference counting based unmapping etc.)
2367 * i.e. we can only unmap the MMIO region if no other APIC is mapped on that location.
2368 * - It's unclear how/if IOM can fallback to handling regions as regular memory (if the MMIO
2369 * region remains mapped but doesn't belong to the called VCPU's APIC).
2370 */
2371 /** @todo Handle per-VCPU APIC base relocation. */
2372 if (MSR_IA32_APICBASE_GET_ADDR(uBaseMsr) != MSR_IA32_APICBASE_ADDR)
2373 {
2374 if (pVCpu->apic.s.cLogMaxSetApicBaseAddr++ < 5)
2375 LogRel(("APIC%u: Attempt to relocate base to %#RGp, unsupported -> #GP(0)\n", pVCpu->idCpu,
2376 MSR_IA32_APICBASE_GET_ADDR(uBaseMsr)));
2377 return VERR_CPUM_RAISE_GP_0;
2378 }
2379
2380 /* Don't allow enabling xAPIC/x2APIC if the VM is configured with the APIC disabled. */
2381 if (pApic->enmMaxMode == PDMAPICMODE_NONE)
2382 {
2383 LogRel(("APIC%u: Disallowing APIC base MSR write as the VM is configured with APIC disabled!\n", pVCpu->idCpu));
2384 return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_DISALLOWED_CONFIG);
2385 }
2386
2387 /*
2388 * Act on state transition.
2389 */
2390 if (enmNewMode != enmOldMode)
2391 {
2392 switch (enmNewMode)
2393 {
2394 case APICMODE_DISABLED:
2395 {
2396 /*
2397 * The APIC state needs to be reset (especially the APIC ID as x2APIC APIC ID bit layout
2398 * is different). We can start with a clean slate identical to the state after a power-up/reset.
2399 *
2400 * See Intel spec. 10.4.3 "Enabling or Disabling the Local APIC".
2401 *
2402 * We'll also manually manage the APIC base MSR here. We want a single-point of commit
2403 * at the end of this function rather than updating it in apicR3ResetCpu. This means we also
2404 * need to update the CPUID leaf ourselves.
2405 */
2406 apicResetCpu(pVCpu, false /* fResetApicBaseMsr */);
2407 uBaseMsr &= ~(MSR_IA32_APICBASE_EN | MSR_IA32_APICBASE_EXTD);
2408 CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, false /*fVisible*/);
2409 LogRel(("APIC%u: Switched mode to disabled\n", pVCpu->idCpu));
2410 break;
2411 }
2412
2413 case APICMODE_XAPIC:
2414 {
2415 if (enmOldMode != APICMODE_DISABLED)
2416 {
2417 LogRel(("APIC%u: Can only transition to xAPIC state from disabled state\n", pVCpu->idCpu));
2418 return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
2419 }
2420
2421 uBaseMsr |= MSR_IA32_APICBASE_EN;
2422 CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, true /*fVisible*/);
2423 LogRel(("APIC%u: Switched mode to xAPIC\n", pVCpu->idCpu));
2424 break;
2425 }
2426
2427 case APICMODE_X2APIC:
2428 {
2429 if (pApic->enmMaxMode != PDMAPICMODE_X2APIC)
2430 {
2431 LogRel(("APIC%u: Disallowing transition to x2APIC mode as the VM is configured with the x2APIC disabled!\n",
2432 pVCpu->idCpu));
2433 return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
2434 }
2435
2436 if (enmOldMode != APICMODE_XAPIC)
2437 {
2438 LogRel(("APIC%u: Can only transition to x2APIC state from xAPIC state\n", pVCpu->idCpu));
2439 return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
2440 }
2441
2442 uBaseMsr |= MSR_IA32_APICBASE_EN | MSR_IA32_APICBASE_EXTD;
2443
2444 /*
2445 * The APIC ID needs updating when entering x2APIC mode.
2446 * Software written APIC ID in xAPIC mode isn't preserved.
2447 * The APIC ID becomes read-only to software in x2APIC mode.
2448 *
2449 * See Intel spec. 10.12.5.1 "x2APIC States".
2450 */
2451 PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
2452 ASMMemZero32(&pX2ApicPage->id, sizeof(pX2ApicPage->id));
2453 pX2ApicPage->id.u32ApicId = pVCpu->idCpu;
2454
2455 /*
2456 * LDR initialization occurs when entering x2APIC mode.
2457 * See Intel spec. 10.12.10.2 "Deriving Logical x2APIC ID from the Local x2APIC ID".
2458 */
2459 pX2ApicPage->ldr.u32LogicalApicId = ((pX2ApicPage->id.u32ApicId & UINT32_C(0xffff0)) << 16)
2460 | (UINT32_C(1) << pX2ApicPage->id.u32ApicId & UINT32_C(0xf));
2461
2462 LogRel(("APIC%u: Switched mode to x2APIC\n", pVCpu->idCpu));
2463 break;
2464 }
2465
2466 case APICMODE_INVALID:
2467 default:
2468 {
2469 Log(("APIC%u: Invalid state transition attempted\n", pVCpu->idCpu));
2470 return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
2471 }
2472 }
2473 }
2474
2475 ASMAtomicWriteU64(&pApicCpu->uApicBaseMsr, uBaseMsr);
2476 return VINF_SUCCESS;
2477}
2478
2479
2480/**
2481 * Gets the APIC base MSR (no checks are performed wrt APIC hardware or its
2482 * state).
2483 *
2484 * @returns The base MSR value.
2485 * @param pVCpu The cross context virtual CPU structure.
2486 */
2487VMM_INT_DECL(uint64_t) APICGetBaseMsrNoCheck(PVMCPU pVCpu)
2488{
2489 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2490 PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2491 return pApicCpu->uApicBaseMsr;
2492}
2493
2494
2495/**
2496 * Gets the APIC base MSR.
2497 *
2498 * @returns Strict VBox status code.
2499 * @param pVCpu The cross context virtual CPU structure.
2500 * @param pu64Value Where to store the MSR value.
2501 */
2502VMM_INT_DECL(VBOXSTRICTRC) APICGetBaseMsr(PVMCPU pVCpu, uint64_t *pu64Value)
2503{
2504 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2505
2506 PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
2507 if (pApic->enmMaxMode != PDMAPICMODE_NONE)
2508 {
2509 *pu64Value = APICGetBaseMsrNoCheck(pVCpu);
2510 return VINF_SUCCESS;
2511 }
2512
2513 if (pVCpu->apic.s.cLogMaxGetApicBaseAddr++ < 5)
2514 LogRel(("APIC%u: Reading APIC base MSR (%#x) when there is no APIC -> #GP(0)\n", pVCpu->idCpu, MSR_IA32_APICBASE));
2515 return VERR_CPUM_RAISE_GP_0;
2516}
2517
2518
2519/**
2520 * Sets the TPR (Task Priority Register).
2521 *
2522 * @retval VINF_SUCCESS
2523 * @retval VERR_CPUM_RAISE_GP_0
2524 * @retval VERR_PDM_NO_APIC_INSTANCE
2525 *
2526 * @param pVCpu The cross context virtual CPU structure.
2527 * @param u8Tpr The TPR value to set.
2528 */
2529VMMDECL(int) APICSetTpr(PVMCPU pVCpu, uint8_t u8Tpr)
2530{
2531 if (APICIsEnabled(pVCpu))
2532 return apicSetTprEx(pVCpu, u8Tpr, false /* fForceX2ApicBehaviour */);
2533 return VERR_PDM_NO_APIC_INSTANCE;
2534}
2535
2536
2537/**
2538 * Gets the highest priority pending interrupt.
2539 *
2540 * @returns true if any interrupt is pending, false otherwise.
2541 * @param pVCpu The cross context virtual CPU structure.
2542 * @param pu8PendingIntr Where to store the interrupt vector if the
2543 * interrupt is pending (optional, can be NULL).
2544 */
2545static bool apicGetHighestPendingInterrupt(PVMCPU pVCpu, uint8_t *pu8PendingIntr)
2546{
2547 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
2548 int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1);
2549 if (irrv >= 0)
2550 {
2551 Assert(irrv <= (int)UINT8_MAX);
2552 if (pu8PendingIntr)
2553 *pu8PendingIntr = (uint8_t)irrv;
2554 return true;
2555 }
2556 return false;
2557}
2558
2559
2560/**
2561 * Gets the APIC TPR (Task Priority Register).
2562 *
2563 * @returns VBox status code.
2564 * @param pVCpu The cross context virtual CPU structure.
2565 * @param pu8Tpr Where to store the TPR.
2566 * @param pfPending Where to store whether there is a pending interrupt
2567 * (optional, can be NULL).
2568 * @param pu8PendingIntr Where to store the highest-priority pending
2569 * interrupt (optional, can be NULL).
2570 */
2571VMMDECL(int) APICGetTpr(PVMCPU pVCpu, uint8_t *pu8Tpr, bool *pfPending, uint8_t *pu8PendingIntr)
2572{
2573 VMCPU_ASSERT_EMT(pVCpu);
2574 if (APICIsEnabled(pVCpu))
2575 {
2576 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
2577 if (pfPending)
2578 {
2579 /*
2580 * Just return whatever the highest pending interrupt is in the IRR.
2581 * The caller is responsible for figuring out if it's masked by the TPR etc.
2582 */
2583 *pfPending = apicGetHighestPendingInterrupt(pVCpu, pu8PendingIntr);
2584 }
2585
2586 *pu8Tpr = pXApicPage->tpr.u8Tpr;
2587 return VINF_SUCCESS;
2588 }
2589
2590 *pu8Tpr = 0;
2591 return VERR_PDM_NO_APIC_INSTANCE;
2592}
2593
2594
2595/**
2596 * Gets the APIC timer frequency.
2597 *
2598 * @returns Strict VBox status code.
2599 * @param pVM The cross context VM structure.
2600 * @param pu64Value Where to store the timer frequency.
2601 */
2602VMM_INT_DECL(int) APICGetTimerFreq(PVM pVM, uint64_t *pu64Value)
2603{
2604 /*
2605 * Validate.
2606 */
2607 Assert(pVM);
2608 AssertPtrReturn(pu64Value, VERR_INVALID_PARAMETER);
2609
2610 PVMCPU pVCpu = &pVM->aCpus[0];
2611 if (APICIsEnabled(pVCpu))
2612 {
2613 PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2614 *pu64Value = TMTimerGetFreq(pApicCpu->CTX_SUFF(pTimer));
2615 return VINF_SUCCESS;
2616 }
2617 return VERR_PDM_NO_APIC_INSTANCE;
2618}
2619
2620
2621/**
2622 * Delivers an interrupt message via the system bus.
2623 *
2624 * @returns VBox status code.
2625 * @param pVM The cross context VM structure.
2626 * @param uDest The destination mask.
2627 * @param uDestMode The destination mode.
2628 * @param uDeliveryMode The delivery mode.
2629 * @param uVector The interrupt vector.
2630 * @param uPolarity The interrupt line polarity.
2631 * @param uTriggerMode The trigger mode.
2632 * @param uSrcTag The interrupt source tag (debugging).
2633 */
2634VMM_INT_DECL(int) APICBusDeliver(PVM pVM, uint8_t uDest, uint8_t uDestMode, uint8_t uDeliveryMode, uint8_t uVector,
2635 uint8_t uPolarity, uint8_t uTriggerMode, uint32_t uSrcTag)
2636{
2637 NOREF(uPolarity);
2638
2639 /*
2640 * If the APIC isn't enabled, do nothing and pretend success.
2641 */
2642 if (APICIsEnabled(&pVM->aCpus[0]))
2643 { /* likely */ }
2644 else
2645 return VINF_SUCCESS;
2646
2647 /*
2648 * The destination field (mask) in the IO APIC redirectable table entry is 8-bits.
2649 * Hence, the broadcast mask is 0xff.
2650 * See IO APIC spec. 3.2.4. "IOREDTBL[23:0] - I/O Redirectable Table Registers".
2651 */
2652 XAPICTRIGGERMODE enmTriggerMode = (XAPICTRIGGERMODE)uTriggerMode;
2653 XAPICDELIVERYMODE enmDeliveryMode = (XAPICDELIVERYMODE)uDeliveryMode;
2654 XAPICDESTMODE enmDestMode = (XAPICDESTMODE)uDestMode;
2655 uint32_t fDestMask = uDest;
2656 uint32_t fBroadcastMask = UINT32_C(0xff);
2657
2658 Log2(("APIC: apicBusDeliver: fDestMask=%#x enmDestMode=%s enmTriggerMode=%s enmDeliveryMode=%s uVector=%#x\n", fDestMask,
2659 apicGetDestModeName(enmDestMode), apicGetTriggerModeName(enmTriggerMode), apicGetDeliveryModeName(enmDeliveryMode),
2660 uVector));
2661
2662 bool fIntrAccepted;
2663 VMCPUSET DestCpuSet;
2664 apicGetDestCpuSet(pVM, fDestMask, fBroadcastMask, enmDestMode, enmDeliveryMode, &DestCpuSet);
2665 VBOXSTRICTRC rcStrict = apicSendIntr(pVM, NULL /* pVCpu */, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
2666 &fIntrAccepted, uSrcTag, VINF_SUCCESS /* rcRZ */);
2667 if (fIntrAccepted)
2668 return VBOXSTRICTRC_VAL(rcStrict);
2669 return VERR_APIC_INTR_DISCARDED;
2670}
2671
2672
2673/**
2674 * Assert/de-assert the local APIC's LINT0/LINT1 interrupt pins.
2675 *
2676 * @returns Strict VBox status code.
2677 * @param pVCpu The cross context virtual CPU structure.
2678 * @param u8Pin The interrupt pin (0 for LINT0 or 1 for LINT1).
2679 * @param u8Level The level (0 for low or 1 for high).
2680 * @param rcRZ The return code if the operation cannot be performed in
2681 * the current context.
2682 */
2683VMM_INT_DECL(VBOXSTRICTRC) APICLocalInterrupt(PVMCPU pVCpu, uint8_t u8Pin, uint8_t u8Level, int rcRZ)
2684{
2685 AssertReturn(u8Pin <= 1, VERR_INVALID_PARAMETER);
2686 AssertReturn(u8Level <= 1, VERR_INVALID_PARAMETER);
2687
2688 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
2689
2690 /* If the APIC is enabled, the interrupt is subject to LVT programming. */
2691 if (APICIsEnabled(pVCpu))
2692 {
2693 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
2694
2695 /* Pick the LVT entry corresponding to the interrupt pin. */
2696 static const uint16_t s_au16LvtOffsets[] =
2697 {
2698 XAPIC_OFF_LVT_LINT0,
2699 XAPIC_OFF_LVT_LINT1
2700 };
2701 Assert(u8Pin < RT_ELEMENTS(s_au16LvtOffsets));
2702 uint16_t const offLvt = s_au16LvtOffsets[u8Pin];
2703 uint32_t const uLvt = apicReadRaw32(pXApicPage, offLvt);
2704
2705 /* If software hasn't masked the interrupt in the LVT entry, proceed interrupt processing. */
2706 if (!XAPIC_LVT_IS_MASKED(uLvt))
2707 {
2708 XAPICDELIVERYMODE const enmDeliveryMode = XAPIC_LVT_GET_DELIVERY_MODE(uLvt);
2709 XAPICTRIGGERMODE enmTriggerMode = XAPIC_LVT_GET_TRIGGER_MODE(uLvt);
2710
2711 switch (enmDeliveryMode)
2712 {
2713 case XAPICDELIVERYMODE_INIT:
2714 {
2715 /** @todo won't work in R0/RC because callers don't care about rcRZ. */
2716 AssertMsgFailed(("INIT through LINT0/LINT1 is not yet supported\n"));
2717 }
2718 RT_FALL_THRU();
2719 case XAPICDELIVERYMODE_FIXED:
2720 {
2721 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2722 uint8_t const uVector = XAPIC_LVT_GET_VECTOR(uLvt);
2723 bool fActive = RT_BOOL(u8Level & 1);
2724 bool volatile *pfActiveLine = u8Pin == 0 ? &pApicCpu->fActiveLint0 : &pApicCpu->fActiveLint1;
2725 /** @todo Polarity is busted elsewhere, we need to fix that
2726 * first. See @bugref{8386#c7}. */
2727#if 0
2728 uint8_t const u8Polarity = XAPIC_LVT_GET_POLARITY(uLvt);
2729 fActive ^= u8Polarity; */
2730#endif
2731 if (!fActive)
2732 {
2733 ASMAtomicCmpXchgBool(pfActiveLine, false, true);
2734 break;
2735 }
2736
2737 /* Level-sensitive interrupts are not supported for LINT1. See Intel spec. 10.5.1 "Local Vector Table". */
2738 if (offLvt == XAPIC_OFF_LVT_LINT1)
2739 enmTriggerMode = XAPICTRIGGERMODE_EDGE;
2740 /** @todo figure out what "If the local APIC is not used in conjunction with an I/O APIC and fixed
2741 delivery mode is selected; the Pentium 4, Intel Xeon, and P6 family processors will always
2742 use level-sensitive triggering, regardless if edge-sensitive triggering is selected."
2743 means. */
2744
2745 bool fSendIntr;
2746 if (enmTriggerMode == XAPICTRIGGERMODE_EDGE)
2747 {
2748 /* Recognize and send the interrupt only on an edge transition. */
2749 fSendIntr = ASMAtomicCmpXchgBool(pfActiveLine, true, false);
2750 }
2751 else
2752 {
2753 /* For level-triggered interrupts, redundant interrupts are not a problem. */
2754 Assert(enmTriggerMode == XAPICTRIGGERMODE_LEVEL);
2755 ASMAtomicCmpXchgBool(pfActiveLine, true, false);
2756
2757 /* Only when the remote IRR isn't set, set it and send the interrupt. */
2758 if (!(pXApicPage->lvt_lint0.all.u32LvtLint0 & XAPIC_LVT_REMOTE_IRR))
2759 {
2760 Assert(offLvt == XAPIC_OFF_LVT_LINT0);
2761 ASMAtomicOrU32((volatile uint32_t *)&pXApicPage->lvt_lint0.all.u32LvtLint0, XAPIC_LVT_REMOTE_IRR);
2762 fSendIntr = true;
2763 }
2764 else
2765 fSendIntr = false;
2766 }
2767
2768 if (fSendIntr)
2769 {
2770 VMCPUSET DestCpuSet;
2771 VMCPUSET_EMPTY(&DestCpuSet);
2772 VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
2773 rcStrict = apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode,
2774 &DestCpuSet, NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
2775 }
2776 break;
2777 }
2778
2779 case XAPICDELIVERYMODE_SMI:
2780 case XAPICDELIVERYMODE_NMI:
2781 {
2782 VMCPUSET DestCpuSet;
2783 VMCPUSET_EMPTY(&DestCpuSet);
2784 VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
2785 uint8_t const uVector = XAPIC_LVT_GET_VECTOR(uLvt);
2786 rcStrict = apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
2787 NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
2788 break;
2789 }
2790
2791 case XAPICDELIVERYMODE_EXTINT:
2792 {
2793 Log2(("APIC%u: apicLocalInterrupt: %s ExtINT through LINT%u\n", pVCpu->idCpu,
2794 u8Level ? "Raising" : "Lowering", u8Pin));
2795 if (u8Level)
2796 apicSetInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
2797 else
2798 apicClearInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
2799 break;
2800 }
2801
2802 /* Reserved/unknown delivery modes: */
2803 case XAPICDELIVERYMODE_LOWEST_PRIO:
2804 case XAPICDELIVERYMODE_STARTUP:
2805 default:
2806 {
2807 rcStrict = VERR_INTERNAL_ERROR_3;
2808 AssertMsgFailed(("APIC%u: LocalInterrupt: Invalid delivery mode %#x (%s) on LINT%d\n", pVCpu->idCpu,
2809 enmDeliveryMode, apicGetDeliveryModeName(enmDeliveryMode), u8Pin));
2810 break;
2811 }
2812 }
2813 }
2814 }
2815 else
2816 {
2817 /* The APIC is hardware disabled. The CPU behaves as though there is no on-chip APIC. */
2818 if (u8Pin == 0)
2819 {
2820 /* LINT0 behaves as an external interrupt pin. */
2821 Log2(("APIC%u: apicLocalInterrupt: APIC hardware-disabled, %s INTR\n", pVCpu->idCpu,
2822 u8Level ? "raising" : "lowering"));
2823 if (u8Level)
2824 apicSetInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
2825 else
2826 apicClearInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
2827 }
2828 else
2829 {
2830 /* LINT1 behaves as NMI. */
2831 Log2(("APIC%u: apicLocalInterrupt: APIC hardware-disabled, raising NMI\n", pVCpu->idCpu));
2832 apicSetInterruptFF(pVCpu, PDMAPICIRQ_NMI);
2833 }
2834 }
2835
2836 return rcStrict;
2837}
2838
2839
2840/**
2841 * Gets the next highest-priority interrupt from the APIC, marking it as an
2842 * "in-service" interrupt.
2843 *
2844 * @returns VBox status code.
2845 * @param pVCpu The cross context virtual CPU structure.
2846 * @param pu8Vector Where to store the vector.
2847 * @param puSrcTag Where to store the interrupt source tag (debugging).
2848 */
2849VMM_INT_DECL(int) APICGetInterrupt(PVMCPU pVCpu, uint8_t *pu8Vector, uint32_t *puSrcTag)
2850{
2851 VMCPU_ASSERT_EMT(pVCpu);
2852 Assert(pu8Vector);
2853
2854 LogFlow(("APIC%u: apicGetInterrupt:\n", pVCpu->idCpu));
2855
2856 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
2857 bool const fApicHwEnabled = APICIsEnabled(pVCpu);
2858 if ( fApicHwEnabled
2859 && pXApicPage->svr.u.fApicSoftwareEnable)
2860 {
2861 int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1);
2862 if (RT_LIKELY(irrv >= 0))
2863 {
2864 Assert(irrv <= (int)UINT8_MAX);
2865 uint8_t const uVector = irrv;
2866
2867 /*
2868 * This can happen if the APIC receives an interrupt when the CPU has interrupts
2869 * disabled but the TPR is raised by the guest before re-enabling interrupts.
2870 */
2871 uint8_t const uTpr = pXApicPage->tpr.u8Tpr;
2872 if ( uTpr > 0
2873 && XAPIC_TPR_GET_TP(uVector) <= XAPIC_TPR_GET_TP(uTpr))
2874 {
2875 Log2(("APIC%u: apicGetInterrupt: Interrupt masked. uVector=%#x uTpr=%#x SpuriousVector=%#x\n", pVCpu->idCpu,
2876 uVector, uTpr, pXApicPage->svr.u.u8SpuriousVector));
2877 *pu8Vector = uVector;
2878 *puSrcTag = 0;
2879 STAM_COUNTER_INC(&pVCpu->apic.s.StatMaskedByTpr);
2880 return VERR_APIC_INTR_MASKED_BY_TPR;
2881 }
2882
2883 /*
2884 * The PPR should be up-to-date at this point through apicSetEoi().
2885 * We're on EMT so no parallel updates possible.
2886 * Subject the pending vector to PPR prioritization.
2887 */
2888 uint8_t const uPpr = pXApicPage->ppr.u8Ppr;
2889 if ( !uPpr
2890 || XAPIC_PPR_GET_PP(uVector) > XAPIC_PPR_GET_PP(uPpr))
2891 {
2892 apicClearVectorInReg(&pXApicPage->irr, uVector);
2893 apicSetVectorInReg(&pXApicPage->isr, uVector);
2894 apicUpdatePpr(pVCpu);
2895 apicSignalNextPendingIntr(pVCpu);
2896
2897 /* Retrieve the interrupt source tag associated with this interrupt. */
2898 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
2899 AssertCompile(RT_ELEMENTS(pApicCpu->auSrcTags) > UINT8_MAX);
2900 *puSrcTag = pApicCpu->auSrcTags[uVector];
2901 pApicCpu->auSrcTags[uVector] = 0;
2902
2903 Log2(("APIC%u: apicGetInterrupt: Valid Interrupt. uVector=%#x\n", pVCpu->idCpu, uVector));
2904 *pu8Vector = uVector;
2905 return VINF_SUCCESS;
2906 }
2907 else
2908 {
2909 STAM_COUNTER_INC(&pVCpu->apic.s.StatMaskedByPpr);
2910 Log2(("APIC%u: apicGetInterrupt: Interrupt's priority is not higher than the PPR. uVector=%#x PPR=%#x\n",
2911 pVCpu->idCpu, uVector, uPpr));
2912 }
2913 }
2914 else
2915 Log2(("APIC%u: apicGetInterrupt: No pending bits in IRR\n", pVCpu->idCpu));
2916 }
2917 else
2918 Log2(("APIC%u: apicGetInterrupt: APIC %s disabled\n", pVCpu->idCpu, !fApicHwEnabled ? "hardware" : "software"));
2919
2920 *pu8Vector = 0;
2921 *puSrcTag = 0;
2922 return VERR_APIC_INTR_NOT_PENDING;
2923}
2924
2925
2926/**
2927 * @callback_method_impl{FNIOMMMIOREAD}
2928 */
2929APICBOTHCBDECL(int) apicReadMmio(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2930{
2931 NOREF(pvUser);
2932 Assert(!(GCPhysAddr & 0xf));
2933 Assert(cb == 4); RT_NOREF_PV(cb);
2934
2935 PAPICDEV pApicDev = PDMINS_2_DATA(pDevIns, PAPICDEV);
2936 PVMCPU pVCpu = PDMDevHlpGetVMCPU(pDevIns);
2937 uint16_t offReg = GCPhysAddr & 0xff0;
2938 uint32_t uValue = 0;
2939
2940 STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMmioRead));
2941
2942 int rc = VBOXSTRICTRC_VAL(apicReadRegister(pApicDev, pVCpu, offReg, &uValue));
2943 *(uint32_t *)pv = uValue;
2944
2945 Log2(("APIC%u: apicReadMmio: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
2946 return rc;
2947}
2948
2949
2950/**
2951 * @callback_method_impl{FNIOMMMIOWRITE}
2952 */
2953APICBOTHCBDECL(int) apicWriteMmio(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
2954{
2955 NOREF(pvUser);
2956 Assert(!(GCPhysAddr & 0xf));
2957 Assert(cb == 4); RT_NOREF_PV(cb);
2958
2959 PAPICDEV pApicDev = PDMINS_2_DATA(pDevIns, PAPICDEV);
2960 PVMCPU pVCpu = PDMDevHlpGetVMCPU(pDevIns);
2961 uint16_t offReg = GCPhysAddr & 0xff0;
2962 uint32_t uValue = *(uint32_t *)pv;
2963
2964 STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMmioWrite));
2965
2966 Log2(("APIC%u: apicWriteMmio: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
2967
2968 int rc = VBOXSTRICTRC_VAL(apicWriteRegister(pApicDev, pVCpu, offReg, uValue));
2969 return rc;
2970}
2971
2972
2973/**
2974 * Sets the interrupt pending force-flag and pokes the EMT if required.
2975 *
2976 * @param pVCpu The cross context virtual CPU structure.
2977 * @param enmType The IRQ type.
2978 */
2979static void apicSetInterruptFF(PVMCPU pVCpu, PDMAPICIRQ enmType)
2980{
2981#ifdef IN_RING3
2982 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
2983 Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
2984#endif
2985
2986 switch (enmType)
2987 {
2988 case PDMAPICIRQ_HARDWARE:
2989 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
2990 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
2991 break;
2992 case PDMAPICIRQ_UPDATE_PENDING: VMCPU_FF_SET(pVCpu, VMCPU_FF_UPDATE_APIC); break;
2993 case PDMAPICIRQ_NMI: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI); break;
2994 case PDMAPICIRQ_SMI: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI); break;
2995 case PDMAPICIRQ_EXTINT: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC); break;
2996 default:
2997 AssertMsgFailed(("enmType=%d\n", enmType));
2998 break;
2999 }
3000
3001 /*
3002 * We need to wake up the target CPU if we're not on EMT.
3003 */
3004#if defined(IN_RING0)
3005 PVM pVM = pVCpu->CTX_SUFF(pVM);
3006 VMCPUID idCpu = pVCpu->idCpu;
3007 if ( enmType != PDMAPICIRQ_HARDWARE
3008 && VMMGetCpuId(pVM) != idCpu)
3009 {
3010 switch (VMCPU_GET_STATE(pVCpu))
3011 {
3012 case VMCPUSTATE_STARTED_EXEC:
3013 GVMMR0SchedPokeNoGVMNoLock(pVM, idCpu);
3014 break;
3015
3016 case VMCPUSTATE_STARTED_HALTED:
3017 GVMMR0SchedWakeUpNoGVMNoLock(pVM, idCpu);
3018 break;
3019
3020 default:
3021 break; /* nothing to do in other states. */
3022 }
3023 }
3024#elif defined(IN_RING3)
3025# ifdef VBOX_WITH_REM
3026 REMR3NotifyInterruptSet(pVCpu->CTX_SUFF(pVM), pVCpu);
3027# endif
3028 if (enmType != PDMAPICIRQ_HARDWARE)
3029 VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM | VMNOTIFYFF_FLAGS_POKE);
3030#endif
3031}
3032
3033
3034/**
3035 * Clears the interrupt pending force-flag.
3036 *
3037 * @param pVCpu The cross context virtual CPU structure.
3038 * @param enmType The IRQ type.
3039 */
3040VMM_INT_DECL(void) apicClearInterruptFF(PVMCPU pVCpu, PDMAPICIRQ enmType)
3041{
3042#ifdef IN_RING3
3043 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
3044 Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
3045#endif
3046
3047 /* NMI/SMI can't be cleared. */
3048 switch (enmType)
3049 {
3050 case PDMAPICIRQ_HARDWARE: VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC); break;
3051 case PDMAPICIRQ_EXTINT: VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC); break;
3052 default:
3053 AssertMsgFailed(("enmType=%d\n", enmType));
3054 break;
3055 }
3056
3057#if defined(IN_RING3) && defined(VBOX_WITH_REM)
3058 REMR3NotifyInterruptClear(pVCpu->CTX_SUFF(pVM), pVCpu);
3059#endif
3060}
3061
3062
3063/**
3064 * Posts an interrupt to a target APIC.
3065 *
3066 * This function handles interrupts received from the system bus or
3067 * interrupts generated locally from the LVT or via a self IPI.
3068 *
3069 * Don't use this function to try and deliver ExtINT style interrupts.
3070 *
3071 * @returns true if the interrupt was accepted, false otherwise.
3072 * @param pVCpu The cross context virtual CPU structure.
3073 * @param uVector The vector of the interrupt to be posted.
3074 * @param enmTriggerMode The trigger mode of the interrupt.
3075 * @param uSrcTag The interrupt source tag (debugging).
3076 *
3077 * @thread Any.
3078 */
3079VMM_INT_DECL(bool) apicPostInterrupt(PVMCPU pVCpu, uint8_t uVector, XAPICTRIGGERMODE enmTriggerMode, uint32_t uSrcTag)
3080{
3081 Assert(pVCpu);
3082 Assert(uVector > XAPIC_ILLEGAL_VECTOR_END);
3083
3084 PVM pVM = pVCpu->CTX_SUFF(pVM);
3085 PCAPIC pApic = VM_TO_APIC(pVM);
3086 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
3087 bool fAccepted = true;
3088
3089 STAM_PROFILE_START(&pApicCpu->StatPostIntr, a);
3090
3091 /*
3092 * Only post valid interrupt vectors.
3093 * See Intel spec. 10.5.2 "Valid Interrupt Vectors".
3094 */
3095 if (RT_LIKELY(uVector > XAPIC_ILLEGAL_VECTOR_END))
3096 {
3097 /*
3098 * If the interrupt is already pending in the IRR we can skip the
3099 * potential expensive operation of poking the guest EMT out of execution.
3100 */
3101 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
3102 if (!apicTestVectorInReg(&pXApicPage->irr, uVector)) /* PAV */
3103 {
3104 /* Update the interrupt source tag (debugging). */
3105 if (!pApicCpu->auSrcTags[uVector])
3106 pApicCpu->auSrcTags[uVector] = uSrcTag;
3107 else
3108 pApicCpu->auSrcTags[uVector] |= RT_BIT_32(31);
3109
3110 Log2(("APIC: apicPostInterrupt: SrcCpu=%u TargetCpu=%u uVector=%#x\n", VMMGetCpuId(pVM), pVCpu->idCpu, uVector));
3111 if (enmTriggerMode == XAPICTRIGGERMODE_EDGE)
3112 {
3113 if (pApic->fPostedIntrsEnabled)
3114 { /** @todo posted-interrupt call to hardware */ }
3115 else
3116 {
3117 apicSetVectorInPib(pApicCpu->CTX_SUFF(pvApicPib), uVector);
3118 uint32_t const fAlreadySet = apicSetNotificationBitInPib((PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib));
3119 if (!fAlreadySet)
3120 {
3121 Log2(("APIC: apicPostInterrupt: Setting UPDATE_APIC FF for edge-triggered intr. uVector=%#x\n", uVector));
3122 apicSetInterruptFF(pVCpu, PDMAPICIRQ_UPDATE_PENDING);
3123 }
3124 }
3125 }
3126 else
3127 {
3128 /*
3129 * Level-triggered interrupts requires updating of the TMR and thus cannot be
3130 * delivered asynchronously.
3131 */
3132 apicSetVectorInPib(&pApicCpu->ApicPibLevel, uVector);
3133 uint32_t const fAlreadySet = apicSetNotificationBitInPib(&pApicCpu->ApicPibLevel);
3134 if (!fAlreadySet)
3135 {
3136 Log2(("APIC: apicPostInterrupt: Setting UPDATE_APIC FF for level-triggered intr. uVector=%#x\n", uVector));
3137 apicSetInterruptFF(pVCpu, PDMAPICIRQ_UPDATE_PENDING);
3138 }
3139 }
3140 }
3141 else
3142 {
3143 Log2(("APIC: apicPostInterrupt: SrcCpu=%u TargetCpu=%u. Vector %#x Already in IRR, skipping\n", VMMGetCpuId(pVM),
3144 pVCpu->idCpu, uVector));
3145 STAM_COUNTER_INC(&pApicCpu->StatPostIntrAlreadyPending);
3146 }
3147 }
3148 else
3149 {
3150 fAccepted = false;
3151 apicSetError(pVCpu, XAPIC_ESR_RECV_ILLEGAL_VECTOR);
3152 }
3153
3154 STAM_PROFILE_STOP(&pApicCpu->StatPostIntr, a);
3155 return fAccepted;
3156}
3157
3158
3159/**
3160 * Starts the APIC timer.
3161 *
3162 * @param pVCpu The cross context virtual CPU structure.
3163 * @param uInitialCount The timer's Initial-Count Register (ICR), must be >
3164 * 0.
3165 * @thread Any.
3166 */
3167VMM_INT_DECL(void) apicStartTimer(PVMCPU pVCpu, uint32_t uInitialCount)
3168{
3169 Assert(pVCpu);
3170 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
3171 Assert(TMTimerIsLockOwner(pApicCpu->CTX_SUFF(pTimer)));
3172 Assert(uInitialCount > 0);
3173
3174 PCXAPICPAGE pXApicPage = APICCPU_TO_CXAPICPAGE(pApicCpu);
3175 uint8_t const uTimerShift = apicGetTimerShift(pXApicPage);
3176 uint64_t const cTicksToNext = (uint64_t)uInitialCount << uTimerShift;
3177
3178 Log2(("APIC%u: apicStartTimer: uInitialCount=%#RX32 uTimerShift=%u cTicksToNext=%RU64\n", pVCpu->idCpu, uInitialCount,
3179 uTimerShift, cTicksToNext));
3180
3181 /*
3182 * The assumption here is that the timer doesn't tick during this call
3183 * and thus setting a relative time to fire next is accurate. The advantage
3184 * however is updating u64TimerInitial 'atomically' while setting the next
3185 * tick.
3186 */
3187 PTMTIMER pTimer = pApicCpu->CTX_SUFF(pTimer);
3188 TMTimerSetRelative(pTimer, cTicksToNext, &pApicCpu->u64TimerInitial);
3189 apicHintTimerFreq(pApicCpu, uInitialCount, uTimerShift);
3190}
3191
3192
3193/**
3194 * Stops the APIC timer.
3195 *
3196 * @param pVCpu The cross context virtual CPU structure.
3197 * @thread Any.
3198 */
3199static void apicStopTimer(PVMCPU pVCpu)
3200{
3201 Assert(pVCpu);
3202 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
3203 Assert(TMTimerIsLockOwner(pApicCpu->CTX_SUFF(pTimer)));
3204
3205 Log2(("APIC%u: apicStopTimer\n", pVCpu->idCpu));
3206
3207 PTMTIMER pTimer = pApicCpu->CTX_SUFF(pTimer);
3208 TMTimerStop(pTimer); /* This will reset the hint, no need to explicitly call TMTimerSetFrequencyHint(). */
3209 pApicCpu->uHintedTimerInitialCount = 0;
3210 pApicCpu->uHintedTimerShift = 0;
3211}
3212
3213
3214/**
3215 * Queues a pending interrupt as in-service.
3216 *
3217 * This function should only be needed without virtualized APIC
3218 * registers. With virtualized APIC registers, it's sufficient to keep
3219 * the interrupts pending in the IRR as the hardware takes care of
3220 * virtual interrupt delivery.
3221 *
3222 * @returns true if the interrupt was queued to in-service interrupts,
3223 * false otherwise.
3224 * @param pVCpu The cross context virtual CPU structure.
3225 * @param u8PendingIntr The pending interrupt to queue as
3226 * in-service.
3227 *
3228 * @remarks This assumes the caller has done the necessary checks and
3229 * is ready to take actually service the interrupt (TPR,
3230 * interrupt shadow etc.)
3231 */
3232VMM_INT_DECL(bool) APICQueueInterruptToService(PVMCPU pVCpu, uint8_t u8PendingIntr)
3233{
3234 VMCPU_ASSERT_EMT(pVCpu);
3235
3236 PVM pVM = pVCpu->CTX_SUFF(pVM);
3237 PAPIC pApic = VM_TO_APIC(pVM);
3238 Assert(!pApic->fVirtApicRegsEnabled);
3239 NOREF(pApic);
3240
3241 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
3242 bool const fIsPending = apicTestVectorInReg(&pXApicPage->irr, u8PendingIntr);
3243 if (fIsPending)
3244 {
3245 apicClearVectorInReg(&pXApicPage->irr, u8PendingIntr);
3246 apicSetVectorInReg(&pXApicPage->isr, u8PendingIntr);
3247 apicUpdatePpr(pVCpu);
3248 return true;
3249 }
3250 return false;
3251}
3252
3253
3254/**
3255 * De-queues a pending interrupt from in-service.
3256 *
3257 * This undoes APICQueueInterruptToService() for premature VM-exits before event
3258 * injection.
3259 *
3260 * @param pVCpu The cross context virtual CPU structure.
3261 * @param u8PendingIntr The pending interrupt to de-queue from
3262 * in-service.
3263 */
3264VMM_INT_DECL(void) APICDequeueInterruptFromService(PVMCPU pVCpu, uint8_t u8PendingIntr)
3265{
3266 VMCPU_ASSERT_EMT(pVCpu);
3267
3268 PVM pVM = pVCpu->CTX_SUFF(pVM);
3269 PAPIC pApic = VM_TO_APIC(pVM);
3270 Assert(!pApic->fVirtApicRegsEnabled);
3271 NOREF(pApic);
3272
3273 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
3274 bool const fInService = apicTestVectorInReg(&pXApicPage->isr, u8PendingIntr);
3275 if (fInService)
3276 {
3277 apicClearVectorInReg(&pXApicPage->isr, u8PendingIntr);
3278 apicSetVectorInReg(&pXApicPage->irr, u8PendingIntr);
3279 apicUpdatePpr(pVCpu);
3280 }
3281}
3282
3283
3284/**
3285 * Updates pending interrupts from the pending-interrupt bitmaps to the IRR.
3286 *
3287 * @param pVCpu The cross context virtual CPU structure.
3288 *
3289 * @note NEM/win is ASSUMING the an up to date TPR is not required here.
3290 */
3291VMMDECL(void) APICUpdatePendingInterrupts(PVMCPU pVCpu)
3292{
3293 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
3294
3295 PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
3296 PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
3297 bool fHasPendingIntrs = false;
3298
3299 Log3(("APIC%u: APICUpdatePendingInterrupts:\n", pVCpu->idCpu));
3300 STAM_PROFILE_START(&pApicCpu->StatUpdatePendingIntrs, a);
3301
3302 /* Update edge-triggered pending interrupts. */
3303 PAPICPIB pPib = (PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib);
3304 for (;;)
3305 {
3306 uint32_t const fAlreadySet = apicClearNotificationBitInPib((PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib));
3307 if (!fAlreadySet)
3308 break;
3309
3310 AssertCompile(RT_ELEMENTS(pXApicPage->irr.u) == 2 * RT_ELEMENTS(pPib->au64VectorBitmap));
3311 for (size_t idxPib = 0, idxReg = 0; idxPib < RT_ELEMENTS(pPib->au64VectorBitmap); idxPib++, idxReg += 2)
3312 {
3313 uint64_t const u64Fragment = ASMAtomicXchgU64(&pPib->au64VectorBitmap[idxPib], 0);
3314 if (u64Fragment)
3315 {
3316 uint32_t const u32FragmentLo = RT_LO_U32(u64Fragment);
3317 uint32_t const u32FragmentHi = RT_HI_U32(u64Fragment);
3318
3319 pXApicPage->irr.u[idxReg].u32Reg |= u32FragmentLo;
3320 pXApicPage->irr.u[idxReg + 1].u32Reg |= u32FragmentHi;
3321
3322 pXApicPage->tmr.u[idxReg].u32Reg &= ~u32FragmentLo;
3323 pXApicPage->tmr.u[idxReg + 1].u32Reg &= ~u32FragmentHi;
3324 fHasPendingIntrs = true;
3325 }
3326 }
3327 }
3328
3329 /* Update level-triggered pending interrupts. */
3330 pPib = (PAPICPIB)&pApicCpu->ApicPibLevel;
3331 for (;;)
3332 {
3333 uint32_t const fAlreadySet = apicClearNotificationBitInPib((PAPICPIB)&pApicCpu->ApicPibLevel);
3334 if (!fAlreadySet)
3335 break;
3336
3337 AssertCompile(RT_ELEMENTS(pXApicPage->irr.u) == 2 * RT_ELEMENTS(pPib->au64VectorBitmap));
3338 for (size_t idxPib = 0, idxReg = 0; idxPib < RT_ELEMENTS(pPib->au64VectorBitmap); idxPib++, idxReg += 2)
3339 {
3340 uint64_t const u64Fragment = ASMAtomicXchgU64(&pPib->au64VectorBitmap[idxPib], 0);
3341 if (u64Fragment)
3342 {
3343 uint32_t const u32FragmentLo = RT_LO_U32(u64Fragment);
3344 uint32_t const u32FragmentHi = RT_HI_U32(u64Fragment);
3345
3346 pXApicPage->irr.u[idxReg].u32Reg |= u32FragmentLo;
3347 pXApicPage->irr.u[idxReg + 1].u32Reg |= u32FragmentHi;
3348
3349 pXApicPage->tmr.u[idxReg].u32Reg |= u32FragmentLo;
3350 pXApicPage->tmr.u[idxReg + 1].u32Reg |= u32FragmentHi;
3351 fHasPendingIntrs = true;
3352 }
3353 }
3354 }
3355
3356 STAM_PROFILE_STOP(&pApicCpu->StatUpdatePendingIntrs, a);
3357 Log3(("APIC%u: APICUpdatePendingInterrupts: fHasPendingIntrs=%RTbool\n", pVCpu->idCpu, fHasPendingIntrs));
3358
3359 if ( fHasPendingIntrs
3360 && !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC))
3361 apicSignalNextPendingIntr(pVCpu);
3362}
3363
3364
3365/**
3366 * Gets the highest priority pending interrupt.
3367 *
3368 * @returns true if any interrupt is pending, false otherwise.
3369 * @param pVCpu The cross context virtual CPU structure.
3370 * @param pu8PendingIntr Where to store the interrupt vector if the
3371 * interrupt is pending.
3372 */
3373VMM_INT_DECL(bool) APICGetHighestPendingInterrupt(PVMCPU pVCpu, uint8_t *pu8PendingIntr)
3374{
3375 VMCPU_ASSERT_EMT(pVCpu);
3376 return apicGetHighestPendingInterrupt(pVCpu, pu8PendingIntr);
3377}
3378
3379
3380/**
3381 * Posts an interrupt to a target APIC, Hyper-V interface.
3382 *
3383 * @returns true if the interrupt was accepted, false otherwise.
3384 * @param pVCpu The cross context virtual CPU structure.
3385 * @param uVector The vector of the interrupt to be posted.
3386 * @param fAutoEoi Whether this interrupt has automatic EOI
3387 * treatment.
3388 * @param enmTriggerMode The trigger mode of the interrupt.
3389 *
3390 * @thread Any.
3391 */
3392VMM_INT_DECL(void) APICHvSendInterrupt(PVMCPU pVCpu, uint8_t uVector, bool fAutoEoi, XAPICTRIGGERMODE enmTriggerMode)
3393{
3394 Assert(pVCpu);
3395 Assert(!fAutoEoi); /** @todo AutoEOI. */
3396 RT_NOREF(fAutoEoi);
3397 apicPostInterrupt(pVCpu, uVector, enmTriggerMode, 0 /* uSrcTag */);
3398}
3399
3400
3401/**
3402 * Sets the Task Priority Register (TPR), Hyper-V interface.
3403 *
3404 * @returns Strict VBox status code.
3405 * @param pVCpu The cross context virtual CPU structure.
3406 * @param uTpr The TPR value to set.
3407 *
3408 * @remarks Validates like in x2APIC mode.
3409 */
3410VMM_INT_DECL(VBOXSTRICTRC) APICHvSetTpr(PVMCPU pVCpu, uint8_t uTpr)
3411{
3412 Assert(pVCpu);
3413 VMCPU_ASSERT_EMT(pVCpu);
3414 return apicSetTprEx(pVCpu, uTpr, true /* fForceX2ApicBehaviour */);
3415}
3416
3417
3418/**
3419 * Gets the Task Priority Register (TPR), Hyper-V interface.
3420 *
3421 * @returns The TPR value.
3422 * @param pVCpu The cross context virtual CPU structure.
3423 */
3424VMM_INT_DECL(uint8_t) APICHvGetTpr(PVMCPU pVCpu)
3425{
3426 Assert(pVCpu);
3427 VMCPU_ASSERT_EMT(pVCpu);
3428
3429 /*
3430 * The APIC could be operating in xAPIC mode and thus we should not use the apicReadMsr()
3431 * interface which validates the APIC mode and will throw a #GP(0) if not in x2APIC mode.
3432 * We could use the apicReadRegister() MMIO interface, but why bother getting the PDMDEVINS
3433 * pointer, so just directly read the APIC page.
3434 */
3435 PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
3436 return apicReadRaw32(pXApicPage, XAPIC_OFF_TPR);
3437}
3438
3439
3440/**
3441 * Sets the Interrupt Command Register (ICR), Hyper-V interface.
3442 *
3443 * @returns Strict VBox status code.
3444 * @param pVCpu The cross context virtual CPU structure.
3445 * @param uIcr The ICR value to set.
3446 */
3447VMM_INT_DECL(VBOXSTRICTRC) APICHvSetIcr(PVMCPU pVCpu, uint64_t uIcr)
3448{
3449 Assert(pVCpu);
3450 VMCPU_ASSERT_EMT(pVCpu);
3451 return apicSetIcr(pVCpu, uIcr, VINF_CPUM_R3_MSR_WRITE);
3452}
3453
3454
3455/**
3456 * Gets the Interrupt Command Register (ICR), Hyper-V interface.
3457 *
3458 * @returns The ICR value.
3459 * @param pVCpu The cross context virtual CPU structure.
3460 */
3461VMM_INT_DECL(uint64_t) APICHvGetIcr(PVMCPU pVCpu)
3462{
3463 Assert(pVCpu);
3464 VMCPU_ASSERT_EMT(pVCpu);
3465 return apicGetIcrNoCheck(pVCpu);
3466}
3467
3468
3469/**
3470 * Sets the End-Of-Interrupt (EOI) register, Hyper-V interface.
3471 *
3472 * @returns Strict VBox status code.
3473 * @param pVCpu The cross context virtual CPU structure.
3474 * @param uEoi The EOI value.
3475 */
3476VMM_INT_DECL(VBOXSTRICTRC) APICHvSetEoi(PVMCPU pVCpu, uint32_t uEoi)
3477{
3478 Assert(pVCpu);
3479 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
3480 return apicSetEoi(pVCpu, uEoi, VINF_CPUM_R3_MSR_WRITE, true /* fForceX2ApicBehaviour */);
3481}
3482
3483
3484/**
3485 * Gets the APIC page pointers for the specified VCPU.
3486 *
3487 * @returns VBox status code.
3488 * @param pVCpu The cross context virtual CPU structure.
3489 * @param pHCPhys Where to store the host-context physical address.
3490 * @param pR0Ptr Where to store the ring-0 address.
3491 * @param pR3Ptr Where to store the ring-3 address (optional).
3492 * @param pRCPtr Where to store the raw-mode context address
3493 * (optional).
3494 */
3495VMM_INT_DECL(int) APICGetApicPageForCpu(PVMCPU pVCpu, PRTHCPHYS pHCPhys, PRTR0PTR pR0Ptr, PRTR3PTR pR3Ptr, PRTRCPTR pRCPtr)
3496{
3497 AssertReturn(pVCpu, VERR_INVALID_PARAMETER);
3498 AssertReturn(pHCPhys, VERR_INVALID_PARAMETER);
3499 AssertReturn(pR0Ptr, VERR_INVALID_PARAMETER);
3500
3501 Assert(PDMHasApic(pVCpu->CTX_SUFF(pVM)));
3502
3503 PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
3504 *pHCPhys = pApicCpu->HCPhysApicPage;
3505 *pR0Ptr = pApicCpu->pvApicPageR0;
3506 if (pR3Ptr)
3507 *pR3Ptr = pApicCpu->pvApicPageR3;
3508 if (pRCPtr)
3509 *pRCPtr = pApicCpu->pvApicPageRC;
3510 return VINF_SUCCESS;
3511}
3512
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette