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 new copyright and license
[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 DEBUG_IO
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32
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);
35
36
37 void init_vmm_io_map(struct guest_info * info) {
38   struct vmm_io_map * io_map = &(info->io_map);
39   io_map->num_ports = 0;
40   io_map->head = NULL;
41 }
42
43
44
45
46
47 static int add_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
48
49   if (!(io_map->head)) {
50     io_map->head = io_hook;
51     io_map->num_ports = 1;
52     return 0;
53   } else if (io_map->head->port > io_hook->port) {
54     io_hook->next = io_map->head;
55
56     io_map->head->prev = io_hook;
57     io_map->head = io_hook;
58     io_map->num_ports++;
59
60     return 0;
61   } else {
62     struct vmm_io_hook * tmp_hook = io_map->head;
63     
64     while ((tmp_hook->next)  && 
65            (tmp_hook->next->port <= io_hook->port)) {
66         tmp_hook = tmp_hook->next;
67     }
68     
69     if (tmp_hook->port == io_hook->port) {
70       //tmp_hook->read = io_hook->read;
71       //tmp_hook->write = io_hook->write;
72       //V3_Free(io_hook);
73       return -1;
74     } else {
75       io_hook->prev = tmp_hook;
76       io_hook->next = tmp_hook->next;
77
78       if (tmp_hook->next) {
79         tmp_hook->next->prev = io_hook;
80       }
81
82       tmp_hook->next = io_hook;
83
84       io_map->num_ports++;
85       return 0;
86     }
87   }
88   return -1;
89 }
90
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;
96   } else {
97     return -1;
98     // data corruption failure
99   }
100   
101   if (io_hook->next) {
102     io_hook->next->prev = io_hook->prev;
103   }
104
105   io_map->num_ports--;
106
107   return 0;
108 }
109
110
111
112
113
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), 
117                     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));
120
121   io_hook->port = port;
122
123   if (!read) {
124     io_hook->read = &default_read;
125   } else {
126     io_hook->read = read;
127   }
128
129   if (!write) {
130     io_hook->write = &default_write;
131   } else {
132     io_hook->write = write;
133   }
134
135   io_hook->next = NULL;
136   io_hook->prev = NULL;
137
138   io_hook->priv_data = priv_data;
139
140   if (add_io_hook(io_map, io_hook) != 0) {
141     V3_Free(io_hook);
142     return -1;
143   }
144
145   return 0;
146 }
147
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);
151
152   if (hook == NULL) {
153     return -1;
154   }
155
156   remove_io_hook(io_map, hook);
157   return 0;
158 }
159
160
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) {
165       return tmp_hook;
166     }
167   }
168   return NULL;
169 }
170
171
172
173 void PrintDebugIOMap(struct vmm_io_map * io_map) {
174   struct vmm_io_hook * iter = io_map->head;
175
176   PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
177
178   while (iter) {
179     PrintDebug("IO Port: %hu (Read=%x) (Write=%x)\n", iter->port, iter->read, iter->write);
180   }
181 }
182
183
184
185 /*
186  * Write a byte to an I/O port.
187  */
188 void v3_outb(ushort_t port, uchar_t value) {
189     __asm__ __volatile__ (
190         "outb %b0, %w1"
191         :
192         : "a" (value), "Nd" (port)
193     );
194 }
195
196 /*
197  * Read a byte from an I/O port.
198  */
199 uchar_t v3_inb(ushort_t port) {
200     uchar_t value;
201
202     __asm__ __volatile__ (
203         "inb %w1, %b0"
204         : "=a" (value)
205         : "Nd" (port)
206     );
207
208     return value;
209 }
210
211 /*
212  * Write a word to an I/O port.
213  */
214 void v3_outw(ushort_t port, ushort_t value) {
215     __asm__ __volatile__ (
216         "outw %w0, %w1"
217         :
218         : "a" (value), "Nd" (port)
219     );
220 }
221
222 /*
223  * Read a word from an I/O port.
224  */
225 ushort_t v3_inw(ushort_t port) {
226     ushort_t value;
227
228     __asm__ __volatile__ (
229         "inw %w1, %w0"
230         : "=a" (value)
231         : "Nd" (port)
232     );
233
234     return value;
235 }
236
237 /*
238  * Write a double word to an I/O port.
239  */
240 void v3_outdw(ushort_t port, uint_t value) {
241     __asm__ __volatile__ (
242         "outl %0, %1"
243         :
244         : "a" (value), "Nd" (port)
245     );
246 }
247
248 /*
249  * Read a double word from an I/O port.
250  */
251 uint_t v3_indw(ushort_t port) {
252     uint_t value;
253
254     __asm__ __volatile__ (
255         "inl %1, %0"
256         : "=a" (value)
257         : "Nd" (port)
258     );
259
260     return value;
261 }
262
263
264
265
266 /* FIX ME */
267 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
268   /*
269     
270   if (length == 1) {
271   __asm__ __volatile__ (
272   "outb %b0, %w1"
273   :
274   : "a" (*dst), "Nd" (port)
275   );
276   } else if (length == 2) {
277   __asm__ __volatile__ (
278   "outw %b0, %w1"
279   :
280   : "a" (*dst), "Nd" (port)
281   );
282   } else if (length == 4) {
283   __asm__ __volatile__ (
284   "outw %b0, %w1"
285   :
286   : "a" (*dst), "Nd" (port)
287   );
288   }
289   */
290   return 0;
291 }
292
293 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
294
295   /*    
296         uchar_t value;
297
298     __asm__ __volatile__ (
299         "inb %w1, %b0"
300         : "=a" (value)
301         : "Nd" (port)
302     );
303
304     return value;
305   */
306
307   return 0;
308 }