1 #include <palacios/vmm_io.h>
2 #include <palacios/vmm_string.h>
3 #include <palacios/vmm.h>
10 #define PrintDebug(fmt, args...)
14 void init_vmm_io_map(struct guest_info * info) {
15 struct vmm_io_map * io_map = &(info->io_map);
16 io_map->num_ports = 0;
24 static int add_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
26 if (!(io_map->head)) {
27 io_map->head = io_hook;
28 io_map->num_ports = 1;
30 } else if (io_map->head->port > io_hook->port) {
31 io_hook->next = io_map->head;
33 io_map->head->prev = io_hook;
34 io_map->head = io_hook;
39 struct vmm_io_hook * tmp_hook = io_map->head;
41 while ((tmp_hook->next) &&
42 (tmp_hook->next->port <= io_hook->port)) {
43 tmp_hook = tmp_hook->next;
46 if (tmp_hook->port == io_hook->port) {
47 //tmp_hook->read = io_hook->read;
48 //tmp_hook->write = io_hook->write;
52 io_hook->prev = tmp_hook;
53 io_hook->next = tmp_hook->next;
56 tmp_hook->next->prev = io_hook;
59 tmp_hook->next = io_hook;
68 static int remove_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
69 if (io_map->head == io_hook) {
70 io_map->head = io_hook->next;
71 } else if (io_hook->prev) {
72 io_hook->prev->next = io_hook->next;
75 // data corruption failure
79 io_hook->next->prev = io_hook->prev;
90 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
94 __asm__ __volatile__ (
97 : "a" (*dst), "Nd" (port)
99 } else if (length == 2) {
100 __asm__ __volatile__ (
103 : "a" (*dst), "Nd" (port)
105 } else if (length == 4) {
106 __asm__ __volatile__ (
109 : "a" (*dst), "Nd" (port)
116 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data)
122 __asm__ __volatile__ (
134 int v3_hook_io_port(struct guest_info * info, uint_t port,
135 int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
136 int (*write)(ushort_t port, void * src, uint_t length, void * priv_data),
138 struct vmm_io_map * io_map = &(info->io_map);
139 struct vmm_io_hook * io_hook = (struct vmm_io_hook *)V3_Malloc(sizeof(struct vmm_io_hook));
141 io_hook->port = port;
144 io_hook->read = &default_read;
146 io_hook->read = read;
150 io_hook->write = &default_write;
152 io_hook->write = write;
155 io_hook->next = NULL;
156 io_hook->prev = NULL;
158 io_hook->priv_data = priv_data;
160 if (add_io_hook(io_map, io_hook) != 0) {
168 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
169 struct vmm_io_map * io_map = &(info->io_map);
170 struct vmm_io_hook * hook = v3_get_io_hook(io_map, port);
176 remove_io_hook(io_map, hook);
181 struct vmm_io_hook * v3_get_io_hook(struct vmm_io_map * io_map, uint_t port) {
182 struct vmm_io_hook * tmp_hook;
183 FOREACH_IO_HOOK(*io_map, tmp_hook) {
184 if (tmp_hook->port == port) {
193 void PrintDebugIOMap(struct vmm_io_map * io_map) {
194 struct vmm_io_hook * iter = io_map->head;
196 PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
199 PrintDebug("IO Port: %hu (Read=%x) (Write=%x)\n", iter->port, iter->read, iter->write);