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.


fixed format string issues due to printf attribute checks
[palacios.git] / palacios / src / palacios / vmm_dev_mgr.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 #include <palacios/vm_dev.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vm_guest.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_decoder.h>
25
26
27 #ifndef DEBUG_DEV_MGR
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32
33
34 int v3_init_dev_mgr(struct guest_info * info) {
35   struct vmm_dev_mgr * mgr = &(info->dev_mgr);
36   INIT_LIST_HEAD(&(mgr->dev_list));
37   mgr->num_devs = 0;
38
39   INIT_LIST_HEAD(&(mgr->io_hooks));
40   mgr->num_io_hooks = 0;
41
42   return 0;
43 }
44
45
46 int v3_dev_mgr_deinit(struct guest_info * info) {
47   struct vm_device * dev;
48   struct vmm_dev_mgr * mgr = &(info->dev_mgr);
49   struct vm_device * tmp;
50
51   list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
52     v3_unattach_device(dev);
53     v3_free_device(dev);
54   }
55
56   return 0;
57 }
58
59
60
61
62 static int dev_mgr_add_device(struct vmm_dev_mgr * mgr, struct vm_device * dev) {
63   list_add(&(dev->dev_link), &(mgr->dev_list));
64   mgr->num_devs++;
65
66   return 0;
67 }
68
69 static int dev_mgr_remove_device(struct vmm_dev_mgr * mgr, struct vm_device * dev) {
70   list_del(&(dev->dev_link));
71   mgr->num_devs--;
72
73   return 0;
74 }
75
76
77
78 /* IO HOOKS */
79 static int dev_mgr_add_io_hook(struct vmm_dev_mgr * mgr, struct dev_io_hook * hook) {
80   list_add(&(hook->mgr_list), &(mgr->io_hooks));
81   mgr->num_io_hooks++;
82   return 0;
83 }
84
85
86 static int dev_mgr_remove_io_hook(struct vmm_dev_mgr * mgr, struct dev_io_hook * hook) {
87   list_del(&(hook->mgr_list));
88   mgr->num_io_hooks--;
89
90   return 0;
91 }
92
93
94 static int dev_add_io_hook(struct vm_device * dev, struct dev_io_hook * hook) {
95   list_add(&(hook->dev_list), &(dev->io_hooks));
96   dev->num_io_hooks++;
97   return 0;
98 }
99
100
101 static int dev_remove_io_hook(struct vm_device * dev, struct dev_io_hook * hook) {
102   list_del(&(hook->dev_list));
103   dev->num_io_hooks--;
104
105   return 0;
106 }
107
108
109
110
111
112 static struct dev_io_hook * dev_mgr_find_io_hook(struct vmm_dev_mgr * mgr, ushort_t port) {
113   struct dev_io_hook * tmp = NULL;
114
115   list_for_each_entry(tmp, &(mgr->io_hooks), mgr_list) {
116     if (tmp->port == port) {
117       return tmp;
118     }
119   }
120   return NULL;
121 }
122
123
124 /*
125 static struct dev_io_hook * dev_find_io_hook(struct vm_device * dev, ushort_t port) {
126   struct dev_io_hook * tmp = NULL;
127
128   list_for_each_entry(tmp, &(dev->io_hooks), dev_list) {
129     if (tmp->port == port) {
130       return tmp;
131     }
132   }
133   return NULL;
134 }
135 */
136
137
138
139 int v3_dev_hook_io(struct vm_device   *dev,
140                    ushort_t            port,
141                    int (*read)(ushort_t port, void * dst, uint_t length, struct vm_device * dev),
142                    int (*write)(ushort_t port, void * src, uint_t length, struct vm_device * dev)) {
143   
144   struct dev_io_hook *hook = (struct dev_io_hook *)V3_Malloc(sizeof(struct dev_io_hook));
145   
146   if (!hook) { 
147     return -1;
148   }
149
150
151   if (v3_hook_io_port(dev->vm, port, 
152                       (int (*)(ushort_t, void *, uint_t, void *))read, 
153                       (int (*)(ushort_t, void *, uint_t, void *))write, 
154                       (void *)dev) == 0) {
155
156     hook->dev = dev;
157     hook->port = port;
158     hook->read = read;
159     hook->write = write;
160     
161     dev_mgr_add_io_hook(&(dev->vm->dev_mgr), hook);
162     dev_add_io_hook(dev, hook);
163   } else {
164
165     return -1;
166   }
167
168   return 0;
169 }
170
171
172 int v3_dev_unhook_io(struct vm_device   *dev,
173                   ushort_t            port) {
174
175   struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
176   struct dev_io_hook * hook = dev_mgr_find_io_hook(mgr, port);
177
178   if (!hook) { 
179     return -1;
180   }
181
182   dev_mgr_remove_io_hook(mgr, hook);
183   dev_remove_io_hook(dev, hook);
184
185   return v3_unhook_io_port(dev->vm, port);
186 }
187
188
189 int v3_attach_device(struct guest_info * vm, struct vm_device * dev) {
190   struct vmm_dev_mgr *mgr= &(vm->dev_mgr);
191   
192   dev->vm = vm;
193   dev_mgr_add_device(mgr, dev);
194   dev->ops->init(dev);
195
196   return 0;
197 }
198
199 int v3_unattach_device(struct vm_device * dev) {
200   struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
201
202   dev->ops->deinit(dev);
203   dev_mgr_remove_device(mgr, dev);
204   dev->vm = NULL;
205
206   return 0;
207 }
208
209
210
211
212 #if 0
213 static int dev_mgr_hook_mem(struct guest_info    *vm,
214                             struct vm_device   *device,
215                             void               *start,
216                             void               *end)
217 {
218
219   struct dev_mem_hook * hook = (struct dev_mem_hook*)V3_Malloc(sizeof(struct dev_mem_hook));
220   //  V3_Malloc(struct dev_mem_hook *, hook,sizeof(struct dev_mem_hook));
221
222   if (!hook) { 
223     return -1;
224   }
225
226   /* not implemented yet
227   hook_memory(vm->mem_map, 
228               guest_physical_address_start, 
229               guest_physical_address_end, 
230               read,
231               write,
232               device);
233
234   */
235
236   return -1;   // remove when hook_memory works
237
238
239   hook->addr_start = start;
240   hook->addr_end = end;
241
242   return 0;
243   
244 }
245
246
247 static int dev_mgr_unhook_mem(struct vm_device   *dev,
248                               addr_t start,
249                               addr_t end)  {
250   /*
251   struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
252   struct dev_mem_hook *hook = dev_mgr_find_mem_hook(mgr, start, end);
253   
254   if (!hook) { 
255     // Very bad - unhooking something that doesn't exist!
256     return -1;
257   }
258   */
259
260   /* not implemented yet
261   return unhook_mem_port(vm->mem_map,
262                          guest_physical_start,
263                          guest_physical_end) ;
264
265   */
266   return -1;
267 }
268 #endif
269
270
271 #ifdef DEBUG_DEV_MGR
272
273 void PrintDebugDevMgr(struct guest_info * info) {
274   struct vmm_dev_mgr * mgr = &(info->dev_mgr);
275   struct vm_device * dev;
276   PrintDebug("%d devices registered with manager\n", mgr->num_devs);
277
278   list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
279     PrintDebugDev(dev);
280     PrintDebug("next..\n");
281   }
282
283   return;
284 }
285
286
287 void PrintDebugDev(struct vm_device * dev) {
288   
289   PrintDebug("Device: %s\n", dev->name);
290   PrintDebugDevIO(dev);
291 }
292
293 void PrintDebugDevMgrIO(struct vmm_dev_mgr * mgr) {
294
295 }
296
297 void PrintDebugDevIO(struct vm_device * dev) {
298   struct dev_io_hook * hook;
299
300   PrintDebug("IO Hooks(%d)  for Device: %s\n", dev->num_io_hooks,  dev->name);
301
302   list_for_each_entry(hook, &(dev->io_hooks), dev_list) {
303     PrintDebug("\tPort: 0x%x (read=0x%p), (write=0x%p)\n", hook->port, 
304                (void *)(addr_t)(hook->read), 
305                (void *)(addr_t)(hook->write));
306   }
307
308   return;
309 }
310
311 #else 
312 void PrintDebugDevMgr(struct guest_info * info) {}
313 void PrintDebugDev(struct vm_device * dev) {}
314 void PrintDebugDevMgrIO(struct vmm_dev_mgr * mgr) {}
315 void PrintDebugDevIO(struct vm_device * dev) {}
316 #endif