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_deinit_devices() {
90 v3_free_htable(master_dev_table, 0, 0);
95 int v3_init_dev_mgr(struct v3_vm_info * vm) {
96 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
98 INIT_LIST_HEAD(&(mgr->dev_list));
101 mgr->dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
103 INIT_LIST_HEAD(&(mgr->blk_list));
104 INIT_LIST_HEAD(&(mgr->net_list));
105 INIT_LIST_HEAD(&(mgr->char_list));
106 INIT_LIST_HEAD(&(mgr->cons_list));
108 mgr->blk_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
109 mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
110 mgr->char_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
111 mgr->cons_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
117 int v3_free_vm_devices(struct v3_vm_info * vm) {
118 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
119 struct vm_device * dev;
120 struct vm_device * tmp;
122 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
123 v3_remove_device(dev);
129 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr);
131 int v3_deinit_dev_mgr(struct v3_vm_info * vm) {
132 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
134 // clear frontend lists
136 free_frontends(vm, mgr);
140 v3_free_htable(mgr->dev_table, 0, 0);
147 int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_t * cfg) {
148 int (*dev_init)(struct v3_vm_info * vm, void * cfg_data);
150 dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
152 if (dev_init == NULL) {
153 PrintError("Could not find device %s in master device table\n", dev_name);
158 if (dev_init(vm, cfg) == -1) {
159 PrintError("Could not initialize Device %s\n", dev_name);
169 struct vm_device * v3_find_dev(struct v3_vm_info * vm, const char * dev_name) {
170 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
176 return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
180 /****************************************************************/
181 /* The remaining functions are called by the devices themselves */
182 /****************************************************************/
187 struct list_head node;
192 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
193 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
194 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data)) {
195 struct dev_io_hook * io_hook = NULL;
198 ret = v3_hook_io_port(dev->vm, port,
199 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))read,
200 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))write,
201 (void *)dev->private_data);
207 io_hook = V3_Malloc(sizeof(struct dev_io_hook));
209 if (io_hook == NULL) {
210 PrintError("Could not allocate io hook dev state\n");
214 io_hook->port = port;
215 list_add(&(io_hook->node), &(dev->io_hooks));
221 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
222 struct dev_io_hook * io_hook = NULL;
223 struct dev_io_hook * tmp;
225 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
226 if (io_hook->port == port) {
228 list_del(&(io_hook->node));
231 return v3_unhook_io_port(dev->vm, port);
240 int v3_remove_device(struct vm_device * dev) {
241 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
242 struct dev_io_hook * io_hook = NULL;
243 struct dev_io_hook * tmp;
245 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
246 v3_unhook_io_port(dev->vm, io_hook->port);
247 list_del(&(io_hook->node));
251 if (dev->ops->free) {
252 dev->ops->free(dev->private_data);
254 PrintError("Error: %s free() not implemented\n", dev->name);
257 list_del(&(dev->dev_link));
267 struct vm_device * v3_add_device(struct v3_vm_info * vm,
269 struct v3_device_ops * ops,
270 void * private_data) {
271 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
272 struct vm_device * dev = NULL;
274 dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
280 INIT_LIST_HEAD(&(dev->io_hooks));
282 strncpy(dev->name, name, 32);
284 dev->private_data = private_data;
288 list_add(&(dev->dev_link), &(mgr->dev_list));
291 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
297 void v3_print_dev_mgr(struct v3_vm_info * vm) {
298 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
299 struct vm_device * dev;
301 V3_Print("%d devices registered with manager\n", mgr->num_devs);
303 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
304 V3_Print("Device: %s\n", dev->name);
313 struct blk_frontend {
314 int (*connect)(struct v3_vm_info * vm,
315 void * frontend_data,
316 struct v3_dev_blk_ops * ops,
321 struct list_head blk_node;
328 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
330 int (*connect)(struct v3_vm_info * vm,
331 void * frontend_data,
332 struct v3_dev_blk_ops * ops,
337 struct blk_frontend * frontend = NULL;
339 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
340 memset(frontend, 0, sizeof(struct blk_frontend));
342 frontend->connect = connect;
343 frontend->priv_data = priv_data;
345 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
346 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
351 int v3_dev_connect_blk(struct v3_vm_info * vm,
352 char * frontend_name,
353 struct v3_dev_blk_ops * ops,
355 void * private_data) {
357 struct blk_frontend * frontend = NULL;
359 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
360 (addr_t)frontend_name);
362 if (frontend == NULL) {
363 PrintError("Could not find frontend blk device %s\n", frontend_name);
367 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
368 PrintError("Error connecting to block frontend %s\n", frontend_name);
377 struct net_frontend {
378 int (*connect)(struct v3_vm_info * vm,
379 void * frontend_data,
380 struct v3_dev_net_ops * ops,
385 struct list_head net_node;
391 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
393 int (*connect)(struct v3_vm_info * vm,
394 void * frontend_data,
395 struct v3_dev_net_ops * ops,
397 void * private_data),
400 struct net_frontend * frontend = NULL;
402 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
403 memset(frontend, 0, sizeof(struct net_frontend));
405 frontend->connect = connect;
406 frontend->priv_data = priv_data;
408 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
409 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
415 int v3_dev_connect_net(struct v3_vm_info * vm,
416 char * frontend_name,
417 struct v3_dev_net_ops * ops,
421 struct net_frontend * frontend = NULL;
423 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
424 (addr_t)frontend_name);
426 if (frontend == NULL) {
427 PrintError("Could not find frontend net device %s\n", frontend_name);
431 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
432 PrintError("Error connecting to net frontend %s\n", frontend_name);
440 struct cons_frontend {
441 int (*connect)(struct v3_vm_info * vm,
442 void * frontend_data,
443 struct v3_dev_console_ops * ops,
448 struct list_head cons_node;
453 int v3_dev_add_console_frontend(struct v3_vm_info * vm,
455 int (*connect)(struct v3_vm_info * vm,
456 void * frontend_data,
457 struct v3_dev_console_ops * ops,
459 void * private_data),
462 struct cons_frontend * frontend = NULL;
464 frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
465 memset(frontend, 0, sizeof(struct cons_frontend));
467 frontend->connect = connect;
468 frontend->priv_data = priv_data;
470 list_add(&(frontend->cons_node), &(vm->dev_mgr.cons_list));
471 v3_htable_insert(vm->dev_mgr.cons_table, (addr_t)(name), (addr_t)frontend);
477 int v3_dev_connect_console(struct v3_vm_info * vm,
478 char * frontend_name,
479 struct v3_dev_console_ops * ops,
483 struct cons_frontend * frontend = NULL;
485 frontend = (struct cons_frontend *)v3_htable_search(vm->dev_mgr.cons_table,
486 (addr_t)frontend_name);
488 if (frontend == NULL) {
489 PrintError("Could not find frontend console device %s\n", frontend_name);
493 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
494 PrintError("Error connecting to console frontend %s\n", frontend_name);
501 struct char_frontend {
502 int (*connect)(struct v3_vm_info * vm,
503 void * frontend_data,
504 struct v3_dev_char_ops * ops,
507 void ** push_fn_arg);
510 struct list_head char_node;
515 int v3_dev_add_char_frontend(struct v3_vm_info * vm,
517 int (*connect)(struct v3_vm_info * vm,
518 void * frontend_data,
519 struct v3_dev_char_ops * ops,
522 void ** push_fn_arg),
525 struct char_frontend * frontend = NULL;
527 frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
528 memset(frontend, 0, sizeof(struct char_frontend));
530 frontend->connect = connect;
531 frontend->priv_data = priv_data;
533 list_add(&(frontend->char_node), &(vm->dev_mgr.char_list));
534 v3_htable_insert(vm->dev_mgr.char_table, (addr_t)(name), (addr_t)frontend);
540 int v3_dev_connect_char(struct v3_vm_info * vm,
541 char * frontend_name,
542 struct v3_dev_char_ops * ops,
547 struct char_frontend * frontend = NULL;
549 frontend = (struct char_frontend *)v3_htable_search(vm->dev_mgr.char_table,
550 (addr_t)frontend_name);
552 if (frontend == NULL) {
553 PrintError("Could not find frontend char device %s\n", frontend_name);
557 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
558 PrintError("Error connecting to char frontend %s\n", frontend_name);
567 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
568 struct char_frontend * chr = NULL;
569 struct char_frontend * tmp_chr = NULL;
570 struct cons_frontend * cons = NULL;
571 struct cons_frontend * tmp_cons = NULL;
572 struct net_frontend * net = NULL;
573 struct net_frontend * tmp_net = NULL;
574 struct blk_frontend * blk = NULL;
575 struct blk_frontend * tmp_blk = NULL;
579 list_for_each_entry_safe(chr, tmp_chr, &(mgr->char_list), char_node) {
580 list_del(&(chr->char_node));
584 list_for_each_entry_safe(cons, tmp_cons, &(mgr->cons_list), cons_node) {
585 list_del(&(cons->cons_node));
589 list_for_each_entry_safe(net, tmp_net, &(mgr->net_list), net_node) {
590 list_del(&(net->net_node));
594 list_for_each_entry_safe(blk, tmp_blk, &(mgr->blk_list), blk_node) {
595 list_del(&(blk->blk_node));
599 v3_free_htable(mgr->blk_table, 0, 0);
600 v3_free_htable(mgr->net_table, 0, 0);
601 v3_free_htable(mgr->char_table, 0, 0);
602 v3_free_htable(mgr->cons_table, 0, 0);