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(ushort_t port, void *src, uint_t length, void * priv_data);
34 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data);
37 void v3_init_io_map(struct guest_info * info) {
38 info->io_map.rb_node = NULL;
44 static inline struct v3_io_hook * __insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
45 struct rb_node ** p = &(info->io_map.rb_node);
46 struct rb_node * parent = NULL;
47 struct v3_io_hook * tmp_hook = NULL;
51 tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
53 if (hook->port < tmp_hook->port) {
55 } else if (hook->port > tmp_hook->port) {
61 rb_link_node(&(hook->tree_node), parent, p);
67 static inline struct v3_io_hook * insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
68 struct v3_io_hook * ret;
70 if ((ret = __insert_io_hook(info, hook))) {
74 v3_rb_insert_color(&(hook->tree_node), &(info->io_map));
80 struct v3_io_hook * v3_get_io_hook(struct guest_info * info, uint_t port) {
81 struct rb_node * n = info->io_map.rb_node;
82 struct v3_io_hook * hook = NULL;
85 hook = rb_entry(n, struct v3_io_hook, tree_node);
87 if (port < hook->port) {
89 } else if (port > hook->port) {
104 int v3_hook_io_port(struct guest_info * info, uint_t port,
105 int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
106 int (*write)(ushort_t port, void * src, uint_t length, void * priv_data),
108 struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
110 io_hook->port = port;
113 io_hook->read = &default_read;
115 io_hook->read = read;
119 io_hook->write = &default_write;
121 io_hook->write = write;
125 io_hook->priv_data = priv_data;
127 if (insert_io_hook(info, io_hook)) {
135 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
136 struct v3_io_hook * hook = v3_get_io_hook(info, port);
142 v3_rb_erase(&(hook->tree_node), &(info->io_map));
154 void v3_print_io_map(struct guest_info * info) {
155 struct v3_io_hook * tmp_hook = NULL;
156 struct rb_node * node = v3_rb_first(&(info->io_map));
158 PrintDebug("VMM IO Map\n");
161 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
163 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
165 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
166 } while ((node = v3_rb_next(node)));
172 * Write a byte to an I/O port.
174 void v3_outb(ushort_t port, uchar_t value) {
175 __asm__ __volatile__ (
178 : "a" (value), "Nd" (port)
183 * Read a byte from an I/O port.
185 uchar_t v3_inb(ushort_t port) {
188 __asm__ __volatile__ (
198 * Write a word to an I/O port.
200 void v3_outw(ushort_t port, ushort_t value) {
201 __asm__ __volatile__ (
204 : "a" (value), "Nd" (port)
209 * Read a word from an I/O port.
211 ushort_t v3_inw(ushort_t port) {
214 __asm__ __volatile__ (
224 * Write a double word to an I/O port.
226 void v3_outdw(ushort_t port, uint_t value) {
227 __asm__ __volatile__ (
230 : "a" (value), "Nd" (port)
235 * Read a double word from an I/O port.
237 uint_t v3_indw(ushort_t port) {
240 __asm__ __volatile__ (
253 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
257 __asm__ __volatile__ (
260 : "a" (*dst), "Nd" (port)
262 } else if (length == 2) {
263 __asm__ __volatile__ (
266 : "a" (*dst), "Nd" (port)
268 } else if (length == 4) {
269 __asm__ __volatile__ (
272 : "a" (*dst), "Nd" (port)
279 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
284 __asm__ __volatile__ (