1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008 The V3VEE Project <http://www.v3vee.org> */
5 #include <palacios/svm_io.h>
6 #include <palacios/vmm_io.h>
7 #include <palacios/vmm_ctrl_regs.h>
8 #include <palacios/vmm_decoder.h>
9 #include <palacios/vm_guest_mem.h>
13 #define PrintDebug(fmt, args...)
17 // This should package up an IO request and call vmm_handle_io
18 int handle_svm_io_in(struct guest_info * info) {
19 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
20 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
21 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
23 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
27 PrintError("Hook Not present for in on port %x\n", io_info->port);
28 // error, we should not have exited on this port
35 } else if (io_info->sz16) {
37 } else if (io_info->sz32) {
41 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
43 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
44 // not sure how we handle errors.....
45 PrintError("Read Failure for in on port %x\n", io_info->port);
49 info->rip = ctrl_area->exit_info2;
58 /* We might not handle wrap around of the RDI register correctly...
59 * In that if we do wrap around the effect will manifest in the higher bits of the register
61 int handle_svm_io_ins(struct guest_info * info) {
62 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
63 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 vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
76 // This is kind of hacky...
77 // direction can equal either 1 or -1
78 // We will multiply the final added offset by this value to go the correct direction
80 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
87 PrintError("Hook Not present for ins on port %x\n", io_info->port);
88 // error, we should not have exited on this port
92 struct v3_segment *theseg = &(info->segments.es); // default is ES
96 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
97 PrintError("Can't access instruction\n");
101 while (is_prefix_byte(*((char*)inst_ptr))) {
102 switch (*((char*)inst_ptr)) {
103 case PREFIX_CS_OVERRIDE:
104 theseg = &(info->segments.cs);
106 case PREFIX_SS_OVERRIDE:
107 theseg = &(info->segments.ss);
109 case PREFIX_DS_OVERRIDE:
110 theseg = &(info->segments.ds);
112 case PREFIX_ES_OVERRIDE:
113 theseg = &(info->segments.es);
115 case PREFIX_FS_OVERRIDE:
116 theseg = &(info->segments.fs);
118 case PREFIX_GS_OVERRIDE:
119 theseg = &(info->segments.gs);
128 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
132 } else if (io_info->sz16) {
134 } else if (io_info->sz32) {
137 PrintError("io_info Invalid Size\n");
142 if (io_info->addr16) {
144 } else if (io_info->addr32) {
146 } else if (io_info->addr64) {
147 mask = 0xffffffffffffffffLL;
149 // This value should be set depending on the host register size...
150 mask = get_gpr_mask(info);
152 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
153 // PrintDebug("INS Aborted... Check implementation\n");
158 // rep_num = info->vm_regs.rcx & mask;
159 rep_num = info->vm_regs.rcx;
163 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
165 while (rep_num > 0) {
167 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
169 PrintDebug("Writing 0x%x\n", dst_addr);
171 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
172 // either page fault or gpf...
173 PrintError("Could not convert Guest VA to host VA\n");
177 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
178 // not sure how we handle errors.....
179 PrintError("Read Failure for ins on port %x\n", io_info->port);
183 info->vm_regs.rdi += read_size * direction;
192 info->rip = ctrl_area->exit_info2;
197 int handle_svm_io_out(struct guest_info * info) {
198 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
199 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
200 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
202 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
203 uint_t write_size = 0;
206 PrintError("Hook Not present for out on port %x\n", io_info->port);
207 // error, we should not have exited on this port
214 } else if (io_info->sz16) {
216 } else if (io_info->sz32) {
220 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
222 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
223 // not sure how we handle errors.....
224 PrintError("Write Failure for out on port %x\n", io_info->port);
228 info->rip = ctrl_area->exit_info2;
234 /* We might not handle wrap around of the RSI register correctly...
235 * In that if we do wrap around the effect will manifest in the higher bits of the register
238 int handle_svm_io_outs(struct guest_info * info) {
239 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
240 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
243 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
245 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
246 uint_t write_size = 0;
254 // This is kind of hacky...
255 // direction can equal either 1 or -1
256 // We will multiply the final added offset by this value to go the correct direction
258 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
265 PrintError("Hook Not present for outs on port %x\n", io_info->port);
266 // error, we should not have exited on this port
270 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
274 } else if (io_info->sz16) {
276 } else if (io_info->sz32) {
281 if (io_info->addr16) {
283 } else if (io_info->addr32) {
285 } else if (io_info->addr64) {
286 mask = 0xffffffffffffffffLL;
288 // This value should be set depending on the host register size...
289 mask = get_gpr_mask(info);
291 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
292 // PrintDebug("INS Aborted... Check implementation\n");
294 // should never happen
295 //PrintDebug("Invalid Address length\n");
300 rep_num = info->vm_regs.rcx & mask;
303 struct v3_segment *theseg = &(info->segments.es); // default is ES
307 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
308 PrintError("Can't access instruction\n");
312 while (is_prefix_byte(*((char*)inst_ptr))) {
313 switch (*((char*)inst_ptr)) {
314 case PREFIX_CS_OVERRIDE:
315 theseg = &(info->segments.cs);
317 case PREFIX_SS_OVERRIDE:
318 theseg = &(info->segments.ss);
320 case PREFIX_DS_OVERRIDE:
321 theseg = &(info->segments.ds);
323 case PREFIX_ES_OVERRIDE:
324 theseg = &(info->segments.es);
326 case PREFIX_FS_OVERRIDE:
327 theseg = &(info->segments.fs);
329 case PREFIX_GS_OVERRIDE:
330 theseg = &(info->segments.gs);
338 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
340 while (rep_num > 0) {
345 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
347 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
348 // either page fault or gpf...
351 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
352 // not sure how we handle errors.....
353 PrintError("Write Failure for outs on port %x\n", io_info->port);
357 info->vm_regs.rsi += write_size * direction;
366 info->rip = ctrl_area->exit_info2;