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>
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 // This is a temporary method of communication between the IOAPIC and the LAPIC
139 struct vm_device * apic;
144 static void init_ioapic_state(struct io_apic_state * ioapic) {
146 ioapic->base_addr = IO_APIC_BASE_ADDR;
147 ioapic->index_reg = 0;
149 ioapic->ioapic_id.val = 0x00000000;
150 ioapic->ioapic_ver.val = 0x00170011;
151 ioapic->ioapic_arb_id.val = 0x00000000;
153 for (i = 0; i < 24; i++) {
154 ioapic->redir_tbl[i].val = 0x0001000000000000LL;
155 // Mask all interrupts until they are enabled....
156 ioapic->redir_tbl[i].mask = 1;
161 static int ioapic_read(addr_t guest_addr, void * dst, uint_t length, void * priv_data) {
162 struct vm_device * dev = (struct vm_device *)priv_data;
163 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
164 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
165 uint32_t * op_val = (uint32_t *)dst;
167 PrintDebug("IOAPIC Read at %p\n", (void *)guest_addr);
169 if (reg_tgt == 0x00) {
170 *op_val = ioapic->index_reg;
171 } else if (reg_tgt == 0x10) {
173 switch (ioapic->index_reg) {
175 *op_val = ioapic->ioapic_id.val;
178 *op_val = ioapic->ioapic_ver.val;
181 *op_val = ioapic->ioapic_arb_id.val;
185 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 0xfffffffe;
186 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 1;
188 if (redir_index > 0x3f) {
189 PrintError("Invalid redirection table entry %x\n", (uint32_t)redir_index);
193 *op_val = ioapic->redir_tbl[redir_index].hi;
195 *op_val = ioapic->redir_tbl[redir_index].lo;
205 static int ioapic_write(addr_t guest_addr, void * src, uint_t length, void * priv_data) {
206 struct vm_device * dev = (struct vm_device *)priv_data;
207 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
208 uint32_t reg_tgt = guest_addr - ioapic->base_addr;
209 uint32_t op_val = *(uint32_t *)src;
211 PrintDebug("IOAPIC Write at %p (val = %d)\n", (void *)guest_addr, *(uint32_t *)src);
213 if (reg_tgt == 0x00) {
214 ioapic->index_reg = op_val;
215 } else if (reg_tgt == 0x10) {
217 switch (ioapic->index_reg) {
219 ioapic->ioapic_id.val = op_val;
222 // GPF/PageFault/Ignore?
223 PrintError("Writing to read only IOAPIC register\n");
226 ioapic->ioapic_arb_id.val = op_val;
230 uint_t redir_index = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) & 0xfffffffe;
231 uint_t hi_val = (ioapic->index_reg - IOAPIC_REDIR_BASE_REG) % 1;
236 if (redir_index > 0x3f) {
237 PrintError("Invalid redirection table entry %x\n", (uint32_t)redir_index);
241 PrintDebug("Writing to hi of pin %d\n", redir_index);
242 ioapic->redir_tbl[redir_index].hi = op_val;
244 PrintDebug("Writing to lo of pin %d\n", redir_index);
245 op_val &= REDIR_LO_MASK;
246 ioapic->redir_tbl[redir_index].lo &= ~REDIR_LO_MASK;
247 ioapic->redir_tbl[redir_index].lo |= op_val;
256 /* Interrupt controller functions */
257 static int ioapic_intr_pending(void * private_data) {
262 static int ioapic_get_intr_number(void * private_data) {
266 static int ioapic_begin_irq(void * private_data, int irq) {
270 static int ioapic_raise_irq(void * private_data, int irq) {
271 struct vm_device * dev = (struct vm_device *)private_data;
272 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
273 struct redir_tbl_entry * irq_entry = NULL;
276 PrintError("IRQ out of range of IO APIC\n");
280 irq_entry = &(ioapic->redir_tbl[irq]);
282 if (irq_entry->mask == 0) {
283 PrintDebug("IOAPIC Signalling APIC to raise INTR %d\n", irq_entry->vec);
284 v3_apic_raise_intr(ioapic->apic, irq_entry->vec);
290 /* I don't know if we can do anything here.... */
291 static int ioapic_lower_irq(void * private_data, int irq) {
295 static struct intr_ctrl_ops intr_ops = {
296 .intr_pending = ioapic_intr_pending,
297 .get_intr_number = ioapic_get_intr_number,
298 .raise_intr = ioapic_raise_irq,
299 .begin_irq = ioapic_begin_irq,
300 .lower_intr = ioapic_lower_irq,
304 static int io_apic_init(struct vm_device * dev) {
305 struct guest_info * info = dev->vm;
306 struct io_apic_state * ioapic = (struct io_apic_state *)(dev->private_data);
308 v3_register_intr_controller(dev->vm, &intr_ops, dev);
309 init_ioapic_state(ioapic);
311 v3_hook_full_mem(info, ioapic->base_addr, ioapic->base_addr + PAGE_SIZE_4KB,
312 ioapic_read, ioapic_write, dev);
318 static int io_apic_deinit(struct vm_device * dev) {
319 // struct guest_info * info = dev->vm;
325 static struct vm_device_ops dev_ops = {
326 .init = io_apic_init,
327 .deinit = io_apic_deinit,
335 struct vm_device * v3_create_io_apic(struct vm_device * apic) {
336 PrintDebug("Creating IO APIC\n");
338 struct io_apic_state * ioapic = (struct io_apic_state *)V3_Malloc(sizeof(struct io_apic_state));
341 struct vm_device * device = v3_create_device("IOAPIC", &dev_ops, ioapic);