VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/apic.c@ 483

Last change on this file since 483 was 483, checked in by vboxsync, 18 years ago

64-bit alignment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.9 KB
Line 
1#ifdef VBOX
2/** @file
3 *
4 * VBox basic PC devices:
5 * Advanced Programmable Interrupt Controller (APIC) device
6 */
7
8/*
9 * Copyright (C) 2006 InnoTek Systemberatung GmbH
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License as published by the Free Software Foundation,
15 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
16 * distribution. VirtualBox OSE is distributed in the hope that it will
17 * be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * If you received this file as part of a commercial VirtualBox
20 * distribution, then only the terms of your commercial VirtualBox
21 * license agreement apply instead of the previous paragraph.
22 *
23 * --------------------------------------------------------------------
24 *
25 * This code is based on:
26 *
27 * apic.c revision 1.5 @@OSETODO
28 */
29
30/*******************************************************************************
31* Header Files *
32*******************************************************************************/
33#define LOG_GROUP LOG_GROUP_DEV_APIC
34#include <VBox/pdm.h>
35
36#include <VBox/log.h>
37#include <VBox/stam.h>
38#include <iprt/assert.h>
39#include <iprt/asm.h>
40
41#include "Builtins2.h"
42#include "vl_vbox.h"
43
44#define MSR_IA32_APICBASE 0x1b
45#define MSR_IA32_APICBASE_BSP (1<<8)
46#define MSR_IA32_APICBASE_ENABLE (1<<11)
47#define MSR_IA32_APICBASE_BASE (0xfffff<<12)
48
49#ifndef EINVAL
50# define EINVAL 1
51#endif
52
53#ifdef _MSC_VER
54# pragma warning(disable:4244)
55#endif
56
57/** @def APIC_LOCK
58 * Acquires the PDM lock. This is a NOP if locking is disabled. */
59/** @def APIC_UNLOCK
60 * Releases the PDM lock. This is a NOP if locking is disabled. */
61/** @def IOAPIC_LOCK
62 * Acquires the PDM lock. This is a NOP if locking is disabled. */
63/** @def IOAPIC_UNLOCK
64 * Releases the PDM lock. This is a NOP if locking is disabled. */
65#ifdef VBOX_WITH_PDM_LOCK
66# define APIC_LOCK(pThis, rc) \
67 do { \
68 int rc2 = (pThis)->CTXALLSUFF(pApicHlp)->pfnLock((pThis)->CTXSUFF(pDevIns), rc); \
69 if (rc2 != VINF_SUCCESS) \
70 return rc2; \
71 } while (0)
72# define APIC_UNLOCK(pThis) \
73 (pThis)->CTXALLSUFF(pApicHlp)->pfnUnlock((pThis)->CTXSUFF(pDevIns))
74# define IOAPIC_LOCK(pThis, rc) \
75 do { \
76 int rc2 = (pThis)->CTXALLSUFF(pIoApicHlp)->pfnLock((pThis)->CTXSUFF(pDevIns), rc); \
77 if (rc2 != VINF_SUCCESS) \
78 return rc2; \
79 } while (0)
80# define IOAPIC_UNLOCK(pThis) (pThis)->CTXALLSUFF(pIoApicHlp)->pfnUnlock((pThis)->CTXSUFF(pDevIns))
81#else /* !VBOX_WITH_PDM_LOCK */
82# define APIC_LOCK(pThis, rc) do { } while (0)
83# define APIC_UNLOCK(pThis) do { } while (0)
84# define IOAPIC_LOCK(pThis, rc) do { } while (0)
85# define IOAPIC_UNLOCK(pThis) do { } while (0)
86#endif /* !VBOX_WITH_PDM_LOCK */
87
88
89#endif /* VBOX */
90
91/*
92 * APIC support
93 *
94 * Copyright (c) 2004-2005 Fabrice Bellard
95 *
96 * This library is free software; you can redistribute it and/or
97 * modify it under the terms of the GNU Lesser General Public
98 * License as published by the Free Software Foundation; either
99 * version 2 of the License, or (at your option) any later version.
100 *
101 * This library is distributed in the hope that it will be useful,
102 * but WITHOUT ANY WARRANTY; without even the implied warranty of
103 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
104 * Lesser General Public License for more details.
105 *
106 * You should have received a copy of the GNU Lesser General Public
107 * License along with this library; if not, write to the Free Software
108 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
109 */
110#ifndef VBOX
111#include "vl.h"
112#endif
113
114#define DEBUG_APIC
115#define DEBUG_IOAPIC
116
117/* APIC Local Vector Table */
118#define APIC_LVT_TIMER 0
119#define APIC_LVT_THERMAL 1
120#define APIC_LVT_PERFORM 2
121#define APIC_LVT_LINT0 3
122#define APIC_LVT_LINT1 4
123#define APIC_LVT_ERROR 5
124#define APIC_LVT_NB 6
125
126/* APIC delivery modes */
127#define APIC_DM_FIXED 0
128#define APIC_DM_LOWPRI 1
129#define APIC_DM_SMI 2
130#define APIC_DM_NMI 4
131#define APIC_DM_INIT 5
132#define APIC_DM_SIPI 6
133#define APIC_DM_EXTINT 7
134
135/* APIC destination mode */
136#define APIC_DESTMODE_FLAT 0xf
137#define APIC_DESTMODE_CLUSTER 1
138
139#define APIC_TRIGGER_EDGE 0
140#define APIC_TRIGGER_LEVEL 1
141
142#define APIC_LVT_TIMER_PERIODIC (1<<17)
143#define APIC_LVT_MASKED (1<<16)
144#define APIC_LVT_LEVEL_TRIGGER (1<<15)
145#define APIC_LVT_REMOTE_IRR (1<<14)
146#define APIC_INPUT_POLARITY (1<<13)
147#define APIC_SEND_PENDING (1<<12)
148
149#define IOAPIC_NUM_PINS 0x18
150
151#define ESR_ILLEGAL_ADDRESS (1 << 7)
152
153#define APIC_SV_ENABLE (1 << 8)
154
155#ifdef VBOX
156#define APIC_MAX_PATCH_ATTEMPTS 100
157#endif
158
159typedef struct APICState {
160#ifndef VBOX
161 CPUState *cpu_env;
162#endif /* !VBOX */
163 uint32_t apicbase;
164 uint8_t id;
165 uint8_t arb_id;
166#ifdef VBOX
167 uint32_t tpr;
168#else
169 uint8_t tpr;
170#endif
171 uint32_t spurious_vec;
172 uint8_t log_dest;
173 uint8_t dest_mode;
174 uint32_t isr[8]; /* in service register */
175 uint32_t tmr[8]; /* trigger mode register */
176 uint32_t irr[8]; /* interrupt request register */
177 uint32_t lvt[APIC_LVT_NB];
178 uint32_t esr; /* error register */
179 uint32_t icr[2];
180
181 uint32_t divide_conf;
182 int count_shift;
183 uint32_t initial_count;
184#ifdef VBOX
185 uint32_t Alignment0;
186#endif
187 int64_t initial_count_load_time, next_time;
188#ifndef VBOX
189 QEMUTimer *timer;
190
191 struct APICState *next_apic;
192#else /* VBOX */
193 /** HC pointer to the device instance. */
194 PPDMDEVINSHC pDevInsHC;
195 /** Pointer to the APIC HC helpers. */
196 PCPDMAPICHLPR3 pApicHlpR3;
197 /** The APIC timer - HC Ptr. */
198 PTMTIMERHC pTimerHC;
199 /** Pointer to the APIC R0 helpers. */
200 PCPDMAPICHLPR0 pApicHlpR0;
201
202 /** GC pointer to the device instance. */
203 PPDMDEVINSGC pDevInsGC;
204 /** Pointer to the APIC GC helpers. */
205 PCPDMAPICHLPGC pApicHlpGC;
206 /** The APIC timer - GC Ptr. */
207 PTMTIMERGC pTimerGC;
208
209 /** Number of attempts made to optimize TPR accesses. */
210 uint32_t ulTPRPatchAttempts;
211
212# ifdef VBOX_WITH_STATISTICS
213 STAMCOUNTER StatMMIOReadGC;
214 STAMCOUNTER StatMMIOReadHC;
215 STAMCOUNTER StatMMIOWriteGC;
216 STAMCOUNTER StatMMIOWriteHC;
217# endif
218#endif /* VBOX */
219} APICState;
220
221struct IOAPICState {
222 uint8_t id;
223 uint8_t ioregsel;
224
225 uint32_t irr;
226 uint64_t ioredtbl[IOAPIC_NUM_PINS];
227
228#ifdef VBOX
229 /** HC pointer to the device instance. */
230 PPDMDEVINSHC pDevInsHC;
231 /** Pointer to the IOAPIC R3 helpers. */
232 PCPDMIOAPICHLPR3 pIoApicHlpR3;
233
234 /** GC pointer to the device instance. */
235 PPDMDEVINSGC pDevInsGC;
236 /** Pointer to the IOAPIC GC helpers. */
237 PCPDMIOAPICHLPGC pIoApicHlpGC;
238
239 /** Pointer to the IOAPIC R0 helpers. */
240 PCPDMIOAPICHLPR0 pIoApicHlpR0;
241# if HC_ARCH_BITS == 32
242 uint32_t Alignment0;
243# endif
244# ifdef VBOX_WITH_STATISTICS
245 STAMCOUNTER StatMMIOReadGC;
246 STAMCOUNTER StatMMIOReadHC;
247 STAMCOUNTER StatMMIOWriteGC;
248 STAMCOUNTER StatMMIOWriteHC;
249 STAMCOUNTER StatSetIrqGC;
250 STAMCOUNTER StatSetIrqHC;
251# endif
252#endif /* VBOX */
253};
254
255#ifdef VBOX
256typedef struct IOAPICState IOAPICState;
257#endif /* VBOX */
258
259#ifndef VBOX_DEVICE_STRUCT_TESTCASE
260#ifndef VBOX
261static int apic_io_memory;
262static APICState *first_local_apic = NULL;
263static int last_apic_id = 0;
264#endif /* !VBOX */
265
266static void apic_init_ipi(APICState *s);
267static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
268static void apic_update_irq(APICState *s);
269
270#ifdef VBOX
271static uint32_t apic_get_delivery_bitmask(APICState *s, uint8_t dest, uint8_t dest_mode);
272__BEGIN_DECLS
273PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
274PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
275PDMBOTHCBDECL(int) apicGetInterrupt(PPDMDEVINS pDevIns);
276PDMBOTHCBDECL(void) apicSetBase(PPDMDEVINS pDevIns, uint64_t val);
277PDMBOTHCBDECL(uint64_t) apicGetBase(PPDMDEVINS pDevIns);
278PDMBOTHCBDECL(void) apicSetTPR(PPDMDEVINS pDevIns, uint8_t val);
279PDMBOTHCBDECL(uint8_t) apicGetTPR(PPDMDEVINS pDevIns);
280PDMBOTHCBDECL(void) apicBusDeliverCallback(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
281 uint8_t u8DeliveryMode, uint8_t iVector, uint8_t u8Polarity,
282 uint8_t u8TriggerMode);
283PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
284PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
285PDMBOTHCBDECL(void) ioapicSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel);
286__END_DECLS
287#endif /* VBOX */
288
289#ifndef VBOX
290static void apic_bus_deliver(uint32_t deliver_bitmask, uint8_t delivery_mode,
291 uint8_t vector_num, uint8_t polarity,
292 uint8_t trigger_mode)
293{
294 APICState *apic_iter;
295#else /* VBOX */
296static void apic_bus_deliver(APICState *s, uint32_t deliver_bitmask, uint8_t delivery_mode,
297 uint8_t vector_num, uint8_t polarity,
298 uint8_t trigger_mode)
299{
300#endif /* VBOX */
301
302 switch (delivery_mode) {
303 case APIC_DM_LOWPRI:
304 case APIC_DM_FIXED:
305 /* XXX: arbitration */
306 break;
307
308 case APIC_DM_SMI:
309 case APIC_DM_NMI:
310 break;
311
312 case APIC_DM_INIT:
313 /* normal INIT IPI sent to processors */
314#ifdef VBOX
315 apic_init_ipi (s);
316#else
317 for (apic_iter = first_local_apic; apic_iter != NULL;
318 apic_iter = apic_iter->next_apic) {
319 apic_init_ipi(apic_iter);
320 }
321#endif
322 return;
323
324 case APIC_DM_EXTINT:
325 /* handled in I/O APIC code */
326 break;
327
328 default:
329 return;
330 }
331
332#ifdef VBOX
333 if (deliver_bitmask & (1 << s->id))
334 apic_set_irq (s, vector_num, trigger_mode);
335#else /* VBOX */
336 for (apic_iter = first_local_apic; apic_iter != NULL;
337 apic_iter = apic_iter->next_apic) {
338 if (deliver_bitmask & (1 << apic_iter->id))
339 apic_set_irq(apic_iter, vector_num, trigger_mode);
340 }
341#endif /* VBOX */
342}
343
344#ifndef VBOX
345void cpu_set_apic_base(CPUState *env, uint64_t val)
346{
347 APICState *s = env->apic_state;
348#ifdef DEBUG_APIC
349 Log(("cpu_set_apic_base: %016llx\n", val));
350#endif
351
352 s->apicbase = (val & 0xfffff000) |
353 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
354 /* if disabled, cannot be enabled again */
355 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
356 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
357 env->cpuid_features &= ~CPUID_APIC;
358 s->spurious_vec &= ~APIC_SV_ENABLE;
359 }
360}
361#else /* VBOX */
362PDMBOTHCBDECL(void) apicSetBase(PPDMDEVINS pDevIns, uint64_t val)
363{
364 APICState *s = PDMINS2DATA(pDevIns, APICState *);
365 Log(("cpu_set_apic_base: %016RX64\n", val));
366
367 /** @todo If this change is valid immediately, then we should change the MMIO registration! */
368 s->apicbase = (val & 0xfffff000) |
369 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
370 /* if disabled, cannot be enabled again (until reset) */
371 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
372 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
373 s->spurious_vec &= ~APIC_SV_ENABLE;
374
375 /* Clear any pending APIC interrupt action flag. */
376 s->CTXALLSUFF(pApicHlp)->pfnClearInterruptFF(s->CTXSUFF(pDevIns));
377 s->CTXALLSUFF(pApicHlp)->pfnChangeFeature(pDevIns, false);
378 }
379}
380#endif /* VBOX */
381#ifndef VBOX
382
383uint64_t cpu_get_apic_base(CPUState *env)
384{
385 APICState *s = env->apic_state;
386#ifdef DEBUG_APIC
387 Log(("cpu_get_apic_base: %016llx\n", (uint64_t)s->apicbase));
388#endif
389 return s->apicbase;
390}
391
392void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
393{
394 APICState *s = env->apic_state;
395 s->tpr = (val & 0x0f) << 4;
396 apic_update_irq(s);
397}
398
399uint8_t cpu_get_apic_tpr(CPUX86State *env)
400{
401 APICState *s = env->apic_state;
402 return s->tpr >> 4;
403}
404
405static int fls_bit(int value)
406{
407 unsigned int ret = 0;
408
409#ifdef HOST_I386
410 __asm__ __volatile__ ("bsr %1, %0\n" : "+r" (ret) : "rm" (value));
411 return ret;
412#else
413 if (value > 0xffff)
414 value >>= 16, ret = 16;
415 if (value > 0xff)
416 value >>= 8, ret += 8;
417 if (value > 0xf)
418 value >>= 4, ret += 4;
419 if (value > 0x3)
420 value >>= 2, ret += 2;
421 return ret + (value >> 1);
422#endif
423}
424
425static inline void set_bit(uint32_t *tab, int index)
426{
427 int i, mask;
428 i = index >> 5;
429 mask = 1 << (index & 0x1f);
430 tab[i] |= mask;
431}
432
433static inline void reset_bit(uint32_t *tab, int index)
434{
435 int i, mask;
436 i = index >> 5;
437 mask = 1 << (index & 0x1f);
438 tab[i] &= ~mask;
439}
440
441
442#else /* VBOX */
443
444PDMBOTHCBDECL(uint64_t) apicGetBase(PPDMDEVINS pDevIns)
445{
446 APICState *s = PDMINS2DATA(pDevIns, APICState *);
447 Log(("apicGetBase: %016llx\n", (uint64_t)s->apicbase));
448 return s->apicbase;
449}
450
451PDMBOTHCBDECL(void) apicSetTPR(PPDMDEVINS pDevIns, uint8_t val)
452{
453 APICState *s = PDMINS2DATA(pDevIns, APICState *);
454 LogFlow(("apicSetTPR: val=%#x (trp %#x -> %#x)\n", val, s->tpr, (val & 0x0f) << 4));
455 s->tpr = (val & 0x0f) << 4;
456 apic_update_irq(s);
457}
458
459PDMBOTHCBDECL(uint8_t) apicGetTPR(PPDMDEVINS pDevIns)
460{
461 APICState *s = PDMINS2DATA(pDevIns, APICState *);
462 LogFlow(("apicGetTPR: returns %#x\n", s->tpr >> 4));
463 return s->tpr >> 4;
464}
465
466/**
467 * More or less private interface between IOAPIC, only PDM is responsible
468 * for connecting the two devices.
469 */
470PDMBOTHCBDECL(void) apicBusDeliverCallback(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
471 uint8_t u8DeliveryMode, uint8_t iVector, uint8_t u8Polarity,
472 uint8_t u8TriggerMode)
473{
474 APICState *s = PDMINS2DATA(pDevIns, APICState *);
475 LogFlow(("apicBusDeliverCallback: s=%p pDevIns=%p u8Dest=%#x u8DestMode=%#x u8DeliveryMode=%#x iVector=%#x u8Polarity=%#x u8TriggerMode=%#x\n",
476 s, pDevIns, u8Dest, u8DestMode, u8DeliveryMode, iVector, u8Polarity, u8TriggerMode));
477 apic_bus_deliver(s, apic_get_delivery_bitmask(s, u8Dest, u8DestMode),
478 u8DeliveryMode, iVector, u8Polarity, u8TriggerMode);
479}
480
481# define set_bit(pvBitmap, iBit) ASMBitSet(pvBitmap, iBit)
482# define reset_bit(pvBitmap, iBit) ASMBitClear(pvBitmap, iBit)
483# define fls_bit(value) (ASMBitLastSetU32(value) - 1)
484
485#endif /* VBOX */
486
487/* return -1 if no bit is set */
488static int get_highest_priority_int(uint32_t *tab)
489{
490 int i;
491 for(i = 7; i >= 0; i--) {
492 if (tab[i] != 0) {
493 return i * 32 + fls_bit(tab[i]);
494 }
495 }
496 return -1;
497}
498
499static int apic_get_ppr(APICState *s)
500{
501 int tpr, isrv, ppr;
502
503 tpr = (s->tpr >> 4);
504 isrv = get_highest_priority_int(s->isr);
505 if (isrv < 0)
506 isrv = 0;
507 isrv >>= 4;
508 if (tpr >= isrv)
509 ppr = s->tpr;
510 else
511 ppr = isrv << 4;
512 return ppr;
513}
514
515static int apic_get_arb_pri(APICState *s)
516{
517 /* XXX: arbitration */
518 return 0;
519}
520
521/* signal the CPU if an irq is pending */
522static void apic_update_irq(APICState *s)
523{
524 int irrv, ppr;
525 if (!(s->spurious_vec & APIC_SV_ENABLE))
526#ifdef VBOX
527 {
528 /* Clear any pending APIC interrupt action flag. */
529 s->CTXALLSUFF(pApicHlp)->pfnClearInterruptFF(s->CTXSUFF(pDevIns));
530 return;
531 }
532#else
533 return;
534#endif /* VBOX */
535 irrv = get_highest_priority_int(s->irr);
536 if (irrv < 0)
537 return;
538 ppr = apic_get_ppr(s);
539 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
540 return;
541#ifndef VBOX
542 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
543#else
544 s->CTXALLSUFF(pApicHlp)->pfnSetInterruptFF(s->CTXSUFF(pDevIns));
545#endif
546}
547
548static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
549{
550 set_bit(s->irr, vector_num);
551 if (trigger_mode)
552 set_bit(s->tmr, vector_num);
553 else
554 reset_bit(s->tmr, vector_num);
555 apic_update_irq(s);
556}
557
558static void apic_eoi(APICState *s)
559{
560 int isrv;
561 isrv = get_highest_priority_int(s->isr);
562 if (isrv < 0)
563 return;
564 reset_bit(s->isr, isrv);
565 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
566 set the remote IRR bit for level triggered interrupts. */
567 apic_update_irq(s);
568}
569
570#ifndef VBOX
571static uint32_t apic_get_delivery_bitmask(uint8_t dest, uint8_t dest_mode)
572#else /* VBOX */
573static uint32_t apic_get_delivery_bitmask(APICState *s, uint8_t dest, uint8_t dest_mode)
574#endif /* VBOX */
575{
576 uint32_t mask = 0;
577#ifndef VBOX
578 APICState *apic_iter;
579#endif /* !VBOX */
580
581 if (dest_mode == 0) {
582 if (dest == 0xff)
583 mask = 0xff;
584 else
585 mask = 1 << dest;
586 } else {
587 /* XXX: cluster mode */
588#ifdef VBOX
589 if (dest & s->log_dest)
590 mask |= 1 << s->id;
591#else /* !VBOX */
592 for (apic_iter = first_local_apic; apic_iter != NULL;
593 apic_iter = apic_iter->next_apic) {
594 if (dest & apic_iter->log_dest)
595 mask |= (1 << apic_iter->id);
596 }
597#endif /* !VBOX */
598 }
599
600 return mask;
601}
602
603
604static void apic_init_ipi(APICState *s)
605{
606 int i;
607
608 for(i = 0; i < APIC_LVT_NB; i++)
609 s->lvt[i] = 1 << 16; /* mask LVT */
610 s->tpr = 0;
611 s->spurious_vec = 0xff;
612 s->log_dest = 0;
613 s->dest_mode = 0xff;
614 memset(s->isr, 0, sizeof(s->isr));
615 memset(s->tmr, 0, sizeof(s->tmr));
616 memset(s->irr, 0, sizeof(s->irr));
617 memset(s->lvt, 0, sizeof(s->lvt));
618 s->esr = 0;
619 memset(s->icr, 0, sizeof(s->icr));
620 s->divide_conf = 0;
621 s->count_shift = 0;
622 s->initial_count = 0;
623 s->initial_count_load_time = 0;
624 s->next_time = 0;
625}
626
627static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
628 uint8_t delivery_mode, uint8_t vector_num,
629 uint8_t polarity, uint8_t trigger_mode)
630{
631 uint32_t deliver_bitmask = 0;
632 int dest_shorthand = (s->icr[0] >> 18) & 3;
633#ifndef VBOX
634 APICState *apic_iter;
635#endif /* !VBOX */
636
637 switch (delivery_mode) {
638 case APIC_DM_LOWPRI:
639 /* XXX: serch for focus processor, arbitration */
640 dest = s->id;
641
642 case APIC_DM_INIT:
643 {
644 int trig_mode = (s->icr[0] >> 15) & 1;
645 int level = (s->icr[0] >> 14) & 1;
646 if (level == 0 && trig_mode == 1) {
647#ifdef VBOX
648 if (deliver_bitmask & (1 << s->id)) {
649 s->arb_id = s->id;
650 }
651#else /* !VBOX */
652 for (apic_iter = first_local_apic; apic_iter != NULL;
653 apic_iter = apic_iter->next_apic) {
654 if (deliver_bitmask & (1 << apic_iter->id)) {
655 apic_iter->arb_id = apic_iter->id;
656 }
657 }
658#endif /* !VBOX */
659 return;
660 }
661 }
662 break;
663
664 case APIC_DM_SIPI:
665#ifndef VBOX
666 for (apic_iter = first_local_apic; apic_iter != NULL;
667 apic_iter = apic_iter->next_apic) {
668 if (deliver_bitmask & (1 << apic_iter->id)) {
669 /* XXX: SMP support */
670 /* apic_startup(apic_iter); */
671 }
672 }
673#endif /* !VBOX */
674 return;
675 }
676
677 switch (dest_shorthand) {
678 case 0:
679#ifndef VBOX
680 deliver_bitmask = apic_get_delivery_bitmask(dest, dest_mode);
681#else /* VBOX */
682 deliver_bitmask = apic_get_delivery_bitmask(s, dest, dest_mode);
683#endif /* !VBOX */
684 break;
685 case 1:
686 deliver_bitmask = (1 << s->id);
687 break;
688 case 2:
689 deliver_bitmask = 0xffffffff;
690 break;
691 case 3:
692 deliver_bitmask = 0xffffffff & ~(1 << s->id);
693 break;
694 }
695
696#ifndef VBOX
697 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
698 trigger_mode);
699#else /* VBOX */
700 apic_bus_deliver(s, deliver_bitmask, delivery_mode, vector_num, polarity,
701 trigger_mode);
702#endif /* VBOX */
703}
704
705#ifndef VBOX
706int apic_get_interrupt(CPUState *env)
707{
708 APICState *s = env->apic_state;
709#else /* VBOX */
710PDMBOTHCBDECL(int) apicGetInterrupt(PPDMDEVINS pDevIns)
711{
712 APICState *s = PDMINS2DATA(pDevIns, APICState *);
713#endif /* VBOX */
714 int intno;
715
716 /* if the APIC is installed or enabled, we let the 8259 handle the
717 IRQs */
718 if (!s) {
719 Log(("apic_get_interrupt: returns -1 (!s)\n"));
720 return -1;
721 }
722 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
723 Log(("apic_get_interrupt: returns -1 (APIC_SV_ENABLE)\n"));
724 return -1;
725 }
726
727 /* XXX: spurious IRQ handling */
728 intno = get_highest_priority_int(s->irr);
729 if (intno < 0) {
730 Log(("apic_get_interrupt: returns -1 (irr)\n"));
731 return -1;
732 }
733 reset_bit(s->irr, intno);
734 if (s->tpr && intno <= s->tpr) {
735 Log(("apic_get_interrupt: returns %d (sp)\n", s->spurious_vec & 0xff));
736 return s->spurious_vec & 0xff;
737 }
738 set_bit(s->isr, intno);
739 apic_update_irq(s);
740 LogFlow(("apic_get_interrupt: returns %d\n", intno));
741 return intno;
742}
743
744static uint32_t apic_get_current_count(APICState *s)
745{
746 int64_t d;
747 uint32_t val;
748#ifndef VBOX
749 d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
750 s->count_shift;
751#else /* VBOX */
752 d = (TMTimerGet(s->CTXSUFF(pTimer)) - s->initial_count_load_time) >>
753 s->count_shift;
754#endif /* VBOX */
755 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
756 /* periodic */
757 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
758 } else {
759 if (d >= s->initial_count)
760 val = 0;
761 else
762 val = s->initial_count - d;
763 }
764 return val;
765}
766
767static void apic_timer_update(APICState *s, int64_t current_time)
768{
769 int64_t next_time, d;
770
771 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
772 d = (current_time - s->initial_count_load_time) >>
773 s->count_shift;
774 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
775 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
776 } else {
777 if (d >= s->initial_count)
778 goto no_timer;
779 d = (uint64_t)s->initial_count + 1;
780 }
781 next_time = s->initial_count_load_time + (d << s->count_shift);
782#ifndef VBOX
783 qemu_mod_timer(s->timer, next_time);
784#else
785 TMTimerSet(s->CTXSUFF(pTimer), next_time);
786#endif
787 s->next_time = next_time;
788 } else {
789 no_timer:
790#ifndef VBOX
791 qemu_del_timer(s->timer);
792#else
793 TMTimerStop(s->CTXSUFF(pTimer));
794#endif
795 }
796}
797
798#ifdef IN_RING3
799#ifndef VBOX
800static void apic_timer(void *opaque)
801{
802 APICState *s = opaque;
803#else /* VBOX */
804static DECLCALLBACK(void) apicTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer)
805{
806 APICState *s = PDMINS2DATA(pDevIns, APICState *);
807# ifdef VBOX_WITH_PDM_LOCK
808 s->pApicHlpR3->pfnLock(pDevIns, VERR_INTERNAL_ERROR);
809# endif
810#endif /* VBOX */
811
812 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
813 apic_set_irq(s, s->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE);
814 }
815 apic_timer_update(s, s->next_time);
816
817#ifdef VBOX
818 APIC_UNLOCK(s);
819#endif
820}
821#endif /* IN_RING3 */
822
823#ifndef VBOX
824static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
825{
826 return 0;
827}
828
829static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
830{
831 return 0;
832}
833
834static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
835{
836}
837
838static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
839{
840}
841#endif /* !VBOX */
842
843
844#ifndef VBOX
845static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
846{
847 CPUState *env;
848 APICState *s;
849#else /* VBOX */
850static uint32_t apic_mem_readl(APICState *s, target_phys_addr_t addr)
851{
852#endif /* VBOX */
853 uint32_t val;
854 int index;
855
856#ifndef VBOX
857 env = cpu_single_env;
858 if (!env)
859 return 0;
860 s = env->apic_state;
861#endif /* !VBOX */
862
863 index = (addr >> 4) & 0xff;
864 switch(index) {
865 case 0x02: /* id */
866 val = s->id << 24;
867 break;
868 case 0x03: /* version */
869 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
870 break;
871 case 0x08:
872 val = s->tpr;
873 break;
874 case 0x09:
875 val = apic_get_arb_pri(s);
876 break;
877 case 0x0a:
878 /* ppr */
879 val = apic_get_ppr(s);
880 break;
881#ifdef VBOX
882 case 0x0b:
883 Log(("apic_mem_readl %x %x -> write only returning 0\n", addr, index));
884 val = 0;
885 break;
886#endif
887
888 case 0x0d:
889 val = s->log_dest << 24;
890 break;
891 case 0x0e:
892#ifdef VBOX
893 /* Bottom 28 bits are always 1 */
894 val = (s->dest_mode << 28) | 0xfffffff;
895#else
896 val = s->dest_mode << 28;
897#endif
898 break;
899 case 0x0f:
900 val = s->spurious_vec;
901 break;
902#ifndef VBOX
903 case 0x10 ... 0x17:
904#else /* VBOX */
905 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
906#endif /* VBOX */
907 val = s->isr[index & 7];
908 break;
909#ifndef VBOX
910 case 0x18 ... 0x1f:
911#else /* VBOX */
912 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
913#endif /* VBOX */
914 val = s->tmr[index & 7];
915 break;
916#ifndef VBOX
917 case 0x20 ... 0x27:
918#else /* VBOX */
919 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
920#endif /* VBOX */
921 val = s->irr[index & 7];
922 break;
923 case 0x28:
924 val = s->esr;
925 break;
926 case 0x30:
927 case 0x31:
928 val = s->icr[index & 1];
929 break;
930#ifndef VBOX
931 case 0x32 ... 0x37:
932#else /* VBOX */
933 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
934#endif /* VBOX */
935 val = s->lvt[index - 0x32];
936 break;
937 case 0x38:
938 val = s->initial_count;
939 break;
940 case 0x39:
941 val = apic_get_current_count(s);
942 break;
943 case 0x3e:
944 val = s->divide_conf;
945 break;
946 default:
947 AssertMsgFailed(("apic_mem_readl: unknown index %x\n", index));
948 s->esr |= ESR_ILLEGAL_ADDRESS;
949 val = 0;
950 break;
951 }
952#ifdef DEBUG_APIC
953 Log(("APIC read: %08x = %08x\n", (uint32_t)addr, val));
954#endif
955 return val;
956}
957
958#ifndef VBOX
959static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
960{
961 CPUState *env;
962 APICState *s;
963#else /* VBOX */
964static int apic_mem_writel(APICState *s, target_phys_addr_t addr, uint32_t val)
965{
966#endif /* VBOX */
967 int index;
968
969#ifndef VBOX
970 env = cpu_single_env;
971 if (!env)
972 return;
973 s = env->apic_state;
974#endif /* !VBOX */
975
976#ifdef DEBUG_APIC
977 Log(("APIC write: %08x = %08x\n", (uint32_t)addr, val));
978#endif
979
980 index = (addr >> 4) & 0xff;
981 switch(index) {
982 case 0x02:
983 s->id = (val >> 24);
984 break;
985 case 0x03:
986 Log(("apic_mem_writel: write to version register; ignored\n"));
987 break;
988 case 0x08:
989 s->tpr = val;
990 apic_update_irq(s);
991 break;
992 case 0x09:
993 case 0x0a:
994 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
995 break;
996 case 0x0b: /* EOI */
997 apic_eoi(s);
998 break;
999 case 0x0d:
1000 s->log_dest = val >> 24;
1001 break;
1002 case 0x0e:
1003 s->dest_mode = val >> 28;
1004 break;
1005 case 0x0f:
1006 s->spurious_vec = val & 0x1ff;
1007 apic_update_irq(s);
1008 break;
1009#ifndef VBOX
1010 case 0x10 ... 0x17:
1011 case 0x18 ... 0x1f:
1012 case 0x20 ... 0x27:
1013 case 0x28:
1014#else
1015 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
1016 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1017 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1018 case 0x28:
1019 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
1020#endif
1021 break;
1022
1023 case 0x30:
1024 s->icr[0] = val;
1025 apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
1026 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
1027 (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
1028 break;
1029 case 0x31:
1030 s->icr[1] = val;
1031 break;
1032#ifndef VBOX
1033 case 0x32 ... 0x37:
1034#else /* VBOX */
1035 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1036#endif /* VBOX */
1037 {
1038 int n = index - 0x32;
1039 s->lvt[n] = val;
1040 if (n == APIC_LVT_TIMER)
1041#ifndef VBOX
1042 apic_timer_update(s, qemu_get_clock(vm_clock));
1043#else /* VBOX */
1044 apic_timer_update(s, TMTimerGet(s->CTXSUFF(pTimer)));
1045#endif /* VBOX*/
1046 }
1047 break;
1048 case 0x38:
1049 s->initial_count = val;
1050#ifndef VBOX
1051 s->initial_count_load_time = qemu_get_clock(vm_clock);
1052#else /* VBOX */
1053 s->initial_count_load_time = TMTimerGet(s->CTXSUFF(pTimer));
1054#endif /* VBOX*/
1055 apic_timer_update(s, s->initial_count_load_time);
1056 break;
1057 case 0x39:
1058 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
1059 break;
1060 case 0x3e:
1061 {
1062 int v;
1063 s->divide_conf = val & 0xb;
1064 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
1065 s->count_shift = (v + 1) & 7;
1066 }
1067 break;
1068 default:
1069 AssertMsgFailed(("apic_mem_writel: unknown index %x\n", index));
1070 s->esr |= ESR_ILLEGAL_ADDRESS;
1071 break;
1072 }
1073#ifdef VBOX
1074 return VINF_SUCCESS;
1075#endif
1076}
1077
1078#ifdef IN_RING3
1079
1080static void apic_save(QEMUFile *f, void *opaque)
1081{
1082 APICState *s = opaque;
1083 int i;
1084
1085 qemu_put_be32s(f, &s->apicbase);
1086 qemu_put_8s(f, &s->id);
1087 qemu_put_8s(f, &s->arb_id);
1088#ifdef VBOX
1089 qemu_put_be32s(f, &s->tpr);
1090#else
1091 qemu_put_8s(f, &s->tpr);
1092#endif
1093 qemu_put_be32s(f, &s->spurious_vec);
1094 qemu_put_8s(f, &s->log_dest);
1095 qemu_put_8s(f, &s->dest_mode);
1096 for (i = 0; i < 8; i++) {
1097 qemu_put_be32s(f, &s->isr[i]);
1098 qemu_put_be32s(f, &s->tmr[i]);
1099 qemu_put_be32s(f, &s->irr[i]);
1100 }
1101 for (i = 0; i < APIC_LVT_NB; i++) {
1102 qemu_put_be32s(f, &s->lvt[i]);
1103 }
1104 qemu_put_be32s(f, &s->esr);
1105 qemu_put_be32s(f, &s->icr[0]);
1106 qemu_put_be32s(f, &s->icr[1]);
1107 qemu_put_be32s(f, &s->divide_conf);
1108 qemu_put_be32s(f, &s->count_shift);
1109 qemu_put_be32s(f, &s->initial_count);
1110 qemu_put_be64s(f, &s->initial_count_load_time);
1111 qemu_put_be64s(f, &s->next_time);
1112}
1113
1114static int apic_load(QEMUFile *f, void *opaque, int version_id)
1115{
1116 APICState *s = opaque;
1117 int i;
1118
1119 if (version_id != 1)
1120 return -EINVAL;
1121
1122 /* XXX: what if the base changes? (registered memory regions) */
1123 qemu_get_be32s(f, &s->apicbase);
1124 qemu_get_8s(f, &s->id);
1125 qemu_get_8s(f, &s->arb_id);
1126#ifdef VBOX
1127 qemu_get_be32s(f, &s->tpr);
1128#else
1129 qemu_get_8s(f, &s->tpr);
1130#endif
1131 qemu_get_be32s(f, &s->spurious_vec);
1132 qemu_get_8s(f, &s->log_dest);
1133 qemu_get_8s(f, &s->dest_mode);
1134 for (i = 0; i < 8; i++) {
1135 qemu_get_be32s(f, &s->isr[i]);
1136 qemu_get_be32s(f, &s->tmr[i]);
1137 qemu_get_be32s(f, &s->irr[i]);
1138 }
1139 for (i = 0; i < APIC_LVT_NB; i++) {
1140 qemu_get_be32s(f, &s->lvt[i]);
1141 }
1142 qemu_get_be32s(f, &s->esr);
1143 qemu_get_be32s(f, &s->icr[0]);
1144 qemu_get_be32s(f, &s->icr[1]);
1145 qemu_get_be32s(f, &s->divide_conf);
1146 qemu_get_be32s(f, (uint32_t *)&s->count_shift);
1147 qemu_get_be32s(f, (uint32_t *)&s->initial_count);
1148 qemu_get_be64s(f, (uint64_t *)&s->initial_count_load_time);
1149 qemu_get_be64s(f, (uint64_t *)&s->next_time);
1150 return 0;
1151}
1152
1153static void apic_reset(void *opaque)
1154{
1155 APICState *s = opaque;
1156#ifdef VBOX
1157 TMTimerStop(s->CTXSUFF(pTimer));
1158
1159 /* malc, I've removed the initing duplicated in apic_init_ipi(). This
1160 * arb_id was left over.. */
1161 s->arb_id = 0;
1162
1163 /* Reset should re-enable the APIC. */
1164 s->apicbase = 0xfee00000 | MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE;
1165 s->pApicHlpR3->pfnChangeFeature(s->pDevInsHC, true);
1166
1167#endif /* VBOX */
1168 apic_init_ipi(s);
1169}
1170
1171#endif /* IN_RING3 */
1172
1173#ifndef VBOX
1174static CPUReadMemoryFunc *apic_mem_read[3] = {
1175 apic_mem_readb,
1176 apic_mem_readw,
1177 apic_mem_readl,
1178};
1179
1180static CPUWriteMemoryFunc *apic_mem_write[3] = {
1181 apic_mem_writeb,
1182 apic_mem_writew,
1183 apic_mem_writel,
1184};
1185
1186int apic_init(CPUState *env)
1187{
1188 APICState *s;
1189
1190 s = qemu_mallocz(sizeof(APICState));
1191 if (!s)
1192 return -1;
1193 env->apic_state = s;
1194 apic_init_ipi(s);
1195 s->id = last_apic_id++;
1196 s->cpu_env = env;
1197 s->apicbase = 0xfee00000 |
1198 (s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
1199
1200 /* XXX: mapping more APICs at the same memory location */
1201 if (apic_io_memory == 0) {
1202 /* NOTE: the APIC is directly connected to the CPU - it is not
1203 on the global memory bus. */
1204 apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
1205 apic_mem_write, NULL);
1206 cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000,
1207 apic_io_memory);
1208 }
1209 s->timer = qemu_new_timer(vm_clock, apic_timer, s);
1210
1211 register_savevm("apic", 0, 1, apic_save, apic_load, s);
1212 qemu_register_reset(apic_reset, s);
1213
1214 s->next_apic = first_local_apic;
1215 first_local_apic = s;
1216
1217 return 0;
1218}
1219#endif /* !VBOX */
1220
1221static void ioapic_service(IOAPICState *s)
1222{
1223 uint8_t i;
1224 uint8_t trig_mode;
1225 uint8_t vector;
1226 uint8_t delivery_mode;
1227 uint32_t mask;
1228 uint64_t entry;
1229 uint8_t dest;
1230 uint8_t dest_mode;
1231 uint8_t polarity;
1232
1233 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1234 mask = 1 << i;
1235 if (s->irr & mask) {
1236 entry = s->ioredtbl[i];
1237 if (!(entry & APIC_LVT_MASKED)) {
1238 trig_mode = ((entry >> 15) & 1);
1239 dest = entry >> 56;
1240 dest_mode = (entry >> 11) & 1;
1241 delivery_mode = (entry >> 8) & 7;
1242 polarity = (entry >> 13) & 1;
1243 if (trig_mode == APIC_TRIGGER_EDGE)
1244 s->irr &= ~mask;
1245 if (delivery_mode == APIC_DM_EXTINT)
1246#ifndef VBOX /* malc: i'm still not so sure about ExtINT delivery */
1247 vector = pic_read_irq(isa_pic);
1248#else /* VBOX */
1249 {
1250 AssertMsgFailed(("Delivery mode ExtINT"));
1251 vector = 0xff; /* incorrect but shuts up gcc. */
1252 }
1253#endif /* VBOX */
1254 else
1255 vector = entry & 0xff;
1256
1257#ifndef VBOX
1258 apic_bus_deliver(apic_get_delivery_bitmask(dest, dest_mode),
1259 delivery_mode, vector, polarity, trig_mode);
1260#else /* VBOX */
1261 s->CTXALLSUFF(pIoApicHlp)->pfnApicBusDeliver(s->CTXSUFF(pDevIns),
1262 dest,
1263 dest_mode,
1264 delivery_mode,
1265 vector,
1266 polarity,
1267 trig_mode);
1268#endif /* VBOX */
1269 }
1270 }
1271 }
1272}
1273
1274#ifdef VBOX
1275static
1276#endif
1277void ioapic_set_irq(void *opaque, int vector, int level)
1278{
1279 IOAPICState *s = opaque;
1280
1281 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
1282 uint32_t mask = 1 << vector;
1283 uint64_t entry = s->ioredtbl[vector];
1284
1285 if ((entry >> 15) & 1) {
1286 /* level triggered */
1287 if (level) {
1288 s->irr |= mask;
1289 ioapic_service(s);
1290 } else {
1291 s->irr &= ~mask;
1292 }
1293 } else {
1294 /* edge triggered */
1295 if (level) {
1296 s->irr |= mask;
1297 ioapic_service(s);
1298 }
1299 }
1300 }
1301}
1302
1303static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
1304{
1305 IOAPICState *s = opaque;
1306 int index;
1307 uint32_t val = 0;
1308
1309 addr &= 0xff;
1310 if (addr == 0x00) {
1311 val = s->ioregsel;
1312 } else if (addr == 0x10) {
1313 switch (s->ioregsel) {
1314 case 0x00:
1315 val = s->id << 24;
1316 break;
1317 case 0x01:
1318 val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
1319 break;
1320 case 0x02:
1321 val = 0;
1322 break;
1323 default:
1324 index = (s->ioregsel - 0x10) >> 1;
1325 if (index >= 0 && index < IOAPIC_NUM_PINS) {
1326 if (s->ioregsel & 1)
1327 val = s->ioredtbl[index] >> 32;
1328 else
1329 val = s->ioredtbl[index] & 0xffffffff;
1330 }
1331 }
1332#ifdef DEBUG_IOAPIC
1333 Log(("I/O APIC read: %08x = %08x\n", s->ioregsel, val));
1334#endif
1335 }
1336 return val;
1337}
1338
1339static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1340{
1341 IOAPICState *s = opaque;
1342 int index;
1343
1344 addr &= 0xff;
1345 if (addr == 0x00) {
1346 s->ioregsel = val;
1347 return;
1348 } else if (addr == 0x10) {
1349#ifdef DEBUG_IOAPIC
1350 Log(("I/O APIC write: %08x = %08x\n", s->ioregsel, val));
1351#endif
1352 switch (s->ioregsel) {
1353 case 0x00:
1354 s->id = (val >> 24) & 0xff;
1355 return;
1356 case 0x01:
1357 case 0x02:
1358 return;
1359 default:
1360 index = (s->ioregsel - 0x10) >> 1;
1361 if (index >= 0 && index < IOAPIC_NUM_PINS) {
1362 if (s->ioregsel & 1) {
1363 s->ioredtbl[index] &= 0xffffffff;
1364 s->ioredtbl[index] |= (uint64_t)val << 32;
1365 } else {
1366 s->ioredtbl[index] &= ~0xffffffffULL;
1367 s->ioredtbl[index] |= val;
1368 }
1369 ioapic_service(s);
1370 }
1371 }
1372 }
1373}
1374
1375#ifdef IN_RING3
1376
1377static void ioapic_save(QEMUFile *f, void *opaque)
1378{
1379 IOAPICState *s = opaque;
1380 int i;
1381
1382 qemu_put_8s(f, &s->id);
1383 qemu_put_8s(f, &s->ioregsel);
1384 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1385 qemu_put_be64s(f, &s->ioredtbl[i]);
1386 }
1387}
1388
1389static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
1390{
1391 IOAPICState *s = opaque;
1392 int i;
1393
1394 if (version_id != 1)
1395 return -EINVAL;
1396
1397 qemu_get_8s(f, &s->id);
1398 qemu_get_8s(f, &s->ioregsel);
1399 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1400 qemu_get_be64s(f, &s->ioredtbl[i]);
1401 }
1402 return 0;
1403}
1404
1405static void ioapic_reset(void *opaque)
1406{
1407 IOAPICState *s = opaque;
1408#ifdef VBOX
1409 PPDMDEVINSHC pDevIns = s->pDevInsHC;
1410 PCPDMIOAPICHLPR3 pIoApicHlp = s->pIoApicHlpR3;
1411#endif
1412 int i;
1413
1414 memset(s, 0, sizeof(*s));
1415 for(i = 0; i < IOAPIC_NUM_PINS; i++)
1416 s->ioredtbl[i] = 1 << 16; /* mask LVT */
1417
1418#ifdef VBOX
1419 if (pDevIns)
1420 {
1421 s->pDevInsHC = pDevIns;
1422 s->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
1423 }
1424 if (pIoApicHlp)
1425 {
1426 s->pIoApicHlpR3 = pIoApicHlp;
1427 s->pIoApicHlpGC = s->pIoApicHlpR3->pfnGetGCHelpers(pDevIns);
1428 s->pIoApicHlpR0 = s->pIoApicHlpR3->pfnGetR0Helpers(pDevIns);
1429 }
1430#endif
1431}
1432
1433#endif /* IN_RING3 */
1434
1435#ifndef VBOX
1436static CPUReadMemoryFunc *ioapic_mem_read[3] = {
1437 ioapic_mem_readl,
1438 ioapic_mem_readl,
1439 ioapic_mem_readl,
1440};
1441
1442static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
1443 ioapic_mem_writel,
1444 ioapic_mem_writel,
1445 ioapic_mem_writel,
1446};
1447
1448IOAPICState *ioapic_init(void)
1449{
1450 IOAPICState *s;
1451 int io_memory;
1452
1453 s = qemu_mallocz(sizeof(IOAPICState));
1454 if (!s)
1455 return NULL;
1456 ioapic_reset(s);
1457 s->id = last_apic_id++;
1458
1459 io_memory = cpu_register_io_memory(0, ioapic_mem_read,
1460 ioapic_mem_write, s);
1461 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
1462
1463 register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s);
1464 qemu_register_reset(ioapic_reset, s);
1465
1466 return s;
1467}
1468#endif /* !VBOX */
1469
1470/* LAPIC */
1471
1472/* LAPICs MMIO is wrong, in SMP there is no relation between memory range and
1473 lapic, it's only the CPU that executes this memory access is what matters */
1474
1475PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1476{
1477 APICState *s = PDMINS2DATA(pDevIns, APICState *);
1478
1479 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIORead));
1480 switch (cb)
1481 {
1482 case 1:
1483 *(uint8_t *)pv = 0;
1484 break;
1485
1486 case 2:
1487 *(uint16_t *)pv = 0;
1488 break;
1489
1490 case 4:
1491 {
1492#if 0 /** @note experimental */
1493#ifndef IN_RING3
1494 uint32_t index = (GCPhysAddr >> 4) & 0xff;
1495
1496 if ( index == 0x08 /* TPR */
1497 && ++s->ulTPRPatchAttempts < APIC_MAX_PATCH_ATTEMPTS)
1498 {
1499#ifdef IN_GC
1500 pDevIns->pDevHlpGC->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, &s->tpr);
1501#else
1502 RTGCPTR pDevInsGC = PDMINS2DATA_GCPTR(pDevIns);
1503 pDevIns->pDevHlpR0->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, pDevIns + RT_OFFSETOF(APICState, tpr));
1504#endif
1505 return VINF_PATM_HC_MMIO_PATCH_READ;
1506 }
1507#endif
1508#endif /* experimental */
1509 APIC_LOCK(s, VINF_IOM_HC_MMIO_READ);
1510 *(uint32_t *)pv = apic_mem_readl(s, GCPhysAddr);
1511 APIC_UNLOCK(s);
1512 break;
1513 }
1514 default:
1515 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1516 return VERR_INTERNAL_ERROR;
1517 }
1518 return VINF_SUCCESS;
1519}
1520
1521PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1522{
1523 APICState *s = PDMINS2DATA(pDevIns, APICState *);
1524
1525 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIOWrite));
1526 switch (cb)
1527 {
1528 case 1:
1529 case 2:
1530 /* ignore */
1531 break;
1532
1533 case 4:
1534 {
1535 int rc;
1536 APIC_LOCK(s, VINF_IOM_HC_MMIO_WRITE);
1537 rc = apic_mem_writel(s, GCPhysAddr, *(uint32_t *)pv);
1538 APIC_UNLOCK(s);
1539 return rc;
1540 }
1541
1542 default:
1543 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1544 return VERR_INTERNAL_ERROR;
1545 }
1546 return VINF_SUCCESS;
1547}
1548
1549#ifdef IN_RING3
1550
1551/**
1552 * @copydoc FNSSMDEVSAVEEXEC
1553 */
1554static DECLCALLBACK(int) apicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1555{
1556 APICState *s = PDMINS2DATA(pDevIns, APICState *);
1557 apic_save(pSSMHandle, s);
1558 return TMR3TimerSave(s->CTXSUFF(pTimer), pSSMHandle);
1559}
1560
1561/**
1562 * @copydoc FNSSMDEVLOADEXEC
1563 */
1564static DECLCALLBACK(int) apicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
1565{
1566 APICState *s = PDMINS2DATA(pDevIns, APICState *);
1567 if (apic_load(pSSMHandle, s, u32Version)) {
1568 AssertFailed();
1569 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1570 }
1571 return TMR3TimerLoad(s->CTXSUFF(pTimer), pSSMHandle);
1572}
1573
1574/**
1575 * @copydoc FNPDMDEVRESET
1576 */
1577static DECLCALLBACK(void) apicReset(PPDMDEVINS pDevIns)
1578{
1579 APICState *s = PDMINS2DATA(pDevIns, APICState *);
1580#ifdef VBOX_WITH_PDM_LOCK
1581 s->pApicHlpR3->pfnLock(pDevIns, VERR_INTERNAL_ERROR);
1582#endif
1583 apic_reset(s);
1584 /* Clear any pending APIC interrupt action flag. */
1585 s->pApicHlpR3->pfnClearInterruptFF(pDevIns);
1586 APIC_UNLOCK(s);
1587}
1588
1589/**
1590 * @copydoc FNPDMDEVRELOCATE
1591 */
1592static DECLCALLBACK(void) apicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1593{
1594 APICState *pData = PDMINS2DATA(pDevIns, APICState *);
1595 pData->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
1596 pData->pApicHlpGC = pData->pApicHlpR3->pfnGetGCHelpers(pDevIns);
1597 pData->pTimerGC = TMTimerGCPtr(pData->CTXSUFF(pTimer));
1598}
1599
1600/**
1601 * @copydoc FNPDMDEVCONSTRUCT
1602 */
1603static DECLCALLBACK(int) apicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1604{
1605 APICState *pData = PDMINS2DATA(pDevIns, APICState *);
1606 PDMAPICREG ApicReg;
1607 int rc;
1608 int i;
1609 bool fGCEnabled;
1610 bool fR0Enabled;
1611 Assert(iInstance == 0);
1612
1613 /*
1614 * Validate configuration.
1615 */
1616 if (!CFGMR3AreValuesValid(pCfgHandle, "GCEnabled\0R0Enabled"))
1617 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1618
1619 rc = CFGMR3QueryBool(pCfgHandle, "GCEnabled", &fGCEnabled);
1620 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1621 fGCEnabled = true;
1622 else
1623 return PDMDEV_SET_ERROR(pDevIns, rc,
1624 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
1625 Log(("APIC: fGCEnabled=%d\n", fGCEnabled));
1626
1627 rc = CFGMR3QueryBool(pCfgHandle, "R0Enabled", &fR0Enabled);
1628 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1629 fR0Enabled = true;
1630 else
1631 return PDMDEV_SET_ERROR(pDevIns, rc,
1632 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
1633 Log(("APIC: fR0Enabled=%d\n", fR0Enabled));
1634
1635 /*
1636 * Init the data.
1637 */
1638 pData->pDevInsHC = pDevIns;
1639 pData->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
1640 pData->apicbase = 0xfee00000 | MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE;
1641 for (i = 0; i < APIC_LVT_NB; i++)
1642 pData->lvt[i] = 1 << 16; /* mask LVT */
1643 pData->spurious_vec = 0xff;
1644
1645 /*
1646 * Register the APIC.
1647 */
1648 ApicReg.u32Version = PDM_APICREG_VERSION;
1649 ApicReg.pfnGetInterruptHC = apicGetInterrupt;
1650 ApicReg.pfnSetBaseHC = apicSetBase;
1651 ApicReg.pfnGetBaseHC = apicGetBase;
1652 ApicReg.pfnSetTPRHC = apicSetTPR;
1653 ApicReg.pfnGetTPRHC = apicGetTPR;
1654 ApicReg.pfnBusDeliverHC = apicBusDeliverCallback;
1655 if (fGCEnabled) {
1656 ApicReg.pszGetInterruptGC = "apicGetInterrupt";
1657 ApicReg.pszSetBaseGC = "apicSetBase";
1658 ApicReg.pszGetBaseGC = "apicGetBase";
1659 ApicReg.pszSetTPRGC = "apicSetTPR";
1660 ApicReg.pszGetTPRGC = "apicGetTPR";
1661 ApicReg.pszBusDeliverGC = "apicBusDeliverCallback";
1662 } else {
1663 ApicReg.pszGetInterruptGC = NULL;
1664 ApicReg.pszSetBaseGC = NULL;
1665 ApicReg.pszGetBaseGC = NULL;
1666 ApicReg.pszSetTPRGC = NULL;
1667 ApicReg.pszGetTPRGC = NULL;
1668 ApicReg.pszBusDeliverGC = NULL;
1669 }
1670 if (fR0Enabled) {
1671 ApicReg.pszGetInterruptR0 = "apicGetInterrupt";
1672 ApicReg.pszSetBaseR0 = "apicSetBase";
1673 ApicReg.pszGetBaseR0 = "apicGetBase";
1674 ApicReg.pszSetTPRR0 = "apicSetTPR";
1675 ApicReg.pszGetTPRR0 = "apicGetTPR";
1676 ApicReg.pszBusDeliverR0 = "apicBusDeliverCallback";
1677 } else {
1678 ApicReg.pszGetInterruptR0 = NULL;
1679 ApicReg.pszSetBaseR0 = NULL;
1680 ApicReg.pszGetBaseR0 = NULL;
1681 ApicReg.pszSetTPRR0 = NULL;
1682 ApicReg.pszGetTPRR0 = NULL;
1683 ApicReg.pszBusDeliverR0 = NULL;
1684 }
1685
1686 Assert(pDevIns->pDevHlp->pfnAPICRegister);
1687 rc = pDevIns->pDevHlp->pfnAPICRegister(pDevIns, &ApicReg, &pData->pApicHlpR3);
1688 if (VBOX_FAILURE(rc))
1689 {
1690 AssertMsgFailed(("APICRegister -> %Vrc\n", rc));
1691 return rc;
1692 }
1693 pData->pApicHlpGC = pData->pApicHlpR3->pfnGetGCHelpers(pDevIns);
1694
1695 /*
1696 * The the CPUID feature bit.
1697 */
1698 pData->pApicHlpR3->pfnChangeFeature(pDevIns, true);
1699
1700 /*
1701 * Register the MMIO range.
1702 */
1703 rc = PDMDevHlpMMIORegister(pDevIns, pData->apicbase & ~0xfff, 0x1000, pData,
1704 apicMMIOWrite, apicMMIORead, NULL, "APIC Memory");
1705 if (VBOX_FAILURE(rc))
1706 return rc;
1707
1708 if (fGCEnabled) {
1709 rc = PDMDevHlpMMIORegisterGC(pDevIns, pData->apicbase & ~0xfff, 0x1000, 0,
1710 "apicMMIOWrite", "apicMMIORead", NULL, "APIC Memory");
1711 if (VBOX_FAILURE(rc))
1712 return rc;
1713 }
1714
1715 if (fR0Enabled) {
1716 pData->pApicHlpR0 = pData->pApicHlpR3->pfnGetR0Helpers(pDevIns);
1717
1718 rc = PDMDevHlpMMIORegisterR0(pDevIns, pData->apicbase & ~0xfff, 0x1000, 0,
1719 "apicMMIOWrite", "apicMMIORead", NULL, "APIC Memory");
1720 if (VBOX_FAILURE(rc))
1721 return rc;
1722 }
1723
1724 /*
1725 * Create the APIC timer.
1726 */
1727 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, apicTimer,
1728 "APIC Timer", &pData->CTXSUFF(pTimer));
1729 if (VBOX_FAILURE(rc))
1730 return rc;
1731 pData->pTimerGC = TMTimerGCPtr(pData->CTXSUFF(pTimer));
1732
1733 /*
1734 * Saved state.
1735 */
1736 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1 /* version */,
1737 sizeof(*pData), NULL, apicSaveExec, NULL, NULL, apicLoadExec, NULL);
1738 if (VBOX_FAILURE(rc))
1739 return rc;
1740
1741#ifdef VBOX_WITH_STATISTICS
1742 /*
1743 * Statistics.
1744 */
1745 PDMDevHlpSTAMRegister(pDevIns, &pData->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in GC.");
1746 PDMDevHlpSTAMRegister(pDevIns, &pData->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in HC.");
1747 PDMDevHlpSTAMRegister(pDevIns, &pData->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in GC.");
1748 PDMDevHlpSTAMRegister(pDevIns, &pData->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in HC.");
1749#endif
1750
1751 return VINF_SUCCESS;
1752}
1753
1754
1755/**
1756 * APIC device registration structure.
1757 */
1758const PDMDEVREG g_DeviceAPIC =
1759{
1760 /* u32Version */
1761 PDM_DEVREG_VERSION,
1762 /* szDeviceName */
1763 "apic",
1764 /* szGCMod */
1765 "VBoxDD2GC.gc",
1766 /* szR0Mod */
1767 "VBoxDD2R0.r0",
1768 /* pszDescription */
1769 "Advanced Programmable Interrupt Controller",
1770 /* fFlags */
1771 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_GC | PDM_DEVREG_FLAGS_R0,
1772 /* fClass */
1773 PDM_DEVREG_CLASS_PIC,
1774 /* cMaxInstances */
1775 1,
1776 /* cbInstance */
1777 sizeof(APICState),
1778 /* pfnConstruct */
1779 apicConstruct,
1780 /* pfnDestruct */
1781 NULL,
1782 /* pfnRelocate */
1783 apicRelocate,
1784 /* pfnIOCtl */
1785 NULL,
1786 /* pfnPowerOn */
1787 NULL,
1788 /* pfnReset */
1789 apicReset,
1790 /* pfnSuspend */
1791 NULL,
1792 /* pfnResume */
1793 NULL,
1794 /* pfnAttach */
1795 NULL,
1796 /* pfnDetach */
1797 NULL,
1798 /* pfnQueryInterface. */
1799 NULL
1800};
1801
1802#endif /* IN_RING3 */
1803
1804
1805
1806
1807/* IOAPIC */
1808
1809PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1810{
1811 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1812 IOAPIC_LOCK(s, VINF_IOM_HC_MMIO_READ);
1813
1814 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIORead));
1815 switch (cb)
1816 {
1817 case 1:
1818 *(uint8_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
1819 break;
1820
1821 case 2:
1822 *(uint16_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
1823 break;
1824
1825 case 4:
1826 *(uint32_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
1827 break;
1828
1829 default:
1830 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1831 IOAPIC_UNLOCK(s);
1832 return VERR_INTERNAL_ERROR;
1833 }
1834 IOAPIC_UNLOCK(s);
1835 return VINF_SUCCESS;
1836}
1837
1838PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
1839{
1840 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1841
1842 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIOWrite));
1843 switch (cb)
1844 {
1845 case 1:
1846 case 2:
1847 case 4:
1848 IOAPIC_LOCK(s, VINF_IOM_HC_MMIO_WRITE);
1849 ioapic_mem_writel(s, GCPhysAddr, *(uint32_t *)pv);
1850 IOAPIC_UNLOCK(s);
1851 break;
1852
1853 default:
1854 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
1855 return VERR_INTERNAL_ERROR;
1856 }
1857 return VINF_SUCCESS;
1858}
1859
1860PDMBOTHCBDECL(void) ioapicSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel)
1861{
1862 IOAPICState *pThis = PDMINS2DATA(pDevIns, IOAPICState *);
1863 STAM_COUNTER_INC(&pThis->CTXSUFF(StatSetIrq));
1864 LogFlow(("ioapicSetIrq: iIrq=%d iLevel=%d\n", iIrq, iLevel));
1865 ioapic_set_irq(pThis, iIrq, iLevel);
1866}
1867
1868
1869#ifdef IN_RING3
1870
1871/**
1872 * @copydoc FNSSMDEVSAVEEXEC
1873 */
1874static DECLCALLBACK(int) ioapicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
1875{
1876 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1877 ioapic_save(pSSMHandle, s);
1878 return VINF_SUCCESS;
1879}
1880
1881/**
1882 * @copydoc FNSSMDEVLOADEXEC
1883 */
1884static DECLCALLBACK(int) ioapicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
1885{
1886 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1887
1888 if (ioapic_load(pSSMHandle, s, u32Version)) {
1889 AssertFailed();
1890 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1891 }
1892
1893 return VINF_SUCCESS;
1894}
1895
1896/**
1897 * @copydoc FNPDMDEVRESET
1898 */
1899static DECLCALLBACK(void) ioapicReset(PPDMDEVINS pDevIns)
1900{
1901 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1902#ifdef VBOX_WITH_PDM_LOCK
1903 s->pIoApicHlpR3->pfnLock(pDevIns, VERR_INTERNAL_ERROR);
1904#endif
1905 ioapic_reset(s);
1906 IOAPIC_UNLOCK(s);
1907}
1908
1909/**
1910 * @copydoc FNPDMDEVRELOCATE
1911 */
1912static DECLCALLBACK(void) ioapicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1913{
1914 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1915 s->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
1916 s->pIoApicHlpGC = s->pIoApicHlpR3->pfnGetGCHelpers(pDevIns);
1917}
1918
1919/**
1920 * @copydoc FNPDMDEVCONSTRUCT
1921 */
1922static DECLCALLBACK(int) ioapicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
1923{
1924 IOAPICState *s = PDMINS2DATA(pDevIns, IOAPICState *);
1925 PDMIOAPICREG IoApicReg;
1926 bool fGCEnabled;
1927 bool fR0Enabled;
1928 int rc;
1929
1930 Assert(iInstance == 0);
1931
1932 /*
1933 * Validate and read the configuration.
1934 */
1935 if (!CFGMR3AreValuesValid(pCfgHandle, "GCEnabled\0R0Enabled"))
1936 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1937
1938 rc = CFGMR3QueryBool(pCfgHandle, "GCEnabled", &fGCEnabled);
1939 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1940 fGCEnabled = true;
1941 else if (VBOX_FAILURE(rc))
1942 return PDMDEV_SET_ERROR(pDevIns, rc,
1943 N_("Configuration error: Failed to query boolean value \"GCEnabled\"!"));
1944 Log(("IOAPIC: fGCEnabled=%d\n", fGCEnabled));
1945
1946 rc = CFGMR3QueryBool(pCfgHandle, "R0Enabled", &fR0Enabled);
1947 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1948 fR0Enabled = true;
1949 else if (VBOX_FAILURE(rc))
1950 return PDMDEV_SET_ERROR(pDevIns, rc,
1951 N_("Configuration error: Failed to query boolean value \"R0Enabled\"!"));
1952 Log(("IOAPIC: fR0Enabled=%d\n", fR0Enabled));
1953
1954 /*
1955 * Initialize the state data.
1956 */
1957 s->pDevInsHC = pDevIns;
1958 s->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
1959 ioapic_reset(s);
1960 s->id = 0;
1961
1962 /*
1963 * Register the IOAPIC and get helpers.
1964 */
1965 IoApicReg.u32Version = PDM_IOAPICREG_VERSION;
1966 IoApicReg.pfnSetIrqHC = ioapicSetIrq;
1967 IoApicReg.pszSetIrqGC = fGCEnabled ? "ioapicSetIrq" : NULL;
1968 IoApicReg.pszSetIrqR0 = fR0Enabled ? "ioapicSetIrq" : NULL;
1969 rc = pDevIns->pDevHlp->pfnIOAPICRegister(pDevIns, &IoApicReg, &s->pIoApicHlpR3);
1970 if (VBOX_FAILURE(rc))
1971 {
1972 AssertMsgFailed(("IOAPICRegister -> %Vrc\n", rc));
1973 return rc;
1974 }
1975 s->pIoApicHlpGC = s->pIoApicHlpR3->pfnGetGCHelpers(pDevIns);
1976
1977 /*
1978 * Register MMIO callbacks and saved state.
1979 */
1980 rc = PDMDevHlpMMIORegister(pDevIns, 0xfec00000, 0x1000, s,
1981 ioapicMMIOWrite, ioapicMMIORead, NULL, "I/O APIC Memory");
1982 if (VBOX_FAILURE(rc))
1983 return rc;
1984
1985 if (fGCEnabled) {
1986 rc = PDMDevHlpMMIORegisterGC(pDevIns, 0xfec00000, 0x1000, 0,
1987 "ioapicMMIOWrite", "ioapicMMIORead", NULL, "I/O APIC Memory");
1988 if (VBOX_FAILURE(rc))
1989 return rc;
1990 }
1991
1992 if (fR0Enabled) {
1993 s->pIoApicHlpR0 = s->pIoApicHlpR3->pfnGetR0Helpers(pDevIns);
1994
1995 rc = PDMDevHlpMMIORegisterR0(pDevIns, 0xfec00000, 0x1000, 0,
1996 "ioapicMMIOWrite", "ioapicMMIORead", NULL, "I/O APIC Memory");
1997 if (VBOX_FAILURE(rc))
1998 return rc;
1999 }
2000
2001 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1 /* version */,
2002 sizeof(*s), NULL, ioapicSaveExec, NULL, NULL, ioapicLoadExec, NULL);
2003 if (VBOX_FAILURE(rc))
2004 return rc;
2005
2006#ifdef VBOX_WITH_STATISTICS
2007 /*
2008 * Statistics.
2009 */
2010 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in GC.");
2011 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in HC.");
2012 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in GC.");
2013 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in HC.");
2014 PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in GC.");
2015 PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in HC.");
2016#endif
2017
2018 return VINF_SUCCESS;
2019}
2020
2021/**
2022 * IO APIC device registration structure.
2023 */
2024const PDMDEVREG g_DeviceIOAPIC =
2025{
2026 /* u32Version */
2027 PDM_DEVREG_VERSION,
2028 /* szDeviceName */
2029 "ioapic",
2030 /* szGCMod */
2031 "VBoxDD2GC.gc",
2032 /* szR0Mod */
2033 "VBoxDD2R0.r0",
2034 /* pszDescription */
2035 "I/O Advanced Programmable Interrupt Controller",
2036 /* fFlags */
2037 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_GC | PDM_DEVREG_FLAGS_R0,
2038 /* fClass */
2039 PDM_DEVREG_CLASS_PIC,
2040 /* cMaxInstances */
2041 1,
2042 /* cbInstance */
2043 sizeof(IOAPICState),
2044 /* pfnConstruct */
2045 ioapicConstruct,
2046 /* pfnDestruct */
2047 NULL,
2048 /* pfnRelocate */
2049 ioapicRelocate,
2050 /* pfnIOCtl */
2051 NULL,
2052 /* pfnPowerOn */
2053 NULL,
2054 /* pfnReset */
2055 ioapicReset,
2056 /* pfnSuspend */
2057 NULL,
2058 /* pfnResume */
2059 NULL,
2060 /* pfnAttach */
2061 NULL,
2062 /* pfnDetach */
2063 NULL,
2064 /* pfnQueryInterface. */
2065 NULL,
2066 /* pfnInitComplete */
2067 NULL,
2068 /* pfnPowerOff */
2069 NULL
2070};
2071
2072#endif /* IN_RING3 */
2073#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
2074
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