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.


First cut at a keyboard device (partially done)
[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       //V3_Free(io_hook);
44       return -1;
45     } else {
46       io_hook->prev = tmp_hook;
47       io_hook->next = tmp_hook->next;
48
49       if (tmp_hook->next) {
50         tmp_hook->next->prev = io_hook;
51       }
52
53       tmp_hook->next = io_hook;
54
55       io_map->num_ports++;
56       return 0;
57     }
58   }
59   return -1;
60 }
61
62 int remove_io_hook(vmm_io_map_t * io_map, vmm_io_hook_t * io_hook) {
63   if (io_map->head == io_hook) {
64     io_map->head = io_hook->next;
65   } else if (io_hook->prev) {
66     io_hook->prev->next = io_hook->next;
67   } else {
68     return -1;
69     // data corruption failure
70   }
71   
72   if (io_hook->next) {
73     io_hook->next->prev = io_hook->prev;
74   }
75
76   io_map->num_ports--;
77
78   return 0;
79 }
80
81
82
83 /* FIX ME */
84 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
85   /*
86     
87   if (length == 1) {
88   __asm__ __volatile__ (
89   "outb %b0, %w1"
90   :
91   : "a" (*dst), "Nd" (port)
92   );
93   } else if (length == 2) {
94   __asm__ __volatile__ (
95   "outw %b0, %w1"
96   :
97   : "a" (*dst), "Nd" (port)
98   );
99   } else if (length == 4) {
100   __asm__ __volatile__ (
101   "outw %b0, %w1"
102   :
103   : "a" (*dst), "Nd" (port)
104   );
105   }
106   */
107   return 0;
108 }
109
110 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data)
111 {
112
113   /*    
114         uchar_t value;
115
116     __asm__ __volatile__ (
117         "inb %w1, %b0"
118         : "=a" (value)
119         : "Nd" (port)
120     );
121
122     return value;
123   */
124
125   return 0;
126 }
127
128 int hook_io_port(vmm_io_map_t * io_map, uint_t port, 
129                  int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
130                  int (*write)(ushort_t port, void * src, uint_t length, void * priv_data), 
131                  void * priv_data) {
132   vmm_io_hook_t * io_hook = os_hooks->malloc(sizeof(vmm_io_hook_t));
133
134   io_hook->port = port;
135
136   if (!read) {
137     io_hook->read = &default_read;
138   } else {
139     io_hook->read = read;
140   }
141
142   if (!write) {
143     io_hook->write = &default_write;
144   } else {
145     io_hook->write = write;
146   }
147
148   io_hook->next = NULL;
149   io_hook->prev = NULL;
150
151   io_hook->priv_data = priv_data;
152
153   if (add_io_hook(io_map, io_hook) != 0) {
154     V3_Free(io_hook);
155     return -1;
156   }
157
158   return 0;
159 }
160
161 int unhook_io_port(vmm_io_map_t * io_map, uint_t port) {
162   vmm_io_hook_t * hook = get_io_hook(io_map, port);
163
164   if (hook == NULL) {
165     return -1;
166   }
167
168   remove_io_hook(io_map, hook);
169   return 0;
170 }
171
172
173 vmm_io_hook_t * get_io_hook(vmm_io_map_t * io_map, uint_t port) {
174   vmm_io_hook_t * tmp_hook;
175   FOREACH_IO_HOOK(*io_map, tmp_hook) {
176     if (tmp_hook->port == port) {
177       return tmp_hook;
178     }
179   }
180   return NULL;
181 }
182
183
184
185 void PrintDebugIOMap(vmm_io_map_t * io_map) {
186   vmm_io_hook_t * iter = io_map->head;
187
188   PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
189
190   while (iter) {
191     PrintDebug("IO Port: %hu (Read=%x) (Write=%x)\n", iter->port, iter->read, iter->write);
192   }
193 }