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, Peter Dinda <pdinda@northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Peter Dinda <pdinda@northwestern.edu>
15 * Contributor: 2008, Jack Lange <jarusl@cs.northwestern.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <devices/generic.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_types.h>
25 #include <palacios/vmm_list.h>
31 #define PrintDebug(fmt, args...)
36 #define MEM_HOOKS 0 // not yet implmented in device model
37 #define IRQ_HOOKS 0 // not yet implemented in device model
40 struct generic_internal {
41 struct list_head port_list;
42 uint_t num_port_ranges;
43 struct list_head mem_list;
44 uint_t num_mem_ranges;
45 struct list_head irq_list;
46 uint_t num_irq_ranges;
54 struct list_head range_link;
61 struct list_head range_link;
68 struct list_head range_link;
73 static int generic_reset_device(struct vm_device * dev) {
74 PrintDebug("generic: reset device\n");
82 static int generic_start_device(struct vm_device * dev) {
83 PrintDebug("generic: start device\n");
88 static int generic_stop_device(struct vm_device * dev) {
89 PrintDebug("generic: stop device\n");
96 static int generic_write_port_passthrough(ushort_t port,
99 struct vm_device * dev) {
102 PrintDebug("generic: writing 0x");
104 for (i = 0; i < length; i++) {
105 PrintDebug("%x", ((uchar_t*)src)[i]);
108 PrintDebug(" to port 0x%x ... ", port);
113 v3_outb(port,((uchar_t*)src)[0]);
116 v3_outw(port,((ushort_t*)src)[0]);
119 v3_outdw(port,((uint_t*)src)[0]);
122 for (i = 0; i < length; i++) {
123 v3_outb(port, ((uchar_t*)src)[i]);
128 PrintDebug(" done\n");
133 static int generic_read_port_passthrough(ushort_t port,
136 struct vm_device * dev) {
139 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
144 ((uchar_t*)src)[0] = v3_inb(port);
147 ((ushort_t*)src)[0] = v3_inw(port);
150 ((uint_t*)src)[0] = v3_indw(port);
153 for (i = 0; i < length; i++) {
154 ((uchar_t*)src)[i] = v3_inb(port);
158 PrintDebug(" done ... read 0x");
160 for (i = 0; i < length; i++) {
161 PrintDebug("%x", ((uchar_t*)src)[i]);
169 static int generic_write_port_ignore(ushort_t port,
172 struct vm_device * dev) {
175 PrintDebug("generic: writing 0x");
177 for (i = 0; i < length; i++) {
178 PrintDebug("%x", ((uchar_t*)src)[i]);
181 PrintDebug(" to port 0x%x ... ignored\n", port);
186 static int generic_read_port_ignore(ushort_t port,
189 struct vm_device * dev) {
191 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
193 memset((char*)src, 0, length);
194 PrintDebug(" ignored (return zeroed buffer)\n");
201 static int generic_interrupt(uint_t irq, struct vm_device * dev) {
202 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
204 v3_raise_irq(dev->vm, irq);
210 static int generic_init_device(struct vm_device * dev) {
211 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
213 PrintDebug("generic: init_device\n");
214 generic_reset_device(dev);
217 if (PORT_HOOKS) { // This is a runtime conditional on a #define
218 struct port_range * tmp = NULL;
220 list_for_each_entry(tmp, &(state->port_list), range_link) {
223 PrintDebug("generic: hooking ports 0x%x to 0x%x as %s\n",
224 tmp->start, tmp->end,
225 (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
227 for (i = tmp->start; i <= tmp->end; i++) {
228 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) {
230 if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
231 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
234 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) {
236 if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) {
237 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
244 PrintDebug("generic: hooking ports not supported\n");
249 if (MEM_HOOKS) { // This is a runtime conditional on a #define
250 struct mem_range * tmp;
252 list_for_each_entry(tmp, &(state->mem_list), range_link) {
254 PrintDebug("generic: hooking addresses 0x%p to 0x%p\n",
255 tmp->start, tmp->end);
258 if (v3_dev_hook_mem(dev, tmp->start, tmp->end)) {
259 PrintDebug("generic: Can't hook addresses 0x%p to 0x%p (already hooked?)\n",
260 tmp->start, tmp->end);
264 PrintDebug("generic: hooking addresses not supported\n");
270 if (IRQ_HOOKS) { // This is a runtime conditional on a #define
271 struct irq_range * tmp;
273 list_for_each_entry(tmp, &(state->irq_list), range_link) {
276 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",
277 tmp->start, tmp->end);
279 for (i = tmp->start; i <= tmp->end; i++) {
280 if (v3_dev_hook_irq(dev, i, &generic_interrupt)) {
281 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", i);
287 PrintDebug("generic: hooking irqs not supported\n");
295 static int generic_deinit_device(struct vm_device * dev) {
296 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
299 PrintDebug("generic: deinit_device\n");
302 if (IRQ_HOOKS) { // This is a runtime conditional on a #define
303 struct irq_range * tmp;
304 struct irq_range * cur;
306 list_for_each_entry_safe(cur, tmp, &(state->irq_list), range_link) {
309 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n",
310 cur->start, cur->end);
313 for (i = cur->start; i <= cur->end; i++) {
314 if (v3_dev_unhook_irq(dev, i)) {
315 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n", i);
319 list_del(&(cur->range_link));
320 state->num_irq_ranges--;
324 PrintDebug("generic: unhooking irqs not supported\n");
329 struct mem_range * tmp;
330 struct mem_range * cur;
332 list_for_each_entry_safe(cur, tmp, &(state->mem_list), range_link) {
334 PrintDebug("generic: unhooking addresses 0x%p to 0x%p\n",
335 cur->start, cur->end);
337 if (v3_dev_unhook_mem(dev, cur->start, cur->end)) {
338 PrintDebug("generic: Can't unhook addresses 0x%p to 0x%p (already unhooked?)\n",
339 cur->start, cur->end);
342 list_del(&(cur->range_link));
343 state->num_mem_ranges--;
347 PrintDebug("generic: unhooking addresses not supported\n");
352 struct port_range * tmp;
353 struct port_range * cur;
355 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
358 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
359 cur->start, cur->end);
361 for (i = cur->start; i <= cur->end; i++) {
362 if (v3_dev_unhook_io(dev, i)) {
363 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
367 list_del(&(cur->range_link));
368 state->num_port_ranges--;
372 PrintDebug("generic: unhooking ports not supported\n");
377 generic_reset_device(dev);
385 static struct vm_device_ops dev_ops = {
386 .init = generic_init_device,
387 .deinit = generic_deinit_device,
388 .reset = generic_reset_device,
389 .start = generic_start_device,
390 .stop = generic_stop_device,
396 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
399 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
401 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
402 range->start = start;
407 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
408 range->start, range->end,
409 (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
411 list_add(&(range->range_link), &(state->port_list));
412 state->num_port_ranges++;
414 PrintDebug("generic: hooking IO ports not supported\n");
421 int v3_generic_add_mem_range(struct vm_device * dev, void * start, void * end, uint_t type) {
424 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
426 struct mem_range * range = (struct mem_range *)V3_Malloc(sizeof(struct mem_range));
427 range->start = start;
431 list_add(&(range->range_link), &(state->port_list));
432 state->num_mem_ranges++;
434 PrintDebug("generic: hooking memory not supported\n");
442 int v3_generic_add_irq_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
445 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
447 struct irq_range * range = (struct irq_range *)V3_Malloc(sizeof(struct irq_range));
448 range->start = start;
452 list_add(&(range->range_link), &(state->port_list));
453 state->num_irq_ranges++;
455 PrintDebug("generic: hooking IRQs not supported\n");
464 struct vm_device * v3_create_generic() {
465 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
467 generic_state->num_port_ranges = 0;
468 generic_state->num_mem_ranges = 0;
469 generic_state->num_irq_ranges = 0;
471 INIT_LIST_HEAD(&(generic_state->port_list));
472 INIT_LIST_HEAD(&(generic_state->mem_list));
473 INIT_LIST_HEAD(&(generic_state->irq_list));
475 struct vm_device * device = v3_create_device("GENERIC", &dev_ops, generic_state);