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>
28 #include <palacios/vm_guest_mem.h>
29 #include <palacios/vmm_decoder.h>
31 #ifndef V3_CONFIG_DEBUG_INTERRUPTS
33 #define PrintDebug(fmt, args...)
38 struct intr_controller {
39 struct intr_ctrl_ops * ctrl_ops;
42 struct list_head ctrl_node;
47 struct intr_router_ops * router_ops;
50 struct list_head router_node;
56 void v3_init_intr_controllers(struct guest_info * info) {
57 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
59 intr_state->irq_pending = 0;
60 intr_state->irq_started = 0;
61 intr_state->irq_vector = 0;
63 v3_lock_init(&(intr_state->irq_lock));
65 INIT_LIST_HEAD(&(intr_state->controller_list));
69 void v3_deinit_intr_controllers(struct guest_info * core) {
70 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
71 struct intr_controller * ctrlr;
72 struct intr_controller * tmp;
74 // clear out any controllers that were left around
75 list_for_each_entry_safe(ctrlr, tmp, &(intr_state->controller_list), ctrl_node) {
76 v3_remove_intr_controller(core, ctrlr);
81 void v3_init_intr_routers(struct v3_vm_info * vm) {
83 INIT_LIST_HEAD(&(vm->intr_routers.router_list));
85 v3_lock_init(&(vm->intr_routers.irq_lock));
87 memset((uchar_t *)(vm->intr_routers.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
91 void v3_deinit_intr_routers(struct v3_vm_info * vm) {
92 struct intr_router * rtr = NULL;
93 struct intr_router * tmp = NULL;
95 // clear out any controllers that were left around
96 list_for_each_entry_safe(rtr, tmp, &(vm->intr_routers.router_list), router_node) {
97 v3_remove_intr_router(vm, rtr);
102 void * v3_register_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * priv_data) {
103 struct intr_controller * ctrlr = (struct intr_controller *)V3_Malloc(sizeof(struct intr_controller));
105 ctrlr->priv_data = priv_data;
106 ctrlr->ctrl_ops = ops;
108 list_add(&(ctrlr->ctrl_node), &(info->intr_core_state.controller_list));
110 return (void *)ctrlr;
114 void v3_remove_intr_controller(struct guest_info * core, void * handle) {
115 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
116 struct intr_controller * ctrlr = handle;
117 struct intr_controller * tmp = NULL;
120 // search for the entry in the router list
121 list_for_each_entry(tmp, &(intr_state->controller_list), ctrl_node) {
128 PrintError("Attempted to remove invalid interrupt controller handle\n");
132 list_del(&(ctrlr->ctrl_node));
137 void * v3_register_intr_router(struct v3_vm_info * vm, struct intr_router_ops * ops, void * priv_data) {
138 struct intr_router * router = (struct intr_router *)V3_Malloc(sizeof(struct intr_router));
140 router->priv_data = priv_data;
141 router->router_ops = ops;
143 list_add(&(router->router_node), &(vm->intr_routers.router_list));
145 return (void *)router;
149 void v3_remove_intr_router(struct v3_vm_info * vm, void * handle) {
150 struct intr_router * router = handle;
151 struct intr_router * tmp = NULL;
154 // search for the entry in the router list
155 list_for_each_entry(tmp, &(vm->intr_routers.router_list), router_node) {
162 PrintError("Attempted to remove invalid interrupt router\n");
166 list_del(&(router->router_node));
171 static inline struct v3_irq_hook * get_irq_hook(struct v3_vm_info * vm, uint8_t irq) {
172 return vm->intr_routers.hooks[irq];
176 int v3_hook_irq(struct v3_vm_info * vm,
178 int (*handler)(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data),
181 struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
187 if (get_irq_hook(vm, irq) != NULL) {
188 PrintError("IRQ %d already hooked\n", irq);
192 hook->handler = handler;
193 hook->priv_data = priv_data;
195 vm->intr_routers.hooks[irq] = hook;
197 if (V3_Hook_Interrupt(vm, irq)) {
198 PrintError("hook_irq: failed to hook irq %d\n", irq);
201 PrintDebug("hook_irq: hooked irq %d\n", irq);
207 static int passthrough_irq_handler(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data) {
208 PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%p)\n",
209 intr->irq, (void *)vm);
211 return v3_raise_irq(vm, intr->irq);
215 int v3_hook_passthrough_irq(struct v3_vm_info * vm, uint_t irq) {
216 int rc = v3_hook_irq(vm, irq, passthrough_irq_handler, NULL);
219 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
222 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
228 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr) {
229 PrintDebug("v3_deliver_irq: irq=%d state=0x%p, \n", intr->irq, (void *)intr);
231 struct v3_irq_hook * hook = get_irq_hook(vm, intr->irq);
234 PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
238 return hook->handler(vm, intr, hook->priv_data);
242 int v3_raise_virq(struct guest_info * info, int irq) {
243 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
247 intr_state->virq_map[major] |= (1 << minor);
253 int v3_lower_virq(struct guest_info * info, int irq) {
254 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
258 intr_state->virq_map[major] &= ~(1 << minor);
264 int v3_lower_irq(struct v3_vm_info * vm, int irq) {
265 struct intr_router * router = NULL;
266 struct v3_intr_routers * routers = &(vm->intr_routers);
268 // PrintDebug("[v3_lower_irq]\n");
269 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
271 list_for_each_entry(router, &(routers->router_list), router_node) {
272 router->router_ops->lower_intr(vm, router->priv_data, irq);
275 v3_unlock_irqrestore(routers->irq_lock, irq_state);
281 int v3_raise_irq(struct v3_vm_info * vm, int irq) {
282 struct intr_router * router = NULL;
283 struct v3_intr_routers * routers = &(vm->intr_routers);
285 // PrintDebug("[v3_raise_irq (%d)]\n", irq);
286 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
288 list_for_each_entry(router, &(routers->router_list), router_node) {
289 router->router_ops->raise_intr(vm, router->priv_data, irq);
292 v3_unlock_irqrestore(routers->irq_lock, irq_state);
298 int v3_signal_swintr(struct guest_info * core, int vector) {
299 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
301 PrintDebug("Signaling software interrupt in vmm_intr.c\n");
302 PrintDebug("\tINT vector: %d\n", vector);
304 intr_state->swintr_posted = 1;
305 intr_state->swintr_vector = vector;
310 int v3_handle_swintr(struct guest_info * core) {
313 void * instr_ptr = NULL;
314 struct x86_instr instr;
316 if (core->mem_mode == PHYSICAL_MEM) {
317 ret = v3_gpa_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
319 ret = v3_gva_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
323 PrintError("V3 SWintr Handler: Could not translate Instruction Address (%p)\n", (void *)core->rip);
327 if (v3_decode(core, (addr_t)instr_ptr, &instr) == -1) {
328 PrintError("V3 SWintr Handler: Decoding Error\n");
332 uint8_t vector = instr.dst_operand.operand;
334 struct v3_swintr_hook * hook = core->intr_core_state.swintr_hooks[vector];
336 #ifdef V3_CONFIG_SWINTR_PASSTHROUGH
337 if (v3_hook_passthrough_swintr(core, vector) == -1) {
338 PrintDebug("V3 SWintr Handler: Error hooking passthrough swintr\n");
341 hook = core->intr_core_state.swintr_hooks[vector];
343 core->rip += instr.instr_length;
344 return v3_signal_swintr(core, vector);
348 ret = hook->handler(core, vector, NULL);
350 PrintDebug("V3 SWintr Handler: Error in swintr hook\n");
354 /* KCH: at some point we may need to prioritize swints
355 so that they finish in time for the next
357 core->rip += instr.instr_length;
358 return v3_signal_swintr(core, vector);
362 static inline struct v3_swintr_hook * get_swintr_hook(struct guest_info * core, uint8_t vector) {
363 return core->intr_core_state.swintr_hooks[vector];
367 int v3_hook_swintr(struct guest_info * core,
369 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data),
373 struct v3_swintr_hook * hook = (struct v3_swintr_hook *)V3_Malloc(sizeof(struct v3_swintr_hook));
379 if (get_swintr_hook(core, vector) != NULL) {
380 PrintError("SWINT %d already hooked\n", vector);
384 hook->handler = handler;
385 hook->priv_data = priv_data;
387 core->intr_core_state.swintr_hooks[vector] = hook;
393 static int passthrough_swintr_handler(struct guest_info * core, uint8_t vector, void * priv_data) {
395 PrintDebug("[passthrough_swint_handler] INT vector=%d (guest=0x%p)\n",
396 vector, (void *)core);
402 int v3_hook_passthrough_swintr(struct guest_info * core, uint8_t vector) {
404 int rc = v3_hook_swintr(core, vector, passthrough_swintr_handler, NULL);
407 PrintError("guest_swintr_injection: failed to hook swint 0x%x (guest=0x%p)\n", vector, (void *)core);
410 PrintDebug("guest_swintr_injection: hooked swint 0x%x (guest=0x%p)\n", vector, (void *)core);
414 /* shouldn't get here */
419 void v3_clear_pending_intr(struct guest_info * core) {
420 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
422 intr_state->irq_pending = 0;
426 v3_intr_type_t v3_intr_pending(struct guest_info * info) {
427 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
428 struct intr_controller * ctrl = NULL;
429 int ret = V3_INVALID_INTR;
432 // PrintDebug("[intr_pending]\n");
433 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
435 // VIRQs have priority
436 for (i = 0; i < MAX_IRQ / 8; i++) {
437 if (intr_state->virq_map[i] != 0) {
438 ret = V3_VIRTUAL_IRQ;
443 if (ret == V3_INVALID_INTR) {
444 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
445 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data) == 1) {
446 ret = V3_EXTERNAL_IRQ;
452 // KCH: added for SWintr injection
453 if (intr_state->swintr_posted == 1) {
454 ret = V3_SOFTWARE_INTR;
457 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
463 uint32_t v3_get_intr(struct guest_info * info) {
464 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
465 struct intr_controller * ctrl = NULL;
470 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
472 // virqs have priority
473 for (i = 0; i < MAX_IRQ / 8; i++) {
474 if (intr_state->virq_map[i] != 0) {
475 for (j = 0; j < 8; j++) {
476 if (intr_state->virq_map[i] & (1 << j)) {
486 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
487 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data)) {
488 uint_t intr_num = ctrl->ctrl_ops->get_intr_number(info, ctrl->priv_data);
490 // PrintDebug("[get_intr_number] intr_number = %d\n", intr_num);
497 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
503 intr_type_t v3_get_intr_type(struct guest_info * info) {
504 struct v3_intr_state * intr_state = &(info->intr_state);
505 struct intr_controller * ctrl = NULL;
506 intr_type_t type = V3_INVALID_INTR;
508 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
510 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
511 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
512 //PrintDebug("[get_intr_type] External_irq\n");
513 type = V3_EXTERNAL_IRQ;
518 #ifdef V3_CONFIG_DEBUG_INTERRUPTS
519 if (type == V3_INVALID_INTR) {
520 PrintError("[get_intr_type] Invalid_Intr\n");
524 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
531 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, v3_intr_type_t type) {
532 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
534 if (type == V3_EXTERNAL_IRQ) {
535 struct intr_controller * ctrl = NULL;
537 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
539 // PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
540 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
541 ctrl->ctrl_ops->begin_irq(info, ctrl->priv_data, intr_num);
544 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);