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/vmm.h>
21 #include <palacios/vm_guest.h>
22 #include <palacios/vmm_msr.h>
24 #define LOW_MSR_START 0x00000000
25 #define LOW_MSR_END 0x1fff
26 #define HIGH_MSR_START 0xc0000000
27 #define HIGH_MSR_END 0xc0001fff
29 #define LOW_MSR_INDEX 0
30 #define HIGH_MSR_INDEX 1024
32 static int get_bitmap_index(uint_t msr)
34 if( (msr >= LOW_MSR_START) && msr <= LOW_MSR_END) {
35 return LOW_MSR_INDEX + msr;
36 } else if (( msr >= HIGH_MSR_START ) && (msr <= HIGH_MSR_END)) {
37 return (HIGH_MSR_INDEX * 8) + (msr - HIGH_MSR_START);
39 PrintError(VM_NONE, VCORE_NONE, "MSR out of range: 0x%x\n", msr);
45 static int update_map(struct v3_vm_info * vm, uint_t msr, int hook_reads, int hook_writes) {
46 int index = get_bitmap_index(msr);
47 uint_t major = index / 8;
48 uint_t minor = (index % 8);
50 uint8_t read_val = (hook_reads) ? 0x1 : 0x0;
51 uint8_t write_val = (hook_writes) ? 0x1 : 0x0;
52 uint8_t * bitmap = (uint8_t *)(vm->msr_map.arch_data);
55 // 0rintError("Error updating MSR Map failed bitmap index for (0x%x)\n", msr);
56 // MSRs not in the bitmap covered range will always trigger exits, so we don't need to worry about them here.
60 *(bitmap + major) &= ~(mask << minor);
61 *(bitmap + major) |= (read_val << minor);
63 *(bitmap + 2048 + major) &= ~(mask << minor);
64 *(bitmap + 2048 + major) |= (write_val << minor);
69 int v3_init_vmx_msr_map(struct v3_vm_info * vm) {
72 struct v3_msr_map * msr_map = &(vm->msr_map);
74 msr_map->update_map = update_map;
76 temp = V3_AllocPages(1); // need not be shadow-safe, not exposed to guest
78 PrintError(vm, VCORE_NONE, "Cannot allocat MSR bitmap\n");
81 msr_map->arch_data = V3_VAddr(temp);
82 memset(msr_map->arch_data, 0xff, PAGE_SIZE_4KB);
84 v3_refresh_msr_map(vm);
89 int v3_deinit_vmx_msr_map(struct v3_vm_info * vm) {
90 V3_FreePages(V3_PAddr(vm->msr_map.arch_data), 1);