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_intr.h>
22 #include <palacios/vmm.h>
24 #include <palacios/vm_guest.h>
25 #include <palacios/vmm_ctrl_regs.h>
27 #include <palacios/vmm_lock.h>
29 #ifndef CONFIG_DEBUG_INTERRUPTS
31 #define PrintDebug(fmt, args...)
36 struct intr_controller {
37 struct intr_ctrl_ops * ctrl_ops;
40 struct list_head ctrl_node;
45 struct intr_router_ops * router_ops;
48 struct list_head router_node;
54 void v3_init_intr_controllers(struct guest_info * info) {
55 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
57 intr_state->irq_pending = 0;
58 intr_state->irq_started = 0;
59 intr_state->irq_vector = 0;
61 v3_lock_init(&(intr_state->irq_lock));
63 INIT_LIST_HEAD(&(intr_state->controller_list));
67 void v3_deinit_intr_controllers(struct guest_info * core) {
68 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
69 struct intr_controller * ctrlr;
70 struct intr_controller * tmp;
72 // clear out any controllers that were left around
73 list_for_each_entry_safe(ctrlr, tmp, &(intr_state->controller_list), ctrl_node) {
74 v3_remove_intr_controller(core, ctrlr);
79 void v3_init_intr_routers(struct v3_vm_info * vm) {
81 INIT_LIST_HEAD(&(vm->intr_routers.router_list));
83 v3_lock_init(&(vm->intr_routers.irq_lock));
85 memset((uchar_t *)(vm->intr_routers.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
89 void v3_deinit_intr_routers(struct v3_vm_info * vm) {
90 struct intr_router * rtr = NULL;
91 struct intr_router * tmp = NULL;
93 // clear out any controllers that were left around
94 list_for_each_entry_safe(rtr, tmp, &(vm->intr_routers.router_list), router_node) {
95 v3_remove_intr_router(vm, rtr);
100 void * v3_register_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * priv_data) {
101 struct intr_controller * ctrlr = (struct intr_controller *)V3_Malloc(sizeof(struct intr_controller));
103 ctrlr->priv_data = priv_data;
104 ctrlr->ctrl_ops = ops;
106 list_add(&(ctrlr->ctrl_node), &(info->intr_core_state.controller_list));
108 return (void *)ctrlr;
112 void v3_remove_intr_controller(struct guest_info * core, void * handle) {
113 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
114 struct intr_controller * ctrlr = handle;
115 struct intr_controller * tmp = NULL;
118 // search for the entry in the router list
119 list_for_each_entry(tmp, &(intr_state->controller_list), ctrl_node) {
126 PrintError("Attempted to remove invalid interrupt controller handle\n");
130 list_del(&(ctrlr->ctrl_node));
135 void * v3_register_intr_router(struct v3_vm_info * vm, struct intr_router_ops * ops, void * priv_data) {
136 struct intr_router * router = (struct intr_router *)V3_Malloc(sizeof(struct intr_router));
138 router->priv_data = priv_data;
139 router->router_ops = ops;
141 list_add(&(router->router_node), &(vm->intr_routers.router_list));
143 return (void *)router;
147 void v3_remove_intr_router(struct v3_vm_info * vm, void * handle) {
148 struct intr_router * router = handle;
149 struct intr_router * tmp = NULL;
152 // search for the entry in the router list
153 list_for_each_entry(tmp, &(vm->intr_routers.router_list), router_node) {
160 PrintError("Attempted to remove invalid interrupt router\n");
164 list_del(&(router->router_node));
169 static inline struct v3_irq_hook * get_irq_hook(struct v3_vm_info * vm, uint_t irq) {
170 V3_ASSERT(irq <= 256);
171 return vm->intr_routers.hooks[irq];
175 int v3_hook_irq(struct v3_vm_info * vm,
177 int (*handler)(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data),
180 struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
186 if (get_irq_hook(vm, irq) != NULL) {
187 PrintError("IRQ %d already hooked\n", irq);
191 hook->handler = handler;
192 hook->priv_data = priv_data;
194 vm->intr_routers.hooks[irq] = hook;
196 if (V3_Hook_Interrupt(vm, irq)) {
197 PrintError("hook_irq: failed to hook irq %d\n", irq);
200 PrintDebug("hook_irq: hooked irq %d\n", irq);
206 static int passthrough_irq_handler(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data) {
207 PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%p)\n",
208 intr->irq, (void *)vm);
210 return v3_raise_irq(vm, intr->irq);
214 int v3_hook_passthrough_irq(struct v3_vm_info * vm, uint_t irq) {
215 int rc = v3_hook_irq(vm, irq, passthrough_irq_handler, NULL);
218 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
221 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
227 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr) {
228 PrintDebug("v3_deliver_irq: irq=%d state=0x%p, \n", intr->irq, (void *)intr);
230 struct v3_irq_hook * hook = get_irq_hook(vm, intr->irq);
233 PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
237 return hook->handler(vm, intr, hook->priv_data);
241 int v3_raise_virq(struct guest_info * info, int irq) {
242 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
246 intr_state->virq_map[major] |= (1 << minor);
252 int v3_lower_virq(struct guest_info * info, int irq) {
253 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
257 intr_state->virq_map[major] &= ~(1 << minor);
263 int v3_lower_irq(struct v3_vm_info * vm, int irq) {
264 struct intr_router * router = NULL;
265 struct v3_intr_routers * routers = &(vm->intr_routers);
267 // PrintDebug("[v3_lower_irq]\n");
268 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
270 list_for_each_entry(router, &(routers->router_list), router_node) {
271 router->router_ops->lower_intr(vm, router->priv_data, irq);
274 v3_unlock_irqrestore(routers->irq_lock, irq_state);
280 int v3_raise_irq(struct v3_vm_info * vm, int irq) {
281 struct intr_router * router = NULL;
282 struct v3_intr_routers * routers = &(vm->intr_routers);
284 // PrintDebug("[v3_raise_irq (%d)]\n", irq);
285 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
287 list_for_each_entry(router, &(routers->router_list), router_node) {
288 router->router_ops->raise_intr(vm, router->priv_data, irq);
291 v3_unlock_irqrestore(routers->irq_lock, irq_state);
297 int v3_signal_sw_intr(struct guest_info * core, int vec) {
298 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
300 PrintDebug("KCH: Signalling a software interrupt in vmm_intr.c\n");
301 PrintDebug("\tINT vector: %d\n", vec);
303 intr_state->sw_intr_pending = 1;
304 intr_state->sw_intr_vector = vec;
309 void v3_clear_pending_intr(struct guest_info * core) {
310 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
312 intr_state->irq_pending = 0;
316 v3_intr_type_t v3_intr_pending(struct guest_info * info) {
317 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
318 struct intr_controller * ctrl = NULL;
319 int ret = V3_INVALID_INTR;
322 // PrintDebug("[intr_pending]\n");
323 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
325 // VIRQs have priority
326 for (i = 0; i < MAX_IRQ / 8; i++) {
327 if (intr_state->virq_map[i] != 0) {
328 ret = V3_VIRTUAL_IRQ;
333 if (ret == V3_INVALID_INTR) {
334 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
335 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data) == 1) {
336 ret = V3_EXTERNAL_IRQ;
342 // KCH: not sure about this
343 if (intr_state->sw_intr_pending == 1) {
344 ret = V3_SOFTWARE_INTR;
348 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
354 uint32_t v3_get_intr(struct guest_info * info) {
355 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
356 struct intr_controller * ctrl = NULL;
361 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
363 // virqs have priority
364 for (i = 0; i < MAX_IRQ / 8; i++) {
365 if (intr_state->virq_map[i] != 0) {
366 for (j = 0; j < 8; j++) {
367 if (intr_state->virq_map[i] & (1 << j)) {
377 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
378 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data)) {
379 uint_t intr_num = ctrl->ctrl_ops->get_intr_number(info, ctrl->priv_data);
381 // PrintDebug("[get_intr_number] intr_number = %d\n", intr_num);
388 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
394 intr_type_t v3_get_intr_type(struct guest_info * info) {
395 struct v3_intr_state * intr_state = &(info->intr_state);
396 struct intr_controller * ctrl = NULL;
397 intr_type_t type = V3_INVALID_INTR;
399 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
401 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
402 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
403 //PrintDebug("[get_intr_type] External_irq\n");
404 type = V3_EXTERNAL_IRQ;
409 #ifdef CONFIG_DEBUG_INTERRUPTS
410 if (type == V3_INVALID_INTR) {
411 PrintError("[get_intr_type] Invalid_Intr\n");
415 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
422 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, v3_intr_type_t type) {
423 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
425 if (type == V3_EXTERNAL_IRQ) {
426 struct intr_controller * ctrl = NULL;
428 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
430 // PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
431 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
432 ctrl->ctrl_ops->begin_irq(info, ctrl->priv_data, intr_num);
435 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);