VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevAPIC.cpp@ 19668

Last change on this file since 19668 was 19650, checked in by vboxsync, 16 years ago

Bug fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 81.3 KB
Line 
1#ifdef VBOX
2/* $Id: DevAPIC.cpp 19650 2009-05-13 10:11:08Z vboxsync $ */
3/** @file
4 * Advanced Programmable Interrupt Controller (APIC) Device and
5 * I/O Advanced Programmable Interrupt Controller (IO-APIC) Device.
6 */
7
8/*
9 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 * --------------------------------------------------------------------
23 *
24 * This code is based on:
25 *
26 * apic.c revision 1.5 @@OSETODO
27 */
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV_APIC
33#include <VBox/pdmdev.h>
34
35#include <VBox/log.h>
36#include <VBox/stam.h>
37#include <iprt/assert.h>
38#include <iprt/asm.h>
39
40#include "Builtins2.h"
41#include "vl_vbox.h"
42
43#define MSR_IA32_APICBASE 0x1b
44#define MSR_IA32_APICBASE_BSP (1<<8)
45#define MSR_IA32_APICBASE_ENABLE (1<<11)
46#ifdef VBOX
47#define MSR_IA32_APICBASE_X2ENABLE (1<<10)
48#endif
49#define MSR_IA32_APICBASE_BASE (0xfffff<<12)
50
51#ifndef EINVAL
52# define EINVAL 1
53#endif
54
55#ifdef _MSC_VER
56# pragma warning(disable:4244)
57#endif
58
59/** @def APIC_LOCK
60 * Acquires the PDM lock. */
61#define APIC_LOCK(pThis, rc) \
62 do { \
63 int rc2 = (pThis)->CTX_SUFF(pApicHlp)->pfnLock((pThis)->CTX_SUFF(pDevIns), rc); \
64 if (rc2 != VINF_SUCCESS) \
65 return rc2; \
66 } while (0)
67
68/** @def APIC_LOCK_VOID
69 * Acquires the PDM lock and does not expect failure (i.e. ring-3 only!). */
70#define APIC_LOCK_VOID(pThis, rc) \
71 do { \
72 int rc2 = (pThis)->CTX_SUFF(pApicHlp)->pfnLock((pThis)->CTX_SUFF(pDevIns), rc); \
73 AssertLogRelRCReturnVoid(rc2); \
74 } while (0)
75
76/** @def APIC_UNLOCK
77 * Releases the PDM lock. */
78#define APIC_UNLOCK(pThis) \
79 (pThis)->CTX_SUFF(pApicHlp)->pfnUnlock((pThis)->CTX_SUFF(pDevIns))
80
81/** @def IOAPIC_LOCK
82 * Acquires the PDM lock. */
83#define IOAPIC_LOCK(pThis, rc) \
84 do { \
85 int rc2 = (pThis)->CTX_SUFF(pIoApicHlp)->pfnLock((pThis)->CTX_SUFF(pDevIns), rc); \
86 if (rc2 != VINF_SUCCESS) \
87 return rc2; \
88 } while (0)
89
90/** @def IOAPIC_UNLOCK
91 * Releases the PDM lock. */
92#define IOAPIC_UNLOCK(pThis) (pThis)->CTX_SUFF(pIoApicHlp)->pfnUnlock((pThis)->CTX_SUFF(pDevIns))
93
94/** @def LAPIC_BASE
95 * Return address of first LAPIC state. */
96#define LAPIC_BASE(pThis) ((APICState*)(pThis)->CTX_SUFF(pLapics))
97
98#define foreach_apic(dev, mask, code) \
99 do { \
100 uint32_t i; \
101 APICState* apic = LAPIC_BASE(dev); \
102 for (i = 0; i < dev->cCpus; i++) \
103 { \
104 if (mask & (1 << (apic->id))) \
105 { \
106 code; \
107 } \
108 apic++; \
109 } \
110 } while (0)
111
112# define set_bit(pvBitmap, iBit) ASMBitSet(pvBitmap, iBit)
113# define reset_bit(pvBitmap, iBit) ASMBitClear(pvBitmap, iBit)
114# define fls_bit(value) (ASMBitLastSetU32(value) - 1)
115# define ffs_bit(value) (ASMBitFirstSetU32(value) - 1)
116
117#endif /* VBOX */
118
119/*
120 * APIC support
121 *
122 * Copyright (c) 2004-2005 Fabrice Bellard
123 *
124 * This library is free software; you can redistribute it and/or
125 * modify it under the terms of the GNU Lesser General Public
126 * License as published by the Free Software Foundation; either
127 * version 2 of the License, or (at your option) any later version.
128 *
129 * This library is distributed in the hope that it will be useful,
130 * but WITHOUT ANY WARRANTY; without even the implied warranty of
131 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
132 * Lesser General Public License for more details.
133 *
134 * You should have received a copy of the GNU Lesser General Public
135 * License along with this library; if not, write to the Free Software
136 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
137 */
138#ifndef VBOX
139#include "vl.h"
140#endif
141
142#define DEBUG_APIC
143#define DEBUG_IOAPIC
144
145/* APIC Local Vector Table */
146#define APIC_LVT_TIMER 0
147#define APIC_LVT_THERMAL 1
148#define APIC_LVT_PERFORM 2
149#define APIC_LVT_LINT0 3
150#define APIC_LVT_LINT1 4
151#define APIC_LVT_ERROR 5
152#define APIC_LVT_NB 6
153
154/* APIC delivery modes */
155#define APIC_DM_FIXED 0
156#define APIC_DM_LOWPRI 1
157#define APIC_DM_SMI 2
158#define APIC_DM_NMI 4
159#define APIC_DM_INIT 5
160#define APIC_DM_SIPI 6
161#define APIC_DM_EXTINT 7
162
163/* APIC destination mode */
164#define APIC_DESTMODE_FLAT 0xf
165#define APIC_DESTMODE_CLUSTER 1
166
167#define APIC_TRIGGER_EDGE 0
168#define APIC_TRIGGER_LEVEL 1
169
170#define APIC_LVT_TIMER_PERIODIC (1<<17)
171#define APIC_LVT_MASKED (1<<16)
172#define APIC_LVT_LEVEL_TRIGGER (1<<15)
173#define APIC_LVT_REMOTE_IRR (1<<14)
174#define APIC_INPUT_POLARITY (1<<13)
175#define APIC_SEND_PENDING (1<<12)
176
177#define IOAPIC_NUM_PINS 0x18
178
179#define ESR_ILLEGAL_ADDRESS (1 << 7)
180
181#define APIC_SV_ENABLE (1 << 8)
182
183#ifdef VBOX
184#define APIC_MAX_PATCH_ATTEMPTS 100
185
186typedef uint32_t PhysApicId;
187typedef uint32_t LogApicId;
188#endif
189
190typedef struct APICState {
191#ifndef VBOX
192 CPUState *cpu_env;
193#endif /* !VBOX */
194 uint32_t apicbase;
195#ifdef VBOX
196 /* Task priority register (interrupt level) */
197 uint32_t tpr;
198 /* Logical APIC id */
199 LogApicId id;
200 /* Physical APIC id */
201 PhysApicId phys_id;
202 /** @todo: is it logical or physical? Not really used anyway now. */
203 PhysApicId arb_id;
204#else
205 uint8_t tpr;
206 uint8_t id;
207 uint8_t arb_id;
208#endif
209 uint32_t spurious_vec;
210 uint8_t log_dest;
211 uint8_t dest_mode;
212 uint32_t isr[8]; /* in service register */
213 uint32_t tmr[8]; /* trigger mode register */
214 uint32_t irr[8]; /* interrupt request register */
215 uint32_t lvt[APIC_LVT_NB];
216 uint32_t esr; /* error register */
217 uint32_t icr[2];
218 uint32_t divide_conf;
219 int count_shift;
220 uint32_t initial_count;
221#ifdef VBOX
222 uint32_t Alignment0;
223#endif
224 int64_t initial_count_load_time, next_time;
225#ifndef VBOX
226 QEMUTimer *timer;
227 struct APICState *next_apic;
228#else
229 /** The APIC timer - R3 Ptr. */
230 PTMTIMERR3 pTimerR3;
231
232 /** The APIC timer - R0 Ptr. */
233 PTMTIMERR0 pTimerR0;
234
235 /** The APIC timer - RC Ptr. */
236 PTMTIMERRC pTimerRC;
237
238 /** Alignment */
239 uint32_t Alignment1;
240#endif /* VBOX */
241} APICState;
242
243struct IOAPICState {
244 uint8_t id;
245 uint8_t ioregsel;
246
247 uint32_t irr;
248 uint64_t ioredtbl[IOAPIC_NUM_PINS];
249
250#ifdef VBOX
251 /** The device instance - R3 Ptr. */
252 PPDMDEVINSR3 pDevInsR3;
253 /** The IOAPIC helpers - R3 Ptr. */
254 PCPDMIOAPICHLPR3 pIoApicHlpR3;
255
256 /** The device instance - R0 Ptr. */
257 PPDMDEVINSR0 pDevInsR0;
258 /** The IOAPIC helpers - R0 Ptr. */
259 PCPDMIOAPICHLPR0 pIoApicHlpR0;
260
261 /** The device instance - RC Ptr. */
262 PPDMDEVINSRC pDevInsRC;
263 /** The IOAPIC helpers - RC Ptr. */
264 PCPDMIOAPICHLPRC pIoApicHlpRC;
265
266# ifdef VBOX_WITH_STATISTICS
267 STAMCOUNTER StatMMIOReadGC;
268 STAMCOUNTER StatMMIOReadHC;
269 STAMCOUNTER StatMMIOWriteGC;
270 STAMCOUNTER StatMMIOWriteHC;
271 STAMCOUNTER StatSetIrqGC;
272 STAMCOUNTER StatSetIrqHC;
273# endif
274#endif /* VBOX */
275};
276
277#ifdef VBOX
278typedef struct IOAPICState IOAPICState;
279
280typedef struct
281{
282 /** The device instance - R3 Ptr. */
283 PPDMDEVINSR3 pDevInsR3;
284 /** The APIC helpers - R3 Ptr. */
285 PCPDMAPICHLPR3 pApicHlpR3;
286 /** LAPICs states - R3 Ptr */
287 RTR3PTR pLapicsR3;
288
289 /** The device instance - R0 Ptr. */
290 PPDMDEVINSR0 pDevInsR0;
291 /** The APIC helpers - R0 Ptr. */
292 PCPDMAPICHLPR0 pApicHlpR0;
293 /** LAPICs states - R0 Ptr */
294 RTR0PTR pLapicsR0;
295
296 /** The device instance - RC Ptr. */
297 PPDMDEVINSRC pDevInsRC;
298 /** The APIC helpers - RC Ptr. */
299 PCPDMAPICHLPRC pApicHlpRC;
300 /** LAPICs states - RC Ptr */
301 RTRCPTR pLapicsRC;
302
303 /** APIC specification version in this virtual hardware configuration. */
304 PDMAPICVERSION enmVersion;
305
306 /** Number of attempts made to optimize TPR accesses. */
307 uint32_t ulTPRPatchAttempts;
308
309 /** Number of CPUs on the system (same as LAPIC count). */
310 uint32_t cCpus;
311
312# ifdef VBOX_WITH_STATISTICS
313 STAMCOUNTER StatMMIOReadGC;
314 STAMCOUNTER StatMMIOReadHC;
315 STAMCOUNTER StatMMIOWriteGC;
316 STAMCOUNTER StatMMIOWriteHC;
317 STAMCOUNTER StatClearedActiveIrq;
318# endif
319} APICDeviceInfo;
320
321static void apic_eoi(APICDeviceInfo *dev, APICState* s);
322static int apic_deliver(APICDeviceInfo* dev, APICState *s,
323 uint8_t dest, uint8_t dest_mode,
324 uint8_t delivery_mode, uint8_t vector_num,
325 uint8_t polarity, uint8_t trigger_mode);
326static void apic_timer_update(APICDeviceInfo* dev, APICState *s,
327 int64_t current_time);
328static int apic_get_arb_pri(APICState *s);
329static int apic_get_ppr(APICState *s);
330static uint32_t apic_get_current_count(APICDeviceInfo* dev, APICState *s);
331
332
333DECLINLINE(APICState*) getLapicById(APICDeviceInfo* dev, VMCPUID id)
334{
335 AssertFatalMsg(id < dev->cCpus, ("CPU id %d out of range\n", id));
336 return LAPIC_BASE(dev) + id;
337}
338
339DECLINLINE(APICState*) getLapic(APICDeviceInfo* dev)
340{
341 /* LAPIC's array is indexed by CPU id */
342 VMCPUID id = dev->CTX_SUFF(pApicHlp)->pfnGetCpuId(dev->CTX_SUFF(pDevIns));
343 return getLapicById(dev, id);
344}
345
346DECLINLINE(VMCPUID) getCpuFromLapic(APICDeviceInfo* dev, APICState *s)
347{
348 /* for now we assume LAPIC physical id == CPU id */
349 return VMCPUID(s->phys_id);
350}
351
352DECLINLINE(void) cpuSetInterrupt(APICDeviceInfo* dev, APICState *s)
353{
354 Log2(("apic: setting interrupt flag\n"));
355 dev->CTX_SUFF(pApicHlp)->pfnSetInterruptFF(dev->CTX_SUFF(pDevIns),
356 getCpuFromLapic(dev, s));
357}
358
359DECLINLINE(void) cpuClearInterrupt(APICDeviceInfo* dev, APICState *s)
360{
361 Log2(("apic: clear interrupt flag\n"));
362 dev->CTX_SUFF(pApicHlp)->pfnClearInterruptFF(dev->CTX_SUFF(pDevIns),
363 getCpuFromLapic(dev, s));
364}
365
366#ifdef IN_RING3
367DECLINLINE(void) cpuSendSipi(APICDeviceInfo* dev, APICState *s, int vector)
368{
369 Log2(("apic: send SIPI vector=%d\n", vector));
370
371 dev->pApicHlpR3->pfnSendSipi(dev->pDevInsR3,
372 getCpuFromLapic(dev, s),
373 vector);
374}
375
376DECLINLINE(void) cpuSendInitIpi(APICDeviceInfo* dev, APICState *s)
377{
378 Log2(("apic: send init IPI\n"));
379
380 dev->pApicHlpR3->pfnSendInitIpi(dev->pDevInsR3,
381 getCpuFromLapic(dev, s));
382}
383#endif
384
385DECLINLINE(uint32_t) getApicEnableBits(APICDeviceInfo* dev)
386{
387 switch (dev->enmVersion)
388 {
389 case PDMAPICVERSION_NONE:
390 return 0;
391 case PDMAPICVERSION_APIC:
392 return MSR_IA32_APICBASE_ENABLE;
393 case PDMAPICVERSION_X2APIC:
394 return MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_X2ENABLE ;
395 default:
396 AssertMsgFailed(("Unsuported APIC version %d\n", dev->enmVersion));
397 return 0;
398 }
399}
400
401DECLINLINE(PDMAPICVERSION) getApicMode(APICState *apic)
402{
403 switch (((apic->apicbase) >> 10) & 0x3)
404 {
405 case 0:
406 return PDMAPICVERSION_NONE;
407 case 1:
408 default:
409 /* Invalid */
410 return PDMAPICVERSION_NONE;
411 case 2:
412 return PDMAPICVERSION_APIC;
413 case 3:
414 return PDMAPICVERSION_X2APIC;
415 }
416}
417
418#endif /* VBOX */
419
420#ifndef VBOX_DEVICE_STRUCT_TESTCASE
421#ifndef VBOX
422static int apic_io_memory;
423static APICState *first_local_apic = NULL;
424static int last_apic_id = 0;
425#endif /* !VBOX */
426
427static void apic_init_ipi(APICDeviceInfo* dev, APICState *s);
428static void apic_set_irq(APICDeviceInfo* dev, APICState *s, int vector_num, int trigger_mode);
429static bool apic_update_irq(APICDeviceInfo* dev, APICState *s);
430
431#ifdef VBOX
432static uint32_t apic_get_delivery_bitmask(APICDeviceInfo* dev, uint8_t dest, uint8_t dest_mode);
433__BEGIN_DECLS
434PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
435PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
436PDMBOTHCBDECL(int) apicGetInterrupt(PPDMDEVINS pDevIns);
437PDMBOTHCBDECL(bool) apicHasPendingIrq(PPDMDEVINS pDevIns);
438PDMBOTHCBDECL(void) apicSetBase(PPDMDEVINS pDevIns, uint64_t val);
439PDMBOTHCBDECL(uint64_t) apicGetBase(PPDMDEVINS pDevIns);
440PDMBOTHCBDECL(void) apicSetTPR(PPDMDEVINS pDevIns, uint8_t val);
441PDMBOTHCBDECL(uint8_t) apicGetTPR(PPDMDEVINS pDevIns);
442PDMBOTHCBDECL(int) apicBusDeliverCallback(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
443 uint8_t u8DeliveryMode, uint8_t iVector, uint8_t u8Polarity,
444 uint8_t u8TriggerMode);
445PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID iCpu, uint32_t u32Reg, uint64_t u64Value);
446PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID iCpu, uint32_t u32Reg, uint64_t *pu64Value);
447PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
448PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb);
449PDMBOTHCBDECL(void) ioapicSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel);
450
451static void apic_update_tpr(APICDeviceInfo *dev, APICState* s, uint32_t val);
452__END_DECLS
453#endif /* VBOX */
454
455#ifndef VBOX
456static void apic_bus_deliver(uint32_t deliver_bitmask, uint8_t delivery_mode,
457 uint8_t vector_num, uint8_t polarity,
458 uint8_t trigger_mode)
459{
460 APICState *apic_iter;
461#else /* VBOX */
462static int apic_bus_deliver(APICDeviceInfo* dev,
463 uint32_t deliver_bitmask, uint8_t delivery_mode,
464 uint8_t vector_num, uint8_t polarity,
465 uint8_t trigger_mode)
466{
467#endif /* VBOX */
468
469 LogFlow(("apic_bus_deliver mask=%x mode=%x vector=%x polarity=%x trigger_mode=%x\n", deliver_bitmask, delivery_mode, vector_num, polarity, trigger_mode));
470 switch (delivery_mode) {
471 case APIC_DM_LOWPRI:
472 {
473 int d = -1;
474 if (deliver_bitmask)
475 d = ffs_bit(deliver_bitmask);
476 if (d >= 0)
477 {
478 APICState* apic = getLapicById(dev, d);
479 apic_set_irq(dev, apic, vector_num, trigger_mode);
480 }
481 return VINF_SUCCESS;
482 }
483 case APIC_DM_FIXED:
484 /* XXX: arbitration */
485 break;
486
487 case APIC_DM_SMI:
488 /** @todo: what do we really do with SMI */
489 foreach_apic(dev, deliver_bitmask,
490 cpuSetInterrupt(dev, apic));
491 return VINF_SUCCESS;
492
493 case APIC_DM_NMI:
494 /** @todo: what do we really do with NMI */
495 foreach_apic(dev, deliver_bitmask,
496 cpuSetInterrupt(dev, apic));
497 return VINF_SUCCESS;
498
499 case APIC_DM_INIT:
500 /* normal INIT IPI sent to processors */
501#ifdef VBOX
502#ifdef IN_RING3
503 foreach_apic(dev, deliver_bitmask,
504 apic_init_ipi(dev, apic));
505 return VINF_SUCCESS;
506#else
507 /* We shall send init IPI only in R3, R0 calls should be
508 rescheduled to R3 */
509 return VINF_IOM_HC_MMIO_READ_WRITE;
510#endif /* IN_RING3 */
511
512#else
513 for (apic_iter = first_local_apic; apic_iter != NULL;
514 apic_iter = apic_iter->next_apic) {
515 apic_init_ipi(apic_iter);
516 }
517#endif
518
519 case APIC_DM_EXTINT:
520 /* handled in I/O APIC code */
521 break;
522
523 default:
524 return VINF_SUCCESS;
525 }
526
527#ifdef VBOX
528 foreach_apic(dev, deliver_bitmask,
529 apic_set_irq (dev, apic, vector_num, trigger_mode));
530 return VINF_SUCCESS;
531#else /* VBOX */
532 for (apic_iter = first_local_apic; apic_iter != NULL;
533 apic_iter = apic_iter->next_apic) {
534 if (deliver_bitmask & (1 << apic_iter->id))
535 apic_set_irq(apic_iter, vector_num, trigger_mode);
536 }
537#endif /* VBOX */
538}
539
540#ifndef VBOX
541void cpu_set_apic_base(CPUState *env, uint64_t val)
542{
543 APICState *s = env->apic_state;
544#ifdef DEBUG_APIC
545 Log(("cpu_set_apic_base: %016llx\n", val));
546#endif
547
548 s->apicbase = (val & 0xfffff000) |
549 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
550 /* if disabled, cannot be enabled again */
551 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
552 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
553 env->cpuid_features &= ~CPUID_APIC;
554 s->spurious_vec &= ~APIC_SV_ENABLE;
555 }
556}
557#else /* VBOX */
558PDMBOTHCBDECL(void) apicSetBase(PPDMDEVINS pDevIns, uint64_t val)
559{
560 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
561 APICState *s = getLapic(dev);
562 Log(("cpu_set_apic_base: %016RX64\n", val));
563
564 /** @todo: do we need to lock here ? */
565 /* APIC_LOCK_VOID(dev, VERR_INTERNAL_ERROR); */
566 /** @todo If this change is valid immediately, then we should change the MMIO registration! */
567 /* We cannot change if this CPU is BSP or not by writing to MSR - it's hardwired */
568 PDMAPICVERSION oldMode = getApicMode(s);
569 s->apicbase =
570 (val & 0xfffff000) | /* base */
571 (val & getApicEnableBits(dev)) | /* mode */
572 (s->apicbase & MSR_IA32_APICBASE_BSP) /* keep BSP bit */;
573 PDMAPICVERSION newMode = getApicMode(s);
574
575 if (oldMode != newMode)
576 {
577 switch (newMode)
578 {
579 case PDMAPICVERSION_NONE:
580 {
581 s->spurious_vec &= ~APIC_SV_ENABLE;
582 /* Clear any pending APIC interrupt action flag. */
583 cpuClearInterrupt(dev, s);
584 /** @todo: why do we do that? */
585 dev->CTX_SUFF(pApicHlp)->pfnChangeFeature(pDevIns, PDMAPICVERSION_NONE);
586 break;
587 }
588 case PDMAPICVERSION_APIC:
589 /** @todo: map MMIO ranges, if needed */
590 break;
591 case PDMAPICVERSION_X2APIC:
592 /** @todo: unmap MMIO ranges of this APIC, according to the spec */
593 break;
594 default:
595 break;
596 }
597 }
598 /* APIC_UNLOCK(dev); */
599}
600#endif /* VBOX */
601#ifndef VBOX
602
603uint64_t cpu_get_apic_base(CPUState *env)
604{
605 APICState *s = env->apic_state;
606#ifdef DEBUG_APIC
607 Log(("cpu_get_apic_base: %016llx\n", (uint64_t)s->apicbase));
608#endif
609 return s->apicbase;
610}
611
612void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
613{
614 APICState *s = env->apic_state;
615 s->tpr = (val & 0x0f) << 4;
616 apic_update_irq(s);
617}
618
619uint8_t cpu_get_apic_tpr(CPUX86State *env)
620{
621 APICState *s = env->apic_state;
622 return s->tpr >> 4;
623}
624
625static int fls_bit(int value)
626{
627 unsigned int ret = 0;
628
629#ifdef HOST_I386
630 __asm__ __volatile__ ("bsr %1, %0\n" : "+r" (ret) : "rm" (value));
631 return ret;
632#else
633 if (value > 0xffff)
634 value >>= 16, ret = 16;
635 if (value > 0xff)
636 value >>= 8, ret += 8;
637 if (value > 0xf)
638 value >>= 4, ret += 4;
639 if (value > 0x3)
640 value >>= 2, ret += 2;
641 return ret + (value >> 1);
642#endif
643}
644
645static inline void set_bit(uint32_t *tab, int index)
646{
647 int i, mask;
648 i = index >> 5;
649 mask = 1 << (index & 0x1f);
650 tab[i] |= mask;
651}
652
653static inline void reset_bit(uint32_t *tab, int index)
654{
655 int i, mask;
656 i = index >> 5;
657 mask = 1 << (index & 0x1f);
658 tab[i] &= ~mask;
659}
660
661
662#else /* VBOX */
663
664PDMBOTHCBDECL(uint64_t) apicGetBase(PPDMDEVINS pDevIns)
665{
666 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
667 APICState *s = getLapic(dev);
668 Log(("apicGetBase: %016llx\n", (uint64_t)s->apicbase));
669 return s->apicbase;
670}
671
672PDMBOTHCBDECL(void) apicSetTPR(PPDMDEVINS pDevIns, uint8_t val)
673{
674 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
675 APICState *s = getLapic(dev);
676 LogFlow(("apicSetTPR: val=%#x (trp %#x -> %#x)\n", val, s->tpr, (val & 0x0f) << 4));
677 apic_update_tpr(dev, s, (val & 0x0f) << 4);
678}
679
680PDMBOTHCBDECL(uint8_t) apicGetTPR(PPDMDEVINS pDevIns)
681{
682 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
683 APICState *s = getLapic(dev);
684 Log2(("apicGetTPR: returns %#x\n", s->tpr >> 4));
685 return s->tpr >> 4;
686}
687
688PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t u64Value)
689{
690 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
691 int rc = VINF_SUCCESS;
692
693 if (dev->enmVersion < PDMAPICVERSION_X2APIC)
694 return VERR_EM_INTERPRETER;
695
696 uint32_t index = (u32Reg - MSR_IA32_APIC_START) & 0xff;
697
698 APICState* apic = getLapicById(dev, idCpu);
699
700 switch (index)
701 {
702 case 0x02:
703 apic->id = (u64Value >> 24);
704 break;
705 case 0x03:
706 break;
707 case 0x08:
708 apic_update_tpr(dev, apic, u64Value);
709 break;
710 case 0x09: case 0x0a:
711 Log(("apicWriteMSR: write to read-only register %d ignored\n", index));
712 break;
713 case 0x0b: /* EOI */
714 apic_eoi(dev, apic);
715 break;
716 case 0x0d:
717 apic->log_dest = u64Value >> 24;
718 break;
719 case 0x0e:
720 apic->dest_mode = u64Value >> 28;
721 break;
722 case 0x0f:
723 apic->spurious_vec = u64Value & 0x1ff;
724 apic_update_irq(dev, apic);
725 break;
726 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
727 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
728 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
729 case 0x28:
730 Log(("apicWriteMSR: write to read-only register %d ignored\n", index));
731 break;
732
733 case 0x30:
734 /* Here one of the differences with regular APIC: ICR is single 64-bit register */
735 apic->icr[0] = (uint32_t)u64Value;
736 apic->icr[1] = (uint32_t)(u64Value >> 32);
737 rc = apic_deliver(dev, apic, (apic->icr[1] >> 24) & 0xff, (apic->icr[0] >> 11) & 1,
738 (apic->icr[0] >> 8) & 7, (apic->icr[0] & 0xff),
739 (apic->icr[0] >> 14) & 1, (apic->icr[0] >> 15) & 1);
740 break;
741 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
742 {
743 int n = index - 0x32;
744 apic->lvt[n] = u64Value;
745 if (n == APIC_LVT_TIMER)
746 apic_timer_update(dev, apic, TMTimerGet(apic->CTX_SUFF(pTimer)));
747 }
748 break;
749 case 0x38:
750 apic->initial_count = u64Value;
751 apic->initial_count_load_time = TMTimerGet(apic->CTX_SUFF(pTimer));
752 apic_timer_update(dev, apic, apic->initial_count_load_time);
753 break;
754 case 0x39:
755 Log(("apicWriteMSR: write to read-only register %d ignored\n", index));
756 break;
757 case 0x3e:
758 {
759 int v;
760 apic->divide_conf = u64Value & 0xb;
761 v = (apic->divide_conf & 3) | ((apic->divide_conf >> 1) & 4);
762 apic->count_shift = (v + 1) & 7;
763 break;
764 }
765 case 0x3f:
766 {
767 /* Self IPI, see x2APIC book 2.4.5 */
768 int vector = u64Value & 0xff;
769 rc = apic_bus_deliver(dev,
770 1 << getLapicById(dev, idCpu)->id /* Self */,
771 0 /* Delivery mode - fixed */,
772 vector,
773 0 /* Polarity - conform to the bus */,
774 0 /* Trigger mode - edge */);
775 break;
776 }
777 default:
778 AssertMsgFailed(("apicWriteMSR: unknown index %x\n", index));
779 apic->esr |= ESR_ILLEGAL_ADDRESS;
780 break;
781 }
782
783 return rc;
784}
785PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t *pu64Value)
786{
787 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
788
789 if (dev->enmVersion < PDMAPICVERSION_X2APIC)
790 return VERR_EM_INTERPRETER;
791
792 uint32_t index = (u32Reg - MSR_IA32_APIC_START) & 0xff;
793 APICState* apic = getLapicById(dev, idCpu);
794 uint64_t val = 0;
795
796 switch (index)
797 {
798 case 0x02: /* id */
799 val = apic->id << 24;
800 break;
801 case 0x03: /* version */
802 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
803 break;
804 case 0x08:
805 val = apic->tpr;
806 break;
807 case 0x09:
808 val = apic_get_arb_pri(apic);
809 break;
810 case 0x0a:
811 /* ppr */
812 val = apic_get_ppr(apic);
813 break;
814 case 0x0b:
815 val = 0;
816 break;
817 case 0x0d:
818 val = apic->log_dest << 24;
819 break;
820 case 0x0e:
821 /* Bottom 28 bits are always 1 */
822 val = (apic->dest_mode << 28) | 0xfffffff;
823 break;
824 case 0x0f:
825 val = apic->spurious_vec;
826 break;
827 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
828 val = apic->isr[index & 7];
829 break;
830 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
831 val = apic->tmr[index & 7];
832 break;
833 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
834 val = apic->irr[index & 7];
835 break;
836 case 0x28:
837 val = apic->esr;
838 break;
839 case 0x30:
840 /* Here one of the differences with regular APIC: ICR is single 64-bit register */
841 val = ((uint64_t)apic->icr[0x31] << 32) | apic->icr[0x30];
842 break;
843 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
844 val = apic->lvt[index - 0x32];
845 break;
846 case 0x38:
847 val = apic->initial_count;
848 break;
849 case 0x39:
850 val = apic_get_current_count(dev, apic);
851 break;
852 case 0x3e:
853 val = apic->divide_conf;
854 break;
855 case 0x3f:
856 /* Self IPI register is write only */
857 Log(("apicReadMSR: read from write-only register %d ignored\n", index));
858 break;
859 default:
860 AssertMsgFailed(("apicReadMSR: unknown index %x\n", index));
861 apic->esr |= ESR_ILLEGAL_ADDRESS;
862 val = 0;
863 break;
864 }
865 *pu64Value = val;
866 return VINF_SUCCESS;
867}
868
869/**
870 * More or less private interface between IOAPIC, only PDM is responsible
871 * for connecting the two devices.
872 */
873PDMBOTHCBDECL(int) apicBusDeliverCallback(PPDMDEVINS pDevIns, uint8_t u8Dest, uint8_t u8DestMode,
874 uint8_t u8DeliveryMode, uint8_t iVector, uint8_t u8Polarity,
875 uint8_t u8TriggerMode)
876{
877 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
878 LogFlow(("apicBusDeliverCallback: pDevIns=%p u8Dest=%#x u8DestMode=%#x u8DeliveryMode=%#x iVector=%#x u8Polarity=%#x u8TriggerMode=%#x\n",
879 pDevIns, u8Dest, u8DestMode, u8DeliveryMode, iVector, u8Polarity, u8TriggerMode));
880 return apic_bus_deliver(dev, apic_get_delivery_bitmask(dev, u8Dest, u8DestMode),
881 u8DeliveryMode, iVector, u8Polarity, u8TriggerMode);
882}
883
884#endif /* VBOX */
885
886/* return -1 if no bit is set */
887static int get_highest_priority_int(uint32_t *tab)
888{
889 int i;
890 for(i = 7; i >= 0; i--) {
891 if (tab[i] != 0) {
892 return i * 32 + fls_bit(tab[i]);
893 }
894 }
895 return -1;
896}
897
898static int apic_get_ppr(APICState *s)
899{
900 int tpr, isrv, ppr;
901
902 tpr = (s->tpr >> 4);
903 isrv = get_highest_priority_int(s->isr);
904 if (isrv < 0)
905 isrv = 0;
906 isrv >>= 4;
907 if (tpr >= isrv)
908 ppr = s->tpr;
909 else
910 ppr = isrv << 4;
911 return ppr;
912}
913
914static int apic_get_ppr_zero_tpr(APICState *s)
915{
916 int isrv;
917
918 isrv = get_highest_priority_int(s->isr);
919 if (isrv < 0)
920 isrv = 0;
921 return isrv;
922}
923
924static int apic_get_arb_pri(APICState *s)
925{
926 /* XXX: arbitration */
927 return 0;
928}
929
930/* signal the CPU if an irq is pending */
931static bool apic_update_irq(APICDeviceInfo *dev, APICState* s)
932{
933 int irrv, ppr;
934 if (!(s->spurious_vec & APIC_SV_ENABLE))
935#ifdef VBOX
936 {
937 /* Clear any pending APIC interrupt action flag. */
938 cpuClearInterrupt(dev, s);
939 return false;
940 }
941#else
942 return false;
943#endif /* VBOX */
944 irrv = get_highest_priority_int(s->irr);
945 if (irrv < 0)
946 return false;
947 ppr = apic_get_ppr(s);
948 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
949 return false;
950#ifndef VBOX
951 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
952#else
953 cpuSetInterrupt(dev, s);
954 return true;
955#endif
956}
957
958#ifdef VBOX
959
960/* Check if the APIC has a pending interrupt/if a TPR change would active one. */
961PDMBOTHCBDECL(bool) apicHasPendingIrq(PPDMDEVINS pDevIns)
962{
963 int irrv, ppr;
964 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
965 if (!dev)
966 return false;
967 APICState *s = getLapic(dev);
968
969 /*
970 * All our callbacks now come from single IOAPIC, thus locking
971 * seems to be excessive now (@todo: check)
972 */
973 irrv = get_highest_priority_int(s->irr);
974 if (irrv < 0)
975 return false;
976
977 ppr = apic_get_ppr_zero_tpr(s);
978
979 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
980 return false;
981
982 return true;
983}
984
985static void apic_update_tpr(APICDeviceInfo *dev, APICState* s, uint32_t val)
986{
987 bool fIrqIsActive = false;
988 bool fIrqWasActive = false;
989
990 fIrqWasActive = apic_update_irq(dev, s);
991 s->tpr = val;
992 fIrqIsActive = apic_update_irq(dev, s);
993
994 /* If an interrupt is pending and now masked, then clear the FF flag. */
995 if (fIrqWasActive && !fIrqIsActive)
996 {
997 Log(("apic_update_tpr: deactivate interrupt that was masked by the TPR update (%x)\n", val));
998 STAM_COUNTER_INC(&dev->StatClearedActiveIrq);
999 cpuClearInterrupt(dev, s);
1000 }
1001}
1002#endif
1003
1004static void apic_set_irq(APICDeviceInfo *dev, APICState* s, int vector_num, int trigger_mode)
1005{
1006 LogFlow(("apic_set_irq vector=%x, trigger_mode=%x\n", vector_num, trigger_mode));
1007 set_bit(s->irr, vector_num);
1008 if (trigger_mode)
1009 set_bit(s->tmr, vector_num);
1010 else
1011 reset_bit(s->tmr, vector_num);
1012 apic_update_irq(dev, s);
1013}
1014
1015static void apic_eoi(APICDeviceInfo *dev, APICState* s)
1016{
1017 int isrv;
1018 isrv = get_highest_priority_int(s->isr);
1019 if (isrv < 0)
1020 return;
1021 reset_bit(s->isr, isrv);
1022 LogFlow(("apic_eoi isrv=%x\n", isrv));
1023 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
1024 set the remote IRR bit for level triggered interrupts. */
1025 apic_update_irq(dev, s);
1026}
1027
1028#ifndef VBOX
1029static uint32_t apic_get_delivery_bitmask(uint8_t dest, uint8_t dest_mode)
1030#else /* VBOX */
1031static uint32_t apic_get_delivery_bitmask(APICDeviceInfo *dev, uint8_t dest, uint8_t dest_mode)
1032#endif /* VBOX */
1033{
1034 uint32_t mask = 0;
1035
1036 if (dest_mode == 0)
1037 {
1038 if (dest == 0xff)
1039 mask = 0xff;
1040 else
1041 mask = 1 << dest;
1042 }
1043 else
1044 {
1045 APICState *apic = LAPIC_BASE(dev);
1046 uint32_t i;
1047
1048 /* XXX: cluster mode */
1049 for(i = 0; i < dev->cCpus; i++)
1050 {
1051 if (apic->dest_mode == 0xf)
1052 {
1053 if (dest & apic->log_dest)
1054 mask |= (1 << apic->id);
1055 }
1056 else if (apic->dest_mode == 0x0)
1057 {
1058 if ((dest & 0xf0) == (apic->log_dest & 0xf0)
1059 &&
1060 (dest & apic->log_dest & 0x0f))
1061 {
1062 mask |= (1 << i);
1063 }
1064 }
1065 apic++;
1066 }
1067 }
1068
1069 return mask;
1070}
1071
1072#ifdef IN_RING3
1073static void apic_init_ipi(APICDeviceInfo* dev, APICState *s)
1074{
1075 int i;
1076
1077 for(i = 0; i < APIC_LVT_NB; i++)
1078 s->lvt[i] = 1 << 16; /* mask LVT */
1079 s->tpr = 0;
1080 s->spurious_vec = 0xff;
1081 s->log_dest = 0;
1082 s->dest_mode = 0xff;
1083 memset(s->isr, 0, sizeof(s->isr));
1084 memset(s->tmr, 0, sizeof(s->tmr));
1085 memset(s->irr, 0, sizeof(s->irr));
1086 s->esr = 0;
1087 memset(s->icr, 0, sizeof(s->icr));
1088 s->divide_conf = 0;
1089 s->count_shift = 0;
1090 s->initial_count = 0;
1091 s->initial_count_load_time = 0;
1092 s->next_time = 0;
1093
1094#ifdef VBOX
1095 cpuSendInitIpi(dev, s);
1096#endif
1097}
1098
1099/* send a SIPI message to the CPU to start it */
1100static void apic_startup(APICDeviceInfo* dev, APICState *s, int vector_num)
1101{
1102#ifndef VBOX
1103 CPUState *env = s->cpu_env;
1104 if (!env->halted)
1105 return;
1106 env->eip = 0;
1107 cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
1108 0xffff, 0);
1109 env->halted = 0;
1110#else
1111 Log(("[SMP] apic_startup: %d on CPUs %d\n", vector_num, s->phys_id));
1112 cpuSendSipi(dev, s, vector_num);
1113#endif
1114}
1115#endif /* IN_RING3 */
1116
1117static int apic_deliver(APICDeviceInfo* dev, APICState *s,
1118 uint8_t dest, uint8_t dest_mode,
1119 uint8_t delivery_mode, uint8_t vector_num,
1120 uint8_t polarity, uint8_t trigger_mode)
1121{
1122 uint32_t deliver_bitmask = 0;
1123 int dest_shorthand = (s->icr[0] >> 18) & 3;
1124#ifndef VBOX
1125 APICState *apic_iter;
1126#endif /* !VBOX */
1127
1128 LogFlow(("apic_deliver dest=%x dest_mode=%x dest_shorthand=%x delivery_mode=%x vector_num=%x polarity=%x trigger_mode=%x\n", dest, dest_mode, dest_shorthand, delivery_mode, vector_num, polarity, trigger_mode));
1129
1130 switch (dest_shorthand) {
1131 case 0:
1132#ifndef VBOX
1133 deliver_bitmask = apic_get_delivery_bitmask(dest, dest_mode);
1134#else /* VBOX */
1135 deliver_bitmask = apic_get_delivery_bitmask(dev, dest, dest_mode);
1136#endif /* !VBOX */
1137 break;
1138 case 1:
1139 deliver_bitmask = (1 << s->id);
1140 break;
1141 case 2:
1142 deliver_bitmask = 0xffffffff;
1143 break;
1144 case 3:
1145 deliver_bitmask = 0xffffffff & ~(1 << s->id);
1146 break;
1147 }
1148
1149 switch (delivery_mode) {
1150 case APIC_DM_LOWPRI:
1151 /* XXX: serch for focus processor, arbitration */
1152 dest = s->id;
1153
1154 case APIC_DM_INIT:
1155 {
1156 int trig_mode = (s->icr[0] >> 15) & 1;
1157 int level = (s->icr[0] >> 14) & 1;
1158 if (level == 0 && trig_mode == 1) {
1159 foreach_apic(dev, deliver_bitmask,
1160 apic->arb_id = apic->id);
1161#ifndef VBOX
1162 return;
1163#else
1164 return VINF_SUCCESS;
1165#endif
1166 }
1167 }
1168 break;
1169
1170 case APIC_DM_SIPI:
1171#ifndef VBOX
1172 for (apic_iter = first_local_apic; apic_iter != NULL;
1173 apic_iter = apic_iter->next_apic) {
1174 if (deliver_bitmask & (1 << apic_iter->id)) {
1175 /* XXX: SMP support */
1176 /* apic_startup(apic_iter); */
1177 }
1178 }
1179 return;
1180#else
1181# ifdef IN_RING3
1182 foreach_apic(dev, deliver_bitmask,
1183 apic_startup(dev, apic, vector_num));
1184 return VINF_SUCCESS;
1185# else
1186 /* We shall send SIPI only in R3, R0 calls should be
1187 rescheduled to R3 */
1188 return VINF_IOM_HC_MMIO_WRITE;
1189# endif
1190#endif /* !VBOX */
1191 }
1192
1193#ifndef VBOX
1194 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
1195 trigger_mode);
1196#else /* VBOX */
1197 return apic_bus_deliver(dev, deliver_bitmask, delivery_mode, vector_num,
1198 polarity, trigger_mode);
1199#endif /* VBOX */
1200}
1201
1202
1203PDMBOTHCBDECL(int) apicGetInterrupt(PPDMDEVINS pDevIns)
1204{
1205 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1206 /* if the APIC is not installed or enabled, we let the 8259 handle the
1207 IRQs */
1208 if (!dev)
1209 {
1210 Log(("apic_get_interrupt: returns -1 (!s)\n"));
1211 return -1;
1212 }
1213
1214 APIC_LOCK(dev, VERR_INTERNAL_ERROR);
1215
1216 APICState *s = getLapic(dev);
1217 int intno;
1218
1219 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
1220 Log(("apic_get_interrupt: returns -1 (APIC_SV_ENABLE)\n"));
1221 intno = -1;
1222 goto done;
1223 }
1224
1225 /* XXX: spurious IRQ handling */
1226 intno = get_highest_priority_int(s->irr);
1227 if (intno < 0) {
1228 Log(("apic_get_interrupt: returns -1 (irr)\n"));
1229 intno = -1;
1230 goto done;
1231 }
1232 if (s->tpr && (uint32_t)intno <= s->tpr) {
1233 Log(("apic_get_interrupt: returns %d (sp)\n", s->spurious_vec & 0xff));
1234 intno = s->spurious_vec & 0xff;
1235 goto done;
1236 }
1237 reset_bit(s->irr, intno);
1238 set_bit(s->isr, intno);
1239 apic_update_irq(dev, s);
1240 LogFlow(("apic_get_interrupt: returns %d\n", intno));
1241 done:
1242 APIC_UNLOCK(dev);
1243 return intno;
1244}
1245
1246static uint32_t apic_get_current_count(APICDeviceInfo* dev, APICState *s)
1247{
1248 int64_t d;
1249 uint32_t val;
1250#ifndef VBOX
1251 d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
1252 s->count_shift;
1253#else /* VBOX */
1254 d = (TMTimerGet(s->CTX_SUFF(pTimer)) - s->initial_count_load_time) >>
1255 s->count_shift;
1256#endif /* VBOX */
1257 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
1258 /* periodic */
1259 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
1260 } else {
1261 if (d >= s->initial_count)
1262 val = 0;
1263 else
1264 val = s->initial_count - d;
1265 }
1266 return val;
1267}
1268
1269static void apic_timer_update(APICDeviceInfo* dev, APICState *s, int64_t current_time)
1270{
1271 int64_t next_time, d;
1272
1273 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
1274 d = (current_time - s->initial_count_load_time) >>
1275 s->count_shift;
1276 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
1277 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
1278 } else {
1279 if (d >= s->initial_count)
1280 goto no_timer;
1281 d = (uint64_t)s->initial_count + 1;
1282 }
1283 next_time = s->initial_count_load_time + (d << s->count_shift);
1284#ifndef VBOX
1285 qemu_mod_timer(s->timer, next_time);
1286#else
1287 TMTimerSet(s->CTX_SUFF(pTimer), next_time);
1288#endif
1289 s->next_time = next_time;
1290 } else {
1291 no_timer:
1292#ifndef VBOX
1293 qemu_del_timer(s->timer);
1294#else
1295 TMTimerStop(s->CTX_SUFF(pTimer));
1296#endif
1297 }
1298}
1299
1300#ifdef IN_RING3
1301#ifndef VBOX
1302static void apic_timer(void *opaque)
1303{
1304 APICState *s = opaque;
1305#else /* VBOX */
1306static DECLCALLBACK(void) apicTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer)
1307{
1308 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
1309 APICState *s = getLapic(dev);
1310
1311 APIC_LOCK_VOID(dev, VERR_INTERNAL_ERROR);
1312#endif /* VBOX */
1313
1314 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
1315 LogFlow(("apic_timer: trigger irq\n"));
1316 apic_set_irq(dev, s, s->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE);
1317 }
1318 apic_timer_update(dev, s, s->next_time);
1319
1320#ifdef VBOX
1321 APIC_UNLOCK(dev);
1322#endif
1323}
1324#endif /* IN_RING3 */
1325
1326#ifndef VBOX
1327static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
1328{
1329 return 0;
1330}
1331static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
1332{
1333 return 0;
1334}
1335
1336static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1337{
1338}
1339
1340static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1341{
1342}
1343#endif /* !VBOX */
1344
1345
1346#ifndef VBOX
1347static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
1348{
1349 CPUState *env;
1350 APICState *s;
1351#else /* VBOX */
1352static uint32_t apic_mem_readl(APICDeviceInfo* dev, APICState *s, target_phys_addr_t addr)
1353{
1354#endif /* VBOX */
1355 uint32_t val;
1356 int index;
1357
1358#ifndef VBOX
1359 env = cpu_single_env;
1360 if (!env)
1361 return 0;
1362 s = env->apic_state;
1363#endif /* !VBOX */
1364
1365 index = (addr >> 4) & 0xff;
1366 switch(index) {
1367 case 0x02: /* id */
1368 val = s->id << 24;
1369 break;
1370 case 0x03: /* version */
1371 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
1372 break;
1373 case 0x08:
1374 val = s->tpr;
1375 break;
1376 case 0x09:
1377 val = apic_get_arb_pri(s);
1378 break;
1379 case 0x0a:
1380 /* ppr */
1381 val = apic_get_ppr(s);
1382 break;
1383 case 0x0b:
1384 Log(("apic_mem_readl %x %x -> write only returning 0\n", addr, index));
1385 val = 0;
1386 break;
1387 case 0x0d:
1388 val = s->log_dest << 24;
1389 break;
1390 case 0x0e:
1391#ifdef VBOX
1392 /* Bottom 28 bits are always 1 */
1393 val = (s->dest_mode << 28) | 0xfffffff;
1394#else
1395 val = s->dest_mode << 28;
1396#endif
1397 break;
1398 case 0x0f:
1399 val = s->spurious_vec;
1400 break;
1401#ifndef VBOX
1402 case 0x10 ... 0x17:
1403#else /* VBOX */
1404 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
1405#endif /* VBOX */
1406 val = s->isr[index & 7];
1407 break;
1408#ifndef VBOX
1409 case 0x18 ... 0x1f:
1410#else /* VBOX */
1411 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1412#endif /* VBOX */
1413 val = s->tmr[index & 7];
1414 break;
1415#ifndef VBOX
1416 case 0x20 ... 0x27:
1417#else /* VBOX */
1418 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1419#endif /* VBOX */
1420 val = s->irr[index & 7];
1421 break;
1422 case 0x28:
1423 val = s->esr;
1424 break;
1425 case 0x30:
1426 case 0x31:
1427 val = s->icr[index & 1];
1428 break;
1429#ifndef VBOX
1430 case 0x32 ... 0x37:
1431#else /* VBOX */
1432 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1433#endif /* VBOX */
1434 val = s->lvt[index - 0x32];
1435 break;
1436 case 0x38:
1437 val = s->initial_count;
1438 break;
1439 case 0x39:
1440 val = apic_get_current_count(dev, s);
1441 break;
1442 case 0x3e:
1443 val = s->divide_conf;
1444 break;
1445 default:
1446 AssertMsgFailed(("apic_mem_readl: unknown index %x\n", index));
1447 s->esr |= ESR_ILLEGAL_ADDRESS;
1448 val = 0;
1449 break;
1450 }
1451#ifdef DEBUG_APIC
1452 Log(("APIC read: %08x = %08x\n", (uint32_t)addr, val));
1453#endif
1454 return val;
1455}
1456
1457#ifndef VBOX
1458static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1459{
1460 CPUState *env;
1461 APICState *s;
1462#else /* VBOX */
1463static int apic_mem_writel(APICDeviceInfo* dev, APICState *s, target_phys_addr_t addr, uint32_t val)
1464{
1465 int rc = VINF_SUCCESS;
1466#endif /* VBOX */
1467 int index;
1468
1469#ifndef VBOX
1470 env = cpu_single_env;
1471 if (!env)
1472 return;
1473 s = env->apic_state;
1474#endif /* !VBOX */
1475
1476#ifdef DEBUG_APIC
1477 Log(("APIC write: %08x = %08x\n", (uint32_t)addr, val));
1478#endif
1479
1480 index = (addr >> 4) & 0xff;
1481 switch(index) {
1482 case 0x02:
1483 s->id = (val >> 24);
1484 break;
1485 case 0x03:
1486 Log(("apic_mem_writel: write to version register; ignored\n"));
1487 break;
1488 case 0x08:
1489#ifdef VBOX
1490 apic_update_tpr(dev, s, val);
1491#else
1492 s->tpr = val;
1493 apic_update_irq(s);
1494#endif
1495 break;
1496 case 0x09:
1497 case 0x0a:
1498 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
1499 break;
1500 case 0x0b: /* EOI */
1501 apic_eoi(dev, s);
1502 break;
1503 case 0x0d:
1504 s->log_dest = val >> 24;
1505 break;
1506 case 0x0e:
1507 s->dest_mode = val >> 28;
1508 break;
1509 case 0x0f:
1510 s->spurious_vec = val & 0x1ff;
1511 apic_update_irq(dev, s);
1512 break;
1513#ifndef VBOX
1514 case 0x10 ... 0x17:
1515 case 0x18 ... 0x1f:
1516 case 0x20 ... 0x27:
1517 case 0x28:
1518#else
1519 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
1520 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1521 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1522 case 0x28:
1523 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
1524#endif
1525 break;
1526
1527 case 0x30:
1528 s->icr[0] = val;
1529 rc = apic_deliver(dev, s, (s->icr[1] >> 24) & 0xff,
1530 (s->icr[0] >> 11) & 1,
1531 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
1532 (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
1533 break;
1534 case 0x31:
1535 s->icr[1] = val;
1536 break;
1537#ifndef VBOX
1538 case 0x32 ... 0x37:
1539#else /* VBOX */
1540 case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1541#endif /* VBOX */
1542 {
1543 int n = index - 0x32;
1544 s->lvt[n] = val;
1545 if (n == APIC_LVT_TIMER)
1546#ifndef VBOX
1547 apic_timer_update(s, qemu_get_clock(vm_clock));
1548#else /* VBOX */
1549 apic_timer_update(dev, s, TMTimerGet(s->CTX_SUFF(pTimer)));
1550#endif /* VBOX*/
1551 }
1552 break;
1553 case 0x38:
1554 s->initial_count = val;
1555#ifndef VBOX
1556 s->initial_count_load_time = qemu_get_clock(vm_clock);
1557#else /* VBOX */
1558 s->initial_count_load_time = TMTimerGet(s->CTX_SUFF(pTimer));
1559#endif /* VBOX*/
1560 apic_timer_update(dev, s, s->initial_count_load_time);
1561 break;
1562 case 0x39:
1563 Log(("apic_mem_writel: write to read-only register %d ignored\n", index));
1564 break;
1565 case 0x3e:
1566 {
1567 int v;
1568 s->divide_conf = val & 0xb;
1569 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
1570 s->count_shift = (v + 1) & 7;
1571 }
1572 break;
1573 default:
1574 AssertMsgFailed(("apic_mem_writel: unknown index %x\n", index));
1575 s->esr |= ESR_ILLEGAL_ADDRESS;
1576 break;
1577 }
1578#ifdef VBOX
1579 return rc;
1580#endif
1581}
1582
1583#ifdef IN_RING3
1584
1585static void apic_save(QEMUFile *f, void *opaque)
1586{
1587 APICState *s = (APICState*)opaque;
1588 int i;
1589
1590 qemu_put_be32s(f, &s->apicbase);
1591#ifdef VBOX
1592 qemu_put_be32s(f, &s->id);
1593 qemu_put_be32s(f, &s->phys_id);
1594 qemu_put_be32s(f, &s->arb_id);
1595 qemu_put_be32s(f, &s->tpr);
1596#else
1597 qemu_put_8s(f, &s->id);
1598 qemu_put_8s(f, &s->arb_id);
1599 qemu_put_8s(f, &s->tpr);
1600#endif
1601 qemu_put_be32s(f, &s->spurious_vec);
1602 qemu_put_8s(f, &s->log_dest);
1603 qemu_put_8s(f, &s->dest_mode);
1604 for (i = 0; i < 8; i++) {
1605 qemu_put_be32s(f, &s->isr[i]);
1606 qemu_put_be32s(f, &s->tmr[i]);
1607 qemu_put_be32s(f, &s->irr[i]);
1608 }
1609 for (i = 0; i < APIC_LVT_NB; i++) {
1610 qemu_put_be32s(f, &s->lvt[i]);
1611 }
1612 qemu_put_be32s(f, &s->esr);
1613 qemu_put_be32s(f, &s->icr[0]);
1614 qemu_put_be32s(f, &s->icr[1]);
1615 qemu_put_be32s(f, &s->divide_conf);
1616 qemu_put_be32s(f, &s->count_shift);
1617 qemu_put_be32s(f, &s->initial_count);
1618 qemu_put_be64s(f, &s->initial_count_load_time);
1619 qemu_put_be64s(f, &s->next_time);
1620
1621#ifdef VBOX
1622 TMR3TimerSave(s->CTX_SUFF(pTimer), f);
1623#endif
1624}
1625
1626static int apic_load(QEMUFile *f, void *opaque, int version_id)
1627{
1628 APICState *s = (APICState*)opaque;
1629 int i;
1630
1631#ifdef VBOX
1632 if ((version_id < 1) || (version_id > 2))
1633 return -EINVAL;
1634
1635 /* XXX: what if the base changes? (registered memory regions) */
1636 qemu_get_be32s(f, &s->apicbase);
1637
1638 switch (version_id)
1639 {
1640 case 1:
1641 {
1642 uint8_t val = 0;
1643 qemu_get_8s(f, &val);
1644 s->id = val;
1645 /* UP only in old saved states */
1646 s->phys_id = 0;
1647 qemu_get_8s(f, &val);
1648 s->arb_id = val;
1649 break;
1650 }
1651 case 2:
1652 qemu_get_be32s(f, &s->id);
1653 qemu_get_be32s(f, &s->phys_id);
1654 qemu_get_be32s(f, &s->arb_id);
1655 break;
1656 }
1657 qemu_get_be32s(f, &s->tpr);
1658#else
1659 if (version_id != 1)
1660 return -EINVAL;
1661
1662 /* XXX: what if the base changes? (registered memory regions) */
1663 qemu_get_be32s(f, &s->apicbase);
1664 qemu_get_8s(f, &s->id);
1665 qemu_get_8s(f, &s->arb_id);
1666 qemu_get_8s(f, &s->tpr);
1667#endif
1668 qemu_get_be32s(f, &s->spurious_vec);
1669 qemu_get_8s(f, &s->log_dest);
1670 qemu_get_8s(f, &s->dest_mode);
1671 for (i = 0; i < 8; i++) {
1672 qemu_get_be32s(f, &s->isr[i]);
1673 qemu_get_be32s(f, &s->tmr[i]);
1674 qemu_get_be32s(f, &s->irr[i]);
1675 }
1676 for (i = 0; i < APIC_LVT_NB; i++) {
1677 qemu_get_be32s(f, &s->lvt[i]);
1678 }
1679 qemu_get_be32s(f, &s->esr);
1680 qemu_get_be32s(f, &s->icr[0]);
1681 qemu_get_be32s(f, &s->icr[1]);
1682 qemu_get_be32s(f, &s->divide_conf);
1683 qemu_get_be32s(f, (uint32_t *)&s->count_shift);
1684 qemu_get_be32s(f, (uint32_t *)&s->initial_count);
1685 qemu_get_be64s(f, (uint64_t *)&s->initial_count_load_time);
1686 qemu_get_be64s(f, (uint64_t *)&s->next_time);
1687
1688#ifdef VBOX
1689 TMR3TimerLoad(s->CTX_SUFF(pTimer), f);
1690#endif
1691
1692 return VINF_SUCCESS;
1693}
1694#ifndef VBOX
1695static void apic_reset(void *opaque)
1696{
1697 APICState *s = (APICState*)opaque;
1698 apic_init_ipi(s);
1699}
1700#endif
1701
1702#endif /* IN_RING3 */
1703
1704#ifndef VBOX
1705static CPUReadMemoryFunc *apic_mem_read[3] = {
1706 apic_mem_readb,
1707 apic_mem_readw,
1708 apic_mem_readl,
1709};
1710
1711static CPUWriteMemoryFunc *apic_mem_write[3] = {
1712 apic_mem_writeb,
1713 apic_mem_writew,
1714 apic_mem_writel,
1715};
1716
1717int apic_init(CPUState *env)
1718{
1719 APICState *s;
1720
1721 s = qemu_mallocz(sizeof(APICState));
1722 if (!s)
1723 return -1;
1724 env->apic_state = s;
1725 apic_init_ipi(s);
1726 s->id = last_apic_id++;
1727 s->cpu_env = env;
1728 s->apicbase = 0xfee00000 |
1729 (s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
1730
1731 /* XXX: mapping more APICs at the same memory location */
1732 if (apic_io_memory == 0) {
1733 /* NOTE: the APIC is directly connected to the CPU - it is not
1734 on the global memory bus. */
1735 apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
1736 apic_mem_write, NULL);
1737 cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000,
1738 apic_io_memory);
1739 }
1740 s->timer = qemu_new_timer(vm_clock, apic_timer, s);
1741
1742 register_savevm("apic", 0, 1, apic_save, apic_load, s);
1743 qemu_register_reset(apic_reset, s);
1744
1745 s->next_apic = first_local_apic;
1746 first_local_apic = s;
1747
1748 return 0;
1749}
1750#endif /* !VBOX */
1751
1752static void ioapic_service(IOAPICState *s)
1753{
1754 uint8_t i;
1755 uint8_t trig_mode;
1756 uint8_t vector;
1757 uint8_t delivery_mode;
1758 uint32_t mask;
1759 uint64_t entry;
1760 uint8_t dest;
1761 uint8_t dest_mode;
1762 uint8_t polarity;
1763
1764 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1765 mask = 1 << i;
1766 if (s->irr & mask) {
1767 entry = s->ioredtbl[i];
1768 if (!(entry & APIC_LVT_MASKED)) {
1769 trig_mode = ((entry >> 15) & 1);
1770 dest = entry >> 56;
1771 dest_mode = (entry >> 11) & 1;
1772 delivery_mode = (entry >> 8) & 7;
1773 polarity = (entry >> 13) & 1;
1774 if (trig_mode == APIC_TRIGGER_EDGE)
1775 s->irr &= ~mask;
1776 if (delivery_mode == APIC_DM_EXTINT)
1777#ifndef VBOX /* malc: i'm still not so sure about ExtINT delivery */
1778 vector = pic_read_irq(isa_pic);
1779#else /* VBOX */
1780 {
1781 AssertMsgFailed(("Delivery mode ExtINT"));
1782 vector = 0xff; /* incorrect but shuts up gcc. */
1783 }
1784#endif /* VBOX */
1785 else
1786 vector = entry & 0xff;
1787
1788#ifndef VBOX
1789 apic_bus_deliver(apic_get_delivery_bitmask(dest, dest_mode),
1790 delivery_mode, vector, polarity, trig_mode);
1791#else /* VBOX */
1792 int rc = s->CTX_SUFF(pIoApicHlp)->pfnApicBusDeliver(s->CTX_SUFF(pDevIns),
1793 dest,
1794 dest_mode,
1795 delivery_mode,
1796 vector,
1797 polarity,
1798 trig_mode);
1799 /* We must be sure that attempts to reschedule in R3
1800 never get here */
1801 Assert(rc == VINF_SUCCESS);
1802#endif /* VBOX */
1803 }
1804 }
1805 }
1806}
1807
1808#ifdef VBOX
1809static
1810#endif
1811void ioapic_set_irq(void *opaque, int vector, int level)
1812{
1813 IOAPICState *s = (IOAPICState*)opaque;
1814
1815 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
1816 uint32_t mask = 1 << vector;
1817 uint64_t entry = s->ioredtbl[vector];
1818
1819 if ((entry >> 15) & 1) {
1820 /* level triggered */
1821 if (level) {
1822 s->irr |= mask;
1823 ioapic_service(s);
1824#ifdef VBOX
1825 if ((level & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP) {
1826 s->irr &= ~mask;
1827 }
1828#endif
1829 } else {
1830 s->irr &= ~mask;
1831 }
1832 } else {
1833 /* edge triggered */
1834 if (level) {
1835 s->irr |= mask;
1836 ioapic_service(s);
1837 }
1838 }
1839 }
1840}
1841
1842static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
1843{
1844 IOAPICState *s = (IOAPICState*)opaque;
1845 int index;
1846 uint32_t val = 0;
1847
1848 addr &= 0xff;
1849 if (addr == 0x00) {
1850 val = s->ioregsel;
1851 } else if (addr == 0x10) {
1852 switch (s->ioregsel) {
1853 case 0x00:
1854 val = s->id << 24;
1855 break;
1856 case 0x01:
1857 val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
1858 break;
1859 case 0x02:
1860 val = 0;
1861 break;
1862 default:
1863 index = (s->ioregsel - 0x10) >> 1;
1864 if (index >= 0 && index < IOAPIC_NUM_PINS) {
1865 if (s->ioregsel & 1)
1866 val = s->ioredtbl[index] >> 32;
1867 else
1868 val = s->ioredtbl[index] & 0xffffffff;
1869 }
1870 }
1871#ifdef DEBUG_IOAPIC
1872 Log(("I/O APIC read: %08x = %08x\n", s->ioregsel, val));
1873#endif
1874 }
1875 return val;
1876}
1877
1878static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1879{
1880 IOAPICState *s = (IOAPICState*)opaque;
1881 int index;
1882
1883 addr &= 0xff;
1884 if (addr == 0x00) {
1885 s->ioregsel = val;
1886 return;
1887 } else if (addr == 0x10) {
1888#ifdef DEBUG_IOAPIC
1889 Log(("I/O APIC write: %08x = %08x\n", s->ioregsel, val));
1890#endif
1891 switch (s->ioregsel) {
1892 case 0x00:
1893 s->id = (val >> 24) & 0xff;
1894 return;
1895 case 0x01:
1896 case 0x02:
1897 return;
1898 default:
1899 index = (s->ioregsel - 0x10) >> 1;
1900 if (index >= 0 && index < IOAPIC_NUM_PINS) {
1901 if (s->ioregsel & 1) {
1902 s->ioredtbl[index] &= 0xffffffff;
1903 s->ioredtbl[index] |= (uint64_t)val << 32;
1904 } else {
1905#ifdef VBOX
1906 /* According to IOAPIC spec, vectors should be from 0x10 to 0xfe */
1907 uint8_t vec = val & 0xff;
1908 if ((val & APIC_LVT_MASKED) ||
1909 ((vec >= 0x10) && (vec < 0xff)))
1910 {
1911 s->ioredtbl[index] &= ~0xffffffffULL;
1912 s->ioredtbl[index] |= val;
1913 }
1914 else
1915 {
1916 /*
1917 * Linux 2.6 kernels has pretty strange function
1918 * unlock_ExtINT_logic() which writes
1919 * absolutely bogus (all 0) value into the vector
1920 * with pretty vague explanation why.
1921 * So we just ignore such writes.
1922 */
1923 LogRel(("IOAPIC GUEST BUG: bad vector writing %x(sel=%x) to %d\n", val, s->ioregsel, index));
1924 }
1925 }
1926#else
1927 s->ioredtbl[index] &= ~0xffffffffULL;
1928 s->ioredtbl[index] |= val;
1929#endif
1930 ioapic_service(s);
1931 }
1932 }
1933 }
1934}
1935
1936#ifdef IN_RING3
1937
1938static void ioapic_save(QEMUFile *f, void *opaque)
1939{
1940 IOAPICState *s = (IOAPICState*)opaque;
1941 int i;
1942
1943 qemu_put_8s(f, &s->id);
1944 qemu_put_8s(f, &s->ioregsel);
1945 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1946 qemu_put_be64s(f, &s->ioredtbl[i]);
1947 }
1948}
1949
1950static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
1951{
1952 IOAPICState *s = (IOAPICState*)opaque;
1953 int i;
1954
1955 if (version_id != 1)
1956 return -EINVAL;
1957
1958 qemu_get_8s(f, &s->id);
1959 qemu_get_8s(f, &s->ioregsel);
1960 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1961 qemu_get_be64s(f, &s->ioredtbl[i]);
1962 }
1963 return 0;
1964}
1965
1966static void ioapic_reset(void *opaque)
1967{
1968 IOAPICState *s = (IOAPICState*)opaque;
1969#ifdef VBOX
1970 PPDMDEVINSR3 pDevIns = s->pDevInsR3;
1971 PCPDMIOAPICHLPR3 pIoApicHlp = s->pIoApicHlpR3;
1972#endif
1973 int i;
1974
1975 memset(s, 0, sizeof(*s));
1976 for(i = 0; i < IOAPIC_NUM_PINS; i++)
1977 s->ioredtbl[i] = 1 << 16; /* mask LVT */
1978
1979#ifdef VBOX
1980 if (pDevIns)
1981 {
1982 s->pDevInsR3 = pDevIns;
1983 s->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1984 s->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1985 }
1986 if (pIoApicHlp)
1987 {
1988 s->pIoApicHlpR3 = pIoApicHlp;
1989 s->pIoApicHlpRC = s->pIoApicHlpR3->pfnGetRCHelpers(pDevIns);
1990 s->pIoApicHlpR0 = s->pIoApicHlpR3->pfnGetR0Helpers(pDevIns);
1991 }
1992#endif
1993}
1994
1995#endif /* IN_RING3 */
1996
1997#ifndef VBOX
1998static CPUReadMemoryFunc *ioapic_mem_read[3] = {
1999 ioapic_mem_readl,
2000 ioapic_mem_readl,
2001 ioapic_mem_readl,
2002};
2003
2004static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
2005 ioapic_mem_writel,
2006 ioapic_mem_writel,
2007 ioapic_mem_writel,
2008};
2009
2010IOAPICState *ioapic_init(void)
2011{
2012 IOAPICState *s;
2013 int io_memory;
2014
2015 s = qemu_mallocz(sizeof(IOAPICState));
2016 if (!s)
2017 return NULL;
2018 ioapic_reset(s);
2019 s->id = last_apic_id++;
2020
2021 io_memory = cpu_register_io_memory(0, ioapic_mem_read,
2022 ioapic_mem_write, s);
2023 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
2024
2025 register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s);
2026 qemu_register_reset(ioapic_reset, s);
2027
2028 return s;
2029}
2030#endif /* !VBOX */
2031
2032/* LAPIC */
2033PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2034{
2035 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2036 APICState *s = getLapic(dev);
2037
2038 Log(("apicMMIORead CPU%d at %llx\n", s->phys_id, (uint64_t)GCPhysAddr));
2039
2040 /** @todo: add LAPIC range validity checks (different LAPICs can theoretically have
2041 different physical addresses, see #3092) */
2042
2043 STAM_COUNTER_INC(&CTXSUFF(dev->StatMMIORead));
2044 switch (cb)
2045 {
2046 case 1:
2047 *(uint8_t *)pv = 0;
2048 break;
2049
2050 case 2:
2051 *(uint16_t *)pv = 0;
2052 break;
2053
2054 case 4:
2055 {
2056#if 0 /** @note experimental */
2057#ifndef IN_RING3
2058 uint32_t index = (GCPhysAddr >> 4) & 0xff;
2059
2060 if ( index == 0x08 /* TPR */
2061 && ++s->ulTPRPatchAttempts < APIC_MAX_PATCH_ATTEMPTS)
2062 {
2063#ifdef IN_RC
2064 pDevIns->pDevHlpGC->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, &s->tpr);
2065#else
2066 RTGCPTR pDevInsGC = PDMINS2DATA_GCPTR(pDevIns);
2067 pDevIns->pDevHlpR0->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, pDevIns + RT_OFFSETOF(APICState, tpr));
2068#endif
2069 return VINF_PATM_HC_MMIO_PATCH_READ;
2070 }
2071#endif
2072#endif /* experimental */
2073 APIC_LOCK(dev, VINF_IOM_HC_MMIO_READ);
2074 *(uint32_t *)pv = apic_mem_readl(dev, s, GCPhysAddr);
2075 APIC_UNLOCK(dev);
2076 break;
2077 }
2078 default:
2079 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
2080 return VERR_INTERNAL_ERROR;
2081 }
2082 return VINF_SUCCESS;
2083}
2084
2085PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2086{
2087 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2088 APICState *s = getLapic(dev);
2089
2090 Log(("apicMMIOWrite CPU%d at %llx\n", s->phys_id, (uint64_t)GCPhysAddr));
2091
2092 /** @todo: add LAPIC range validity checks (multiple LAPICs can theoretically have
2093 different physical addresses, see #3092) */
2094
2095 STAM_COUNTER_INC(&CTXSUFF(dev->StatMMIOWrite));
2096 switch (cb)
2097 {
2098 case 1:
2099 case 2:
2100 /* ignore */
2101 break;
2102
2103 case 4:
2104 {
2105 int rc;
2106 APIC_LOCK(dev, VINF_IOM_HC_MMIO_WRITE);
2107 rc = apic_mem_writel(dev, s, GCPhysAddr, *(uint32_t *)pv);
2108 APIC_UNLOCK(dev);
2109 return rc;
2110 }
2111
2112 default:
2113 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
2114 return VERR_INTERNAL_ERROR;
2115 }
2116 return VINF_SUCCESS;
2117}
2118
2119#ifdef IN_RING3
2120
2121/**
2122 * @copydoc FNSSMDEVSAVEEXEC
2123 */
2124static DECLCALLBACK(int) apicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
2125{
2126 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2127
2128 /* save all APICs data, @todo: is it correct? */
2129 foreach_apic(dev, 0xffffffff, apic_save(pSSMHandle, apic));
2130
2131 return VINF_SUCCESS;
2132}
2133
2134/**
2135 * @copydoc FNSSMDEVLOADEXEC
2136 */
2137static DECLCALLBACK(int) apicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
2138{
2139 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2140 /* load all APICs data, @todo: is it correct? */
2141 foreach_apic(dev, 0xffffffff,
2142 if (apic_load(pSSMHandle, apic, u32Version))
2143 {
2144 AssertFailed();
2145 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2146 }
2147 );
2148 return VINF_SUCCESS;
2149}
2150
2151/**
2152 * @copydoc FNPDMDEVRESET
2153 */
2154static DECLCALLBACK(void) apicReset(PPDMDEVINS pDevIns)
2155{
2156 APICDeviceInfo* dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2157 APICState *s = getLapic(dev);
2158
2159 APIC_LOCK_VOID(dev, VERR_INTERNAL_ERROR);
2160
2161 TMTimerStop(s->CTX_SUFF(pTimer));
2162
2163 apic_init_ipi(dev, s);
2164 /* malc, I've removed the initing duplicated in apic_init_ipi(). This
2165 * arb_id was left over.. */
2166 s->arb_id = 0;
2167 /* Reset should re-enable the APIC. */
2168 s->apicbase = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2169 if (s->phys_id == 0)
2170 s->apicbase |= MSR_IA32_APICBASE_BSP;
2171 dev->pApicHlpR3->pfnChangeFeature(dev->pDevInsR3, dev->enmVersion);
2172 /* Clear any pending APIC interrupt action flag. */
2173 cpuClearInterrupt(dev, s);
2174 APIC_UNLOCK(dev);
2175}
2176
2177/**
2178 * @copydoc FNPDMDEVRELOCATE
2179 */
2180static DECLCALLBACK(void) apicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2181{
2182 APICDeviceInfo *dev = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2183 dev->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2184 dev->pApicHlpRC = dev->pApicHlpR3->pfnGetRCHelpers(pDevIns);
2185 dev->pLapicsRC = MMHyperR3ToRC(PDMDevHlpGetVM(pDevIns), dev->pLapicsR3);
2186 foreach_apic(dev, 0xffffffff,
2187 apic->pTimerRC = TMTimerRCPtr(apic->CTX_SUFF(pTimer)));
2188}
2189
2190DECLINLINE(void) initApicData(APICState* apic, uint8_t id)
2191{
2192 int i;
2193 memset(apic, 0, sizeof(*apic));
2194 apic->apicbase = UINT32_C(0xfee00000) | MSR_IA32_APICBASE_ENABLE;
2195 /* Mark first CPU as BSP */
2196 if (id == 0)
2197 apic->apicbase |= MSR_IA32_APICBASE_BSP;
2198 for (i = 0; i < APIC_LVT_NB; i++)
2199 apic->lvt[i] = 1 << 16; /* mask LVT */
2200 apic->spurious_vec = 0xff;
2201 apic->phys_id = apic->id = id;
2202}
2203
2204/**
2205 * @copydoc FNPDMDEVCONSTRUCT
2206 */
2207static DECLCALLBACK(int) apicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
2208{
2209 PDMAPICREG ApicReg;
2210 int rc;
2211 uint32_t i;
2212 bool fIOAPIC;
2213 bool fGCEnabled;
2214 bool fR0Enabled;
2215 APICDeviceInfo *pThis = PDMINS_2_DATA(pDevIns, APICDeviceInfo *);
2216 uint32_t cCpus;
2217 APICState *apic;
2218
2219 /*
2220 * Only single device instance.
2221 */
2222 Assert(iInstance == 0);
2223
2224 /*
2225 * Validate configuration.
2226 */
2227 if (!CFGMR3AreValuesValid(pCfgHandle,
2228 "IOAPIC\0"
2229 "GCEnabled\0"
2230 "R0Enabled\0"
2231 "NumCPUs\0"))
2232 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2233
2234 rc = CFGMR3QueryBoolDef(pCfgHandle, "IOAPIC", &fIOAPIC, true);
2235 if (RT_FAILURE(rc))
2236 return PDMDEV_SET_ERROR(pDevIns, rc,
2237 N_("Configuration error: Failed to read \"IOAPIC\""));
2238
2239 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
2240 if (RT_FAILURE(rc))
2241 return PDMDEV_SET_ERROR(pDevIns, rc,
2242 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2243
2244 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
2245 if (RT_FAILURE(rc))
2246 return PDMDEV_SET_ERROR(pDevIns, rc,
2247 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2248
2249 rc = CFGMR3QueryU32Def(pCfgHandle, "NumCPUs", &cCpus, 1);
2250 if (RT_FAILURE(rc))
2251 return PDMDEV_SET_ERROR(pDevIns, rc,
2252 N_("Configuration error: Failed to query integer value \"NumCPUs\""));
2253
2254 Log(("APIC: cCpus=%d fR0Enabled=%RTbool fGCEnabled=%RTbool fIOAPIC=%RTbool\n", cCpus, fR0Enabled, fGCEnabled, fIOAPIC));
2255
2256 /*
2257 * Init the data.
2258 */
2259 pThis->pDevInsR3 = pDevIns;
2260 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2261 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2262 pThis->cCpus = cCpus;
2263 /* Use PDMAPICVERSION_X2APIC to activate x2APIC mode */
2264 pThis->enmVersion = PDMAPICVERSION_APIC;
2265
2266 PVM pVM = PDMDevHlpGetVM(pDevIns);
2267 /*
2268 * We are not freeing this memory, as it's automatically released when guest exits.
2269 */
2270 rc = MMHyperAlloc(pVM, cCpus*sizeof(APICState), 1, MM_TAG_PDM_DEVICE_USER, (void **)&pThis->pLapicsR3);
2271 if (RT_FAILURE(rc))
2272 return VERR_NO_MEMORY;
2273 pThis->pLapicsR0 = MMHyperR3ToR0(pVM, pThis->pLapicsR3);
2274 pThis->pLapicsRC = MMHyperR3ToRC(pVM, pThis->pLapicsR3);
2275
2276 for (i = 0, apic = LAPIC_BASE(pThis); i < cCpus; i++)
2277 {
2278 initApicData(apic, i);
2279 apic++;
2280 }
2281
2282 /*
2283 * Register the APIC.
2284 */
2285 ApicReg.u32Version = PDM_APICREG_VERSION;
2286 ApicReg.pfnGetInterruptR3 = apicGetInterrupt;
2287 ApicReg.pfnHasPendingIrqR3 = apicHasPendingIrq;
2288 ApicReg.pfnSetBaseR3 = apicSetBase;
2289 ApicReg.pfnGetBaseR3 = apicGetBase;
2290 ApicReg.pfnSetTPRR3 = apicSetTPR;
2291 ApicReg.pfnGetTPRR3 = apicGetTPR;
2292 ApicReg.pfnWriteMSRR3 = apicWriteMSR;
2293 ApicReg.pfnReadMSRR3 = apicReadMSR;
2294 ApicReg.pfnBusDeliverR3 = apicBusDeliverCallback;
2295 if (fGCEnabled) {
2296 ApicReg.pszGetInterruptRC = "apicGetInterrupt";
2297 ApicReg.pszHasPendingIrqRC = "apicHasPendingIrq";
2298 ApicReg.pszSetBaseRC = "apicSetBase";
2299 ApicReg.pszGetBaseRC = "apicGetBase";
2300 ApicReg.pszSetTPRRC = "apicSetTPR";
2301 ApicReg.pszGetTPRRC = "apicGetTPR";
2302 ApicReg.pszWriteMSRRC = "apicWriteMSR";
2303 ApicReg.pszReadMSRRC = "apicReadMSR";
2304 ApicReg.pszBusDeliverRC = "apicBusDeliverCallback";
2305 } else {
2306 ApicReg.pszGetInterruptRC = NULL;
2307 ApicReg.pszHasPendingIrqRC = NULL;
2308 ApicReg.pszSetBaseRC = NULL;
2309 ApicReg.pszGetBaseRC = NULL;
2310 ApicReg.pszSetTPRRC = NULL;
2311 ApicReg.pszGetTPRRC = NULL;
2312 ApicReg.pszWriteMSRRC = NULL;
2313 ApicReg.pszReadMSRRC = NULL;
2314 ApicReg.pszBusDeliverRC = NULL;
2315 }
2316 if (fR0Enabled) {
2317 ApicReg.pszGetInterruptR0 = "apicGetInterrupt";
2318 ApicReg.pszHasPendingIrqR0 = "apicHasPendingIrq";
2319 ApicReg.pszSetBaseR0 = "apicSetBase";
2320 ApicReg.pszGetBaseR0 = "apicGetBase";
2321 ApicReg.pszSetTPRR0 = "apicSetTPR";
2322 ApicReg.pszGetTPRR0 = "apicGetTPR";
2323 ApicReg.pszWriteMSRR0 = "apicWriteMSR";
2324 ApicReg.pszReadMSRR0 = "apicReadMSR";
2325 ApicReg.pszBusDeliverR0 = "apicBusDeliverCallback";
2326 } else {
2327 ApicReg.pszGetInterruptR0 = NULL;
2328 ApicReg.pszHasPendingIrqR0 = NULL;
2329 ApicReg.pszSetBaseR0 = NULL;
2330 ApicReg.pszGetBaseR0 = NULL;
2331 ApicReg.pszSetTPRR0 = NULL;
2332 ApicReg.pszGetTPRR0 = NULL;
2333 ApicReg.pszWriteMSRR0 = NULL;
2334 ApicReg.pszReadMSRR0 = NULL;
2335 ApicReg.pszBusDeliverR0 = NULL;
2336 }
2337
2338 Assert(pDevIns->pDevHlpR3->pfnAPICRegister);
2339 rc = pDevIns->pDevHlpR3->pfnAPICRegister(pDevIns, &ApicReg, &pThis->pApicHlpR3);
2340 if (RT_FAILURE(rc))
2341 {
2342 AssertLogRelMsgFailed(("APICRegister -> %Rrc\n", rc));
2343 return rc;
2344 }
2345
2346 /*
2347 * The the CPUID feature bit.
2348 */
2349 uint32_t u32Eax, u32Ebx, u32Ecx, u32Edx;
2350 PDMDevHlpGetCpuId(pDevIns, 0, &u32Eax, &u32Ebx, &u32Ecx, &u32Edx);
2351 if (u32Eax >= 1)
2352 {
2353 if ( fIOAPIC /* If IOAPIC is enabled, enable Local APIC in any case */
2354 || ( u32Ebx == X86_CPUID_VENDOR_INTEL_EBX
2355 && u32Ecx == X86_CPUID_VENDOR_INTEL_ECX
2356 && u32Edx == X86_CPUID_VENDOR_INTEL_EDX /* GenuineIntel */)
2357 || ( u32Ebx == X86_CPUID_VENDOR_AMD_EBX
2358 && u32Ecx == X86_CPUID_VENDOR_AMD_ECX
2359 && u32Edx == X86_CPUID_VENDOR_AMD_EDX /* AuthenticAMD */))
2360 {
2361 LogRel(("Activating Local APIC\n"));
2362 pThis->pApicHlpR3->pfnChangeFeature(pDevIns, pThis->enmVersion);
2363 }
2364 }
2365
2366 /*
2367 * Register the MMIO range.
2368 * @todo: may need to rethink for cases when different LAPICs mapped to different address
2369 * (see IA32_APIC_BASE_MSR)
2370 */
2371 rc = PDMDevHlpMMIORegister(pDevIns, LAPIC_BASE(pThis)->apicbase & ~0xfff, 0x1000, pThis,
2372 apicMMIOWrite, apicMMIORead, NULL, "APIC Memory");
2373 if (RT_FAILURE(rc))
2374 return rc;
2375
2376 if (fGCEnabled) {
2377 pThis->pApicHlpRC = pThis->pApicHlpR3->pfnGetRCHelpers(pDevIns);
2378
2379 rc = PDMDevHlpMMIORegisterGC(pDevIns, LAPIC_BASE(pThis)->apicbase & ~0xfff, 0x1000, 0,
2380 "apicMMIOWrite", "apicMMIORead", NULL);
2381 if (RT_FAILURE(rc))
2382 return rc;
2383 }
2384
2385 if (fR0Enabled) {
2386 pThis->pApicHlpR0 = pThis->pApicHlpR3->pfnGetR0Helpers(pDevIns);
2387
2388 rc = PDMDevHlpMMIORegisterR0(pDevIns, LAPIC_BASE(pThis)->apicbase & ~0xfff, 0x1000, 0,
2389 "apicMMIOWrite", "apicMMIORead", NULL);
2390 if (RT_FAILURE(rc))
2391 return rc;
2392 }
2393
2394 /*
2395 * Create the APIC timers.
2396 */
2397 for (i = 0, apic = LAPIC_BASE(pThis); i < cCpus; i++)
2398 {
2399 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, apicTimer,
2400 "APIC Timer", &apic->pTimerR3);
2401 if (RT_FAILURE(rc))
2402 return rc;
2403 apic->pTimerR0 = TMTimerR0Ptr(apic->pTimerR3);
2404 apic->pTimerRC = TMTimerRCPtr(apic->pTimerR3);
2405 apic++;
2406 }
2407
2408 /*
2409 * Saved state.
2410 */
2411 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 2 /* version */,
2412 sizeof(*pThis), NULL, apicSaveExec, NULL, NULL, apicLoadExec, NULL);
2413 if (RT_FAILURE(rc))
2414 return rc;
2415
2416#ifdef VBOX_WITH_STATISTICS
2417 /*
2418 * Statistics.
2419 */
2420 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in GC.");
2421 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in HC.");
2422 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in GC.");
2423 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/APIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in HC.");
2424 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveIrq, STAMTYPE_COUNTER, "/PDM/APIC/Masked/ActiveIRQ", STAMUNIT_OCCURENCES, "Number of cleared irqs.");
2425#endif
2426
2427 return VINF_SUCCESS;
2428}
2429
2430
2431/**
2432 * APIC device registration structure.
2433 */
2434const PDMDEVREG g_DeviceAPIC =
2435{
2436 /* u32Version */
2437 PDM_DEVREG_VERSION,
2438 /* szDeviceName */
2439 "apic",
2440 /* szRCMod */
2441 "VBoxDD2GC.gc",
2442 /* szR0Mod */
2443 "VBoxDD2R0.r0",
2444 /* pszDescription */
2445 "Advanced Programmable Interrupt Controller (APIC) Device",
2446 /* fFlags */
2447 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2448 /* fClass */
2449 PDM_DEVREG_CLASS_PIC,
2450 /* cMaxInstances */
2451 1,
2452 /* cbInstance */
2453 sizeof(APICState),
2454 /* pfnConstruct */
2455 apicConstruct,
2456 /* pfnDestruct */
2457 NULL,
2458 /* pfnRelocate */
2459 apicRelocate,
2460 /* pfnIOCtl */
2461 NULL,
2462 /* pfnPowerOn */
2463 NULL,
2464 /* pfnReset */
2465 apicReset,
2466 /* pfnSuspend */
2467 NULL,
2468 /* pfnResume */
2469 NULL,
2470 /* pfnAttach */
2471 NULL,
2472 /* pfnDetach */
2473 NULL,
2474 /* pfnQueryInterface. */
2475 NULL,
2476 /* pfnInitComplete */
2477 NULL,
2478 /* pfnPowerOff */
2479 NULL,
2480 /* pfnSoftReset */
2481 NULL,
2482 /* u32VersionEnd */
2483 PDM_DEVREG_VERSION
2484};
2485
2486#endif /* IN_RING3 */
2487
2488
2489/* IOAPIC */
2490
2491PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2492{
2493 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2494 IOAPIC_LOCK(s, VINF_IOM_HC_MMIO_READ);
2495
2496 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIORead));
2497 switch (cb)
2498 {
2499 case 1:
2500 *(uint8_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
2501 break;
2502
2503 case 2:
2504 *(uint16_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
2505 break;
2506
2507 case 4:
2508 *(uint32_t *)pv = ioapic_mem_readl(s, GCPhysAddr);
2509 break;
2510
2511 default:
2512 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
2513 IOAPIC_UNLOCK(s);
2514 return VERR_INTERNAL_ERROR;
2515 }
2516 IOAPIC_UNLOCK(s);
2517 return VINF_SUCCESS;
2518}
2519
2520PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
2521{
2522 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2523
2524 STAM_COUNTER_INC(&CTXSUFF(s->StatMMIOWrite));
2525 switch (cb)
2526 {
2527 case 1:
2528 case 2:
2529 case 4:
2530 IOAPIC_LOCK(s, VINF_IOM_HC_MMIO_WRITE);
2531 ioapic_mem_writel(s, GCPhysAddr, *(uint32_t *)pv);
2532 IOAPIC_UNLOCK(s);
2533 break;
2534
2535 default:
2536 AssertReleaseMsgFailed(("cb=%d\n", cb)); /* for now we assume simple accesses. */
2537 return VERR_INTERNAL_ERROR;
2538 }
2539 return VINF_SUCCESS;
2540}
2541
2542PDMBOTHCBDECL(void) ioapicSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel)
2543{
2544 IOAPICState *pThis = PDMINS_2_DATA(pDevIns, IOAPICState *);
2545 STAM_COUNTER_INC(&pThis->CTXSUFF(StatSetIrq));
2546 LogFlow(("ioapicSetIrq: iIrq=%d iLevel=%d\n", iIrq, iLevel));
2547 ioapic_set_irq(pThis, iIrq, iLevel);
2548}
2549
2550
2551#ifdef IN_RING3
2552
2553/**
2554 * @copydoc FNSSMDEVSAVEEXEC
2555 */
2556static DECLCALLBACK(int) ioapicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
2557{
2558 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2559 ioapic_save(pSSMHandle, s);
2560 return VINF_SUCCESS;
2561}
2562
2563/**
2564 * @copydoc FNSSMDEVLOADEXEC
2565 */
2566static DECLCALLBACK(int) ioapicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
2567{
2568 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2569
2570 if (ioapic_load(pSSMHandle, s, u32Version)) {
2571 AssertFailed();
2572 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2573 }
2574
2575 return VINF_SUCCESS;
2576}
2577
2578/**
2579 * @copydoc FNPDMDEVRESET
2580 */
2581static DECLCALLBACK(void) ioapicReset(PPDMDEVINS pDevIns)
2582{
2583 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2584 s->pIoApicHlpR3->pfnLock(pDevIns, VERR_INTERNAL_ERROR);
2585 ioapic_reset(s);
2586 IOAPIC_UNLOCK(s);
2587}
2588
2589/**
2590 * @copydoc FNPDMDEVRELOCATE
2591 */
2592static DECLCALLBACK(void) ioapicRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
2593{
2594 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2595 s->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2596 s->pIoApicHlpRC = s->pIoApicHlpR3->pfnGetRCHelpers(pDevIns);
2597}
2598
2599/**
2600 * @copydoc FNPDMDEVCONSTRUCT
2601 */
2602static DECLCALLBACK(int) ioapicConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
2603{
2604 IOAPICState *s = PDMINS_2_DATA(pDevIns, IOAPICState *);
2605 PDMIOAPICREG IoApicReg;
2606 bool fGCEnabled;
2607 bool fR0Enabled;
2608 int rc;
2609
2610 Assert(iInstance == 0);
2611
2612 /*
2613 * Validate and read the configuration.
2614 */
2615 if (!CFGMR3AreValuesValid(pCfgHandle, "GCEnabled\0" "R0Enabled\0"))
2616 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
2617
2618 rc = CFGMR3QueryBoolDef(pCfgHandle, "GCEnabled", &fGCEnabled, true);
2619 if (RT_FAILURE(rc))
2620 return PDMDEV_SET_ERROR(pDevIns, rc,
2621 N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
2622
2623 rc = CFGMR3QueryBoolDef(pCfgHandle, "R0Enabled", &fR0Enabled, true);
2624 if (RT_FAILURE(rc))
2625 return PDMDEV_SET_ERROR(pDevIns, rc,
2626 N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
2627 Log(("IOAPIC: fR0Enabled=%RTbool fGCEnabled=%RTbool\n", fR0Enabled, fGCEnabled));
2628
2629 /*
2630 * Initialize the state data.
2631 */
2632 s->pDevInsR3 = pDevIns;
2633 s->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
2634 s->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
2635 ioapic_reset(s);
2636 s->id = 0;
2637
2638 /*
2639 * Register the IOAPIC and get helpers.
2640 */
2641 IoApicReg.u32Version = PDM_IOAPICREG_VERSION;
2642 IoApicReg.pfnSetIrqR3 = ioapicSetIrq;
2643 IoApicReg.pszSetIrqRC = fGCEnabled ? "ioapicSetIrq" : NULL;
2644 IoApicReg.pszSetIrqR0 = fR0Enabled ? "ioapicSetIrq" : NULL;
2645 rc = pDevIns->pDevHlpR3->pfnIOAPICRegister(pDevIns, &IoApicReg, &s->pIoApicHlpR3);
2646 if (RT_FAILURE(rc))
2647 {
2648 AssertMsgFailed(("IOAPICRegister -> %Rrc\n", rc));
2649 return rc;
2650 }
2651
2652 /*
2653 * Register MMIO callbacks and saved state.
2654 */
2655 rc = PDMDevHlpMMIORegister(pDevIns, 0xfec00000, 0x1000, s,
2656 ioapicMMIOWrite, ioapicMMIORead, NULL, "I/O APIC Memory");
2657 if (RT_FAILURE(rc))
2658 return rc;
2659
2660 if (fGCEnabled) {
2661 s->pIoApicHlpRC = s->pIoApicHlpR3->pfnGetRCHelpers(pDevIns);
2662
2663 rc = PDMDevHlpMMIORegisterGC(pDevIns, 0xfec00000, 0x1000, 0,
2664 "ioapicMMIOWrite", "ioapicMMIORead", NULL);
2665 if (RT_FAILURE(rc))
2666 return rc;
2667 }
2668
2669 if (fR0Enabled) {
2670 s->pIoApicHlpR0 = s->pIoApicHlpR3->pfnGetR0Helpers(pDevIns);
2671
2672 rc = PDMDevHlpMMIORegisterR0(pDevIns, 0xfec00000, 0x1000, 0,
2673 "ioapicMMIOWrite", "ioapicMMIORead", NULL);
2674 if (RT_FAILURE(rc))
2675 return rc;
2676 }
2677
2678 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1 /* version */,
2679 sizeof(*s), NULL, ioapicSaveExec, NULL, NULL, ioapicLoadExec, NULL);
2680 if (RT_FAILURE(rc))
2681 return rc;
2682
2683#ifdef VBOX_WITH_STATISTICS
2684 /*
2685 * Statistics.
2686 */
2687 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in GC.");
2688 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in HC.");
2689 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in GC.");
2690 PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in HC.");
2691 PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqGC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in GC.");
2692 PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqHC, STAMTYPE_COUNTER, "/PDM/IOAPIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in HC.");
2693#endif
2694
2695 return VINF_SUCCESS;
2696}
2697
2698/**
2699 * IO APIC device registration structure.
2700 */
2701const PDMDEVREG g_DeviceIOAPIC =
2702{
2703 /* u32Version */
2704 PDM_DEVREG_VERSION,
2705 /* szDeviceName */
2706 "ioapic",
2707 /* szRCMod */
2708 "VBoxDD2GC.gc",
2709 /* szR0Mod */
2710 "VBoxDD2R0.r0",
2711 /* pszDescription */
2712 "I/O Advanced Programmable Interrupt Controller (IO-APIC) Device",
2713 /* fFlags */
2714 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2715 /* fClass */
2716 PDM_DEVREG_CLASS_PIC,
2717 /* cMaxInstances */
2718 1,
2719 /* cbInstance */
2720 sizeof(IOAPICState),
2721 /* pfnConstruct */
2722 ioapicConstruct,
2723 /* pfnDestruct */
2724 NULL,
2725 /* pfnRelocate */
2726 ioapicRelocate,
2727 /* pfnIOCtl */
2728 NULL,
2729 /* pfnPowerOn */
2730 NULL,
2731 /* pfnReset */
2732 ioapicReset,
2733 /* pfnSuspend */
2734 NULL,
2735 /* pfnResume */
2736 NULL,
2737 /* pfnAttach */
2738 NULL,
2739 /* pfnDetach */
2740 NULL,
2741 /* pfnQueryInterface. */
2742 NULL,
2743 /* pfnInitComplete */
2744 NULL,
2745 /* pfnPowerOff */
2746 NULL,
2747 /* pfnSoftReset */
2748 NULL,
2749 /* u32VersionEnd */
2750 PDM_DEVREG_VERSION
2751};
2752
2753#endif /* IN_RING3 */
2754#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
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