1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <palacios/vmm_intr.h>
5 #include <palacios/vmm.h>
7 #include <palacios/vm_guest.h>
9 #ifndef DEBUG_INTERRUPTS
11 #define PrintDebug(fmt, args...)
17 void init_interrupt_state(struct guest_info * info) {
18 info->intr_state.excp_pending = 0;
19 info->intr_state.excp_num = 0;
20 info->intr_state.excp_error_code = 0;
22 info->vm_ops.raise_irq = &v3_raise_irq;
23 info->vm_ops.lower_irq = &v3_lower_irq; //Zheng added
26 void set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
27 info->intr_state.controller = ops;
28 info->intr_state.controller_state = state;
33 // This structure is used to dispatch
34 // interrupts delivered to vmm via deliver interrupt to vmm
35 // it is what we put into the opaque field given to
36 // the host os when we install the handler
37 struct vmm_intr_decode {
38 void (*handler)(struct vmm_intr_state *state);
39 // This opaque is user supplied by the caller
44 int v3_hook_irq(uint_t irq,
45 void (*handler)(struct vmm_intr_state *state),
48 struct vmm_intr_decode *d = (struct vmm_intr_decode *)V3_Malloc(sizeof(struct vmm_intr_decode));
50 if (!d) { return -1; }
55 if (V3_Hook_Interrupt(irq,d)) {
56 PrintError("hook_irq: failed to hook irq 0x%x to decode 0x%x\n", irq,d);
59 PrintDebug("hook_irq: hooked irq 0x%x to decode 0x%x\n", irq,d);
65 void deliver_interrupt_to_vmm(struct vmm_intr_state *state)
68 PrintDebug("deliver_interrupt_to_vmm: state=0x%x\n",state);
70 struct vmm_intr_decode *d = (struct vmm_intr_decode *)(state->opaque);
72 void *temp = state->opaque;
73 state->opaque = d->opaque;
81 static void guest_injection_irq_handler(struct vmm_intr_state *state)
83 struct guest_info *guest = (struct guest_info *)(state->opaque);
84 PrintDebug("[guest_injection_irq_handler] raise_irq=0x%x (guest=0x%x)\n", state->irq, guest);
85 PrintDebug("guest_irq_injection: state=0x%x\n", state);
86 guest->vm_ops.raise_irq(guest,state->irq);
90 int v3_hook_irq_for_guest_injection(struct guest_info *info, int irq)
93 int rc = v3_hook_irq(irq,
94 guest_injection_irq_handler,
98 PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%x)\n", irq, info);
101 PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%x)\n", irq, info);
109 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
110 struct vm_intr * intr_state = &(info->intr_state);
112 if (intr_state->excp_pending == 0) {
113 intr_state->excp_pending = 1;
114 intr_state->excp_num = excp;
115 intr_state->excp_error_code = error_code;
116 intr_state->excp_error_code_valid = 1;
117 PrintDebug("[v3_raise_exception_with_error] error code: %x\n", error_code);
119 PrintError("exception already pending, currently not implemented\n");
126 int v3_raise_exception(struct guest_info * info, uint_t excp) {
127 struct vm_intr * intr_state = &(info->intr_state);
128 PrintDebug("[v3_raise_exception]\n");
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 = 0;
133 intr_state->excp_error_code_valid = 0;
135 PrintError("exception already pending, currently not implemented\n");
144 int v3_lower_irq(struct guest_info * info, int irq) {
145 // Look up PIC and resend
147 V3_ASSERT(info->intr_state.controller);
148 V3_ASSERT(info->intr_state.controller->raise_intr);
150 PrintDebug("[v3_lower_irq]\n");
152 // if ((info->intr_state.controller) &&
153 // (info->intr_state.controller->raise_intr)) {
154 info->intr_state.controller->lower_intr(info->intr_state.controller_state, irq);
156 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
162 int v3_raise_irq(struct guest_info * info, int irq) {
163 // Look up PIC and resend
165 V3_ASSERT(info->intr_state.controller);
166 V3_ASSERT(info->intr_state.controller->raise_intr);
168 PrintDebug("[v3_raise_irq]\n");
170 // if ((info->intr_state.controller) &&
171 // (info->intr_state.controller->raise_intr)) {
172 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
174 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
182 int intr_pending(struct guest_info * info) {
183 struct vm_intr * intr_state = &(info->intr_state);
185 // PrintDebug("[intr_pending]\n");
186 if (intr_state->excp_pending == 1) {
188 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
198 uint_t get_intr_number(struct guest_info * info) {
199 struct vm_intr * intr_state = &(info->intr_state);
201 if (intr_state->excp_pending == 1) {
202 return intr_state->excp_num;
203 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
204 PrintDebug("[get_intr_number] intr_number = %d\n", intr_state->controller->get_intr_number(intr_state->controller_state));
205 return intr_state->controller->get_intr_number(intr_state->controller_state);
208 /* someway to get the [A]PIC intr */
214 intr_type_t get_intr_type(struct guest_info * info) {
215 struct vm_intr * intr_state = &(info->intr_state);
217 if (intr_state->excp_pending) {
218 PrintDebug("[get_intr_type] Exception\n");
220 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
221 PrintDebug("[get_intr_type] External_irq\n");
224 PrintDebug("[get_intr_type] Invalid_Intr\n");
233 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
234 struct vm_intr * intr_state = &(info->intr_state);
236 if (type == EXCEPTION) {
237 PrintDebug("[injecting_intr] Exception\n");
238 intr_state->excp_pending = 0;
239 intr_state->excp_num = 0;
240 intr_state->excp_error_code = 0;
241 intr_state->excp_error_code_valid = 0;
243 } else if (type == EXTERNAL_IRQ) {
244 PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
245 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);