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);
58 addr_t base_addr = guest_state->es.base;
63 // This is kind of hacky...
64 // direction can equal either 1 or -1
65 // We will multiply the final added offset by this value to go the correct direction
67 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
74 // error, we should not have exited on this port
78 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
82 } else if (io_info->sz16) {
84 } else if (io_info->sz32) {
89 if (io_info->addr16) {
91 } else if (io_info->addr32) {
93 } else if (io_info->addr64) {
94 mask = 0xffffffffffffffffLL;
96 // should never happen
101 rep_num = info->vm_regs.rcx & mask;
106 while (rep_num > 0) {
108 dst_addr = base_addr + (info->vm_regs.rdi & mask);
110 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
111 // either page fault or gpf...
114 if (hook->read(io_info->port, (char*)host_addr, read_size) != read_size) {
115 // not sure how we handle errors.....
119 info->vm_regs.rdi += read_size * direction;
128 info->rip = ctrl_area->exit_info2;
133 int handle_svm_io_out(struct guest_info * info) {
134 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
135 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
136 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
138 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
139 uint_t write_size = 0;
142 // error, we should not have exited on this port
146 PrintDebug("OUT on port %d (0x%x)\n", io_info->port, io_info->port);
150 } else if (io_info->sz16) {
152 } else if (io_info->sz32) {
157 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size) != write_size) {
158 // not sure how we handle errors.....
162 info->rip = ctrl_area->exit_info2;
168 /* We might not handle wrap around of the RSI register correctly...
169 * In that if we do wrap around the effect will manifest in the higher bits of the register
172 int handle_svm_io_outs(struct guest_info * info) {
173 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
174 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
176 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
178 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
179 uint_t write_size = 0;
180 addr_t base_addr = guest_state->ds.base;
185 // This is kind of hacky...
186 // direction can equal either 1 or -1
187 // We will multiply the final added offset by this value to go the correct direction
189 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
196 // error, we should not have exited on this port
200 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
204 } else if (io_info->sz16) {
206 } else if (io_info->sz32) {
211 if (io_info->addr16) {
213 } else if (io_info->addr32) {
215 } else if (io_info->addr64) {
216 mask = 0xffffffffffffffffLL;
218 // should never happen
223 rep_num = info->vm_regs.rcx & mask;
227 while (rep_num > 0) {
229 dst_addr = base_addr + (info->vm_regs.rsi & mask);
231 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
232 // either page fault or gpf...
235 if (hook->write(io_info->port, (char*)host_addr, write_size) != write_size) {
236 // not sure how we handle errors.....
240 info->vm_regs.rsi += write_size * direction;
249 info->rip = ctrl_area->exit_info2;