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".
21 #include <palacios/vmm_msr.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest.h>
25 static int free_hook(struct v3_vm_info * vm, struct v3_msr_hook * hook);
27 void v3_init_msr_map(struct v3_vm_info * vm) {
28 struct v3_msr_map * msr_map = &(vm->msr_map);
30 PrintDebug("Initializing MSR map.\n");
32 INIT_LIST_HEAD(&(msr_map->hook_list));
33 msr_map->num_hooks = 0;
35 msr_map->arch_data = NULL;
36 msr_map->update_map = NULL;
39 int v3_deinit_msr_map(struct v3_vm_info * vm) {
40 struct v3_msr_hook * hook = NULL;
41 struct v3_msr_hook * tmp = NULL;
43 list_for_each_entry_safe(hook, tmp, &(vm->msr_map.hook_list), link) {
50 int v3_handle_msr_write(struct guest_info * info) {
51 uint32_t msr_num = info->vm_regs.rcx;
52 struct v3_msr msr_val;
53 struct v3_msr_hook * hook = NULL;
57 PrintDebug("MSR write for msr 0x%x\n", msr_num);
59 hook = v3_get_msr_hook(info->vm_info, msr_num);
62 v3_msr_unhandled_write(info, msr_num, msr_val, NULL);
64 msr_val.lo = info->vm_regs.rax;
65 msr_val.hi = info->vm_regs.rdx;
67 if (hook->write(info, msr_num, msr_val, hook->priv_data) == -1) {
68 PrintError("Error in MSR hook Write\n");
79 int v3_handle_msr_read(struct guest_info * info) {
80 uint32_t msr_num = info->vm_regs.rcx;
81 struct v3_msr msr_val;
82 struct v3_msr_hook * hook = NULL;
86 hook = v3_get_msr_hook(info->vm_info, msr_num);
89 v3_msr_unhandled_read(info, msr_num, &msr_val, NULL);
91 if (hook->read(info, msr_num, &msr_val, hook->priv_data) == -1) {
92 PrintError("Error in MSR hook Read\n");
97 info->vm_regs.rax = msr_val.lo;
98 info->vm_regs.rdx = msr_val.hi;
106 int v3_msr_unhandled_read(struct guest_info * core, uint32_t msr, struct v3_msr * dst, void * priv_data) {
107 V3_Print("Palacios: Unhandled MSR Read (MSR=0x%x) - returning zero\n", msr);
109 // should produce GPF for unsupported msr
113 int v3_msr_unhandled_write(struct guest_info * core, uint32_t msr, struct v3_msr src, void * priv_data) {
114 V3_Print("Palacios: Unhandled MSR Write (MSR=0x%x) - ignored\n", msr);
115 // should produce GPF for unsupported msr
120 int v3_hook_msr(struct v3_vm_info * vm, uint32_t msr,
121 int (*read)(struct guest_info * core, uint32_t msr, struct v3_msr * dst, void * priv_data),
122 int (*write)(struct guest_info * core, uint32_t msr, struct v3_msr src, void * priv_data),
125 struct v3_msr_map * msr_map = &(vm->msr_map);
126 struct v3_msr_hook * hook = NULL;
128 hook = (struct v3_msr_hook *)V3_Malloc(sizeof(struct v3_msr_hook));
131 PrintError("Could not allocate msr hook for MSR 0x%x\n", msr);
138 hook->priv_data = priv_data;
140 msr_map->num_hooks++;
142 list_add(&(hook->link), &(msr_map->hook_list));
144 if (msr_map->update_map) {
145 msr_map->update_map(vm, msr,
146 (read == NULL) ? 0 : 1,
147 (write == NULL) ? 0 : 1);
157 static int free_hook(struct v3_vm_info * vm, struct v3_msr_hook * hook) {
158 list_del(&(hook->link));
160 if (vm->msr_map.update_map) {
161 vm->msr_map.update_map(vm, hook->msr, 0, 0);
170 int v3_unhook_msr(struct v3_vm_info * vm, uint32_t msr) {
171 struct v3_msr_hook * hook = v3_get_msr_hook(vm, msr);
174 PrintError("Could not find MSR to unhook %u (0x%x)\n", msr, msr);
185 struct v3_msr_hook * v3_get_msr_hook(struct v3_vm_info * vm, uint32_t msr) {
186 struct v3_msr_map * msr_map = &(vm->msr_map);
187 struct v3_msr_hook * hook = NULL;
189 list_for_each_entry(hook, &(msr_map->hook_list), link) {
190 if (hook->msr == msr) {
199 void v3_refresh_msr_map(struct v3_vm_info * vm) {
200 struct v3_msr_map * msr_map = &(vm->msr_map);
201 struct v3_msr_hook * hook = NULL;
203 if (msr_map->update_map == NULL) {
204 PrintError("Trying to refresh an MSR map with no backend\n");
208 list_for_each_entry(hook, &(msr_map->hook_list), link) {
209 PrintDebug("updating MSR map for msr 0x%x\n", hook->msr);
210 msr_map->update_map(vm, hook->msr,
211 (hook->read == NULL) ? 0 : 1,
212 (hook->write == NULL) ? 0 : 1);
216 void v3_print_msr_map(struct v3_vm_info * vm) {
217 struct v3_msr_map * msr_map = &(vm->msr_map);
218 struct v3_msr_hook * hook = NULL;
220 list_for_each_entry(hook, &(msr_map->hook_list), link) {
221 V3_Print("MSR HOOK (MSR=0x%x) (read=0x%p) (write=0x%p)\n",
222 hook->msr, hook->read, hook->write);