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 info->vm_ops.raise_irq = &v3_raise_irq;
40 info->vm_ops.lower_irq = &v3_lower_irq; //Zheng added
43 void set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
44 info->intr_state.controller = ops;
45 info->intr_state.controller_state = state;
50 // This structure is used to dispatch
51 // interrupts delivered to vmm via deliver interrupt to vmm
52 // it is what we put into the opaque field given to
53 // the host os when we install the handler
54 struct vmm_intr_decode {
55 void (*handler)(struct vmm_intr_state *state);
56 // This opaque is user supplied by the caller
61 int v3_hook_irq(uint_t irq,
62 void (*handler)(struct vmm_intr_state *state),
65 struct vmm_intr_decode *d = (struct vmm_intr_decode *)V3_Malloc(sizeof(struct vmm_intr_decode));
67 if (!d) { return -1; }
72 if (V3_Hook_Interrupt(irq,d)) {
73 PrintError("hook_irq: failed to hook irq 0x%x to decode 0x%x\n", irq,d);
76 PrintDebug("hook_irq: hooked irq 0x%x to decode 0x%x\n", irq,d);
82 void deliver_interrupt_to_vmm(struct vmm_intr_state *state)
85 PrintDebug("deliver_interrupt_to_vmm: state=0x%x\n",state);
87 struct vmm_intr_decode *d = (struct vmm_intr_decode *)(state->opaque);
89 void *temp = state->opaque;
90 state->opaque = d->opaque;
98 static void guest_injection_irq_handler(struct vmm_intr_state *state)
100 struct guest_info *guest = (struct guest_info *)(state->opaque);
101 PrintDebug("[guest_injection_irq_handler] raise_irq=0x%x (guest=0x%x)\n", state->irq, guest);
102 PrintDebug("guest_irq_injection: state=0x%x\n", state);
103 guest->vm_ops.raise_irq(guest,state->irq);
107 int v3_hook_irq_for_guest_injection(struct guest_info *info, int irq)
110 int rc = v3_hook_irq(irq,
111 guest_injection_irq_handler,
115 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%x)\n", irq, info);
118 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%x)\n", irq, info);
126 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
127 struct vm_intr * intr_state = &(info->intr_state);
129 if (intr_state->excp_pending == 0) {
130 intr_state->excp_pending = 1;
131 intr_state->excp_num = excp;
132 intr_state->excp_error_code = error_code;
133 intr_state->excp_error_code_valid = 1;
134 PrintDebug("[v3_raise_exception_with_error] error code: %x\n", error_code);
136 PrintError("exception already pending, currently not implemented\n");
143 int v3_raise_exception(struct guest_info * info, uint_t excp) {
144 struct vm_intr * intr_state = &(info->intr_state);
145 PrintDebug("[v3_raise_exception]\n");
146 if (intr_state->excp_pending == 0) {
147 intr_state->excp_pending = 1;
148 intr_state->excp_num = excp;
149 intr_state->excp_error_code = 0;
150 intr_state->excp_error_code_valid = 0;
152 PrintError("exception already pending, currently not implemented\n");
161 int v3_lower_irq(struct guest_info * info, int irq) {
162 // Look up PIC and resend
164 V3_ASSERT(info->intr_state.controller);
165 V3_ASSERT(info->intr_state.controller->raise_intr);
167 PrintDebug("[v3_lower_irq]\n");
169 // if ((info->intr_state.controller) &&
170 // (info->intr_state.controller->raise_intr)) {
171 info->intr_state.controller->lower_intr(info->intr_state.controller_state, irq);
173 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
179 int v3_raise_irq(struct guest_info * info, int irq) {
180 // Look up PIC and resend
182 V3_ASSERT(info->intr_state.controller);
183 V3_ASSERT(info->intr_state.controller->raise_intr);
185 PrintDebug("[v3_raise_irq]\n");
187 // if ((info->intr_state.controller) &&
188 // (info->intr_state.controller->raise_intr)) {
189 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
191 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
199 int intr_pending(struct guest_info * info) {
200 struct vm_intr * intr_state = &(info->intr_state);
202 // PrintDebug("[intr_pending]\n");
203 if (intr_state->excp_pending == 1) {
205 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
215 uint_t get_intr_number(struct guest_info * info) {
216 struct vm_intr * intr_state = &(info->intr_state);
218 if (intr_state->excp_pending == 1) {
219 return intr_state->excp_num;
220 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
221 PrintDebug("[get_intr_number] intr_number = %d\n", intr_state->controller->get_intr_number(intr_state->controller_state));
222 return intr_state->controller->get_intr_number(intr_state->controller_state);
225 /* someway to get the [A]PIC intr */
231 intr_type_t get_intr_type(struct guest_info * info) {
232 struct vm_intr * intr_state = &(info->intr_state);
234 if (intr_state->excp_pending) {
235 PrintDebug("[get_intr_type] Exception\n");
237 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
238 PrintDebug("[get_intr_type] External_irq\n");
241 PrintDebug("[get_intr_type] Invalid_Intr\n");
250 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
251 struct vm_intr * intr_state = &(info->intr_state);
253 if (type == EXCEPTION) {
254 PrintDebug("[injecting_intr] Exception\n");
255 intr_state->excp_pending = 0;
256 intr_state->excp_num = 0;
257 intr_state->excp_error_code = 0;
258 intr_state->excp_error_code_valid = 0;
260 } else if (type == EXTERNAL_IRQ) {
261 PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
262 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);