1 #include <palacios/svm_io.h>
2 #include <palacios/vmm_io.h>
3 #include <palacios/vmm_ctrl_regs.h>
4 #include <palacios/vmm_emulate.h>
5 #include <palacios/vm_guest_mem.h>
8 // This should package up an IO request and call vmm_handle_io
9 int handle_svm_io_in(struct guest_info * info) {
10 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
11 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
12 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
14 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
18 PrintDebug("Hook Not present for in on port %x\n", io_info->port);
19 // error, we should not have exited on this port
23 PrintDebug("IN on port %d (0x%x)\n", io_info->port, io_info->port);
27 } else if (io_info->sz16) {
29 } else if (io_info->sz32) {
34 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
35 // not sure how we handle errors.....
36 PrintDebug("Read Failure for in on port %x\n", io_info->port);
40 info->rip = ctrl_area->exit_info2;
49 /* We might not handle wrap around of the RDI register correctly...
50 * In that if we do wrap around the effect will manifest in the higher bits of the register
52 int handle_svm_io_ins(struct guest_info * info) {
53 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
54 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
56 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
58 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
67 // This is kind of hacky...
68 // direction can equal either 1 or -1
69 // We will multiply the final added offset by this value to go the correct direction
71 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
78 PrintDebug("Hook Not present for ins on port %x\n", io_info->port);
79 // error, we should not have exited on this port
83 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
87 } else if (io_info->sz16) {
89 } else if (io_info->sz32) {
92 PrintDebug("io_info Invalid Size\n");
97 if (io_info->addr16) {
99 } else if (io_info->addr32) {
101 } else if (io_info->addr64) {
102 mask = 0xffffffffffffffffLL;
104 // This value should be set depending on the host register size...
105 mask = get_gpr_mask(info);
109 // rep_num = info->vm_regs.rcx & mask;
110 rep_num = info->vm_regs.rcx;
114 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
116 while (rep_num > 0) {
118 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, &(info->segments.es));
120 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
121 // either page fault or gpf...
122 PrintDebug("Could not convert Guest VA to host VA\n");
126 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
127 // not sure how we handle errors.....
128 PrintDebug("Read Failure for ins on port %x\n", io_info->port);
132 info->vm_regs.rdi += read_size * direction;
141 info->rip = ctrl_area->exit_info2;
146 int handle_svm_io_out(struct guest_info * info) {
147 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
148 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
149 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
151 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
152 uint_t write_size = 0;
155 PrintDebug("Hook Not present for out on port %x\n", io_info->port);
156 // error, we should not have exited on this port
160 PrintDebug("OUT on port %d (0x%x)\n", io_info->port, io_info->port);
164 } else if (io_info->sz16) {
166 } else if (io_info->sz32) {
171 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
172 // not sure how we handle errors.....
173 PrintDebug("Write Failure for out on port %x\n", io_info->port);
177 info->rip = ctrl_area->exit_info2;
183 /* We might not handle wrap around of the RSI register correctly...
184 * In that if we do wrap around the effect will manifest in the higher bits of the register
187 int handle_svm_io_outs(struct guest_info * info) {
188 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
189 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
191 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
193 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
194 uint_t write_size = 0;
202 // This is kind of hacky...
203 // direction can equal either 1 or -1
204 // We will multiply the final added offset by this value to go the correct direction
206 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
213 PrintDebug("Hook Not present for outs on port %x\n", io_info->port);
214 // error, we should not have exited on this port
218 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
222 } else if (io_info->sz16) {
224 } else if (io_info->sz32) {
229 if (io_info->addr16) {
231 } else if (io_info->addr32) {
233 } else if (io_info->addr64) {
234 mask = 0xffffffffffffffffLL;
236 // should never happen
237 PrintDebug("Invalid Address length\n");
242 rep_num = info->vm_regs.rcx & mask;
246 while (rep_num > 0) {
248 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), &(info->segments.ds));
250 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
251 // either page fault or gpf...
254 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
255 // not sure how we handle errors.....
256 PrintDebug("Write Failure for outs on port %x\n", io_info->port);
260 info->vm_regs.rsi += write_size * direction;
269 info->rip = ctrl_area->exit_info2;