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_vmm_io_map(struct guest_info * info) {
38 struct vmm_io_map * io_map = &(info->io_map);
39 io_map->num_ports = 0;
47 static int add_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
49 if (!(io_map->head)) {
50 io_map->head = io_hook;
51 io_map->num_ports = 1;
53 } else if (io_map->head->port > io_hook->port) {
54 io_hook->next = io_map->head;
56 io_map->head->prev = io_hook;
57 io_map->head = io_hook;
62 struct vmm_io_hook * tmp_hook = io_map->head;
64 while ((tmp_hook->next) &&
65 (tmp_hook->next->port <= io_hook->port)) {
66 tmp_hook = tmp_hook->next;
69 if (tmp_hook->port == io_hook->port) {
70 //tmp_hook->read = io_hook->read;
71 //tmp_hook->write = io_hook->write;
75 io_hook->prev = tmp_hook;
76 io_hook->next = tmp_hook->next;
79 tmp_hook->next->prev = io_hook;
82 tmp_hook->next = io_hook;
91 static int remove_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
92 if (io_map->head == io_hook) {
93 io_map->head = io_hook->next;
94 } else if (io_hook->prev) {
95 io_hook->prev->next = io_hook->next;
98 // data corruption failure
102 io_hook->next->prev = io_hook->prev;
114 int v3_hook_io_port(struct guest_info * info, uint_t port,
115 int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
116 int (*write)(ushort_t port, void * src, uint_t length, void * priv_data),
118 struct vmm_io_map * io_map = &(info->io_map);
119 struct vmm_io_hook * io_hook = (struct vmm_io_hook *)V3_Malloc(sizeof(struct vmm_io_hook));
121 io_hook->port = port;
124 io_hook->read = &default_read;
126 io_hook->read = read;
130 io_hook->write = &default_write;
132 io_hook->write = write;
135 io_hook->next = NULL;
136 io_hook->prev = NULL;
138 io_hook->priv_data = priv_data;
140 if (add_io_hook(io_map, io_hook) != 0) {
148 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
149 struct vmm_io_map * io_map = &(info->io_map);
150 struct vmm_io_hook * hook = v3_get_io_hook(io_map, port);
156 remove_io_hook(io_map, hook);
161 struct vmm_io_hook * v3_get_io_hook(struct vmm_io_map * io_map, uint_t port) {
162 struct vmm_io_hook * tmp_hook;
163 FOREACH_IO_HOOK(*io_map, tmp_hook) {
164 if (tmp_hook->port == port) {
173 void v3_print_io_map(struct vmm_io_map * io_map) {
174 struct vmm_io_hook * iter = io_map->head;
176 PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
179 PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n",
181 (void *)(iter->read), (void *)(iter->write));
188 * Write a byte to an I/O port.
190 void v3_outb(ushort_t port, uchar_t value) {
191 __asm__ __volatile__ (
194 : "a" (value), "Nd" (port)
199 * Read a byte from an I/O port.
201 uchar_t v3_inb(ushort_t port) {
204 __asm__ __volatile__ (
214 * Write a word to an I/O port.
216 void v3_outw(ushort_t port, ushort_t value) {
217 __asm__ __volatile__ (
220 : "a" (value), "Nd" (port)
225 * Read a word from an I/O port.
227 ushort_t v3_inw(ushort_t port) {
230 __asm__ __volatile__ (
240 * Write a double word to an I/O port.
242 void v3_outdw(ushort_t port, uint_t value) {
243 __asm__ __volatile__ (
246 : "a" (value), "Nd" (port)
251 * Read a double word from an I/O port.
253 uint_t v3_indw(ushort_t port) {
256 __asm__ __volatile__ (
269 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
273 __asm__ __volatile__ (
276 : "a" (*dst), "Nd" (port)
278 } else if (length == 2) {
279 __asm__ __volatile__ (
282 : "a" (*dst), "Nd" (port)
284 } else if (length == 4) {
285 __asm__ __volatile__ (
288 : "a" (*dst), "Nd" (port)
295 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
300 __asm__ __volatile__ (