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...)
38 struct generic_internal {
39 struct list_head port_list;
40 uint_t num_port_ranges;
49 struct list_head range_link;
55 static int generic_reset_device(struct vm_device * dev) {
56 PrintDebug("generic: reset device\n");
64 static int generic_start_device(struct vm_device * dev) {
65 PrintDebug("generic: start device\n");
70 static int generic_stop_device(struct vm_device * dev) {
71 PrintDebug("generic: stop device\n");
78 static int generic_write_port_passthrough(ushort_t port,
81 struct vm_device * dev) {
84 PrintDebug("generic: writing 0x");
86 for (i = 0; i < length; i++) {
87 PrintDebug("%x", ((uchar_t*)src)[i]);
90 PrintDebug(" to port 0x%x ... ", port);
95 v3_outb(port,((uchar_t*)src)[0]);
98 v3_outw(port,((ushort_t*)src)[0]);
101 v3_outdw(port,((uint_t*)src)[0]);
104 for (i = 0; i < length; i++) {
105 v3_outb(port, ((uchar_t*)src)[i]);
110 PrintDebug(" done\n");
115 static int generic_read_port_passthrough(ushort_t port,
118 struct vm_device * dev) {
121 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
126 ((uchar_t*)src)[0] = v3_inb(port);
129 ((ushort_t*)src)[0] = v3_inw(port);
132 ((uint_t*)src)[0] = v3_indw(port);
135 for (i = 0; i < length; i++) {
136 ((uchar_t*)src)[i] = v3_inb(port);
140 PrintDebug(" done ... read 0x");
142 for (i = 0; i < length; i++) {
143 PrintDebug("%x", ((uchar_t*)src)[i]);
151 static int generic_write_port_ignore(ushort_t port,
154 struct vm_device * dev) {
157 PrintDebug("generic: writing 0x");
159 for (i = 0; i < length; i++) {
160 PrintDebug("%x", ((uchar_t*)src)[i]);
163 PrintDebug(" to port 0x%x ... ignored\n", port);
168 static int generic_read_port_ignore(ushort_t port,
171 struct vm_device * dev) {
173 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
175 memset((char*)src, 0, length);
176 PrintDebug(" ignored (return zeroed buffer)\n");
184 static int generic_init_device(struct vm_device * dev) {
185 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
187 PrintDebug("generic: init_device\n");
188 generic_reset_device(dev);
191 if (PORT_HOOKS) { // This is a runtime conditional on a #define
192 struct port_range * tmp = NULL;
194 list_for_each_entry(tmp, &(state->port_list), range_link) {
197 PrintDebug("generic: hooking ports 0x%x to 0x%x as %s\n",
198 tmp->start, tmp->end,
199 (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
201 for (i = tmp->start; i <= tmp->end; i++) {
202 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) {
204 if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
205 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
208 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) {
210 if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) {
211 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
218 PrintDebug("generic: hooking ports not supported\n");
225 static int generic_deinit_device(struct vm_device * dev) {
226 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
229 PrintDebug("generic: deinit_device\n");
232 struct port_range * tmp;
233 struct port_range * cur;
235 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
238 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
239 cur->start, cur->end);
241 for (i = cur->start; i <= cur->end; i++) {
242 if (v3_dev_unhook_io(dev, i)) {
243 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
247 list_del(&(cur->range_link));
248 state->num_port_ranges--;
252 PrintDebug("generic: unhooking ports not supported\n");
257 generic_reset_device(dev);
265 static struct vm_device_ops dev_ops = {
266 .init = generic_init_device,
267 .deinit = generic_deinit_device,
268 .reset = generic_reset_device,
269 .start = generic_start_device,
270 .stop = generic_stop_device,
276 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
279 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
281 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
282 range->start = start;
287 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
288 range->start, range->end,
289 (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
291 list_add(&(range->range_link), &(state->port_list));
292 state->num_port_ranges++;
294 PrintDebug("generic: hooking IO ports not supported\n");
301 struct vm_device * v3_create_generic() {
302 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
304 generic_state->num_port_ranges = 0;
306 INIT_LIST_HEAD(&(generic_state->port_list));
308 struct vm_device * device = v3_create_device("GENERIC", &dev_ops, generic_state);