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 v3_hook_irq(uint_t irq,
38 void (*handler)(struct vmm_intr_state *state),
41 struct vmm_intr_decode *d = (struct vmm_intr_decode *)V3_Malloc(sizeof(struct vmm_intr_decode));
43 if (!d) { return -1; }
48 if (V3_Hook_Interrupt(irq,d)) {
49 PrintError("hook_irq: failed to hook irq 0x%x to decode 0x%x\n", irq,d);
52 PrintDebug("hook_irq: hooked irq 0x%x to decode 0x%x\n", irq,d);
58 void deliver_interrupt_to_vmm(struct vmm_intr_state *state)
61 PrintDebug("deliver_interrupt_to_vmm: state=0x%x\n",state);
63 struct vmm_intr_decode *d = (struct vmm_intr_decode *)(state->opaque);
65 void *temp = state->opaque;
66 state->opaque = d->opaque;
74 static void guest_injection_irq_handler(struct vmm_intr_state *state)
76 PrintDebug("guest_irq_injection: state=0x%x\n",state);
78 struct guest_info *guest = (struct guest_info *)(state->opaque);
80 PrintDebug("guest_irq_injection: injecting irq 0x%x into guest 0x%x\n",state->irq,guest);
81 guest->vm_ops.raise_irq(guest,state->irq);
85 int v3_hook_irq_for_guest_injection(struct guest_info *info, int irq)
88 int rc = v3_hook_irq(irq,
89 guest_injection_irq_handler,
93 PrintError("guest_irq_injection: failed to hook irq 0x%x for guest 0x%x\n", irq,info);
96 PrintDebug("guest_irq_injection: hooked irq 0x%x for guest 0x%x\n", irq,info);
104 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
105 struct vm_intr * intr_state = &(info->intr_state);
107 if (intr_state->excp_pending == 0) {
108 intr_state->excp_pending = 1;
109 intr_state->excp_num = excp;
110 intr_state->excp_error_code = error_code;
111 intr_state->excp_error_code_valid = 1;
112 PrintDebug("Raising exception with error code: %x\n", error_code);
114 PrintError("exception already pending, currently not implemented\n");
121 int v3_raise_exception(struct guest_info * info, uint_t excp) {
122 struct vm_intr * intr_state = &(info->intr_state);
124 if (intr_state->excp_pending == 0) {
125 intr_state->excp_pending = 1;
126 intr_state->excp_num = excp;
127 intr_state->excp_error_code = 0;
128 intr_state->excp_error_code_valid = 0;
130 PrintError("exception already pending, currently not implemented\n");
138 int v3_raise_irq(struct guest_info * info, int irq) {
139 // Look up PIC and resend
141 V3_ASSERT(info->intr_state.controller);
142 V3_ASSERT(info->intr_state.controller->raise_intr);
144 // if ((info->intr_state.controller) &&
145 // (info->intr_state.controller->raise_intr)) {
146 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
148 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
156 int intr_pending(struct guest_info * info) {
157 struct vm_intr * intr_state = &(info->intr_state);
159 if (intr_state->excp_pending == 1) {
161 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
171 uint_t get_intr_number(struct guest_info * info) {
172 struct vm_intr * intr_state = &(info->intr_state);
174 if (intr_state->excp_pending == 1) {
175 return intr_state->excp_num;
176 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
177 return intr_state->controller->get_intr_number(intr_state->controller_state);
180 /* someway to get the [A]PIC intr */
186 intr_type_t get_intr_type(struct guest_info * info) {
187 struct vm_intr * intr_state = &(info->intr_state);
189 if (intr_state->excp_pending) {
191 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
203 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
204 struct vm_intr * intr_state = &(info->intr_state);
206 if (type == EXCEPTION) {
208 intr_state->excp_pending = 0;
209 intr_state->excp_num = 0;
210 intr_state->excp_error_code = 0;
211 intr_state->excp_error_code_valid = 0;
213 } else if (type == EXTERNAL_IRQ) {
214 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);