2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Authors: Jack Lange <jarusl@cs.northwestern.edu>
15 * Peter Dinda <pdinda@northwestern.edu> (SMP)
17 * This is free software. You are permitted to use,
18 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <devices/apic.h>
23 #include <devices/apic_regs.h>
24 #include <palacios/vmm.h>
25 #include <palacios/vmm_msr.h>
26 #include <palacios/vmm_sprintf.h>
27 #include <palacios/vm_guest.h>
28 #include <palacios/vmm_types.h>
31 #include <palacios/vmm_queue.h>
32 #include <palacios/vmm_lock.h>
34 /* The locking in this file is nasty.
35 * There are 3 different locking approaches that are taken, depending on the APIC operation
36 * 1. Queue locks. Actual irq insertions are done via queueing irq ops at the dest apic.
37 * The destination apic's core is responsible for draining the queue, and actually
38 * setting the vector table.
39 * 2. State lock. This is a standard lock taken when internal apic state is read/written.
40 * When an irq's destination is determined this lock is taken to examine the apic's
42 * 3. VM barrier lock. This is taken when actual VM core state is changed (via SIPI).
47 #ifndef V3_CONFIG_DEBUG_APIC
49 #define PrintDebug(fmt, args...)
52 static char * shorthand_str[] = {
59 static char * deliverymode_str[] = {
72 typedef enum { APIC_TMR_INT, APIC_THERM_INT, APIC_PERF_INT,
73 APIC_LINT0_INT, APIC_LINT1_INT, APIC_ERR_INT } apic_irq_type_t;
76 #define APIC_SHORTHAND_NONE 0x0
77 #define APIC_SHORTHAND_SELF 0x1
78 #define APIC_SHORTHAND_ALL 0x2
79 #define APIC_SHORTHAND_ALL_BUT_ME 0x3
81 #define APIC_DEST_PHYSICAL 0x0
82 #define APIC_DEST_LOGICAL 0x1
85 #define BASE_ADDR_MSR 0x0000001B
86 #define DEFAULT_BASE_ADDR 0xfee00000
88 #define APIC_ID_OFFSET 0x020
89 #define APIC_VERSION_OFFSET 0x030
90 #define TPR_OFFSET 0x080
91 #define APR_OFFSET 0x090
92 #define PPR_OFFSET 0x0a0
93 #define EOI_OFFSET 0x0b0
94 #define REMOTE_READ_OFFSET 0x0c0
95 #define LDR_OFFSET 0x0d0
96 #define DFR_OFFSET 0x0e0
97 #define SPURIOUS_INT_VEC_OFFSET 0x0f0
99 #define ISR_OFFSET0 0x100 // 0x100 - 0x170
100 #define ISR_OFFSET1 0x110 // 0x100 - 0x170
101 #define ISR_OFFSET2 0x120 // 0x100 - 0x170
102 #define ISR_OFFSET3 0x130 // 0x100 - 0x170
103 #define ISR_OFFSET4 0x140 // 0x100 - 0x170
104 #define ISR_OFFSET5 0x150 // 0x100 - 0x170
105 #define ISR_OFFSET6 0x160 // 0x100 - 0x170
106 #define ISR_OFFSET7 0x170 // 0x100 - 0x170
108 #define TRIG_OFFSET0 0x180 // 0x180 - 0x1f0
109 #define TRIG_OFFSET1 0x190 // 0x180 - 0x1f0
110 #define TRIG_OFFSET2 0x1a0 // 0x180 - 0x1f0
111 #define TRIG_OFFSET3 0x1b0 // 0x180 - 0x1f0
112 #define TRIG_OFFSET4 0x1c0 // 0x180 - 0x1f0
113 #define TRIG_OFFSET5 0x1d0 // 0x180 - 0x1f0
114 #define TRIG_OFFSET6 0x1e0 // 0x180 - 0x1f0
115 #define TRIG_OFFSET7 0x1f0 // 0x180 - 0x1f0
118 #define IRR_OFFSET0 0x200 // 0x200 - 0x270
119 #define IRR_OFFSET1 0x210 // 0x200 - 0x270
120 #define IRR_OFFSET2 0x220 // 0x200 - 0x270
121 #define IRR_OFFSET3 0x230 // 0x200 - 0x270
122 #define IRR_OFFSET4 0x240 // 0x200 - 0x270
123 #define IRR_OFFSET5 0x250 // 0x200 - 0x270
124 #define IRR_OFFSET6 0x260 // 0x200 - 0x270
125 #define IRR_OFFSET7 0x270 // 0x200 - 0x270
128 #define ESR_OFFSET 0x280
129 #define INT_CMD_LO_OFFSET 0x300
130 #define INT_CMD_HI_OFFSET 0x310
131 #define TMR_LOC_VEC_TBL_OFFSET 0x320
132 #define THERM_LOC_VEC_TBL_OFFSET 0x330
133 #define PERF_CTR_LOC_VEC_TBL_OFFSET 0x340
134 #define LINT0_VEC_TBL_OFFSET 0x350
135 #define LINT1_VEC_TBL_OFFSET 0x360
136 #define ERR_VEC_TBL_OFFSET 0x370
137 #define TMR_INIT_CNT_OFFSET 0x380
138 #define TMR_CUR_CNT_OFFSET 0x390
139 #define TMR_DIV_CFG_OFFSET 0x3e0
140 #define EXT_APIC_FEATURE_OFFSET 0x400
141 #define EXT_APIC_CMD_OFFSET 0x410
142 #define SEOI_OFFSET 0x420
144 #define IER_OFFSET0 0x480 // 0x480 - 0x4f0
145 #define IER_OFFSET1 0x490 // 0x480 - 0x4f0
146 #define IER_OFFSET2 0x4a0 // 0x480 - 0x4f0
147 #define IER_OFFSET3 0x4b0 // 0x480 - 0x4f0
148 #define IER_OFFSET4 0x4c0 // 0x480 - 0x4f0
149 #define IER_OFFSET5 0x4d0 // 0x480 - 0x4f0
150 #define IER_OFFSET6 0x4e0 // 0x480 - 0x4f0
151 #define IER_OFFSET7 0x4f0 // 0x480 - 0x4f0
153 #define EXT_INT_LOC_VEC_TBL_OFFSET0 0x500 // 0x500 - 0x530
154 #define EXT_INT_LOC_VEC_TBL_OFFSET1 0x510 // 0x500 - 0x530
155 #define EXT_INT_LOC_VEC_TBL_OFFSET2 0x520 // 0x500 - 0x530
156 #define EXT_INT_LOC_VEC_TBL_OFFSET3 0x530 // 0x500 - 0x530
163 uint8_t bootstrap_cpu : 1;
165 uint8_t apic_enable : 1;
166 uint64_t base_addr : 40;
168 } __attribute__((packed));
169 } __attribute__((packed));
170 } __attribute__((packed));
176 struct irq_queue_entry {
178 int (*ack)(struct guest_info * core, uint32_t irq, void * private_data);
181 struct list_head list_node;
187 typedef enum {INIT_ST,
189 STARTED} ipi_state_t;
191 struct apic_dev_state;
197 struct apic_msr base_addr_msr;
200 /* memory map registers */
202 struct lapic_id_reg lapic_id;
203 struct apic_ver_reg apic_ver;
204 struct ext_apic_ctrl_reg ext_apic_ctrl;
205 struct local_vec_tbl_reg local_vec_tbl;
206 struct tmr_vec_tbl_reg tmr_vec_tbl;
207 struct tmr_div_cfg_reg tmr_div_cfg;
208 struct lint_vec_tbl_reg lint0_vec_tbl;
209 struct lint_vec_tbl_reg lint1_vec_tbl;
210 struct perf_ctr_loc_vec_tbl_reg perf_ctr_loc_vec_tbl;
211 struct therm_loc_vec_tbl_reg therm_loc_vec_tbl;
212 struct err_vec_tbl_reg err_vec_tbl;
213 struct err_status_reg err_status;
214 struct spurious_int_reg spurious_int;
215 struct int_cmd_reg int_cmd;
216 struct log_dst_reg log_dst;
217 struct dst_fmt_reg dst_fmt;
218 //struct arb_prio_reg arb_prio; // computed on the fly
219 //struct task_prio_reg task_prio; // stored in core.ctrl_regs.apic_tpr
220 //struct proc_prio_reg proc_prio; // computed on the fly
221 struct ext_apic_feature_reg ext_apic_feature;
222 struct spec_eoi_reg spec_eoi;
225 uint32_t tmr_cur_cnt;
226 uint32_t tmr_init_cnt;
227 uint32_t missed_ints;
229 struct local_vec_tbl_reg ext_intr_vec_tbl[4];
231 uint32_t rem_rd_data;
234 ipi_state_t ipi_state;
236 uint8_t int_req_reg[32];
237 uint8_t int_svc_reg[32];
238 uint8_t int_en_reg[32];
239 uint8_t trig_mode_reg[32];
242 int (*ack)(struct guest_info * core, uint32_t irq, void * private_data);
246 struct guest_info * core;
248 void * controller_handle;
250 struct v3_timer * timer;
256 uint64_t num_entries;
257 struct list_head entries;
268 struct apic_dev_state {
271 v3_lock_t state_lock;
273 struct apic_state apics[0];
274 } __attribute__((packed));
280 static int apic_read(struct guest_info * core, addr_t guest_addr, void * dst, uint_t length, void * priv_data);
281 static int apic_write(struct guest_info * core, addr_t guest_addr, void * src, uint_t length, void * priv_data);
283 static void set_apic_tpr(struct apic_state *apic, uint32_t val);
287 static void init_apic_state(struct apic_state * apic, uint32_t id) {
288 apic->base_addr = DEFAULT_BASE_ADDR;
291 // boot processor, enabled
292 apic->base_addr_msr.value = 0x0000000000000900LL;
294 // ap processor, enabled
295 apic->base_addr_msr.value = 0x0000000000000800LL;
298 // same base address regardless of ap or main
299 apic->base_addr_msr.value |= ((uint64_t)DEFAULT_BASE_ADDR);
301 PrintDebug(VM_NONE, VCORE_NONE, "apic %u: (init_apic_state): msr=0x%llx\n",id, apic->base_addr_msr.value);
303 PrintDebug(VM_NONE, VCORE_NONE, "apic %u: (init_apic_state): Sizeof Interrupt Request Register %d, should be 32\n",
304 id, (uint_t)sizeof(apic->int_req_reg));
306 memset(apic->int_req_reg, 0, sizeof(apic->int_req_reg));
307 memset(apic->int_svc_reg, 0, sizeof(apic->int_svc_reg));
308 memset(apic->int_en_reg, 0xff, sizeof(apic->int_en_reg));
309 memset(apic->trig_mode_reg, 0, sizeof(apic->trig_mode_reg));
311 apic->eoi = 0x00000000;
312 apic->rem_rd_data = 0x00000000;
313 apic->tmr_init_cnt = 0x00000000;
314 apic->tmr_cur_cnt = 0x00000000;
315 apic->missed_ints = 0;
317 // note that it's the *lower* 24 bits that are
318 // reserved, not the upper 24.
319 apic->lapic_id.val = 0;
320 apic->lapic_id.apic_id = id;
322 apic->ipi_state = INIT_ST;
324 // The P6 has 6 LVT entries, so we set the value to (6-1)...
325 apic->apic_ver.val = 0x80050010;
327 set_apic_tpr(apic,0x00000000);
328 // note that arbitration priority and processor priority are derived values
329 // and are computed on the fly
331 apic->log_dst.val = 0x00000000;
332 apic->dst_fmt.val = 0xffffffff;
333 apic->spurious_int.val = 0x000000ff;
334 apic->err_status.val = 0x00000000;
335 apic->int_cmd.val = 0x0000000000000000LL;
336 apic->tmr_vec_tbl.val = 0x00010000;
337 apic->therm_loc_vec_tbl.val = 0x00010000;
338 apic->perf_ctr_loc_vec_tbl.val = 0x00010000;
339 apic->lint0_vec_tbl.val = 0x00010000;
340 apic->lint1_vec_tbl.val = 0x00010000;
341 apic->err_vec_tbl.val = 0x00010000;
342 apic->tmr_div_cfg.val = 0x00000000;
343 //apic->ext_apic_feature.val = 0x00000007;
344 apic->ext_apic_feature.val = 0x00040007;
345 apic->ext_apic_ctrl.val = 0x00000000;
346 apic->spec_eoi.val = 0x00000000;
349 INIT_LIST_HEAD(&(apic->irq_queue.entries));
350 v3_lock_init(&(apic->irq_queue.lock));
351 apic->irq_queue.num_entries = 0;
359 static int read_apic_msr(struct guest_info * core, uint_t msr, v3_msr_t * dst, void * priv_data) {
360 struct apic_dev_state * apic_dev = (struct apic_dev_state *)priv_data;
361 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
363 PrintDebug(core->vm_info, core, "apic %u: core %u: MSR read\n", apic->lapic_id.val, core->vcpu_id);
365 dst->value = apic->base_addr;
371 static int write_apic_msr(struct guest_info * core, uint_t msr, v3_msr_t src, void * priv_data) {
372 struct apic_dev_state * apic_dev = (struct apic_dev_state *)priv_data;
373 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
374 struct v3_mem_region * old_reg = v3_get_mem_region(core->vm_info, core->vcpu_id, apic->base_addr);
377 PrintDebug(core->vm_info, core, "apic %u: core %u: MSR write\n", apic->lapic_id.val, core->vcpu_id);
379 if (old_reg == NULL) {
381 PrintError(core->vm_info, core, "apic %u: core %u: APIC Base address region does not exit...\n",
382 apic->lapic_id.val, core->vcpu_id);
388 v3_delete_mem_region(core->vm_info, old_reg);
390 apic->base_addr = src.value;
392 if (v3_hook_full_mem(core->vm_info, core->vcpu_id, apic->base_addr,
393 apic->base_addr + PAGE_SIZE_4KB,
394 apic_read, apic_write, apic_dev) == -1) {
395 PrintError(core->vm_info, core, "apic %u: core %u: Could not hook new APIC Base address\n",
396 apic->lapic_id.val, core->vcpu_id);
409 // irq_num is the bit offset into a 256 bit buffer...
410 static int activate_apic_irq(struct apic_state * apic, uint32_t irq_num,
411 int (*ack)(struct guest_info * core, uint32_t irq, void * private_data),
412 void * private_data) {
413 int major_offset = (irq_num & ~0x00000007) >> 3;
414 int minor_offset = irq_num & 0x00000007;
415 uint8_t * req_location = apic->int_req_reg + major_offset;
416 uint8_t * en_location = apic->int_en_reg + major_offset;
417 uint8_t flag = 0x1 << minor_offset;
420 PrintDebug(VM_NONE, VCORE_NONE, "apic %u: core %d: Raising APIC IRQ %d\n", apic->lapic_id.val, apic->core->vcpu_id, irq_num);
422 if (*req_location & flag) {
423 PrintDebug(VM_NONE, VCORE_NONE, "Interrupt %d coallescing\n", irq_num);
427 if (*en_location & flag) {
428 *req_location |= flag;
429 apic->irq_ack_cbs[irq_num].ack = ack;
430 apic->irq_ack_cbs[irq_num].private_data = private_data;
434 PrintDebug(VM_NONE, VCORE_NONE, "apic %u: core %d: Interrupt not enabled... %.2x\n",
435 apic->lapic_id.val, apic->core->vcpu_id, *en_location);
443 static int add_apic_irq_entry(struct apic_state * apic, uint32_t irq_num,
444 int (*ack)(struct guest_info * core, uint32_t irq, void * private_data),
445 void * private_data) {
446 unsigned int flags = 0;
447 struct irq_queue_entry * entry = NULL;
450 PrintError(VM_NONE, VCORE_NONE, "core %d: Attempting to raise an invalid interrupt: %d\n",
451 apic->core->vcpu_id, irq_num);
455 entry = V3_Malloc(sizeof(struct irq_queue_entry));
458 PrintError(VM_NONE, VCORE_NONE, "Could not allocate irq queue entry\n");
462 entry->vector = irq_num;
464 entry->private_data = private_data;
466 flags = v3_lock_irqsave(apic->irq_queue.lock);
468 list_add_tail(&(entry->list_node), &(apic->irq_queue.entries));
469 apic->irq_queue.num_entries++;
471 v3_unlock_irqrestore(apic->irq_queue.lock, flags);
477 static void drain_irq_entries(struct apic_state * apic) {
480 unsigned int flags = 0;
481 struct irq_queue_entry * entry = NULL;
483 flags = v3_lock_irqsave(apic->irq_queue.lock);
485 if (!list_empty(&(apic->irq_queue.entries))) {
486 struct list_head * q_entry = apic->irq_queue.entries.next;
487 entry = list_entry(q_entry, struct irq_queue_entry, list_node);
489 apic->irq_queue.num_entries--;
493 v3_unlock_irqrestore(apic->irq_queue.lock, flags);
499 activate_apic_irq(apic, entry->vector, entry->ack, entry->private_data);
508 static int get_highest_isr(struct apic_state * apic) {
511 // We iterate backwards to find the highest priority in-service request
512 for (i = 31; i >= 0; i--) {
513 uint8_t * svc_major = apic->int_svc_reg + i;
515 if ((*svc_major) & 0xff) {
516 for (j = 7; j >= 0; j--) {
517 uint8_t flag = 0x1 << j;
518 if ((*svc_major) & flag) {
519 return ((i * 8) + j);
530 static int get_highest_irr(struct apic_state * apic) {
533 // We iterate backwards to find the highest priority enabled requested interrupt
534 for (i = 31; i >= 0; i--) {
535 uint8_t * req_major = apic->int_req_reg + i;
536 uint8_t * en_major = apic->int_en_reg + i;
538 if ((*req_major) & 0xff) {
539 for (j = 7; j >= 0; j--) {
540 uint8_t flag = 0x1 << j;
541 if ((*req_major & *en_major) & flag) {
542 return ((i * 8) + j);
552 static uint32_t get_isrv(struct apic_state *apic)
554 int isr = get_highest_isr(apic);
557 return (uint32_t) isr;
563 static uint32_t get_irrv(struct apic_state *apic)
565 int irr = get_highest_irr(apic);
568 return (uint32_t) irr;
575 static uint32_t get_apic_tpr(struct apic_state *apic)
577 return (uint32_t) (apic->core->ctrl_regs.apic_tpr); // see comment in vmm_ctrl_regs.c for how this works
581 static void set_apic_tpr(struct apic_state *apic, uint32_t val)
583 PrintDebug(VM_NONE, VCORE_NONE, "Set apic_tpr to 0x%x from apic reg path\n",val);
584 apic->core->ctrl_regs.apic_tpr = (uint64_t) val; // see comment in vmm_ctrl_regs.c for how this works
587 static uint32_t get_apic_ppr(struct apic_state *apic)
589 uint32_t tpr = get_apic_tpr(apic);
590 uint32_t isrv = get_isrv(apic);
591 uint32_t tprlevel, isrlevel;
594 tprlevel = (tpr >> 4) & 0xf;
595 isrlevel = (isrv >> 4) & 0xf;
597 if (tprlevel>=isrlevel) {
598 ppr = tpr; // get class and subclass
600 ppr = (isrlevel << 4); // get class only
608 static uint32_t get_apic_apr(struct apic_state *apic)
610 uint32_t tpr = get_apic_tpr(apic);
611 uint32_t isrv = get_isrv(apic);
612 uint32_t irrv = get_irrv(apic);
613 uint32_t tprlevel, isrlevel, irrlevel;
615 tprlevel = (tpr >> 4) & 0xf;
616 isrlevel = (isrv >> 4) & 0xf;
617 irrlevel = (irrv >> 4) & 0xf;
619 if (tprlevel >= isrlevel) {
620 if (tprlevel >= irrlevel) {
621 return tpr; // get both class and subclass
623 return irrlevel << 4; // get class only
626 if (isrlevel >= irrlevel) {
627 return isrlevel << 4; // get class only
629 return irrlevel << 4; // get class only
636 static int apic_do_eoi(struct guest_info * core, struct apic_state * apic) {
637 int isr_irq = get_highest_isr(apic);
640 int major_offset = (isr_irq & ~0x00000007) >> 3;
641 int minor_offset = isr_irq & 0x00000007;
642 uint8_t flag = 0x1 << minor_offset;
643 uint8_t * svc_location = apic->int_svc_reg + major_offset;
645 PrintDebug(core->vm_info, core, "apic %u: core ?: Received APIC EOI for IRQ %d\n", apic->lapic_id.val,isr_irq);
647 *svc_location &= ~flag;
649 if (apic->irq_ack_cbs[isr_irq].ack) {
650 apic->irq_ack_cbs[isr_irq].ack(core, isr_irq, apic->irq_ack_cbs[isr_irq].private_data);
653 #ifdef V3_CONFIG_CRAY_XT
655 if ((isr_irq == 238) ||
657 PrintDebug(core->vm_info, core, "apic %u: core ?: Acking IRQ %d\n", apic->lapic_id.val,isr_irq);
660 if (isr_irq == 238) {
665 //PrintError(core->vm_info, core, "apic %u: core ?: Spurious EOI...\n",apic->lapic_id.val);
672 static int activate_internal_irq(struct apic_state * apic, apic_irq_type_t int_type) {
673 uint32_t vec_num = 0;
674 uint32_t del_mode = 0;
680 vec_num = apic->tmr_vec_tbl.vec;
681 del_mode = IPI_FIXED;
682 masked = apic->tmr_vec_tbl.mask;
685 vec_num = apic->therm_loc_vec_tbl.vec;
686 del_mode = apic->therm_loc_vec_tbl.msg_type;
687 masked = apic->therm_loc_vec_tbl.mask;
690 vec_num = apic->perf_ctr_loc_vec_tbl.vec;
691 del_mode = apic->perf_ctr_loc_vec_tbl.msg_type;
692 masked = apic->perf_ctr_loc_vec_tbl.mask;
695 vec_num = apic->lint0_vec_tbl.vec;
696 del_mode = apic->lint0_vec_tbl.msg_type;
697 masked = apic->lint0_vec_tbl.mask;
700 vec_num = apic->lint1_vec_tbl.vec;
701 del_mode = apic->lint1_vec_tbl.msg_type;
702 masked = apic->lint1_vec_tbl.mask;
705 vec_num = apic->err_vec_tbl.vec;
706 del_mode = IPI_FIXED;
707 masked = apic->err_vec_tbl.mask;
710 PrintError(VM_NONE, VCORE_NONE, "apic %u: core ?: Invalid APIC interrupt type\n", apic->lapic_id.val);
714 // interrupt is masked, don't send
716 PrintDebug(VM_NONE, VCORE_NONE, "apic %u: core ?: Inerrupt is masked\n", apic->lapic_id.val);
720 if (del_mode == IPI_FIXED) {
721 //PrintDebug(VM_NONE, VCORE_NONE, "Activating internal APIC IRQ %d\n", vec_num);
722 return add_apic_irq_entry(apic, vec_num, NULL, NULL);
724 PrintError(VM_NONE, VCORE_NONE, "apic %u: core ?: Unhandled Delivery Mode\n", apic->lapic_id.val);
731 static inline int should_deliver_cluster_ipi(struct apic_dev_state * apic_dev,
732 struct guest_info * dst_core,
733 struct apic_state * dst_apic, uint8_t mda) {
738 if ( ((mda & 0xf0) == (dst_apic->log_dst.dst_log_id & 0xf0)) && /* (I am in the cluster and */
739 ((mda & 0x0f) & (dst_apic->log_dst.dst_log_id & 0x0f)) ) { /* I am in the set) */
747 PrintDebug(VM_NONE, VCORE_NONE, "apic %u core %u: accepting clustered IRQ (mda 0x%x == log_dst 0x%x)\n",
748 dst_apic->lapic_id.val, dst_core->vcpu_id, mda,
749 dst_apic->log_dst.dst_log_id);
751 PrintDebug(VM_NONE, VCORE_NONE, "apic %u core %u: rejecting clustered IRQ (mda 0x%x != log_dst 0x%x)\n",
752 dst_apic->lapic_id.val, dst_core->vcpu_id, mda,
753 dst_apic->log_dst.dst_log_id);
760 static inline int should_deliver_flat_ipi(struct apic_dev_state * apic_dev,
761 struct guest_info * dst_core,
762 struct apic_state * dst_apic, uint8_t mda) {
767 if ((dst_apic->log_dst.dst_log_id & mda) != 0) { // I am in the set
775 PrintDebug(VM_NONE, VCORE_NONE, "apic %u core %u: accepting flat IRQ (mda 0x%x == log_dst 0x%x)\n",
776 dst_apic->lapic_id.val, dst_core->vcpu_id, mda,
777 dst_apic->log_dst.dst_log_id);
779 PrintDebug(VM_NONE, VCORE_NONE, "apic %u core %u: rejecting flat IRQ (mda 0x%x != log_dst 0x%x)\n",
780 dst_apic->lapic_id.val, dst_core->vcpu_id, mda,
781 dst_apic->log_dst.dst_log_id);
790 static int should_deliver_ipi(struct apic_dev_state * apic_dev,
791 struct guest_info * dst_core,
792 struct apic_state * dst_apic, uint8_t mda) {
796 flags = v3_lock_irqsave(apic_dev->state_lock);
798 if (dst_apic->dst_fmt.model == 0xf) {
801 /* always deliver broadcast */
804 ret = should_deliver_flat_ipi(apic_dev, dst_core, dst_apic, mda);
806 } else if (dst_apic->dst_fmt.model == 0x0) {
809 /* always deliver broadcast */
812 ret = should_deliver_cluster_ipi(apic_dev, dst_core, dst_apic, mda);
819 v3_unlock_irqrestore(apic_dev->state_lock, flags);
823 PrintError(VM_NONE, VCORE_NONE, "apic %u core %u: invalid destination format register value 0x%x for logical mode delivery.\n",
824 dst_apic->lapic_id.val, dst_core->vcpu_id, dst_apic->dst_fmt.model);
833 // Only the src_apic pointer is used
834 static int deliver_ipi(struct apic_state * src_apic,
835 struct apic_state * dst_apic,
836 struct v3_gen_ipi * ipi) {
839 struct guest_info * dst_core = dst_apic->core;
845 case IPI_LOWEST_PRIO: {
847 // caller needs to have decided which apic to deliver to!
849 PrintDebug(VM_NONE, VCORE_NONE, "delivering IRQ %d to core %u\n", ipi->vector, dst_core->vcpu_id);
851 add_apic_irq_entry(dst_apic, ipi->vector, ipi->ack, ipi->private_data);
853 if (dst_apic != src_apic) {
854 PrintDebug(VM_NONE, VCORE_NONE, " non-local core with new interrupt, forcing it to exit now\n");
855 v3_interrupt_cpu(dst_core->vm_info, dst_core->pcpu_id, 0);
862 PrintDebug(VM_NONE, VCORE_NONE, " INIT delivery to core %u\n", dst_core->vcpu_id);
864 // TODO: any APIC reset on dest core (shouldn't be needed, but not sure...)
867 if (dst_apic->ipi_state != INIT_ST) {
868 PrintError(VM_NONE, VCORE_NONE, " Warning: core %u is not in INIT state (mode = %d), ignored (assuming this is the deassert)\n",
869 dst_core->vcpu_id, dst_apic->ipi_state);
870 // Only a warning, since INIT INIT SIPI is common
874 // We transition the target core to SIPI state
875 dst_apic->ipi_state = SIPI; // note: locking should not be needed here
877 // That should be it since the target core should be
878 // waiting in host on this transition
879 // either it's on another core or on a different preemptive thread
880 // in both cases, it will quickly notice this transition
881 // in particular, we should not need to force an exit here
883 PrintDebug(VM_NONE, VCORE_NONE, " INIT delivery done\n");
890 if (dst_apic->ipi_state != SIPI) {
891 PrintError(VM_NONE, VCORE_NONE, " core %u is not in SIPI state (mode = %d), ignored!\n",
892 dst_core->vcpu_id, dst_apic->ipi_state);
896 v3_reset_vm_core(dst_core, ipi->vector);
898 PrintDebug(VM_NONE, VCORE_NONE, " SIPI delivery (0x%x -> 0x%x:0x0) to core %u\n",
899 ipi->vector, dst_core->segments.cs.selector, dst_core->vcpu_id);
900 // Maybe need to adjust the APIC?
902 // We transition the target core to SIPI state
903 dst_core->core_run_state = CORE_RUNNING; // note: locking should not be needed here
904 dst_apic->ipi_state = STARTED;
906 // As with INIT, we should not need to do anything else
908 PrintDebug(VM_NONE, VCORE_NONE, " SIPI delivery done\n");
913 case IPI_EXTINT: // EXTINT
914 /* Two possible things to do here:
915 * 1. Ignore the IPI and assume the 8259a (PIC) will handle it
916 * 2. Add 32 to the vector and inject it...
917 * We probably just want to do 1 here, and assume the raise_irq() will hit the 8259a.
922 case IPI_RES1: // reserved
925 PrintError(VM_NONE, VCORE_NONE, "IPI %d delivery is unsupported\n", ipi->mode);
933 static struct apic_state * find_physical_apic(struct apic_dev_state * apic_dev, uint32_t dst_idx) {
934 struct apic_state * dst_apic = NULL;
938 flags = v3_lock_irqsave(apic_dev->state_lock);
940 if ( (dst_idx > 0) && (dst_idx < apic_dev->num_apics) ) {
941 // see if it simply is the core id
942 if (apic_dev->apics[dst_idx].lapic_id.apic_id == dst_idx) {
943 dst_apic = &(apic_dev->apics[dst_idx]);
947 for (i = 0; i < apic_dev->num_apics; i++) {
948 if (apic_dev->apics[i].lapic_id.apic_id == dst_idx) {
949 dst_apic = &(apic_dev->apics[i]);
953 v3_unlock_irqrestore(apic_dev->state_lock, flags);
960 static int route_ipi(struct apic_dev_state * apic_dev,
961 struct apic_state * src_apic,
962 struct v3_gen_ipi * ipi) {
963 struct apic_state * dest_apic = NULL;
966 PrintDebug(VM_NONE, VCORE_NONE, "apic: IPI %s %u from apic %p to %s %s %u\n",
967 deliverymode_str[ipi->mode],
970 (ipi->logical == 0) ? "(physical)" : "(logical)",
971 shorthand_str[ipi->dst_shorthand],
975 switch (ipi->dst_shorthand) {
977 case APIC_SHORTHAND_NONE: // no shorthand
978 if (ipi->logical == APIC_DEST_PHYSICAL) {
980 dest_apic = find_physical_apic(apic_dev, ipi->dst);
982 if (dest_apic == NULL) {
983 PrintError(VM_NONE, VCORE_NONE, "apic: Attempted send to unregistered apic id=%u\n", ipi->dst);
987 if (deliver_ipi(src_apic, dest_apic, ipi) == -1) {
988 PrintError(VM_NONE, VCORE_NONE, "apic: Could not deliver IPI\n");
993 PrintDebug(VM_NONE, VCORE_NONE, "apic: done\n");
995 } else if (ipi->logical == APIC_DEST_LOGICAL) {
997 if (ipi->mode != IPI_LOWEST_PRIO) {
999 uint8_t mda = ipi->dst;
1001 // logical, but not lowest priority
1002 // we immediately trigger
1003 // fixed, smi, reserved, nmi, init, sipi, etc
1006 for (i = 0; i < apic_dev->num_apics; i++) {
1009 dest_apic = &(apic_dev->apics[i]);
1011 del_flag = should_deliver_ipi(apic_dev, dest_apic->core, dest_apic, mda);
1013 if (del_flag == -1) {
1015 PrintError(VM_NONE, VCORE_NONE, "apic: Error checking delivery mode\n");
1017 } else if (del_flag == 1) {
1019 if (deliver_ipi(src_apic, dest_apic, ipi) == -1) {
1020 PrintError(VM_NONE, VCORE_NONE, "apic: Error: Could not deliver IPI\n");
1025 } else { // APIC_LOWEST_DELIVERY
1026 struct apic_state * cur_best_apic = NULL;
1027 uint32_t cur_best_apr;
1028 uint8_t mda = ipi->dst;
1031 // logical, lowest priority
1033 for (i = 0; i < apic_dev->num_apics; i++) {
1036 dest_apic = &(apic_dev->apics[i]);
1038 del_flag = should_deliver_ipi(apic_dev, dest_apic->core, dest_apic, mda);
1040 if (del_flag == -1) {
1041 PrintError(VM_NONE, VCORE_NONE, "apic: Error checking delivery mode\n");
1044 } else if (del_flag == 1) {
1045 // update priority for lowest priority scan
1048 flags = v3_lock_irqsave(apic_dev->state_lock);
1050 if (cur_best_apic == 0) {
1051 cur_best_apic = dest_apic;
1052 cur_best_apr = get_apic_apr(dest_apic) & 0xf0;
1054 uint32_t dest_apr = get_apic_apr(dest_apic) & 0xf0;
1055 if (dest_apr < cur_best_apr) {
1056 cur_best_apic = dest_apic;
1057 cur_best_apr = dest_apr;
1061 v3_unlock_irqrestore(apic_dev->state_lock, flags);
1066 // now we will deliver to the best one if it exists
1067 if (!cur_best_apic) {
1068 PrintDebug(VM_NONE, VCORE_NONE, "apic: lowest priority deliver, but no destinations!\n");
1070 if (deliver_ipi(src_apic, cur_best_apic, ipi) == -1) {
1071 PrintError(VM_NONE, VCORE_NONE, "apic: Error: Could not deliver IPI\n");
1074 //V3_Print(VM_NONE, VCORE_NONE, "apic: logical, lowest priority delivery to apic %u\n",cur_best_apic->lapic_id.val);
1081 case APIC_SHORTHAND_SELF: // self
1083 if (src_apic == NULL) { /* this is not an apic, but it's trying to send to itself??? */
1084 PrintError(VM_NONE, VCORE_NONE, "apic: Sending IPI to self from generic IPI sender\n");
1090 if (ipi->logical == APIC_DEST_PHYSICAL) { /* physical delivery */
1091 if (deliver_ipi(src_apic, src_apic, ipi) == -1) {
1092 PrintError(VM_NONE, VCORE_NONE, "apic: Could not deliver IPI to self (physical)\n");
1095 } else if (ipi->logical == APIC_DEST_LOGICAL) { /* logical delivery */
1096 PrintError(VM_NONE, VCORE_NONE, "apic: use of logical delivery in self (untested)\n");
1098 if (deliver_ipi(src_apic, src_apic, ipi) == -1) {
1099 PrintError(VM_NONE, VCORE_NONE, "apic: Could not deliver IPI to self (logical)\n");
1106 case APIC_SHORTHAND_ALL:
1107 case APIC_SHORTHAND_ALL_BUT_ME: { /* all and all-but-me */
1108 /* assuming that logical verus physical doesn't matter
1109 although it is odd that both are used */
1112 for (i = 0; i < apic_dev->num_apics; i++) {
1113 dest_apic = &(apic_dev->apics[i]);
1115 if ((dest_apic != src_apic) || (ipi->dst_shorthand == APIC_SHORTHAND_ALL)) {
1116 if (deliver_ipi(src_apic, dest_apic, ipi) == -1) {
1117 PrintError(VM_NONE, VCORE_NONE, "apic: Error: Could not deliver IPI\n");
1126 PrintError(VM_NONE, VCORE_NONE, "apic: Error routing IPI, invalid Mode (%d)\n", ipi->dst_shorthand);
1134 // External function, expected to acquire lock on apic
1135 static int apic_read(struct guest_info * core, addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
1136 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(priv_data);
1137 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1138 addr_t reg_addr = guest_addr - apic->base_addr;
1139 struct apic_msr * msr = (struct apic_msr *)&(apic->base_addr_msr.value);
1143 PrintDebug(core->vm_info, core, "apic %u: core %u: at %p: Read apic address space (%p)\n",
1144 apic->lapic_id.val, core->vcpu_id, apic, (void *)guest_addr);
1146 if (msr->apic_enable == 0) {
1147 PrintError(core->vm_info, core, "apic %u: core %u: Read from APIC address space with disabled APIC, apic msr=0x%llx\n",
1148 apic->lapic_id.val, core->vcpu_id, apic->base_addr_msr.value);
1153 /* Because "May not be supported" doesn't matter to Linux developers... */
1154 /* if (length != 4) { */
1155 /* PrintError(core->vm_info, core, "Invalid apic read length (%d)\n", length); */
1159 switch (reg_addr & ~0x3) {
1161 // Well, only an idiot would read from a architectural write only register
1163 // PrintError(core->vm_info, core, "Attempting to read from write only register\n");
1168 case APIC_ID_OFFSET:
1169 val = apic->lapic_id.val;
1171 case APIC_VERSION_OFFSET:
1172 val = apic->apic_ver.val;
1175 val = get_apic_tpr(apic);
1178 val = get_apic_apr(apic);
1181 val = get_apic_ppr(apic);
1183 case REMOTE_READ_OFFSET:
1184 val = apic->rem_rd_data;
1187 val = apic->log_dst.val;
1190 val = apic->dst_fmt.val;
1192 case SPURIOUS_INT_VEC_OFFSET:
1193 val = apic->spurious_int.val;
1196 val = apic->err_status.val;
1198 case TMR_LOC_VEC_TBL_OFFSET:
1199 val = apic->tmr_vec_tbl.val;
1201 case LINT0_VEC_TBL_OFFSET:
1202 val = apic->lint0_vec_tbl.val;
1204 case LINT1_VEC_TBL_OFFSET:
1205 val = apic->lint1_vec_tbl.val;
1207 case ERR_VEC_TBL_OFFSET:
1208 val = apic->err_vec_tbl.val;
1210 case TMR_INIT_CNT_OFFSET:
1211 val = apic->tmr_init_cnt;
1213 case TMR_DIV_CFG_OFFSET:
1214 val = apic->tmr_div_cfg.val;
1218 val = *(uint32_t *)(apic->int_en_reg);
1221 val = *(uint32_t *)(apic->int_en_reg + 4);
1224 val = *(uint32_t *)(apic->int_en_reg + 8);
1227 val = *(uint32_t *)(apic->int_en_reg + 12);
1230 val = *(uint32_t *)(apic->int_en_reg + 16);
1233 val = *(uint32_t *)(apic->int_en_reg + 20);
1236 val = *(uint32_t *)(apic->int_en_reg + 24);
1239 val = *(uint32_t *)(apic->int_en_reg + 28);
1243 val = *(uint32_t *)(apic->int_svc_reg);
1246 val = *(uint32_t *)(apic->int_svc_reg + 4);
1249 val = *(uint32_t *)(apic->int_svc_reg + 8);
1252 val = *(uint32_t *)(apic->int_svc_reg + 12);
1255 val = *(uint32_t *)(apic->int_svc_reg + 16);
1258 val = *(uint32_t *)(apic->int_svc_reg + 20);
1261 val = *(uint32_t *)(apic->int_svc_reg + 24);
1264 val = *(uint32_t *)(apic->int_svc_reg + 28);
1268 val = *(uint32_t *)(apic->trig_mode_reg);
1271 val = *(uint32_t *)(apic->trig_mode_reg + 4);
1274 val = *(uint32_t *)(apic->trig_mode_reg + 8);
1277 val = *(uint32_t *)(apic->trig_mode_reg + 12);
1280 val = *(uint32_t *)(apic->trig_mode_reg + 16);
1283 val = *(uint32_t *)(apic->trig_mode_reg + 20);
1286 val = *(uint32_t *)(apic->trig_mode_reg + 24);
1289 val = *(uint32_t *)(apic->trig_mode_reg + 28);
1293 val = *(uint32_t *)(apic->int_req_reg);
1296 val = *(uint32_t *)(apic->int_req_reg + 4);
1299 val = *(uint32_t *)(apic->int_req_reg + 8);
1302 val = *(uint32_t *)(apic->int_req_reg + 12);
1305 val = *(uint32_t *)(apic->int_req_reg + 16);
1308 val = *(uint32_t *)(apic->int_req_reg + 20);
1311 val = *(uint32_t *)(apic->int_req_reg + 24);
1314 val = *(uint32_t *)(apic->int_req_reg + 28);
1316 case TMR_CUR_CNT_OFFSET:
1317 val = apic->tmr_cur_cnt;
1320 // We are not going to implement these....
1321 case THERM_LOC_VEC_TBL_OFFSET:
1322 val = apic->therm_loc_vec_tbl.val;
1324 case PERF_CTR_LOC_VEC_TBL_OFFSET:
1325 val = apic->perf_ctr_loc_vec_tbl.val;
1330 // handled registers
1331 case INT_CMD_LO_OFFSET:
1332 val = apic->int_cmd.lo;
1334 case INT_CMD_HI_OFFSET:
1335 val = apic->int_cmd.hi;
1338 // handle current timer count
1340 // Unhandled Registers
1341 case EXT_INT_LOC_VEC_TBL_OFFSET0:
1342 val = apic->ext_intr_vec_tbl[0].val;
1344 case EXT_INT_LOC_VEC_TBL_OFFSET1:
1345 val = apic->ext_intr_vec_tbl[1].val;
1347 case EXT_INT_LOC_VEC_TBL_OFFSET2:
1348 val = apic->ext_intr_vec_tbl[2].val;
1350 case EXT_INT_LOC_VEC_TBL_OFFSET3:
1351 val = apic->ext_intr_vec_tbl[3].val;
1355 case EXT_APIC_FEATURE_OFFSET:
1356 case EXT_APIC_CMD_OFFSET:
1360 PrintError(core->vm_info, core, "apic %u: core %u: Read from Unhandled APIC Register: %x (getting zero)\n",
1361 apic->lapic_id.val, core->vcpu_id, (uint32_t)reg_addr);
1367 uint_t byte_addr = reg_addr & 0x3;
1368 uint8_t * val_ptr = (uint8_t *)dst;
1370 *val_ptr = *(((uint8_t *)&val) + byte_addr);
1372 } else if ((length == 2) &&
1373 ((reg_addr & 0x3) != 0x3)) {
1374 uint_t byte_addr = reg_addr & 0x3;
1375 uint16_t * val_ptr = (uint16_t *)dst;
1376 *val_ptr = *(((uint16_t *)&val) + byte_addr);
1378 } else if (length == 4) {
1379 uint32_t * val_ptr = (uint32_t *)dst;
1383 PrintError(core->vm_info, core, "apic %u: core %u: Invalid apic read length (%d)\n",
1384 apic->lapic_id.val, core->vcpu_id, length);
1388 PrintDebug(core->vm_info, core, "apic %u: core %u: Read finished (val=%x)\n",
1389 apic->lapic_id.val, core->vcpu_id, *(uint32_t *)dst);
1398 static int apic_write(struct guest_info * core, addr_t guest_addr, void * src, uint_t length, void * priv_data) {
1399 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(priv_data);
1400 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1401 addr_t reg_addr = guest_addr - apic->base_addr;
1402 struct apic_msr * msr = (struct apic_msr *)&(apic->base_addr_msr.value);
1403 uint32_t op_val = *(uint32_t *)src;
1406 PrintDebug(core->vm_info, core, "apic %u: core %u: at %p and priv_data is at %p\n",
1407 apic->lapic_id.val, core->vcpu_id, apic, priv_data);
1409 PrintDebug(core->vm_info, core, "apic %u: core %u: write to address space (%p) (val=%x)\n",
1410 apic->lapic_id.val, core->vcpu_id, (void *)guest_addr, *(uint32_t *)src);
1412 if (msr->apic_enable == 0) {
1413 PrintError(core->vm_info, core, "apic %u: core %u: Write to APIC address space with disabled APIC, apic msr=0x%llx\n",
1414 apic->lapic_id.val, core->vcpu_id, apic->base_addr_msr.value);
1420 PrintError(core->vm_info, core, "apic %u: core %u: Invalid apic write length (%d)\n",
1421 apic->lapic_id.val, length, core->vcpu_id);
1426 case REMOTE_READ_OFFSET:
1427 case APIC_VERSION_OFFSET:
1454 case EXT_APIC_FEATURE_OFFSET:
1456 PrintError(core->vm_info, core, "apic %u: core %u: Attempting to write to read only register %p (error)\n",
1457 apic->lapic_id.val, core->vcpu_id, (void *)reg_addr);
1462 case APIC_ID_OFFSET:
1463 //V3_Print(core->vm_info, core, "apic %u: core %u: my id is being changed to %u\n",
1464 // apic->lapic_id.val, core->vcpu_id, op_val);
1466 apic->lapic_id.val = op_val;
1469 set_apic_tpr(apic,op_val);
1472 PrintDebug(core->vm_info, core, "apic %u: core %u: setting log_dst.val to 0x%x\n",
1473 apic->lapic_id.val, core->vcpu_id, op_val);
1474 flags = v3_lock_irqsave(apic_dev->state_lock);
1475 apic->log_dst.val = op_val;
1476 v3_unlock_irqrestore(apic_dev->state_lock, flags);
1479 flags = v3_lock_irqsave(apic_dev->state_lock);
1480 apic->dst_fmt.val = op_val;
1481 v3_unlock_irqrestore(apic_dev->state_lock, flags);
1483 case SPURIOUS_INT_VEC_OFFSET:
1484 apic->spurious_int.val = op_val;
1487 apic->err_status.val = op_val;
1489 case TMR_LOC_VEC_TBL_OFFSET:
1490 apic->tmr_vec_tbl.val = op_val;
1492 case THERM_LOC_VEC_TBL_OFFSET:
1493 apic->therm_loc_vec_tbl.val = op_val;
1495 case PERF_CTR_LOC_VEC_TBL_OFFSET:
1496 apic->perf_ctr_loc_vec_tbl.val = op_val;
1498 case LINT0_VEC_TBL_OFFSET:
1499 apic->lint0_vec_tbl.val = op_val;
1501 case LINT1_VEC_TBL_OFFSET:
1502 apic->lint1_vec_tbl.val = op_val;
1504 case ERR_VEC_TBL_OFFSET:
1505 apic->err_vec_tbl.val = op_val;
1507 case TMR_INIT_CNT_OFFSET:
1508 apic->tmr_init_cnt = op_val;
1509 apic->tmr_cur_cnt = op_val;
1511 case TMR_CUR_CNT_OFFSET:
1512 apic->tmr_cur_cnt = op_val;
1514 case TMR_DIV_CFG_OFFSET:
1515 PrintDebug(core->vm_info, core, "apic %u: core %u: setting tmr_div_cfg to 0x%x\n",
1516 apic->lapic_id.val, core->vcpu_id, op_val);
1517 apic->tmr_div_cfg.val = op_val;
1521 // Enable mask (256 bits)
1523 *(uint32_t *)(apic->int_en_reg) = op_val;
1526 *(uint32_t *)(apic->int_en_reg + 4) = op_val;
1529 *(uint32_t *)(apic->int_en_reg + 8) = op_val;
1532 *(uint32_t *)(apic->int_en_reg + 12) = op_val;
1535 *(uint32_t *)(apic->int_en_reg + 16) = op_val;
1538 *(uint32_t *)(apic->int_en_reg + 20) = op_val;
1541 *(uint32_t *)(apic->int_en_reg + 24) = op_val;
1544 *(uint32_t *)(apic->int_en_reg + 28) = op_val;
1547 case EXT_INT_LOC_VEC_TBL_OFFSET0:
1548 apic->ext_intr_vec_tbl[0].val = op_val;
1550 case EXT_INT_LOC_VEC_TBL_OFFSET1:
1551 apic->ext_intr_vec_tbl[1].val = op_val;
1553 case EXT_INT_LOC_VEC_TBL_OFFSET2:
1554 apic->ext_intr_vec_tbl[2].val = op_val;
1556 case EXT_INT_LOC_VEC_TBL_OFFSET3:
1557 apic->ext_intr_vec_tbl[3].val = op_val;
1564 apic_do_eoi(core, apic);
1567 case INT_CMD_LO_OFFSET: {
1570 struct v3_gen_ipi tmp_ipi;
1572 apic->int_cmd.lo = op_val;
1574 tmp_ipi.vector = apic->int_cmd.vec;
1575 tmp_ipi.mode = apic->int_cmd.del_mode;
1576 tmp_ipi.logical = apic->int_cmd.dst_mode;
1577 tmp_ipi.trigger_mode = apic->int_cmd.trig_mode;
1578 tmp_ipi.dst_shorthand = apic->int_cmd.dst_shorthand;
1579 tmp_ipi.dst = apic->int_cmd.dst;
1582 tmp_ipi.private_data = NULL;
1585 // V3_Print(core->vm_info, core, "apic %u: core %u: sending cmd 0x%llx to apic %u\n",
1586 // apic->lapic_id.val, core->vcpu_id,
1587 // apic->int_cmd.val, apic->int_cmd.dst);
1589 if (route_ipi(apic_dev, apic, &tmp_ipi) == -1) {
1590 PrintError(core->vm_info, core, "IPI Routing failure\n");
1596 case INT_CMD_HI_OFFSET: {
1597 apic->int_cmd.hi = op_val;
1598 //V3_Print(core->vm_info, core, "apic %u: core %u: writing command high=0x%x\n", apic->lapic_id.val, core->vcpu_id,apic->int_cmd.hi);
1601 // Unhandled Registers
1602 case EXT_APIC_CMD_OFFSET:
1605 PrintError(core->vm_info, core, "apic %u: core %u: Write to Unhandled APIC Register: %x (ignored)\n",
1606 apic->lapic_id.val, core->vcpu_id, (uint32_t)reg_addr);
1611 PrintDebug(core->vm_info, core, "apic %u: core %u: Write finished\n", apic->lapic_id.val, core->vcpu_id);
1619 /* Interrupt Controller Functions */
1622 static int apic_intr_pending(struct guest_info * core, void * private_data) {
1623 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(private_data);
1624 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1628 // Activate all queued IRQ entries
1629 drain_irq_entries(apic);
1631 // Check for newly activated entries
1632 req_irq = get_highest_irr(apic);
1633 svc_irq = get_highest_isr(apic);
1635 // PrintDebug(core->vm_info, core, "apic %u: core %u: req_irq=%d, svc_irq=%d\n",apic->lapic_id.val,info->vcpu_id,req_irq,svc_irq);
1638 if ((req_irq >= 0) &&
1639 (req_irq > svc_irq)) {
1641 // We have a new requested vector that is higher priority than
1642 // the vector that is in-service
1644 uint32_t ppr = get_apic_ppr(apic);
1646 if ((req_irq & 0xf0) > (ppr & 0xf0)) {
1647 // it's also higher priority than the current
1648 // processor priority. Therefore this
1649 // interrupt can go in now.
1652 // processor priority is currently too high
1653 // for this interrupt to go in now.
1654 // note that if tpr=0xf?, then ppr=0xf?
1655 // and thus all vectors will be masked
1656 // as required (tpr=0xf? => all masked)
1660 // the vector that is in service is higher
1661 // priority than any new requested vector
1668 static int apic_get_intr_number(struct guest_info * core, void * private_data) {
1669 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(private_data);
1670 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1671 int req_irq = get_highest_irr(apic);
1672 int svc_irq = get_highest_isr(apic);
1675 // for the logic here, see the comments for apic_intr_pending
1676 if ((req_irq >=0) &&
1677 (req_irq > svc_irq)) {
1679 uint32_t ppr = get_apic_ppr(apic);
1681 if ((req_irq & 0xf0) > (ppr & 0xf0)) {
1684 // hmm, this should not have happened, but, anyway,
1685 // no interrupt is currently ready to go in
1695 int v3_apic_send_ipi(struct v3_vm_info * vm, struct v3_gen_ipi * ipi, void * dev_data) {
1696 struct apic_dev_state * apic_dev = (struct apic_dev_state *)
1697 (((struct vm_device *)dev_data)->private_data);
1699 return route_ipi(apic_dev, NULL, ipi);
1704 static int apic_begin_irq(struct guest_info * core, void * private_data, int irq) {
1705 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(private_data);
1706 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1707 int major_offset = (irq & ~0x00000007) >> 3;
1708 int minor_offset = irq & 0x00000007;
1709 uint8_t *req_location = apic->int_req_reg + major_offset;
1710 uint8_t *svc_location = apic->int_svc_reg + major_offset;
1711 uint8_t flag = 0x01 << minor_offset;
1713 if (*req_location & flag) {
1714 // we will only pay attention to a begin irq if we
1715 // know that we initiated it!
1716 *svc_location |= flag;
1717 *req_location &= ~flag;
1720 //PrintDebug(core->vm_info, core, "apic %u: core %u: begin irq for %d ignored since I don't own it\n",
1721 // apic->lapic_id.val, core->vcpu_id, irq);
1728 /* Timer Functions */
1730 static void apic_inject_timer_intr(struct guest_info *core,
1732 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(priv_data);
1733 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1735 PrintDebug(core->vm_info, core, "apic %u: core %u: Raising APIC Timer interrupt (periodic=%d) (icnt=%d)\n",
1736 apic->lapic_id.val, core->vcpu_id,
1737 apic->tmr_vec_tbl.tmr_mode, apic->tmr_init_cnt);
1739 if (apic_intr_pending(core, priv_data)) {
1740 PrintDebug(core->vm_info, core, "apic %u: core %u: Overriding pending IRQ %d\n",
1741 apic->lapic_id.val, core->vcpu_id,
1742 apic_get_intr_number(core, priv_data));
1745 if (activate_internal_irq(apic, APIC_TMR_INT) == -1) {
1746 PrintError(core->vm_info, core, "apic %u: core %u: Could not raise Timer interrupt\n",
1747 apic->lapic_id.val, core->vcpu_id);
1756 static void apic_update_time(struct guest_info * core,
1757 uint64_t cpu_cycles, uint64_t cpu_freq,
1759 struct apic_dev_state * apic_dev = (struct apic_dev_state *)(priv_data);
1760 struct apic_state * apic = &(apic_dev->apics[core->vcpu_id]);
1762 // The 32 bit GCC runtime is a pile of shit
1764 uint64_t tmr_ticks = 0;
1766 uint32_t tmr_ticks = 0;
1769 uint8_t tmr_div = *(uint8_t *)&(apic->tmr_div_cfg.val);
1770 uint_t shift_num = 0;
1773 // Check whether this is true:
1774 // -> If the Init count is zero then the timer is disabled
1775 // and doesn't just blitz interrupts to the CPU
1776 if ((apic->tmr_init_cnt == 0) ||
1777 ( (apic->tmr_vec_tbl.tmr_mode == APIC_TMR_ONESHOT) &&
1778 (apic->tmr_cur_cnt == 0))) {
1779 //PrintDebug(core->vm_info, core, "apic %u: core %u: APIC timer not yet initialized\n",apic->lapic_id.val,info->vcpu_id);
1797 case APIC_TMR_DIV16:
1800 case APIC_TMR_DIV32:
1803 case APIC_TMR_DIV64:
1806 case APIC_TMR_DIV128:
1810 PrintError(core->vm_info, core, "apic %u: core %u: Invalid Timer Divider configuration\n",
1811 apic->lapic_id.val, core->vcpu_id);
1815 tmr_ticks = cpu_cycles >> shift_num;
1816 // PrintDebug(core->vm_info, core, "Timer Ticks: %p\n", (void *)tmr_ticks);
1818 if (tmr_ticks < apic->tmr_cur_cnt) {
1819 apic->tmr_cur_cnt -= tmr_ticks;
1820 #ifdef V3_CONFIG_APIC_ENQUEUE_MISSED_TMR_IRQS
1821 if (apic->missed_ints && !apic_intr_pending(core, priv_data)) {
1822 PrintDebug(core->vm_info, core, "apic %u: core %u: Injecting queued APIC timer interrupt.\n",
1823 apic->lapic_id.val, core->vcpu_id);
1824 apic_inject_timer_intr(core, priv_data);
1825 apic->missed_ints--;
1827 #endif /* CONFIG_APIC_ENQUEUE_MISSED_TMR_IRQS */
1829 tmr_ticks -= apic->tmr_cur_cnt;
1830 apic->tmr_cur_cnt = 0;
1832 apic_inject_timer_intr(core, priv_data);
1834 if (apic->tmr_vec_tbl.tmr_mode == APIC_TMR_PERIODIC) {
1835 int queued_ints = tmr_ticks / apic->tmr_init_cnt;
1836 tmr_ticks = tmr_ticks % apic->tmr_init_cnt;
1837 apic->tmr_cur_cnt = apic->tmr_init_cnt - tmr_ticks;
1838 apic->missed_ints += queued_ints;
1845 static struct intr_ctrl_ops intr_ops = {
1846 .intr_pending = apic_intr_pending,
1847 .get_intr_number = apic_get_intr_number,
1848 .begin_irq = apic_begin_irq,
1852 static struct v3_timer_ops timer_ops = {
1853 .update_timer = apic_update_time,
1859 static int apic_free(struct apic_dev_state * apic_dev) {
1861 struct v3_vm_info * vm = NULL;
1863 for (i = 0; i < apic_dev->num_apics; i++) {
1864 struct apic_state * apic = &(apic_dev->apics[i]);
1865 struct guest_info * core = apic->core;
1869 v3_remove_intr_controller(core, apic->controller_handle);
1872 v3_remove_timer(core, apic->timer);
1879 v3_unhook_msr(vm, BASE_ADDR_MSR);
1885 #ifdef V3_CONFIG_CHECKPOINT
1888 #define MAKE_KEY(x) snprintf(key,KEY_MAX,"%s%d",x,i);
1890 static int apic_save(struct v3_chkpt_ctx * ctx, void * private_data) {
1891 struct apic_dev_state * apic_state = (struct apic_dev_state *)private_data;
1896 V3_CHKPT_SAVE(ctx, "NUM_APICS", apic_state->num_apics,savefailout);
1898 for (i = 0; i < apic_state->num_apics; i++) {
1899 drain_irq_entries(&(apic_state->apics[i]));
1901 MAKE_KEY("BASE_ADDR");
1902 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].base_addr,savefailout);
1903 MAKE_KEY("BASE_ADDR_MSR");
1904 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].base_addr_msr,savefailout);
1905 MAKE_KEY("LAPIC_ID");
1906 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].lapic_id,savefailout);
1907 MAKE_KEY("APIC_VER");
1908 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].apic_ver,savefailout);
1909 MAKE_KEY("EXT_APIC_CTRL");
1910 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].ext_apic_ctrl,savefailout);
1911 MAKE_KEY("LOCAL_VEC_TBL");
1912 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].local_vec_tbl,savefailout);
1913 MAKE_KEY("TMR_VEC_TBL");
1914 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].tmr_vec_tbl,savefailout);
1915 MAKE_KEY("TMR_DIV_CFG");
1916 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].tmr_div_cfg,savefailout);
1917 MAKE_KEY("LINT0_VEC_TBL");
1918 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].lint0_vec_tbl,savefailout);
1919 MAKE_KEY("LINT1_VEC_TBL");
1920 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].lint1_vec_tbl,savefailout);
1921 MAKE_KEY("PERF_CTR_LOC_VEC_TBL");
1922 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].perf_ctr_loc_vec_tbl,savefailout);
1923 MAKE_KEY("THERM_LOC_VEC_TBL");
1924 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].therm_loc_vec_tbl,savefailout);
1925 MAKE_KEY("ERR_VEC_TBL");
1926 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].err_vec_tbl,savefailout);
1927 MAKE_KEY("ERR_STATUS");
1928 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].err_status,savefailout);
1929 MAKE_KEY("SPURIOUS_INT");
1930 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].spurious_int,savefailout);
1931 MAKE_KEY("INT_CMD");
1932 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].int_cmd,savefailout);
1933 MAKE_KEY("LOG_DST");
1934 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].log_dst,savefailout);
1935 MAKE_KEY("DST_FMT");
1936 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].dst_fmt,savefailout);
1938 // APR and PPR are stored only for compatability
1939 // TPR is in APIC_TPR, APR and PPR are derived
1941 temp = get_apic_apr(&(apic_state->apics[i]));
1942 MAKE_KEY("ARB_PRIO");
1943 V3_CHKPT_SAVE(ctx, key, temp,savefailout);
1944 temp = get_apic_tpr(&(apic_state->apics[i]));
1945 MAKE_KEY("TASK_PRIO");
1946 V3_CHKPT_SAVE(ctx,key,temp,savefailout);
1947 temp = get_apic_ppr(&(apic_state->apics[i]));
1948 MAKE_KEY("PROC_PRIO");
1949 V3_CHKPT_SAVE(ctx, key,temp,savefailout);
1951 MAKE_KEY("EXT_APIC_FEATURE");
1952 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].ext_apic_feature,savefailout);
1953 MAKE_KEY("SPEC_EOI");
1954 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].spec_eoi,savefailout);
1955 MAKE_KEY("TMR_CUR_CNT");
1956 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].tmr_cur_cnt,savefailout);
1958 MAKE_KEY("TMR_INIT_CNT");
1959 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].tmr_init_cnt,savefailout);
1960 MAKE_KEY("EXT_INTR_VEC_TBL");
1961 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].ext_intr_vec_tbl,savefailout);
1963 MAKE_KEY("REM_RD_DATA");
1964 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].rem_rd_data,savefailout);
1965 MAKE_KEY("IPI_STATE");
1966 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].ipi_state,savefailout);
1967 MAKE_KEY("INT_REQ_REG");
1968 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].int_req_reg,savefailout);
1969 MAKE_KEY("INT_SVC_REG");
1970 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].int_svc_reg,savefailout);
1971 MAKE_KEY("INT_EN_REG");
1972 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].int_en_reg,savefailout);
1973 MAKE_KEY("TRIG_MODE_REG");
1974 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].trig_mode_reg,savefailout);
1976 V3_CHKPT_SAVE(ctx, key, apic_state->apics[i].eoi,savefailout);
1983 PrintError(VM_NONE, VCORE_NONE, "Failed to save apic\n");
1987 static int apic_load(struct v3_chkpt_ctx * ctx, void * private_data) {
1988 struct apic_dev_state *apic_state = (struct apic_dev_state *)private_data;
1993 V3_CHKPT_LOAD(ctx,"NUM_APICS", apic_state->num_apics, loadfailout);
1995 for (i = 0; i < apic_state->num_apics; i++) {
1996 drain_irq_entries(&(apic_state->apics[i]));
1998 MAKE_KEY("BASE_ADDR");
1999 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].base_addr,loadfailout);
2000 MAKE_KEY("BASE_ADDR_MSR");
2001 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].base_addr_msr,loadfailout);
2002 MAKE_KEY("LAPIC_ID");
2003 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].lapic_id,loadfailout);
2004 MAKE_KEY("APIC_VER");
2005 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].apic_ver,loadfailout);
2006 MAKE_KEY("EXT_APIC_CTRL");
2007 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].ext_apic_ctrl,loadfailout);
2008 MAKE_KEY("LOCAL_VEC_TBL");
2009 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].local_vec_tbl,loadfailout);
2010 MAKE_KEY("TMR_VEC_TBL");
2011 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].tmr_vec_tbl,loadfailout);
2012 MAKE_KEY("TMR_DIV_CFG");
2013 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].tmr_div_cfg,loadfailout);
2014 MAKE_KEY("LINT0_VEC_TBL");
2015 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].lint0_vec_tbl,loadfailout);
2016 MAKE_KEY("LINT1_VEC_TBL");
2017 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].lint1_vec_tbl,loadfailout);
2018 MAKE_KEY("PERF_CTR_LOC_VEC_TBL");
2019 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].perf_ctr_loc_vec_tbl,loadfailout);
2020 MAKE_KEY("THERM_LOC_VEC_TBL");
2021 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].therm_loc_vec_tbl,loadfailout);
2022 MAKE_KEY("ERR_VEC_TBL");
2023 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].err_vec_tbl,loadfailout);
2024 MAKE_KEY("ERR_STATUS");
2025 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].err_status,loadfailout);
2026 MAKE_KEY("SPURIOUS_INT");
2027 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].spurious_int,loadfailout);
2028 MAKE_KEY("INT_CMD");
2029 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].int_cmd,loadfailout);
2030 MAKE_KEY("LOG_DST");
2031 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].log_dst,loadfailout);
2032 MAKE_KEY("DST_FMT");
2033 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].dst_fmt,loadfailout);
2035 // APR and PPR are stored only for compatability
2036 // TPR is in APIC_TPR, APR and PPR are derived
2038 MAKE_KEY("ARB_PRIO");
2039 V3_CHKPT_LOAD(ctx, key, temp,loadfailout);
2042 MAKE_KEY("TASK_PRIO");
2043 V3_CHKPT_LOAD(ctx,key,temp,loadfailout);
2044 set_apic_tpr(&(apic_state->apics[i]),temp);
2046 MAKE_KEY("PROC_PRIO");
2047 V3_CHKPT_LOAD(ctx, key,temp,loadfailout);
2051 MAKE_KEY("EXT_APIC_FEATURE");
2052 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].ext_apic_feature,loadfailout);
2053 MAKE_KEY("SPEC_EOI");
2054 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].spec_eoi,loadfailout);
2055 MAKE_KEY("TMR_CUR_CNT");
2056 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].tmr_cur_cnt,loadfailout);
2058 MAKE_KEY("TMR_INIT_CNT");
2059 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].tmr_init_cnt,loadfailout);
2060 MAKE_KEY("EXT_INTR_VEC_TBL");
2061 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].ext_intr_vec_tbl,loadfailout);
2063 MAKE_KEY("REM_RD_DATA");
2064 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].rem_rd_data,loadfailout);
2065 MAKE_KEY("IPI_STATE");
2066 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].ipi_state,loadfailout);
2067 MAKE_KEY("INT_REQ_REG");
2068 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].int_req_reg,loadfailout);
2069 MAKE_KEY("INT_SVC_REG");
2070 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].int_svc_reg,loadfailout);
2071 MAKE_KEY("INT_EN_REG");
2072 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].int_en_reg,loadfailout);
2073 MAKE_KEY("TRIG_MODE_REG");
2074 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].trig_mode_reg,loadfailout);
2076 V3_CHKPT_LOAD(ctx, key, apic_state->apics[i].eoi,loadfailout);
2083 PrintError(VM_NONE,VCORE_NONE, "Failed to load apic\n");
2090 static struct v3_device_ops dev_ops = {
2091 .free = (int (*)(void *))apic_free,
2092 #ifdef V3_CONFIG_CHECKPOINT
2100 static int apic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
2101 char * dev_id = v3_cfg_val(cfg, "ID");
2102 struct apic_dev_state * apic_dev = NULL;
2105 PrintDebug(vm, VCORE_NONE, "apic: creating an APIC for each core\n");
2107 apic_dev = (struct apic_dev_state *)V3_Malloc(sizeof(struct apic_dev_state) +
2108 sizeof(struct apic_state) * vm->num_cores);
2112 PrintError(vm, VCORE_NONE, "Failed to allocate space for APIC\n");
2116 apic_dev->num_apics = vm->num_cores;
2117 v3_lock_init(&(apic_dev->state_lock));
2119 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, apic_dev);
2122 PrintError(vm, VCORE_NONE, "apic: Could not attach device %s\n", dev_id);
2128 for (i = 0; i < vm->num_cores; i++) {
2129 struct apic_state * apic = &(apic_dev->apics[i]);
2130 struct guest_info * core = &(vm->cores[i]);
2134 init_apic_state(apic, i);
2136 apic->controller_handle = v3_register_intr_controller(core, &intr_ops, apic_dev);
2138 apic->timer = v3_add_timer(core, &timer_ops, apic_dev);
2140 if (apic->timer == NULL) {
2141 PrintError(vm, VCORE_NONE,"APIC: Failed to attach timer to core %d\n", i);
2142 v3_remove_device(dev);
2146 v3_hook_full_mem(vm, core->vcpu_id, apic->base_addr, apic->base_addr + PAGE_SIZE_4KB, apic_read, apic_write, apic_dev);
2148 PrintDebug(vm, VCORE_NONE, "apic %u: (setup device): done, my id is %u\n", i, apic->lapic_id.val);
2151 #ifdef V3_CONFIG_DEBUG_APIC
2152 for (i = 0; i < vm->num_cores; i++) {
2153 struct apic_state * apic = &(apic_dev->apics[i]);
2154 PrintDebug(vm, VCORE_NONE, "apic: sanity check: apic %u (at %p) has id %u and msr value %llx and core at %p\n",
2155 i, apic, apic->lapic_id.val, apic->base_addr_msr.value,apic->core);
2160 PrintDebug(vm, VCORE_NONE, "apic: priv_data is at %p\n", apic_dev);
2162 v3_hook_msr(vm, BASE_ADDR_MSR, read_apic_msr, write_apic_msr, apic_dev);
2169 device_register("LAPIC", apic_init)