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