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, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
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_handler.h>
21 #include <palacios/vmm_types.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmcs.h>
24 #include <palacios/vmx_lowlevel.h>
25 #include <palacios/vmx_io.h>
26 #include <palacios/vmx.h>
27 #include <palacios/vmm_ctrl_regs.h>
30 static int inline check_vmcs_write(vmcs_field_t field, addr_t val)
33 ret = vmcs_write(field,val);
35 if (ret != VMX_SUCCESS) {
36 PrintError("VMWRITE error on %s!: %d\n", v3_vmcs_field_to_str(field), ret);
43 static int inline check_vmcs_read(vmcs_field_t field, void * val)
46 ret = vmcs_read(field,val);
48 if(ret != VMX_SUCCESS) {
49 PrintError("VMREAD error on %s!: %d\n", v3_vmcs_field_to_str(field), ret);
56 static void inline translate_access_to_v3_seg(struct vmcs_segment_access * access,
57 struct v3_segment * v3_seg)
59 v3_seg->type = access->type;
60 v3_seg->system = access->desc_type;
61 v3_seg->dpl = access->dpl;
62 v3_seg->present = access->present;
63 v3_seg->avail = access->avail;
64 v3_seg->long_mode = access->long_mode;
65 v3_seg->db = access->db;
66 v3_seg->granularity = access->granularity;
69 static void load_vmcs_guest_state(struct guest_info * info)
71 check_vmcs_read(VMCS_GUEST_RIP, &(info->rip));
72 check_vmcs_read(VMCS_GUEST_RSP, &(info->vm_regs.rsp));
73 check_vmcs_read(VMCS_GUEST_CR0, &(info->ctrl_regs.cr0));
74 check_vmcs_read(VMCS_GUEST_CR3, &(info->ctrl_regs.cr3));
75 check_vmcs_read(VMCS_GUEST_CR4, &(info->ctrl_regs.cr4));
77 struct vmcs_segment_access access;
79 memset(&access, 0, sizeof(access));
82 check_vmcs_read(VMCS_GUEST_CS_BASE, &(info->segments.cs.base));
83 check_vmcs_read(VMCS_GUEST_CS_SELECTOR, &(info->segments.cs.selector));
84 check_vmcs_read(VMCS_GUEST_CS_LIMIT, &(info->segments.cs.limit));
85 check_vmcs_read(VMCS_GUEST_CS_ACCESS, &(access.value));
87 translate_access_to_v3_seg(&access, &(info->segments.cs));
90 check_vmcs_read(VMCS_GUEST_SS_BASE, &(info->segments.ss.base));
91 check_vmcs_read(VMCS_GUEST_SS_SELECTOR, &(info->segments.ss.selector));
92 check_vmcs_read(VMCS_GUEST_SS_LIMIT, &(info->segments.ss.limit));
93 check_vmcs_read(VMCS_GUEST_SS_ACCESS, &(access.value));
95 translate_access_to_v3_seg(&access, &(info->segments.ss));
98 check_vmcs_read(VMCS_GUEST_DS_BASE, &(info->segments.ds.base));
99 check_vmcs_read(VMCS_GUEST_DS_SELECTOR, &(info->segments.ds.selector));
100 check_vmcs_read(VMCS_GUEST_DS_LIMIT, &(info->segments.ds.limit));
101 check_vmcs_read(VMCS_GUEST_DS_ACCESS, &(access.value));
103 translate_access_to_v3_seg(&access, &(info->segments.ds));
106 check_vmcs_read(VMCS_GUEST_ES_BASE, &(info->segments.es.base));
107 check_vmcs_read(VMCS_GUEST_ES_SELECTOR, &(info->segments.es.selector));
108 check_vmcs_read(VMCS_GUEST_ES_LIMIT, &(info->segments.es.limit));
109 check_vmcs_read(VMCS_GUEST_ES_ACCESS, &(access.value));
111 translate_access_to_v3_seg(&access, &(info->segments.es));
114 check_vmcs_read(VMCS_GUEST_FS_BASE, &(info->segments.fs.base));
115 check_vmcs_read(VMCS_GUEST_FS_SELECTOR, &(info->segments.fs.selector));
116 check_vmcs_read(VMCS_GUEST_FS_LIMIT, &(info->segments.fs.limit));
117 check_vmcs_read(VMCS_GUEST_FS_ACCESS, &(access.value));
119 translate_access_to_v3_seg(&access, &(info->segments.fs));
123 check_vmcs_read(VMCS_GUEST_GS_BASE, &(info->segments.gs.base));
124 check_vmcs_read(VMCS_GUEST_GS_SELECTOR, &(info->segments.gs.selector));
125 check_vmcs_read(VMCS_GUEST_GS_LIMIT, &(info->segments.gs.limit));
126 check_vmcs_read(VMCS_GUEST_GS_ACCESS, &(access.value));
128 translate_access_to_v3_seg(&access, &(info->segments.gs));
131 check_vmcs_read(VMCS_GUEST_LDTR_BASE, &(info->segments.ldtr.base));
132 check_vmcs_read(VMCS_GUEST_LDTR_SELECTOR, &(info->segments.ldtr.selector));
133 check_vmcs_read(VMCS_GUEST_LDTR_LIMIT, &(info->segments.ldtr.limit));
134 check_vmcs_read(VMCS_GUEST_LDTR_ACCESS, &(access.value));
136 translate_access_to_v3_seg(&access, &(info->segments.ldtr));
139 check_vmcs_read(VMCS_GUEST_TR_BASE, &(info->segments.tr.base));
140 check_vmcs_read(VMCS_GUEST_TR_SELECTOR, &(info->segments.tr.selector));
141 check_vmcs_read(VMCS_GUEST_TR_LIMIT, &(info->segments.tr.limit));
142 check_vmcs_read(VMCS_GUEST_TR_ACCESS, &(access.value));
144 translate_access_to_v3_seg(&access, &(info->segments.tr));
147 check_vmcs_read(VMCS_GUEST_GDTR_BASE, &(info->segments.gdtr.base));
148 check_vmcs_read(VMCS_GUEST_GDTR_LIMIT, &(info->segments.gdtr.limit));
151 check_vmcs_read(VMCS_GUEST_IDTR_BASE, &(info->segments.idtr.base));
152 check_vmcs_read(VMCS_GUEST_IDTR_LIMIT, &(info->segments.idtr.limit));
156 static void setup_v8086_mode_for_boot(struct guest_info * info)
159 ((struct vmx_data *)info->vmm_data)->state = VMXASSIST_V8086_BIOS;
160 struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags);
166 //info->vm_regs.rsp = 0x0;
168 /* Zero the segment registers */
169 memset(&(info->segments), 0, sizeof(struct v3_segment)*6);
172 info->segments.cs.selector = 0xf000;
173 info->segments.cs.base = 0xf000 << 4;
174 info->segments.cs.limit = 0xffff;
175 info->segments.cs.type = 3;
176 info->segments.cs.system = 1;
177 info->segments.cs.dpl = 3;
178 info->segments.cs.present = 1;
179 info->segments.cs.granularity = 0;
183 /* Set values for selectors ds through ss */
184 struct v3_segment * seg_ptr = (struct v3_segment *)&(info->segments);
185 for(i = 1; i < 6 ; i++) {
186 seg_ptr[i].selector = 0x0000;
187 seg_ptr[i].base = 0x00000;
188 seg_ptr[i].limit = 0xffff;
190 seg_ptr[i].system = 1;
192 seg_ptr[i].present = 1;
193 seg_ptr[i].granularity = 0;
196 PrintDebug("END INFO!\n");
198 for(i = 6; i < 10; i++) {
199 seg_ptr[i].base = 0x0;
200 seg_ptr[i].limit = 0xffff;
203 info->segments.ldtr.type = 2;
204 info->segments.ldtr.system = 0;
205 info->segments.ldtr.present = 1;
206 info->segments.ldtr.granularity = 0;
208 info->segments.tr.type = 3;
209 info->segments.tr.system = 0;
210 info->segments.tr.present = 1;
211 info->segments.tr.granularity = 0;
215 static int inline handle_cr_access(struct guest_info * info, ulong_t exit_qual)
217 struct vmexit_cr_qual * cr_qual = (struct vmexit_cr_qual *)&exit_qual;
219 if(cr_qual->access_type < 2) {
221 switch(cr_qual->gpr) {
223 reg = info->vm_regs.rax;
226 reg = info->vm_regs.rcx;
229 reg = info->vm_regs.rdx;
232 reg = info->vm_regs.rbx;
235 reg = info->vm_regs.rsp;
238 reg = info->vm_regs.rbp;
241 reg = info->vm_regs.rsi;
244 reg = info->vm_regs.rdi;
247 reg = info->vm_regs.r8;
250 reg = info->vm_regs.r9;
253 reg = info->vm_regs.r10;
256 reg = info->vm_regs.r11;
259 reg = info->vm_regs.r11;
262 reg = info->vm_regs.r13;
265 reg = info->vm_regs.r14;
268 reg = info->vm_regs.r15;
271 PrintDebug("RAX: %p\n", (void *)info->vm_regs.rax);
273 if(cr_qual->cr_id == 0
275 && ((struct vmx_data*)info->vmm_data)->state == VMXASSIST_STARTUP) {
276 setup_v8086_mode_for_boot(info);
277 info->shdw_pg_state.guest_cr0 = 0x0;
278 v3_update_vmcs_guest_state(info);
282 PrintError("Unhandled CR access\n");
287 int v3_handle_vmx_exit(struct v3_gprs * gprs, struct guest_info * info)
289 uint32_t exit_reason;
292 check_vmcs_read(VMCS_EXIT_REASON, &exit_reason);
293 check_vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
295 PrintDebug("VMX Exit taken, id-qual: %u-%lu\n", exit_reason, exit_qual);
297 /* Update guest state */
298 load_vmcs_guest_state(info);
302 case VMEXIT_INFO_EXCEPTION_OR_NMI:
305 pf_error_t error_code;
306 check_vmcs_read(VMCS_EXIT_INT_INFO, &int_info);
307 check_vmcs_read(VMCS_EXIT_INT_ERR, &error_code);
309 if((uint8_t)int_info == 0x0e) {
310 PrintDebug("Page Fault at %p\n", (void*)exit_qual);
311 if(info->shdw_pg_mode == SHADOW_PAGING) {
312 if(v3_handle_shadow_pagefault(info, (addr_t)exit_qual, error_code) == -1) {
316 PrintError("Page fault in unimplemented paging mode\n");
320 PrintDebug("Unknown exception: 0x%x\n", (uint8_t)int_info);
327 case VMEXIT_IO_INSTR:
329 struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&exit_qual;
331 if(io_qual->dir == 0) {
332 if(io_qual->string) {
333 if(v3_handle_vmx_io_outs(info) == -1) {
337 if(v3_handle_vmx_io_out(info) == -1) {
342 if(io_qual->string) {
343 if(v3_handle_vmx_io_ins(info) == -1) {
347 if(v3_handle_vmx_io_in(info) == -1) {
355 case VMEXIT_CR_REG_ACCESSES:
356 if(handle_cr_access(info,exit_qual) != 0)
361 PrintError("Unhandled VMEXIT\n");
365 check_vmcs_write(VMCS_GUEST_CR0, info->ctrl_regs.cr0);
366 check_vmcs_write(VMCS_GUEST_CR3, info->ctrl_regs.cr3);
367 check_vmcs_write(VMCS_GUEST_CR4, info->ctrl_regs.cr4);
368 check_vmcs_write(VMCS_GUEST_RIP, info->rip);
369 check_vmcs_write(VMCS_GUEST_RSP, info->vm_regs.rsp);
371 check_vmcs_write(VMCS_CR0_READ_SHDW, info->shdw_pg_state.guest_cr0);