VirtualBox

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

Last change on this file since 64655 was 64655, checked in by vboxsync, 8 years ago

VMM,recompiler: Get rid of PDM APIC interfaces reducing one level of indirection, cleaned up some unused stuff in recompiler.

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