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".
23 #include <palacios/svm_io.h>
24 #include <palacios/vmm_io.h>
25 #include <palacios/vmm_ctrl_regs.h>
26 #include <palacios/vmm_decoder.h>
27 #include <palacios/vm_guest_mem.h>
31 #define PrintDebug(fmt, args...)
38 // This should package up an IO request and call vmm_handle_io
39 int v3_handle_svm_io_in(struct guest_info * info) {
40 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
41 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
42 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
44 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
48 PrintError("Hook Not present for in on port %x\n", io_info->port);
49 // error, we should not have exited on this port
56 } else if (io_info->sz16) {
58 } else if (io_info->sz32) {
62 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
64 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
65 // not sure how we handle errors.....
66 PrintError("Read Failure for in on port %x\n", io_info->port);
70 info->rip = ctrl_area->exit_info2;
79 /* We might not handle wrap around of the RDI register correctly...
80 * In that if we do wrap around the effect will manifest in the higher bits of the register
82 int v3_handle_svm_io_ins(struct guest_info * info) {
83 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
84 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
86 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
88 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
97 // This is kind of hacky...
98 // direction can equal either 1 or -1
99 // We will multiply the final added offset by this value to go the correct direction
101 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
108 PrintError("Hook Not present for ins on port %x\n", io_info->port);
109 // error, we should not have exited on this port
113 struct v3_segment *theseg = &(info->segments.es); // default is ES
117 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
118 PrintError("Can't access instruction\n");
122 while (is_prefix_byte(*((char*)inst_ptr))) {
123 switch (*((char*)inst_ptr)) {
124 case PREFIX_CS_OVERRIDE:
125 theseg = &(info->segments.cs);
127 case PREFIX_SS_OVERRIDE:
128 theseg = &(info->segments.ss);
130 case PREFIX_DS_OVERRIDE:
131 theseg = &(info->segments.ds);
133 case PREFIX_ES_OVERRIDE:
134 theseg = &(info->segments.es);
136 case PREFIX_FS_OVERRIDE:
137 theseg = &(info->segments.fs);
139 case PREFIX_GS_OVERRIDE:
140 theseg = &(info->segments.gs);
149 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
153 } else if (io_info->sz16) {
155 } else if (io_info->sz32) {
158 PrintError("io_info Invalid Size\n");
163 if (io_info->addr16) {
165 } else if (io_info->addr32) {
167 } else if (io_info->addr64) {
168 mask = 0xffffffffffffffffLL;
170 // This value should be set depending on the host register size...
171 mask = get_gpr_mask(info);
173 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
174 // PrintDebug("INS Aborted... Check implementation\n");
179 // rep_num = info->vm_regs.rcx & mask;
180 rep_num = info->vm_regs.rcx;
184 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
186 while (rep_num > 0) {
188 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
190 PrintDebug("Writing 0x%x\n", dst_addr);
192 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
193 // either page fault or gpf...
194 PrintError("Could not convert Guest VA to host VA\n");
198 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
199 // not sure how we handle errors.....
200 PrintError("Read Failure for ins on port %x\n", io_info->port);
204 info->vm_regs.rdi += read_size * direction;
213 info->rip = ctrl_area->exit_info2;
218 int v3_handle_svm_io_out(struct guest_info * info) {
219 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
220 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
221 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
223 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
227 PrintError("Hook Not present for out on port %x\n", io_info->port);
228 // error, we should not have exited on this port
235 } else if (io_info->sz16) {
237 } else if (io_info->sz32) {
241 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
243 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
244 // not sure how we handle errors.....
245 PrintError("Write Failure for out on port %x\n", io_info->port);
249 info->rip = ctrl_area->exit_info2;
255 /* We might not handle wrap around of the RSI register correctly...
256 * In that if we do wrap around the effect will manifest in the higher bits of the register
259 int v3_handle_svm_io_outs(struct guest_info * info) {
260 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
261 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
264 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
266 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
275 // This is kind of hacky...
276 // direction can equal either 1 or -1
277 // We will multiply the final added offset by this value to go the correct direction
279 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
286 PrintError("Hook Not present for outs on port %x\n", io_info->port);
287 // error, we should not have exited on this port
291 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
295 } else if (io_info->sz16) {
297 } else if (io_info->sz32) {
302 if (io_info->addr16) {
304 } else if (io_info->addr32) {
306 } else if (io_info->addr64) {
307 mask = 0xffffffffffffffffLL;
309 // This value should be set depending on the host register size...
310 mask = get_gpr_mask(info);
312 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
313 // PrintDebug("INS Aborted... Check implementation\n");
315 // should never happen
316 //PrintDebug("Invalid Address length\n");
321 rep_num = info->vm_regs.rcx & mask;
324 struct v3_segment *theseg = &(info->segments.es); // default is ES
328 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
329 PrintError("Can't access instruction\n");
333 while (is_prefix_byte(*((char*)inst_ptr))) {
334 switch (*((char*)inst_ptr)) {
335 case PREFIX_CS_OVERRIDE:
336 theseg = &(info->segments.cs);
338 case PREFIX_SS_OVERRIDE:
339 theseg = &(info->segments.ss);
341 case PREFIX_DS_OVERRIDE:
342 theseg = &(info->segments.ds);
344 case PREFIX_ES_OVERRIDE:
345 theseg = &(info->segments.es);
347 case PREFIX_FS_OVERRIDE:
348 theseg = &(info->segments.fs);
350 case PREFIX_GS_OVERRIDE:
351 theseg = &(info->segments.gs);
359 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
361 while (rep_num > 0) {
366 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
368 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
369 // either page fault or gpf...
372 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
373 // not sure how we handle errors.....
374 PrintError("Write Failure for outs on port %x\n", io_info->port);
378 info->vm_regs.rsi += write_size * direction;
387 info->rip = ctrl_area->exit_info2;