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));
153 void v3_print_io_map(struct guest_info * info) {
154 struct v3_io_hook * tmp_hook = NULL;
155 struct rb_node * node = v3_rb_first(&(info->io_map));
157 PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
160 tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
162 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
164 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
165 } while ((node = v3_rb_next(node)));
171 * Write a byte to an I/O port.
173 void v3_outb(ushort_t port, uchar_t value) {
174 __asm__ __volatile__ (
177 : "a" (value), "Nd" (port)
182 * Read a byte from an I/O port.
184 uchar_t v3_inb(ushort_t port) {
187 __asm__ __volatile__ (
197 * Write a word to an I/O port.
199 void v3_outw(ushort_t port, ushort_t value) {
200 __asm__ __volatile__ (
203 : "a" (value), "Nd" (port)
208 * Read a word from an I/O port.
210 ushort_t v3_inw(ushort_t port) {
213 __asm__ __volatile__ (
223 * Write a double word to an I/O port.
225 void v3_outdw(ushort_t port, uint_t value) {
226 __asm__ __volatile__ (
229 : "a" (value), "Nd" (port)
234 * Read a double word from an I/O port.
236 uint_t v3_indw(ushort_t port) {
239 __asm__ __volatile__ (
252 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
256 __asm__ __volatile__ (
259 : "a" (*dst), "Nd" (port)
261 } else if (length == 2) {
262 __asm__ __volatile__ (
265 : "a" (*dst), "Nd" (port)
267 } else if (length == 4) {
268 __asm__ __volatile__ (
271 : "a" (*dst), "Nd" (port)
278 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
283 __asm__ __volatile__ (