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.


more namespace changes
[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 void v3_init_interrupt_state(struct guest_info * info) {
35   info->intr_state.excp_pending = 0;
36   info->intr_state.excp_num = 0;
37   info->intr_state.excp_error_code = 0;
38
39   memset((uchar_t *)(info->intr_state.hooks), 0, sizeof(struct v3_irq_hook *) * 256);
40
41   info->vm_ops.raise_irq = &v3_raise_irq;
42   info->vm_ops.lower_irq = &v3_lower_irq; 
43 }
44
45 void v3_set_intr_controller(struct guest_info * info, struct intr_ctrl_ops * ops, void * state) {
46   info->intr_state.controller = ops;
47   info->intr_state.controller_state = state;
48 }
49
50
51
52
53 static inline struct v3_irq_hook * get_irq_hook(struct guest_info * info, uint_t irq) {
54   V3_ASSERT(irq <= 256);
55   return info->intr_state.hooks[irq];
56 }
57
58
59 int v3_hook_irq(struct guest_info * info, 
60                 uint_t irq,
61                 int (*handler)(struct guest_info * info, struct v3_interrupt * intr, void * priv_data),
62                 void  * priv_data) 
63 {
64   struct v3_irq_hook * hook = (struct v3_irq_hook *)V3_Malloc(sizeof(struct v3_irq_hook));
65
66   if (hook == NULL) { 
67     return -1; 
68   }
69
70   if (get_irq_hook(info, irq) != NULL) {
71     PrintError("IRQ %d already hooked\n", irq);
72     return -1;
73   }
74
75   hook->handler = handler;
76   hook->priv_data = priv_data;
77   
78   info->intr_state.hooks[irq] = hook;
79
80   if (V3_Hook_Interrupt(info, irq)) { 
81     PrintError("hook_irq: failed to hook irq %d\n", irq);
82     return -1;
83   } else {
84     PrintDebug("hook_irq: hooked irq %d\n", irq);
85     return 0;
86   }
87 }
88
89
90
91 static int passthrough_irq_handler(struct guest_info * info, struct v3_interrupt * intr, void * priv_data)
92 {
93
94   PrintDebug("[passthrough_irq_handler] raise_irq=%d (guest=0x%x)\n", intr->irq, info);
95   return v3_raise_irq(info, intr->irq);
96
97 }
98
99 int v3_hook_passthrough_irq(struct guest_info * info, uint_t irq)
100 {
101
102   int rc = v3_hook_irq(info, 
103                        irq,
104                        passthrough_irq_handler,
105                        NULL);
106
107   if (rc) { 
108     PrintError("guest_irq_injection: failed to hook irq 0x%x (guest=0x%x)\n", irq, info);
109     return -1;
110   } else {
111     PrintDebug("guest_irq_injection: hooked irq 0x%x (guest=0x%x)\n", irq, info);
112     return 0;
113   }
114 }
115
116
117
118
119
120 int v3_deliver_irq(struct guest_info * info, struct v3_interrupt * intr) {
121   PrintDebug("v3_deliver_irq: irq=%d state=0x%x, \n", intr->irq, intr);
122   
123   struct v3_irq_hook * hook = get_irq_hook(info, intr->irq);
124
125   if (hook == NULL) {
126     PrintError("Attempting to deliver interrupt to non registered hook(irq=%d)\n", intr->irq);
127     return -1;
128   }
129   
130   return hook->handler(info, intr, hook->priv_data);
131 }
132
133
134
135
136
137
138
139
140 int v3_raise_exception_with_error(struct guest_info * info, uint_t excp, uint_t error_code) {
141   struct v3_intr_state * intr_state = &(info->intr_state);
142
143   if (intr_state->excp_pending == 0) {
144     intr_state->excp_pending = 1;
145     intr_state->excp_num = excp;
146     intr_state->excp_error_code = error_code;
147     intr_state->excp_error_code_valid = 1;
148     PrintDebug("[v3_raise_exception_with_error] error code: %x\n", error_code);
149   } else {
150     PrintError("exception already pending, currently not implemented\n");
151     return -1;
152   }
153
154   return 0;
155 }
156
157 int v3_raise_exception(struct guest_info * info, uint_t excp) {
158   struct v3_intr_state * intr_state = &(info->intr_state);
159   PrintDebug("[v3_raise_exception]\n");
160   if (intr_state->excp_pending == 0) {
161     intr_state->excp_pending = 1;
162     intr_state->excp_num = excp;
163     intr_state->excp_error_code = 0;
164     intr_state->excp_error_code_valid = 0;
165   } else {
166     PrintError("exception already pending, currently not implemented\n");
167     return -1;
168   }
169
170   return 0;
171 }
172
173
174 int v3_lower_irq(struct guest_info * info, int irq) {
175   // Look up PIC and resend
176   V3_ASSERT(info);
177   V3_ASSERT(info->intr_state.controller);
178   V3_ASSERT(info->intr_state.controller->lower_intr);
179
180   PrintDebug("[v3_lower_irq]\n");
181
182   if ((info->intr_state.controller) && 
183       (info->intr_state.controller->lower_intr)) {
184     info->intr_state.controller->lower_intr(info->intr_state.controller_state, irq);
185   } else {
186     PrintError("There is no registered Interrupt Controller... (NULL POINTER)\n");
187     return -1;
188   }
189
190   return 0;
191 }
192
193 int v3_raise_irq(struct guest_info * info, int irq) {
194   // Look up PIC and resend
195   V3_ASSERT(info);
196   V3_ASSERT(info->intr_state.controller);
197   V3_ASSERT(info->intr_state.controller->raise_intr);
198
199   PrintDebug("[v3_raise_irq]\n");
200
201   if ((info->intr_state.controller) && 
202       (info->intr_state.controller->raise_intr)) {
203     info->intr_state.controller->raise_intr(info->intr_state.controller_state, irq);
204   } else {
205     PrintError("There is no registered Interrupt Controller... (NULL POINTER)\n");
206     return -1;
207   }
208
209   return 0;
210 }
211
212  
213
214 int v3_intr_pending(struct guest_info * info) {
215   struct v3_intr_state * intr_state = &(info->intr_state);
216
217   //  PrintDebug("[intr_pending]\n");
218   if (intr_state->excp_pending == 1) {
219     return 1;
220   } else if (intr_state->controller->intr_pending(intr_state->controller_state) == 1) {
221     return 1;
222   }
223
224   /* Check [A]PIC */
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 if (intr_state->controller->intr_pending(intr_state->controller_state)) {
236     PrintDebug("[get_intr_number] intr_number = %d\n", intr_state->controller->get_intr_number(intr_state->controller_state));
237     return intr_state->controller->get_intr_number(intr_state->controller_state);
238   }
239
240   /* someway to get the [A]PIC intr */
241
242   return 0;
243 }
244
245
246 intr_type_t v3_get_intr_type(struct guest_info * info) {
247   struct v3_intr_state * intr_state = &(info->intr_state);
248
249   if (intr_state->excp_pending) {
250     PrintDebug("[get_intr_type] Exception\n");
251     return EXCEPTION;
252   } else if (intr_state->controller->intr_pending(intr_state->controller_state)) {
253     PrintDebug("[get_intr_type] External_irq\n");
254     return EXTERNAL_IRQ;
255   }
256     PrintDebug("[get_intr_type] Invalid_Intr\n");
257   return INVALID_INTR;
258 }
259
260
261
262
263
264
265 int v3_injecting_intr(struct guest_info * info, uint_t intr_num, intr_type_t type) {
266   struct v3_intr_state * intr_state = &(info->intr_state);
267
268   if (type == EXCEPTION) {
269     PrintDebug("[injecting_intr] Exception\n");
270     intr_state->excp_pending = 0;
271     intr_state->excp_num = 0;
272     intr_state->excp_error_code = 0;
273     intr_state->excp_error_code_valid = 0;
274     
275   } else if (type == EXTERNAL_IRQ) {
276     PrintDebug("[injecting_intr] External_Irq with intr_num = %x\n", intr_num);
277     return intr_state->controller->begin_irq(intr_state->controller_state, intr_num);
278   }
279
280   return 0;
281 }