1 #include <palacios/vmm_intr.h>
2 #include <palacios/vmm.h>
4 #include <palacios/vm_guest.h>
8 void init_interrupt_state(struct guest_info * info) {
9 info->intr_state.excp_pending = 0;
10 info->intr_state.excp_num = 0;
11 info->intr_state.excp_error_code = 0;
13 info->vm_ops.raise_irq = &v3_raise_irq;
16 void set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
17 info->intr_state.controller = ops;
18 info->intr_state.controller_state = state;
23 // This structure is used to dispatch
24 // interrupts delivered to vmm via deliver interrupt to vmm
25 // it is what we put into the opaque field given to
26 // the host os when we install the handler
27 struct vmm_intr_decode {
28 void (*handler)(struct vmm_intr_state *state);
29 // This opaque is user supplied by the caller
34 int hook_irq_new(uint_t irq,
35 void (*handler)(struct vmm_intr_state *state),
38 extern struct vmm_os_hooks * os_hooks;
40 struct vmm_intr_decode *d = V3_Malloc(sizeof(struct vmm_intr_decode));
42 if (!d) { return -1; }
47 if (os_hooks->hook_interrupt_new(irq,d)) {
48 PrintDebug("hook_irq_new: failed to hook irq 0x%x to decode 0x%x\n", irq,d);
51 PrintDebug("hook_irq_new: hooked irq 0x%x to decode 0x%x\n", irq,d);
57 void deliver_interrupt_to_vmm(struct vmm_intr_state *state)
60 PrintDebug("deliver_interrupt_to_vmm: state=0x%x\n",state);
62 struct vmm_intr_decode *d = (struct vmm_intr_decode *)(state->opaque);
64 void *temp = state->opaque;
65 state->opaque = d->opaque;
73 void guest_injection_irq_handler(struct vmm_intr_state *state)
75 PrintDebug("guest_irq_injection: state=0x%x\n",state);
77 struct guest_info *guest = (struct guest_info *)(state->opaque);
79 PrintDebug("guest_irq_injection: injecting irq 0x%x into guest 0x%x\n",state->irq,guest);
80 guest->vm_ops.raise_irq(guest,state->irq);
84 int hook_irq_for_guest_injection(struct guest_info *info, int irq)
87 int rc = hook_irq_new(irq,
88 guest_injection_irq_handler,
92 PrintDebug("guest_irq_injection: failed to hook irq 0x%x for guest 0x%x\n", irq,info);
95 PrintDebug("guest_irq_injection: hooked irq 0x%x for guest 0x%x\n", irq,info);
103 int hook_irq(struct guest_info * info, int irq) {
104 extern struct vmm_os_hooks * os_hooks;
106 return os_hooks->hook_interrupt(info, irq);
109 int 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("Raising exception with error code: %x\n", error_code);
119 PrintDebug("exception already pending, currently not implemented\n");
126 int raise_exception(struct guest_info * info, uint_t excp) {
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 = 0;
133 intr_state->excp_error_code_valid = 0;
135 PrintDebug("exception already pending, currently not implemented\n");
143 int v3_raise_irq(struct guest_info * info, int irq) {
144 // Look up PIC and resend
146 V3_ASSERT(info->intr_state.controller);
147 V3_ASSERT(info->intr_state.controller->raise_intr);
149 // if ((info->intr_state.controller) &&
150 // (info->intr_state.controller->raise_intr)) {
151 info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
153 // PrintDebug("There is no registered Interrupt Controller... (NULL POINTER)\n");
161 int intr_pending(struct guest_info * info) {
162 struct vm_intr * intr_state = &(info->intr_state);
164 if (intr_state->excp_pending == 1) {
166 } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
176 uint_t get_intr_number(struct guest_info * info) {
177 struct vm_intr * intr_state = &(info->intr_state);
179 if (intr_state->excp_pending == 1) {
180 return intr_state->excp_num;
181 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
182 return intr_state->controller->get_intr_number(intr_state->controller_state);
185 /* someway to get the [A]PIC intr */
191 intr_type_t get_intr_type(struct guest_info * info) {
192 struct vm_intr * intr_state = &(info->intr_state);
194 if (intr_state->excp_pending) {
196 } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
208 int injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
209 struct vm_intr * intr_state = &(info->intr_state);
211 if (type == EXCEPTION) {
213 intr_state->excp_pending = 0;
214 intr_state->excp_num = 0;
215 intr_state->excp_error_code = 0;
216 intr_state->excp_error_code_valid = 0;
218 } else if (type == EXTERNAL_IRQ) {
219 return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);