VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/GICAll.cpp@ 107056

Last change on this file since 107056 was 107056, checked in by vboxsync, 6 months ago

VMM/GIC: Fix hack so the value for the priority matches the written register, bugref:10404

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.5 KB
Line 
1/* $Id: GICAll.cpp 107056 2024-11-20 11:32:10Z vboxsync $ */
2/** @file
3 * GIC - Generic Interrupt Controller Architecture (GICv3) - All Contexts.
4 */
5
6/*
7 * Copyright (C) 2023-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV_APIC
33#include "GICInternal.h"
34#include <VBox/vmm/gic.h>
35#include <VBox/vmm/pdmdev.h>
36#include <VBox/vmm/pdmapi.h>
37#include <VBox/vmm/vmcc.h>
38#include <VBox/vmm/vmm.h>
39#include <VBox/vmm/vmcpuset.h>
40#ifdef IN_RING0
41# include <VBox/vmm/gvmm.h>
42#endif
43
44
45/*********************************************************************************************************************************
46* Internal Functions *
47*********************************************************************************************************************************/
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53
54#ifdef LOG_ENABLED
55/**
56 * Returns a human readable string of the given exception class.
57 *
58 * @returns Pointer to the string matching the given EC.
59 * @param u32Ec The exception class to return the string for.
60 */
61static const char *gicIccRegisterStringify(uint32_t u32Reg)
62{
63 switch (u32Reg)
64 {
65#define GIC_ICC_REG_CASE(a_Reg) case ARMV8_AARCH64_SYSREG_ ## a_Reg: return #a_Reg
66 GIC_ICC_REG_CASE(ICC_PMR_EL1);
67 GIC_ICC_REG_CASE(ICC_IAR0_EL1);
68 GIC_ICC_REG_CASE(ICC_EOIR0_EL1);
69 GIC_ICC_REG_CASE(ICC_HPPIR0_EL1);
70 GIC_ICC_REG_CASE(ICC_BPR0_EL1);
71 GIC_ICC_REG_CASE(ICC_AP0R0_EL1);
72 GIC_ICC_REG_CASE(ICC_AP0R1_EL1);
73 GIC_ICC_REG_CASE(ICC_AP0R2_EL1);
74 GIC_ICC_REG_CASE(ICC_AP0R3_EL1);
75 GIC_ICC_REG_CASE(ICC_AP1R0_EL1);
76 GIC_ICC_REG_CASE(ICC_AP1R1_EL1);
77 GIC_ICC_REG_CASE(ICC_AP1R2_EL1);
78 GIC_ICC_REG_CASE(ICC_AP1R3_EL1);
79 GIC_ICC_REG_CASE(ICC_DIR_EL1);
80 GIC_ICC_REG_CASE(ICC_RPR_EL1);
81 GIC_ICC_REG_CASE(ICC_SGI1R_EL1);
82 GIC_ICC_REG_CASE(ICC_ASGI1R_EL1);
83 GIC_ICC_REG_CASE(ICC_SGI0R_EL1);
84 GIC_ICC_REG_CASE(ICC_IAR1_EL1);
85 GIC_ICC_REG_CASE(ICC_EOIR1_EL1);
86 GIC_ICC_REG_CASE(ICC_HPPIR1_EL1);
87 GIC_ICC_REG_CASE(ICC_BPR1_EL1);
88 GIC_ICC_REG_CASE(ICC_CTLR_EL1);
89 GIC_ICC_REG_CASE(ICC_SRE_EL1);
90 GIC_ICC_REG_CASE(ICC_IGRPEN0_EL1);
91 GIC_ICC_REG_CASE(ICC_IGRPEN1_EL1);
92#undef GIC_ICC_REG_CASE
93 default:
94 break;
95 }
96
97 return "<UNKNOWN>";
98}
99#endif
100
101
102/**
103 * Sets the interrupt pending force-flag and pokes the EMT if required.
104 *
105 * @param pVCpu The cross context virtual CPU structure.
106 * @param fIrq Flag whether to assert the IRQ line or leave it alone.
107 * @param fFiq Flag whether to assert the FIQ line or leave it alone.
108 */
109static void gicSetInterruptFF(PVMCPUCC pVCpu, bool fIrq, bool fFiq)
110{
111 LogFlowFunc(("pVCpu=%p{.idCpu=%u} fIrq=%RTbool fFiq=%RTbool\n",
112 pVCpu, pVCpu->idCpu, fIrq, fFiq));
113
114 Assert(fIrq || fFiq);
115
116#ifdef IN_RING3
117 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
118 Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
119#endif
120
121 if (fIrq)
122 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
123 if (fFiq)
124 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
125
126 /*
127 * We need to wake up the target CPU if we're not on EMT.
128 */
129 /** @todo We could just use RTThreadNativeSelf() here, couldn't we? */
130#if defined(IN_RING0)
131 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
132 VMCPUID idCpu = pVCpu->idCpu;
133 if (VMMGetCpuId(pVM) != idCpu)
134 {
135 switch (VMCPU_GET_STATE(pVCpu))
136 {
137 case VMCPUSTATE_STARTED_EXEC:
138 Log7Func(("idCpu=%u VMCPUSTATE_STARTED_EXEC\n", idCpu));
139 GVMMR0SchedPokeNoGVMNoLock(pVM, idCpu);
140 break;
141
142 case VMCPUSTATE_STARTED_HALTED:
143 Log7Func(("idCpu=%u VMCPUSTATE_STARTED_HALTED\n", idCpu));
144 GVMMR0SchedWakeUpNoGVMNoLock(pVM, idCpu);
145 break;
146
147 default:
148 Log7Func(("idCpu=%u enmState=%d\n", idCpu, pVCpu->enmState));
149 break; /* nothing to do in other states. */
150 }
151 }
152#elif defined(IN_RING3)
153 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
154 VMCPUID idCpu = pVCpu->idCpu;
155 if (VMMGetCpuId(pVM) != idCpu)
156 {
157 Log7Func(("idCpu=%u enmState=%d\n", idCpu, pVCpu->enmState));
158 VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_POKE);
159 }
160#endif
161}
162
163
164/**
165 * Clears the interrupt pending force-flag.
166 *
167 * @param pVCpu The cross context virtual CPU structure.
168 * @param fIrq Flag whether to clear the IRQ flag.
169 * @param fFiq Flag whether to clear the FIQ flag.
170 */
171DECLINLINE(void) gicClearInterruptFF(PVMCPUCC pVCpu, bool fIrq, bool fFiq)
172{
173 LogFlowFunc(("pVCpu=%p{.idCpu=%u} fIrq=%RTbool fFiq=%RTbool\n",
174 pVCpu, pVCpu->idCpu, fIrq, fFiq));
175
176 Assert(fIrq || fFiq);
177
178#ifdef IN_RING3
179 /* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
180 Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
181#endif
182
183 if (fIrq)
184 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_IRQ);
185 if (fFiq)
186 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_FIQ);
187}
188
189
190DECLINLINE(void) gicUpdateInterruptFF(PVMCPUCC pVCpu, bool fIrq, bool fFiq)
191{
192 LogFlowFunc(("pVCpu=%p{.idCpu=%u} fIrq=%RTbool fFiq=%RTbool\n",
193 pVCpu, pVCpu->idCpu, fIrq, fFiq));
194
195 if (fIrq || fFiq)
196 gicSetInterruptFF(pVCpu, fIrq, fFiq);
197
198 if (!fIrq || !fFiq)
199 gicClearInterruptFF(pVCpu, !fIrq, !fFiq);
200}
201
202
203DECLINLINE(void) gicReDistHasIrqPending(PGICCPU pThis, bool *pfIrq, bool *pfFiq)
204{
205 /* Read the interrupt state. */
206 uint32_t u32RegIGrp0 = ASMAtomicReadU32(&pThis->u32RegIGrp0);
207 uint32_t bmIntEnabled = ASMAtomicReadU32(&pThis->bmIntEnabled);
208 uint32_t bmIntPending = ASMAtomicReadU32(&pThis->bmIntPending);
209 uint32_t bmIntActive = ASMAtomicReadU32(&pThis->bmIntActive);
210 bool fIrqGrp0Enabled = ASMAtomicReadBool(&pThis->fIrqGrp0Enabled);
211 bool fIrqGrp1Enabled = ASMAtomicReadBool(&pThis->fIrqGrp1Enabled);
212
213 /* Only allow interrupts with higher priority than the current configured and running one. */
214 uint8_t bPriority = RT_MIN(pThis->bInterruptPriority, pThis->abRunningPriorities[pThis->idxRunningPriority]);
215
216 /* Is anything enabled at all? */
217 uint32_t bmIntForward = (bmIntPending & bmIntEnabled) & ~bmIntActive; /* Exclude the currently active interrupt. */
218 if (bmIntForward)
219 {
220 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->abIntPriority); i++)
221 {
222 Log4(("SGI/PPI %u, configured priority %u, running priority %u\n", i, pThis->abIntPriority[i], bPriority));
223 if ( (bmIntForward & RT_BIT_32(i))
224 && pThis->abIntPriority[i] < bPriority)
225 break;
226 else
227 bmIntForward &= ~RT_BIT_32(i);
228
229 if (!bmIntForward)
230 break;
231 }
232 }
233
234 if (bmIntForward)
235 {
236 /* Determine whether we have to assert the IRQ or FIQ line. */
237 *pfIrq = RT_BOOL(bmIntForward & u32RegIGrp0) && fIrqGrp1Enabled;
238 *pfFiq = RT_BOOL(bmIntForward & ~u32RegIGrp0) && fIrqGrp0Enabled;
239 }
240 else
241 {
242 *pfIrq = false;
243 *pfFiq = false;
244 }
245
246 LogFlowFunc(("pThis=%p bPriority=%u bmIntEnabled=%#x bmIntPending=%#x bmIntActive=%#x fIrq=%RTbool fFiq=%RTbool\n",
247 pThis, bPriority, bmIntEnabled, bmIntPending, bmIntActive, *pfIrq, *pfFiq));
248}
249
250
251DECLINLINE(void) gicDistHasIrqPendingForVCpu(PGICDEV pThis, PGICCPU pGicVCpu, VMCPUID idCpu, bool *pfIrq, bool *pfFiq)
252{
253 /* Read the interrupt state. */
254 uint32_t u32RegIGrp0 = ASMAtomicReadU32(&pThis->u32RegIGrp0);
255 uint32_t bmIntEnabled = ASMAtomicReadU32(&pThis->bmIntEnabled);
256 uint32_t bmIntPending = ASMAtomicReadU32(&pThis->bmIntPending);
257 uint32_t bmIntActive = ASMAtomicReadU32(&pThis->bmIntActive);
258 bool fIrqGrp0Enabled = ASMAtomicReadBool(&pThis->fIrqGrp0Enabled);
259 bool fIrqGrp1Enabled = ASMAtomicReadBool(&pThis->fIrqGrp1Enabled);
260
261 /* Only allow interrupts with higher priority than the current configured and running one. */
262 uint8_t bPriority = RT_MIN(pGicVCpu->bInterruptPriority, pGicVCpu->abRunningPriorities[pGicVCpu->idxRunningPriority]);
263
264 /* Is anything enabled at all? */
265 uint32_t bmIntForward = (bmIntPending & bmIntEnabled) & ~bmIntActive; /* Exclude the currently active interrupt. */
266 if (bmIntForward)
267 {
268 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->abIntPriority); i++)
269 {
270 Log4(("SPI %u, configured priority %u (routing %#x), running priority %u\n", i + GIC_INTID_RANGE_SPI_START, pThis->abIntPriority[i],
271 pThis->au32IntRouting[i], bPriority));
272 if ( (bmIntForward & RT_BIT_32(i))
273 && pThis->abIntPriority[i] < bPriority
274 && pThis->au32IntRouting[i] == idCpu)
275 break;
276 else
277 bmIntForward &= ~RT_BIT_32(i);
278
279 if (!bmIntForward)
280 break;
281 }
282 }
283
284 if (bmIntForward)
285 {
286 /* Determine whether we have to assert the IRQ or FIQ line. */
287 *pfIrq = RT_BOOL(bmIntForward & u32RegIGrp0) && fIrqGrp1Enabled;
288 *pfFiq = RT_BOOL(bmIntForward & ~u32RegIGrp0) && fIrqGrp0Enabled;
289 }
290 else
291 {
292 *pfIrq = false;
293 *pfFiq = false;
294 }
295
296 LogFlowFunc(("pThis=%p bPriority=%u bmIntEnabled=%#x bmIntPending=%#x bmIntActive=%#x fIrq=%RTbool fFiq=%RTbool\n",
297 pThis, bPriority, bmIntEnabled, bmIntPending, bmIntActive, *pfIrq, *pfFiq));
298}
299
300
301/**
302 * Updates the internal IRQ state and sets or clears the appropirate force action flags.
303 *
304 * @returns Strict VBox status code.
305 * @param pThis The GIC re-distributor state for the associated vCPU.
306 * @param pVCpu The cross context virtual CPU structure.
307 */
308static VBOXSTRICTRC gicReDistUpdateIrqState(PGICCPU pThis, PVMCPUCC pVCpu)
309{
310 bool fIrq, fFiq;
311 gicReDistHasIrqPending(pThis, &fIrq, &fFiq);
312
313 PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
314 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
315 bool fIrqDist, fFiqDist;
316 gicDistHasIrqPendingForVCpu(pGicDev, pThis, pVCpu->idCpu, &fIrqDist, &fFiqDist);
317 fIrq |= fIrqDist;
318 fFiq |= fFiqDist;
319
320 gicUpdateInterruptFF(pVCpu, fIrq, fFiq);
321 return VINF_SUCCESS;
322}
323
324
325/**
326 * Updates the internal IRQ state of the distributor and sets or clears the appropirate force action flags.
327 *
328 * @returns Strict VBox status code.
329 * @param pVM The cross context VM state.
330 * @param pThis The GIC distributor state.
331 */
332static VBOXSTRICTRC gicDistUpdateIrqState(PVMCC pVM, PGICDEV pThis)
333{
334 for (uint32_t i = 0; i < pVM->cCpus; i++)
335 {
336 PVMCPUCC pVCpu = pVM->CTX_SUFF(apCpus)[i];
337 PGICCPU pGicVCpu = VMCPU_TO_GICCPU(pVCpu);
338
339 bool fIrq, fFiq;
340 gicReDistHasIrqPending(pGicVCpu, &fIrq, &fFiq);
341
342 bool fIrqDist, fFiqDist;
343 gicDistHasIrqPendingForVCpu(pThis, pGicVCpu, i, &fIrqDist, &fFiqDist);
344 fIrq |= fIrqDist;
345 fFiq |= fFiqDist;
346
347 gicUpdateInterruptFF(pVCpu, fIrq, fFiq);
348 }
349 return VINF_SUCCESS;
350}
351
352
353/**
354 * Sets the given SGI/PPI interrupt ID on the re-distributor of the given vCPU.
355 *
356 * @returns VBox status code.
357 * @param pVCpu The cross context virtual CPU structure.
358 * @param uIntId The SGI/PPI interrupt identifier.
359 * @param fAsserted Flag whether the SGI/PPI interrupt is asserted or not.
360 */
361static int gicReDistInterruptSet(PVMCPUCC pVCpu, uint32_t uIntId, bool fAsserted)
362{
363 PGICCPU pThis = VMCPU_TO_GICCPU(pVCpu);
364
365 /* Update the interrupts pending state. */
366 if (fAsserted)
367 ASMAtomicOrU32(&pThis->bmIntPending, RT_BIT_32(uIntId));
368 else
369 ASMAtomicAndU32(&pThis->bmIntPending, ~RT_BIT_32(uIntId));
370
371 return VBOXSTRICTRC_VAL(gicReDistUpdateIrqState(pThis, pVCpu));
372}
373
374
375/**
376 * Reads a GIC distributor register.
377 *
378 * @returns VBox status code.
379 * @param pDevIns The device instance.
380 * @param pVCpu The cross context virtual CPU structure.
381 * @param offReg The offset of the register being read.
382 * @param puValue Where to store the register value.
383 */
384DECLINLINE(VBOXSTRICTRC) gicDistRegisterRead(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t *puValue)
385{
386 VMCPU_ASSERT_EMT(pVCpu);
387 PGICDEV pThis = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
388
389 if (offReg >= GIC_DIST_REG_IROUTERn_OFF_START && offReg <= GIC_DIST_REG_IROUTERn_OFF_LAST)
390 {
391 *puValue = pThis->au32IntRouting[(offReg - GIC_DIST_REG_IROUTERn_OFF_START) / 4];
392 return VINF_SUCCESS;
393 }
394
395 switch (offReg)
396 {
397 case GIC_DIST_REG_CTLR_OFF:
398 *puValue = (ASMAtomicReadBool(&pThis->fIrqGrp0Enabled) ? GIC_DIST_REG_CTRL_ENABLE_GRP0 : 0)
399 | (ASMAtomicReadBool(&pThis->fIrqGrp1Enabled) ? GIC_DIST_REG_CTRL_ENABLE_GRP1_NS : 0)
400 | GIC_DIST_REG_CTRL_DS
401 | GIC_DIST_REG_CTRL_ARE_S;
402 break;
403 case GIC_DIST_REG_TYPER_OFF:
404 *puValue = GIC_DIST_REG_TYPER_NUM_ITLINES_SET(1) /** @todo 32 SPIs for now. */
405 | GIC_DIST_REG_TYPER_NUM_PES_SET(0) /* 1 PE */
406 /*| GIC_DIST_REG_TYPER_ESPI*/ /** @todo */
407 /*| GIC_DIST_REG_TYPER_NMI*/ /** @todo Non-maskable interrupts */
408 /*| GIC_DIST_REG_TYPER_SECURITY_EXTN */ /** @todo */
409 /*| GIC_DIST_REG_TYPER_MBIS */ /** @todo Message based interrupts */
410 /*| GIC_DIST_REG_TYPER_LPIS */ /** @todo Support LPIs */
411 | GIC_DIST_REG_TYPER_IDBITS_SET(16);
412 break;
413 case GIC_DIST_REG_STATUSR_OFF:
414 AssertReleaseFailed();
415 break;
416 case GIC_DIST_REG_IGROUPRn_OFF_START: /* Only 32 lines for now. */
417 AssertReleaseFailed();
418 break;
419 case GIC_DIST_REG_ISENABLERn_OFF_START + 4: /* Only 32 lines for now. */
420 case GIC_DIST_REG_ICENABLERn_OFF_START + 4: /* Only 32 lines for now. */
421 *puValue = ASMAtomicReadU32(&pThis->bmIntEnabled);
422 break;
423 case GIC_DIST_REG_ISPENDRn_OFF_START: /* Only 32 lines for now. */
424 AssertReleaseFailed();
425 break;
426 case GIC_DIST_REG_ICPENDRn_OFF_START: /* Only 32 lines for now. */
427 AssertReleaseFailed();
428 break;
429 case GIC_DIST_REG_ISACTIVERn_OFF_START: /* Only 32 lines for now. */
430 AssertReleaseFailed();
431 break;
432 case GIC_DIST_REG_ICACTIVERn_OFF_START: /* Only 32 lines for now. */
433 AssertReleaseFailed();
434 break;
435 case GIC_DIST_REG_IPRIORITYn_OFF_START:
436 case GIC_DIST_REG_IPRIORITYn_OFF_START + 4: /* These are banked for the PEs and access the redistributor. */
437 {
438 PGICCPU pGicVCpu = VMCPU_TO_GICCPU(pVCpu);
439
440 /* Figure out the register which is written. */
441 uint8_t idxPrio = offReg - GIC_DIST_REG_IPRIORITYn_OFF_START;
442 Assert(idxPrio <= RT_ELEMENTS(pThis->abIntPriority) - sizeof(uint32_t));
443
444 uint32_t u32Value = 0;
445 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
446 u32Value |= pGicVCpu->abIntPriority[i] << ((i - idxPrio) * 8);
447
448 *puValue = u32Value;
449 break;
450 }
451 case GIC_DIST_REG_IPRIORITYn_OFF_START + 32: /* Only 32 lines for now. */
452 {
453 /* Figure out the register which is written. */
454 uint8_t idxPrio = offReg - GIC_DIST_REG_IPRIORITYn_OFF_START - 32;
455 Assert(idxPrio <= RT_ELEMENTS(pThis->abIntPriority) - sizeof(uint32_t));
456
457 uint32_t u32Value = 0;
458 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
459 u32Value |= pThis->abIntPriority[i] << ((i - idxPrio) * 8);
460
461 *puValue = u32Value;
462 break;
463 }
464 case GIC_DIST_REG_ITARGETSRn_OFF_START: /* Only 32 lines for now. */
465 AssertReleaseFailed();
466 break;
467 case GIC_DIST_REG_ICFGRn_OFF_START: /* Only 32 lines for now. */
468 AssertReleaseFailed();
469 break;
470 case GIC_DIST_REG_IGRPMODRn_OFF_START: /* Only 32 lines for now. */
471 AssertReleaseFailed();
472 break;
473 case GIC_DIST_REG_NSACRn_OFF_START: /* Only 32 lines for now. */
474 AssertReleaseFailed();
475 break;
476 case GIC_DIST_REG_SGIR_OFF:
477 AssertReleaseFailed();
478 break;
479 case GIC_DIST_REG_CPENDSGIRn_OFF_START:
480 AssertReleaseFailed();
481 break;
482 case GIC_DIST_REG_SPENDSGIRn_OFF_START:
483 AssertReleaseFailed();
484 break;
485 case GIC_DIST_REG_INMIn_OFF_START:
486 AssertReleaseFailed();
487 break;
488 case GIC_DIST_REG_PIDR2_OFF:
489 *puValue = GIC_REDIST_REG_PIDR2_ARCH_REV_SET(GIC_REDIST_REG_PIDR2_ARCH_REV_GICV3);
490 break;
491 case GIC_DIST_REG_IIDR_OFF:
492 case GIC_DIST_REG_TYPER2_OFF:
493 *puValue = 0;
494 break;
495 case GIC_DIST_REG_IROUTERn_OFF_START:
496 AssertFailed();
497 break;
498 default:
499 *puValue = 0;
500 }
501 return VINF_SUCCESS;
502}
503
504
505/**
506 * Writes a GIC distributor register.
507 *
508 * @returns Strict VBox status code.
509 * @param pDevIns The device instance.
510 * @param pVCpu The cross context virtual CPU structure.
511 * @param offReg The offset of the register being written.
512 * @param uValue The register value.
513 */
514DECLINLINE(VBOXSTRICTRC) gicDistRegisterWrite(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t uValue)
515{
516 VMCPU_ASSERT_EMT(pVCpu); RT_NOREF(pVCpu);
517 PGICDEV pThis = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
518 PVMCC pVM = PDMDevHlpGetVM(pDevIns);
519
520 if (offReg >= GIC_DIST_REG_IROUTERn_OFF_START && offReg <= GIC_DIST_REG_IROUTERn_OFF_LAST)
521 {
522 uint32_t idxReg = (offReg - GIC_DIST_REG_IROUTERn_OFF_START) / 4;
523 LogFlowFunc(("GicDist: idxIRouter=%u uValue=%#x\n", idxReg, uValue));
524 if (idxReg < RT_ELEMENTS(pThis->au32IntRouting))
525 pThis->au32IntRouting[idxReg] = uValue;
526 return VINF_SUCCESS;
527 }
528
529 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
530 switch (offReg)
531 {
532 case GIC_DIST_REG_CTLR_OFF:
533 ASMAtomicWriteBool(&pThis->fIrqGrp0Enabled, RT_BOOL(uValue & GIC_DIST_REG_CTRL_ENABLE_GRP0));
534 ASMAtomicWriteBool(&pThis->fIrqGrp1Enabled, RT_BOOL(uValue & GIC_DIST_REG_CTRL_ENABLE_GRP1_NS));
535 Assert(!(uValue & GIC_DIST_REG_CTRL_ARE_NS));
536 rcStrict = gicDistUpdateIrqState(pVM, pThis);
537 break;
538 case GIC_DIST_REG_STATUSR_OFF:
539 AssertReleaseFailed();
540 break;
541 case GIC_DIST_REG_SETSPI_NSR_OFF:
542 AssertReleaseFailed();
543 break;
544 case GIC_DIST_REG_CLRSPI_NSR_OFF:
545 AssertReleaseFailed();
546 break;
547 case GIC_DIST_REG_SETSPI_SR_OFF:
548 AssertReleaseFailed();
549 break;
550 case GIC_DIST_REG_CLRSPI_SR_OFF:
551 AssertReleaseFailed();
552 break;
553 case GIC_DIST_REG_IGROUPRn_OFF_START: /* Only 32 lines for now. */
554 AssertReleaseFailed();
555 break;
556 case GIC_DIST_REG_IGROUPRn_OFF_START + 4: /* Only 32 lines for now. */
557 ASMAtomicOrU32(&pThis->u32RegIGrp0, uValue);
558 rcStrict = gicDistUpdateIrqState(pVM, pThis);
559 break;
560 case GIC_DIST_REG_ISENABLERn_OFF_START + 4: /* Only 32 lines for now. */
561 ASMAtomicOrU32(&pThis->bmIntEnabled, uValue);
562 rcStrict = gicDistUpdateIrqState(pVM, pThis);
563 break;
564 case GIC_DIST_REG_ICENABLERn_OFF_START:
565 AssertReleaseFailed();
566 break;
567 case GIC_DIST_REG_ICENABLERn_OFF_START + 4: /* Only 32 lines for now. */
568 ASMAtomicAndU32(&pThis->bmIntEnabled, ~uValue);
569 rcStrict = gicDistUpdateIrqState(pVM, pThis);
570 break;
571 case GIC_DIST_REG_ISPENDRn_OFF_START: /* Only 32 lines for now. */
572 AssertReleaseFailed();
573 break;
574 case GIC_DIST_REG_ICPENDRn_OFF_START: /* Only 32 lines for now. */
575 AssertReleaseFailed();
576 break;
577 case GIC_DIST_REG_ISACTIVERn_OFF_START: /* Only 32 lines for now. */
578 AssertReleaseFailed();
579 break;
580 case GIC_DIST_REG_ICACTIVERn_OFF_START + 4: /* Only 32 lines for now. */
581 ASMAtomicAndU32(&pThis->bmIntActive, ~uValue);
582 rcStrict = gicDistUpdateIrqState(pVM, pThis);
583 break;
584 case GIC_DIST_REG_IPRIORITYn_OFF_START: /* These are banked for the PEs and access the redistributor. */
585 case GIC_DIST_REG_IPRIORITYn_OFF_START + 4:
586 case GIC_DIST_REG_IPRIORITYn_OFF_START + 8:
587 case GIC_DIST_REG_IPRIORITYn_OFF_START + 12:
588 case GIC_DIST_REG_IPRIORITYn_OFF_START + 16:
589 case GIC_DIST_REG_IPRIORITYn_OFF_START + 20:
590 case GIC_DIST_REG_IPRIORITYn_OFF_START + 24:
591 case GIC_DIST_REG_IPRIORITYn_OFF_START + 28:
592 {
593 PGICCPU pGicVCpu = VMCPU_TO_GICCPU(pVCpu);
594
595 /* Figure out the register which is written. */
596 uint8_t idxPrio = offReg - GIC_DIST_REG_IPRIORITYn_OFF_START;
597 Assert(idxPrio <= RT_ELEMENTS(pGicVCpu->abIntPriority) - sizeof(uint32_t));
598 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
599 {
600 pGicVCpu->abIntPriority[i] = (uint8_t)(uValue & 0xff);
601 uValue >>= 8;
602 }
603 break;
604 }
605 case GIC_DIST_REG_IPRIORITYn_OFF_START + 32: /* Only 32 lines for now. */
606 case GIC_DIST_REG_IPRIORITYn_OFF_START + 36:
607 case GIC_DIST_REG_IPRIORITYn_OFF_START + 40:
608 case GIC_DIST_REG_IPRIORITYn_OFF_START + 44:
609 case GIC_DIST_REG_IPRIORITYn_OFF_START + 48:
610 case GIC_DIST_REG_IPRIORITYn_OFF_START + 52:
611 case GIC_DIST_REG_IPRIORITYn_OFF_START + 56:
612 case GIC_DIST_REG_IPRIORITYn_OFF_START + 60:
613 {
614 /* Figure out the register which is written. */
615 uint8_t idxPrio = offReg - GIC_DIST_REG_IPRIORITYn_OFF_START - 32;
616 Assert(idxPrio <= RT_ELEMENTS(pThis->abIntPriority) - sizeof(uint32_t));
617 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
618 {
619#if 1
620 /** @todo r=aeichner This gross hack prevents Windows from hanging during boot because
621 * it tries to set the interrupt priority for PCI interrupt lines to 0 which will cause an interrupt
622 * storm later on because the lowest interrupt priority Windows seems to use is 32 for the per vCPU
623 * timer.
624 */
625 if ((uValue & 0xff) == 0)
626 {
627 uValue >>= 8;
628 continue;
629 }
630#endif
631 pThis->abIntPriority[i] = (uint8_t)(uValue & 0xff);
632 uValue >>= 8;
633 }
634 break;
635 }
636 case GIC_DIST_REG_ITARGETSRn_OFF_START: /* Only 32 lines for now. */
637 AssertReleaseFailed();
638 break;
639 case GIC_DIST_REG_ICFGRn_OFF_START + 8: /* Only 32 lines for now. */
640 ASMAtomicWriteU32(&pThis->u32RegICfg0, uValue);
641 break;
642 case GIC_DIST_REG_ICFGRn_OFF_START+ 12:
643 ASMAtomicWriteU32(&pThis->u32RegICfg1, uValue);
644 break;
645 case GIC_DIST_REG_IGRPMODRn_OFF_START: /* Only 32 lines for now. */
646 AssertReleaseFailed();
647 break;
648 case GIC_DIST_REG_NSACRn_OFF_START: /* Only 32 lines for now. */
649 AssertReleaseFailed();
650 break;
651 case GIC_DIST_REG_SGIR_OFF:
652 AssertReleaseFailed();
653 break;
654 case GIC_DIST_REG_CPENDSGIRn_OFF_START:
655 AssertReleaseFailed();
656 break;
657 case GIC_DIST_REG_SPENDSGIRn_OFF_START:
658 AssertReleaseFailed();
659 break;
660 case GIC_DIST_REG_INMIn_OFF_START:
661 AssertReleaseFailed();
662 break;
663 default:
664 //AssertReleaseFailed();
665 break;
666 }
667
668 return rcStrict;
669}
670
671
672/**
673 * Reads a GIC redistributor register.
674 *
675 * @returns VBox status code.
676 * @param pDevIns The device instance.
677 * @param pVCpu The cross context virtual CPU structure.
678 * @param idRedist The redistributor ID.
679 * @param offReg The offset of the register being read.
680 * @param puValue Where to store the register value.
681 */
682DECLINLINE(VBOXSTRICTRC) gicReDistRegisterRead(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint32_t idRedist, uint16_t offReg, uint32_t *puValue)
683{
684 RT_NOREF(pDevIns);
685
686 switch (offReg)
687 {
688 case GIC_REDIST_REG_TYPER_OFF:
689 {
690 PVMCC pVM = PDMDevHlpGetVM(pDevIns);
691 *puValue = ((pVCpu->idCpu == pVM->cCpus - 1) ? GIC_REDIST_REG_TYPER_LAST : 0)
692 | GIC_REDIST_REG_TYPER_CPU_NUMBER_SET(idRedist)
693 | GIC_REDIST_REG_TYPER_CMN_LPI_AFF_SET(GIC_REDIST_REG_TYPER_CMN_LPI_AFF_ALL);
694 break;
695 }
696 case GIC_REDIST_REG_TYPER_AFFINITY_OFF:
697 *puValue = idRedist;
698 break;
699 case GIC_REDIST_REG_PIDR2_OFF:
700 *puValue = GIC_REDIST_REG_PIDR2_ARCH_REV_SET(GIC_REDIST_REG_PIDR2_ARCH_REV_GICV3);
701 break;
702 default:
703 *puValue = 0;
704 }
705
706 return VINF_SUCCESS;
707}
708
709
710/**
711 * Reads a GIC redistributor SGI/PPI frame register.
712 *
713 * @returns VBox status code.
714 * @param pDevIns The device instance.
715 * @param pVCpu The cross context virtual CPU structure.
716 * @param offReg The offset of the register being read.
717 * @param puValue Where to store the register value.
718 */
719DECLINLINE(VBOXSTRICTRC) gicReDistSgiPpiRegisterRead(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t *puValue)
720{
721 VMCPU_ASSERT_EMT(pVCpu);
722 RT_NOREF(pDevIns);
723
724 PGICCPU pThis = VMCPU_TO_GICCPU(pVCpu);
725 switch (offReg)
726 {
727 case GIC_REDIST_SGI_PPI_REG_ISENABLER0_OFF:
728 case GIC_REDIST_SGI_PPI_REG_ICENABLER0_OFF:
729 *puValue = ASMAtomicReadU32(&pThis->bmIntEnabled);
730 break;
731 case GIC_REDIST_SGI_PPI_REG_ISPENDR0_OFF:
732 case GIC_REDIST_SGI_PPI_REG_ICPENDR0_OFF:
733 *puValue = ASMAtomicReadU32(&pThis->bmIntPending);
734 break;
735 case GIC_REDIST_SGI_PPI_REG_ISACTIVER0_OFF:
736 case GIC_REDIST_SGI_PPI_REG_ICACTIVER0_OFF:
737 *puValue = ASMAtomicReadU32(&pThis->bmIntActive);
738 break;
739 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START:
740 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 4:
741 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 8:
742 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 12:
743 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 16:
744 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 20:
745 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 24:
746 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 28:
747 {
748 /* Figure out the register which is written. */
749 uint8_t idxPrio = offReg - GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START;
750 Assert(idxPrio <= RT_ELEMENTS(pThis->abIntPriority) - sizeof(uint32_t));
751
752 uint32_t u32Value = 0;
753 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
754 u32Value |= pThis->abIntPriority[i] << ((i - idxPrio) * 8);
755
756 *puValue = u32Value;
757 break;
758 }
759 case GIC_REDIST_SGI_PPI_REG_ICFGR0_OFF:
760 *puValue = ASMAtomicReadU32(&pThis->u32RegICfg0);
761 break;
762 case GIC_REDIST_SGI_PPI_REG_ICFGR1_OFF:
763 *puValue = ASMAtomicReadU32(&pThis->u32RegICfg1);
764 break;
765 default:
766 AssertReleaseFailed();
767 *puValue = 0;
768 }
769
770 return VINF_SUCCESS;
771}
772
773
774/**
775 * Writes a GIC redistributor frame register.
776 *
777 * @returns Strict VBox status code.
778 * @param pDevIns The device instance.
779 * @param pVCpu The cross context virtual CPU structure.
780 * @param offReg The offset of the register being written.
781 * @param uValue The register value.
782 */
783DECLINLINE(VBOXSTRICTRC) gicReDistRegisterWrite(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t uValue)
784{
785 VMCPU_ASSERT_EMT(pVCpu);
786 RT_NOREF(pDevIns, pVCpu, uValue);
787
788 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
789 switch (offReg)
790 {
791 case GIC_REDIST_REG_STATUSR_OFF:
792 AssertReleaseFailed();
793 break;
794 case GIC_REDIST_REG_WAKER_OFF:
795 Assert(uValue == 0);
796 break;
797 case GIC_REDIST_REG_PARTIDR_OFF:
798 AssertReleaseFailed();
799 break;
800 case GIC_REDIST_REG_SETLPIR_OFF:
801 AssertReleaseFailed();
802 break;
803 case GIC_REDIST_REG_CLRLPIR_OFF:
804 AssertReleaseFailed();
805 break;
806 case GIC_REDIST_REG_PROPBASER_OFF:
807 AssertReleaseFailed();
808 break;
809 case GIC_REDIST_REG_PENDBASER_OFF:
810 AssertReleaseFailed();
811 break;
812 case GIC_REDIST_REG_INVLPIR_OFF:
813 AssertReleaseFailed();
814 break;
815 case GIC_REDIST_REG_INVALLR_OFF:
816 AssertReleaseFailed();
817 break;
818 default:
819 AssertReleaseFailed();
820 break;
821 }
822
823 return rcStrict;
824}
825
826
827/**
828 * Writes a GIC redistributor SGI/PPI frame register.
829 *
830 * @returns Strict VBox status code.
831 * @param pDevIns The device instance.
832 * @param pVCpu The cross context virtual CPU structure.
833 * @param offReg The offset of the register being written.
834 * @param uValue The register value.
835 */
836DECLINLINE(VBOXSTRICTRC) gicReDistSgiPpiRegisterWrite(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t uValue)
837{
838 VMCPU_ASSERT_EMT(pVCpu);
839 RT_NOREF(pDevIns);
840
841 PGICCPU pThis = VMCPU_TO_GICCPU(pVCpu);
842 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
843 switch (offReg)
844 {
845 case GIC_REDIST_SGI_PPI_REG_IGROUPR0_OFF:
846 ASMAtomicOrU32(&pThis->u32RegIGrp0, uValue);
847 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
848 break;
849 case GIC_REDIST_SGI_PPI_REG_ISENABLER0_OFF:
850 ASMAtomicOrU32(&pThis->bmIntEnabled, uValue);
851 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
852 break;
853 case GIC_REDIST_SGI_PPI_REG_ICENABLER0_OFF:
854 ASMAtomicAndU32(&pThis->bmIntEnabled, ~uValue);
855 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
856 break;
857 case GIC_REDIST_SGI_PPI_REG_ISPENDR0_OFF:
858 ASMAtomicOrU32(&pThis->bmIntPending, uValue);
859 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
860 break;
861 case GIC_REDIST_SGI_PPI_REG_ICPENDR0_OFF:
862 ASMAtomicAndU32(&pThis->bmIntPending, ~uValue);
863 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
864 break;
865 case GIC_REDIST_SGI_PPI_REG_ISACTIVER0_OFF:
866 ASMAtomicOrU32(&pThis->bmIntActive, uValue);
867 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
868 break;
869 case GIC_REDIST_SGI_PPI_REG_ICACTIVER0_OFF:
870 ASMAtomicAndU32(&pThis->bmIntActive, ~uValue);
871 rcStrict = gicReDistUpdateIrqState(pThis, pVCpu);
872 break;
873 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START:
874 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 4:
875 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 8:
876 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 12:
877 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 16:
878 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 20:
879 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 24:
880 case GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START + 28:
881 {
882 /* Figure out the register whch is written. */
883 uint8_t idxPrio = offReg - GIC_REDIST_SGI_PPI_REG_IPRIORITYn_OFF_START;
884 Assert(idxPrio <= RT_ELEMENTS(pThis->abIntPriority) - sizeof(uint32_t));
885 for (uint32_t i = idxPrio; i < idxPrio + sizeof(uint32_t); i++)
886 {
887 pThis->abIntPriority[i] = (uint8_t)(uValue & 0xff);
888 uValue >>= 8;
889 }
890 break;
891 }
892 case GIC_REDIST_SGI_PPI_REG_ICFGR0_OFF:
893 ASMAtomicWriteU32(&pThis->u32RegICfg0, uValue);
894 break;
895 case GIC_REDIST_SGI_PPI_REG_ICFGR1_OFF:
896 ASMAtomicWriteU32(&pThis->u32RegICfg1, uValue);
897 break;
898 default:
899 //AssertReleaseFailed();
900 break;
901 }
902
903 return rcStrict;
904}
905
906
907/**
908 * Reads a GIC system register.
909 *
910 * @returns Strict VBox status code.
911 * @param pVCpu The cross context virtual CPU structure.
912 * @param u32Reg The system register being read.
913 * @param pu64Value Where to store the read value.
914 */
915VMM_INT_DECL(VBOXSTRICTRC) GICReadSysReg(PVMCPUCC pVCpu, uint32_t u32Reg, uint64_t *pu64Value)
916{
917 /*
918 * Validate.
919 */
920 VMCPU_ASSERT_EMT(pVCpu);
921 Assert(pu64Value);
922
923 *pu64Value = 0;
924 PGICCPU pThis = VMCPU_TO_GICCPU(pVCpu);
925 PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
926 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
927
928 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
929 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
930
931 switch (u32Reg)
932 {
933 case ARMV8_AARCH64_SYSREG_ICC_PMR_EL1:
934 *pu64Value = pThis->bInterruptPriority;
935 break;
936 case ARMV8_AARCH64_SYSREG_ICC_IAR0_EL1:
937 AssertReleaseFailed();
938 break;
939 case ARMV8_AARCH64_SYSREG_ICC_EOIR0_EL1:
940 AssertReleaseFailed();
941 break;
942 case ARMV8_AARCH64_SYSREG_ICC_HPPIR0_EL1:
943 AssertReleaseFailed();
944 break;
945 case ARMV8_AARCH64_SYSREG_ICC_BPR0_EL1:
946 *pu64Value = pThis->bBinaryPointGrp0 & 0x7;
947 break;
948 case ARMV8_AARCH64_SYSREG_ICC_AP0R0_EL1:
949 AssertReleaseFailed();
950 break;
951 case ARMV8_AARCH64_SYSREG_ICC_AP0R1_EL1:
952 AssertReleaseFailed();
953 break;
954 case ARMV8_AARCH64_SYSREG_ICC_AP0R2_EL1:
955 AssertReleaseFailed();
956 break;
957 case ARMV8_AARCH64_SYSREG_ICC_AP0R3_EL1:
958 AssertReleaseFailed();
959 break;
960 case ARMV8_AARCH64_SYSREG_ICC_AP1R0_EL1:
961 AssertReleaseFailed();
962 break;
963 case ARMV8_AARCH64_SYSREG_ICC_AP1R1_EL1:
964 AssertReleaseFailed();
965 break;
966 case ARMV8_AARCH64_SYSREG_ICC_AP1R2_EL1:
967 AssertReleaseFailed();
968 break;
969 case ARMV8_AARCH64_SYSREG_ICC_AP1R3_EL1:
970 AssertReleaseFailed();
971 break;
972 case ARMV8_AARCH64_SYSREG_ICC_NMIAR1_EL1:
973 AssertReleaseFailed();
974 break;
975 case ARMV8_AARCH64_SYSREG_ICC_DIR_EL1:
976 AssertReleaseFailed();
977 break;
978 case ARMV8_AARCH64_SYSREG_ICC_RPR_EL1:
979 *pu64Value = pThis->abRunningPriorities[pThis->idxRunningPriority];
980 break;
981 case ARMV8_AARCH64_SYSREG_ICC_SGI1R_EL1:
982 AssertReleaseFailed();
983 break;
984 case ARMV8_AARCH64_SYSREG_ICC_ASGI1R_EL1:
985 AssertReleaseFailed();
986 break;
987 case ARMV8_AARCH64_SYSREG_ICC_SGI0R_EL1:
988 AssertReleaseFailed();
989 break;
990 case ARMV8_AARCH64_SYSREG_ICC_IAR1_EL1:
991 {
992 /** @todo Figure out the highest priority interrupt. */
993 uint32_t bmIntActive = ASMAtomicReadU32(&pThis->bmIntActive);
994 uint32_t bmIntEnabled = ASMAtomicReadU32(&pThis->bmIntEnabled);
995 uint32_t bmPending = (ASMAtomicReadU32(&pThis->bmIntPending) & bmIntEnabled) & ~bmIntActive;
996 int32_t idxIntPending = ASMBitFirstSet(&bmPending, sizeof(bmPending) * 8);
997 if (idxIntPending > -1)
998 {
999 /* Mark the interrupt as active. */
1000 ASMAtomicOrU32(&pThis->bmIntActive, RT_BIT_32(idxIntPending));
1001 /* Drop priority. */
1002 Assert((uint32_t)idxIntPending < RT_ELEMENTS(pThis->abIntPriority));
1003 Assert(pThis->idxRunningPriority < RT_ELEMENTS(pThis->abRunningPriorities) - 1);
1004
1005 LogFlowFunc(("Dropping interrupt priority from %u -> %u (idxRunningPriority: %u -> %u)\n",
1006 pThis->abRunningPriorities[pThis->idxRunningPriority],
1007 pThis->abIntPriority[idxIntPending],
1008 pThis->idxRunningPriority, pThis->idxRunningPriority + 1));
1009
1010 pThis->abRunningPriorities[++pThis->idxRunningPriority] = pThis->abIntPriority[idxIntPending];
1011
1012 /* Clear edge level interrupts like SGIs as pending. */
1013 if (idxIntPending <= GIC_INTID_RANGE_SGI_LAST)
1014 ASMAtomicBitClear(&pThis->bmIntPending, idxIntPending);
1015 *pu64Value = idxIntPending;
1016 gicReDistUpdateIrqState(pThis, pVCpu);
1017 }
1018 else
1019 {
1020 /** @todo This is wrong as the guest might decide to prioritize PPIs and SPIs differently. */
1021 bmIntActive = ASMAtomicReadU32(&pGicDev->bmIntActive);
1022 bmIntEnabled = ASMAtomicReadU32(&pGicDev->bmIntEnabled);
1023 bmPending = (ASMAtomicReadU32(&pGicDev->bmIntPending) & bmIntEnabled) & ~bmIntActive;
1024 idxIntPending = ASMBitFirstSet(&bmPending, sizeof(bmPending) * 8);
1025 if ( idxIntPending > -1
1026 && pGicDev->abIntPriority[idxIntPending] < pThis->bInterruptPriority)
1027 {
1028 /* Mark the interrupt as active. */
1029 ASMAtomicOrU32(&pGicDev->bmIntActive, RT_BIT_32(idxIntPending));
1030
1031 /* Drop priority. */
1032 Assert((uint32_t)idxIntPending < RT_ELEMENTS(pGicDev->abIntPriority));
1033 Assert(pThis->idxRunningPriority < RT_ELEMENTS(pThis->abRunningPriorities) - 1);
1034
1035 LogFlowFunc(("Dropping interrupt priority from %u -> %u (idxRunningPriority: %u -> %u)\n",
1036 pThis->abRunningPriorities[pThis->idxRunningPriority],
1037 pThis->abIntPriority[idxIntPending],
1038 pThis->idxRunningPriority, pThis->idxRunningPriority + 1));
1039
1040 pThis->abRunningPriorities[++pThis->idxRunningPriority] = pGicDev->abIntPriority[idxIntPending];
1041
1042 *pu64Value = idxIntPending + GIC_INTID_RANGE_SPI_START;
1043 gicReDistUpdateIrqState(pThis, pVCpu);
1044 }
1045 else
1046 *pu64Value = GIC_INTID_RANGE_SPECIAL_NO_INTERRUPT;
1047 }
1048 break;
1049 }
1050 case ARMV8_AARCH64_SYSREG_ICC_EOIR1_EL1:
1051 AssertReleaseFailed();
1052 break;
1053 case ARMV8_AARCH64_SYSREG_ICC_HPPIR1_EL1:
1054 {
1055 /** @todo Figure out the highest priority interrupt. */
1056 uint32_t bmIntActive = ASMAtomicReadU32(&pThis->bmIntActive);
1057 uint32_t bmIntEnabled = ASMAtomicReadU32(&pThis->bmIntEnabled);
1058 uint32_t bmPending = (ASMAtomicReadU32(&pThis->bmIntPending) & bmIntEnabled) & ~bmIntActive;
1059 int32_t idxIntPending = ASMBitFirstSet(&bmPending, sizeof(bmPending) * 8);
1060 if (idxIntPending > -1)
1061 *pu64Value = idxIntPending;
1062 else
1063 {
1064 /** @todo This is wrong as the guest might decide to prioritize PPIs and SPIs differently. */
1065 bmIntActive = ASMAtomicReadU32(&pGicDev->bmIntActive);
1066 bmIntEnabled = ASMAtomicReadU32(&pGicDev->bmIntEnabled);
1067 bmPending = (ASMAtomicReadU32(&pGicDev->bmIntPending) & bmIntEnabled) & ~bmIntActive;
1068 idxIntPending = ASMBitFirstSet(&bmPending, sizeof(bmPending) * 8);
1069 if (idxIntPending > -1)
1070 *pu64Value = idxIntPending + GIC_INTID_RANGE_SPI_START;
1071 else
1072 *pu64Value = GIC_INTID_RANGE_SPECIAL_NO_INTERRUPT;
1073 }
1074 break;
1075 }
1076 case ARMV8_AARCH64_SYSREG_ICC_BPR1_EL1:
1077 *pu64Value = pThis->bBinaryPointGrp1 & 0x7;
1078 break;
1079 case ARMV8_AARCH64_SYSREG_ICC_CTLR_EL1:
1080 *pu64Value = ARMV8_ICC_CTLR_EL1_AARCH64_PMHE
1081 | ARMV8_ICC_CTLR_EL1_AARCH64_PRIBITS_SET(4)
1082 | ARMV8_ICC_CTLR_EL1_AARCH64_IDBITS_SET(ARMV8_ICC_CTLR_EL1_AARCH64_IDBITS_16BITS);
1083 break;
1084 case ARMV8_AARCH64_SYSREG_ICC_SRE_EL1:
1085 AssertReleaseFailed();
1086 break;
1087 case ARMV8_AARCH64_SYSREG_ICC_IGRPEN0_EL1:
1088 *pu64Value = ASMAtomicReadBool(&pThis->fIrqGrp0Enabled) ? ARMV8_ICC_IGRPEN0_EL1_AARCH64_ENABLE : 0;
1089 break;
1090 case ARMV8_AARCH64_SYSREG_ICC_IGRPEN1_EL1:
1091 *pu64Value = ASMAtomicReadBool(&pThis->fIrqGrp1Enabled) ? ARMV8_ICC_IGRPEN1_EL1_AARCH64_ENABLE : 0;
1092 break;
1093 default:
1094 AssertReleaseFailed();
1095 break;
1096 }
1097
1098 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1099
1100 LogFlowFunc(("pVCpu=%p u32Reg=%#x{%s} pu64Value=%RX64\n", pVCpu, u32Reg, gicIccRegisterStringify(u32Reg), *pu64Value));
1101 return VINF_SUCCESS;
1102}
1103
1104
1105/**
1106 * Writes an GIC system register.
1107 *
1108 * @returns Strict VBox status code.
1109 * @param pVCpu The cross context virtual CPU structure.
1110 * @param u32Reg The system register being written (IPRT system register identifier).
1111 * @param u64Value The value to write.
1112 */
1113VMM_INT_DECL(VBOXSTRICTRC) GICWriteSysReg(PVMCPUCC pVCpu, uint32_t u32Reg, uint64_t u64Value)
1114{
1115 /*
1116 * Validate.
1117 */
1118 VMCPU_ASSERT_EMT(pVCpu);
1119 LogFlowFunc(("pVCpu=%p u32Reg=%#x{%s} u64Value=%RX64\n", pVCpu, u32Reg, gicIccRegisterStringify(u32Reg), u64Value));
1120
1121 PGICCPU pThis = VMCPU_TO_GICCPU(pVCpu);
1122 PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
1123 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
1124
1125 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
1126 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
1127
1128 switch (u32Reg)
1129 {
1130 case ARMV8_AARCH64_SYSREG_ICC_PMR_EL1:
1131 LogFlowFunc(("ICC_PMR_EL1: Interrupt priority now %u\n", (uint8_t)u64Value));
1132 ASMAtomicWriteU8(&pThis->bInterruptPriority, (uint8_t)u64Value);
1133 gicReDistUpdateIrqState(pThis, pVCpu);
1134 break;
1135 case ARMV8_AARCH64_SYSREG_ICC_IAR0_EL1:
1136 AssertReleaseFailed();
1137 break;
1138 case ARMV8_AARCH64_SYSREG_ICC_EOIR0_EL1:
1139 AssertReleaseFailed();
1140 break;
1141 case ARMV8_AARCH64_SYSREG_ICC_HPPIR0_EL1:
1142 AssertReleaseFailed();
1143 break;
1144 case ARMV8_AARCH64_SYSREG_ICC_BPR0_EL1:
1145 pThis->bBinaryPointGrp0 = (uint8_t)(u64Value & 0x7);
1146 break;
1147 case ARMV8_AARCH64_SYSREG_ICC_AP0R0_EL1:
1148 /** @todo */
1149 break;
1150 case ARMV8_AARCH64_SYSREG_ICC_AP0R1_EL1:
1151 AssertReleaseFailed();
1152 break;
1153 case ARMV8_AARCH64_SYSREG_ICC_AP0R2_EL1:
1154 AssertReleaseFailed();
1155 break;
1156 case ARMV8_AARCH64_SYSREG_ICC_AP0R3_EL1:
1157 AssertReleaseFailed();
1158 break;
1159 case ARMV8_AARCH64_SYSREG_ICC_AP1R0_EL1:
1160 /** @todo */
1161 break;
1162 case ARMV8_AARCH64_SYSREG_ICC_AP1R1_EL1:
1163 AssertReleaseFailed();
1164 break;
1165 case ARMV8_AARCH64_SYSREG_ICC_AP1R2_EL1:
1166 AssertReleaseFailed();
1167 break;
1168 case ARMV8_AARCH64_SYSREG_ICC_AP1R3_EL1:
1169 AssertReleaseFailed();
1170 break;
1171 case ARMV8_AARCH64_SYSREG_ICC_NMIAR1_EL1:
1172 AssertReleaseFailed();
1173 break;
1174 case ARMV8_AARCH64_SYSREG_ICC_DIR_EL1:
1175 AssertReleaseFailed();
1176 break;
1177 case ARMV8_AARCH64_SYSREG_ICC_RPR_EL1:
1178 AssertReleaseFailed();
1179 break;
1180 case ARMV8_AARCH64_SYSREG_ICC_SGI1R_EL1:
1181 {
1182 uint32_t uIntId = ARMV8_ICC_SGI1R_EL1_AARCH64_INTID_GET(u64Value) - GIC_INTID_RANGE_SGI_START;
1183 if (u64Value & ARMV8_ICC_SGI1R_EL1_AARCH64_IRM)
1184 {
1185 /* Route to all but this vCPU. */
1186 for (uint32_t i = 0; i < pVCpu->pVMR3->cCpus; i++)
1187 {
1188 if (i != pVCpu->idCpu)
1189 {
1190 PVMCPUCC pVCpuDst = VMMGetCpuById(pVCpu->CTX_SUFF(pVM), i);
1191 if (pVCpuDst)
1192 GICSgiSet(pVCpuDst, uIntId, true /*fAsserted*/);
1193 else
1194 AssertFailed();
1195 }
1196 }
1197 }
1198 else
1199 {
1200 /* Examine target list. */
1201 /** @todo Range selector support. */
1202 VMCPUID idCpu = 0;
1203 uint16_t uTgtList = ARMV8_ICC_SGI1R_EL1_AARCH64_TARGET_LIST_GET(u64Value);
1204 /** @todo rewrite using ASMBitFirstSetU16. */
1205 while (uTgtList)
1206 {
1207 if (uTgtList & 0x1)
1208 {
1209 PVMCPUCC pVCpuDst = VMMGetCpuById(pVCpu->CTX_SUFF(pVM), idCpu);
1210 if (pVCpuDst)
1211 GICSgiSet(pVCpuDst, uIntId, true /*fAsserted*/);
1212 else
1213 AssertFailed();
1214 }
1215 uTgtList >>= 1;
1216 idCpu++;
1217 }
1218 }
1219 break;
1220 }
1221 case ARMV8_AARCH64_SYSREG_ICC_ASGI1R_EL1:
1222 AssertReleaseFailed();
1223 break;
1224 case ARMV8_AARCH64_SYSREG_ICC_SGI0R_EL1:
1225 AssertReleaseFailed();
1226 break;
1227 case ARMV8_AARCH64_SYSREG_ICC_IAR1_EL1:
1228 AssertReleaseFailed();
1229 break;
1230 case ARMV8_AARCH64_SYSREG_ICC_EOIR1_EL1:
1231 {
1232 /* Mark the interrupt as not active anymore, though it might still be pending. */
1233 if (u64Value < GIC_INTID_RANGE_SPI_START)
1234 ASMAtomicAndU32(&pThis->bmIntActive, ~RT_BIT_32((uint32_t)u64Value));
1235 else
1236 ASMAtomicAndU32(&pGicDev->bmIntActive, ~RT_BIT_32((uint32_t)(u64Value - GIC_INTID_RANGE_SPI_START)));
1237
1238 /* Restore previous interrupt priority. */
1239 Assert(pThis->idxRunningPriority > 0);
1240 if (RT_LIKELY(pThis->idxRunningPriority))
1241 {
1242 LogFlowFunc(("Restoring interrupt priority from %u -> %u (idxRunningPriority: %u -> %u)\n",
1243 pThis->abRunningPriorities[pThis->idxRunningPriority],
1244 pThis->abRunningPriorities[pThis->idxRunningPriority - 1],
1245 pThis->idxRunningPriority, pThis->idxRunningPriority - 1));
1246 pThis->idxRunningPriority--;
1247 }
1248 gicReDistUpdateIrqState(pThis, pVCpu);
1249 break;
1250 }
1251 case ARMV8_AARCH64_SYSREG_ICC_HPPIR1_EL1:
1252 AssertReleaseFailed();
1253 break;
1254 case ARMV8_AARCH64_SYSREG_ICC_BPR1_EL1:
1255 pThis->bBinaryPointGrp0 = (uint8_t)(u64Value & 0x7);
1256 break;
1257 case ARMV8_AARCH64_SYSREG_ICC_CTLR_EL1:
1258 u64Value &= ARMV8_ICC_CTLR_EL1_RW;
1259 /** @todo */
1260 break;
1261 case ARMV8_AARCH64_SYSREG_ICC_SRE_EL1:
1262 AssertReleaseFailed();
1263 break;
1264 case ARMV8_AARCH64_SYSREG_ICC_IGRPEN0_EL1:
1265 ASMAtomicWriteBool(&pThis->fIrqGrp0Enabled, RT_BOOL(u64Value & ARMV8_ICC_IGRPEN0_EL1_AARCH64_ENABLE));
1266 break;
1267 case ARMV8_AARCH64_SYSREG_ICC_IGRPEN1_EL1:
1268 ASMAtomicWriteBool(&pThis->fIrqGrp1Enabled, RT_BOOL(u64Value & ARMV8_ICC_IGRPEN1_EL1_AARCH64_ENABLE));
1269 break;
1270 default:
1271 AssertReleaseFailed();
1272 break;
1273 }
1274
1275 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1276 return VINF_SUCCESS;
1277}
1278
1279
1280/**
1281 * Sets the specified shared peripheral interrupt starting.
1282 *
1283 * @returns VBox status code.
1284 * @param pVM The cross context virtual machine structure.
1285 * @param uIntId The SPI ID (minus GIC_INTID_RANGE_SPI_START) to assert/de-assert.
1286 * @param fAsserted Flag whether to mark the interrupt as asserted/de-asserted.
1287 */
1288VMM_INT_DECL(int) GICSpiSet(PVMCC pVM, uint32_t uIntId, bool fAsserted)
1289{
1290 LogFlowFunc(("pVM=%p uIntId=%u fAsserted=%RTbool\n",
1291 pVM, uIntId, fAsserted));
1292
1293 AssertReturn(uIntId < GIC_SPI_MAX, VERR_INVALID_PARAMETER);
1294
1295 PGIC pGic = VM_TO_GIC(pVM);
1296
1297 /** @todo r=aeichner There must be another way to do this better, maybe create some callback interface
1298 * the GIC can register. */
1299#ifdef IN_RING3
1300 if (pGic->fNemGic)
1301 return GICR3NemSpiSet(pVM, uIntId, fAsserted);
1302#else
1303# error "Impossible to call the NEM in-kernel GIC from this context!"
1304#endif
1305
1306 PPDMDEVINS pDevIns = pGic->CTX_SUFF(pDevIns);
1307 PGICDEV pThis = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
1308
1309 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
1310 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
1311
1312 /* Update the interrupts pending state. */
1313 if (fAsserted)
1314 ASMAtomicOrU32(&pThis->bmIntPending, RT_BIT_32(uIntId));
1315 else
1316 ASMAtomicAndU32(&pThis->bmIntPending, ~RT_BIT_32(uIntId));
1317
1318 int rc = VBOXSTRICTRC_VAL(gicDistUpdateIrqState(pVM, pThis));
1319 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1320 return rc;
1321}
1322
1323
1324/**
1325 * Sets the specified private peripheral interrupt starting.
1326 *
1327 * @returns VBox status code.
1328 * @param pVCpu The cross context virtual CPU structure.
1329 * @param uIntId The PPI ID (minus GIC_INTID_RANGE_PPI_START) to assert/de-assert.
1330 * @param fAsserted Flag whether to mark the interrupt as asserted/de-asserted.
1331 */
1332VMM_INT_DECL(int) GICPpiSet(PVMCPUCC pVCpu, uint32_t uIntId, bool fAsserted)
1333{
1334 LogFlowFunc(("pVCpu=%p{.idCpu=%u} uIntId=%u fAsserted=%RTbool\n",
1335 pVCpu, pVCpu->idCpu, uIntId, fAsserted));
1336
1337 PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
1338
1339 /** @todo r=aeichner There must be another way to do this better, maybe create some callback interface
1340 * the GIC can register. */
1341#ifdef IN_RING3
1342 PGIC pGic = VM_TO_GIC(pVCpu->pVMR3);
1343 if (pGic->fNemGic)
1344 return GICR3NemPpiSet(pVCpu, uIntId, fAsserted);
1345#else
1346# error "Impossible to call the NEM in-kernel GIC from this context!"
1347#endif
1348
1349 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
1350 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
1351
1352 AssertReturn(uIntId <= (GIC_INTID_RANGE_PPI_LAST - GIC_INTID_RANGE_PPI_START), VERR_INVALID_PARAMETER);
1353 int rc = gicReDistInterruptSet(pVCpu, uIntId + GIC_INTID_RANGE_PPI_START, fAsserted);
1354 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1355
1356 return rc;
1357}
1358
1359
1360/**
1361 * Sets the specified software generated interrupt starting.
1362 *
1363 * @returns VBox status code.
1364 * @param pVCpu The cross context virtual CPU structure.
1365 * @param uIntId The PPI ID (minus GIC_INTID_RANGE_SGI_START) to assert/de-assert.
1366 * @param fAsserted Flag whether to mark the interrupt as asserted/de-asserted.
1367 */
1368VMM_INT_DECL(int) GICSgiSet(PVMCPUCC pVCpu, uint32_t uIntId, bool fAsserted)
1369{
1370 LogFlowFunc(("pVCpu=%p{.idCpu=%u} uIntId=%u fAsserted=%RTbool\n",
1371 pVCpu, pVCpu->idCpu, uIntId, fAsserted));
1372
1373 PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
1374
1375 /** @todo r=aeichner There must be another way to do this better, maybe create some callback interface
1376 * the GIC can register. */
1377#ifdef IN_RING3
1378 PGIC pGic = VM_TO_GIC(pVCpu->pVMR3);
1379 /* These should be handled in the kernel and never be set from here. */
1380 AssertReturn(!pGic->fNemGic, VERR_NEM_IPE_6);
1381#else
1382# error "Impossible to call the in-kernel GIC from this context!"
1383#endif
1384
1385 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
1386 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
1387
1388 AssertReturn(uIntId <= (GIC_INTID_RANGE_SGI_LAST - GIC_INTID_RANGE_SGI_START), VERR_INVALID_PARAMETER);
1389 int rc = gicReDistInterruptSet(pVCpu, uIntId + GIC_INTID_RANGE_SGI_START, fAsserted);
1390 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1391
1392 return rc;
1393}
1394
1395
1396/**
1397 * Initializes per-VCPU GIC to the state following a power-up or hardware
1398 * reset.
1399 *
1400 * @param pVCpu The cross context virtual CPU structure.
1401 */
1402DECLHIDDEN(void) gicResetCpu(PVMCPUCC pVCpu)
1403{
1404 LogFlowFunc(("GIC%u\n", pVCpu->idCpu));
1405 VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
1406
1407 memset((void *)&pVCpu->gic.s.abRunningPriorities[0], 0xff, sizeof(pVCpu->gic.s.abRunningPriorities));
1408 pVCpu->gic.s.idxRunningPriority = 0;
1409 pVCpu->gic.s.bInterruptPriority = 0; /* Means no interrupt gets through to the PE. */
1410}
1411
1412
1413/**
1414 * @callback_method_impl{FNIOMMMIONEWREAD}
1415 */
1416DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicDistMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1417{
1418 NOREF(pvUser);
1419 Assert(!(off & 0x3));
1420 Assert(cb == 4); RT_NOREF_PV(cb);
1421
1422 PVMCPUCC pVCpu = PDMDevHlpGetVMCPU(pDevIns);
1423 uint16_t offReg = off & 0xfffc;
1424 uint32_t uValue = 0;
1425
1426 STAM_COUNTER_INC(&pVCpu->gic.s.CTX_SUFF_Z(StatMmioRead));
1427
1428 VBOXSTRICTRC rc = VBOXSTRICTRC_VAL(gicDistRegisterRead(pDevIns, pVCpu, offReg, &uValue));
1429 *(uint32_t *)pv = uValue;
1430
1431 Log2(("GIC%u: gicDistMmioRead: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
1432 return rc;
1433}
1434
1435
1436/**
1437 * @callback_method_impl{FNIOMMMIONEWWRITE}
1438 */
1439DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicDistMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
1440{
1441 NOREF(pvUser);
1442 Assert(!(off & 0x3));
1443 Assert(cb == 4); RT_NOREF_PV(cb);
1444
1445 PVMCPUCC pVCpu = PDMDevHlpGetVMCPU(pDevIns);
1446 uint16_t offReg = off & 0xfffc;
1447 uint32_t uValue = *(uint32_t *)pv;
1448
1449 STAM_COUNTER_INC(&pVCpu->gic.s.CTX_SUFF_Z(StatMmioWrite));
1450
1451 Log2(("GIC%u: gicDistMmioWrite: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
1452 return gicDistRegisterWrite(pDevIns, pVCpu, offReg, uValue);
1453}
1454
1455
1456/**
1457 * @callback_method_impl{FNIOMMMIONEWREAD}
1458 */
1459DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicReDistMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1460{
1461 NOREF(pvUser);
1462 Assert(!(off & 0x3));
1463 Assert(cb == 4); RT_NOREF_PV(cb);
1464
1465 /*
1466 * Determine the redistributor being targeted. Each redistributor takes GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE bytes
1467 * and the redistributors are adjacent.
1468 */
1469 uint32_t idReDist = off / (GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE);
1470 off %= (GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE);
1471
1472 PVMCC pVM = PDMDevHlpGetVM(pDevIns);
1473 Assert(idReDist < pVM->cCpus);
1474 PVMCPUCC pVCpu = pVM->apCpusR3[idReDist];
1475
1476 STAM_COUNTER_INC(&pVCpu->gic.s.CTX_SUFF_Z(StatMmioRead));
1477
1478 /* Redistributor or SGI/PPI frame? */
1479 uint16_t offReg = off & 0xfffc;
1480 uint32_t uValue = 0;
1481 VBOXSTRICTRC rcStrict;
1482 if (off < GIC_REDIST_REG_FRAME_SIZE)
1483 rcStrict = gicReDistRegisterRead(pDevIns, pVCpu, idReDist, offReg, &uValue);
1484 else
1485 rcStrict = gicReDistSgiPpiRegisterRead(pDevIns, pVCpu, offReg, &uValue);
1486
1487 *(uint32_t *)pv = uValue;
1488 Log2(("GICReDist%u: gicReDistMmioRead: off=%RGp idReDist=%u offReg=%#RX16 uValue=%#RX32 -> %Rrc\n",
1489 pVCpu->idCpu, off, idReDist, offReg, uValue, VBOXSTRICTRC_VAL(rcStrict)));
1490 return rcStrict;
1491}
1492
1493
1494/**
1495 * @callback_method_impl{FNIOMMMIONEWWRITE}
1496 */
1497DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicReDistMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
1498{
1499 NOREF(pvUser);
1500 Assert(!(off & 0x3));
1501 Assert(cb == 4); RT_NOREF_PV(cb);
1502
1503 uint32_t uValue = *(uint32_t *)pv;
1504
1505 /*
1506 * Determine the redistributor being targeted. Each redistributor takes GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE bytes
1507 * and the redistributors are adjacent.
1508 */
1509 uint32_t idReDist = off / (GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE);
1510 off %= (GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE);
1511
1512 PVMCC pVM = PDMDevHlpGetVM(pDevIns);
1513 Assert(idReDist < pVM->cCpus);
1514 PVMCPUCC pVCpu = pVM->apCpusR3[idReDist];
1515
1516 STAM_COUNTER_INC(&pVCpu->gic.s.CTX_SUFF_Z(StatMmioWrite));
1517
1518 /* Redistributor or SGI/PPI frame? */
1519 uint16_t offReg = off & 0xfffc;
1520 VBOXSTRICTRC rcStrict;
1521 if (off < GIC_REDIST_REG_FRAME_SIZE)
1522 rcStrict = gicReDistRegisterWrite(pDevIns, pVCpu, offReg, uValue);
1523 else
1524 rcStrict = gicReDistSgiPpiRegisterWrite(pDevIns, pVCpu, offReg, uValue);
1525
1526 Log2(("GICReDist%u: gicReDistMmioWrite: off=%RGp idReDist=%u offReg=%#RX16 uValue=%#RX32 -> %Rrc\n",
1527 pVCpu->idCpu, off, idReDist, offReg, uValue, VBOXSTRICTRC_VAL(rcStrict)));
1528 return rcStrict;
1529}
1530
1531
1532#ifndef IN_RING3
1533
1534/**
1535 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
1536 */
1537static DECLCALLBACK(int) gicRZConstruct(PPDMDEVINS pDevIns)
1538{
1539 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1540 AssertReleaseFailed();
1541 return VINF_SUCCESS;
1542}
1543#endif /* !IN_RING3 */
1544
1545/**
1546 * GIC device registration structure.
1547 */
1548const PDMDEVREG g_DeviceGIC =
1549{
1550 /* .u32Version = */ PDM_DEVREG_VERSION,
1551 /* .uReserved0 = */ 0,
1552 /* .szName = */ "gic",
1553 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
1554 /* .fClass = */ PDM_DEVREG_CLASS_PIC,
1555 /* .cMaxInstances = */ 1,
1556 /* .uSharedVersion = */ 42,
1557 /* .cbInstanceShared = */ sizeof(GICDEV),
1558 /* .cbInstanceCC = */ 0,
1559 /* .cbInstanceRC = */ 0,
1560 /* .cMaxPciDevices = */ 0,
1561 /* .cMaxMsixVectors = */ 0,
1562 /* .pszDescription = */ "Generic Interrupt Controller",
1563#if defined(IN_RING3)
1564 /* .szRCMod = */ "VMMRC.rc",
1565 /* .szR0Mod = */ "VMMR0.r0",
1566 /* .pfnConstruct = */ gicR3Construct,
1567 /* .pfnDestruct = */ gicR3Destruct,
1568 /* .pfnRelocate = */ gicR3Relocate,
1569 /* .pfnMemSetup = */ NULL,
1570 /* .pfnPowerOn = */ NULL,
1571 /* .pfnReset = */ gicR3Reset,
1572 /* .pfnSuspend = */ NULL,
1573 /* .pfnResume = */ NULL,
1574 /* .pfnAttach = */ NULL,
1575 /* .pfnDetach = */ NULL,
1576 /* .pfnQueryInterface = */ NULL,
1577 /* .pfnInitComplete = */ NULL,
1578 /* .pfnPowerOff = */ NULL,
1579 /* .pfnSoftReset = */ NULL,
1580 /* .pfnReserved0 = */ NULL,
1581 /* .pfnReserved1 = */ NULL,
1582 /* .pfnReserved2 = */ NULL,
1583 /* .pfnReserved3 = */ NULL,
1584 /* .pfnReserved4 = */ NULL,
1585 /* .pfnReserved5 = */ NULL,
1586 /* .pfnReserved6 = */ NULL,
1587 /* .pfnReserved7 = */ NULL,
1588#elif defined(IN_RING0)
1589 /* .pfnEarlyConstruct = */ NULL,
1590 /* .pfnConstruct = */ gicRZConstruct,
1591 /* .pfnDestruct = */ NULL,
1592 /* .pfnFinalDestruct = */ NULL,
1593 /* .pfnRequest = */ NULL,
1594 /* .pfnReserved0 = */ NULL,
1595 /* .pfnReserved1 = */ NULL,
1596 /* .pfnReserved2 = */ NULL,
1597 /* .pfnReserved3 = */ NULL,
1598 /* .pfnReserved4 = */ NULL,
1599 /* .pfnReserved5 = */ NULL,
1600 /* .pfnReserved6 = */ NULL,
1601 /* .pfnReserved7 = */ NULL,
1602#elif defined(IN_RC)
1603 /* .pfnConstruct = */ gicRZConstruct,
1604 /* .pfnReserved0 = */ NULL,
1605 /* .pfnReserved1 = */ NULL,
1606 /* .pfnReserved2 = */ NULL,
1607 /* .pfnReserved3 = */ NULL,
1608 /* .pfnReserved4 = */ NULL,
1609 /* .pfnReserved5 = */ NULL,
1610 /* .pfnReserved6 = */ NULL,
1611 /* .pfnReserved7 = */ NULL,
1612#else
1613# error "Not in IN_RING3, IN_RING0 or IN_RC!"
1614#endif
1615 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
1616};
1617
Note: See TracBrowser for help on using the repository browser.

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