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 ;
65 // This is kind of hacky...
66 // direction can equal either 1 or -1
67 // We will multiply the final added offset by this value to go the correct direction
69 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
76 // error, we should not have exited on this port
80 PrintDebug("INS on port %d (0x%x)\n", io_info->port, io_info->port);
84 } else if (io_info->sz16) {
86 } else if (io_info->sz32) {
91 if (io_info->addr16) {
93 } else if (io_info->addr32) {
95 } else if (io_info->addr64) {
96 mask = 0xffffffffffffffffLL;
98 // should never happen
103 rep_num = info->vm_regs.rcx & mask;
108 while (rep_num > 0) {
110 dst_addr = get_addr_linear(info, info->vm_regs.rdi & mask, base_addr);
112 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
113 // either page fault or gpf...
116 if (hook->read(io_info->port, (char*)host_addr, read_size) != read_size) {
117 // not sure how we handle errors.....
121 info->vm_regs.rdi += read_size * direction;
130 info->rip = ctrl_area->exit_info2;
135 int handle_svm_io_out(struct guest_info * info) {
136 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
137 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
138 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
140 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
141 uint_t write_size = 0;
144 // error, we should not have exited on this port
148 PrintDebug("OUT on port %d (0x%x)\n", io_info->port, io_info->port);
152 } else if (io_info->sz16) {
154 } else if (io_info->sz32) {
159 if (hook->write(io_info->port, &(info->vm_regs.rax), write_size) != write_size) {
160 // not sure how we handle errors.....
164 info->rip = ctrl_area->exit_info2;
170 /* We might not handle wrap around of the RSI register correctly...
171 * In that if we do wrap around the effect will manifest in the higher bits of the register
174 int handle_svm_io_outs(struct guest_info * info) {
175 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
176 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
178 struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
180 vmm_io_hook_t * hook = get_io_hook(&(info->io_map), io_info->port);
181 uint_t write_size = 0;
182 addr_t base_addr = guest_state->ds.base;
189 // This is kind of hacky...
190 // direction can equal either 1 or -1
191 // We will multiply the final added offset by this value to go the correct direction
193 struct rflags * flags = (struct rflags *)&(guest_state->rflags);
200 // error, we should not have exited on this port
204 PrintDebug("OUTS on port %d (0x%x)\n", io_info->port, io_info->port);
208 } else if (io_info->sz16) {
210 } else if (io_info->sz32) {
215 if (io_info->addr16) {
217 } else if (io_info->addr32) {
219 } else if (io_info->addr64) {
220 mask = 0xffffffffffffffffLL;
222 // should never happen
227 rep_num = info->vm_regs.rcx & mask;
231 while (rep_num > 0) {
233 dst_addr = get_addr_linear(info, (info->vm_regs.rsi & mask), base_addr);
235 if (guest_va_to_host_va(info, dst_addr, &host_addr) == -1) {
236 // either page fault or gpf...
239 if (hook->write(io_info->port, (char*)host_addr, write_size) != write_size) {
240 // not sure how we handle errors.....
244 info->vm_regs.rsi += write_size * direction;
253 info->rip = ctrl_area->exit_info2;