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
;
92 static int apic_io_memory
;
93 static APICState
*local_apics
[MAX_APICS
+ 1];
94 static int last_apic_idx
= 0;
95 static int apic_irq_delivered
;
98 static void apic_init_ipi(APICState
*s
);
99 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
100 static void apic_update_irq(APICState
*s
);
101 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
102 uint8_t dest
, uint8_t dest_mode
);
104 /* Find first bit starting from msb */
105 static int fls_bit(uint32_t value
)
107 return 31 - clz32(value
);
110 /* Find first bit starting from lsb */
111 static int ffs_bit(uint32_t value
)
116 static inline void set_bit(uint32_t *tab
, int index
)
120 mask
= 1 << (index
& 0x1f);
124 static inline void reset_bit(uint32_t *tab
, int index
)
128 mask
= 1 << (index
& 0x1f);
132 static inline int get_bit(uint32_t *tab
, int index
)
136 mask
= 1 << (index
& 0x1f);
137 return !!(tab
[i
] & mask
);
140 static void apic_local_deliver(CPUState
*env
, int vector
)
142 APICState
*s
= env
->apic_state
;
143 uint32_t lvt
= s
->lvt
[vector
];
146 if (lvt
& APIC_LVT_MASKED
)
149 switch ((lvt
>> 8) & 7) {
151 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
155 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
159 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
163 trigger_mode
= APIC_TRIGGER_EDGE
;
164 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
165 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
166 trigger_mode
= APIC_TRIGGER_LEVEL
;
167 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
171 void apic_deliver_pic_intr(CPUState
*env
, int level
)
174 apic_local_deliver(env
, APIC_LVT_LINT0
);
176 APICState
*s
= env
->apic_state
;
177 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
179 switch ((lvt
>> 8) & 7) {
181 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
183 reset_bit(s
->irr
, lvt
& 0xff);
186 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
192 #define foreach_apic(apic, deliver_bitmask, code) \
194 int __i, __j, __mask;\
195 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
196 __mask = deliver_bitmask[__i];\
198 for(__j = 0; __j < 32; __j++) {\
199 if (__mask & (1 << __j)) {\
200 apic = local_apics[__i * 32 + __j];\
210 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
211 uint8_t delivery_mode
,
212 uint8_t vector_num
, uint8_t polarity
,
213 uint8_t trigger_mode
)
215 APICState
*apic_iter
;
217 switch (delivery_mode
) {
219 /* XXX: search for focus processor, arbitration */
223 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
224 if (deliver_bitmask
[i
]) {
225 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
230 apic_iter
= local_apics
[d
];
232 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
242 foreach_apic(apic_iter
, deliver_bitmask
,
243 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
247 foreach_apic(apic_iter
, deliver_bitmask
,
248 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
252 /* normal INIT IPI sent to processors */
253 foreach_apic(apic_iter
, deliver_bitmask
,
254 apic_init_ipi(apic_iter
) );
258 /* handled in I/O APIC code */
265 foreach_apic(apic_iter
, deliver_bitmask
,
266 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
269 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
270 uint8_t delivery_mode
, uint8_t vector_num
,
271 uint8_t polarity
, uint8_t trigger_mode
)
273 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
275 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
276 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
280 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
282 APICState
*s
= env
->apic_state
;
284 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
288 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel())
291 s
->apicbase
= (val
& 0xfffff000) |
292 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
293 /* if disabled, cannot be enabled again */
294 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
295 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
296 env
->cpuid_features
&= ~CPUID_APIC
;
297 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
301 uint64_t cpu_get_apic_base(CPUState
*env
)
303 APICState
*s
= env
->apic_state
;
305 printf("cpu_get_apic_base: %016" PRIx64
"\n",
306 s
? (uint64_t)s
->apicbase
: 0);
308 return s
? s
->apicbase
: 0;
311 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
313 APICState
*s
= env
->apic_state
;
316 s
->tpr
= (val
& 0x0f) << 4;
320 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
322 APICState
*s
= env
->apic_state
;
323 return s
? s
->tpr
>> 4 : 0;
326 /* return -1 if no bit is set */
327 static int get_highest_priority_int(uint32_t *tab
)
330 for(i
= 7; i
>= 0; i
--) {
332 return i
* 32 + fls_bit(tab
[i
]);
338 static int apic_get_ppr(APICState
*s
)
343 isrv
= get_highest_priority_int(s
->isr
);
354 static int apic_get_arb_pri(APICState
*s
)
356 /* XXX: arbitration */
360 /* signal the CPU if an irq is pending */
361 static void apic_update_irq(APICState
*s
)
364 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
366 irrv
= get_highest_priority_int(s
->irr
);
369 ppr
= apic_get_ppr(s
);
370 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
372 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
375 void apic_reset_irq_delivered(void)
377 apic_irq_delivered
= 0;
380 int apic_get_irq_delivered(void)
382 return apic_irq_delivered
;
385 void apic_set_irq_delivered(void)
387 apic_irq_delivered
= 1;
390 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
392 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
394 set_bit(s
->irr
, vector_num
);
396 set_bit(s
->tmr
, vector_num
);
398 reset_bit(s
->tmr
, vector_num
);
402 static void apic_eoi(APICState
*s
)
405 isrv
= get_highest_priority_int(s
->isr
);
408 reset_bit(s
->isr
, isrv
);
409 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
410 set the remote IRR bit for level triggered interrupts. */
414 static int apic_find_dest(uint8_t dest
)
416 APICState
*apic
= local_apics
[dest
];
419 if (apic
&& apic
->id
== dest
)
420 return dest
; /* shortcut in case apic->id == apic->idx */
422 for (i
= 0; i
< MAX_APICS
; i
++) {
423 apic
= local_apics
[i
];
424 if (apic
&& apic
->id
== dest
)
431 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
432 uint8_t dest
, uint8_t dest_mode
)
434 APICState
*apic_iter
;
437 if (dest_mode
== 0) {
439 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
441 int idx
= apic_find_dest(dest
);
442 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
444 set_bit(deliver_bitmask
, idx
);
447 /* XXX: cluster mode */
448 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
449 for(i
= 0; i
< MAX_APICS
; i
++) {
450 apic_iter
= local_apics
[i
];
452 if (apic_iter
->dest_mode
== 0xf) {
453 if (dest
& apic_iter
->log_dest
)
454 set_bit(deliver_bitmask
, i
);
455 } else if (apic_iter
->dest_mode
== 0x0) {
456 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
457 (dest
& apic_iter
->log_dest
& 0x0f)) {
458 set_bit(deliver_bitmask
, i
);
467 static void apic_init_ipi(APICState
*s
)
472 s
->spurious_vec
= 0xff;
475 memset(s
->isr
, 0, sizeof(s
->isr
));
476 memset(s
->tmr
, 0, sizeof(s
->tmr
));
477 memset(s
->irr
, 0, sizeof(s
->irr
));
478 for(i
= 0; i
< APIC_LVT_NB
; i
++)
479 s
->lvt
[i
] = 1 << 16; /* mask LVT */
481 memset(s
->icr
, 0, sizeof(s
->icr
));
484 s
->initial_count
= 0;
485 s
->initial_count_load_time
= 0;
488 cpu_reset(s
->cpu_env
);
490 s
->cpu_env
->halted
= !(s
->apicbase
& MSR_IA32_APICBASE_BSP
);
492 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
493 kvm_apic_init(s
->cpu_env
);
496 /* send a SIPI message to the CPU to start it */
497 static void apic_startup(APICState
*s
, int vector_num
)
499 CPUState
*env
= s
->cpu_env
;
503 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
506 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
507 kvm_update_after_sipi(env
);
510 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
511 uint8_t delivery_mode
, uint8_t vector_num
,
512 uint8_t polarity
, uint8_t trigger_mode
)
514 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
515 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
516 APICState
*apic_iter
;
518 switch (dest_shorthand
) {
520 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
523 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
524 set_bit(deliver_bitmask
, s
->idx
);
527 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
530 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
531 reset_bit(deliver_bitmask
, s
->idx
);
535 switch (delivery_mode
) {
538 int trig_mode
= (s
->icr
[0] >> 15) & 1;
539 int level
= (s
->icr
[0] >> 14) & 1;
540 if (level
== 0 && trig_mode
== 1) {
541 foreach_apic(apic_iter
, deliver_bitmask
,
542 apic_iter
->arb_id
= apic_iter
->id
);
549 foreach_apic(apic_iter
, deliver_bitmask
,
550 apic_startup(apic_iter
, vector_num
) );
554 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
558 int apic_get_interrupt(CPUState
*env
)
560 APICState
*s
= env
->apic_state
;
563 /* if the APIC is installed or enabled, we let the 8259 handle the
567 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
570 /* XXX: spurious IRQ handling */
571 intno
= get_highest_priority_int(s
->irr
);
574 if (s
->tpr
&& intno
<= s
->tpr
)
575 return s
->spurious_vec
& 0xff;
576 reset_bit(s
->irr
, intno
);
577 set_bit(s
->isr
, intno
);
582 int apic_accept_pic_intr(CPUState
*env
)
584 APICState
*s
= env
->apic_state
;
590 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
592 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
593 (lvt0
& APIC_LVT_MASKED
) == 0)
599 static uint32_t apic_get_current_count(APICState
*s
)
603 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
605 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
607 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
609 if (d
>= s
->initial_count
)
612 val
= s
->initial_count
- d
;
617 static void apic_timer_update(APICState
*s
, int64_t current_time
)
619 int64_t next_time
, d
;
621 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
622 d
= (current_time
- s
->initial_count_load_time
) >>
624 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
625 if (!s
->initial_count
)
627 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
629 if (d
>= s
->initial_count
)
631 d
= (uint64_t)s
->initial_count
+ 1;
633 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
634 qemu_mod_timer(s
->timer
, next_time
);
635 s
->next_time
= next_time
;
638 qemu_del_timer(s
->timer
);
642 static void apic_timer(void *opaque
)
644 APICState
*s
= opaque
;
646 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
647 apic_timer_update(s
, s
->next_time
);
650 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
655 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
660 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
664 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
668 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
675 env
= cpu_single_env
;
680 index
= (addr
>> 4) & 0xff;
685 case 0x03: /* version */
686 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
692 val
= apic_get_arb_pri(s
);
696 val
= apic_get_ppr(s
);
702 val
= s
->log_dest
<< 24;
705 val
= s
->dest_mode
<< 28;
708 val
= s
->spurious_vec
;
711 val
= s
->isr
[index
& 7];
714 val
= s
->tmr
[index
& 7];
717 val
= s
->irr
[index
& 7];
724 val
= s
->icr
[index
& 1];
727 val
= s
->lvt
[index
- 0x32];
730 val
= s
->initial_count
;
733 val
= apic_get_current_count(s
);
736 val
= s
->divide_conf
;
739 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
744 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
749 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
755 env
= cpu_single_env
;
761 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
764 index
= (addr
>> 4) & 0xff;
782 s
->log_dest
= val
>> 24;
785 s
->dest_mode
= val
>> 28;
788 s
->spurious_vec
= val
& 0x1ff;
798 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
799 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
800 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
807 int n
= index
- 0x32;
809 if (n
== APIC_LVT_TIMER
)
810 apic_timer_update(s
, qemu_get_clock(vm_clock
));
814 s
->initial_count
= val
;
815 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
816 apic_timer_update(s
, s
->initial_count_load_time
);
823 s
->divide_conf
= val
& 0xb;
824 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
825 s
->count_shift
= (v
+ 1) & 7;
829 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
834 #ifdef KVM_CAP_IRQCHIP
836 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
838 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
841 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
842 int reg_id
, uint32_t val
)
844 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
847 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
849 struct kvm_lapic_state apic
;
850 struct kvm_lapic_state
*kapic
= &apic
;
853 kvm_get_lapic(s
->cpu_env
->kvm_cpu_state
.vcpu_ctx
, kapic
);
855 s
->id
= kapic_reg(kapic
, 0x2) >> 24;
856 s
->tpr
= kapic_reg(kapic
, 0x8);
857 s
->arb_id
= kapic_reg(kapic
, 0x9);
858 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
859 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
860 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
861 for (i
= 0; i
< 8; i
++) {
862 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
863 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
864 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
866 s
->esr
= kapic_reg(kapic
, 0x28);
867 s
->icr
[0] = kapic_reg(kapic
, 0x30);
868 s
->icr
[1] = kapic_reg(kapic
, 0x31);
869 for (i
= 0; i
< APIC_LVT_NB
; i
++)
870 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
871 s
->initial_count
= kapic_reg(kapic
, 0x38);
872 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
874 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
875 s
->count_shift
= (v
+ 1) & 7;
877 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
878 apic_timer_update(s
, s
->initial_count_load_time
);
881 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
883 struct kvm_lapic_state apic
;
884 struct kvm_lapic_state
*klapic
= &apic
;
887 memset(klapic
, 0, sizeof apic
);
888 kapic_set_reg(klapic
, 0x2, s
->id
<< 24);
889 kapic_set_reg(klapic
, 0x8, s
->tpr
);
890 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
891 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
892 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
893 for (i
= 0; i
< 8; i
++) {
894 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
895 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
896 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
898 kapic_set_reg(klapic
, 0x28, s
->esr
);
899 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
900 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
901 for (i
= 0; i
< APIC_LVT_NB
; i
++)
902 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
903 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
904 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
906 kvm_set_lapic(s
->cpu_env
->kvm_cpu_state
.vcpu_ctx
, klapic
);
911 void qemu_kvm_load_lapic(CPUState
*env
)
913 #ifdef KVM_CAP_IRQCHIP
914 if (kvm_enabled() && kvm_vcpu_inited(env
) && qemu_kvm_irqchip_in_kernel()) {
915 kvm_kernel_lapic_load_from_user(env
->apic_state
);
920 static void apic_save(QEMUFile
*f
, void *opaque
)
922 APICState
*s
= opaque
;
925 #ifdef KVM_CAP_IRQCHIP
926 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
927 kvm_kernel_lapic_save_to_user(s
);
931 qemu_put_be32s(f
, &s
->apicbase
);
932 qemu_put_8s(f
, &s
->id
);
933 qemu_put_8s(f
, &s
->arb_id
);
934 qemu_put_8s(f
, &s
->tpr
);
935 qemu_put_be32s(f
, &s
->spurious_vec
);
936 qemu_put_8s(f
, &s
->log_dest
);
937 qemu_put_8s(f
, &s
->dest_mode
);
938 for (i
= 0; i
< 8; i
++) {
939 qemu_put_be32s(f
, &s
->isr
[i
]);
940 qemu_put_be32s(f
, &s
->tmr
[i
]);
941 qemu_put_be32s(f
, &s
->irr
[i
]);
943 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
944 qemu_put_be32s(f
, &s
->lvt
[i
]);
946 qemu_put_be32s(f
, &s
->esr
);
947 qemu_put_be32s(f
, &s
->icr
[0]);
948 qemu_put_be32s(f
, &s
->icr
[1]);
949 qemu_put_be32s(f
, &s
->divide_conf
);
950 qemu_put_be32(f
, s
->count_shift
);
951 qemu_put_be32s(f
, &s
->initial_count
);
952 qemu_put_be64(f
, s
->initial_count_load_time
);
953 qemu_put_be64(f
, s
->next_time
);
955 qemu_put_timer(f
, s
->timer
);
958 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
960 APICState
*s
= opaque
;
966 /* XXX: what if the base changes? (registered memory regions) */
967 qemu_get_be32s(f
, &s
->apicbase
);
968 qemu_get_8s(f
, &s
->id
);
969 qemu_get_8s(f
, &s
->arb_id
);
970 qemu_get_8s(f
, &s
->tpr
);
971 qemu_get_be32s(f
, &s
->spurious_vec
);
972 qemu_get_8s(f
, &s
->log_dest
);
973 qemu_get_8s(f
, &s
->dest_mode
);
974 for (i
= 0; i
< 8; i
++) {
975 qemu_get_be32s(f
, &s
->isr
[i
]);
976 qemu_get_be32s(f
, &s
->tmr
[i
]);
977 qemu_get_be32s(f
, &s
->irr
[i
]);
979 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
980 qemu_get_be32s(f
, &s
->lvt
[i
]);
982 qemu_get_be32s(f
, &s
->esr
);
983 qemu_get_be32s(f
, &s
->icr
[0]);
984 qemu_get_be32s(f
, &s
->icr
[1]);
985 qemu_get_be32s(f
, &s
->divide_conf
);
986 s
->count_shift
=qemu_get_be32(f
);
987 qemu_get_be32s(f
, &s
->initial_count
);
988 s
->initial_count_load_time
=qemu_get_be64(f
);
989 s
->next_time
=qemu_get_be64(f
);
992 qemu_get_timer(f
, s
->timer
);
994 qemu_kvm_load_lapic(s
->cpu_env
);
999 static void apic_reset(void *opaque
)
1001 APICState
*s
= opaque
;
1002 int bsp
= cpu_is_bsp(s
->cpu_env
);
1004 s
->apicbase
= 0xfee00000 |
1005 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
1011 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
1012 * time typically by BIOS, so PIC interrupt can be delivered to the
1013 * processor when local APIC is enabled.
1015 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
1017 qemu_kvm_load_lapic(s
->cpu_env
);
1020 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
1026 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
1032 int apic_init(CPUState
*env
)
1036 if (last_apic_idx
>= MAX_APICS
)
1038 s
= qemu_mallocz(sizeof(APICState
));
1039 env
->apic_state
= s
;
1040 s
->idx
= last_apic_idx
++;
1041 s
->id
= env
->cpuid_apic_id
;
1046 /* XXX: mapping more APICs at the same memory location */
1047 if (apic_io_memory
== 0) {
1048 /* NOTE: the APIC is directly connected to the CPU - it is not
1049 on the global memory bus. */
1050 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
1051 apic_mem_write
, NULL
);
1052 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1055 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1057 register_savevm("apic", s
->idx
, 2, apic_save
, apic_load
, s
);
1058 qemu_register_reset(apic_reset
, 0, s
);
1060 local_apics
[s
->idx
] = s
;