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".
23 #include <palacios/vm_guest.h>
24 #include <palacios/vmm_ctrl_regs.h>
25 #include <palacios/vmm.h>
26 #include <palacios/vmcb.h>
29 v3_vm_cpu_mode_t v3_get_cpu_mode(struct guest_info * info) {
31 struct efer_64 * efer;
32 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
33 struct v3_segment * cs = &(info->segments.cs);
34 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
36 if (info->shdw_pg_mode == SHADOW_PAGING) {
37 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
38 efer = (struct efer_64 *)&(info->guest_efer);
39 } else if (info->shdw_pg_mode == NESTED_PAGING) {
40 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
41 efer = (struct efer_64 *)&(guest_state->efer);
43 PrintError("Invalid Paging Mode...\n");
50 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
52 } else if (efer->lme == 0) {
54 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
57 // What about LONG_16_COMPAT???
58 return LONG_32_COMPAT;
62 // Get address width in bytes
63 uint_t v3_get_addr_width(struct guest_info * info) {
65 struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
66 struct efer_64 * efer = (struct efer_64 *)&(info->guest_efer);
67 struct v3_segment * cs = &(info->segments.cs);
69 if (info->shdw_pg_mode == SHADOW_PAGING) {
70 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
71 } else if (info->shdw_pg_mode == NESTED_PAGING) {
72 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
74 PrintError("Invalid Paging Mode...\n");
81 } else if ((cr4->pae == 0) && (efer->lme == 0)) {
83 } else if (efer->lme == 0) {
85 } else if ((efer->lme == 1) && (cs->long_mode == 1)) {
88 // What about LONG_16_COMPAT???
94 static const uchar_t REAL_STR[] = "Real";
95 static const uchar_t PROTECTED_STR[] = "Protected";
96 static const uchar_t PROTECTED_PAE_STR[] = "Protected+PAE";
97 static const uchar_t LONG_STR[] = "Long";
98 static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
99 static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
101 const uchar_t * v3_cpu_mode_to_str(v3_vm_cpu_mode_t mode) {
106 return PROTECTED_STR;
108 return PROTECTED_PAE_STR;
112 return LONG_32_COMPAT_STR;
114 return LONG_16_COMPAT_STR;
120 v3_vm_mem_mode_t v3_get_mem_mode(struct guest_info * info) {
123 if (info->shdw_pg_mode == SHADOW_PAGING) {
124 cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
125 } else if (info->shdw_pg_mode == NESTED_PAGING) {
126 cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
128 PrintError("Invalid Paging Mode...\n");
140 static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
141 static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
143 const uchar_t * v3_mem_mode_to_str(v3_vm_mem_mode_t mode) {
155 void v3_print_segments(struct guest_info * info) {
156 struct v3_segments * segs = &(info->segments);
158 struct v3_segment * seg_ptr;
160 seg_ptr=(struct v3_segment *)segs;
162 char *seg_names[] = {"CS", "DS" , "ES", "FS", "GS", "SS" , "LDTR", "GDTR", "IDTR", "TR", NULL};
163 PrintDebug("Segments\n");
165 for (i = 0; seg_names[i] != NULL; i++) {
167 PrintDebug("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
168 (void *)(addr_t)seg_ptr[i].base, seg_ptr[i].limit,
169 seg_ptr[i].long_mode, seg_ptr[i].db);
176 void v3_print_ctrl_regs(struct guest_info * info) {
177 struct v3_ctrl_regs * regs = &(info->ctrl_regs);
180 char * reg_names[] = {"CR0", "CR2", "CR3", "CR4", "CR8", "FLAGS", NULL};
181 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(info->vmm_data);
183 reg_ptr= (v3_reg_t *)regs;
185 PrintDebug("32 bit Ctrl Regs:\n");
187 for (i = 0; reg_names[i] != NULL; i++) {
188 PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
191 PrintDebug("\tEFER=0x%p\n", (void*)(addr_t)(guest_state->efer));
197 void v3_print_GPRs(struct guest_info * info) {
198 struct v3_gprs * regs = &(info->vm_regs);
201 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", NULL};
203 reg_ptr= (v3_reg_t *)regs;
205 PrintDebug("32 bit GPRs:\n");
207 for (i = 0; reg_names[i] != NULL; i++) {
208 PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
212 void v3_print_GPRs(struct guest_info * info) {
213 struct v3_gprs * regs = &(info->vm_regs);
216 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", \
217 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", NULL};
219 reg_ptr= (v3_reg_t *)regs;
221 PrintDebug("64 bit GPRs:\n");
223 for (i = 0; reg_names[i] != NULL; i++) {
224 PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);