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...)
35 // This should package up an IO request and call vmm_handle_io
36 int handle_svm_io_in(struct guest_info * info) {
37 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
38 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
39 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
41 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
45 PrintError("Hook Not present for in on port %x\n", io_info->port);
46 // error, we should not have exited on this port
53 } else if (io_info->sz16) {
55 } else if (io_info->sz32) {
59 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
61 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
62 // not sure how we handle errors.....
63 PrintError("Read Failure for in on port %x\n", io_info->port);
67 info->rip = ctrl_area->exit_info2;
76 /* We might not handle wrap around of the RDI register correctly...
77 * In that if we do wrap around the effect will manifest in the higher bits of the register
79 int handle_svm_io_ins(struct guest_info * info) {
80 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
81 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
83 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
85 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
94 // This is kind of hacky...
95 // direction can equal either 1 or -1
96 // We will multiply the final added offset by this value to go the correct direction
98 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
105 PrintError("Hook Not present for ins on port %x\n", io_info->port);
106 // error, we should not have exited on this port
110 struct v3_segment *theseg = &(info->segments.es); // default is ES
114 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
115 PrintError("Can't access instruction\n");
119 while (is_prefix_byte(*((char*)inst_ptr))) {
120 switch (*((char*)inst_ptr)) {
121 case PREFIX_CS_OVERRIDE:
122 theseg = &(info->segments.cs);
124 case PREFIX_SS_OVERRIDE:
125 theseg = &(info->segments.ss);
127 case PREFIX_DS_OVERRIDE:
128 theseg = &(info->segments.ds);
130 case PREFIX_ES_OVERRIDE:
131 theseg = &(info->segments.es);
133 case PREFIX_FS_OVERRIDE:
134 theseg = &(info->segments.fs);
136 case PREFIX_GS_OVERRIDE:
137 theseg = &(info->segments.gs);
146 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
150 } else if (io_info->sz16) {
152 } else if (io_info->sz32) {
155 PrintError("io_info Invalid Size\n");
160 if (io_info->addr16) {
162 } else if (io_info->addr32) {
164 } else if (io_info->addr64) {
165 mask = 0xffffffffffffffffLL;
167 // This value should be set depending on the host register size...
168 mask = get_gpr_mask(info);
170 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
171 // PrintDebug("INS Aborted... Check implementation\n");
176 // rep_num = info->vm_regs.rcx & mask;
177 rep_num = info->vm_regs.rcx;
181 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
183 while (rep_num > 0) {
185 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
187 PrintDebug("Writing 0x%x\n", dst_addr);
189 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
190 // either page fault or gpf...
191 PrintError("Could not convert Guest VA to host VA\n");
195 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
196 // not sure how we handle errors.....
197 PrintError("Read Failure for ins on port %x\n", io_info->port);
201 info->vm_regs.rdi += read_size * direction;
210 info->rip = ctrl_area->exit_info2;
215 int handle_svm_io_out(struct guest_info * info) {
216 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
217 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
218 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
220 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
221 uint_t write_size = 0;
224 PrintError("Hook Not present for out on port %x\n", io_info->port);
225 // error, we should not have exited on this port
232 } else if (io_info->sz16) {
234 } else if (io_info->sz32) {
238 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
240 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
241 // not sure how we handle errors.....
242 PrintError("Write Failure for out on port %x\n", io_info->port);
246 info->rip = ctrl_area->exit_info2;
252 /* We might not handle wrap around of the RSI register correctly...
253 * In that if we do wrap around the effect will manifest in the higher bits of the register
256 int handle_svm_io_outs(struct guest_info * info) {
257 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
258 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
261 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
263 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
264 uint_t write_size = 0;
272 // This is kind of hacky...
273 // direction can equal either 1 or -1
274 // We will multiply the final added offset by this value to go the correct direction
276 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
283 PrintError("Hook Not present for outs on port %x\n", io_info->port);
284 // error, we should not have exited on this port
288 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
292 } else if (io_info->sz16) {
294 } else if (io_info->sz32) {
299 if (io_info->addr16) {
301 } else if (io_info->addr32) {
303 } else if (io_info->addr64) {
304 mask = 0xffffffffffffffffLL;
306 // This value should be set depending on the host register size...
307 mask = get_gpr_mask(info);
309 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
310 // PrintDebug("INS Aborted... Check implementation\n");
312 // should never happen
313 //PrintDebug("Invalid Address length\n");
318 rep_num = info->vm_regs.rcx & mask;
321 struct v3_segment *theseg = &(info->segments.es); // default is ES
325 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
326 PrintError("Can't access instruction\n");
330 while (is_prefix_byte(*((char*)inst_ptr))) {
331 switch (*((char*)inst_ptr)) {
332 case PREFIX_CS_OVERRIDE:
333 theseg = &(info->segments.cs);
335 case PREFIX_SS_OVERRIDE:
336 theseg = &(info->segments.ss);
338 case PREFIX_DS_OVERRIDE:
339 theseg = &(info->segments.ds);
341 case PREFIX_ES_OVERRIDE:
342 theseg = &(info->segments.es);
344 case PREFIX_FS_OVERRIDE:
345 theseg = &(info->segments.fs);
347 case PREFIX_GS_OVERRIDE:
348 theseg = &(info->segments.gs);
356 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
358 while (rep_num > 0) {
363 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
365 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
366 // either page fault or gpf...
369 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
370 // not sure how we handle errors.....
371 PrintError("Write Failure for outs on port %x\n", io_info->port);
375 info->vm_regs.rsi += write_size * direction;
384 info->rip = ctrl_area->exit_info2;