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 int generic_reset_device(struct vm_device * dev)
75 PrintDebug("generic: reset device\n");
85 int generic_start_device(struct vm_device * dev)
87 PrintDebug("generic: start device\n");
92 int generic_stop_device(struct vm_device * dev)
94 PrintDebug("generic: stop device\n");
101 int generic_write_port_passthrough(ushort_t port,
104 struct vm_device * dev)
108 PrintDebug("generic: writing 0x");
110 for (i = 0; i < length; i++) {
111 PrintDebug("%x", ((uchar_t*)src)[i]);
114 PrintDebug(" to port 0x%x ... ", port);
119 v3_outb(port,((uchar_t*)src)[0]);
122 v3_outw(port,((ushort_t*)src)[0]);
125 v3_outdw(port,((uint_t*)src)[0]);
128 for (i = 0; i < length; i++) {
129 v3_outb(port, ((uchar_t*)src)[i]);
134 PrintDebug(" done\n");
139 int generic_read_port_passthrough(ushort_t port,
142 struct vm_device * dev)
146 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
151 ((uchar_t*)src)[0] = v3_inb(port);
154 ((ushort_t*)src)[0] = v3_inw(port);
157 ((uint_t*)src)[0] = v3_indw(port);
160 for (i = 0; i < length; i++) {
161 ((uchar_t*)src)[i] = v3_inb(port);
165 PrintDebug(" done ... read 0x");
167 for (i = 0; i < length; i++) {
168 PrintDebug("%x", ((uchar_t*)src)[i]);
176 int generic_write_port_ignore(ushort_t port,
179 struct vm_device * dev)
183 PrintDebug("generic: writing 0x");
185 for (i = 0; i < length; i++) {
186 PrintDebug("%x", ((uchar_t*)src)[i]);
189 PrintDebug(" to port 0x%x ... ignored\n", port);
194 int generic_read_port_ignore(ushort_t port,
197 struct vm_device * dev)
200 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
202 memset((char*)src, 0, length);
203 PrintDebug(" ignored (return zeroed buffer)\n");
210 int generic_interrupt(uint_t irq, struct vm_device * dev) {
211 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
213 dev->vm->vm_ops.raise_irq(dev->vm, irq);
219 int generic_init_device(struct vm_device * dev) {
220 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
222 PrintDebug("generic: init_device\n");
223 generic_reset_device(dev);
226 if (PORT_HOOKS) { // This is a runtime conditional on a #define
227 struct port_range * tmp = NULL;
229 list_for_each_entry(tmp, &(state->port_list), range_link) {
232 PrintDebug("generic: hooking ports 0x%x to 0x%x as %x\n",
233 tmp->start, tmp->end,
234 (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
236 for (i = tmp->start; i <= tmp->end; i++) {
237 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) {
239 if (dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
240 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
243 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) {
245 if (dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) {
246 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
253 PrintDebug("generic: hooking ports not supported\n");
258 if (MEM_HOOKS) { // This is a runtime conditional on a #define
259 struct mem_range * tmp;
261 list_for_each_entry(tmp, &(state->mem_list), range_link) {
263 PrintDebug("generic: hooking addresses 0x%x to 0x%x\n",
264 tmp->start, tmp->end);
267 if (dev_hook_mem(dev, tmp->start, tmp->end)) {
268 PrintDebug("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
269 tmp->start, tmp->end);
273 PrintDebug("generic: hooking addresses not supported\n");
279 if (IRQ_HOOKS) { // This is a runtime conditional on a #define
280 struct irq_range * tmp;
282 list_for_each_entry(tmp, &(state->irq_list), range_link) {
285 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",
286 tmp->start, tmp->end);
288 for (i = tmp->start; i <= tmp->end; i++) {
289 if (dev_hook_irq(dev, i, &generic_interrupt)) {
290 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", i);
296 PrintDebug("generic: hooking irqs not supported\n");
304 int generic_deinit_device(struct vm_device * dev) {
305 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
308 PrintDebug("generic: deinit_device\n");
311 if (IRQ_HOOKS) { // This is a runtime conditional on a #define
312 struct irq_range * tmp;
313 struct irq_range * cur;
315 list_for_each_entry_safe(cur, tmp, &(state->irq_list), range_link) {
318 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n",
319 cur->start, cur->end);
322 for (i = cur->start; i <= cur->end; i++) {
323 if (dev_unhook_irq(dev, i)) {
324 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n", i);
328 list_del(&(cur->range_link));
329 state->num_irq_ranges--;
333 PrintDebug("generic: unhooking irqs not supported\n");
338 struct mem_range * tmp;
339 struct mem_range * cur;
341 list_for_each_entry_safe(cur, tmp, &(state->mem_list), range_link) {
343 PrintDebug("generic: unhooking addresses 0x%x to 0x%x\n",
344 cur->start, cur->end);
346 if (dev_unhook_mem(dev, cur->start, cur->end)) {
347 PrintDebug("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
348 cur->start, cur->end);
351 list_del(&(cur->range_link));
352 state->num_mem_ranges--;
356 PrintDebug("generic: unhooking addresses not supported\n");
361 struct port_range * tmp;
362 struct port_range * cur;
364 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
367 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
368 cur->start, cur->end);
370 for (i = cur->start; i <= cur->end; i++) {
371 if (dev_unhook_io(dev, i)) {
372 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
376 list_del(&(cur->range_link));
377 state->num_port_ranges--;
381 PrintDebug("generic: unhooking ports not supported\n");
386 generic_reset_device(dev);
394 static struct vm_device_ops dev_ops = {
395 .init = generic_init_device,
396 .deinit = generic_deinit_device,
397 .reset = generic_reset_device,
398 .start = generic_start_device,
399 .stop = generic_stop_device,
405 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
408 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
410 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
411 range->start = start;
416 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %x\n",
417 range->start, range->end,
418 (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
420 list_add(&(range->range_link), &(state->port_list));
421 state->num_port_ranges++;
423 PrintDebug("generic: hooking IO ports not supported\n");
430 int v3_generic_add_mem_range(struct vm_device * dev, void * start, void * end, uint_t type) {
433 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
435 struct mem_range * range = (struct mem_range *)V3_Malloc(sizeof(struct mem_range));
436 range->start = start;
440 list_add(&(range->range_link), &(state->port_list));
441 state->num_mem_ranges++;
443 PrintDebug("generic: hooking memory not supported\n");
451 int v3_generic_add_irq_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
454 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
456 struct irq_range * range = (struct irq_range *)V3_Malloc(sizeof(struct irq_range));
457 range->start = start;
461 list_add(&(range->range_link), &(state->port_list));
462 state->num_irq_ranges++;
464 PrintDebug("generic: hooking IRQs not supported\n");
473 struct vm_device * create_generic() {
474 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
476 generic_state->num_port_ranges = 0;
477 generic_state->num_mem_ranges = 0;
478 generic_state->num_irq_ranges = 0;
480 INIT_LIST_HEAD(&(generic_state->port_list));
481 INIT_LIST_HEAD(&(generic_state->mem_list));
482 INIT_LIST_HEAD(&(generic_state->irq_list));
484 struct vm_device * device = create_device("GENERIC", &dev_ops, generic_state);