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)) {
137 if (info->io_map.update_map(info, port,
138 ((read == NULL) ? 0 : 1),
139 ((write == NULL) ? 0 : 1)) == -1) {
148 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
149 struct v3_io_hook * hook = v3_get_io_hook(info, port);
155 v3_rb_erase(&(hook->tree_node), &(info->io_map.map));
157 // set the arch map to default (this should be 1, 1)
158 info->io_map.update_map(info, port, 0, 0);
170 void v3_print_io_map(struct guest_info * info) {
171 struct v3_io_hook * tmp_hook = NULL;
172 struct rb_node * node = v3_rb_first(&(info->io_map.map));
174 PrintDebug("VMM IO Map\n");
177 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
179 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
181 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
182 } while ((node = v3_rb_next(node)));
188 * Write a byte to an I/O port.
190 void v3_outb(uint16_t port, uint8_t value) {
191 __asm__ __volatile__ (
194 : "a" (value), "Nd" (port)
199 * Read a byte from an I/O port.
201 uint8_t v3_inb(uint16_t port) {
204 __asm__ __volatile__ (
214 * Write a word to an I/O port.
216 void v3_outw(uint16_t port, uint16_t value) {
217 __asm__ __volatile__ (
220 : "a" (value), "Nd" (port)
225 * Read a word from an I/O port.
227 uint16_t v3_inw(uint16_t port) {
230 __asm__ __volatile__ (
240 * Write a double word to an I/O port.
242 void v3_outdw(uint16_t port, uint_t value) {
243 __asm__ __volatile__ (
246 : "a" (value), "Nd" (port)
251 * Read a double word from an I/O port.
253 uint_t v3_indw(uint16_t port) {
256 __asm__ __volatile__ (
269 static int default_write(uint16_t port, void *src, uint_t length, void * priv_data) {
273 __asm__ __volatile__ (
276 : "a" (*dst), "Nd" (port)
278 } else if (length == 2) {
279 __asm__ __volatile__ (
282 : "a" (*dst), "Nd" (port)
284 } else if (length == 4) {
285 __asm__ __volatile__ (
288 : "a" (*dst), "Nd" (port)
295 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data) {
300 __asm__ __volatile__ (