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 void init_interrupt_state(struct guest_info * info) {
35 info->intr_state.excp_pending = 0;
36 info->intr_state.excp_num = 0;
37 info->intr_state.excp_error_code = 0;
39 memset((uchar_t *)(info->intr_state.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
41 info->vm_ops.raise_irq = &v3_raise_irq;
42 info->vm_ops.lower_irq = &v3_lower_irq;
45 void set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
46 info->intr_state.controller = ops;
47 info->intr_state.controller_state = state;
53 static inline struct v3_irq_hook * get_irq_hook(struct guest_info * info, uint_t irq) {
54 V3_ASSERT(irq <= 256);
55 return info->intr_state.hooks[irq];
59 int v3_hook_irq(struct guest_info * info,
61 int (*handler)(struct guest_info * info, struct v3_interrupt * intr, void * priv_data),
64 struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
70 if (get_irq_hook(info, irq) != NULL) {
71 PrintError("IRQ %d already hooked\n", irq);
75 hook->handler = handler;
76 hook->priv_data = priv_data;
78 info->intr_state.hooks[irq] = hook;
80 if (V3_Hook_Interrupt(info, irq)) {
81 PrintError("hook_irq: failed to hook irq %d\n", irq);
84 PrintDebug("hook_irq: hooked irq %d\n", irq);
91 static int passthrough_irq_handler(struct guest_info * info, struct v3_interrupt * intr, void * priv_data)
94 PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%x)\n", intr->irq, info);
95 return v3_raise_irq(info, intr->irq);
99 int v3_hook_passthrough_irq(struct guest_info * info, uint_t irq)
102 int rc = v3_hook_irq(info,
104 passthrough_irq_handler,
108 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%x)\n", irq, info);
111 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%x)\n", irq, info);
120 int v3_deliver_irq(struct guest_info * info, struct v3_interrupt * intr) {
121 PrintDebug("v3_deliver_irq: irq=%d state=0x%x, \n", intr->irq, intr);
123 struct v3_irq_hook * hook = get_irq_hook(info, intr->irq);
126 PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
130 return hook->handler(info, intr, hook->priv_data);
140 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
141 struct v3_intr_state * intr_state = &(info->intr_state);
143 if (intr_state->excp_pending == 0) {
144 intr_state->excp_pending = 1;
145 intr_state->excp_num = excp;
146 intr_state->excp_error_code = error_code;
147 intr_state->excp_error_code_valid = 1;
148 PrintDebug("[v3_raise_exception_with_error] error code: %x\n", error_code);
150 PrintError("exception already pending, currently not implemented\n");
157 int v3_raise_exception(struct guest_info * info, uint_t excp) {
158 struct v3_intr_state * intr_state = &(info->intr_state);
159 PrintDebug("[v3_raise_exception]\n");
160 if (intr_state->excp_pending == 0) {
161 intr_state->excp_pending = 1;
162 intr_state->excp_num = excp;
163 intr_state->excp_error_code = 0;
164 intr_state->excp_error_code_valid = 0;
166 PrintError("exception already pending, currently not implemented\n");
174 int v3_lower_irq(struct guest_info * info, int irq) {
175 // Look up PIC and resend
177 V3_ASSERT(info->intr_state.controller);
178 V3_ASSERT(info->intr_state.controller->lower_intr);
180 PrintDebug("[v3_lower_irq]\n");
182 if ((info->intr_state.controller) &&
183 (info->intr_state.controller->lower_intr)) {
184 info->intr_state.controller->lower_intr(info->intr_state.controller_state, irq);
186 PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
193 int v3_raise_irq(struct guest_info * info, int irq) {
194 // Look up PIC and resend
196 V3_ASSERT(info->intr_state.controller);
197 V3_ASSERT(info->intr_state.controller->raise_intr);
199 PrintDebug("[v3_raise_irq]\n");
201 if ((info->intr_state.controller) &&
202 (info->intr_state.controller->raise_intr)) {
203 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
205 PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
214 int intr_pending(struct guest_info * info) {
215 struct v3_intr_state * intr_state = &(info->intr_state);
217 // PrintDebug("[intr_pending]\n");
218 if (intr_state->excp_pending == 1) {
220 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
230 uint_t get_intr_number(struct guest_info * info) {
231 struct v3_intr_state * intr_state = &(info->intr_state);
233 if (intr_state->excp_pending == 1) {
234 return intr_state->excp_num;
235 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
236 PrintDebug("[get_intr_number] intr_number = %d\n", intr_state->controller->get_intr_number(intr_state->controller_state));
237 return intr_state->controller->get_intr_number(intr_state->controller_state);
240 /* someway to get the [A]PIC intr */
246 intr_type_t get_intr_type(struct guest_info * info) {
247 struct v3_intr_state * intr_state = &(info->intr_state);
249 if (intr_state->excp_pending) {
250 PrintDebug("[get_intr_type] Exception\n");
252 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
253 PrintDebug("[get_intr_type] External_irq\n");
256 PrintDebug("[get_intr_type] Invalid_Intr\n");
265 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
266 struct v3_intr_state * intr_state = &(info->intr_state);
268 if (type == EXCEPTION) {
269 PrintDebug("[injecting_intr] Exception\n");
270 intr_state->excp_pending = 0;
271 intr_state->excp_num = 0;
272 intr_state->excp_error_code = 0;
273 intr_state->excp_error_code_valid = 0;
275 } else if (type == EXTERNAL_IRQ) {
276 PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
277 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);