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 <palacios/vmm.h>
23 #include <palacios/vmm_types.h>
24 #include <palacios/vmm_list.h>
25 #include <palacios/vmm_io.h>
26 #include <palacios/vmm_dev_mgr.h>
28 #ifndef CONFIG_DEBUG_GENERIC
30 #define PrintDebug(fmt, args...)
34 typedef enum {GENERIC_IGNORE,
36 GENERIC_PRINT_AND_PASSTHROUGH,
37 GENERIC_PRINT_AND_IGNORE} generic_mode_t;
39 struct generic_internal {
40 struct list_head port_list;
41 uint_t num_port_ranges;
50 struct list_head range_link;
57 static int generic_write_port_passthrough(struct guest_info * core, uint16_t port, void * src,
58 uint_t length, void * priv_data) {
61 PrintDebug("generic: writing 0x");
63 for (i = 0; i < length; i++) {
64 PrintDebug("%x", ((uint8_t *)src)[i]);
67 PrintDebug(" to port 0x%x ... ", port);
71 v3_outb(port, ((uint8_t *)src)[0]);
74 v3_outw(port, ((uint16_t *)src)[0]);
77 v3_outdw(port, ((uint32_t *)src)[0]);
80 for (i = 0; i < length; i++) {
81 v3_outb(port, ((uint8_t *)src)[i]);
85 PrintDebug(" done\n");
90 static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src,
91 uint_t length, void * priv_data) {
94 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
99 ((uint8_t *)src)[0] = v3_inb(port);
102 ((uint16_t *)src)[0] = v3_inw(port);
105 ((uint32_t *)src)[0] = v3_indw(port);
108 for (i = 0; i < length; i++) {
109 ((uint8_t *)src)[i] = v3_inb(port);
113 PrintDebug(" done ... read 0x");
115 for (i = 0; i < length; i++) {
116 PrintDebug("%x", ((uint8_t *)src)[i]);
124 static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src,
125 uint_t length, void * priv_data) {
128 PrintDebug("generic: writing 0x");
130 for (i = 0; i < length; i++) {
131 PrintDebug("%x", ((uint8_t *)src)[i]);
134 PrintDebug(" to port 0x%x ... ignored\n", port);
139 static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src,
140 uint_t length, void * priv_data) {
142 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
144 memset((uint8_t *)src, 0, length);
145 PrintDebug(" ignored (return zeroed buffer)\n");
154 static int generic_free(struct vm_device * dev) {
155 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
156 struct port_range * tmp;
157 struct port_range * cur;
159 PrintDebug("generic: deinit_device\n");
161 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
164 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
165 cur->start, cur->end);
167 for (i = cur->start; i <= cur->end; i++) {
168 if (v3_unhook_io_port(dev->vm, i)) {
169 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
173 list_del(&(cur->range_link));
174 state->num_port_ranges--;
186 static struct v3_device_ops dev_ops = {
187 .free = generic_free,
193 static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, generic_mode_t mode) {
194 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
195 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
198 range->start = start;
202 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
204 (mode == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
206 for (i = start; i <= end; i++) {
207 if (mode == GENERIC_PRINT_AND_PASSTHROUGH) {
208 if (v3_hook_io_port(dev->vm, i,
209 &generic_read_port_passthrough,
210 &generic_write_port_passthrough, dev) == -1) {
211 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
214 } else if (mode == GENERIC_PRINT_AND_IGNORE) {
215 if (v3_hook_io_port(dev->vm, i,
216 &generic_read_port_ignore,
217 &generic_write_port_ignore, dev) == -1) {
218 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
224 list_add(&(range->range_link), &(state->port_list));
225 state->num_port_ranges++;
234 static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
235 struct generic_internal * state = NULL;
236 char * dev_id = v3_cfg_val(cfg, "ID");
237 v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
240 state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
243 PrintError("Could not allocate generic state\n");
247 memset(state, 0, sizeof(struct generic_internal));
249 INIT_LIST_HEAD(&(state->port_list));
250 state->num_port_ranges = 0;
252 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
254 if (v3_attach_device(vm, dev) == -1) {
255 PrintError("Could not attach device %s\n", dev_id);
260 PrintDebug("generic: init_device\n");
262 // scan port list....
264 uint16_t start = atox(v3_cfg_val(port_cfg, "start"));
265 uint16_t end = atox(v3_cfg_val(port_cfg, "end"));
266 char * mode_str = v3_cfg_val(port_cfg, "mode");
267 generic_mode_t mode = GENERIC_IGNORE;
269 if (strcasecmp(mode_str, "print_and_ignore") == 0) {
270 mode = GENERIC_PRINT_AND_IGNORE;
271 } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
272 mode = GENERIC_PRINT_AND_PASSTHROUGH;
274 PrintError("Invalid Mode %s\n", mode_str);
275 v3_detach_device(dev);
279 if (add_port_range(dev, start, end, mode) == -1) {
280 PrintError("Could not add port range %d-%d\n", start, end);
281 v3_detach_device(dev);
285 port_cfg = v3_cfg_next_branch(port_cfg);
292 device_register("GENERIC", generic_init)