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 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 struct vm_device * apic_dev;
143 static void init_ioapic_state(struct io_apic_state * ioapic, uint32_t id) {
145 ioapic->base_addr = IO_APIC_BASE_ADDR;
146 ioapic->index_reg = 0;
148 ioapic->ioapic_id.id = id;
149 ioapic->ioapic_ver.val = 0x00170011;
150 ioapic->ioapic_arb_id.val = 0x00000000;
152 for (i = 0; i < 24; i++) {
153 ioapic->redir_tbl[i].val = 0x0001000000000000LL;
154 // Mask all interrupts until they are enabled....
155 ioapic->redir_tbl[i].mask = 1;
158 // special case redir_tbl[0] for pin 0 as ExtInt for Virtual Wire Mode
159 // ioapic->redir_tbl[0].del_mode=EXTINT;
160 // ioapic->redir_tbl[0].mask=0;
164 static int ioapic_read(struct guest_info * core, addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
165 struct vm_device * dev = (struct vm_device *)priv_data;
166 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
167 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
168 uint32_t * op_val = (uint32_t *)dst;
170 PrintDebug("ioapic %u: IOAPIC Read at %p\n", ioapic->ioapic_id.id, (void *)guest_addr);
172 if (reg_tgt == 0x00) {
173 *op_val = ioapic->index_reg;
174 } else if (reg_tgt == 0x10) {
176 switch (ioapic->index_reg) {
178 *op_val = ioapic->ioapic_id.val;
181 *op_val = ioapic->ioapic_ver.val;
184 *op_val = ioapic->ioapic_arb_id.val;
187 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) >> 1;
188 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 1;
190 if (redir_index > 0x3f) {
191 PrintError("ioapic %u: Invalid redirection table entry %x\n", ioapic->ioapic_id.id, (uint32_t)redir_index);
196 *op_val = ioapic->redir_tbl[redir_index].hi;
198 *op_val = ioapic->redir_tbl[redir_index].lo;
204 PrintDebug("ioapic %u: IOAPIC Read at %p gave value 0x%x\n", ioapic->ioapic_id.id, (void *)guest_addr, *op_val);
210 static int ioapic_write(struct guest_info * core, addr_t guest_addr, void * src, uint_t length, void * priv_data) {
211 struct vm_device * dev = (struct vm_device *)priv_data;
212 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
213 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
214 uint32_t op_val = *(uint32_t *)src;
216 PrintDebug("ioapic %u: IOAPIC Write at %p (val = %d)\n", ioapic->ioapic_id.id, (void *)guest_addr, *(uint32_t *)src);
218 if (reg_tgt == 0x00) {
219 PrintDebug("ioapic %u: Setting ioapic index register to 0x%x.\n", ioapic->ioapic_id.id, op_val);
220 ioapic->index_reg = op_val;
221 } else if (reg_tgt == 0x10) {
223 switch (ioapic->index_reg) {
225 // What does this do to our relationship with the ICC bus?
226 ioapic->ioapic_id.val = op_val;
229 // GPF/PageFault/Ignore?
230 PrintError("ioapic %u: Writing to read only IOAPIC register\n", ioapic->ioapic_id.id);
233 ioapic->ioapic_arb_id.val = op_val;
237 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) >> 1;
238 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 1;
240 PrintDebug("ioapic %u: Writing value 0x%x to redirection entry %u (%s)\n",
241 ioapic->ioapic_id.id, op_val, redir_index, hi_val ? "hi" : "low");
243 if (redir_index > 0x3f) {
244 PrintError("ioapic %u: Invalid redirection table entry %x\n", ioapic->ioapic_id.id, (uint32_t)redir_index);
248 PrintDebug("ioapic %u: Writing to hi of pin %d\n", ioapic->ioapic_id.id, redir_index);
249 ioapic->redir_tbl[redir_index].hi = op_val;
251 PrintDebug("ioapic %u: Writing to lo of pin %d\n", ioapic->ioapic_id.id, redir_index);
252 op_val &= REDIR_LO_MASK;
253 ioapic->redir_tbl[redir_index].lo &= ~REDIR_LO_MASK;
254 ioapic->redir_tbl[redir_index].lo |= op_val;
264 static int ioapic_raise_irq(struct v3_vm_info * vm, void * private_data, int irq) {
265 struct vm_device * dev = (struct vm_device *)private_data;
266 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
267 struct redir_tbl_entry * irq_entry = NULL;
270 PrintDebug("ioapic %u: IRQ out of range of IO APIC\n", ioapic->ioapic_id.id);
274 irq_entry = &(ioapic->redir_tbl[irq]);
276 if (irq_entry->mask == 0) {
277 struct v3_gen_ipi ipi;
279 PrintDebug("ioapic %u: IOAPIC Signalling APIC to raise INTR %d\n",
280 ioapic->ioapic_id.id, irq_entry->vec);
283 ipi.vector = irq_entry->vec;
284 ipi.mode = irq_entry->del_mode;
285 ipi.logical = irq_entry->dst_mode;
286 ipi.trigger_mode = irq_entry->trig_mode;
287 ipi.dst = irq_entry->dst_field;
288 ipi.dst_shorthand = 0;
290 // Need to add destination argument here...
291 if (v3_apic_send_ipi(vm, ioapic->apic_dev, &ipi) == -1) {
292 PrintError("Error sending IPI to apic %d\n", ipi.dst);
300 /* I don't know if we can do anything here.... */
301 static int ioapic_lower_irq(struct v3_vm_info * vm, void * private_data, int irq) {
305 static struct intr_router_ops router_ops = {
306 .raise_intr = ioapic_raise_irq,
307 .lower_intr = ioapic_lower_irq,
313 static int io_apic_free(struct vm_device * dev) {
314 // struct guest_info * info = dev->vm;
320 static struct v3_device_ops dev_ops = {
321 .free = io_apic_free,
329 static int ioapic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
330 struct vm_device * apic_dev = v3_find_dev(vm, v3_cfg_val(cfg, "apic"));
331 char * dev_id = v3_cfg_val(cfg, "ID");
334 PrintDebug("ioapic: Creating IO APIC\n");
336 struct io_apic_state * ioapic = (struct io_apic_state *)V3_Malloc(sizeof(struct io_apic_state));
338 ioapic->apic_dev = apic_dev;
340 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, ioapic);
343 if (v3_attach_device(vm, dev) == -1) {
344 PrintError("ioapic: Could not attach device %s\n", dev_id);
349 v3_register_intr_router(vm, &router_ops, dev);
351 init_ioapic_state(ioapic,vm->num_cores);
353 v3_hook_full_mem(vm, V3_MEM_CORE_ANY, ioapic->base_addr, ioapic->base_addr + PAGE_SIZE_4KB,
354 ioapic_read, ioapic_write, dev);
360 device_register("IOAPIC", ioapic_init)