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_dev_mgr_deinit(struct v3_vm_info * vm) {
112 struct vm_device * dev;
113 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
114 struct vm_device * tmp;
116 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
117 v3_remove_device(dev);
120 v3_free_htable(mgr->blk_table, 0, 0);
121 v3_free_htable(mgr->net_table, 0, 0);
122 v3_free_htable(mgr->char_table, 0, 0);
123 v3_free_htable(mgr->cons_table, 0, 0);
125 v3_free_htable(mgr->dev_table, 0, 0);
132 int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_t * cfg) {
133 int (*dev_init)(struct v3_vm_info * vm, void * cfg_data);
135 dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
137 if (dev_init == NULL) {
138 PrintError("Could not find device %s in master device table\n", dev_name);
143 if (dev_init(vm, cfg) == -1) {
144 PrintError("Could not initialize Device %s\n", dev_name);
154 struct vm_device * v3_find_dev(struct v3_vm_info * vm, const char * dev_name) {
155 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
161 return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
165 /****************************************************************/
166 /* The remaining functions are called by the devices themselves */
167 /****************************************************************/
172 struct list_head node;
177 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
178 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
179 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data)) {
180 struct dev_io_hook * io_hook = NULL;
183 ret = v3_hook_io_port(dev->vm, port,
184 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))read,
185 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))write,
186 (void *)dev->private_data);
192 io_hook = V3_Malloc(sizeof(struct dev_io_hook));
194 if (io_hook == NULL) {
195 PrintError("Could not allocate io hook dev state\n");
199 io_hook->port = port;
200 list_add(&(io_hook->node), &(dev->io_hooks));
206 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
207 struct dev_io_hook * io_hook = NULL;
208 struct dev_io_hook * tmp;
210 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
211 if (io_hook->port == port) {
213 list_del(&(io_hook->node));
216 return v3_unhook_io_port(dev->vm, port);
225 int v3_remove_device(struct vm_device * dev) {
226 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
227 struct dev_io_hook * io_hook = NULL;
228 struct dev_io_hook * tmp;
230 list_for_each_entry_safe(io_hook, tmp, &(dev->io_hooks), node) {
231 v3_unhook_io_port(dev->vm, io_hook->port);
232 list_del(&(io_hook->node));
236 if (dev->ops->free) {
237 dev->ops->free(dev->private_data);
239 PrintError("Error: %s free() not implemented\n", dev->name);
242 list_del(&(dev->dev_link));
252 struct vm_device * v3_add_device(struct v3_vm_info * vm,
254 struct v3_device_ops * ops,
255 void * private_data) {
256 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
257 struct vm_device * dev = NULL;
259 dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
265 INIT_LIST_HEAD(&(dev->io_hooks));
267 strncpy(dev->name, name, 32);
269 dev->private_data = private_data;
273 list_add(&(dev->dev_link), &(mgr->dev_list));
276 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
282 void v3_print_dev_mgr(struct v3_vm_info * vm) {
283 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
284 struct vm_device * dev;
286 V3_Print("%d devices registered with manager\n", mgr->num_devs);
288 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
289 V3_Print("Device: %s\n", dev->name);
298 struct blk_frontend {
299 int (*connect)(struct v3_vm_info * vm,
300 void * frontend_data,
301 struct v3_dev_blk_ops * ops,
306 struct list_head blk_node;
313 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
315 int (*connect)(struct v3_vm_info * vm,
316 void * frontend_data,
317 struct v3_dev_blk_ops * ops,
322 struct blk_frontend * frontend = NULL;
324 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
325 memset(frontend, 0, sizeof(struct blk_frontend));
327 frontend->connect = connect;
328 frontend->priv_data = priv_data;
330 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
331 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
336 int v3_dev_connect_blk(struct v3_vm_info * vm,
337 char * frontend_name,
338 struct v3_dev_blk_ops * ops,
340 void * private_data) {
342 struct blk_frontend * frontend = NULL;
344 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
345 (addr_t)frontend_name);
347 if (frontend == NULL) {
348 PrintError("Could not find frontend blk device %s\n", frontend_name);
352 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
353 PrintError("Error connecting to block frontend %s\n", frontend_name);
362 struct net_frontend {
363 int (*connect)(struct v3_vm_info * vm,
364 void * frontend_data,
365 struct v3_dev_net_ops * ops,
370 struct list_head net_node;
376 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
378 int (*connect)(struct v3_vm_info * vm,
379 void * frontend_data,
380 struct v3_dev_net_ops * ops,
382 void * private_data),
385 struct net_frontend * frontend = NULL;
387 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
388 memset(frontend, 0, sizeof(struct net_frontend));
390 frontend->connect = connect;
391 frontend->priv_data = priv_data;
393 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
394 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
400 int v3_dev_connect_net(struct v3_vm_info * vm,
401 char * frontend_name,
402 struct v3_dev_net_ops * ops,
406 struct net_frontend * frontend = NULL;
408 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
409 (addr_t)frontend_name);
411 if (frontend == NULL) {
412 PrintError("Could not find frontend net device %s\n", frontend_name);
416 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
417 PrintError("Error connecting to net frontend %s\n", frontend_name);
425 struct cons_frontend {
426 int (*connect)(struct v3_vm_info * vm,
427 void * frontend_data,
428 struct v3_dev_console_ops * ops,
433 struct list_head cons_node;
438 int v3_dev_add_console_frontend(struct v3_vm_info * vm,
440 int (*connect)(struct v3_vm_info * vm,
441 void * frontend_data,
442 struct v3_dev_console_ops * ops,
444 void * private_data),
447 struct cons_frontend * frontend = NULL;
449 frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
450 memset(frontend, 0, sizeof(struct cons_frontend));
452 frontend->connect = connect;
453 frontend->priv_data = priv_data;
455 list_add(&(frontend->cons_node), &(vm->dev_mgr.cons_list));
456 v3_htable_insert(vm->dev_mgr.cons_table, (addr_t)(name), (addr_t)frontend);
462 int v3_dev_connect_console(struct v3_vm_info * vm,
463 char * frontend_name,
464 struct v3_dev_console_ops * ops,
468 struct cons_frontend * frontend = NULL;
470 frontend = (struct cons_frontend *)v3_htable_search(vm->dev_mgr.cons_table,
471 (addr_t)frontend_name);
473 if (frontend == NULL) {
474 PrintError("Could not find frontend console device %s\n", frontend_name);
478 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
479 PrintError("Error connecting to console frontend %s\n", frontend_name);
486 struct char_frontend {
487 int (*connect)(struct v3_vm_info * vm,
488 void * frontend_data,
489 struct v3_dev_char_ops * ops,
492 void ** push_fn_arg);
495 struct list_head char_node;
500 int v3_dev_add_char_frontend(struct v3_vm_info * vm,
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 char_frontend * frontend = NULL;
512 frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
513 memset(frontend, 0, sizeof(struct char_frontend));
515 frontend->connect = connect;
516 frontend->priv_data = priv_data;
518 list_add(&(frontend->char_node), &(vm->dev_mgr.char_list));
519 v3_htable_insert(vm->dev_mgr.char_table, (addr_t)(name), (addr_t)frontend);
525 int v3_dev_connect_char(struct v3_vm_info * vm,
526 char * frontend_name,
527 struct v3_dev_char_ops * ops,
532 struct char_frontend * frontend = NULL;
534 frontend = (struct char_frontend *)v3_htable_search(vm->dev_mgr.char_table,
535 (addr_t)frontend_name);
537 if (frontend == NULL) {
538 PrintError("Could not find frontend char device %s\n", frontend_name);
542 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
543 PrintError("Error connecting to char frontend %s\n", frontend_name);