2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Andy Gocke <agocke@gmail.com>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Andy Gocke <agocke@gmail.com>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmx_io.h>
21 #include <palacios/vmm_io.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmx_handler.h>
24 #include <palacios/vmm_ctrl_regs.h>
25 #include <palacios/vm_guest_mem.h>
26 #include <palacios/vmm_decoder.h>
28 #ifndef CONFIG_DEBUG_IO
30 #define PrintDebug(fmt, args...)
35 static int update_map(struct v3_vm_info * vm, uint16_t port, int hook_read, int hook_write) {
36 uchar_t * bitmap = (uint8_t *)(vm->io_map.arch_data);
40 if ((hook_read == 0) && (hook_write == 0)) {
41 *(bitmap + major) &= ~(0x1 << minor);
43 *(bitmap + major) |= (0x1 << minor);
49 int v3_init_vmx_io_map(struct v3_vm_info * vm) {
50 vm->io_map.update_map = update_map;
52 vm->io_map.arch_data = V3_VAddr(V3_AllocPages(2));
53 memset(vm->io_map.arch_data, 0, PAGE_SIZE_4KB * 2);
55 v3_refresh_io_map(vm);
60 int v3_handle_vmx_io_in(struct guest_info * core, struct vmx_exit_info * exit_info) {
61 struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);;
62 struct v3_io_hook * hook = NULL;
66 hook = v3_get_io_hook(core->vm_info, io_qual.port);
69 PrintError("Hook not present for IN on port %x\n", io_qual.port);
73 read_size = io_qual.access_size + 1;
75 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_qual.port, io_qual.port);
77 if (hook->read(core, io_qual.port, &(core->vm_regs.rax), read_size, hook->priv_data) != read_size) {
78 PrintError("Read failure for IN on port %x\n", io_qual.port);
83 core->rip += exit_info->instr_len;
88 int v3_handle_vmx_io_ins(struct guest_info * core, struct vmx_exit_info * exit_info) {
89 struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);;
90 struct v3_io_hook * hook = NULL;
92 addr_t guest_va = exit_info->guest_linear_addr;
96 struct rflags * flags = (struct rflags *)&(core->ctrl_regs.rflags);
98 hook = v3_get_io_hook(core->vm_info, io_qual.port);
101 PrintError("Hook not present for INS on port 0x%x\n", io_qual.port);
105 PrintDebug("INS on port 0x%x\n", io_qual.port);
107 read_size = io_qual.access_size + 1;
110 struct vmx_exit_io_instr_info instr_info = *(struct vmx_exit_io_instr_info *)&(exit_info->instr_info);
112 if (instr_info.addr_size == 0) {
113 rep_num = core->vm_regs.rcx & 0xffff;
114 } else if(instr_info.addr_size == 1) {
115 rep_num = core->vm_regs.rcx & 0xffffffff;
116 } else if(instr_info.addr_size == 2) {
117 rep_num = core->vm_regs.rcx & 0xffffffffffffffffLL;
119 PrintDebug("Unknown INS address size!\n");
125 rdi_change = -read_size;
127 rdi_change = read_size;
130 PrintDebug("INS size=%d for %ld steps\n", read_size, rep_num);
134 if (v3_gva_to_hva(core, guest_va, &host_addr) == -1) {
135 PrintError("Could not convert Guest VA to host VA\n");
140 if (hook->read(core, io_qual.port, (char *)host_addr, read_size, hook->priv_data) != read_size) {
141 PrintError("Read Failure for INS on port 0x%x\n", io_qual.port);
145 host_addr += rdi_change;
146 core->vm_regs.rdi += rdi_change;
152 } while (--rep_num > 0);
155 core->rip += exit_info->instr_len;
162 int v3_handle_vmx_io_out(struct guest_info * core, struct vmx_exit_info * exit_info) {
163 struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);
164 struct v3_io_hook * hook = NULL;
167 hook = v3_get_io_hook(core->vm_info, io_qual.port);
170 PrintError("Hook not present for out on port %x\n", io_qual.port);
174 write_size = io_qual.access_size + 1;
176 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_qual.port, io_qual.port);
178 if (hook->write(core, io_qual.port, &(core->vm_regs.rax), write_size, hook->priv_data) != write_size) {
179 PrintError("Write failure for out on port %x\n",io_qual.port);
185 core->rip += exit_info->instr_len;
192 int v3_handle_vmx_io_outs(struct guest_info * core, struct vmx_exit_info * exit_info) {
193 struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);
194 struct v3_io_hook * hook = NULL;
196 addr_t guest_va = exit_info->guest_linear_addr;
200 struct rflags * flags = (struct rflags *)&(core->ctrl_regs.rflags);
202 hook = v3_get_io_hook(core->vm_info, io_qual.port);
205 PrintError("Hook not present for OUTS on port 0x%x\n", io_qual.port);
209 PrintDebug("OUTS on port 0x%x\n", io_qual.port);
211 write_size = io_qual.access_size + 1;
214 // Grab the address sized bits of rcx
215 struct vmx_exit_io_instr_info instr_info = *(struct vmx_exit_io_instr_info *)&(exit_info->instr_info);
217 if (instr_info.addr_size == 0) {
218 rep_num = core->vm_regs.rcx & 0xffff;
219 } else if(instr_info.addr_size == 1) {
220 rep_num = core->vm_regs.rcx & 0xffffffff;
221 } else if(instr_info.addr_size == 2) {
222 rep_num = core->vm_regs.rcx & 0xffffffffffffffffLL;
224 PrintDebug("Unknown INS address size!\n");
230 rsi_change = -write_size;
232 rsi_change = write_size;
237 PrintDebug("OUTS size=%d for %ld steps\n", write_size, rep_num);
239 if (v3_gva_to_hva(core, guest_va, &host_addr) == -1) {
240 PrintError("Could not convert guest VA to host VA\n");
245 if (hook->write(core, io_qual.port, (char *)host_addr, write_size, hook->priv_data) != write_size) {
246 PrintError("Read failure for INS on port 0x%x\n", io_qual.port);
250 host_addr += rsi_change;
251 core->vm_regs.rsi += rsi_change;
257 } while (--rep_num > 0);
260 core->rip += exit_info->instr_len;