1 #include <geekos/svm_io.h>
2 #include <geekos/vmm_io.h>
3 #include <geekos/vmm_ctrl_regs.h>
4 #include <geekos/vmm_emulate.h>
5 #include <geekos/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 // error, we should not have exited on this port
22 PrintDebug("IN on port %d (0x%x)\n", io_info->port, io_info->port);
26 } else if (io_info->sz16) {
28 } else if (io_info->sz32) {
33 if (hook->read(io_info->port, &(info->vm_regs.rax), read_size) != read_size) {
34 // not sure how we handle errors.....
38 info->rip = ctrl_area->exit_info2;
47 /* We might not handle wrap around of the RDI register correctly...
48 * In that if we do wrap around the effect will manifest in the higher bits of the register
50 int handle_svm_io_ins(struct guest_info * info) {
51 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
52 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
54 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
56 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
64 if (info->cpu_mode == REAL) {
65 base_addr = (guest_state->es.base << 4);
66 } else if (info->cpu_mode == PROTECTED) {
67 base_addr = guest_state->es.base;
71 // This is kind of hacky...
72 // direction can equal either 1 or -1
73 // We will multiply the final added offset by this value to go the correct direction
75 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
82 // error, we should not have exited on this port
86 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
90 } else if (io_info->sz16) {
92 } else if (io_info->sz32) {
97 if (io_info->addr16) {
99 } else if (io_info->addr32) {
101 } else if (io_info->addr64) {
102 mask = 0xffffffffffffffffLL;
104 // should never happen
109 rep_num = info->vm_regs.rcx & mask;
114 while (rep_num > 0) {
116 dst_addr = base_addr + (info->vm_regs.rdi & mask);
118 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
119 // either page fault or gpf...
122 if (hook->read(io_info->port, (char*)host_addr, read_size) != read_size) {
123 // not sure how we handle errors.....
127 info->vm_regs.rdi += read_size * direction;
136 info->rip = ctrl_area->exit_info2;
141 int handle_svm_io_out(struct guest_info * info) {
142 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
143 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
144 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
146 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
147 uint_t write_size = 0;
150 // error, we should not have exited on this port
154 PrintDebug("OUT on port %d (0x%x)\n", io_info->port, io_info->port);
158 } else if (io_info->sz16) {
160 } else if (io_info->sz32) {
165 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size) != write_size) {
166 // not sure how we handle errors.....
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;
188 addr_t base_addr = 0;
195 if (info->cpu_mode == REAL) {
196 base_addr = (guest_state->ds.base << 4);
197 } else if (info->cpu_mode == PROTECTED) {
198 base_addr = guest_state->ds.base;
201 // This is kind of hacky...
202 // direction can equal either 1 or -1
203 // We will multiply the final added offset by this value to go the correct direction
205 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
212 // error, we should not have exited on this port
216 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
220 } else if (io_info->sz16) {
222 } else if (io_info->sz32) {
227 if (io_info->addr16) {
229 } else if (io_info->addr32) {
231 } else if (io_info->addr64) {
232 mask = 0xffffffffffffffffLL;
234 // should never happen
239 rep_num = info->vm_regs.rcx & mask;
243 while (rep_num > 0) {
245 dst_addr = base_addr + (info->vm_regs.rsi & mask);
247 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
248 // either page fault or gpf...
251 if (hook->write(io_info->port, (char*)host_addr, write_size) != write_size) {
252 // not sure how we handle errors.....
256 info->vm_regs.rsi += write_size * direction;
265 info->rip = ctrl_area->exit_info2;