4 * Copyright (c) 2004-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22 #include "qemu-timer.h"
23 #include "host-utils.h"
29 /* APIC Local Vector Table */
30 #define APIC_LVT_TIMER 0
31 #define APIC_LVT_THERMAL 1
32 #define APIC_LVT_PERFORM 2
33 #define APIC_LVT_LINT0 3
34 #define APIC_LVT_LINT1 4
35 #define APIC_LVT_ERROR 5
38 /* APIC delivery modes */
39 #define APIC_DM_FIXED 0
40 #define APIC_DM_LOWPRI 1
43 #define APIC_DM_INIT 5
44 #define APIC_DM_SIPI 6
45 #define APIC_DM_EXTINT 7
47 /* APIC destination mode */
48 #define APIC_DESTMODE_FLAT 0xf
49 #define APIC_DESTMODE_CLUSTER 1
51 #define APIC_TRIGGER_EDGE 0
52 #define APIC_TRIGGER_LEVEL 1
54 #define APIC_LVT_TIMER_PERIODIC (1<<17)
55 #define APIC_LVT_MASKED (1<<16)
56 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
57 #define APIC_LVT_REMOTE_IRR (1<<14)
58 #define APIC_INPUT_POLARITY (1<<13)
59 #define APIC_SEND_PENDING (1<<12)
61 #define ESR_ILLEGAL_ADDRESS (1 << 7)
63 #define APIC_SV_ENABLE (1 << 8)
66 #define MAX_APIC_WORDS 8
68 typedef struct APICState
{
74 uint32_t spurious_vec
;
77 uint32_t isr
[8]; /* in service register */
78 uint32_t tmr
[8]; /* trigger mode register */
79 uint32_t irr
[8]; /* interrupt request register */
80 uint32_t lvt
[APIC_LVT_NB
];
81 uint32_t esr
; /* error register */
86 uint32_t initial_count
;
87 int64_t initial_count_load_time
, next_time
;
91 static int apic_io_memory
;
92 static APICState
*local_apics
[MAX_APICS
+ 1];
93 static int last_apic_id
= 0;
94 static int apic_irq_delivered
;
97 static void apic_init_ipi(APICState
*s
);
98 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
99 static void apic_update_irq(APICState
*s
);
100 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
101 uint8_t dest
, uint8_t dest_mode
);
103 /* Find first bit starting from msb */
104 static int fls_bit(uint32_t value
)
106 return 31 - clz32(value
);
109 /* Find first bit starting from lsb */
110 static int ffs_bit(uint32_t value
)
115 static inline void set_bit(uint32_t *tab
, int index
)
119 mask
= 1 << (index
& 0x1f);
123 static inline void reset_bit(uint32_t *tab
, int index
)
127 mask
= 1 << (index
& 0x1f);
131 static inline int get_bit(uint32_t *tab
, int index
)
135 mask
= 1 << (index
& 0x1f);
136 return !!(tab
[i
] & mask
);
139 static void apic_local_deliver(CPUState
*env
, int vector
)
141 APICState
*s
= env
->apic_state
;
142 uint32_t lvt
= s
->lvt
[vector
];
145 if (lvt
& APIC_LVT_MASKED
)
148 switch ((lvt
>> 8) & 7) {
150 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
154 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
158 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
162 trigger_mode
= APIC_TRIGGER_EDGE
;
163 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
164 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
165 trigger_mode
= APIC_TRIGGER_LEVEL
;
166 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
170 void apic_deliver_pic_intr(CPUState
*env
, int level
)
173 apic_local_deliver(env
, APIC_LVT_LINT0
);
175 APICState
*s
= env
->apic_state
;
176 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
178 switch ((lvt
>> 8) & 7) {
180 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
182 reset_bit(s
->irr
, lvt
& 0xff);
185 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
191 #define foreach_apic(apic, deliver_bitmask, code) \
193 int __i, __j, __mask;\
194 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
195 __mask = deliver_bitmask[__i];\
197 for(__j = 0; __j < 32; __j++) {\
198 if (__mask & (1 << __j)) {\
199 apic = local_apics[__i * 32 + __j];\
209 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
210 uint8_t delivery_mode
,
211 uint8_t vector_num
, uint8_t polarity
,
212 uint8_t trigger_mode
)
214 APICState
*apic_iter
;
216 switch (delivery_mode
) {
218 /* XXX: search for focus processor, arbitration */
222 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
223 if (deliver_bitmask
[i
]) {
224 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
229 apic_iter
= local_apics
[d
];
231 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
241 foreach_apic(apic_iter
, deliver_bitmask
,
242 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
246 foreach_apic(apic_iter
, deliver_bitmask
,
247 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
251 /* normal INIT IPI sent to processors */
252 foreach_apic(apic_iter
, deliver_bitmask
,
253 apic_init_ipi(apic_iter
) );
257 /* handled in I/O APIC code */
264 foreach_apic(apic_iter
, deliver_bitmask
,
265 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
268 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
269 uint8_t delivery_mode
, uint8_t vector_num
,
270 uint8_t polarity
, uint8_t trigger_mode
)
272 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
274 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
275 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
279 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
281 APICState
*s
= env
->apic_state
;
283 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
287 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel())
290 s
->apicbase
= (val
& 0xfffff000) |
291 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
292 /* if disabled, cannot be enabled again */
293 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
294 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
295 env
->cpuid_features
&= ~CPUID_APIC
;
296 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
300 uint64_t cpu_get_apic_base(CPUState
*env
)
302 APICState
*s
= env
->apic_state
;
304 printf("cpu_get_apic_base: %016" PRIx64
"\n",
305 s
? (uint64_t)s
->apicbase
: 0);
307 return s
? s
->apicbase
: 0;
310 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
312 APICState
*s
= env
->apic_state
;
315 s
->tpr
= (val
& 0x0f) << 4;
319 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
321 APICState
*s
= env
->apic_state
;
322 return s
? s
->tpr
>> 4 : 0;
325 /* return -1 if no bit is set */
326 static int get_highest_priority_int(uint32_t *tab
)
329 for(i
= 7; i
>= 0; i
--) {
331 return i
* 32 + fls_bit(tab
[i
]);
337 static int apic_get_ppr(APICState
*s
)
342 isrv
= get_highest_priority_int(s
->isr
);
353 static int apic_get_arb_pri(APICState
*s
)
355 /* XXX: arbitration */
359 /* signal the CPU if an irq is pending */
360 static void apic_update_irq(APICState
*s
)
363 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
365 irrv
= get_highest_priority_int(s
->irr
);
368 ppr
= apic_get_ppr(s
);
369 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
371 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
374 void apic_reset_irq_delivered(void)
376 apic_irq_delivered
= 0;
379 int apic_get_irq_delivered(void)
381 return apic_irq_delivered
;
384 void apic_set_irq_delivered(void)
386 apic_irq_delivered
= 1;
389 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
391 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
393 set_bit(s
->irr
, vector_num
);
395 set_bit(s
->tmr
, vector_num
);
397 reset_bit(s
->tmr
, vector_num
);
401 static void apic_eoi(APICState
*s
)
404 isrv
= get_highest_priority_int(s
->isr
);
407 reset_bit(s
->isr
, isrv
);
408 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
409 set the remote IRR bit for level triggered interrupts. */
413 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
414 uint8_t dest
, uint8_t dest_mode
)
416 APICState
*apic_iter
;
419 if (dest_mode
== 0) {
421 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
423 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
424 set_bit(deliver_bitmask
, dest
);
427 /* XXX: cluster mode */
428 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
429 for(i
= 0; i
< MAX_APICS
; i
++) {
430 apic_iter
= local_apics
[i
];
432 if (apic_iter
->dest_mode
== 0xf) {
433 if (dest
& apic_iter
->log_dest
)
434 set_bit(deliver_bitmask
, i
);
435 } else if (apic_iter
->dest_mode
== 0x0) {
436 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
437 (dest
& apic_iter
->log_dest
& 0x0f)) {
438 set_bit(deliver_bitmask
, i
);
447 static void apic_init_ipi(APICState
*s
)
452 s
->spurious_vec
= 0xff;
455 memset(s
->isr
, 0, sizeof(s
->isr
));
456 memset(s
->tmr
, 0, sizeof(s
->tmr
));
457 memset(s
->irr
, 0, sizeof(s
->irr
));
458 for(i
= 0; i
< APIC_LVT_NB
; i
++)
459 s
->lvt
[i
] = 1 << 16; /* mask LVT */
461 memset(s
->icr
, 0, sizeof(s
->icr
));
464 s
->initial_count
= 0;
465 s
->initial_count_load_time
= 0;
468 cpu_reset(s
->cpu_env
);
470 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
) &&
471 (!kvm_enabled() || !qemu_kvm_irqchip_in_kernel()))
472 s
->cpu_env
->halted
= 1;
474 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
476 kvm_apic_init(s
->cpu_env
);
479 /* send a SIPI message to the CPU to start it */
480 static void apic_startup(APICState
*s
, int vector_num
)
482 CPUState
*env
= s
->cpu_env
;
486 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
489 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
490 kvm_update_after_sipi(env
);
493 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
494 uint8_t delivery_mode
, uint8_t vector_num
,
495 uint8_t polarity
, uint8_t trigger_mode
)
497 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
498 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
499 APICState
*apic_iter
;
501 switch (dest_shorthand
) {
503 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
506 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
507 set_bit(deliver_bitmask
, s
->id
);
510 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
513 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
514 reset_bit(deliver_bitmask
, s
->id
);
518 switch (delivery_mode
) {
521 int trig_mode
= (s
->icr
[0] >> 15) & 1;
522 int level
= (s
->icr
[0] >> 14) & 1;
523 if (level
== 0 && trig_mode
== 1) {
524 foreach_apic(apic_iter
, deliver_bitmask
,
525 apic_iter
->arb_id
= apic_iter
->id
);
532 foreach_apic(apic_iter
, deliver_bitmask
,
533 apic_startup(apic_iter
, vector_num
) );
537 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
541 int apic_get_interrupt(CPUState
*env
)
543 APICState
*s
= env
->apic_state
;
546 /* if the APIC is installed or enabled, we let the 8259 handle the
550 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
553 /* XXX: spurious IRQ handling */
554 intno
= get_highest_priority_int(s
->irr
);
557 if (s
->tpr
&& intno
<= s
->tpr
)
558 return s
->spurious_vec
& 0xff;
559 reset_bit(s
->irr
, intno
);
560 set_bit(s
->isr
, intno
);
565 int apic_accept_pic_intr(CPUState
*env
)
567 APICState
*s
= env
->apic_state
;
573 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
575 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
576 (lvt0
& APIC_LVT_MASKED
) == 0)
582 static uint32_t apic_get_current_count(APICState
*s
)
586 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
588 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
590 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
592 if (d
>= s
->initial_count
)
595 val
= s
->initial_count
- d
;
600 static void apic_timer_update(APICState
*s
, int64_t current_time
)
602 int64_t next_time
, d
;
604 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
605 d
= (current_time
- s
->initial_count_load_time
) >>
607 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
608 if (!s
->initial_count
)
610 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
612 if (d
>= s
->initial_count
)
614 d
= (uint64_t)s
->initial_count
+ 1;
616 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
617 qemu_mod_timer(s
->timer
, next_time
);
618 s
->next_time
= next_time
;
621 qemu_del_timer(s
->timer
);
625 static void apic_timer(void *opaque
)
627 APICState
*s
= opaque
;
629 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
630 apic_timer_update(s
, s
->next_time
);
633 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
638 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
643 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
647 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
651 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
658 env
= cpu_single_env
;
663 index
= (addr
>> 4) & 0xff;
668 case 0x03: /* version */
669 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
675 val
= apic_get_arb_pri(s
);
679 val
= apic_get_ppr(s
);
685 val
= s
->log_dest
<< 24;
688 val
= s
->dest_mode
<< 28;
691 val
= s
->spurious_vec
;
694 val
= s
->isr
[index
& 7];
697 val
= s
->tmr
[index
& 7];
700 val
= s
->irr
[index
& 7];
707 val
= s
->icr
[index
& 1];
710 val
= s
->lvt
[index
- 0x32];
713 val
= s
->initial_count
;
716 val
= apic_get_current_count(s
);
719 val
= s
->divide_conf
;
722 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
727 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
732 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
738 env
= cpu_single_env
;
744 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
747 index
= (addr
>> 4) & 0xff;
765 s
->log_dest
= val
>> 24;
768 s
->dest_mode
= val
>> 28;
771 s
->spurious_vec
= val
& 0x1ff;
781 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
782 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
783 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
790 int n
= index
- 0x32;
792 if (n
== APIC_LVT_TIMER
)
793 apic_timer_update(s
, qemu_get_clock(vm_clock
));
797 s
->initial_count
= val
;
798 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
799 apic_timer_update(s
, s
->initial_count_load_time
);
806 s
->divide_conf
= val
& 0xb;
807 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
808 s
->count_shift
= (v
+ 1) & 7;
812 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
817 #ifdef KVM_CAP_IRQCHIP
819 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
821 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
824 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
825 int reg_id
, uint32_t val
)
827 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
830 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
832 struct kvm_lapic_state apic
;
833 struct kvm_lapic_state
*kapic
= &apic
;
836 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
838 s
->id
= kapic_reg(kapic
, 0x2) >> 24;
839 s
->tpr
= kapic_reg(kapic
, 0x8);
840 s
->arb_id
= kapic_reg(kapic
, 0x9);
841 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
842 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
843 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
844 for (i
= 0; i
< 8; i
++) {
845 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
846 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
847 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
849 s
->esr
= kapic_reg(kapic
, 0x28);
850 s
->icr
[0] = kapic_reg(kapic
, 0x30);
851 s
->icr
[1] = kapic_reg(kapic
, 0x31);
852 for (i
= 0; i
< APIC_LVT_NB
; i
++)
853 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
854 s
->initial_count
= kapic_reg(kapic
, 0x38);
855 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
857 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
858 s
->count_shift
= (v
+ 1) & 7;
860 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
861 apic_timer_update(s
, s
->initial_count_load_time
);
864 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
866 struct kvm_lapic_state apic
;
867 struct kvm_lapic_state
*klapic
= &apic
;
870 memset(klapic
, 0, sizeof apic
);
871 kapic_set_reg(klapic
, 0x2, s
->id
<< 24);
872 kapic_set_reg(klapic
, 0x8, s
->tpr
);
873 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
874 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
875 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
876 for (i
= 0; i
< 8; i
++) {
877 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
878 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
879 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
881 kapic_set_reg(klapic
, 0x28, s
->esr
);
882 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
883 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
884 for (i
= 0; i
< APIC_LVT_NB
; i
++)
885 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
886 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
887 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
889 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
894 static void apic_save(QEMUFile
*f
, void *opaque
)
896 APICState
*s
= opaque
;
899 #ifdef KVM_CAP_IRQCHIP
900 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
901 kvm_kernel_lapic_save_to_user(s
);
905 qemu_put_be32s(f
, &s
->apicbase
);
906 qemu_put_8s(f
, &s
->id
);
907 qemu_put_8s(f
, &s
->arb_id
);
908 qemu_put_8s(f
, &s
->tpr
);
909 qemu_put_be32s(f
, &s
->spurious_vec
);
910 qemu_put_8s(f
, &s
->log_dest
);
911 qemu_put_8s(f
, &s
->dest_mode
);
912 for (i
= 0; i
< 8; i
++) {
913 qemu_put_be32s(f
, &s
->isr
[i
]);
914 qemu_put_be32s(f
, &s
->tmr
[i
]);
915 qemu_put_be32s(f
, &s
->irr
[i
]);
917 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
918 qemu_put_be32s(f
, &s
->lvt
[i
]);
920 qemu_put_be32s(f
, &s
->esr
);
921 qemu_put_be32s(f
, &s
->icr
[0]);
922 qemu_put_be32s(f
, &s
->icr
[1]);
923 qemu_put_be32s(f
, &s
->divide_conf
);
924 qemu_put_be32(f
, s
->count_shift
);
925 qemu_put_be32s(f
, &s
->initial_count
);
926 qemu_put_be64(f
, s
->initial_count_load_time
);
927 qemu_put_be64(f
, s
->next_time
);
929 qemu_put_timer(f
, s
->timer
);
932 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
934 APICState
*s
= opaque
;
940 /* XXX: what if the base changes? (registered memory regions) */
941 qemu_get_be32s(f
, &s
->apicbase
);
942 qemu_get_8s(f
, &s
->id
);
943 qemu_get_8s(f
, &s
->arb_id
);
944 qemu_get_8s(f
, &s
->tpr
);
945 qemu_get_be32s(f
, &s
->spurious_vec
);
946 qemu_get_8s(f
, &s
->log_dest
);
947 qemu_get_8s(f
, &s
->dest_mode
);
948 for (i
= 0; i
< 8; i
++) {
949 qemu_get_be32s(f
, &s
->isr
[i
]);
950 qemu_get_be32s(f
, &s
->tmr
[i
]);
951 qemu_get_be32s(f
, &s
->irr
[i
]);
953 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
954 qemu_get_be32s(f
, &s
->lvt
[i
]);
956 qemu_get_be32s(f
, &s
->esr
);
957 qemu_get_be32s(f
, &s
->icr
[0]);
958 qemu_get_be32s(f
, &s
->icr
[1]);
959 qemu_get_be32s(f
, &s
->divide_conf
);
960 s
->count_shift
=qemu_get_be32(f
);
961 qemu_get_be32s(f
, &s
->initial_count
);
962 s
->initial_count_load_time
=qemu_get_be64(f
);
963 s
->next_time
=qemu_get_be64(f
);
966 qemu_get_timer(f
, s
->timer
);
968 #ifdef KVM_CAP_IRQCHIP
969 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
970 kvm_kernel_lapic_load_from_user(s
);
977 static void apic_reset(void *opaque
)
979 APICState
*s
= opaque
;
981 s
->apicbase
= 0xfee00000 |
982 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
988 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
989 * time typically by BIOS, so PIC interrupt can be delivered to the
990 * processor when local APIC is enabled.
992 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
994 #ifdef KVM_CAP_IRQCHIP
995 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
996 kvm_kernel_lapic_load_from_user(s
);
1001 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
1007 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
1013 int apic_init(CPUState
*env
)
1017 if (last_apic_id
>= MAX_APICS
)
1019 s
= qemu_mallocz(sizeof(APICState
));
1020 env
->apic_state
= s
;
1021 s
->id
= last_apic_id
++;
1022 env
->cpuid_apic_id
= s
->id
;
1027 /* XXX: mapping more APICs at the same memory location */
1028 if (apic_io_memory
== 0) {
1029 /* NOTE: the APIC is directly connected to the CPU - it is not
1030 on the global memory bus. */
1031 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
1032 apic_mem_write
, NULL
);
1033 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1036 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1038 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
1039 qemu_register_reset(apic_reset
, s
);
1041 local_apics
[s
->id
] = s
;