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>
26 #ifndef DEBUG_INTERRUPTS
28 #define PrintDebug(fmt, args...)
34 struct intr_controller {
35 struct intr_ctrl_ops * ctrl_ops;
38 struct list_head ctrl_node;
42 void v3_init_interrupt_state(struct guest_info * info) {
44 info->intr_state.irq_pending = 0;
45 info->intr_state.irq_vector = 0;
47 INIT_LIST_HEAD(&(info->intr_state.controller_list));
49 memset((uchar_t *)(info->intr_state.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
52 void v3_register_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
53 struct intr_controller * ctrlr = (struct intr_controller *)V3_Malloc(sizeof(struct intr_controller));
55 ctrlr->priv_data = state;
56 ctrlr->ctrl_ops = ops;
58 list_add(&(ctrlr->ctrl_node), &(info->intr_state.controller_list));
65 static inline struct v3_irq_hook * get_irq_hook(struct guest_info * info, uint_t irq) {
66 V3_ASSERT(irq <= 256);
67 return info->intr_state.hooks[irq];
71 int v3_hook_irq(struct guest_info * info,
73 int (*handler)(struct guest_info * info, struct v3_interrupt * intr, void * priv_data),
76 struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
82 if (get_irq_hook(info, irq) != NULL) {
83 PrintError("IRQ %d already hooked\n", irq);
87 hook->handler = handler;
88 hook->priv_data = priv_data;
90 info->intr_state.hooks[irq] = hook;
92 if (V3_Hook_Interrupt(info, irq)) {
93 PrintError("hook_irq: failed to hook irq %d\n", irq);
96 PrintDebug("hook_irq: hooked irq %d\n", irq);
103 static int passthrough_irq_handler(struct guest_info * info, struct v3_interrupt * intr, void * priv_data) {
104 PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%p)\n",
105 intr->irq, (void *)info);
107 return v3_raise_irq(info, intr->irq);
110 int v3_hook_passthrough_irq(struct guest_info * info, uint_t irq) {
111 int rc = v3_hook_irq(info, irq, passthrough_irq_handler, NULL);
114 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%p)\n", irq, (void *)info);
117 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%p)\n", irq, (void *)info);
126 int v3_deliver_irq(struct guest_info * info, struct v3_interrupt * intr) {
127 PrintDebug("v3_deliver_irq: irq=%d state=0x%p, \n", intr->irq, (void *)intr);
129 struct v3_irq_hook * hook = get_irq_hook(info, intr->irq);
132 PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
136 return hook->handler(info, intr, hook->priv_data);
147 int v3_lower_irq(struct guest_info * info, int irq) {
148 struct intr_controller * ctrl = NULL;
149 struct v3_intr_state * intr_state = &(info->intr_state);
151 // PrintDebug("[v3_lower_irq]\n");
153 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
154 ctrl->ctrl_ops->lower_intr(ctrl->priv_data, irq);
160 int v3_raise_irq(struct guest_info * info, int irq) {
161 struct intr_controller * ctrl = NULL;
162 struct v3_intr_state * intr_state = &(info->intr_state);
164 // PrintDebug("[v3_raise_irq (%d)]\n", irq);
166 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
167 ctrl->ctrl_ops->raise_intr(ctrl->priv_data, irq);
175 int v3_intr_pending(struct guest_info * info) {
176 struct v3_intr_state * intr_state = &(info->intr_state);
177 struct intr_controller * ctrl = NULL;
178 // PrintDebug("[intr_pending]\n");
180 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
181 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
191 uint_t v3_get_intr_number(struct guest_info * info) {
192 struct v3_intr_state * intr_state = &(info->intr_state);
193 struct intr_controller * ctrl = NULL;
195 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
196 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data)) {
197 uint_t intr_num = ctrl->ctrl_ops->get_intr_number(ctrl->priv_data);
199 // PrintDebug("[get_intr_number] intr_number = %d\n", intr_num);
208 intr_type_t v3_get_intr_type(struct guest_info * info) {
209 struct v3_intr_state * intr_state = &(info->intr_state);
210 struct intr_controller * ctrl = NULL;
212 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
213 if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
214 //PrintDebug("[get_intr_type] External_irq\n");
219 PrintError("[get_intr_type] Invalid_Intr\n");
228 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
229 struct v3_intr_state * intr_state = &(info->intr_state);
231 if (type == EXTERNAL_IRQ) {
232 struct intr_controller * ctrl = NULL;
234 // PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
235 list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
236 ctrl->ctrl_ops->begin_irq(ctrl->priv_data, intr_num);