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...)
35 struct generic_internal {
36 struct list_head port_list;
37 uint_t num_port_ranges;
46 struct list_head range_link;
52 static int generic_reset_device(struct vm_device * dev) {
53 PrintDebug("generic: reset device\n");
61 static int generic_start_device(struct vm_device * dev) {
62 PrintDebug("generic: start device\n");
67 static int generic_stop_device(struct vm_device * dev) {
68 PrintDebug("generic: stop device\n");
75 static int generic_write_port_passthrough(uint16_t port, void * src,
76 uint_t length, struct vm_device * dev) {
79 PrintDebug("generic: writing 0x");
81 for (i = 0; i < length; i++) {
82 PrintDebug("%x", ((uchar_t*)src)[i]);
85 PrintDebug(" to port 0x%x ... ", port);
89 v3_outb(port,((uchar_t*)src)[0]);
92 v3_outw(port,((uint16_t*)src)[0]);
95 v3_outdw(port,((uint_t*)src)[0]);
98 for (i = 0; i < length; i++) {
99 v3_outb(port, ((uchar_t*)src)[i]);
103 PrintDebug(" done\n");
108 static int generic_read_port_passthrough(uint16_t port, void * src,
109 uint_t length, struct vm_device * dev) {
112 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
117 ((uchar_t*)src)[0] = v3_inb(port);
120 ((uint16_t*)src)[0] = v3_inw(port);
123 ((uint_t*)src)[0] = v3_indw(port);
126 for (i = 0; i < length; i++) {
127 ((uchar_t*)src)[i] = v3_inb(port);
131 PrintDebug(" done ... read 0x");
133 for (i = 0; i < length; i++) {
134 PrintDebug("%x", ((uchar_t*)src)[i]);
142 static int generic_write_port_ignore(uint16_t port, void * src,
143 uint_t length, struct vm_device * dev) {
146 PrintDebug("generic: writing 0x");
148 for (i = 0; i < length; i++) {
149 PrintDebug("%x", ((uchar_t*)src)[i]);
152 PrintDebug(" to port 0x%x ... ignored\n", port);
157 static int generic_read_port_ignore(uint16_t port, void * src,
158 uint_t length, struct vm_device * dev) {
160 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
162 memset((char*)src, 0, length);
163 PrintDebug(" ignored (return zeroed buffer)\n");
172 static int generic_free(struct vm_device * dev) {
173 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
174 struct port_range * tmp;
175 struct port_range * cur;
177 PrintDebug("generic: deinit_device\n");
179 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
182 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
183 cur->start, cur->end);
185 for (i = cur->start; i <= cur->end; i++) {
186 if (v3_dev_unhook_io(dev, i)) {
187 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
191 list_del(&(cur->range_link));
192 state->num_port_ranges--;
196 generic_reset_device(dev);
204 static struct v3_device_ops dev_ops = {
205 .free = generic_free,
206 .reset = generic_reset_device,
207 .start = generic_start_device,
208 .stop = generic_stop_device,
214 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
215 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
216 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
219 range->start = start;
223 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
224 range->start, range->end,
225 (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
227 for (i = start; i <= end; i++) {
228 if (type == GENERIC_PRINT_AND_PASSTHROUGH) {
229 if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough) == -1) {
230 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
233 } else if (type == GENERIC_PRINT_AND_IGNORE) {
234 if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore) == -1) {
235 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
241 list_add(&(range->range_link), &(state->port_list));
242 state->num_port_ranges++;
251 static int generic_init(struct guest_info * vm, void * cfg_data) {
252 struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
254 INIT_LIST_HEAD(&(state->port_list));
255 state->num_port_ranges = 0;
257 struct vm_device * dev = v3_allocate_device("GENERIC", &dev_ops, state);
259 if (v3_attach_device(vm, dev) == -1) {
260 PrintError("Could not attach device %s\n", "GENERIC");
264 PrintDebug("generic: init_device\n");
265 generic_reset_device(dev);
270 device_register("GENERIC", generic_init)