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 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_dev_mgr.h>
23 #include <devices/apic.h>
24 #include <palacios/vm_guest.h>
26 #ifndef V3_CONFIG_DEBUG_IO_APIC
28 #define PrintDebug(fmt, args...)
33 #define IO_APIC_BASE_ADDR 0xfec00000
36 #define IOAPIC_ID_REG 0x00
37 #define IOAPIC_VER_REG 0x01
38 #define IOAPIC_ARB_REG 0x02
40 #define IOAPIC_REDIR_BASE_REG 0x10
42 #define REDIR_LO_MASK ~0x00005000
44 struct ioapic_reg_sel {
50 } __attribute__((packed));
51 } __attribute__((packed));
52 } __attribute__((packed));
54 struct ioapic_id_reg {
61 } __attribute__((packed));
62 } __attribute__((packed));
63 } __attribute__((packed));
65 struct ioapic_ver_reg {
73 } __attribute__((packed));
74 } __attribute__((packed));
75 } __attribute__((packed));
78 struct ioapic_arb_reg {
85 } __attribute__((packed));
86 } __attribute__((packed));
87 } __attribute__((packed));
90 struct redir_tbl_entry {
96 } __attribute__((packed));
101 #define LOWEST_PRIOR 0x1
108 #define PHSYICAL_DST_MODE 0
109 #define LOGICAL_DST_MODE 1
111 uint_t del_status : 1;
113 #define HIGH_ACTIVE 0
117 uint_t trig_mode : 1;
120 uint_t dst_field : 8;
121 } __attribute__((packed));
122 } __attribute__((packed));
123 } __attribute__((packed));
127 struct io_apic_state {
132 struct ioapic_id_reg ioapic_id;
133 struct ioapic_ver_reg ioapic_ver;
134 struct ioapic_arb_reg ioapic_arb_id;
136 struct redir_tbl_entry redir_tbl[24];
138 void * apic_dev_data;
140 void * router_handle;
142 struct v3_vm_info * vm;
147 static void init_ioapic_state(struct io_apic_state * ioapic, uint32_t id) {
149 ioapic->base_addr = IO_APIC_BASE_ADDR;
150 ioapic->index_reg = 0;
152 ioapic->ioapic_id.id = id;
153 ioapic->ioapic_ver.val = 0x00170011;
154 ioapic->ioapic_arb_id.val = 0x00000000;
156 for (i = 0; i < 24; i++) {
157 ioapic->redir_tbl[i].val = 0x0001000000000000LL;
158 // Mask all interrupts until they are enabled....
159 ioapic->redir_tbl[i].mask = 1;
162 // special case redir_tbl[0] for pin 0 as ExtInt for Virtual Wire Mode
163 // ioapic->redir_tbl[0].del_mode=EXTINT;
164 // ioapic->redir_tbl[0].mask=0;
168 static int ioapic_read(struct guest_info * core, addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
169 struct io_apic_state * ioapic = (struct io_apic_state *)(priv_data);
170 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
171 uint32_t * op_val = (uint32_t *)dst;
173 PrintDebug(core->vm_info, core, "ioapic %u: IOAPIC Read at %p\n", ioapic->ioapic_id.id, (void *)guest_addr);
175 if (reg_tgt == 0x00) {
176 *op_val = ioapic->index_reg;
177 } else if (reg_tgt == 0x10) {
179 switch (ioapic->index_reg) {
181 *op_val = ioapic->ioapic_id.val;
184 *op_val = ioapic->ioapic_ver.val;
187 *op_val = ioapic->ioapic_arb_id.val;
190 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) >> 1;
191 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 1;
193 if (redir_index > 0x3f) {
194 PrintError(core->vm_info, core, "ioapic %u: Invalid redirection table entry %x\n", ioapic->ioapic_id.id, (uint32_t)redir_index);
199 *op_val = ioapic->redir_tbl[redir_index].hi;
201 *op_val = ioapic->redir_tbl[redir_index].lo;
207 PrintDebug(core->vm_info, core, "ioapic %u: IOAPIC Read at %p gave value 0x%x\n", ioapic->ioapic_id.id, (void *)guest_addr, *op_val);
213 static int ioapic_write(struct guest_info * core, addr_t guest_addr, void * src, uint_t length, void * priv_data) {
214 struct io_apic_state * ioapic = (struct io_apic_state *)(priv_data);
215 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
216 uint32_t op_val = *(uint32_t *)src;
218 PrintDebug(core->vm_info, core, "ioapic %u: IOAPIC Write at %p (val = %d)\n", ioapic->ioapic_id.id, (void *)guest_addr, *(uint32_t *)src);
220 if (reg_tgt == 0x00) {
221 PrintDebug(core->vm_info, core, "ioapic %u: Setting ioapic index register to 0x%x.\n", ioapic->ioapic_id.id, op_val);
222 ioapic->index_reg = op_val;
223 } else if (reg_tgt == 0x10) {
225 switch (ioapic->index_reg) {
227 // What does this do to our relationship with the ICC bus?
228 ioapic->ioapic_id.val = op_val;
231 // GPF/PageFault/Ignore?
232 PrintError(core->vm_info, core, "ioapic %u: Writing to read only IOAPIC register\n", ioapic->ioapic_id.id);
235 ioapic->ioapic_arb_id.val = op_val;
239 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) >> 1;
240 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 1;
242 PrintDebug(core->vm_info, core, "ioapic %u: Writing value 0x%x to redirection entry %u (%s)\n",
243 ioapic->ioapic_id.id, op_val, redir_index, hi_val ? "hi" : "low");
245 if (redir_index > 0x3f) {
246 PrintError(core->vm_info, core, "ioapic %u: Invalid redirection table entry %x\n", ioapic->ioapic_id.id, (uint32_t)redir_index);
250 PrintDebug(core->vm_info, core, "ioapic %u: Writing to hi of pin %d\n", ioapic->ioapic_id.id, redir_index);
251 ioapic->redir_tbl[redir_index].hi = op_val;
253 PrintDebug(core->vm_info, core, "ioapic %u: Writing to lo of pin %d\n", ioapic->ioapic_id.id, redir_index);
254 op_val &= REDIR_LO_MASK;
255 ioapic->redir_tbl[redir_index].lo &= ~REDIR_LO_MASK;
256 ioapic->redir_tbl[redir_index].lo |= op_val;
266 static int ioapic_raise_irq(struct v3_vm_info * vm, void * private_data, struct v3_irq * irq) {
267 struct io_apic_state * ioapic = (struct io_apic_state *)(private_data);
268 struct redir_tbl_entry * irq_entry = NULL;
269 uint8_t irq_num = irq->irq;
272 // IRQ 0 being raised, in the Palacios context, means the PIT
273 // However, the convention is that it is the PIC that is connected
274 // to PIN 0 of the IOAPIC and the PIT is connected to pin 2
275 // Hence we convert this to the relvant pin. In the future,
276 // the PIC may signal to the IOAPIC in a different path.
277 // Yes, this is kind of hideous, but it is needed to have the
278 // PIT correctly show up via the IOAPIC
283 PrintDebug(vm, VCORE_NONE, "ioapic %u: IRQ out of range of IO APIC\n", ioapic->ioapic_id.id);
287 irq_entry = &(ioapic->redir_tbl[irq_num]);
289 if (irq_entry->mask == 0) {
290 struct v3_gen_ipi ipi;
292 PrintDebug(vm, VCORE_NONE, "ioapic %u: IOAPIC Signaling APIC to raise INTR %d\n",
293 ioapic->ioapic_id.id, irq_entry->vec);
296 ipi.vector = irq_entry->vec;
297 ipi.mode = irq_entry->del_mode;
298 ipi.logical = irq_entry->dst_mode;
299 ipi.trigger_mode = irq_entry->trig_mode;
300 ipi.dst = irq_entry->dst_field;
301 ipi.dst_shorthand = 0;
304 ipi.private_data = irq->private_data;
306 PrintDebug(vm, VCORE_NONE, "ioapic %u: IPI: vector 0x%x, mode 0x%x, logical 0x%x, trigger 0x%x, dst 0x%x, shorthand 0x%x\n",
307 ioapic->ioapic_id.id, ipi.vector, ipi.mode, ipi.logical, ipi.trigger_mode, ipi.dst, ipi.dst_shorthand);
308 // Need to add destination argument here...
309 if (v3_apic_send_ipi(vm, &ipi, ioapic->apic_dev_data) == -1) {
310 PrintError(vm, VCORE_NONE, "Error sending IPI to apic %d\n", ipi.dst);
318 /* I don't know if we can do anything here.... */
319 static int ioapic_lower_irq(struct v3_vm_info * vm, void * private_data, struct v3_irq * irq) {
323 static struct intr_router_ops router_ops = {
324 .raise_intr = ioapic_raise_irq,
325 .lower_intr = ioapic_lower_irq,
331 static int io_apic_free(struct io_apic_state * ioapic) {
333 v3_remove_intr_router(ioapic->vm, ioapic->router_handle);
342 #ifdef V3_CONFIG_CHECKPOINT
343 static int io_apic_save(struct v3_chkpt_ctx * ctx, void * private_data) {
344 struct io_apic_state * io_apic = (struct io_apic_state *)private_data;
347 V3_CHKPT_SAVE(ctx, "BASE_ADDR" ,io_apic->base_addr,savefailout);
348 V3_CHKPT_SAVE(ctx, "INDEX_REG", io_apic->index_reg,savefailout);
349 V3_CHKPT_SAVE(ctx, "IOAPIC_ID", io_apic->ioapic_id,savefailout);
350 V3_CHKPT_SAVE(ctx, "IOAPIC_VER", io_apic->ioapic_ver,savefailout);
351 V3_CHKPT_SAVE(ctx, "IOAPIC_ARB_ID", io_apic->ioapic_arb_id,savefailout);
352 V3_CHKPT_SAVE(ctx, "REDIR_TABLE", io_apic->redir_tbl,savefailout);
357 PrintError(VM_NONE, VCORE_NONE, "ioapic save failed\n");
361 static int io_apic_load(struct v3_chkpt_ctx * ctx, void * private_data) {
362 struct io_apic_state * io_apic = (struct io_apic_state *)private_data;
364 V3_CHKPT_LOAD(ctx, "BASE_ADDR", io_apic->base_addr,loadfailout);
365 V3_CHKPT_LOAD(ctx, "INDEX_REG", io_apic->index_reg,loadfailout);
366 V3_CHKPT_LOAD(ctx, "IOAPIC_ID", io_apic->ioapic_id,loadfailout);
367 V3_CHKPT_LOAD(ctx, "IOAPIC_VER", io_apic->ioapic_ver,loadfailout);
368 V3_CHKPT_LOAD(ctx, "IOAPIC_ARB_ID", io_apic->ioapic_arb_id,loadfailout);
369 V3_CHKPT_LOAD(ctx, "REDIR_TABLE", io_apic->redir_tbl,loadfailout);
374 PrintError(VM_NONE, VCORE_NONE, "ioapic load failed\n");
382 static struct v3_device_ops dev_ops = {
383 .free = (int (*)(void *))io_apic_free,
384 #ifdef V3_CONFIG_CHECKPOINT
385 .save = io_apic_save,
392 static int ioapic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
393 struct vm_device * apic_dev = v3_find_dev(vm, v3_cfg_val(cfg, "apic"));
394 char * dev_id = v3_cfg_val(cfg, "ID");
397 PrintDebug(vm, VCORE_NONE, "ioapic: Creating IO APIC\n");
399 struct io_apic_state * ioapic = (struct io_apic_state *)V3_Malloc(sizeof(struct io_apic_state));
402 PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
406 ioapic->apic_dev_data = apic_dev;
408 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, ioapic);
411 PrintError(vm, VCORE_NONE, "ioapic: Could not attach device %s\n", dev_id);
416 ioapic->router_handle = v3_register_intr_router(vm, &router_ops, ioapic);
419 init_ioapic_state(ioapic, vm->num_cores);
421 v3_hook_full_mem(vm, V3_MEM_CORE_ANY, ioapic->base_addr, ioapic->base_addr + PAGE_SIZE_4KB,
422 ioapic_read, ioapic_write, ioapic);
428 device_register("IOAPIC", ioapic_init)