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;
56 static int generic_reset_device(struct vm_device * dev) {
57 PrintDebug("generic: reset device\n");
65 static int generic_start_device(struct vm_device * dev) {
66 PrintDebug("generic: start device\n");
71 static int generic_stop_device(struct vm_device * dev) {
72 PrintDebug("generic: stop device\n");
79 static int generic_write_port_passthrough(struct guest_info * core, uint16_t port, void * src,
80 uint_t length, struct vm_device * dev) {
83 PrintDebug("generic: writing 0x");
85 for (i = 0; i < length; i++) {
86 PrintDebug("%x", ((uchar_t*)src)[i]);
89 PrintDebug(" to port 0x%x ... ", port);
93 v3_outb(port,((uchar_t*)src)[0]);
96 v3_outw(port,((uint16_t*)src)[0]);
99 v3_outdw(port,((uint32_t *)src)[0]);
102 for (i = 0; i < length; i++) {
103 v3_outb(port, ((uchar_t *)src)[i]);
107 PrintDebug(" done\n");
112 static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src,
113 uint_t length, struct vm_device * dev) {
116 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
121 ((uchar_t*)src)[0] = v3_inb(port);
124 ((uint16_t*)src)[0] = v3_inw(port);
127 ((uint_t*)src)[0] = v3_indw(port);
130 for (i = 0; i < length; i++) {
131 ((uchar_t*)src)[i] = v3_inb(port);
135 PrintDebug(" done ... read 0x");
137 for (i = 0; i < length; i++) {
138 PrintDebug("%x", ((uchar_t*)src)[i]);
146 static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src,
147 uint_t length, struct vm_device * dev) {
150 PrintDebug("generic: writing 0x");
152 for (i = 0; i < length; i++) {
153 PrintDebug("%x", ((uchar_t*)src)[i]);
156 PrintDebug(" to port 0x%x ... ignored\n", port);
161 static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src,
162 uint_t length, struct vm_device * dev) {
164 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
166 memset((char*)src, 0, length);
167 PrintDebug(" ignored (return zeroed buffer)\n");
176 static int generic_free(struct vm_device * dev) {
177 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
178 struct port_range * tmp;
179 struct port_range * cur;
181 PrintDebug("generic: deinit_device\n");
183 list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
186 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
187 cur->start, cur->end);
189 for (i = cur->start; i <= cur->end; i++) {
190 if (v3_dev_unhook_io(dev, i)) {
191 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
195 list_del(&(cur->range_link));
196 state->num_port_ranges--;
200 generic_reset_device(dev);
208 static struct v3_device_ops dev_ops = {
209 .free = generic_free,
210 .reset = generic_reset_device,
211 .start = generic_start_device,
212 .stop = generic_stop_device,
218 static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, generic_mode_t mode) {
219 struct generic_internal * state = (struct generic_internal *)(dev->private_data);
220 struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
223 range->start = start;
227 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
229 (mode == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
231 for (i = start; i <= end; i++) {
232 if (mode == GENERIC_PRINT_AND_PASSTHROUGH) {
233 if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough) == -1) {
234 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
237 } else if (mode == GENERIC_PRINT_AND_IGNORE) {
238 if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore) == -1) {
239 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
245 list_add(&(range->range_link), &(state->port_list));
246 state->num_port_ranges++;
255 static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
256 struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
257 char * dev_id = v3_cfg_val(cfg, "ID");
259 v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
262 INIT_LIST_HEAD(&(state->port_list));
263 state->num_port_ranges = 0;
266 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
268 if (v3_attach_device(vm, dev) == -1) {
269 PrintError("Could not attach device %s\n", dev_id);
273 PrintDebug("generic: init_device\n");
274 generic_reset_device(dev);
276 // scan port list....
278 uint16_t start = atox(v3_cfg_val(port_cfg, "start"));
279 uint16_t end = atox(v3_cfg_val(port_cfg, "end"));
280 char * mode_str = v3_cfg_val(port_cfg, "mode");
281 generic_mode_t mode = GENERIC_IGNORE;
283 if (strcasecmp(mode_str, "print_and_ignore") == 0) {
284 mode = GENERIC_PRINT_AND_IGNORE;
285 } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
286 mode = GENERIC_PRINT_AND_PASSTHROUGH;
288 PrintError("Invalid Mode %s\n", mode_str);
292 if (add_port_range(dev, start, end, mode) == -1) {
293 PrintError("Could not add port range %d-%d\n", start, end);
297 port_cfg = v3_cfg_next_branch(port_cfg);
304 device_register("GENERIC", generic_init)