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->console_list));
101 mgr->blk_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
102 mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
103 mgr->console_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
109 int v3_dev_mgr_deinit(struct v3_vm_info * vm) {
110 struct vm_device * dev;
111 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
112 struct vm_device * tmp;
114 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
115 v3_detach_device(dev);
120 /* TODO: Clear hash tables */
126 int v3_init_core_dev_mgr(struct v3_vm_info * vm) {
127 struct v3_core_dev_mgr * mgr = &(vm->core_dev_mgr);
129 INIT_LIST_HEAD(&(mgr->dev_list));
130 mgr->dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
135 int v3_core_dev_mgr_deinit(struct v3_vm_info * vm) {
136 struct vm_device * dev;
137 struct v3_core_dev_mgr * mgr = &(vm->core_dev_mgr);
138 struct vm_device * tmp;
140 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
141 v3_detach_device(dev);
145 // TODO: Clear hash tables
151 int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_t * cfg) {
152 int (*dev_init)(struct v3_vm_info * vm, void * cfg_data);
154 dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
156 if (dev_init == NULL) {
157 PrintError("Could not find device %s in master device table\n", dev_name);
162 if (dev_init(vm, cfg) == -1) {
163 PrintError("Could not initialize Device %s\n", dev_name);
171 void v3_free_device(struct vm_device * dev) {
177 struct vm_device * v3_find_dev(struct v3_vm_info * vm, const char * dev_name) {
178 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
184 return (struct vm_device *)v3_htable_search(mgr->dev_table, (addr_t)dev_name);
188 /****************************************************************/
189 /* The remaining functions are called by the devices themselves */
190 /****************************************************************/
193 int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
194 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, struct vm_device * dev),
195 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, struct vm_device * dev)) {
196 return v3_hook_io_port(dev->vm, port,
197 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))read,
198 (int (*)(struct guest_info * core, ushort_t, void *, uint_t, void *))write,
203 int v3_dev_unhook_io(struct vm_device * dev, uint16_t port) {
204 return v3_unhook_io_port(dev->vm, port);
209 int v3_detach_device(struct vm_device * dev) {
210 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
214 list_del(&(dev->dev_link));
223 struct vm_device * v3_allocate_device(char * name,
224 struct v3_device_ops * ops,
225 void * private_data) {
226 struct vm_device * dev = NULL;
228 dev = (struct vm_device*)V3_Malloc(sizeof(struct vm_device));
230 strncpy(dev->name, name, 32);
232 dev->private_data = private_data;
240 int v3_attach_device(struct v3_vm_info * vm, struct vm_device * dev ) {
241 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
245 list_add(&(dev->dev_link), &(mgr->dev_list));
249 v3_htable_insert(mgr->dev_table, (addr_t)(dev->name), (addr_t)dev);
256 void v3_print_dev_mgr(struct v3_vm_info * vm) {
257 struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
258 struct vm_device * dev;
260 V3_Print("%d devices registered with manager\n", mgr->num_devs);
262 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
263 V3_Print("Device: %s\n", dev->name);
272 struct blk_frontend {
273 int (*connect)(struct v3_vm_info * vm,
274 void * frontend_data,
275 struct v3_dev_blk_ops * ops,
280 struct list_head blk_node;
287 int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
289 int (*connect)(struct v3_vm_info * vm,
290 void * frontend_data,
291 struct v3_dev_blk_ops * ops,
296 struct blk_frontend * frontend = NULL;
298 frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
299 memset(frontend, 0, sizeof(struct blk_frontend));
301 frontend->connect = connect;
302 frontend->priv_data = priv_data;
304 list_add(&(frontend->blk_node), &(vm->dev_mgr.blk_list));
305 v3_htable_insert(vm->dev_mgr.blk_table, (addr_t)(name), (addr_t)frontend);
310 int v3_dev_connect_blk(struct v3_vm_info * vm,
311 char * frontend_name,
312 struct v3_dev_blk_ops * ops,
314 void * private_data) {
316 struct blk_frontend * frontend = NULL;
318 frontend = (struct blk_frontend *)v3_htable_search(vm->dev_mgr.blk_table,
319 (addr_t)frontend_name);
321 if (frontend == NULL) {
322 PrintError("Could not find frontend blk device %s\n", frontend_name);
326 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
327 PrintError("Error connecting to block frontend %s\n", frontend_name);
336 struct net_frontend {
337 int (*connect)(struct v3_vm_info * vm,
338 void * frontend_data,
339 struct v3_dev_net_ops * ops,
344 struct list_head net_node;
350 int v3_dev_add_net_frontend(struct v3_vm_info * vm,
352 int (*connect)(struct v3_vm_info * vm,
353 void * frontend_data,
354 struct v3_dev_net_ops * ops,
356 void * private_data),
359 struct net_frontend * frontend = NULL;
361 frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
362 memset(frontend, 0, sizeof(struct net_frontend));
364 frontend->connect = connect;
365 frontend->priv_data = priv_data;
367 list_add(&(frontend->net_node), &(vm->dev_mgr.net_list));
368 v3_htable_insert(vm->dev_mgr.net_table, (addr_t)(name), (addr_t)frontend);
374 int v3_dev_connect_net(struct v3_vm_info * vm,
375 char * frontend_name,
376 struct v3_dev_net_ops * ops,
380 struct net_frontend * frontend = NULL;
382 frontend = (struct net_frontend *)v3_htable_search(vm->dev_mgr.net_table,
383 (addr_t)frontend_name);
385 if (frontend == NULL) {
386 PrintError("Could not find frontend net device %s\n", frontend_name);
390 if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
391 PrintError("Error connecting to net frontend %s\n", frontend_name);