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 {
47 struct list_head range_link;
54 static int generic_write_port_passthrough(struct guest_info * core, uint16_t port, void * src,
55 uint_t length, void * priv_data) {
58 PrintDebug("generic: writing 0x");
60 for (i = 0; i < length; i++) {
61 PrintDebug("%x", ((uint8_t *)src)[i]);
64 PrintDebug(" to port 0x%x ... ", port);
68 v3_outb(port, ((uint8_t *)src)[0]);
71 v3_outw(port, ((uint16_t *)src)[0]);
74 v3_outdw(port, ((uint32_t *)src)[0]);
77 for (i = 0; i < length; i++) {
78 v3_outb(port, ((uint8_t *)src)[i]);
82 PrintDebug(" done\n");
87 static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src,
88 uint_t length, void * priv_data) {
91 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
96 ((uint8_t *)src)[0] = v3_inb(port);
99 ((uint16_t *)src)[0] = v3_inw(port);
102 ((uint32_t *)src)[0] = v3_indw(port);
105 for (i = 0; i < length; i++) {
106 ((uint8_t *)src)[i] = v3_inb(port);
110 PrintDebug(" done ... read 0x");
112 for (i = 0; i < length; i++) {
113 PrintDebug("%x", ((uint8_t *)src)[i]);
121 static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src,
122 uint_t length, void * priv_data) {
125 PrintDebug("generic: writing 0x");
127 for (i = 0; i < length; i++) {
128 PrintDebug("%x", ((uint8_t *)src)[i]);
131 PrintDebug(" to port 0x%x ... ignored\n", port);
136 static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src,
137 uint_t length, void * priv_data) {
139 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
141 memset((uint8_t *)src, 0, length);
142 PrintDebug(" ignored (return zeroed buffer)\n");
151 static int generic_free(struct generic_internal * state) {
152 PrintDebug("generic: deinit_device\n");
162 static struct v3_device_ops dev_ops = {
163 .free = (int (*)(void *))generic_free,
169 static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, generic_mode_t mode) {
172 PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n",
174 (mode == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
176 for (i = start; i <= end; i++) {
177 if (mode == GENERIC_PRINT_AND_PASSTHROUGH) {
178 if (v3_dev_hook_io(dev, i,
179 &generic_read_port_passthrough,
180 &generic_write_port_passthrough) == -1) {
181 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
184 } else if (mode == GENERIC_PRINT_AND_IGNORE) {
185 if (v3_dev_hook_io(dev, i,
186 &generic_read_port_ignore,
187 &generic_write_port_ignore) == -1) {
188 PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
201 static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
202 struct generic_internal * state = NULL;
203 char * dev_id = v3_cfg_val(cfg, "ID");
204 v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
207 state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
210 PrintError("Could not allocate generic state\n");
214 memset(state, 0, sizeof(struct generic_internal));
216 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
219 PrintError("Could not attach device %s\n", dev_id);
224 PrintDebug("generic: init_device\n");
226 // scan port list....
228 uint16_t start = atox(v3_cfg_val(port_cfg, "start"));
229 uint16_t end = atox(v3_cfg_val(port_cfg, "end"));
230 char * mode_str = v3_cfg_val(port_cfg, "mode");
231 generic_mode_t mode = GENERIC_IGNORE;
233 if (strcasecmp(mode_str, "print_and_ignore") == 0) {
234 mode = GENERIC_PRINT_AND_IGNORE;
235 } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
236 mode = GENERIC_PRINT_AND_PASSTHROUGH;
238 PrintError("Invalid Mode %s\n", mode_str);
239 v3_remove_device(dev);
243 if (add_port_range(dev, start, end, mode) == -1) {
244 PrintError("Could not add port range %d-%d\n", start, end);
245 v3_remove_device(dev);
249 port_cfg = v3_cfg_next_branch(port_cfg);
256 device_register("GENERIC", generic_init)