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
);
285 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel())
288 s
->apicbase
= (val
& 0xfffff000) |
289 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
290 /* if disabled, cannot be enabled again */
291 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
292 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
293 env
->cpuid_features
&= ~CPUID_APIC
;
294 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
298 uint64_t cpu_get_apic_base(CPUState
*env
)
300 APICState
*s
= env
->apic_state
;
302 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
307 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
309 APICState
*s
= env
->apic_state
;
310 s
->tpr
= (val
& 0x0f) << 4;
314 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
316 APICState
*s
= env
->apic_state
;
320 /* return -1 if no bit is set */
321 static int get_highest_priority_int(uint32_t *tab
)
324 for(i
= 7; i
>= 0; i
--) {
326 return i
* 32 + fls_bit(tab
[i
]);
332 static int apic_get_ppr(APICState
*s
)
337 isrv
= get_highest_priority_int(s
->isr
);
348 static int apic_get_arb_pri(APICState
*s
)
350 /* XXX: arbitration */
354 /* signal the CPU if an irq is pending */
355 static void apic_update_irq(APICState
*s
)
358 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
360 irrv
= get_highest_priority_int(s
->irr
);
363 ppr
= apic_get_ppr(s
);
364 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
366 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
369 void apic_reset_irq_delivered(void)
371 apic_irq_delivered
= 0;
374 int apic_get_irq_delivered(void)
376 return apic_irq_delivered
;
379 void apic_set_irq_delivered(void)
381 apic_irq_delivered
= 1;
384 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
386 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
388 set_bit(s
->irr
, vector_num
);
390 set_bit(s
->tmr
, vector_num
);
392 reset_bit(s
->tmr
, vector_num
);
396 static void apic_eoi(APICState
*s
)
399 isrv
= get_highest_priority_int(s
->isr
);
402 reset_bit(s
->isr
, isrv
);
403 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
404 set the remote IRR bit for level triggered interrupts. */
408 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
409 uint8_t dest
, uint8_t dest_mode
)
411 APICState
*apic_iter
;
414 if (dest_mode
== 0) {
416 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
418 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
419 set_bit(deliver_bitmask
, dest
);
422 /* XXX: cluster mode */
423 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
424 for(i
= 0; i
< MAX_APICS
; i
++) {
425 apic_iter
= local_apics
[i
];
427 if (apic_iter
->dest_mode
== 0xf) {
428 if (dest
& apic_iter
->log_dest
)
429 set_bit(deliver_bitmask
, i
);
430 } else if (apic_iter
->dest_mode
== 0x0) {
431 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
432 (dest
& apic_iter
->log_dest
& 0x0f)) {
433 set_bit(deliver_bitmask
, i
);
442 static void apic_init_ipi(APICState
*s
)
447 s
->spurious_vec
= 0xff;
450 memset(s
->isr
, 0, sizeof(s
->isr
));
451 memset(s
->tmr
, 0, sizeof(s
->tmr
));
452 memset(s
->irr
, 0, sizeof(s
->irr
));
453 for(i
= 0; i
< APIC_LVT_NB
; i
++)
454 s
->lvt
[i
] = 1 << 16; /* mask LVT */
456 memset(s
->icr
, 0, sizeof(s
->icr
));
459 s
->initial_count
= 0;
460 s
->initial_count_load_time
= 0;
463 cpu_reset(s
->cpu_env
);
465 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
) &&
466 (!kvm_enabled() || !qemu_kvm_irqchip_in_kernel()))
467 s
->cpu_env
->halted
= 1;
469 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
471 kvm_apic_init(s
->cpu_env
);
474 /* send a SIPI message to the CPU to start it */
475 static void apic_startup(APICState
*s
, int vector_num
)
477 CPUState
*env
= s
->cpu_env
;
481 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
484 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
485 kvm_update_after_sipi(env
);
488 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
489 uint8_t delivery_mode
, uint8_t vector_num
,
490 uint8_t polarity
, uint8_t trigger_mode
)
492 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
493 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
494 APICState
*apic_iter
;
496 switch (dest_shorthand
) {
498 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
501 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
502 set_bit(deliver_bitmask
, s
->id
);
505 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
508 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
509 reset_bit(deliver_bitmask
, s
->id
);
513 switch (delivery_mode
) {
516 int trig_mode
= (s
->icr
[0] >> 15) & 1;
517 int level
= (s
->icr
[0] >> 14) & 1;
518 if (level
== 0 && trig_mode
== 1) {
519 foreach_apic(apic_iter
, deliver_bitmask
,
520 apic_iter
->arb_id
= apic_iter
->id
);
527 foreach_apic(apic_iter
, deliver_bitmask
,
528 apic_startup(apic_iter
, vector_num
) );
532 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
536 int apic_get_interrupt(CPUState
*env
)
538 APICState
*s
= env
->apic_state
;
541 /* if the APIC is installed or enabled, we let the 8259 handle the
545 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
548 /* XXX: spurious IRQ handling */
549 intno
= get_highest_priority_int(s
->irr
);
552 if (s
->tpr
&& intno
<= s
->tpr
)
553 return s
->spurious_vec
& 0xff;
554 reset_bit(s
->irr
, intno
);
555 set_bit(s
->isr
, intno
);
560 int apic_accept_pic_intr(CPUState
*env
)
562 APICState
*s
= env
->apic_state
;
568 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
570 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
571 (lvt0
& APIC_LVT_MASKED
) == 0)
577 static uint32_t apic_get_current_count(APICState
*s
)
581 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
583 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
585 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
587 if (d
>= s
->initial_count
)
590 val
= s
->initial_count
- d
;
595 static void apic_timer_update(APICState
*s
, int64_t current_time
)
597 int64_t next_time
, d
;
599 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
600 d
= (current_time
- s
->initial_count_load_time
) >>
602 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
603 if (!s
->initial_count
)
605 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
607 if (d
>= s
->initial_count
)
609 d
= (uint64_t)s
->initial_count
+ 1;
611 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
612 qemu_mod_timer(s
->timer
, next_time
);
613 s
->next_time
= next_time
;
616 qemu_del_timer(s
->timer
);
620 static void apic_timer(void *opaque
)
622 APICState
*s
= opaque
;
624 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
625 apic_timer_update(s
, s
->next_time
);
628 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
633 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
638 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
642 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
646 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
653 env
= cpu_single_env
;
658 index
= (addr
>> 4) & 0xff;
663 case 0x03: /* version */
664 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
670 val
= apic_get_arb_pri(s
);
674 val
= apic_get_ppr(s
);
680 val
= s
->log_dest
<< 24;
683 val
= s
->dest_mode
<< 28;
686 val
= s
->spurious_vec
;
689 val
= s
->isr
[index
& 7];
692 val
= s
->tmr
[index
& 7];
695 val
= s
->irr
[index
& 7];
702 val
= s
->icr
[index
& 1];
705 val
= s
->lvt
[index
- 0x32];
708 val
= s
->initial_count
;
711 val
= apic_get_current_count(s
);
714 val
= s
->divide_conf
;
717 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
722 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
727 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
733 env
= cpu_single_env
;
739 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
742 index
= (addr
>> 4) & 0xff;
760 s
->log_dest
= val
>> 24;
763 s
->dest_mode
= val
>> 28;
766 s
->spurious_vec
= val
& 0x1ff;
776 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
777 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
778 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
785 int n
= index
- 0x32;
787 if (n
== APIC_LVT_TIMER
)
788 apic_timer_update(s
, qemu_get_clock(vm_clock
));
792 s
->initial_count
= val
;
793 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
794 apic_timer_update(s
, s
->initial_count_load_time
);
801 s
->divide_conf
= val
& 0xb;
802 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
803 s
->count_shift
= (v
+ 1) & 7;
807 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
812 #ifdef KVM_CAP_IRQCHIP
814 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
816 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
819 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
820 int reg_id
, uint32_t val
)
822 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
825 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
827 struct kvm_lapic_state apic
;
828 struct kvm_lapic_state
*kapic
= &apic
;
831 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
833 s
->id
= kapic_reg(kapic
, 0x2);
834 s
->tpr
= kapic_reg(kapic
, 0x8);
835 s
->arb_id
= kapic_reg(kapic
, 0x9);
836 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
837 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
838 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
839 for (i
= 0; i
< 8; i
++) {
840 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
841 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
842 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
844 s
->esr
= kapic_reg(kapic
, 0x28);
845 s
->icr
[0] = kapic_reg(kapic
, 0x30);
846 s
->icr
[1] = kapic_reg(kapic
, 0x31);
847 for (i
= 0; i
< APIC_LVT_NB
; i
++)
848 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
849 s
->initial_count
= kapic_reg(kapic
, 0x38);
850 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
852 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
853 s
->count_shift
= (v
+ 1) & 7;
855 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
856 apic_timer_update(s
, s
->initial_count_load_time
);
859 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
861 struct kvm_lapic_state apic
;
862 struct kvm_lapic_state
*klapic
= &apic
;
865 memset(klapic
, 0, sizeof apic
);
866 kapic_set_reg(klapic
, 0x2, s
->id
);
867 kapic_set_reg(klapic
, 0x8, s
->tpr
);
868 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
869 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
870 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
871 for (i
= 0; i
< 8; i
++) {
872 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
873 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
874 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
876 kapic_set_reg(klapic
, 0x28, s
->esr
);
877 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
878 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
879 for (i
= 0; i
< APIC_LVT_NB
; i
++)
880 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
881 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
882 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
884 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
889 static void apic_save(QEMUFile
*f
, void *opaque
)
891 APICState
*s
= opaque
;
894 #ifdef KVM_CAP_IRQCHIP
895 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
896 kvm_kernel_lapic_save_to_user(s
);
900 qemu_put_be32s(f
, &s
->apicbase
);
901 qemu_put_8s(f
, &s
->id
);
902 qemu_put_8s(f
, &s
->arb_id
);
903 qemu_put_8s(f
, &s
->tpr
);
904 qemu_put_be32s(f
, &s
->spurious_vec
);
905 qemu_put_8s(f
, &s
->log_dest
);
906 qemu_put_8s(f
, &s
->dest_mode
);
907 for (i
= 0; i
< 8; i
++) {
908 qemu_put_be32s(f
, &s
->isr
[i
]);
909 qemu_put_be32s(f
, &s
->tmr
[i
]);
910 qemu_put_be32s(f
, &s
->irr
[i
]);
912 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
913 qemu_put_be32s(f
, &s
->lvt
[i
]);
915 qemu_put_be32s(f
, &s
->esr
);
916 qemu_put_be32s(f
, &s
->icr
[0]);
917 qemu_put_be32s(f
, &s
->icr
[1]);
918 qemu_put_be32s(f
, &s
->divide_conf
);
919 qemu_put_be32(f
, s
->count_shift
);
920 qemu_put_be32s(f
, &s
->initial_count
);
921 qemu_put_be64(f
, s
->initial_count_load_time
);
922 qemu_put_be64(f
, s
->next_time
);
924 qemu_put_timer(f
, s
->timer
);
927 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
929 APICState
*s
= opaque
;
935 /* XXX: what if the base changes? (registered memory regions) */
936 qemu_get_be32s(f
, &s
->apicbase
);
937 qemu_get_8s(f
, &s
->id
);
938 qemu_get_8s(f
, &s
->arb_id
);
939 qemu_get_8s(f
, &s
->tpr
);
940 qemu_get_be32s(f
, &s
->spurious_vec
);
941 qemu_get_8s(f
, &s
->log_dest
);
942 qemu_get_8s(f
, &s
->dest_mode
);
943 for (i
= 0; i
< 8; i
++) {
944 qemu_get_be32s(f
, &s
->isr
[i
]);
945 qemu_get_be32s(f
, &s
->tmr
[i
]);
946 qemu_get_be32s(f
, &s
->irr
[i
]);
948 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
949 qemu_get_be32s(f
, &s
->lvt
[i
]);
951 qemu_get_be32s(f
, &s
->esr
);
952 qemu_get_be32s(f
, &s
->icr
[0]);
953 qemu_get_be32s(f
, &s
->icr
[1]);
954 qemu_get_be32s(f
, &s
->divide_conf
);
955 s
->count_shift
=qemu_get_be32(f
);
956 qemu_get_be32s(f
, &s
->initial_count
);
957 s
->initial_count_load_time
=qemu_get_be64(f
);
958 s
->next_time
=qemu_get_be64(f
);
961 qemu_get_timer(f
, s
->timer
);
963 #ifdef KVM_CAP_IRQCHIP
964 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
965 kvm_kernel_lapic_load_from_user(s
);
972 static void apic_reset(void *opaque
)
974 APICState
*s
= opaque
;
976 s
->apicbase
= 0xfee00000 |
977 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
983 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
984 * time typically by BIOS, so PIC interrupt can be delivered to the
985 * processor when local APIC is enabled.
987 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
989 #ifdef KVM_CAP_IRQCHIP
990 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
991 kvm_kernel_lapic_load_from_user(s
);
996 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
1002 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
1008 int apic_init(CPUState
*env
)
1012 if (last_apic_id
>= MAX_APICS
)
1014 s
= qemu_mallocz(sizeof(APICState
));
1015 env
->apic_state
= s
;
1016 s
->id
= last_apic_id
++;
1017 env
->cpuid_apic_id
= s
->id
;
1022 /* XXX: mapping more APICs at the same memory location */
1023 if (apic_io_memory
== 0) {
1024 /* NOTE: the APIC is directly connected to the CPU - it is not
1025 on the global memory bus. */
1026 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
1027 apic_mem_write
, NULL
);
1028 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1031 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1033 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
1034 qemu_register_reset(apic_reset
, s
);
1036 local_apics
[s
->id
] = s
;