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 V3_CONFIG_DEBUG_IO
29 #define PrintDebug(fmt, args...)
32 static int free_hook(struct v3_vm_info * vm, struct v3_io_hook * hook);
34 static int default_write(struct guest_info * core, uint16_t port, void *src, uint_t length, void * priv_data);
35 static int default_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data);
38 void v3_init_io_map(struct v3_vm_info * vm) {
40 vm->io_map.map.rb_node = NULL;
41 vm->io_map.arch_data = NULL;
42 vm->io_map.update_map = NULL;
46 int v3_deinit_io_map(struct v3_vm_info * vm) {
47 struct rb_node * node = v3_rb_first(&(vm->io_map.map));
48 struct v3_io_hook * hook = NULL;
49 struct rb_node * tmp_node = NULL;
52 hook = rb_entry(node, struct v3_io_hook, tree_node);
54 node = v3_rb_next(node);
65 static inline struct v3_io_hook * __insert_io_hook(struct v3_vm_info * vm, struct v3_io_hook * hook) {
66 struct rb_node ** p = &(vm->io_map.map.rb_node);
67 struct rb_node * parent = NULL;
68 struct v3_io_hook * tmp_hook = NULL;
72 tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
74 if (hook->port < tmp_hook->port) {
76 } else if (hook->port > tmp_hook->port) {
83 rb_link_node(&(hook->tree_node), parent, p);
89 static inline struct v3_io_hook * insert_io_hook(struct v3_vm_info * vm, struct v3_io_hook * hook) {
90 struct v3_io_hook * ret;
92 if ((ret = __insert_io_hook(vm, hook))) {
96 v3_rb_insert_color(&(hook->tree_node), &(vm->io_map.map));
102 struct v3_io_hook * v3_get_io_hook(struct v3_vm_info * vm, uint16_t port) {
103 struct rb_node * n = vm->io_map.map.rb_node;
104 struct v3_io_hook * hook = NULL;
107 hook = rb_entry(n, struct v3_io_hook, tree_node);
109 if (port < hook->port) {
111 } else if (port > hook->port) {
125 int v3_hook_io_port(struct v3_vm_info * vm, uint16_t port,
126 int (*read)(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data),
127 int (*write)(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data),
129 struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
131 io_hook->port = port;
134 io_hook->read = &default_read;
136 io_hook->read = read;
140 io_hook->write = &default_write;
142 io_hook->write = write;
145 io_hook->priv_data = priv_data;
147 if (insert_io_hook(vm, io_hook)) {
148 PrintError("Could not insert IO hook for port %u (0x%x)\n", port, port);
153 if (vm->io_map.update_map) {
154 if (vm->io_map.update_map(vm, port,
155 ((read == NULL) ? 0 : 1),
156 ((write == NULL) ? 0 : 1)) == -1) {
157 PrintError("Could not update IO map for port %u (0x%x)\n", port, port);
167 static int free_hook(struct v3_vm_info * vm, struct v3_io_hook * hook) {
168 v3_rb_erase(&(hook->tree_node), &(vm->io_map.map));
170 if (vm->io_map.update_map) {
171 // set the arch map to default (this should be 1, 1)
172 vm->io_map.update_map(vm, hook->port, 0, 0);
180 int v3_unhook_io_port(struct v3_vm_info * vm, uint16_t port) {
181 struct v3_io_hook * hook = v3_get_io_hook(vm, port);
184 PrintError("Could not find port to unhook %u (0x%x)\n", port, port);
199 void v3_refresh_io_map(struct v3_vm_info * vm) {
200 struct v3_io_map * io_map = &(vm->io_map);
201 struct v3_io_hook * tmp = NULL;
203 if (io_map->update_map == NULL) {
204 PrintError("Trying to refresh an io map with no backend\n");
208 v3_rb_for_each_entry(tmp, &(io_map->map), tree_node) {
209 io_map->update_map(vm, tmp->port,
210 ((tmp->read == NULL) ? 0 : 1),
211 ((tmp->write == NULL) ? 0 : 1));
218 void v3_print_io_map(struct v3_vm_info * vm) {
219 struct v3_io_map * io_map = &(vm->io_map);
220 struct v3_io_hook * tmp_hook = NULL;
222 V3_Print("VMM IO Map\n");
224 v3_rb_for_each_entry(tmp_hook, &(io_map->map), tree_node) {
225 V3_Print("IO Port: %hu (Read=%p) (Write=%p)\n",
227 (void *)(tmp_hook->read), (void *)(tmp_hook->write));
234 * Write a byte to an I/O port.
236 void v3_outb(uint16_t port, uint8_t value) {
237 __asm__ __volatile__ (
240 : "a" (value), "Nd" (port)
245 * Read a byte from an I/O port.
247 uint8_t v3_inb(uint16_t port) {
250 __asm__ __volatile__ (
260 * Write a word to an I/O port.
262 void v3_outw(uint16_t port, uint16_t value) {
263 __asm__ __volatile__ (
266 : "a" (value), "Nd" (port)
271 * Read a word from an I/O port.
273 uint16_t v3_inw(uint16_t port) {
276 __asm__ __volatile__ (
286 * Write a double word to an I/O port.
288 void v3_outdw(uint16_t port, uint_t value) {
289 __asm__ __volatile__ (
292 : "a" (value), "Nd" (port)
297 * Read a double word from an I/O port.
299 uint_t v3_indw(uint16_t port) {
302 __asm__ __volatile__ (
315 static int default_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) {
317 v3_outb(port, *(uint8_t *)src);
318 } else if (length == 2) {
319 v3_outw(port, *(uint16_t *)src);
320 } else if (length == 4) {
321 v3_outdw(port, *(uint32_t *)src);
329 static int default_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data) {
331 *(uint8_t *)dst = v3_inb(port);
332 } else if (length == 2) {
333 *(uint16_t *)dst = v3_inw(port);
334 } else if (length == 4) {
335 *(uint32_t *)dst = v3_indw(port);