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;
45 static inline struct v3_io_hook * __insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
46 struct rb_node ** p = &(info->io_map.rb_node);
47 struct rb_node * parent = NULL;
48 struct v3_io_hook * tmp_hook = NULL;
52 tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
54 if (hook->port < tmp_hook->port) {
56 } else if (hook->port > tmp_hook->port) {
62 rb_link_node(&(hook->tree_node), parent, p);
68 static inline struct v3_io_hook * insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
69 struct v3_io_hook * ret;
71 if ((ret = __insert_io_hook(info, hook))) {
75 v3_rb_insert_color(&(hook->tree_node), &(info->io_map));
81 struct v3_io_hook * v3_get_io_hook(struct guest_info * info, uint_t port) {
82 struct rb_node * n = info->io_map.rb_node;
83 struct v3_io_hook * hook = NULL;
86 hook = rb_entry(n, struct v3_io_hook, tree_node);
88 if (port < hook->port) {
90 } else if (port > hook->port) {
105 int v3_hook_io_port(struct guest_info * info, uint_t port,
106 int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
107 int (*write)(ushort_t port, void * src, uint_t length, void * priv_data),
109 struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
111 io_hook->port = port;
114 io_hook->read = &default_read;
116 io_hook->read = read;
120 io_hook->write = &default_write;
122 io_hook->write = write;
126 io_hook->priv_data = priv_data;
128 if (insert_io_hook(info, io_hook)) {
136 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
137 struct v3_io_hook * hook = v3_get_io_hook(info, port);
143 v3_rb_erase(&(hook->tree_node), &(info->io_map));
155 void v3_print_io_map(struct guest_info * info) {
156 struct v3_io_hook * tmp_hook = NULL;
157 struct rb_node * node = v3_rb_first(&(info->io_map));
159 PrintDebug("VMM IO Map\n");
162 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
164 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
166 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
167 } while ((node = v3_rb_next(node)));
173 * Write a byte to an I/O port.
175 void v3_outb(ushort_t port, uchar_t value) {
176 __asm__ __volatile__ (
179 : "a" (value), "Nd" (port)
184 * Read a byte from an I/O port.
186 uchar_t v3_inb(ushort_t port) {
189 __asm__ __volatile__ (
199 * Write a word to an I/O port.
201 void v3_outw(ushort_t port, ushort_t value) {
202 __asm__ __volatile__ (
205 : "a" (value), "Nd" (port)
210 * Read a word from an I/O port.
212 ushort_t v3_inw(ushort_t port) {
215 __asm__ __volatile__ (
225 * Write a double word to an I/O port.
227 void v3_outdw(ushort_t port, uint_t value) {
228 __asm__ __volatile__ (
231 : "a" (value), "Nd" (port)
236 * Read a double word from an I/O port.
238 uint_t v3_indw(ushort_t port) {
241 __asm__ __volatile__ (
254 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
258 __asm__ __volatile__ (
261 : "a" (*dst), "Nd" (port)
263 } else if (length == 2) {
264 __asm__ __volatile__ (
267 : "a" (*dst), "Nd" (port)
269 } else if (length == 4) {
270 __asm__ __volatile__ (
273 : "a" (*dst), "Nd" (port)
280 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
285 __asm__ __volatile__ (