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>
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, uint_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, uint_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 %d\n", 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 %d\n", port);
150 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
151 struct v3_io_hook * hook = v3_get_io_hook(info, port);
157 v3_rb_erase(&(hook->tree_node), &(info->io_map.map));
159 // set the arch map to default (this should be 1, 1)
160 info->io_map.update_map(info, port, 0, 0);
172 void v3_print_io_map(struct guest_info * info) {
173 struct v3_io_hook * tmp_hook = NULL;
174 struct rb_node * node = v3_rb_first(&(info->io_map.map));
176 PrintDebug("VMM IO Map\n");
179 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
181 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
183 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
184 } while ((node = v3_rb_next(node)));
190 * Write a byte to an I/O port.
192 void v3_outb(uint16_t port, uint8_t value) {
193 __asm__ __volatile__ (
196 : "a" (value), "Nd" (port)
201 * Read a byte from an I/O port.
203 uint8_t v3_inb(uint16_t port) {
206 __asm__ __volatile__ (
216 * Write a word to an I/O port.
218 void v3_outw(uint16_t port, uint16_t value) {
219 __asm__ __volatile__ (
222 : "a" (value), "Nd" (port)
227 * Read a word from an I/O port.
229 uint16_t v3_inw(uint16_t port) {
232 __asm__ __volatile__ (
242 * Write a double word to an I/O port.
244 void v3_outdw(uint16_t port, uint_t value) {
245 __asm__ __volatile__ (
248 : "a" (value), "Nd" (port)
253 * Read a double word from an I/O port.
255 uint_t v3_indw(uint16_t port) {
258 __asm__ __volatile__ (
271 static int default_write(uint16_t port, void *src, uint_t length, void * priv_data) {
275 __asm__ __volatile__ (
278 : "a" (*dst), "Nd" (port)
280 } else if (length == 2) {
281 __asm__ __volatile__ (
284 : "a" (*dst), "Nd" (port)
286 } else if (length == 4) {
287 __asm__ __volatile__ (
290 : "a" (*dst), "Nd" (port)
297 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data) {
302 __asm__ __volatile__ (