1 #include <palacios/vmm_intr.h>
2 #include <palacios/vmm.h>
4 #include <palacios/vm_guest.h>
6 #ifndef DEBUG_INTERRUPTS
8 #define PrintDebug(fmt, args...)
11 void init_interrupt_state(struct guest_info * info) {
12 info->intr_state.excp_pending = 0;
13 info->intr_state.excp_num = 0;
14 info->intr_state.excp_error_code = 0;
16 info->vm_ops.raise_irq = &v3_raise_irq;
19 void set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
20 info->intr_state.controller = ops;
21 info->intr_state.controller_state = state;
26 // This structure is used to dispatch
27 // interrupts delivered to vmm via deliver interrupt to vmm
28 // it is what we put into the opaque field given to
29 // the host os when we install the handler
30 struct vmm_intr_decode {
31 void (*handler)(struct vmm_intr_state *state);
32 // This opaque is user supplied by the caller
37 int hook_irq_new(uint_t irq,
38 void (*handler)(struct vmm_intr_state *state),
41 extern struct vmm_os_hooks * os_hooks;
43 struct vmm_intr_decode *d = V3_Malloc(sizeof(struct vmm_intr_decode));
45 if (!d) { return -1; }
50 if (os_hooks->hook_interrupt_new(irq,d)) {
51 PrintError("hook_irq_new: failed to hook irq 0x%x to decode 0x%x\n", irq,d);
54 PrintDebug("hook_irq_new: hooked irq 0x%x to decode 0x%x\n", irq,d);
60 void deliver_interrupt_to_vmm(struct vmm_intr_state *state)
63 PrintDebug("deliver_interrupt_to_vmm: state=0x%x\n",state);
65 struct vmm_intr_decode *d = (struct vmm_intr_decode *)(state->opaque);
67 void *temp = state->opaque;
68 state->opaque = d->opaque;
76 void guest_injection_irq_handler(struct vmm_intr_state *state)
78 PrintDebug("guest_irq_injection: state=0x%x\n",state);
80 struct guest_info *guest = (struct guest_info *)(state->opaque);
82 PrintDebug("guest_irq_injection: injecting irq 0x%x into guest 0x%x\n",state->irq,guest);
83 guest->vm_ops.raise_irq(guest,state->irq);
87 int hook_irq_for_guest_injection(struct guest_info *info, int irq)
90 int rc = hook_irq_new(irq,
91 guest_injection_irq_handler,
95 PrintError("guest_irq_injection: failed to hook irq 0x%x for guest 0x%x\n", irq,info);
98 PrintDebug("guest_irq_injection: hooked irq 0x%x for guest 0x%x\n", irq,info);
106 int hook_irq(struct guest_info * info, int irq) {
107 extern struct vmm_os_hooks * os_hooks;
109 return os_hooks->hook_interrupt(info, irq);
112 int raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
113 struct vm_intr * intr_state = &(info->intr_state);
115 if (intr_state->excp_pending == 0) {
116 intr_state->excp_pending = 1;
117 intr_state->excp_num = excp;
118 intr_state->excp_error_code = error_code;
119 intr_state->excp_error_code_valid = 1;
120 PrintDebug("Raising exception with error code: %x\n", error_code);
122 PrintError("exception already pending, currently not implemented\n");
129 int raise_exception(struct guest_info * info, uint_t excp) {
130 struct vm_intr * intr_state = &(info->intr_state);
132 if (intr_state->excp_pending == 0) {
133 intr_state->excp_pending = 1;
134 intr_state->excp_num = excp;
135 intr_state->excp_error_code = 0;
136 intr_state->excp_error_code_valid = 0;
138 PrintError("exception already pending, currently not implemented\n");
146 int v3_raise_irq(struct guest_info * info, int irq) {
147 // Look up PIC and resend
149 V3_ASSERT(info->intr_state.controller);
150 V3_ASSERT(info->intr_state.controller->raise_intr);
152 // if ((info->intr_state.controller) &&
153 // (info->intr_state.controller->raise_intr)) {
154 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
156 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
164 int intr_pending(struct guest_info * info) {
165 struct vm_intr * intr_state = &(info->intr_state);
167 if (intr_state->excp_pending == 1) {
169 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
179 uint_t get_intr_number(struct guest_info * info) {
180 struct vm_intr * intr_state = &(info->intr_state);
182 if (intr_state->excp_pending == 1) {
183 return intr_state->excp_num;
184 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
185 return intr_state->controller->get_intr_number(intr_state->controller_state);
188 /* someway to get the [A]PIC intr */
194 intr_type_t get_intr_type(struct guest_info * info) {
195 struct vm_intr * intr_state = &(info->intr_state);
197 if (intr_state->excp_pending) {
199 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
211 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
212 struct vm_intr * intr_state = &(info->intr_state);
214 if (type == EXCEPTION) {
216 intr_state->excp_pending = 0;
217 intr_state->excp_num = 0;
218 intr_state->excp_error_code = 0;
219 intr_state->excp_error_code_valid = 0;
221 } else if (type == EXTERNAL_IRQ) {
222 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);