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>
26 void v3_init_msr_map(struct guest_info * info) {
27 struct v3_msr_map * msr_map = &(info->msr_map);
29 INIT_LIST_HEAD(&(msr_map->hook_list));
30 msr_map->num_hooks = 0;
32 msr_map->arch_data = NULL;
33 msr_map->update_map = NULL;
36 int v3_handle_msr_write(struct guest_info * info) {
37 uint_t msr_num = info->vm_regs.rcx;
38 struct v3_msr msr_val;
39 struct v3_msr_hook * hook = NULL;
41 hook = v3_get_msr_hook(info, msr_num);
44 PrintError("Hook for MSR write %d not found\n", msr_num);
49 msr_val.lo = info->vm_regs.rax;
50 msr_val.hi = info->vm_regs.rdx;
52 if (hook->write(msr_num, msr_val, hook->priv_data) == -1) {
53 PrintError("Error in MSR hook Write\n");
63 int v3_handle_msr_read(struct guest_info * info) {
64 uint_t msr_num = info->vm_regs.rcx;
65 struct v3_msr msr_val;
66 struct v3_msr_hook * hook = NULL;
68 hook = v3_get_msr_hook(info, msr_num);
71 PrintError("Hook for MSR read %d not found\n", msr_num);
77 if (hook->read(msr_num, &msr_val, hook->priv_data) == -1) {
78 PrintError("Error in MSR hook Read\n");
82 info->vm_regs.rax = msr_val.lo;
83 info->vm_regs.rdx = msr_val.hi;
89 int v3_hook_msr(struct guest_info * info, uint_t msr,
90 int (*read)(uint_t msr, struct v3_msr * dst, void * priv_data),
91 int (*write)(uint_t msr, struct v3_msr src, void * priv_data),
94 struct v3_msr_map * msr_map = &(info->msr_map);
95 struct v3_msr_hook * hook = NULL;
97 hook = (struct v3_msr_hook *)V3_Malloc(sizeof(struct v3_msr_hook));
100 PrintError("Could not allocate msr hook for MSR %d\n", msr);
107 hook->priv_data = priv_data;
109 msr_map->num_hooks++;
111 list_add(&(hook->link), &(msr_map->hook_list));
113 if (msr_map->update_map) {
114 msr_map->update_map(info, msr,
115 (read == NULL) ? 0 : 1,
116 (write == NULL) ? 0 : 1);
123 int v3_unhook_msr(struct guest_info * info, uint_t msr) {
124 PrintError("Unhooking MSRs currently not supported\n");
130 struct v3_msr_hook * v3_get_msr_hook(struct guest_info * info, uint_t msr) {
131 struct v3_msr_map * msr_map = &(info->msr_map);
132 struct v3_msr_hook * hook = NULL;
134 list_for_each_entry(hook, &(msr_map->hook_list), link) {
135 if (hook->msr == msr) {
144 void v3_refresh_msr_map(struct guest_info * info) {
145 struct v3_msr_map * msr_map = &(info->msr_map);
146 struct v3_msr_hook * hook = NULL;
148 if (msr_map->update_map == NULL) {
149 PrintError("Trying to refresh an MSR map with no backend\n");
153 list_for_each_entry(hook, &(msr_map->hook_list), link) {
154 msr_map->update_map(info, hook->msr,
155 (hook->read == NULL) ? 0 : 1,
156 (hook->write == NULL) ? 0 : 1);
160 void v3_print_msr_map(struct guest_info * info) {
161 struct v3_msr_map * msr_map = &(info->msr_map);
162 struct v3_msr_hook * hook = NULL;
164 list_for_each_entry(hook, &(msr_map->hook_list), link) {
165 PrintDebug("MSR HOOK (MSR=%d) (read=0x%p) (write=0x%p)\n",
166 hook->msr, hook->read, hook->write);