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>
9 #define PrintDebug(fmt, args...)
13 // This should package up an IO request and call vmm_handle_io
14 int handle_svm_io_in(struct guest_info * info) {
15 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
16 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
17 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
19 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
23 PrintError("Hook Not present for in on port %x\n", io_info->port);
24 // error, we should not have exited on this port
31 } else if (io_info->sz16) {
33 } else if (io_info->sz32) {
37 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
39 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
40 // not sure how we handle errors.....
41 PrintError("Read Failure for in on port %x\n", io_info->port);
45 info->rip = ctrl_area->exit_info2;
54 /* We might not handle wrap around of the RDI register correctly...
55 * In that if we do wrap around the effect will manifest in the higher bits of the register
57 int handle_svm_io_ins(struct guest_info * info) {
58 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
59 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
61 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
63 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
72 // This is kind of hacky...
73 // direction can equal either 1 or -1
74 // We will multiply the final added offset by this value to go the correct direction
76 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
83 PrintError("Hook Not present for ins on port %x\n", io_info->port);
84 // error, we should not have exited on this port
88 struct v3_segment *theseg = &(info->segments.es); // default is ES
92 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
93 PrintError("Can't access instruction\n");
97 while (is_prefix_byte(*((char*)inst_ptr))) {
98 switch (*((char*)inst_ptr)) {
99 case PREFIX_CS_OVERRIDE:
100 theseg = &(info->segments.cs);
102 case PREFIX_SS_OVERRIDE:
103 theseg = &(info->segments.ss);
105 case PREFIX_DS_OVERRIDE:
106 theseg = &(info->segments.ds);
108 case PREFIX_ES_OVERRIDE:
109 theseg = &(info->segments.es);
111 case PREFIX_FS_OVERRIDE:
112 theseg = &(info->segments.fs);
114 case PREFIX_GS_OVERRIDE:
115 theseg = &(info->segments.gs);
124 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
128 } else if (io_info->sz16) {
130 } else if (io_info->sz32) {
133 PrintError("io_info Invalid Size\n");
138 if (io_info->addr16) {
140 } else if (io_info->addr32) {
142 } else if (io_info->addr64) {
143 mask = 0xffffffffffffffffLL;
145 // This value should be set depending on the host register size...
146 mask = get_gpr_mask(info);
148 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
149 // PrintDebug("INS Aborted... Check implementation\n");
154 // rep_num = info->vm_regs.rcx & mask;
155 rep_num = info->vm_regs.rcx;
159 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
161 while (rep_num > 0) {
163 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
165 PrintDebug("Writing 0x%x\n", dst_addr);
167 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
168 // either page fault or gpf...
169 PrintError("Could not convert Guest VA to host VA\n");
173 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
174 // not sure how we handle errors.....
175 PrintError("Read Failure for ins on port %x\n", io_info->port);
179 info->vm_regs.rdi += read_size * direction;
188 info->rip = ctrl_area->exit_info2;
193 int handle_svm_io_out(struct guest_info * info) {
194 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
195 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
196 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
198 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
199 uint_t write_size = 0;
202 PrintError("Hook Not present for out on port %x\n", io_info->port);
203 // error, we should not have exited on this port
210 } else if (io_info->sz16) {
212 } else if (io_info->sz32) {
216 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
218 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
219 // not sure how we handle errors.....
220 PrintError("Write Failure for out on port %x\n", io_info->port);
224 info->rip = ctrl_area->exit_info2;
230 /* We might not handle wrap around of the RSI register correctly...
231 * In that if we do wrap around the effect will manifest in the higher bits of the register
234 int handle_svm_io_outs(struct guest_info * info) {
235 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
236 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
239 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
241 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
242 uint_t write_size = 0;
250 // This is kind of hacky...
251 // direction can equal either 1 or -1
252 // We will multiply the final added offset by this value to go the correct direction
254 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
261 PrintError("Hook Not present for outs on port %x\n", io_info->port);
262 // error, we should not have exited on this port
266 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
270 } else if (io_info->sz16) {
272 } else if (io_info->sz32) {
277 if (io_info->addr16) {
279 } else if (io_info->addr32) {
281 } else if (io_info->addr64) {
282 mask = 0xffffffffffffffffLL;
284 // This value should be set depending on the host register size...
285 mask = get_gpr_mask(info);
287 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
288 // PrintDebug("INS Aborted... Check implementation\n");
290 // should never happen
291 //PrintDebug("Invalid Address length\n");
296 rep_num = info->vm_regs.rcx & mask;
299 struct v3_segment *theseg = &(info->segments.es); // default is ES
303 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
304 PrintError("Can't access instruction\n");
308 while (is_prefix_byte(*((char*)inst_ptr))) {
309 switch (*((char*)inst_ptr)) {
310 case PREFIX_CS_OVERRIDE:
311 theseg = &(info->segments.cs);
313 case PREFIX_SS_OVERRIDE:
314 theseg = &(info->segments.ss);
316 case PREFIX_DS_OVERRIDE:
317 theseg = &(info->segments.ds);
319 case PREFIX_ES_OVERRIDE:
320 theseg = &(info->segments.es);
322 case PREFIX_FS_OVERRIDE:
323 theseg = &(info->segments.fs);
325 case PREFIX_GS_OVERRIDE:
326 theseg = &(info->segments.gs);
334 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
336 while (rep_num > 0) {
341 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
343 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
344 // either page fault or gpf...
347 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
348 // not sure how we handle errors.....
349 PrintError("Write Failure for outs on port %x\n", io_info->port);
353 info->vm_regs.rsi += write_size * direction;
362 info->rip = ctrl_area->exit_info2;