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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/svm_io.h>
21 #include <palacios/vmm_io.h>
22 #include <palacios/vmm_ctrl_regs.h>
23 #include <palacios/vmm_decoder.h>
24 #include <palacios/vm_guest_mem.h>
26 #ifndef CONFIG_DEBUG_IO
28 #define PrintDebug(fmt, args...)
32 static int update_map(struct guest_info * info, uint16_t port, int hook_read, int hook_write) {
33 uchar_t * bitmap = (uint8_t *)(info->io_map.arch_data);;
37 if ((hook_read == 0) && (hook_write == 0)) {
38 *(bitmap + major) &= ~(0x1 << minor);
40 *(bitmap + major) |= (0x1 << minor);
47 int v3_init_svm_io_map(struct guest_info * info) {
48 info->io_map.update_map = update_map;
50 info->io_map.arch_data = V3_VAddr(V3_AllocPages(3));
51 memset(info->io_map.arch_data, 0, PAGE_SIZE_4KB * 3);
54 v3_refresh_io_map(info);
61 // This should package up an IO request and call vmm_handle_io
62 int v3_handle_svm_io_in(struct guest_info * info) {
63 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
64 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
65 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
67 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
71 PrintError("Hook Not present for in on port 0x%x\n", io_info->port);
72 // error, we should not have exited on this port
79 } else if (io_info->sz16) {
81 } else if (io_info->sz32) {
85 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
87 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
88 // not sure how we handle errors.....
89 PrintError("Read Failure for in on port 0x%x\n", io_info->port);
93 info->rip = ctrl_area->exit_info2;
102 /* We might not handle wrap around of the RDI register correctly...
103 * In that if we do wrap around the effect will manifest in the higher bits of the register
105 int v3_handle_svm_io_ins(struct guest_info * info) {
106 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
107 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
109 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
111 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
116 struct v3_segment * theseg = &(info->segments.es); // default is ES
120 // This is kind of hacky...
121 // direction can equal either 1 or -1
122 // We will multiply the final added offset by this value to go the correct direction
124 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
132 PrintError("Hook Not present for ins on port 0x%x\n", io_info->port);
133 // error, we should not have exited on this port
139 if (guest_va_to_host_va(info, get_addr_linear(info, info->rip, &(info->segments.cs)), &inst_ptr) == -1) {
140 PrintError("Can't access instruction\n");
144 while (is_prefix_byte(*((char *)inst_ptr))) {
145 switch (*((char *)inst_ptr)) {
146 case PREFIX_CS_OVERRIDE:
147 theseg = &(info->segments.cs);
149 case PREFIX_SS_OVERRIDE:
150 theseg = &(info->segments.ss);
152 case PREFIX_DS_OVERRIDE:
153 theseg = &(info->segments.ds);
155 case PREFIX_ES_OVERRIDE:
156 theseg = &(info->segments.es);
158 case PREFIX_FS_OVERRIDE:
159 theseg = &(info->segments.fs);
161 case PREFIX_GS_OVERRIDE:
162 theseg = &(info->segments.gs);
171 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
175 } else if (io_info->sz16) {
177 } else if (io_info->sz32) {
180 PrintError("io_info Invalid Size\n");
185 if (io_info->addr16) {
187 } else if (io_info->addr32) {
189 } else if (io_info->addr64) {
190 mask = 0xffffffffffffffffLL;
192 // This value should be set depending on the host register size...
193 mask = get_gpr_mask(info);
195 PrintDebug("INS io_info invalid address size, mask=0x%p, io_info=0x%p\n",
196 (void *)(addr_t)mask, (void *)(addr_t)(io_info));
197 // PrintDebug("INS Aborted... Check implementation\n");
202 rep_num = info->vm_regs.rcx & mask;
203 //rep_num = info->vm_regs.rcx;
207 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
209 while (rep_num > 0) {
211 dst_addr = get_addr_linear(info, (info->vm_regs.rdi & mask), theseg);
213 // PrintDebug("Writing 0x%p\n", (void *)dst_addr);
215 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
216 // either page fault or gpf...
217 PrintError("Could not convert Guest VA to host VA\n");
221 if (hook->read(io_info->port, (char *)host_addr, read_size, hook->priv_data) != read_size) {
222 // not sure how we handle errors.....
223 PrintError("Read Failure for ins on port 0x%x\n", io_info->port);
227 info->vm_regs.rdi += (read_size * direction);
237 info->rip = ctrl_area->exit_info2;
242 int v3_handle_svm_io_out(struct guest_info * info) {
243 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
244 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
245 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
247 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
251 PrintError("Hook Not present for out on port 0x%x\n", io_info->port);
252 // error, we should not have exited on this port
259 } else if (io_info->sz16) {
261 } else if (io_info->sz32) {
265 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
267 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
268 // not sure how we handle errors.....
269 PrintError("Write Failure for out on port 0x%x\n", io_info->port);
273 info->rip = ctrl_area->exit_info2;
279 /* We might not handle wrap around of the RSI register correctly...
280 * In that if we do wrap around the effect will manifest in the higher bits of the register
283 int v3_handle_svm_io_outs(struct guest_info * info) {
284 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
285 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
288 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
290 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
296 struct v3_segment * theseg = &(info->segments.es); // default is ES
298 // This is kind of hacky...
299 // direction can equal either 1 or -1
300 // We will multiply the final added offset by this value to go the correct direction
302 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
310 PrintError("Hook Not present for outs on port 0x%x\n", io_info->port);
311 // error, we should not have exited on this port
315 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
319 } else if (io_info->sz16) {
321 } else if (io_info->sz32) {
326 if (io_info->addr16) {
328 } else if (io_info->addr32) {
330 } else if (io_info->addr64) {
331 mask = 0xffffffffffffffffLL;
333 // This value should be set depending on the host register size...
334 mask = get_gpr_mask(info);
336 PrintDebug("OUTS io_info invalid address size, mask=0%p, io_info=0x%p\n",
337 (void *)(addr_t)mask, (void *)(addr_t)io_info);
338 // PrintDebug("INS Aborted... Check implementation\n");
340 // should never happen
341 //PrintDebug("Invalid Address length\n");
346 rep_num = info->vm_regs.rcx & mask;
352 if (guest_va_to_host_va(info, get_addr_linear(info, info->rip, &(info->segments.cs)), &inst_ptr) == -1) {
353 PrintError("Can't access instruction\n");
357 while (is_prefix_byte(*((char *)inst_ptr))) {
358 switch (*((char *)inst_ptr)) {
359 case PREFIX_CS_OVERRIDE:
360 theseg = &(info->segments.cs);
362 case PREFIX_SS_OVERRIDE:
363 theseg = &(info->segments.ss);
365 case PREFIX_DS_OVERRIDE:
366 theseg = &(info->segments.ds);
368 case PREFIX_ES_OVERRIDE:
369 theseg = &(info->segments.es);
371 case PREFIX_FS_OVERRIDE:
372 theseg = &(info->segments.fs);
374 case PREFIX_GS_OVERRIDE:
375 theseg = &(info->segments.gs);
383 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
385 while (rep_num > 0) {
388 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
390 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
391 // either page fault or gpf...
394 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
395 // not sure how we handle errors.....
396 PrintError("Write Failure for outs on port 0x%x\n", io_info->port);
400 info->vm_regs.rsi += write_size * direction;
410 info->rip = ctrl_area->exit_info2;