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) {
94 if (io_info->addr16) {
96 } else if (io_info->addr32) {
98 } else if (io_info->addr64) {
99 mask = 0xffffffffffffffffLL;
101 // should never happen
106 rep_num = info->vm_regs.rcx & mask;
111 while (rep_num > 0) {
113 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, &(info->segments.es));
115 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
116 // either page fault or gpf...
119 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
120 // not sure how we handle errors.....
121 PrintDebug("Read Failure for ins on port %x\n", io_info->port);
125 info->vm_regs.rdi += read_size * direction;
134 info->rip = ctrl_area->exit_info2;
139 int handle_svm_io_out(struct guest_info * info) {
140 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
141 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
142 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
144 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
145 uint_t write_size = 0;
148 PrintDebug("Hook Not present for out on port %x\n", io_info->port);
149 // error, we should not have exited on this port
153 PrintDebug("OUT on port %d (0x%x)\n", io_info->port, io_info->port);
157 } else if (io_info->sz16) {
159 } else if (io_info->sz32) {
164 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
165 // not sure how we handle errors.....
166 PrintDebug("Write Failure for out on port %x\n", io_info->port);
170 info->rip = ctrl_area->exit_info2;
176 /* We might not handle wrap around of the RSI register correctly...
177 * In that if we do wrap around the effect will manifest in the higher bits of the register
180 int handle_svm_io_outs(struct guest_info * info) {
181 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
182 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
184 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
186 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
187 uint_t write_size = 0;
195 // This is kind of hacky...
196 // direction can equal either 1 or -1
197 // We will multiply the final added offset by this value to go the correct direction
199 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
206 PrintDebug("Hook Not present for outs on port %x\n", io_info->port);
207 // error, we should not have exited on this port
211 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
215 } else if (io_info->sz16) {
217 } else if (io_info->sz32) {
222 if (io_info->addr16) {
224 } else if (io_info->addr32) {
226 } else if (io_info->addr64) {
227 mask = 0xffffffffffffffffLL;
229 // should never happen
234 rep_num = info->vm_regs.rcx & mask;
238 while (rep_num > 0) {
240 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), &(info->segments.ds));
242 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
243 // either page fault or gpf...
246 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
247 // not sure how we handle errors.....
248 PrintDebug("Write Failure for outs on port %x\n", io_info->port);
252 info->vm_regs.rsi += write_size * direction;
261 info->rip = ctrl_area->exit_info2;