1 #include <palacios/svm_io.h>
2 #include <palacios/vmm_io.h>
3 #include <palacios/vmm_ctrl_regs.h>
4 #include <palacios/vmm_decoder.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
26 } else if (io_info->sz16) {
28 } else if (io_info->sz32) {
32 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
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);
108 PrintDebug("INS Aborted... Check implementation\n");
113 // rep_num = info->vm_regs.rcx & mask;
114 rep_num = info->vm_regs.rcx;
118 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
120 while (rep_num > 0) {
122 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, &(info->segments.es));
124 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
125 // either page fault or gpf...
126 PrintDebug("Could not convert Guest VA to host VA\n");
130 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
131 // not sure how we handle errors.....
132 PrintDebug("Read Failure for ins on port %x\n", io_info->port);
136 info->vm_regs.rdi += read_size * direction;
145 info->rip = ctrl_area->exit_info2;
150 int handle_svm_io_out(struct guest_info * info) {
151 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
152 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
153 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
155 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
156 uint_t write_size = 0;
159 PrintDebug("Hook Not present for out on port %x\n", io_info->port);
160 // error, we should not have exited on this port
167 } else if (io_info->sz16) {
169 } else if (io_info->sz32) {
173 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
175 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
176 // not sure how we handle errors.....
177 PrintDebug("Write Failure for out on port %x\n", io_info->port);
181 info->rip = ctrl_area->exit_info2;
187 /* We might not handle wrap around of the RSI register correctly...
188 * In that if we do wrap around the effect will manifest in the higher bits of the register
191 int handle_svm_io_outs(struct guest_info * info) {
192 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
193 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
195 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
197 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
198 uint_t write_size = 0;
206 // This is kind of hacky...
207 // direction can equal either 1 or -1
208 // We will multiply the final added offset by this value to go the correct direction
210 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
217 PrintDebug("Hook Not present for outs on port %x\n", io_info->port);
218 // error, we should not have exited on this port
222 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
226 } else if (io_info->sz16) {
228 } else if (io_info->sz32) {
233 if (io_info->addr16) {
235 } else if (io_info->addr32) {
237 } else if (io_info->addr64) {
238 mask = 0xffffffffffffffffLL;
240 // should never happen
241 PrintDebug("Invalid Address length\n");
246 rep_num = info->vm_regs.rcx & mask;
250 while (rep_num > 0) {
252 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), &(info->segments.ds));
254 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
255 // either page fault or gpf...
258 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
259 // not sure how we handle errors.....
260 PrintDebug("Write Failure for outs on port %x\n", io_info->port);
264 info->vm_regs.rsi += write_size * direction;
273 info->rip = ctrl_area->exit_info2;