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 struct v3_segment *theseg = &(info->segments.es); // default is ES
87 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
88 PrintDebug("Can't access instruction\n");
92 while (is_prefix_byte(*((char*)inst_ptr))) {
93 switch (*((char*)inst_ptr)) {
94 case PREFIX_CS_OVERRIDE:
95 theseg = &(info->segments.cs);
97 case PREFIX_SS_OVERRIDE:
98 theseg = &(info->segments.ss);
100 case PREFIX_DS_OVERRIDE:
101 theseg = &(info->segments.ds);
103 case PREFIX_ES_OVERRIDE:
104 theseg = &(info->segments.es);
106 case PREFIX_FS_OVERRIDE:
107 theseg = &(info->segments.fs);
109 case PREFIX_GS_OVERRIDE:
110 theseg = &(info->segments.gs);
119 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
123 } else if (io_info->sz16) {
125 } else if (io_info->sz32) {
128 PrintDebug("io_info Invalid Size\n");
133 if (io_info->addr16) {
135 } else if (io_info->addr32) {
137 } else if (io_info->addr64) {
138 mask = 0xffffffffffffffffLL;
140 // This value should be set depending on the host register size...
141 mask = get_gpr_mask(info);
143 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
144 // PrintDebug("INS Aborted... Check implementation\n");
149 // rep_num = info->vm_regs.rcx & mask;
150 rep_num = info->vm_regs.rcx;
154 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
156 while (rep_num > 0) {
158 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
160 PrintDebug("Writing 0x%x\n", dst_addr);
162 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
163 // either page fault or gpf...
164 PrintDebug("Could not convert Guest VA to host VA\n");
168 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
169 // not sure how we handle errors.....
170 PrintDebug("Read Failure for ins on port %x\n", io_info->port);
174 info->vm_regs.rdi += read_size * direction;
183 info->rip = ctrl_area->exit_info2;
188 int handle_svm_io_out(struct guest_info * info) {
189 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
190 // 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;
197 PrintDebug("Hook Not present for out on port %x\n", io_info->port);
198 // error, we should not have exited on this port
205 } else if (io_info->sz16) {
207 } else if (io_info->sz32) {
211 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
213 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
214 // not sure how we handle errors.....
215 PrintDebug("Write Failure for out on port %x\n", io_info->port);
219 info->rip = ctrl_area->exit_info2;
225 /* We might not handle wrap around of the RSI register correctly...
226 * In that if we do wrap around the effect will manifest in the higher bits of the register
229 int handle_svm_io_outs(struct guest_info * info) {
230 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
231 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
234 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
236 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
237 uint_t write_size = 0;
245 // This is kind of hacky...
246 // direction can equal either 1 or -1
247 // We will multiply the final added offset by this value to go the correct direction
249 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
256 PrintDebug("Hook Not present for outs on port %x\n", io_info->port);
257 // error, we should not have exited on this port
261 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
265 } else if (io_info->sz16) {
267 } else if (io_info->sz32) {
272 if (io_info->addr16) {
274 } else if (io_info->addr32) {
276 } else if (io_info->addr64) {
277 mask = 0xffffffffffffffffLL;
279 // This value should be set depending on the host register size...
280 mask = get_gpr_mask(info);
282 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
283 // PrintDebug("INS Aborted... Check implementation\n");
285 // should never happen
286 //PrintDebug("Invalid Address length\n");
291 rep_num = info->vm_regs.rcx & mask;
294 struct v3_segment *theseg = &(info->segments.es); // default is ES
298 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
299 PrintDebug("Can't access instruction\n");
303 while (is_prefix_byte(*((char*)inst_ptr))) {
304 switch (*((char*)inst_ptr)) {
305 case PREFIX_CS_OVERRIDE:
306 theseg = &(info->segments.cs);
308 case PREFIX_SS_OVERRIDE:
309 theseg = &(info->segments.ss);
311 case PREFIX_DS_OVERRIDE:
312 theseg = &(info->segments.ds);
314 case PREFIX_ES_OVERRIDE:
315 theseg = &(info->segments.es);
317 case PREFIX_FS_OVERRIDE:
318 theseg = &(info->segments.fs);
320 case PREFIX_GS_OVERRIDE:
321 theseg = &(info->segments.gs);
329 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
331 while (rep_num > 0) {
336 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
338 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
339 // either page fault or gpf...
342 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
343 // not sure how we handle errors.....
344 PrintDebug("Write Failure for outs on port %x\n", io_info->port);
348 info->vm_regs.rsi += write_size * direction;
357 info->rip = ctrl_area->exit_info2;