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");
185 static int generic_free(struct vm_device * dev) {
186 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
189 PrintDebug("generic: deinit_device\n");
192 struct port_range * tmp;
193 struct port_range * cur;
195 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
198 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
199 cur->start, cur->end);
201 for (i = cur->start; i <= cur->end; i++) {
202 if (v3_dev_unhook_io(dev, i)) {
203 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
207 list_del(&(cur->range_link));
208 state->num_port_ranges--;
212 PrintDebug("generic: unhooking ports not supported\n");
217 generic_reset_device(dev);
225 static struct v3_device_ops dev_ops = {
226 .free = generic_free,
227 .reset = generic_reset_device,
228 .start = generic_start_device,
229 .stop = generic_stop_device,
235 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
238 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
240 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
241 range->start = start;
246 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
247 range->start, range->end,
248 (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
250 list_add(&(range->range_link), &(state->port_list));
251 state->num_port_ranges++;
253 PrintDebug("generic: hooking IO ports not supported\n");
263 static int generic_init(struct guest_info * vm, void * cfg_data) {
264 struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
266 state->num_port_ranges = 0;
268 INIT_LIST_HEAD(&(state->port_list));
270 struct vm_device * dev = v3_allocate_device("GENERIC", &dev_ops, state);
273 if (v3_attach_device(vm, dev) == -1) {
274 PrintError("Could not attach device %s\n", "GENERIC");
278 PrintDebug("generic: init_device\n");
279 generic_reset_device(dev);
282 if (PORT_HOOKS) { // This is a runtime conditional on a #define
283 struct port_range * tmp = NULL;
285 list_for_each_entry(tmp, &(state->port_list), range_link) {
288 PrintDebug("generic: hooking ports 0x%x to 0x%x as %s\n",
289 tmp->start, tmp->end,
290 (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
292 for (i = tmp->start; i <= tmp->end; i++) {
293 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) {
295 if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
296 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
299 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) {
301 if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) {
302 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
309 PrintDebug("generic: hooking ports not supported\n");
316 device_register("GENERIC", generic_init)