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>
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);
58 // This should package up an IO request and call vmm_handle_io
59 int v3_handle_svm_io_in(struct guest_info * info) {
60 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
61 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
62 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
64 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
68 PrintError("Hook Not present for in on port %x\n", io_info->port);
69 // error, we should not have exited on this port
76 } else if (io_info->sz16) {
78 } else if (io_info->sz32) {
82 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
84 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
85 // not sure how we handle errors.....
86 PrintError("Read Failure for in on port %x\n", io_info->port);
90 info->rip = ctrl_area->exit_info2;
99 /* We might not handle wrap around of the RDI register correctly...
100 * In that if we do wrap around the effect will manifest in the higher bits of the register
102 int v3_handle_svm_io_ins(struct guest_info * info) {
103 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
104 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
106 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
108 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
113 struct v3_segment * theseg = &(info->segments.es); // default is ES
117 // This is kind of hacky...
118 // direction can equal either 1 or -1
119 // We will multiply the final added offset by this value to go the correct direction
121 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
129 PrintError("Hook Not present for ins on port %x\n", io_info->port);
130 // error, we should not have exited on this port
136 if (guest_va_to_host_va(info, get_addr_linear(info, info->rip, &(info->segments.cs)), &inst_ptr) == -1) {
137 PrintError("Can't access instruction\n");
141 while (is_prefix_byte(*((char *)inst_ptr))) {
142 switch (*((char *)inst_ptr)) {
143 case PREFIX_CS_OVERRIDE:
144 theseg = &(info->segments.cs);
146 case PREFIX_SS_OVERRIDE:
147 theseg = &(info->segments.ss);
149 case PREFIX_DS_OVERRIDE:
150 theseg = &(info->segments.ds);
152 case PREFIX_ES_OVERRIDE:
153 theseg = &(info->segments.es);
155 case PREFIX_FS_OVERRIDE:
156 theseg = &(info->segments.fs);
158 case PREFIX_GS_OVERRIDE:
159 theseg = &(info->segments.gs);
168 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
172 } else if (io_info->sz16) {
174 } else if (io_info->sz32) {
177 PrintError("io_info Invalid Size\n");
182 if (io_info->addr16) {
184 } else if (io_info->addr32) {
186 } else if (io_info->addr64) {
187 mask = 0xffffffffffffffffLL;
189 // This value should be set depending on the host register size...
190 mask = get_gpr_mask(info);
192 PrintDebug("INS io_info invalid address size, mask=0x%p, io_info=0x%p\n",
193 (void *)(addr_t)mask, (void *)(addr_t)(io_info));
194 // PrintDebug("INS Aborted... Check implementation\n");
199 rep_num = info->vm_regs.rcx & mask;
200 //rep_num = info->vm_regs.rcx;
204 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
206 while (rep_num > 0) {
208 dst_addr = get_addr_linear(info, (info->vm_regs.rdi & mask), theseg);
210 // PrintDebug("Writing 0x%p\n", (void *)dst_addr);
212 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
213 // either page fault or gpf...
214 PrintError("Could not convert Guest VA to host VA\n");
218 if (hook->read(io_info->port, (char *)host_addr, read_size, hook->priv_data) != read_size) {
219 // not sure how we handle errors.....
220 PrintError("Read Failure for ins on port %x\n", io_info->port);
224 info->vm_regs.rdi += (read_size * direction);
234 info->rip = ctrl_area->exit_info2;
239 int v3_handle_svm_io_out(struct guest_info * info) {
240 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
241 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
242 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
244 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
248 PrintError("Hook Not present for out on port %x\n", io_info->port);
249 // error, we should not have exited on this port
256 } else if (io_info->sz16) {
258 } else if (io_info->sz32) {
262 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
264 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
265 // not sure how we handle errors.....
266 PrintError("Write Failure for out on port %x\n", io_info->port);
270 info->rip = ctrl_area->exit_info2;
276 /* We might not handle wrap around of the RSI register correctly...
277 * In that if we do wrap around the effect will manifest in the higher bits of the register
280 int v3_handle_svm_io_outs(struct guest_info * info) {
281 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
282 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
285 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
287 struct v3_io_hook * hook = v3_get_io_hook(info, io_info->port);
293 struct v3_segment * theseg = &(info->segments.es); // default is ES
295 // This is kind of hacky...
296 // direction can equal either 1 or -1
297 // We will multiply the final added offset by this value to go the correct direction
299 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
307 PrintError("Hook Not present for outs on port %x\n", io_info->port);
308 // error, we should not have exited on this port
312 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
316 } else if (io_info->sz16) {
318 } else if (io_info->sz32) {
323 if (io_info->addr16) {
325 } else if (io_info->addr32) {
327 } else if (io_info->addr64) {
328 mask = 0xffffffffffffffffLL;
330 // This value should be set depending on the host register size...
331 mask = get_gpr_mask(info);
333 PrintDebug("OUTS io_info invalid address size, mask=0%p, io_info=0x%p\n",
334 (void *)(addr_t)mask, (void *)(addr_t)io_info);
335 // PrintDebug("INS Aborted... Check implementation\n");
337 // should never happen
338 //PrintDebug("Invalid Address length\n");
343 rep_num = info->vm_regs.rcx & mask;
349 if (guest_va_to_host_va(info, get_addr_linear(info, info->rip, &(info->segments.cs)), &inst_ptr) == -1) {
350 PrintError("Can't access instruction\n");
354 while (is_prefix_byte(*((char *)inst_ptr))) {
355 switch (*((char *)inst_ptr)) {
356 case PREFIX_CS_OVERRIDE:
357 theseg = &(info->segments.cs);
359 case PREFIX_SS_OVERRIDE:
360 theseg = &(info->segments.ss);
362 case PREFIX_DS_OVERRIDE:
363 theseg = &(info->segments.ds);
365 case PREFIX_ES_OVERRIDE:
366 theseg = &(info->segments.es);
368 case PREFIX_FS_OVERRIDE:
369 theseg = &(info->segments.fs);
371 case PREFIX_GS_OVERRIDE:
372 theseg = &(info->segments.gs);
380 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
382 while (rep_num > 0) {
385 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
387 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
388 // either page fault or gpf...
391 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
392 // not sure how we handle errors.....
393 PrintError("Write Failure for outs on port %x\n", io_info->port);
397 info->vm_regs.rsi += write_size * direction;
407 info->rip = ctrl_area->exit_info2;