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.


moved network backend/frontend registration to correct location
[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/vmm_dev_mgr.h>
21 #include <palacios/vm_guest.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_decoder.h>
24
25
26 #ifndef CONFIG_DEBUG_DEV_MGR
27 #undef PrintDebug
28 #define PrintDebug(fmt, args...)
29 #endif
30
31
32 static struct hashtable * master_dev_table = NULL;
33
34 static uint_t dev_hash_fn(addr_t key) {
35     char * name = (char *)key;
36     return v3_hash_buffer((uchar_t *)name, strlen(name));
37 }
38
39 static int dev_eq_fn(addr_t key1, addr_t key2) {
40     char * name1 = (char *)key1;
41     char * name2 = (char *)key2;
42     
43     return (strcmp(name1, name2) == 0);
44 }
45
46
47 int v3_init_devices() {
48     extern struct v3_device_info __start__v3_devices[];
49     extern struct v3_device_info __stop__v3_devices[];
50     struct v3_device_info * tmp_dev =  __start__v3_devices;
51     int i = 0;
52
53 #ifdef CONFIG_DEBUG_DEV_MGR
54     {
55         int num_devices = (__stop__v3_devices - __start__v3_devices) / sizeof(struct v3_device_info);
56         PrintDebug("%d Virtual devices registered with Palacios\n", num_devices);
57     }
58 #endif
59
60     PrintDebug("Start addres=%p, Stop address=%p\n", __start__v3_devices, __stop__v3_devices);
61
62     master_dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
63
64
65
66     while (tmp_dev != __stop__v3_devices) {
67         PrintDebug("Device: %s\n", tmp_dev->name);
68
69         if (v3_htable_search(master_dev_table, (addr_t)(tmp_dev->name))) {
70             PrintError("Multiple instance of device (%s)\n", tmp_dev->name);
71             return -1;
72         }
73
74         if (v3_htable_insert(master_dev_table, 
75                              (addr_t)(tmp_dev->name), 
76                              (addr_t)(tmp_dev->init)) == 0) {
77             PrintError("Could not add device %s to master list\n", tmp_dev->name);
78             return -1;
79         }
80
81         tmp_dev = &(__start__v3_devices[++i]);
82     }
83
84
85     return 0;
86 }
87
88
89 int v3_init_dev_mgr(struct guest_info * info) {
90     struct vmm_dev_mgr * mgr = &(info->dev_mgr);
91
92     INIT_LIST_HEAD(&(mgr->dev_list));
93     mgr->num_devs = 0;
94
95     mgr->dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
96
97     INIT_LIST_HEAD(&(mgr->blk_list));
98     INIT_LIST_HEAD(&(mgr->net_list));
99     INIT_LIST_HEAD(&(mgr->console_list));
100
101     mgr->blk_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
102     mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
103     mgr->console_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
104     
105     return 0;
106 }
107
108
109 int v3_dev_mgr_deinit(struct guest_info * info) {
110     struct vm_device * dev;
111     struct vmm_dev_mgr * mgr = &(info->dev_mgr);
112     struct vm_device * tmp;
113
114     list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
115         v3_detach_device(dev);
116         v3_free_device(dev);
117     }
118
119     return 0;
120 }
121
122
123
124 int v3_create_device(struct guest_info * info, const char * dev_name, v3_cfg_tree_t * cfg) {
125     int (*dev_init)(struct guest_info * info, void * cfg_data);
126
127     dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
128
129     if (dev_init == NULL) {
130         PrintError("Could not find device %s in master device table\n", dev_name);
131         return -1;
132     }
133
134
135     if (dev_init(info, cfg) == -1) {
136         PrintError("Could not initialize Device %s\n", dev_name);
137         return -1;
138     }
139
140     return 0;
141 }
142
143
144 void v3_free_device(struct vm_device * dev) {
145     V3_Free(dev);
146 }
147
148
149
150 struct vm_device * v3_find_dev(struct guest_info * info, const char * dev_name) {
151     struct vmm_dev_mgr * mgr = &(info->dev_mgr);
152
153     if (!dev_name) {
154         return NULL;
155     }
156
157     return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
158 }
159
160
161 /****************************************************************/
162 /* The remaining functions are called by the devices themselves */
163 /****************************************************************/
164
165 /* IO HOOKS */
166 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
167                    int (*read)(uint16_t port, void * dst, uint_t length, struct vm_device * dev),
168                    int (*write)(uint16_t port, void * src, uint_t length, struct vm_device * dev)) {
169     return v3_hook_io_port(dev->vm, port, 
170                            (int (*)(ushort_t, void *, uint_t, void *))read, 
171                            (int (*)(ushort_t, void *, uint_t, void *))write, 
172                            (void *)dev);
173 }
174
175
176 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
177     return v3_unhook_io_port(dev->vm, port);
178 }
179
180
181
182 int v3_detach_device(struct vm_device * dev) {
183     struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
184
185     dev->ops->free(dev);
186
187     list_del(&(dev->dev_link));
188     mgr->num_devs--;
189
190     dev->vm = NULL;
191
192     return -1;
193 }
194
195
196 struct vm_device * v3_allocate_device(char * name, 
197                                       struct v3_device_ops * ops, 
198                                       void * private_data) {
199     struct vm_device * dev = NULL;
200
201     dev = (struct vm_device*)V3_Malloc(sizeof(struct vm_device));
202
203     strncpy(dev->name, name, 32);
204     dev->ops = ops;
205     dev->private_data = private_data;
206
207     dev->vm = NULL;
208
209     return dev;
210 }
211
212
213 int v3_attach_device(struct guest_info * vm, struct vm_device * dev ) {
214     struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
215
216     dev->vm = vm;
217
218     list_add(&(dev->dev_link), &(mgr->dev_list));
219     mgr->num_devs++;
220
221
222     v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
223
224     return 0;
225 }
226
227
228
229 void v3_print_dev_mgr(struct guest_info * info) {
230     struct vmm_dev_mgr * mgr = &(info->dev_mgr);
231     struct vm_device * dev;
232
233     V3_Print("%d devices registered with manager\n", mgr->num_devs);
234
235     list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
236         V3_Print("Device: %s\n", dev->name);
237     }
238
239     return;
240 }
241
242
243
244
245 struct blk_frontend {
246     int (*connect)(struct guest_info * info, 
247                     void * frontend_data, 
248                     struct v3_dev_blk_ops * ops, 
249                     v3_cfg_tree_t * cfg, 
250                     void * priv_data);
251         
252
253     struct list_head blk_node;
254
255     void * priv_data;
256 };
257
258
259
260 int v3_dev_add_blk_frontend(struct guest_info * info, 
261                             char * name, 
262                             int (*connect)(struct guest_info * info, 
263                                             void * frontend_data, 
264                                             struct v3_dev_blk_ops * ops, 
265                                             v3_cfg_tree_t * cfg, 
266                                             void * priv_data), 
267                             void * priv_data) {
268
269     struct blk_frontend * frontend = NULL;
270
271     frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
272     memset(frontend, 0, sizeof(struct blk_frontend));
273     
274     frontend->connect = connect;
275     frontend->priv_data = priv_data;
276         
277     list_add(&(frontend->blk_node), &(info->dev_mgr.blk_list));
278     v3_htable_insert(info->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
279
280     return 0;
281 }
282
283 int v3_dev_connect_blk(struct guest_info * info, 
284                        char * frontend_name, 
285                        struct v3_dev_blk_ops * ops, 
286                        v3_cfg_tree_t * cfg, 
287                        void * private_data) {
288
289     struct blk_frontend * frontend = NULL;
290
291     frontend = (struct blk_frontend *)v3_htable_search(info->dev_mgr.blk_table,
292                                                        (addr_t)frontend_name);
293     
294     if (frontend == NULL) {
295         PrintError("Could not find frontend blk device %s\n", frontend_name);
296         return 0;
297     }
298
299     if (frontend->connect(info, frontend->priv_data, ops, cfg, private_data) == -1) {
300         PrintError("Error connecting to block frontend %s\n", frontend_name);
301         return -1;
302     }
303
304     return 0;
305 }
306
307
308
309 struct net_frontend {
310     int (*connect)(struct guest_info * info, 
311                     void * frontend_data, 
312                     struct v3_dev_net_ops * ops, 
313                     v3_cfg_tree_t * cfg, 
314                     void * priv_data);
315         
316
317     struct list_head net_node;
318
319     void * priv_data;
320 };
321
322
323 int v3_dev_add_net_frontend(struct guest_info * info, 
324                             char * name, 
325                             int (*connect)(struct guest_info * info, 
326                                             void * frontend_data, 
327                                             struct v3_dev_net_ops * ops, 
328                                             v3_cfg_tree_t * cfg, 
329                                             void * private_data), 
330                             void * priv_data)
331 {
332     struct net_frontend * frontend = NULL;
333
334     frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
335     memset(frontend, 0, sizeof(struct net_frontend));
336     
337     frontend->connect = connect;
338     frontend->priv_data = priv_data;
339         
340     list_add(&(frontend->net_node), &(info->dev_mgr.net_list));
341     v3_htable_insert(info->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
342
343     return 0;
344 }
345
346
347 int v3_dev_connect_net(struct guest_info * info, 
348                        char * frontend_name, 
349                        struct v3_dev_net_ops * ops, 
350                        v3_cfg_tree_t * cfg, 
351                        void * private_data)
352 {
353     struct net_frontend * frontend = NULL;
354
355     frontend = (struct net_frontend *)v3_htable_search(info->dev_mgr.net_table,
356                                                        (addr_t)frontend_name);
357     
358     if (frontend == NULL) {
359         PrintError("Could not find frontend net device %s\n", frontend_name);
360         return 0;
361     }
362
363     if (frontend->connect(info, frontend->priv_data, ops, cfg, private_data) == -1) {
364         PrintError("Error connecting to net frontend %s\n", frontend_name);
365         return -1;
366     }
367
368     return 0;
369 }
370