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 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, uint_t irq) {
172 V3_ASSERT(irq <= 255);
173 return vm->intr_routers.hooks[irq];
177 int v3_hook_irq(struct v3_vm_info * vm,
179 int (*handler)(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data),
182 struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
188 if (get_irq_hook(vm, irq) != NULL) {
189 PrintError("IRQ %d already hooked\n", irq);
193 hook->handler = handler;
194 hook->priv_data = priv_data;
196 vm->intr_routers.hooks[irq] = hook;
198 if (V3_Hook_Interrupt(vm, irq)) {
199 PrintError("hook_irq: failed to hook irq %d\n", irq);
202 PrintDebug("hook_irq: hooked irq %d\n", irq);
208 static int passthrough_irq_handler(struct v3_vm_info * vm, struct v3_interrupt * intr, void * priv_data) {
209 PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%p)\n",
210 intr->irq, (void *)vm);
212 return v3_raise_irq(vm, intr->irq);
216 int v3_hook_passthrough_irq(struct v3_vm_info * vm, uint_t irq) {
217 int rc = v3_hook_irq(vm, irq, passthrough_irq_handler, NULL);
220 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
223 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%p)\n", irq, (void *)vm);
229 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr) {
230 PrintDebug("v3_deliver_irq: irq=%d state=0x%p, \n", intr->irq, (void *)intr);
232 struct v3_irq_hook * hook = get_irq_hook(vm, intr->irq);
235 PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
239 return hook->handler(vm, intr, hook->priv_data);
243 int v3_raise_virq(struct guest_info * info, int irq) {
244 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
248 intr_state->virq_map[major] |= (1 << minor);
254 int v3_lower_virq(struct guest_info * info, int irq) {
255 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
259 intr_state->virq_map[major] &= ~(1 << minor);
265 int v3_lower_irq(struct v3_vm_info * vm, int irq) {
266 struct intr_router * router = NULL;
267 struct v3_intr_routers * routers = &(vm->intr_routers);
269 // PrintDebug("[v3_lower_irq]\n");
270 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
272 list_for_each_entry(router, &(routers->router_list), router_node) {
273 router->router_ops->lower_intr(vm, router->priv_data, irq);
276 v3_unlock_irqrestore(routers->irq_lock, irq_state);
282 int v3_raise_irq(struct v3_vm_info * vm, int irq) {
283 struct intr_router * router = NULL;
284 struct v3_intr_routers * routers = &(vm->intr_routers);
286 // PrintDebug("[v3_raise_irq (%d)]\n", irq);
287 addr_t irq_state = v3_lock_irqsave(routers->irq_lock);
289 list_for_each_entry(router, &(routers->router_list), router_node) {
290 router->router_ops->raise_intr(vm, router->priv_data, irq);
293 v3_unlock_irqrestore(routers->irq_lock, irq_state);
299 int v3_signal_swintr(struct guest_info * core, int vector) {
300 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
302 PrintDebug("Signaling software interrupt in vmm_intr.c\n");
303 PrintDebug("\tINT vector: %d\n", vector);
305 intr_state->swintr_posted = 1;
306 intr_state->swintr_vector = vector;
311 int v3_handle_swintr(struct guest_info * core) {
314 void * instr_ptr = NULL;
315 struct x86_instr instr;
317 if (core->mem_mode == PHYSICAL_MEM) {
318 ret = v3_gpa_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
320 ret = v3_gva_to_hva(core, get_addr_linear(core, core->rip, &(core->segments.cs)), (addr_t *)&instr_ptr);
324 PrintError("V3 Syscall Hijack: Could not translate Instruction Address (%p)\n", (void *)core->rip);
328 if (v3_decode(core, (addr_t)instr_ptr, &instr) == -1) {
329 PrintError("V3 Syscall Hijack: Decoding Error\n");
333 uint8_t vector = instr.dst_operand.operand;
335 //PrintDebug("KCH: SWINT\n");
336 //PrintDebug("KCH: Data - %x\n",*((uint32_t*)instr_ptr));
337 //PrintDebug("\t RIP: %llx CS: %x\n", core->rip, core->segments.cs.selector);
338 //PrintDebug("KCH: Disassembling\n\t");
339 //addr_t rip = (addr_t) core->rip;
340 //v3_disasm(core, instr_ptr, &rip, 1);
342 //v3_print_instr(&instr);
343 // only consider system calls
346 if (vector == 0x80) {
347 print_syscall(0, core);
351 struct v3_swintr_hook * hook = core->intr_core_state.swintr_hooks[vector];
353 #ifdef CONFIG_SWINTR_PASSTHROUGH
354 if (v3_hook_passthrough_swintr(core, vector) == -1) {
355 PrintDebug("Error hooking passthrough swintr\n");
358 hook = core->intr_core_state.swintr_hooks[vector];
360 core->rip += instr.instr_length;
361 return v3_signal_swintr(core, vector);
365 ret = hook->handler(core, vector, NULL);
367 PrintDebug("V3 SWINT Handler: Error in swint hook\n");
371 /* make software interrupts prioritized so they finish in time for the next
373 core->rip += instr.instr_length;
374 return v3_signal_swintr(core, vector);
378 static inline struct v3_swintr_hook * get_swintr_hook(struct guest_info * core, uint8_t vector) {
379 return core->intr_core_state.swintr_hooks[vector];
383 int v3_hook_swintr(struct guest_info * core,
385 int (*handler)(struct guest_info * core, uint8_t vector, void * priv_data),
389 struct v3_swintr_hook * hook = (struct v3_swintr_hook *)V3_Malloc(sizeof(struct v3_swintr_hook));
395 if (get_swintr_hook(core, vector) != NULL) {
396 PrintError("SWINT %d already hooked\n", vector);
400 hook->handler = handler;
401 hook->priv_data = priv_data;
403 core->intr_core_state.swintr_hooks[vector] = hook;
409 static int passthrough_swintr_handler(struct guest_info * core, uint8_t vector, void * priv_data) {
411 PrintDebug("[passthrough_swint_handler] INT vector=%d (guest=0x%p)\n",
412 vector, (void *)core);
418 int v3_hook_passthrough_swintr(struct guest_info * core, uint8_t vector) {
420 int rc = v3_hook_swintr(core, vector, passthrough_swintr_handler, NULL);
423 PrintError("guest_swintr_injection: failed to hook swint 0x%x (guest=0x%p)\n", vector, (void *)core);
426 PrintDebug("guest_swintr_injection: hooked swint 0x%x (guest=0x%p)\n", vector, (void *)core);
430 /* shouldn't get here */
435 void v3_clear_pending_intr(struct guest_info * core) {
436 struct v3_intr_core_state * intr_state = &(core->intr_core_state);
438 intr_state->irq_pending = 0;
442 v3_intr_type_t v3_intr_pending(struct guest_info * info) {
443 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
444 struct intr_controller * ctrl = NULL;
445 int ret = V3_INVALID_INTR;
448 // PrintDebug("[intr_pending]\n");
449 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
451 // VIRQs have priority
452 for (i = 0; i < MAX_IRQ / 8; i++) {
453 if (intr_state->virq_map[i] != 0) {
454 ret = V3_VIRTUAL_IRQ;
459 if (ret == V3_INVALID_INTR) {
460 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
461 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data) == 1) {
462 ret = V3_EXTERNAL_IRQ;
469 if (intr_state->swintr_posted == 1) {
470 ret = V3_SOFTWARE_INTR;
473 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
479 uint32_t v3_get_intr(struct guest_info * info) {
480 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
481 struct intr_controller * ctrl = NULL;
486 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
488 // virqs have priority
489 for (i = 0; i < MAX_IRQ / 8; i++) {
490 if (intr_state->virq_map[i] != 0) {
491 for (j = 0; j < 8; j++) {
492 if (intr_state->virq_map[i] & (1 << j)) {
502 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
503 if (ctrl->ctrl_ops->intr_pending(info, ctrl->priv_data)) {
504 uint_t intr_num = ctrl->ctrl_ops->get_intr_number(info, ctrl->priv_data);
506 // PrintDebug("[get_intr_number] intr_number = %d\n", intr_num);
513 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
519 intr_type_t v3_get_intr_type(struct guest_info * info) {
520 struct v3_intr_state * intr_state = &(info->intr_state);
521 struct intr_controller * ctrl = NULL;
522 intr_type_t type = V3_INVALID_INTR;
524 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
526 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
527 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
528 //PrintDebug("[get_intr_type] External_irq\n");
529 type = V3_EXTERNAL_IRQ;
534 #ifdef CONFIG_DEBUG_INTERRUPTS
535 if (type == V3_INVALID_INTR) {
536 PrintError("[get_intr_type] Invalid_Intr\n");
540 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);
547 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, v3_intr_type_t type) {
548 struct v3_intr_core_state * intr_state = &(info->intr_core_state);
550 if (type == V3_EXTERNAL_IRQ) {
551 struct intr_controller * ctrl = NULL;
553 addr_t irq_state = v3_lock_irqsave(intr_state->irq_lock);
555 // PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
556 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
557 ctrl->ctrl_ops->begin_irq(info, ctrl->priv_data, intr_num);
560 v3_unlock_irqrestore(intr_state->irq_lock, irq_state);