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, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_io.h>
21 #include <palacios/vmm_string.h>
22 #include <palacios/vmm.h>
27 #ifndef CONFIG_DEBUG_IO
29 #define PrintDebug(fmt, args...)
33 static int default_write(uint16_t port, void *src, uint_t length, void * priv_data);
34 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data);
37 void v3_init_io_map(struct guest_info * info) {
39 info->io_map.map.rb_node = NULL;
40 info->io_map.arch_data = NULL;
41 info->io_map.update_map = NULL;
48 static inline struct v3_io_hook * __insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
49 struct rb_node ** p = &(info->io_map.map.rb_node);
50 struct rb_node * parent = NULL;
51 struct v3_io_hook * tmp_hook = NULL;
55 tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
57 if (hook->port < tmp_hook->port) {
59 } else if (hook->port > tmp_hook->port) {
65 rb_link_node(&(hook->tree_node), parent, p);
71 static inline struct v3_io_hook * insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
72 struct v3_io_hook * ret;
74 if ((ret = __insert_io_hook(info, hook))) {
78 v3_rb_insert_color(&(hook->tree_node), &(info->io_map.map));
84 struct v3_io_hook * v3_get_io_hook(struct guest_info * info, uint16_t port) {
85 struct rb_node * n = info->io_map.map.rb_node;
86 struct v3_io_hook * hook = NULL;
89 hook = rb_entry(n, struct v3_io_hook, tree_node);
91 if (port < hook->port) {
93 } else if (port > hook->port) {
108 int v3_hook_io_port(struct guest_info * info, uint16_t port,
109 int (*read)(uint16_t port, void * dst, uint_t length, void * priv_data),
110 int (*write)(uint16_t port, void * src, uint_t length, void * priv_data),
112 struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
114 io_hook->port = port;
117 io_hook->read = &default_read;
119 io_hook->read = read;
123 io_hook->write = &default_write;
125 io_hook->write = write;
129 io_hook->priv_data = priv_data;
131 if (insert_io_hook(info, io_hook)) {
132 PrintError("Could not insert IO hook for port %u (0x%x)\n", port, port);
138 if (info->io_map.update_map(info, port,
139 ((read == NULL) ? 0 : 1),
140 ((write == NULL) ? 0 : 1)) == -1) {
141 PrintError("Could not update IO map for port %u (0x%x)\n", port, port);
150 int v3_unhook_io_port(struct guest_info * info, uint16_t port) {
151 struct v3_io_hook * hook = v3_get_io_hook(info, port);
154 PrintError("Could not find port to unhook %u (0x%x)\n", port, port);
158 v3_rb_erase(&(hook->tree_node), &(info->io_map.map));
160 // set the arch map to default (this should be 1, 1)
161 info->io_map.update_map(info, port, 0, 0);
173 void v3_print_io_map(struct guest_info * info) {
174 struct v3_io_hook * tmp_hook = NULL;
175 struct rb_node * node = v3_rb_first(&(info->io_map.map));
177 V3_Print("VMM IO Map\n");
180 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
182 V3_Print("IO Port: %hu (Read=%p) (Write=%p)\n",
184 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
185 } while ((node = v3_rb_next(node)));
191 * Write a byte to an I/O port.
193 void v3_outb(uint16_t port, uint8_t value) {
194 __asm__ __volatile__ (
197 : "a" (value), "Nd" (port)
202 * Read a byte from an I/O port.
204 uint8_t v3_inb(uint16_t port) {
207 __asm__ __volatile__ (
217 * Write a word to an I/O port.
219 void v3_outw(uint16_t port, uint16_t value) {
220 __asm__ __volatile__ (
223 : "a" (value), "Nd" (port)
228 * Read a word from an I/O port.
230 uint16_t v3_inw(uint16_t port) {
233 __asm__ __volatile__ (
243 * Write a double word to an I/O port.
245 void v3_outdw(uint16_t port, uint_t value) {
246 __asm__ __volatile__ (
249 : "a" (value), "Nd" (port)
254 * Read a double word from an I/O port.
256 uint_t v3_indw(uint16_t port) {
259 __asm__ __volatile__ (
272 static int default_write(uint16_t port, void * src, uint_t length, void * priv_data) {
274 v3_outb(port, *(uint8_t *)src);
275 } else if (length == 2) {
276 v3_outw(port, *(uint16_t *)src);
277 } else if (length == 4) {
278 v3_outdw(port, *(uint32_t *)src);
286 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data) {
288 *(uint8_t *)dst = v3_inb(port);
289 } else if (length == 2) {
290 *(uint16_t *)dst = v3_inw(port);
291 } else if (length == 4) {
292 *(uint32_t *)dst = v3_indw(port);