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/vm_dev.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vm_guest.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_decoder.h>
29 #define PrintDebug(fmt, args...)
34 int dev_mgr_init(struct guest_info * info) {
35 struct vmm_dev_mgr * mgr = &(info->dev_mgr);
36 INIT_LIST_HEAD(&(mgr->dev_list));
39 INIT_LIST_HEAD(&(mgr->io_hooks));
40 mgr->num_io_hooks = 0;
46 int dev_mgr_deinit(struct guest_info * info) {
47 struct vm_device * dev;
48 struct vmm_dev_mgr * mgr = &(info->dev_mgr);
49 struct vm_device * tmp;
51 list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
52 v3_unattach_device(dev);
62 static int dev_mgr_add_device(struct vmm_dev_mgr * mgr, struct vm_device * dev) {
63 list_add(&(dev->dev_link), &(mgr->dev_list));
69 static int dev_mgr_remove_device(struct vmm_dev_mgr * mgr, struct vm_device * dev) {
70 list_del(&(dev->dev_link));
79 int dev_mgr_add_io_hook(struct vmm_dev_mgr * mgr, struct dev_io_hook * hook) {
80 list_add(&(hook->mgr_list), &(mgr->io_hooks));
86 int dev_mgr_remove_io_hook(struct vmm_dev_mgr * mgr, struct dev_io_hook * hook) {
87 list_del(&(hook->mgr_list));
94 int dev_add_io_hook(struct vm_device * dev, struct dev_io_hook * hook) {
95 list_add(&(hook->dev_list), &(dev->io_hooks));
101 int dev_remove_io_hook(struct vm_device * dev, struct dev_io_hook * hook) {
102 list_del(&(hook->dev_list));
112 struct dev_io_hook * dev_mgr_find_io_hook(struct vmm_dev_mgr * mgr, ushort_t port) {
113 struct dev_io_hook * tmp = NULL;
115 list_for_each_entry(tmp, &(mgr->io_hooks), mgr_list) {
116 if (tmp->port == port) {
123 struct dev_io_hook * dev_find_io_hook(struct vm_device * dev, ushort_t port) {
124 struct dev_io_hook * tmp = NULL;
126 list_for_each_entry(tmp, &(dev->io_hooks), dev_list) {
127 if (tmp->port == port) {
137 int dev_hook_io(struct vm_device *dev,
139 int (*read)(ushort_t port, void * dst, uint_t length, struct vm_device * dev),
140 int (*write)(ushort_t port, void * src, uint_t length, struct vm_device * dev)) {
142 struct dev_io_hook *hook = (struct dev_io_hook *)V3_Malloc(sizeof(struct dev_io_hook));
149 if (v3_hook_io_port(dev->vm, port,
150 (int (*)(ushort_t, void *, uint_t, void *))read,
151 (int (*)(ushort_t, void *, uint_t, void *))write,
159 dev_mgr_add_io_hook(&(dev->vm->dev_mgr), hook);
160 dev_add_io_hook(dev, hook);
170 int dev_unhook_io(struct vm_device *dev,
173 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
174 struct dev_io_hook * hook = dev_mgr_find_io_hook(mgr, port);
180 dev_mgr_remove_io_hook(mgr, hook);
181 dev_remove_io_hook(dev, hook);
183 return v3_unhook_io_port(dev->vm, port);
187 int v3_attach_device(struct guest_info * vm, struct vm_device * dev) {
188 struct vmm_dev_mgr *mgr= &(vm->dev_mgr);
191 dev_mgr_add_device(mgr, dev);
197 int v3_unattach_device(struct vm_device * dev) {
198 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
200 dev->ops->deinit(dev);
201 dev_mgr_remove_device(mgr, dev);
211 int dev_mgr_hook_mem(struct guest_info *vm,
212 struct vm_device *device,
217 struct dev_mem_hook * hook = (struct dev_mem_hook*)V3_Malloc(sizeof(struct dev_mem_hook));
218 // V3_Malloc(struct dev_mem_hook *, hook,sizeof(struct dev_mem_hook));
224 /* not implemented yet
225 hook_memory(vm->mem_map,
226 guest_physical_address_start,
227 guest_physical_address_end,
234 return -1; // remove when hook_memory works
237 hook->addr_start = start;
238 hook->addr_end = end;
245 int dev_mgr_unhook_mem(struct vm_device *dev,
249 struct vmm_dev_mgr * mgr = &(dev->vm->dev_mgr);
250 struct dev_mem_hook *hook = dev_mgr_find_mem_hook(mgr, start, end);
253 // Very bad - unhooking something that doesn't exist!
258 /* not implemented yet
259 return unhook_mem_port(vm->mem_map,
260 guest_physical_start,
261 guest_physical_end) ;
271 void PrintDebugDevMgr(struct guest_info * info) {
272 struct vmm_dev_mgr * mgr = &(info->dev_mgr);
273 struct vm_device * dev;
274 PrintDebug("%d devices registered with manager\n", mgr->num_devs);
276 list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
278 PrintDebug("next..\n");
285 void PrintDebugDev(struct vm_device * dev) {
287 PrintDebug("Device: %s\n", dev->name);
288 PrintDebugDevIO(dev);
291 void PrintDebugDevMgrIO(struct vmm_dev_mgr * mgr) {
295 void PrintDebugDevIO(struct vm_device * dev) {
296 struct dev_io_hook * hook;
298 PrintDebug("IO Hooks(%d) for Device: %s\n", dev->num_io_hooks, dev->name);
300 list_for_each_entry(hook, &(dev->io_hooks), dev_list) {
301 PrintDebug("\tPort: 0x%x (read=0x%x), (write=0x%x)\n", hook->port, hook->read, hook->write);
308 void PrintDebugDevMgr(struct guest_info * info) {}
309 void PrintDebugDev(struct vm_device * dev) {}
310 void PrintDebugDevMgrIO(struct vmm_dev_mgr * mgr) {}
311 void PrintDebugDevIO(struct vm_device * dev) {}