VirtualBox

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

Last change on this file since 82041 was 82041, checked in by vboxsync, 5 years ago

PIC,APIC,IOAPIC,PDM: The PIC, APIC, and IOAPIC cannot have their ring-0 bits disabled or PDM will get interrupt handling all wrong (left todo). bugref:9218

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