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>
25 #ifdef V3_CONFIG_CHECKPOINT
26 #include <palacios/vmm_checkpoint.h>
30 #ifndef V3_CONFIG_DEBUG_DEV_MGR
32 #define PrintDebug(fmt, args...)
36 static struct hashtable * master_dev_table = NULL;
38 static uint_t dev_hash_fn(addr_t key) {
39 char * name = (char *)key;
40 return v3_hash_buffer((uchar_t *)name, strlen(name));
43 static int dev_eq_fn(addr_t key1, addr_t key2) {
44 char * name1 = (char *)key1;
45 char * name2 = (char *)key2;
47 return (strcmp(name1, name2) == 0);
51 int V3_init_devices() {
52 extern struct v3_device_info __start__v3_devices[];
53 extern struct v3_device_info __stop__v3_devices[];
54 struct v3_device_info * tmp_dev = __start__v3_devices;
57 #ifdef V3_CONFIG_DEBUG_DEV_MGR
59 int num_devices = (__stop__v3_devices - __start__v3_devices) / sizeof(struct v3_device_info);
60 PrintDebug("%d Virtual devices registered with Palacios\n", num_devices);
64 PrintDebug("Start addres=%p, Stop address=%p\n", __start__v3_devices, __stop__v3_devices);
66 master_dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
70 while (tmp_dev != __stop__v3_devices) {
71 V3_Print("Registering Device: %s\n", tmp_dev->name);
73 if (v3_htable_search(master_dev_table, (addr_t)(tmp_dev->name))) {
74 PrintError("Multiple instance of device (%s)\n", tmp_dev->name);
78 if (v3_htable_insert(master_dev_table,
79 (addr_t)(tmp_dev->name),
80 (addr_t)(tmp_dev->init)) == 0) {
81 PrintError("Could not add device %s to master list\n", tmp_dev->name);
85 tmp_dev = &(__start__v3_devices[++i]);
93 int V3_deinit_devices() {
94 v3_free_htable(master_dev_table, 0, 0);
99 int v3_init_dev_mgr(struct v3_vm_info * vm) {
100 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
102 INIT_LIST_HEAD(&(mgr->dev_list));
105 mgr->dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
107 INIT_LIST_HEAD(&(mgr->blk_list));
108 INIT_LIST_HEAD(&(mgr->net_list));
109 INIT_LIST_HEAD(&(mgr->char_list));
110 INIT_LIST_HEAD(&(mgr->cons_list));
112 mgr->blk_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
113 mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
114 mgr->char_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
115 mgr->cons_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
121 int v3_free_vm_devices(struct v3_vm_info * vm) {
122 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
123 struct vm_device * dev;
124 struct vm_device * tmp;
126 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
127 v3_remove_device(dev);
133 #ifdef V3_CONFIG_CHECKPOINT
135 int v3_save_vm_devices(struct v3_vm_info * vm, struct v3_chkpt * chkpt) {
136 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
137 struct vm_device * dev;
138 struct v3_chkpt_ctx * dev_mgr_ctx = NULL;
140 uint32_t num_saved_devs = 0;
141 uint32_t table_len = mgr->num_devs * 32;
142 char * name_table = NULL;
143 uint32_t tbl_offset = 0;
145 name_table = V3_Malloc(table_len);
147 memset(name_table, 0, table_len);
150 dev_mgr_ctx = v3_chkpt_open_ctx(chkpt, NULL, "devices");
152 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
153 if (dev->ops->save) {
154 strncpy(name_table + tbl_offset, dev->name, 32);
160 v3_chkpt_save(dev_mgr_ctx, "num_devs", 4, &num_saved_devs);
161 v3_chkpt_save(dev_mgr_ctx, "names", num_saved_devs*32, name_table);
163 v3_chkpt_close_ctx(dev_mgr_ctx);
167 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
168 if (dev->ops->save) {
169 struct v3_chkpt_ctx * dev_ctx = NULL;
171 V3_Print("Saving state for device (%s)\n", dev->name);
173 dev_ctx = v3_chkpt_open_ctx(chkpt, NULL, dev->name);
175 dev->ops->save(dev_ctx, dev->private_data);
177 v3_chkpt_close_ctx(dev_ctx);
181 PrintError("Error: %s save() not implemented\n", dev->name);
189 int v3_load_vm_devices(struct v3_vm_info * vm, struct v3_chkpt * chkpt) {
190 struct vm_device * dev;
191 struct v3_chkpt_ctx * dev_mgr_ctx = NULL;
192 uint32_t num_devs = 0;
193 char * name_table = NULL;
196 dev_mgr_ctx = v3_chkpt_open_ctx(chkpt, NULL, "devices");
198 v3_chkpt_load(dev_mgr_ctx, "num_devs", 4, &num_devs);
200 V3_Print("Loading State for %d devices\n", num_devs);
202 name_table = V3_Malloc(32 * num_devs);
204 v3_chkpt_load(dev_mgr_ctx, "names", 32 * num_devs, name_table);
206 v3_chkpt_close_ctx(dev_mgr_ctx);
208 for (i = 0; i < num_devs; i++) {
209 char * name = &(name_table[i * 32]);
210 struct v3_chkpt_ctx * dev_ctx = NULL;
211 dev = v3_find_dev(vm, name);
214 PrintError("Tried to load state into non existant device: %s\n", name);
218 if (!dev->ops->load) {
219 PrintError("Error Device (%s) does not support load operation\n", name);
223 dev_ctx = v3_chkpt_open_ctx(chkpt, NULL, name);
226 PrintError("Error missing device context (%s)\n", name);
231 dev->ops->load(dev_ctx, dev->private_data);
233 v3_chkpt_close_ctx(dev_ctx);
244 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr);
246 int v3_deinit_dev_mgr(struct v3_vm_info * vm) {
247 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
249 // clear frontend lists
251 free_frontends(vm, mgr);
255 v3_free_htable(mgr->dev_table, 0, 0);
262 int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_t * cfg) {
263 int (*dev_init)(struct v3_vm_info * vm, void * cfg_data);
265 dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
267 if (dev_init == NULL) {
268 PrintError("Could not find device %s in master device table\n", dev_name);
273 if (dev_init(vm, cfg) == -1) {
274 PrintError("Could not initialize Device %s\n", dev_name);
284 struct vm_device * v3_find_dev(struct v3_vm_info * vm, const char * dev_name) {
285 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
291 return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
295 /****************************************************************/
296 /* The remaining functions are called by the devices themselves */
297 /****************************************************************/
299 typedef enum {DEV_IO_HOOK, DEV_MSR_HOOK, DEV_CPUID_HOOK, DEV_MEM_HOOK, DEV_HCALL_HOOK} dev_rsrc_type_t;
302 dev_rsrc_type_t type;
305 struct list_head node;
310 static int add_resource(struct vm_device * dev, dev_rsrc_type_t type, uint64_t rsrc_id) {
311 struct dev_rsrc * resource = NULL;
313 resource = V3_Malloc(sizeof(struct dev_rsrc));
315 if (resource == NULL) {
316 PrintError("Error: Could not allocate device resource\n");
320 resource->rsrc = rsrc_id;
321 resource->type = type;
323 list_add(&(resource->node), &(dev->res_hooks));
327 static int free_resource(struct vm_device * dev, dev_rsrc_type_t type, uint64_t rsrc_id) {
328 struct dev_rsrc * resource = NULL;
329 struct dev_rsrc * tmp;
331 list_for_each_entry_safe(resource, tmp, &(dev->res_hooks), node) {
332 if ((resource->type == type) &&
333 (resource->rsrc == rsrc_id)) {
335 list_del(&(resource->node));
346 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
347 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
348 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data)) {
351 ret = v3_hook_io_port(dev->vm, port,
352 (int (*)(struct guest_info * core, uint16_t, void *, uint_t, void *))read,
353 (int (*)(struct guest_info * core, uint16_t, void *, uint_t, void *))write,
354 (void *)dev->private_data);
360 if (add_resource(dev, DEV_IO_HOOK, port) == -1) {
361 v3_unhook_io_port(dev->vm, port);
362 PrintError("Could not allocate io hook dev state\n");
370 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
371 if (free_resource(dev, DEV_IO_HOOK, port) == 0) {
372 return v3_unhook_io_port(dev->vm, port);
379 int v3_dev_hook_msr(struct vm_device * dev, uint32_t msr,
380 int (*read)(struct guest_info * core, uint32_t msr, struct v3_msr * dst, void * priv_data),
381 int (*write)(struct guest_info * core, uint32_t msr, struct v3_msr src, void * priv_data)) {
384 ret = v3_hook_msr(dev->vm, msr, read, write, dev->private_data);
390 if (add_resource(dev, DEV_MSR_HOOK, msr) == -1) {
391 v3_unhook_msr(dev->vm, msr);
398 int v3_dev_unhook_msr(struct vm_device * dev, uint32_t msr) {
399 if (free_resource(dev, DEV_MSR_HOOK, msr) == 0) {
400 return v3_unhook_msr(dev->vm, msr);
409 int v3_remove_device(struct vm_device * dev) {
410 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
411 struct dev_rsrc * resource = NULL;
412 struct dev_rsrc * tmp;
414 list_for_each_entry_safe(resource, tmp, &(dev->res_hooks), node) {
415 if (resource->type == DEV_IO_HOOK) {
416 v3_unhook_io_port(dev->vm, (uint16_t)(resource->rsrc));
417 } else if (resource->type == DEV_MSR_HOOK) {
418 v3_unhook_msr(dev->vm, (uint32_t)(resource->rsrc));
421 list_del(&(resource->node));
425 if (dev->ops->free) {
426 dev->ops->free(dev->private_data);
428 PrintError("Error: %s free() not implemented\n", dev->name);
431 list_del(&(dev->dev_link));
441 struct vm_device * v3_add_device(struct v3_vm_info * vm,
443 struct v3_device_ops * ops,
444 void * private_data) {
445 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
446 struct vm_device * dev = NULL;
449 // Check if we already registered a device of the same name
450 if (v3_htable_search(mgr->dev_table, (addr_t)name) != (addr_t)NULL) {
451 PrintError("Device with name (%s) already registered with VM\n", name);
455 dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
461 INIT_LIST_HEAD(&(dev->res_hooks));
463 strncpy(dev->name, name, 32);
465 dev->private_data = private_data;
469 list_add(&(dev->dev_link), &(mgr->dev_list));
472 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
478 void v3_print_dev_mgr(struct v3_vm_info * vm) {
479 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
480 struct vm_device * dev;
482 V3_Print("%d devices registered with manager\n", mgr->num_devs);
484 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
485 V3_Print("Device: %s\n", dev->name);
494 struct blk_frontend {
495 int (*connect)(struct v3_vm_info * vm,
496 void * frontend_data,
497 struct v3_dev_blk_ops * ops,
502 struct list_head blk_node;
509 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
511 int (*connect)(struct v3_vm_info * vm,
512 void * frontend_data,
513 struct v3_dev_blk_ops * ops,
518 struct blk_frontend * frontend = NULL;
520 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
521 memset(frontend, 0, sizeof(struct blk_frontend));
523 frontend->connect = connect;
524 frontend->priv_data = priv_data;
526 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
527 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
532 int v3_dev_connect_blk(struct v3_vm_info * vm,
533 char * frontend_name,
534 struct v3_dev_blk_ops * ops,
536 void * private_data) {
538 struct blk_frontend * frontend = NULL;
540 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
541 (addr_t)frontend_name);
543 if (frontend == NULL) {
544 PrintError("Could not find frontend blk device %s\n", frontend_name);
548 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
549 PrintError("Error connecting to block frontend %s\n", frontend_name);
558 struct net_frontend {
559 int (*connect)(struct v3_vm_info * vm,
560 void * frontend_data,
561 struct v3_dev_net_ops * ops,
566 struct list_head net_node;
572 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
574 int (*connect)(struct v3_vm_info * vm,
575 void * frontend_data,
576 struct v3_dev_net_ops * ops,
578 void * private_data),
581 struct net_frontend * frontend = NULL;
583 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
584 memset(frontend, 0, sizeof(struct net_frontend));
586 frontend->connect = connect;
587 frontend->priv_data = priv_data;
589 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
590 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
596 int v3_dev_connect_net(struct v3_vm_info * vm,
597 char * frontend_name,
598 struct v3_dev_net_ops * ops,
602 struct net_frontend * frontend = NULL;
604 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
605 (addr_t)frontend_name);
607 if (frontend == NULL) {
608 PrintError("Could not find frontend net device %s\n", frontend_name);
612 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
613 PrintError("Error connecting to net frontend %s\n", frontend_name);
621 struct cons_frontend {
622 int (*connect)(struct v3_vm_info * vm,
623 void * frontend_data,
624 struct v3_dev_console_ops * ops,
629 struct list_head cons_node;
634 int v3_dev_add_console_frontend(struct v3_vm_info * vm,
636 int (*connect)(struct v3_vm_info * vm,
637 void * frontend_data,
638 struct v3_dev_console_ops * ops,
640 void * private_data),
643 struct cons_frontend * frontend = NULL;
645 frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
646 memset(frontend, 0, sizeof(struct cons_frontend));
648 frontend->connect = connect;
649 frontend->priv_data = priv_data;
651 list_add(&(frontend->cons_node), &(vm->dev_mgr.cons_list));
652 v3_htable_insert(vm->dev_mgr.cons_table, (addr_t)(name), (addr_t)frontend);
658 int v3_dev_connect_console(struct v3_vm_info * vm,
659 char * frontend_name,
660 struct v3_dev_console_ops * ops,
664 struct cons_frontend * frontend = NULL;
666 frontend = (struct cons_frontend *)v3_htable_search(vm->dev_mgr.cons_table,
667 (addr_t)frontend_name);
669 if (frontend == NULL) {
670 PrintError("Could not find frontend console device %s\n", frontend_name);
674 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
675 PrintError("Error connecting to console frontend %s\n", frontend_name);
682 struct char_frontend {
683 int (*connect)(struct v3_vm_info * vm,
684 void * frontend_data,
685 struct v3_dev_char_ops * ops,
688 void ** push_fn_arg);
691 struct list_head char_node;
696 int v3_dev_add_char_frontend(struct v3_vm_info * vm,
698 int (*connect)(struct v3_vm_info * vm,
699 void * frontend_data,
700 struct v3_dev_char_ops * ops,
703 void ** push_fn_arg),
706 struct char_frontend * frontend = NULL;
708 frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
709 memset(frontend, 0, sizeof(struct char_frontend));
711 frontend->connect = connect;
712 frontend->priv_data = priv_data;
714 list_add(&(frontend->char_node), &(vm->dev_mgr.char_list));
715 v3_htable_insert(vm->dev_mgr.char_table, (addr_t)(name), (addr_t)frontend);
721 int v3_dev_connect_char(struct v3_vm_info * vm,
722 char * frontend_name,
723 struct v3_dev_char_ops * ops,
728 struct char_frontend * frontend = NULL;
730 frontend = (struct char_frontend *)v3_htable_search(vm->dev_mgr.char_table,
731 (addr_t)frontend_name);
733 if (frontend == NULL) {
734 PrintError("Could not find frontend char device %s\n", frontend_name);
738 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
739 PrintError("Error connecting to char frontend %s\n", frontend_name);
748 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
749 struct char_frontend * chr = NULL;
750 struct char_frontend * tmp_chr = NULL;
751 struct cons_frontend * cons = NULL;
752 struct cons_frontend * tmp_cons = NULL;
753 struct net_frontend * net = NULL;
754 struct net_frontend * tmp_net = NULL;
755 struct blk_frontend * blk = NULL;
756 struct blk_frontend * tmp_blk = NULL;
760 list_for_each_entry_safe(chr, tmp_chr, &(mgr->char_list), char_node) {
761 list_del(&(chr->char_node));
765 list_for_each_entry_safe(cons, tmp_cons, &(mgr->cons_list), cons_node) {
766 list_del(&(cons->cons_node));
770 list_for_each_entry_safe(net, tmp_net, &(mgr->net_list), net_node) {
771 list_del(&(net->net_node));
775 list_for_each_entry_safe(blk, tmp_blk, &(mgr->blk_list), blk_node) {
776 list_del(&(blk->blk_node));
780 v3_free_htable(mgr->blk_table, 0, 0);
781 v3_free_htable(mgr->net_table, 0, 0);
782 v3_free_htable(mgr->char_table, 0, 0);
783 v3_free_htable(mgr->cons_table, 0, 0);