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".
20 #include <devices/io_apic.h>
21 #include <palacios/vmm.h>
23 #include <devices/apic.h>
26 #define IO_APIC_BASE_ADDR 0xfec00000
29 #define IOAPIC_ID_REG 0x00
30 #define IOAPIC_VER_REG 0x01
31 #define IOAPIC_ARB_REG 0x02
33 #define IOAPIC_REDIR_BASE_REG 0x10
35 #define REDIR_LO_MASK ~0x00005000
37 struct ioapic_reg_sel {
43 } __attribute__((packed));
44 } __attribute__((packed));
45 } __attribute__((packed));
47 struct ioapic_id_reg {
54 } __attribute__((packed));
55 } __attribute__((packed));
56 } __attribute__((packed));
58 struct ioapic_ver_reg {
66 } __attribute__((packed));
67 } __attribute__((packed));
68 } __attribute__((packed));
71 struct ioapic_arb_reg {
78 } __attribute__((packed));
79 } __attribute__((packed));
80 } __attribute__((packed));
83 struct redir_tbl_entry {
89 } __attribute__((packed));
94 #define LOWEST_PRIOR 0x1
101 #define PHSYICAL_DST_MODE 0
102 #define LOGICAL_DST_MODE 1
104 uint_t del_status : 1;
106 #define HIGH_ACTIVE 0
110 uint_t trig_mode : 1;
113 uint_t dst_field : 8;
114 } __attribute__((packed));
115 } __attribute__((packed));
116 } __attribute__((packed));
120 struct io_apic_state {
125 struct ioapic_id_reg ioapic_id;
126 struct ioapic_ver_reg ioapic_ver;
127 struct ioapic_arb_reg ioapic_arb_id;
129 struct redir_tbl_entry redir_tbl[24];
131 // This is a temporary method of communication between the IOAPIC and the LAPIC
132 struct vm_device * apic;
137 static void init_ioapic_state(struct io_apic_state * ioapic) {
139 ioapic->base_addr = IO_APIC_BASE_ADDR;
140 ioapic->index_reg = 0;
142 ioapic->ioapic_id.val = 0x00000000;
143 ioapic->ioapic_ver.val = 0x00170011;
144 ioapic->ioapic_arb_id.val = 0x00000000;
146 for (i = 0; i < 24; i++) {
147 ioapic->redir_tbl[i].val = 0x0001000000000000LL;
148 // Mask all interrupts until they are enabled....
149 ioapic->redir_tbl[i].mask = 1;
154 static int ioapic_read(addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
155 struct vm_device * dev = (struct vm_device *)priv_data;
156 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
157 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
158 uint32_t * op_val = (uint32_t *)dst;
160 PrintDebug("IOAPIC Read at %p\n", (void *)guest_addr);
162 if (reg_tgt == 0x00) {
163 *op_val = ioapic->index_reg;
164 } else if (reg_tgt == 0x10) {
166 switch (ioapic->index_reg) {
168 *op_val = ioapic->ioapic_id.val;
171 *op_val = ioapic->ioapic_ver.val;
174 *op_val = ioapic->ioapic_arb_id.val;
178 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 2;
179 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 1;
181 if (redir_index > 0x3f) {
182 PrintError("Invalid redirection table entry %x\n", (uint32_t)redir_index);
186 *op_val = ioapic->redir_tbl[redir_index].hi;
188 *op_val = ioapic->redir_tbl[redir_index].lo;
198 static int ioapic_write(addr_t guest_addr, void * src, uint_t length, void * priv_data) {
199 struct vm_device * dev = (struct vm_device *)priv_data;
200 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
201 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
202 uint32_t op_val = *(uint32_t *)src;
204 PrintDebug("IOAPIC Write at %p (val = %d)\n", (void *)guest_addr, *(uint32_t *)src);
206 if (reg_tgt == 0x00) {
207 ioapic->index_reg = op_val;
208 } else if (reg_tgt == 0x10) {
210 switch (ioapic->index_reg) {
212 ioapic->ioapic_id.val = op_val;
215 // GPF/PageFault/Ignore?
216 PrintError("Writing to read only IOAPIC register\n");
219 ioapic->ioapic_arb_id.val = op_val;
223 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 2;
224 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 1;
226 if (redir_index > 0x3f) {
227 PrintError("Invalid redirection table entry %x\n", (uint32_t)redir_index);
231 ioapic->redir_tbl[redir_index].hi = op_val;
233 op_val &= REDIR_LO_MASK;
234 ioapic->redir_tbl[redir_index].lo &= ~REDIR_LO_MASK;
235 ioapic->redir_tbl[redir_index].lo |= op_val;
244 /* Interrupt controller functions */
245 static int ioapic_intr_pending(void * private_data) {
250 static int ioapic_get_intr_number(void * private_data) {
254 static int ioapic_begin_irq(void * private_data, int irq) {
258 static int ioapic_raise_irq(void * private_data, int irq) {
259 struct vm_device * dev = (struct vm_device *)private_data;
260 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
261 struct redir_tbl_entry * irq_entry = NULL;
264 PrintError("IRQ out of range of IO APIC\n");
268 irq_entry = &(ioapic->redir_tbl[irq]);
270 if (irq_entry->mask == 0) {
271 v3_apic_raise_intr(ioapic->apic, irq_entry->vec);
277 /* I don't know if we can do anything here.... */
278 static int ioapic_lower_irq(void * private_data, int irq) {
282 static struct intr_ctrl_ops intr_ops = {
283 .intr_pending = ioapic_intr_pending,
284 .get_intr_number = ioapic_get_intr_number,
285 .raise_intr = ioapic_raise_irq,
286 .begin_irq = ioapic_begin_irq,
287 .lower_intr = ioapic_lower_irq,
291 static int io_apic_init(struct vm_device * dev) {
292 struct guest_info * info = dev->vm;
293 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
295 v3_register_intr_controller(dev->vm, &intr_ops, dev);
296 init_ioapic_state(ioapic);
298 v3_hook_full_mem(info, ioapic->base_addr, ioapic->base_addr + PAGE_SIZE_4KB,
299 ioapic_read, ioapic_write, dev);
305 static int io_apic_deinit(struct vm_device * dev) {
306 // struct guest_info * info = dev->vm;
312 static struct vm_device_ops dev_ops = {
313 .init = io_apic_init,
314 .deinit = io_apic_deinit,
322 struct vm_device * v3_create_io_apic(struct vm_device * apic) {
323 PrintDebug("Creating IO APIC\n");
325 struct io_apic_state * ioapic = (struct io_apic_state *)V3_Malloc(sizeof(struct io_apic_state));
328 struct vm_device * device = v3_create_device("IOAPIC", &dev_ops, ioapic);