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 V3_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 V3_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 /****************************************************************/
184 typedef enum {DEV_IO_HOOK, DEV_MSR_HOOK, DEV_CPUID_HOOK, DEV_MEM_HOOK, DEV_HCALL_HOOK} dev_rsrc_type_t;
187 dev_rsrc_type_t type;
190 struct list_head node;
195 static int add_resource(struct vm_device * dev, dev_rsrc_type_t type, uint64_t rsrc_id) {
196 struct dev_rsrc * resource = NULL;
198 resource = V3_Malloc(sizeof(struct dev_rsrc));
200 if (resource == NULL) {
201 PrintError("Error: Could not allocate device resource\n");
205 resource->rsrc = rsrc_id;
206 resource->type = type;
208 list_add(&(resource->node), &(dev->res_hooks));
212 static int free_resource(struct vm_device * dev, dev_rsrc_type_t type, uint64_t rsrc_id) {
213 struct dev_rsrc * resource = NULL;
214 struct dev_rsrc * tmp;
216 list_for_each_entry_safe(resource, tmp, &(dev->res_hooks), node) {
217 if ((resource->type == type) &&
218 (resource->rsrc == rsrc_id)) {
220 list_del(&(resource->node));
231 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
232 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
233 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data)) {
236 ret = v3_hook_io_port(dev->vm, port,
237 (int (*)(struct guest_info * core, uint16_t, void *, uint_t, void *))read,
238 (int (*)(struct guest_info * core, uint16_t, void *, uint_t, void *))write,
239 (void *)dev->private_data);
245 if (add_resource(dev, DEV_IO_HOOK, port) == -1) {
246 v3_unhook_io_port(dev->vm, port);
247 PrintError("Could not allocate io hook dev state\n");
255 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
256 if (free_resource(dev, DEV_IO_HOOK, port) == 0) {
257 return v3_unhook_io_port(dev->vm, port);
264 int v3_dev_hook_msr(struct vm_device * dev, uint32_t msr,
265 int (*read)(struct guest_info * core, uint32_t msr, struct v3_msr * dst, void * priv_data),
266 int (*write)(struct guest_info * core, uint32_t msr, struct v3_msr src, void * priv_data)) {
269 ret = v3_hook_msr(dev->vm, msr, read, write, dev->private_data);
275 if (add_resource(dev, DEV_MSR_HOOK, msr) == -1) {
276 v3_unhook_msr(dev->vm, msr);
283 int v3_dev_unhook_msr(struct vm_device * dev, uint32_t msr) {
284 if (free_resource(dev, DEV_MSR_HOOK, msr) == 0) {
285 return v3_unhook_msr(dev->vm, msr);
294 int v3_remove_device(struct vm_device * dev) {
295 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
296 struct dev_rsrc * resource = NULL;
297 struct dev_rsrc * tmp;
299 list_for_each_entry_safe(resource, tmp, &(dev->res_hooks), node) {
300 if (resource->type == DEV_IO_HOOK) {
301 v3_unhook_io_port(dev->vm, (uint16_t)(resource->rsrc));
302 } else if (resource->type == DEV_MSR_HOOK) {
303 v3_unhook_msr(dev->vm, (uint32_t)(resource->rsrc));
306 list_del(&(resource->node));
310 if (dev->ops->free) {
311 dev->ops->free(dev->private_data);
313 PrintError("Error: %s free() not implemented\n", dev->name);
316 list_del(&(dev->dev_link));
326 struct vm_device * v3_add_device(struct v3_vm_info * vm,
328 struct v3_device_ops * ops,
329 void * private_data) {
330 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
331 struct vm_device * dev = NULL;
333 dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
339 INIT_LIST_HEAD(&(dev->res_hooks));
341 strncpy(dev->name, name, 32);
343 dev->private_data = private_data;
347 list_add(&(dev->dev_link), &(mgr->dev_list));
350 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
356 void v3_print_dev_mgr(struct v3_vm_info * vm) {
357 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
358 struct vm_device * dev;
360 V3_Print("%d devices registered with manager\n", mgr->num_devs);
362 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
363 V3_Print("Device: %s\n", dev->name);
372 struct blk_frontend {
373 int (*connect)(struct v3_vm_info * vm,
374 void * frontend_data,
375 struct v3_dev_blk_ops * ops,
380 struct list_head blk_node;
387 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
389 int (*connect)(struct v3_vm_info * vm,
390 void * frontend_data,
391 struct v3_dev_blk_ops * ops,
396 struct blk_frontend * frontend = NULL;
398 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
399 memset(frontend, 0, sizeof(struct blk_frontend));
401 frontend->connect = connect;
402 frontend->priv_data = priv_data;
404 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
405 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
410 int v3_dev_connect_blk(struct v3_vm_info * vm,
411 char * frontend_name,
412 struct v3_dev_blk_ops * ops,
414 void * private_data) {
416 struct blk_frontend * frontend = NULL;
418 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
419 (addr_t)frontend_name);
421 if (frontend == NULL) {
422 PrintError("Could not find frontend blk device %s\n", frontend_name);
426 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
427 PrintError("Error connecting to block frontend %s\n", frontend_name);
436 struct net_frontend {
437 int (*connect)(struct v3_vm_info * vm,
438 void * frontend_data,
439 struct v3_dev_net_ops * ops,
444 struct list_head net_node;
450 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
452 int (*connect)(struct v3_vm_info * vm,
453 void * frontend_data,
454 struct v3_dev_net_ops * ops,
456 void * private_data),
459 struct net_frontend * frontend = NULL;
461 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
462 memset(frontend, 0, sizeof(struct net_frontend));
464 frontend->connect = connect;
465 frontend->priv_data = priv_data;
467 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
468 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
474 int v3_dev_connect_net(struct v3_vm_info * vm,
475 char * frontend_name,
476 struct v3_dev_net_ops * ops,
480 struct net_frontend * frontend = NULL;
482 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
483 (addr_t)frontend_name);
485 if (frontend == NULL) {
486 PrintError("Could not find frontend net device %s\n", frontend_name);
490 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
491 PrintError("Error connecting to net frontend %s\n", frontend_name);
499 struct cons_frontend {
500 int (*connect)(struct v3_vm_info * vm,
501 void * frontend_data,
502 struct v3_dev_console_ops * ops,
507 struct list_head cons_node;
512 int v3_dev_add_console_frontend(struct v3_vm_info * vm,
514 int (*connect)(struct v3_vm_info * vm,
515 void * frontend_data,
516 struct v3_dev_console_ops * ops,
518 void * private_data),
521 struct cons_frontend * frontend = NULL;
523 frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
524 memset(frontend, 0, sizeof(struct cons_frontend));
526 frontend->connect = connect;
527 frontend->priv_data = priv_data;
529 list_add(&(frontend->cons_node), &(vm->dev_mgr.cons_list));
530 v3_htable_insert(vm->dev_mgr.cons_table, (addr_t)(name), (addr_t)frontend);
536 int v3_dev_connect_console(struct v3_vm_info * vm,
537 char * frontend_name,
538 struct v3_dev_console_ops * ops,
542 struct cons_frontend * frontend = NULL;
544 frontend = (struct cons_frontend *)v3_htable_search(vm->dev_mgr.cons_table,
545 (addr_t)frontend_name);
547 if (frontend == NULL) {
548 PrintError("Could not find frontend console device %s\n", frontend_name);
552 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
553 PrintError("Error connecting to console frontend %s\n", frontend_name);
560 struct char_frontend {
561 int (*connect)(struct v3_vm_info * vm,
562 void * frontend_data,
563 struct v3_dev_char_ops * ops,
566 void ** push_fn_arg);
569 struct list_head char_node;
574 int v3_dev_add_char_frontend(struct v3_vm_info * vm,
576 int (*connect)(struct v3_vm_info * vm,
577 void * frontend_data,
578 struct v3_dev_char_ops * ops,
581 void ** push_fn_arg),
584 struct char_frontend * frontend = NULL;
586 frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
587 memset(frontend, 0, sizeof(struct char_frontend));
589 frontend->connect = connect;
590 frontend->priv_data = priv_data;
592 list_add(&(frontend->char_node), &(vm->dev_mgr.char_list));
593 v3_htable_insert(vm->dev_mgr.char_table, (addr_t)(name), (addr_t)frontend);
599 int v3_dev_connect_char(struct v3_vm_info * vm,
600 char * frontend_name,
601 struct v3_dev_char_ops * ops,
606 struct char_frontend * frontend = NULL;
608 frontend = (struct char_frontend *)v3_htable_search(vm->dev_mgr.char_table,
609 (addr_t)frontend_name);
611 if (frontend == NULL) {
612 PrintError("Could not find frontend char device %s\n", frontend_name);
616 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
617 PrintError("Error connecting to char frontend %s\n", frontend_name);
626 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
627 struct char_frontend * chr = NULL;
628 struct char_frontend * tmp_chr = NULL;
629 struct cons_frontend * cons = NULL;
630 struct cons_frontend * tmp_cons = NULL;
631 struct net_frontend * net = NULL;
632 struct net_frontend * tmp_net = NULL;
633 struct blk_frontend * blk = NULL;
634 struct blk_frontend * tmp_blk = NULL;
638 list_for_each_entry_safe(chr, tmp_chr, &(mgr->char_list), char_node) {
639 list_del(&(chr->char_node));
643 list_for_each_entry_safe(cons, tmp_cons, &(mgr->cons_list), cons_node) {
644 list_del(&(cons->cons_node));
648 list_for_each_entry_safe(net, tmp_net, &(mgr->net_list), net_node) {
649 list_del(&(net->net_node));
653 list_for_each_entry_safe(blk, tmp_blk, &(mgr->blk_list), blk_node) {
654 list_del(&(blk->blk_node));
658 v3_free_htable(mgr->blk_table, 0, 0);
659 v3_free_htable(mgr->net_table, 0, 0);
660 v3_free_htable(mgr->char_table, 0, 0);
661 v3_free_htable(mgr->cons_table, 0, 0);