1 /* Northwestern University */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
3 #include <palacios/svm_io.h>
4 #include <palacios/vmm_io.h>
5 #include <palacios/vmm_ctrl_regs.h>
6 #include <palacios/vmm_decoder.h>
7 #include <palacios/vm_guest_mem.h>
11 #define PrintDebug(fmt, args...)
15 // This should package up an IO request and call vmm_handle_io
16 int handle_svm_io_in(struct guest_info * info) {
17 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
18 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
19 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
21 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
25 PrintError("Hook Not present for in on port %x\n", io_info->port);
26 // error, we should not have exited on this port
33 } else if (io_info->sz16) {
35 } else if (io_info->sz32) {
39 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_info->port, io_info->port);
41 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
42 // not sure how we handle errors.....
43 PrintError("Read Failure for in on port %x\n", io_info->port);
47 info->rip = ctrl_area->exit_info2;
56 /* We might not handle wrap around of the RDI register correctly...
57 * In that if we do wrap around the effect will manifest in the higher bits of the register
59 int handle_svm_io_ins(struct guest_info * info) {
60 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
61 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
63 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
65 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
74 // This is kind of hacky...
75 // direction can equal either 1 or -1
76 // We will multiply the final added offset by this value to go the correct direction
78 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
85 PrintError("Hook Not present for ins on port %x\n", io_info->port);
86 // error, we should not have exited on this port
90 struct v3_segment *theseg = &(info->segments.es); // default is ES
94 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
95 PrintError("Can't access instruction\n");
99 while (is_prefix_byte(*((char*)inst_ptr))) {
100 switch (*((char*)inst_ptr)) {
101 case PREFIX_CS_OVERRIDE:
102 theseg = &(info->segments.cs);
104 case PREFIX_SS_OVERRIDE:
105 theseg = &(info->segments.ss);
107 case PREFIX_DS_OVERRIDE:
108 theseg = &(info->segments.ds);
110 case PREFIX_ES_OVERRIDE:
111 theseg = &(info->segments.es);
113 case PREFIX_FS_OVERRIDE:
114 theseg = &(info->segments.fs);
116 case PREFIX_GS_OVERRIDE:
117 theseg = &(info->segments.gs);
126 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
130 } else if (io_info->sz16) {
132 } else if (io_info->sz32) {
135 PrintError("io_info Invalid Size\n");
140 if (io_info->addr16) {
142 } else if (io_info->addr32) {
144 } else if (io_info->addr64) {
145 mask = 0xffffffffffffffffLL;
147 // This value should be set depending on the host register size...
148 mask = get_gpr_mask(info);
150 PrintDebug("INS io_info invalid address size, mask=0x%x, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
151 // PrintDebug("INS Aborted... Check implementation\n");
156 // rep_num = info->vm_regs.rcx & mask;
157 rep_num = info->vm_regs.rcx;
161 PrintDebug("INS size=%d for %d steps\n", read_size, rep_num);
163 while (rep_num > 0) {
165 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, theseg);
167 PrintDebug("Writing 0x%x\n", dst_addr);
169 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
170 // either page fault or gpf...
171 PrintError("Could not convert Guest VA to host VA\n");
175 if (hook->read(io_info->port, (char*)host_addr, read_size, hook->priv_data) != read_size) {
176 // not sure how we handle errors.....
177 PrintError("Read Failure for ins on port %x\n", io_info->port);
181 info->vm_regs.rdi += read_size * direction;
190 info->rip = ctrl_area->exit_info2;
195 int handle_svm_io_out(struct guest_info * info) {
196 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
197 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
198 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
200 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
201 uint_t write_size = 0;
204 PrintError("Hook Not present for out on port %x\n", io_info->port);
205 // error, we should not have exited on this port
212 } else if (io_info->sz16) {
214 } else if (io_info->sz32) {
218 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_info->port, io_info->port);
220 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
221 // not sure how we handle errors.....
222 PrintError("Write Failure for out on port %x\n", io_info->port);
226 info->rip = ctrl_area->exit_info2;
232 /* We might not handle wrap around of the RSI register correctly...
233 * In that if we do wrap around the effect will manifest in the higher bits of the register
236 int handle_svm_io_outs(struct guest_info * info) {
237 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
238 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
241 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
243 struct vmm_io_hook * hook = v3_get_io_hook(&(info->io_map), io_info->port);
244 uint_t write_size = 0;
252 // This is kind of hacky...
253 // direction can equal either 1 or -1
254 // We will multiply the final added offset by this value to go the correct direction
256 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
263 PrintError("Hook Not present for outs on port %x\n", io_info->port);
264 // error, we should not have exited on this port
268 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
272 } else if (io_info->sz16) {
274 } else if (io_info->sz32) {
279 if (io_info->addr16) {
281 } else if (io_info->addr32) {
283 } else if (io_info->addr64) {
284 mask = 0xffffffffffffffffLL;
286 // This value should be set depending on the host register size...
287 mask = get_gpr_mask(info);
289 PrintDebug("OUTS io_info invalid address size, mask=0x%, io_info=0x%x\n",mask,*((uint_t*)(io_info)));
290 // PrintDebug("INS Aborted... Check implementation\n");
292 // should never happen
293 //PrintDebug("Invalid Address length\n");
298 rep_num = info->vm_regs.rcx & mask;
301 struct v3_segment *theseg = &(info->segments.es); // default is ES
305 if (guest_va_to_host_pa(info,get_addr_linear(info,info->rip,&(info->segments.cs)),&inst_ptr)==-1) {
306 PrintError("Can't access instruction\n");
310 while (is_prefix_byte(*((char*)inst_ptr))) {
311 switch (*((char*)inst_ptr)) {
312 case PREFIX_CS_OVERRIDE:
313 theseg = &(info->segments.cs);
315 case PREFIX_SS_OVERRIDE:
316 theseg = &(info->segments.ss);
318 case PREFIX_DS_OVERRIDE:
319 theseg = &(info->segments.ds);
321 case PREFIX_ES_OVERRIDE:
322 theseg = &(info->segments.es);
324 case PREFIX_FS_OVERRIDE:
325 theseg = &(info->segments.fs);
327 case PREFIX_GS_OVERRIDE:
328 theseg = &(info->segments.gs);
336 PrintDebug("OUTS size=%d for %d steps\n", write_size, rep_num);
338 while (rep_num > 0) {
343 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), theseg);
345 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
346 // either page fault or gpf...
349 if (hook->write(io_info->port, (char*)host_addr, write_size, hook->priv_data) != write_size) {
350 // not sure how we handle errors.....
351 PrintError("Write Failure for outs on port %x\n", io_info->port);
355 info->vm_regs.rsi += write_size * direction;
364 info->rip = ctrl_area->exit_info2;