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);
57 v3_refresh_io_map(info);
62 int v3_handle_vmx_io_in(struct guest_info * info) {
63 struct vmx_exit_io_qual io_qual;
64 struct v3_io_hook * hook = NULL;
66 uint32_t instr_length = 0;
69 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
70 hook = v3_get_io_hook(info, io_qual.port);
73 PrintError("Hook not present for IN on port %x\n", io_qual.port);
77 read_size = io_qual.access_size + 1;
79 PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_qual.port, io_qual.port);
81 if (hook->read(io_qual.port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) {
82 PrintError("Read failure for IN on port %x\n", io_qual.port);
86 if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
87 PrintError("Could not read instruction length\n");
91 info->rip += instr_length;
96 int v3_handle_vmx_io_ins(struct guest_info * info) {
97 struct vmx_exit_io_qual io_qual;
98 struct v3_io_hook * hook = NULL;
101 addr_t host_addr = 0;
104 struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags);
108 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
109 hook = v3_get_io_hook(info, io_qual.port);
112 PrintError("Hook not present for INS on port 0x%x\n", io_qual.port);
116 PrintDebug("INS on port 0x%x\n", io_qual.port);
118 read_size = io_qual.access_size + 1;
121 struct vmx_exit_io_instr_info instr_info;
123 instr_info.value = 0;
124 vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
126 if (instr_info.addr_size == 0) {
127 rep_num = info->vm_regs.rcx & 0xffff;
128 } else if(instr_info.addr_size == 1) {
129 rep_num = info->vm_regs.rcx & 0xffffffff;
130 } else if(instr_info.addr_size == 2) {
131 rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
133 PrintDebug("Unknown INS address size!\n");
139 rdi_change = -read_size;
141 rdi_change = read_size;
144 PrintDebug("INS size=%d for %ld steps\n", read_size, rep_num);
146 vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va);
148 if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) {
149 PrintError("Could not convert Guest VA to host VA\n");
154 if (hook->read(io_qual.port, (char *)host_addr, read_size, hook->priv_data) != read_size) {
155 PrintError("Read Failure for INS on port 0x%x\n", io_qual.port);
159 host_addr += rdi_change;
160 info->vm_regs.rdi += rdi_change;
166 } while (--rep_num > 0);
169 vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
170 info->rip += instr_len;
177 int v3_handle_vmx_io_out(struct guest_info * info) {
178 struct vmx_exit_io_qual io_qual;
179 struct v3_io_hook * hook = NULL;
181 uint32_t instr_length = 0;
183 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
184 hook = v3_get_io_hook(info, io_qual.port);
187 PrintError("Hook not present for out on port %x\n", io_qual.port);
191 write_size = io_qual.access_size + 1;
193 PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_qual.port, io_qual.port);
195 if (hook->write(io_qual.port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) {
196 PrintError("Write failure for out on port %x\n",io_qual.port);
201 if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
202 PrintError("Could not read instruction length\n");
206 info->rip += instr_length;
213 int v3_handle_vmx_io_outs(struct guest_info * info) {
214 struct vmx_exit_io_qual io_qual;
215 struct v3_io_hook * hook = NULL;
221 struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags);
224 vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value));
225 hook = v3_get_io_hook(info, io_qual.port);
228 PrintError("Hook not present for OUTS on port 0x%x\n", io_qual.port);
232 PrintDebug("OUTS on port 0x%x\n", io_qual.port);
234 write_size = io_qual.access_size + 1;
237 // Grab the address sized bits of rcx
238 struct vmx_exit_io_instr_info instr_info;
240 instr_info.value = 0;
241 vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
243 if (instr_info.addr_size == 0) {
244 rep_num = info->vm_regs.rcx & 0xffff;
245 } else if(instr_info.addr_size == 1) {
246 rep_num = info->vm_regs.rcx & 0xffffffff;
247 } else if(instr_info.addr_size == 2) {
248 rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
250 PrintDebug("Unknown INS address size!\n");
256 rsi_change = -write_size;
258 rsi_change = write_size;
261 vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va);
263 PrintDebug("OUTS size=%d for %ld steps\n", write_size, rep_num);
265 if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) {
266 PrintError("Could not convert guest VA to host VA\n");
271 if (hook->write(io_qual.port, (char *)host_addr, write_size, hook->priv_data) != write_size) {
272 PrintError("Read failure for INS on port 0x%x\n", io_qual.port);
276 host_addr += rsi_change;
277 info->vm_regs.rsi += rsi_change;
283 } while (--rep_num > 0);
286 vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
287 info->rip += instr_len;