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 copyright tags
[palacios.git] / palacios / src / palacios / vmm_io.c
1 /* Northwestern University */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
3
4 #include <palacios/vmm_io.h>
5 #include <palacios/vmm_string.h>
6 #include <palacios/vmm.h>
7
8
9
10
11 #ifndef DEBUG_IO
12 #undef PrintDebug
13 #define PrintDebug(fmt, args...)
14 #endif
15
16
17 void init_vmm_io_map(struct guest_info * info) {
18   struct vmm_io_map * io_map = &(info->io_map);
19   io_map->num_ports = 0;
20   io_map->head = NULL;
21 }
22
23
24
25
26
27 static int add_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
28
29   if (!(io_map->head)) {
30     io_map->head = io_hook;
31     io_map->num_ports = 1;
32     return 0;
33   } else if (io_map->head->port > io_hook->port) {
34     io_hook->next = io_map->head;
35
36     io_map->head->prev = io_hook;
37     io_map->head = io_hook;
38     io_map->num_ports++;
39
40     return 0;
41   } else {
42     struct vmm_io_hook * tmp_hook = io_map->head;
43     
44     while ((tmp_hook->next)  && 
45            (tmp_hook->next->port <= io_hook->port)) {
46         tmp_hook = tmp_hook->next;
47     }
48     
49     if (tmp_hook->port == io_hook->port) {
50       //tmp_hook->read = io_hook->read;
51       //tmp_hook->write = io_hook->write;
52       //V3_Free(io_hook);
53       return -1;
54     } else {
55       io_hook->prev = tmp_hook;
56       io_hook->next = tmp_hook->next;
57
58       if (tmp_hook->next) {
59         tmp_hook->next->prev = io_hook;
60       }
61
62       tmp_hook->next = io_hook;
63
64       io_map->num_ports++;
65       return 0;
66     }
67   }
68   return -1;
69 }
70
71 static int remove_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
72   if (io_map->head == io_hook) {
73     io_map->head = io_hook->next;
74   } else if (io_hook->prev) {
75     io_hook->prev->next = io_hook->next;
76   } else {
77     return -1;
78     // data corruption failure
79   }
80   
81   if (io_hook->next) {
82     io_hook->next->prev = io_hook->prev;
83   }
84
85   io_map->num_ports--;
86
87   return 0;
88 }
89
90
91
92 /* FIX ME */
93 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
94   /*
95     
96   if (length == 1) {
97   __asm__ __volatile__ (
98   "outb %b0, %w1"
99   :
100   : "a" (*dst), "Nd" (port)
101   );
102   } else if (length == 2) {
103   __asm__ __volatile__ (
104   "outw %b0, %w1"
105   :
106   : "a" (*dst), "Nd" (port)
107   );
108   } else if (length == 4) {
109   __asm__ __volatile__ (
110   "outw %b0, %w1"
111   :
112   : "a" (*dst), "Nd" (port)
113   );
114   }
115   */
116   return 0;
117 }
118
119 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data)
120 {
121
122   /*    
123         uchar_t value;
124
125     __asm__ __volatile__ (
126         "inb %w1, %b0"
127         : "=a" (value)
128         : "Nd" (port)
129     );
130
131     return value;
132   */
133
134   return 0;
135 }
136
137 int v3_hook_io_port(struct guest_info * info, uint_t port, 
138                     int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
139                     int (*write)(ushort_t port, void * src, uint_t length, void * priv_data), 
140                     void * priv_data) {
141   struct vmm_io_map * io_map = &(info->io_map);
142   struct vmm_io_hook * io_hook = (struct vmm_io_hook *)V3_Malloc(sizeof(struct vmm_io_hook));
143
144   io_hook->port = port;
145
146   if (!read) {
147     io_hook->read = &default_read;
148   } else {
149     io_hook->read = read;
150   }
151
152   if (!write) {
153     io_hook->write = &default_write;
154   } else {
155     io_hook->write = write;
156   }
157
158   io_hook->next = NULL;
159   io_hook->prev = NULL;
160
161   io_hook->priv_data = priv_data;
162
163   if (add_io_hook(io_map, io_hook) != 0) {
164     V3_Free(io_hook);
165     return -1;
166   }
167
168   return 0;
169 }
170
171 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
172   struct vmm_io_map * io_map = &(info->io_map);
173   struct vmm_io_hook * hook = v3_get_io_hook(io_map, port);
174
175   if (hook == NULL) {
176     return -1;
177   }
178
179   remove_io_hook(io_map, hook);
180   return 0;
181 }
182
183
184 struct vmm_io_hook * v3_get_io_hook(struct vmm_io_map * io_map, uint_t port) {
185   struct vmm_io_hook * tmp_hook;
186   FOREACH_IO_HOOK(*io_map, tmp_hook) {
187     if (tmp_hook->port == port) {
188       return tmp_hook;
189     }
190   }
191   return NULL;
192 }
193
194
195
196 void PrintDebugIOMap(struct vmm_io_map * io_map) {
197   struct vmm_io_hook * iter = io_map->head;
198
199   PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
200
201   while (iter) {
202     PrintDebug("IO Port: %hu (Read=%x) (Write=%x)\n", iter->port, iter->read, iter->write);
203   }
204 }