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/vmcs.h>
23 #include <palacios/vmx_lowlevel.h>
24 #include <palacios/vmm.h>
25 #include <palacios/vmx_handler.h>
26 #include <palacios/vmm_ctrl_regs.h>
27 #include <palacios/vm_guest_mem.h>
28 #include <palacios/vmm_decoder.h>
30 #ifndef CONFIG_DEBUG_IO
32 #define PrintDebug(fmt, args...)
37 static int update_map(struct guest_info * info, uint16_t port, int hook_read, int hook_write) {
38 uchar_t * bitmap = (uint8_t *)(info->io_map.arch_data);
42 if ((hook_read == 0) && (hook_write == 0)) {
43 *(bitmap + major) &= ~(0x1 << minor);
45 *(bitmap + major) |= (0x1 << minor);
51 int v3_init_vmx_io_map(struct guest_info * info) {
52 info->io_map.update_map = update_map;
54 info->io_map.arch_data = V3_VAddr(V3_AllocPages(2));
55 memset(info->io_map.arch_data, 0, PAGE_SIZE_4KB * 2);
60 int v3_handle_vmx_io_in(struct guest_info * info) {
61 struct vmx_exit_io_qual io_qual;
62 struct v3_io_hook * hook = NULL;
64 uint32_t instr_length = 0;
67 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
68 hook = v3_get_io_hook(info, io_qual.port);
71 PrintError("Hook not present for IN on port %x\n", io_qual.port);
75 read_size = io_qual.access_size + 1;
77 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_qual.port, io_qual.port);
79 if (hook->read(io_qual.port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
80 PrintError("Read failure for IN on port %x\n", io_qual.port);
84 if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
85 PrintError("Could not read instruction length\n");
89 info->rip += instr_length;
94 int v3_handle_vmx_io_ins(struct guest_info * info) {
95 struct vmx_exit_io_qual io_qual;
96 struct v3_io_hook * hook = NULL;
102 struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags);
106 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
107 hook = v3_get_io_hook(info, io_qual.port);
110 PrintError("Hook not present for INS on port 0x%x\n", io_qual.port);
114 PrintDebug("INS on port 0x%x\n", io_qual.port);
116 read_size = io_qual.access_size + 1;
119 struct vmx_exit_io_instr_info instr_info;
121 instr_info.value = 0;
122 vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
124 if (instr_info.addr_size == 0) {
125 rep_num = info->vm_regs.rcx & 0xffff;
126 } else if(instr_info.addr_size == 1) {
127 rep_num = info->vm_regs.rcx & 0xffffffff;
128 } else if(instr_info.addr_size == 2) {
129 rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
131 PrintDebug("Unknown INS address size!\n");
137 rdi_change = -read_size;
139 rdi_change = read_size;
142 PrintDebug("INS size=%d for %ld steps\n", read_size, rep_num);
144 vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va);
146 if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) {
147 PrintError("Could not convert Guest VA to host VA\n");
152 if (hook->read(io_qual.port, (char *)host_addr, read_size, hook->priv_data) != read_size) {
153 PrintError("Read Failure for INS on port 0x%x\n", io_qual.port);
157 host_addr += rdi_change;
158 info->vm_regs.rdi += rdi_change;
164 } while (--rep_num > 0);
167 vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
168 info->rip += instr_len;
175 int v3_handle_vmx_io_out(struct guest_info * info) {
176 struct vmx_exit_io_qual io_qual;
177 struct v3_io_hook * hook = NULL;
179 uint32_t instr_length = 0;
181 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
182 hook = v3_get_io_hook(info, io_qual.port);
185 PrintError("Hook not present for out on port %x\n", io_qual.port);
189 write_size = io_qual.access_size + 1;
191 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_qual.port, io_qual.port);
193 if (hook->write(io_qual.port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
194 PrintError("Write failure for out on port %x\n",io_qual.port);
199 if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
200 PrintError("Could not read instruction length\n");
204 info->rip += instr_length;
211 int v3_handle_vmx_io_outs(struct guest_info * info) {
212 struct vmx_exit_io_qual io_qual;
213 struct v3_io_hook * hook = NULL;
219 struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags);
222 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
223 hook = v3_get_io_hook(info, io_qual.port);
226 PrintError("Hook not present for OUTS on port 0x%x\n", io_qual.port);
230 PrintDebug("OUTS on port 0x%x\n", io_qual.port);
232 write_size = io_qual.access_size + 1;
235 // Grab the address sized bits of rcx
236 struct vmx_exit_io_instr_info instr_info;
238 instr_info.value = 0;
239 vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
241 if (instr_info.addr_size == 0) {
242 rep_num = info->vm_regs.rcx & 0xffff;
243 } else if(instr_info.addr_size == 1) {
244 rep_num = info->vm_regs.rcx & 0xffffffff;
245 } else if(instr_info.addr_size == 2) {
246 rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
248 PrintDebug("Unknown INS address size!\n");
254 rsi_change = -write_size;
256 rsi_change = write_size;
259 vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va);
261 PrintDebug("OUTS size=%d for %ld steps\n", write_size, rep_num);
263 if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) {
264 PrintError("Could not convert guest VA to host VA\n");
269 if (hook->write(io_qual.port, (char *)host_addr, write_size, hook->priv_data) != write_size) {
270 PrintError("Read failure for INS on port 0x%x\n", io_qual.port);
274 host_addr += rsi_change;
275 info->vm_regs.rsi += rsi_change;
281 } while (--rep_num > 0);
284 vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
285 info->rip += instr_len;