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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_dev_mgr.h>
21 #include <palacios/vm_guest.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_decoder.h>
26 #ifndef CONFIG_DEBUG_DEV_MGR
28 #define PrintDebug(fmt, args...)
32 static struct hashtable * master_dev_table = NULL;
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));
39 static int dev_eq_fn(addr_t key1, addr_t key2) {
40 char * name1 = (char *)key1;
41 char * name2 = (char *)key2;
43 return (strcmp(name1, name2) == 0);
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;
53 #ifdef CONFIG_DEBUG_DEV_MGR
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);
60 PrintDebug("Start addres=%p, Stop address=%p\n", __start__v3_devices, __stop__v3_devices);
62 master_dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
66 while (tmp_dev != __stop__v3_devices) {
67 PrintDebug("Device: %s\n", tmp_dev->name);
69 if (v3_htable_search(master_dev_table, (addr_t)(tmp_dev->name))) {
70 PrintError("Multiple instance of device (%s)\n", tmp_dev->name);
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);
81 tmp_dev = &(__start__v3_devices[++i]);
89 int v3_init_dev_mgr(struct v3_vm_info * vm) {
90 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
92 INIT_LIST_HEAD(&(mgr->dev_list));
95 mgr->dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
97 INIT_LIST_HEAD(&(mgr->blk_list));
98 INIT_LIST_HEAD(&(mgr->net_list));
99 INIT_LIST_HEAD(&(mgr->char_list));
100 INIT_LIST_HEAD(&(mgr->cons_list));
102 mgr->blk_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
103 mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
104 mgr->char_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
105 mgr->cons_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
111 int v3_free_vm_devices(struct v3_vm_info * vm) {
112 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
113 struct vm_device * dev;
114 struct vm_device * tmp;
116 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
117 v3_remove_device(dev);
123 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr);
125 int v3_deinit_dev_mgr(struct v3_vm_info * vm) {
126 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
128 // clear frontend lists
130 free_frontends(vm, mgr);
134 v3_free_htable(mgr->dev_table, 0, 0);
141 int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_t * cfg) {
142 int (*dev_init)(struct v3_vm_info * vm, void * cfg_data);
144 dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
146 if (dev_init == NULL) {
147 PrintError("Could not find device %s in master device table\n", dev_name);
152 if (dev_init(vm, cfg) == -1) {
153 PrintError("Could not initialize Device %s\n", dev_name);
163 struct vm_device * v3_find_dev(struct v3_vm_info * vm, const char * dev_name) {
164 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
170 return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
174 /****************************************************************/
175 /* The remaining functions are called by the devices themselves */
176 /****************************************************************/
181 struct list_head node;
186 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
187 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
188 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data)) {
189 struct dev_io_hook * io_hook = NULL;
192 ret = v3_hook_io_port(dev->vm, port,
193 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))read,
194 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))write,
195 (void *)dev->private_data);
201 io_hook = V3_Malloc(sizeof(struct dev_io_hook));
203 if (io_hook == NULL) {
204 PrintError("Could not allocate io hook dev state\n");
208 io_hook->port = port;
209 list_add(&(io_hook->node), &(dev->io_hooks));
215 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
216 struct dev_io_hook * io_hook = NULL;
217 struct dev_io_hook * tmp;
219 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
220 if (io_hook->port == port) {
222 list_del(&(io_hook->node));
225 return v3_unhook_io_port(dev->vm, port);
234 int v3_remove_device(struct vm_device * dev) {
235 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
236 struct dev_io_hook * io_hook = NULL;
237 struct dev_io_hook * tmp;
239 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
240 v3_unhook_io_port(dev->vm, io_hook->port);
241 list_del(&(io_hook->node));
245 if (dev->ops->free) {
246 dev->ops->free(dev->private_data);
248 PrintError("Error: %s free() not implemented\n", dev->name);
251 list_del(&(dev->dev_link));
261 struct vm_device * v3_add_device(struct v3_vm_info * vm,
263 struct v3_device_ops * ops,
264 void * private_data) {
265 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
266 struct vm_device * dev = NULL;
268 dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
274 INIT_LIST_HEAD(&(dev->io_hooks));
276 strncpy(dev->name, name, 32);
278 dev->private_data = private_data;
282 list_add(&(dev->dev_link), &(mgr->dev_list));
285 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
291 void v3_print_dev_mgr(struct v3_vm_info * vm) {
292 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
293 struct vm_device * dev;
295 V3_Print("%d devices registered with manager\n", mgr->num_devs);
297 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
298 V3_Print("Device: %s\n", dev->name);
307 struct blk_frontend {
308 int (*connect)(struct v3_vm_info * vm,
309 void * frontend_data,
310 struct v3_dev_blk_ops * ops,
315 struct list_head blk_node;
322 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
324 int (*connect)(struct v3_vm_info * vm,
325 void * frontend_data,
326 struct v3_dev_blk_ops * ops,
331 struct blk_frontend * frontend = NULL;
333 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
334 memset(frontend, 0, sizeof(struct blk_frontend));
336 frontend->connect = connect;
337 frontend->priv_data = priv_data;
339 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
340 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
345 int v3_dev_connect_blk(struct v3_vm_info * vm,
346 char * frontend_name,
347 struct v3_dev_blk_ops * ops,
349 void * private_data) {
351 struct blk_frontend * frontend = NULL;
353 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
354 (addr_t)frontend_name);
356 if (frontend == NULL) {
357 PrintError("Could not find frontend blk device %s\n", frontend_name);
361 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
362 PrintError("Error connecting to block frontend %s\n", frontend_name);
371 struct net_frontend {
372 int (*connect)(struct v3_vm_info * vm,
373 void * frontend_data,
374 struct v3_dev_net_ops * ops,
379 struct list_head net_node;
385 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
387 int (*connect)(struct v3_vm_info * vm,
388 void * frontend_data,
389 struct v3_dev_net_ops * ops,
391 void * private_data),
394 struct net_frontend * frontend = NULL;
396 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
397 memset(frontend, 0, sizeof(struct net_frontend));
399 frontend->connect = connect;
400 frontend->priv_data = priv_data;
402 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
403 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
409 int v3_dev_connect_net(struct v3_vm_info * vm,
410 char * frontend_name,
411 struct v3_dev_net_ops * ops,
415 struct net_frontend * frontend = NULL;
417 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
418 (addr_t)frontend_name);
420 if (frontend == NULL) {
421 PrintError("Could not find frontend net device %s\n", frontend_name);
425 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
426 PrintError("Error connecting to net frontend %s\n", frontend_name);
434 struct cons_frontend {
435 int (*connect)(struct v3_vm_info * vm,
436 void * frontend_data,
437 struct v3_dev_console_ops * ops,
442 struct list_head cons_node;
447 int v3_dev_add_console_frontend(struct v3_vm_info * vm,
449 int (*connect)(struct v3_vm_info * vm,
450 void * frontend_data,
451 struct v3_dev_console_ops * ops,
453 void * private_data),
456 struct cons_frontend * frontend = NULL;
458 frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
459 memset(frontend, 0, sizeof(struct cons_frontend));
461 frontend->connect = connect;
462 frontend->priv_data = priv_data;
464 list_add(&(frontend->cons_node), &(vm->dev_mgr.cons_list));
465 v3_htable_insert(vm->dev_mgr.cons_table, (addr_t)(name), (addr_t)frontend);
471 int v3_dev_connect_console(struct v3_vm_info * vm,
472 char * frontend_name,
473 struct v3_dev_console_ops * ops,
477 struct cons_frontend * frontend = NULL;
479 frontend = (struct cons_frontend *)v3_htable_search(vm->dev_mgr.cons_table,
480 (addr_t)frontend_name);
482 if (frontend == NULL) {
483 PrintError("Could not find frontend console device %s\n", frontend_name);
487 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
488 PrintError("Error connecting to console frontend %s\n", frontend_name);
495 struct char_frontend {
496 int (*connect)(struct v3_vm_info * vm,
497 void * frontend_data,
498 struct v3_dev_char_ops * ops,
501 void ** push_fn_arg);
504 struct list_head char_node;
509 int v3_dev_add_char_frontend(struct v3_vm_info * vm,
511 int (*connect)(struct v3_vm_info * vm,
512 void * frontend_data,
513 struct v3_dev_char_ops * ops,
516 void ** push_fn_arg),
519 struct char_frontend * frontend = NULL;
521 frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
522 memset(frontend, 0, sizeof(struct char_frontend));
524 frontend->connect = connect;
525 frontend->priv_data = priv_data;
527 list_add(&(frontend->char_node), &(vm->dev_mgr.char_list));
528 v3_htable_insert(vm->dev_mgr.char_table, (addr_t)(name), (addr_t)frontend);
534 int v3_dev_connect_char(struct v3_vm_info * vm,
535 char * frontend_name,
536 struct v3_dev_char_ops * ops,
541 struct char_frontend * frontend = NULL;
543 frontend = (struct char_frontend *)v3_htable_search(vm->dev_mgr.char_table,
544 (addr_t)frontend_name);
546 if (frontend == NULL) {
547 PrintError("Could not find frontend char device %s\n", frontend_name);
551 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
552 PrintError("Error connecting to char frontend %s\n", frontend_name);
561 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
562 struct char_frontend * chr = NULL;
563 struct char_frontend * tmp_chr = NULL;
564 struct cons_frontend * cons = NULL;
565 struct cons_frontend * tmp_cons = NULL;
566 struct net_frontend * net = NULL;
567 struct net_frontend * tmp_net = NULL;
568 struct blk_frontend * blk = NULL;
569 struct blk_frontend * tmp_blk = NULL;
573 list_for_each_entry_safe(chr, tmp_chr, &(mgr->char_list), char_node) {
574 list_del(&(chr->char_node));
578 list_for_each_entry_safe(cons, tmp_cons, &(mgr->cons_list), cons_node) {
579 list_del(&(cons->cons_node));
583 list_for_each_entry_safe(net, tmp_net, &(mgr->net_list), net_node) {
584 list_del(&(net->net_node));
588 list_for_each_entry_safe(blk, tmp_blk, &(mgr->blk_list), blk_node) {
589 list_del(&(blk->blk_node));
593 v3_free_htable(mgr->blk_table, 0, 0);
594 v3_free_htable(mgr->net_table, 0, 0);
595 v3_free_htable(mgr->char_table, 0, 0);
596 v3_free_htable(mgr->cons_table, 0, 0);