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 {
45 static int generic_write_port_passthrough(struct guest_info * core, uint16_t port, void * src,
46 uint_t length, void * priv_data) {
51 v3_outb(port, ((uint8_t *)src)[0]);
54 v3_outw(port, ((uint16_t *)src)[0]);
57 v3_outdw(port, ((uint32_t *)src)[0]);
60 for (i = 0; i < length; i++) {
61 v3_outb(port, ((uint8_t *)src)[i]);
68 static int generic_write_port_print_and_passthrough(struct guest_info * core, uint16_t port, void * src,
69 uint_t length, void * priv_data) {
73 PrintDebug("generic: writing 0x");
75 for (i = 0; i < length; i++) {
76 PrintDebug("%x", ((uint8_t *)src)[i]);
79 PrintDebug(" to port 0x%x ... ", port);
81 rc=generic_write_port_passthrough(core,port,src,length,priv_data);
83 PrintDebug(" done\n");
88 static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src,
89 uint_t length, void * priv_data) {
94 ((uint8_t *)src)[0] = v3_inb(port);
97 ((uint16_t *)src)[0] = v3_inw(port);
100 ((uint32_t *)src)[0] = v3_indw(port);
103 for (i = 0; i < length; i++) {
104 ((uint8_t *)src)[i] = v3_inb(port);
111 static int generic_read_port_print_and_passthrough(struct guest_info * core, uint16_t port, void * src,
112 uint_t length, void * priv_data) {
116 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
119 rc=generic_read_port_passthrough(core,port,src,length,priv_data);
121 PrintDebug(" done ... read 0x");
123 for (i = 0; i < rc; i++) {
124 PrintDebug("%x", ((uint8_t *)src)[i]);
133 static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src,
134 uint_t length, void * priv_data) {
136 memset((uint8_t *)src, 0, length);
141 static int generic_read_port_print_and_ignore(struct guest_info * core, uint16_t port, void * src,
142 uint_t length, void * priv_data) {
144 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
146 memset((uint8_t *)src, 0, length);
147 PrintDebug(" ignored (return zeroed buffer)\n");
152 static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src,
153 uint_t length, void * priv_data) {
158 static int generic_write_port_print_and_ignore(struct guest_info * core, uint16_t port, void * src,
159 uint_t length, void * priv_data) {
162 PrintDebug("generic: writing 0x%x bytes to port 0x%x ", length, port);
164 memset((uint8_t *)src, 0, length);
165 PrintDebug(" ignored - data was: 0x");
167 for (i = 0; i < length; i++) {
168 PrintDebug("%x", ((uint8_t *)src)[i]);
179 static int generic_free(struct generic_internal * state) {
180 PrintDebug("generic: deinit_device\n");
190 static struct v3_device_ops dev_ops = {
191 .free = (int (*)(void *))generic_free,
197 static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, generic_mode_t mode) {
200 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
202 (mode == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
204 for (i = start; i <= end; i++) {
205 if (mode == GENERIC_PRINT_AND_PASSTHROUGH) {
206 if (v3_dev_hook_io(dev, i,
207 &generic_read_port_print_and_passthrough,
208 &generic_write_port_print_and_passthrough) == -1) {
209 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
212 } else if (mode == GENERIC_PRINT_AND_IGNORE) {
213 if (v3_dev_hook_io(dev, i,
214 &generic_read_port_print_and_ignore,
215 &generic_write_port_print_and_ignore) == -1) {
216 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
219 } else if (mode == GENERIC_PASSTHROUGH) {
220 if (v3_dev_hook_io(dev, i,
221 &generic_read_port_passthrough,
222 &generic_write_port_passthrough) == -1) {
223 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
226 } else if (mode == GENERIC_IGNORE) {
227 if (v3_dev_hook_io(dev, i,
228 &generic_read_port_ignore,
229 &generic_write_port_ignore) == -1) {
230 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
243 static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
244 struct generic_internal * state = NULL;
245 char * dev_id = v3_cfg_val(cfg, "ID");
246 v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
249 state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
252 PrintError("Could not allocate generic state\n");
256 memset(state, 0, sizeof(struct generic_internal));
258 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
261 PrintError("Could not attach device %s\n", dev_id);
266 PrintDebug("generic: init_device\n");
268 // scan port list....
270 uint16_t start = atox(v3_cfg_val(port_cfg, "start"));
271 uint16_t end = atox(v3_cfg_val(port_cfg, "end"));
272 char * mode_str = v3_cfg_val(port_cfg, "mode");
273 generic_mode_t mode = GENERIC_IGNORE;
275 if (strcasecmp(mode_str, "print_and_ignore") == 0) {
276 mode = GENERIC_PRINT_AND_IGNORE;
277 } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
278 mode = GENERIC_PRINT_AND_PASSTHROUGH;
279 } else if (strcasecmp(mode_str, "passthrough") == 0) {
280 mode = GENERIC_PASSTHROUGH;
281 } else if (strcasecmp(mode_str, "ignore") == 0) {
282 mode = GENERIC_IGNORE;
284 PrintError("Invalid Mode %s\n", mode_str);
285 v3_remove_device(dev);
289 if (add_port_range(dev, start, end, mode) == -1) {
290 PrintError("Could not add port range %d-%d\n", start, end);
291 v3_remove_device(dev);
295 port_cfg = v3_cfg_next_branch(port_cfg);
302 device_register("GENERIC", generic_init)