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>
23 #include <palacios/vm_guest.h>
27 #ifndef CONFIG_DEBUG_IO
29 #define PrintDebug(fmt, args...)
33 static int default_write(struct guest_info * core, uint16_t port, void *src, uint_t length, void * priv_data);
34 static int default_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data);
37 void v3_init_io_map(struct v3_vm_info * vm) {
39 vm->io_map.map.rb_node = NULL;
40 vm->io_map.arch_data = NULL;
41 vm->io_map.update_map = NULL;
48 static inline struct v3_io_hook * __insert_io_hook(struct v3_vm_info * vm, struct v3_io_hook * hook) {
49 struct rb_node ** p = &(vm->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 v3_vm_info * vm, struct v3_io_hook * hook) {
72 struct v3_io_hook * ret;
74 if ((ret = __insert_io_hook(vm, hook))) {
78 v3_rb_insert_color(&(hook->tree_node), &(vm->io_map.map));
84 struct v3_io_hook * v3_get_io_hook(struct v3_vm_info * vm, uint16_t port) {
85 struct rb_node * n = vm->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) {
107 int v3_hook_io_port(struct v3_vm_info * vm, uint16_t port,
108 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
109 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data),
111 struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
113 io_hook->port = port;
116 io_hook->read = &default_read;
118 io_hook->read = read;
122 io_hook->write = &default_write;
124 io_hook->write = write;
127 io_hook->priv_data = priv_data;
129 if (insert_io_hook(vm, io_hook)) {
130 PrintError("Could not insert IO hook for port %u (0x%x)\n", port, port);
135 if (vm->io_map.update_map) {
136 if (vm->io_map.update_map(vm, port,
137 ((read == NULL) ? 0 : 1),
138 ((write == NULL) ? 0 : 1)) == -1) {
139 PrintError("Could not update IO map for port %u (0x%x)\n", port, port);
148 int v3_unhook_io_port(struct v3_vm_info * vm, uint16_t port) {
149 struct v3_io_hook * hook = v3_get_io_hook(vm, port);
152 PrintError("Could not find port to unhook %u (0x%x)\n", port, port);
156 v3_rb_erase(&(hook->tree_node), &(vm->io_map.map));
158 if (vm->io_map.update_map) {
159 // set the arch map to default (this should be 1, 1)
160 vm->io_map.update_map(vm, port, 0, 0);
174 void v3_refresh_io_map(struct v3_vm_info * vm) {
175 struct v3_io_map * io_map = &(vm->io_map);
176 struct v3_io_hook * tmp = NULL;
178 if (io_map->update_map == NULL) {
179 PrintError("Trying to refresh an io map with no backend\n");
183 v3_rb_for_each_entry(tmp, &(io_map->map), tree_node) {
184 io_map->update_map(vm, tmp->port,
185 ((tmp->read == NULL) ? 0 : 1),
186 ((tmp->write == NULL) ? 0 : 1));
193 void v3_print_io_map(struct v3_vm_info * vm) {
194 struct v3_io_map * io_map = &(vm->io_map);
195 struct v3_io_hook * tmp_hook = NULL;
197 V3_Print("VMM IO Map\n");
199 v3_rb_for_each_entry(tmp_hook, &(io_map->map), tree_node) {
200 V3_Print("IO Port: %hu (Read=%p) (Write=%p)\n",
202 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
209 * Write a byte to an I/O port.
211 void v3_outb(uint16_t port, uint8_t value) {
212 __asm__ __volatile__ (
215 : "a" (value), "Nd" (port)
220 * Read a byte from an I/O port.
222 uint8_t v3_inb(uint16_t port) {
225 __asm__ __volatile__ (
235 * Write a word to an I/O port.
237 void v3_outw(uint16_t port, uint16_t value) {
238 __asm__ __volatile__ (
241 : "a" (value), "Nd" (port)
246 * Read a word from an I/O port.
248 uint16_t v3_inw(uint16_t port) {
251 __asm__ __volatile__ (
261 * Write a double word to an I/O port.
263 void v3_outdw(uint16_t port, uint_t value) {
264 __asm__ __volatile__ (
267 : "a" (value), "Nd" (port)
272 * Read a double word from an I/O port.
274 uint_t v3_indw(uint16_t port) {
277 __asm__ __volatile__ (
290 static int default_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) {
292 v3_outb(port, *(uint8_t *)src);
293 } else if (length == 2) {
294 v3_outw(port, *(uint16_t *)src);
295 } else if (length == 4) {
296 v3_outdw(port, *(uint32_t *)src);
304 static int default_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data) {
306 *(uint8_t *)dst = v3_inb(port);
307 } else if (length == 2) {
308 *(uint16_t *)dst = v3_inw(port);
309 } else if (length == 4) {
310 *(uint32_t *)dst = v3_indw(port);