Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added support for multiple interrupt controllers
[palacios.git] / palacios / src / palacios / vmm_intr.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #include <palacios/vmm_intr.h>
22 #include <palacios/vmm.h>
23
24 #include <palacios/vm_guest.h>
25
26 #ifndef DEBUG_INTERRUPTS
27 #undef PrintDebug
28 #define PrintDebug(fmt, args...)
29 #endif
30
31
32
33
34 struct intr_controller {
35   struct intr_ctrl_ops * ctrl_ops;
36
37   void * priv_data;
38   struct list_head ctrl_node;
39 };
40
41
42 void v3_init_interrupt_state(struct guest_info * info) {
43   info->intr_state.excp_pending = 0;
44   info->intr_state.excp_num = 0;
45   info->intr_state.excp_error_code = 0;
46
47   memset((uchar_t *)(info->intr_state.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
48 }
49
50 void v3_register_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
51   struct intr_controller * ctrlr = (struct intr_controller *)V3_Malloc(sizeof(struct intr_controller));
52
53   ctrlr->priv_data = state;
54   ctrlr->ctrl_ops = ops;
55
56   list_add(&(ctrlr->ctrl_node), &(info->intr_state.controller_list));
57
58 }
59
60
61
62
63 static inline struct v3_irq_hook * get_irq_hook(struct guest_info * info, uint_t irq) {
64   V3_ASSERT(irq <= 256);
65   return info->intr_state.hooks[irq];
66 }
67
68
69 int v3_hook_irq(struct guest_info * info, 
70                 uint_t irq,
71                 int (*handler)(struct guest_info * info, struct v3_interrupt * intr, void * priv_data),
72                 void  * priv_data) 
73 {
74   struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
75
76   if (hook == NULL) { 
77     return -1; 
78   }
79
80   if (get_irq_hook(info, irq) != NULL) {
81     PrintError("IRQ %d already hooked\n", irq);
82     return -1;
83   }
84
85   hook->handler = handler;
86   hook->priv_data = priv_data;
87   
88   info->intr_state.hooks[irq] = hook;
89
90   if (V3_Hook_Interrupt(info, irq)) { 
91     PrintError("hook_irq: failed to hook irq %d\n", irq);
92     return -1;
93   } else {
94     PrintDebug("hook_irq: hooked irq %d\n", irq);
95     return 0;
96   }
97 }
98
99
100
101 static int passthrough_irq_handler(struct guest_info * info, struct v3_interrupt * intr, void * priv_data)
102 {
103
104   PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%p)\n", 
105              intr->irq, (void *)info);
106   return v3_raise_irq(info, intr->irq);
107
108 }
109
110 int v3_hook_passthrough_irq(struct guest_info * info, uint_t irq)
111 {
112
113   int rc = v3_hook_irq(info, irq, passthrough_irq_handler, NULL);
114
115   if (rc) { 
116     PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%p)\n", irq, (void *)info);
117     return -1;
118   } else {
119     PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%p)\n", irq, (void *)info);
120     return 0;
121   }
122 }
123
124
125
126
127
128 int v3_deliver_irq(struct guest_info * info, struct v3_interrupt * intr) {
129   PrintDebug("v3_deliver_irq: irq=%d state=0x%p, \n", intr->irq, (void *)intr);
130   
131   struct v3_irq_hook * hook = get_irq_hook(info, intr->irq);
132
133   if (hook == NULL) {
134     PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
135     return -1;
136   }
137   
138   return hook->handler(info, intr, hook->priv_data);
139 }
140
141
142
143
144
145
146
147
148 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
149   struct v3_intr_state * intr_state = &(info->intr_state);
150
151   if (intr_state->excp_pending == 0) {
152     intr_state->excp_pending = 1;
153     intr_state->excp_num = excp;
154     intr_state->excp_error_code = error_code;
155     intr_state->excp_error_code_valid = 1;
156     PrintDebug("[v3_raise_exception_with_error] error code: %x\n", error_code);
157   } else {
158     PrintError("exception already pending, currently not implemented\n");
159     return -1;
160   }
161
162   return 0;
163 }
164
165 int v3_raise_exception(struct guest_info * info, uint_t excp) {
166   struct v3_intr_state * intr_state = &(info->intr_state);
167   PrintDebug("[v3_raise_exception]\n");
168   if (intr_state->excp_pending == 0) {
169     intr_state->excp_pending = 1;
170     intr_state->excp_num = excp;
171     intr_state->excp_error_code = 0;
172     intr_state->excp_error_code_valid = 0;
173   } else {
174     PrintError("exception already pending, currently not implemented\n");
175     return -1;
176   }
177
178   return 0;
179 }
180
181
182 int v3_lower_irq(struct guest_info * info, int irq) {
183   struct intr_controller * ctrl = NULL;
184   struct v3_intr_state * intr_state = &(info->intr_state);
185
186   PrintDebug("[v3_lower_irq]\n");
187
188   list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
189     ctrl->ctrl_ops->lower_intr(ctrl->priv_data, irq);
190   }
191  
192   return 0;
193 }
194
195 int v3_raise_irq(struct guest_info * info, int irq) {
196   struct intr_controller * ctrl = NULL;
197   struct v3_intr_state * intr_state = &(info->intr_state);
198
199   PrintDebug("[v3_raise_irq]\n");
200
201   list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
202     ctrl->ctrl_ops->raise_intr(ctrl->priv_data, irq);
203   }
204
205   return 0;
206 }
207
208
209
210 int v3_intr_pending(struct guest_info * info) {
211   struct v3_intr_state * intr_state = &(info->intr_state);
212
213   //  PrintDebug("[intr_pending]\n");
214   if (intr_state->excp_pending == 1) {
215     return 1;
216   } else {
217     struct intr_controller * ctrl = NULL;
218
219     list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
220       if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
221         return 1;
222       }
223     }
224   }
225
226   return 0;
227 }
228
229
230 uint_t v3_get_intr_number(struct guest_info * info) {
231   struct v3_intr_state * intr_state = &(info->intr_state);
232
233   if (intr_state->excp_pending == 1) {
234     return intr_state->excp_num;
235   } else {
236     struct intr_controller * ctrl = NULL;
237
238     list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
239       if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data)) {
240         uint_t intr_num = ctrl->ctrl_ops->get_intr_number(ctrl->priv_data);
241
242         PrintDebug("[get_intr_number] intr_number = %d\n", intr_num);
243
244         return intr_num;
245       }
246     }
247   }
248
249   return 0;
250 }
251
252
253 intr_type_t v3_get_intr_type(struct guest_info * info) {
254   struct v3_intr_state * intr_state = &(info->intr_state);
255
256   if (intr_state->excp_pending) {
257     PrintDebug("[get_intr_type] Exception\n");
258     return EXCEPTION;
259   } else {
260     struct intr_controller * ctrl = NULL;
261
262     list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
263       if (ctrl->ctrl_ops->intr_pending(ctrl->priv_data) == 1) {
264         PrintDebug("[get_intr_type] External_irq\n");
265         return EXTERNAL_IRQ;
266       }
267     }
268   }
269
270   PrintDebug("[get_intr_type] Invalid_Intr\n");
271   return INVALID_INTR;
272 }
273
274
275
276
277
278
279 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
280   struct v3_intr_state * intr_state = &(info->intr_state);
281
282   if (type == EXCEPTION) {
283     PrintDebug("[injecting_intr] Exception\n");
284     intr_state->excp_pending = 0;
285     intr_state->excp_num = 0;
286     intr_state->excp_error_code = 0;
287     intr_state->excp_error_code_valid = 0;
288     
289   } else if (type == EXTERNAL_IRQ) {
290     struct intr_controller * ctrl = NULL;
291
292     list_for_each_entry(ctrl, &(intr_state->controller_list), ctrl_node) {
293       PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
294       ctrl->ctrl_ops->begin_irq(ctrl->priv_data, intr_num);
295     }
296   }
297
298   return 0;
299 }