Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added pci passthrough and a few other things
[palacios.git] / palacios / src / palacios / vmm_io.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm_io.h>
21 #include <palacios/vmm_string.h>
22 #include <palacios/vmm.h>
23
24
25
26
27 #ifndef CONFIG_DEBUG_IO
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32
33 static int default_write(uint16_t port, void *src, uint_t length, void * priv_data);
34 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data);
35
36
37 void v3_init_io_map(struct guest_info * info) {
38
39   info->io_map.map.rb_node = NULL;
40   info->io_map.arch_data = NULL;
41   info->io_map.update_map = NULL;
42
43 }
44
45
46
47
48 static inline struct v3_io_hook * __insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
49   struct rb_node ** p = &(info->io_map.map.rb_node);
50   struct rb_node * parent = NULL;
51   struct v3_io_hook * tmp_hook = NULL;
52
53   while (*p) {
54     parent = *p;
55     tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
56
57     if (hook->port < tmp_hook->port) {
58       p = &(*p)->rb_left;
59     } else if (hook->port > tmp_hook->port) {
60       p = &(*p)->rb_right;
61     } else {
62       return tmp_hook;
63     }
64   }
65   rb_link_node(&(hook->tree_node), parent, p);
66
67   return NULL;
68 }
69
70
71 static inline struct v3_io_hook * insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
72   struct v3_io_hook * ret;
73
74   if ((ret = __insert_io_hook(info, hook))) {
75     return ret;
76   }
77
78   v3_rb_insert_color(&(hook->tree_node), &(info->io_map.map));
79
80   return NULL;
81 }
82
83
84 struct v3_io_hook * v3_get_io_hook(struct guest_info * info, uint16_t port) {
85   struct rb_node * n = info->io_map.map.rb_node;
86   struct v3_io_hook * hook = NULL;
87
88   while (n) {
89     hook = rb_entry(n, struct v3_io_hook, tree_node);
90     
91     if (port < hook->port) {
92       n = n->rb_left;
93     } else if (port > hook->port) {
94       n = n->rb_right;
95     } else {
96       return hook;
97     }
98   }
99
100   return NULL;
101 }
102
103
104
105
106
107
108 int v3_hook_io_port(struct guest_info * info, uint16_t port, 
109                     int (*read)(uint16_t port, void * dst, uint_t length, void * priv_data),
110                     int (*write)(uint16_t port, void * src, uint_t length, void * priv_data), 
111                     void * priv_data) {
112   struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
113
114   io_hook->port = port;
115
116   if (!read) {
117     io_hook->read = &default_read;
118   } else {
119     io_hook->read = read;
120   }
121
122   if (!write) {
123     io_hook->write = &default_write;
124   } else {
125     io_hook->write = write;
126   }
127
128
129   io_hook->priv_data = priv_data;
130
131   if (insert_io_hook(info, io_hook)) {
132       PrintError("Could not insert IO hook for port %u (0x%x)\n", port, port);
133       V3_Free(io_hook);
134       return -1;
135   }
136
137
138   if (info->io_map.update_map(info, port, 
139                               ((read == NULL) ? 0 : 1), 
140                               ((write == NULL) ? 0 : 1)) == -1) {
141       PrintError("Could not update IO map for port %u (0x%x)\n", port, port);
142       V3_Free(io_hook);
143       return -1;
144   }
145
146
147   return 0;
148 }
149
150 int v3_unhook_io_port(struct guest_info * info, uint16_t port) {
151     struct v3_io_hook * hook = v3_get_io_hook(info, port);
152
153     if (hook == NULL) {
154         PrintError("Could not find port to unhook %u (0x%x)\n", port, port);
155         return -1;
156     }
157
158     v3_rb_erase(&(hook->tree_node), &(info->io_map.map));
159
160     // set the arch map to default (this should be 1, 1)
161     info->io_map.update_map(info, port, 0, 0);
162
163     V3_Free(hook);
164
165     return 0;
166 }
167
168
169
170
171
172
173 void v3_print_io_map(struct guest_info * info) {
174     struct v3_io_hook * tmp_hook = NULL;
175     struct rb_node * node = v3_rb_first(&(info->io_map.map));
176
177     V3_Print("VMM IO Map\n");
178
179     do {
180         tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
181
182         V3_Print("IO Port: %hu (Read=%p) (Write=%p)\n", 
183                  tmp_hook->port, 
184                  (void *)(tmp_hook->read), (void *)(tmp_hook->write));
185     } while ((node = v3_rb_next(node)));
186 }
187
188
189
190 /*
191  * Write a byte to an I/O port.
192  */
193 void v3_outb(uint16_t port, uint8_t value) {
194     __asm__ __volatile__ (
195         "outb %b0, %w1"
196         :
197         : "a" (value), "Nd" (port)
198     );
199 }
200
201 /*
202  * Read a byte from an I/O port.
203  */
204 uint8_t v3_inb(uint16_t port) {
205     uint8_t value;
206
207     __asm__ __volatile__ (
208         "inb %w1, %b0"
209         : "=a" (value)
210         : "Nd" (port)
211     );
212
213     return value;
214 }
215
216 /*
217  * Write a word to an I/O port.
218  */
219 void v3_outw(uint16_t port, uint16_t value) {
220     __asm__ __volatile__ (
221         "outw %w0, %w1"
222         :
223         : "a" (value), "Nd" (port)
224     );
225 }
226
227 /*
228  * Read a word from an I/O port.
229  */
230 uint16_t v3_inw(uint16_t port) {
231     uint16_t value;
232
233     __asm__ __volatile__ (
234         "inw %w1, %w0"
235         : "=a" (value)
236         : "Nd" (port)
237     );
238
239     return value;
240 }
241
242 /*
243  * Write a double word to an I/O port.
244  */
245 void v3_outdw(uint16_t port, uint_t value) {
246     __asm__ __volatile__ (
247         "outl %0, %1"
248         :
249         : "a" (value), "Nd" (port)
250     );
251 }
252
253 /*
254  * Read a double word from an I/O port.
255  */
256 uint_t v3_indw(uint16_t port) {
257     uint_t value;
258
259     __asm__ __volatile__ (
260         "inl %1, %0"
261         : "=a" (value)
262         : "Nd" (port)
263     );
264
265     return value;
266 }
267
268
269
270
271 /* FIX ME */
272 static int default_write(uint16_t port, void * src, uint_t length, void * priv_data) {
273     if (length == 1) {
274         v3_outb(port, *(uint8_t *)src);
275     } else if (length == 2) {
276         v3_outw(port, *(uint16_t *)src);
277     } else if (length == 4) {
278         v3_outdw(port, *(uint32_t *)src);
279     } else {
280         return -1;
281     }
282     
283     return length;
284 }
285
286 static int default_read(uint16_t port, void * dst, uint_t length, void * priv_data) {
287     if (length == 1) {
288         *(uint8_t *)dst = v3_inb(port);
289     } else if (length == 2) {
290         *(uint16_t *)dst = v3_inw(port);
291     } else if (length == 4) {
292         *(uint32_t *)dst = v3_indw(port);
293     } else {
294         return -1;
295     }
296
297     return length;
298 }