Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added support for unhooking an MSR
[palacios.git] / palacios / src / palacios / vmm_msr.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #include <palacios/vmm_msr.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest.h>
24
25
26 void v3_init_msr_map(struct v3_vm_info * vm) {
27     struct v3_msr_map * msr_map  = &(vm->msr_map);
28
29     PrintDebug("Initializing MSR map.\n");
30
31     INIT_LIST_HEAD(&(msr_map->hook_list));
32     msr_map->num_hooks = 0;
33
34     msr_map->arch_data = NULL;
35     msr_map->update_map = NULL;
36 }
37
38 int v3_handle_msr_write(struct guest_info * info) {
39     uint_t msr_num = info->vm_regs.rcx;
40     struct v3_msr msr_val;
41     struct v3_msr_hook * hook = NULL;
42
43     PrintDebug("MSR write for msr 0x%x\n", msr_num);
44
45     hook = v3_get_msr_hook(info->vm_info, msr_num);
46
47     if (!hook) {
48         PrintError("Hook for MSR write %d not found\n", msr_num);
49         return -1;
50     }
51
52     msr_val.value = 0;
53     msr_val.lo = info->vm_regs.rax;
54     msr_val.hi = info->vm_regs.rdx;
55
56     if (hook->write(info, msr_num, msr_val, hook->priv_data) == -1) {
57         PrintError("Error in MSR hook Write\n");
58         return -1;
59     }
60
61     info->rip += 2;
62
63     return 0;
64 }
65
66
67 int v3_handle_msr_read(struct guest_info * info) {
68     uint_t msr_num = info->vm_regs.rcx;
69     struct v3_msr msr_val;
70     struct v3_msr_hook * hook = NULL;
71
72     hook = v3_get_msr_hook(info->vm_info, msr_num);
73
74     if (!hook) {
75         PrintError("Hook for MSR read %d not found\n", msr_num);
76         return -1;
77     }
78
79     msr_val.value = 0;
80
81     if (hook->read(info, msr_num, &msr_val, hook->priv_data) == -1) {
82         PrintError("Error in MSR hook Read\n");
83         return -1;
84     }
85
86     info->vm_regs.rax = msr_val.lo;
87     info->vm_regs.rdx = msr_val.hi;
88
89     info->rip += 2;
90     return 0;
91 }
92
93 int v3_hook_msr(struct v3_vm_info * vm, uint_t msr, 
94                 int (*read)(struct guest_info * core, uint_t msr, struct v3_msr * dst, void * priv_data),
95                 int (*write)(struct guest_info * core, uint_t msr, struct v3_msr src, void * priv_data),
96                 void * priv_data) {
97
98     struct v3_msr_map * msr_map = &(vm->msr_map);
99     struct v3_msr_hook * hook = NULL;
100
101     hook = (struct v3_msr_hook *)V3_Malloc(sizeof(struct v3_msr_hook));
102
103     if (hook == NULL) {
104         PrintError("Could not allocate msr hook for MSR %d\n", msr);
105         return -1;
106     }
107
108     hook->read = read;
109     hook->write = write;
110     hook->msr = msr;
111     hook->priv_data = priv_data;
112
113     msr_map->num_hooks++;
114
115     list_add(&(hook->link), &(msr_map->hook_list));
116
117     if (msr_map->update_map) {
118         msr_map->update_map(vm, msr, 
119                             (read == NULL) ? 0 : 1,
120                             (write == NULL) ? 0 : 1);
121     }
122
123     return 0;
124 }
125
126
127 int v3_unhook_msr(struct v3_vm_info * vm, uint_t msr) {
128     struct v3_msr_hook * hook = v3_get_msr_hook(vm, msr);
129
130     if (hook == NULL) {
131         PrintError("Could not find MSR to unhook %u (0x%x)\n", msr, msr);
132         return -1;
133     }
134
135     list_del(&(hook->link));
136
137     if (vm->msr_map.update_map) {
138         vm->msr_map.update_map(vm, msr, 0, 0);
139     }
140
141     V3_Free(hook);
142
143     return 0;
144 }
145
146
147
148 struct v3_msr_hook * v3_get_msr_hook(struct v3_vm_info * vm, uint_t msr) {
149     struct v3_msr_map * msr_map = &(vm->msr_map);
150     struct v3_msr_hook * hook = NULL;
151
152     list_for_each_entry(hook, &(msr_map->hook_list), link) {
153         if (hook->msr == msr) {
154             return hook;
155         }
156     }
157
158     return NULL;
159 }
160
161
162 void v3_refresh_msr_map(struct v3_vm_info * vm) {
163     struct v3_msr_map * msr_map = &(vm->msr_map);
164     struct v3_msr_hook * hook = NULL;
165
166     if (msr_map->update_map == NULL) {
167         PrintError("Trying to refresh an MSR map with no backend\n");
168         return;
169     }
170
171     list_for_each_entry(hook, &(msr_map->hook_list), link) {
172         PrintDebug("updating MSR map for msr %d\n", hook->msr);
173         msr_map->update_map(vm, hook->msr,      
174                             (hook->read == NULL) ? 0 : 1,
175                             (hook->write == NULL) ? 0 : 1);
176     }
177 }
178
179 void v3_print_msr_map(struct v3_vm_info * vm) {
180     struct v3_msr_map * msr_map = &(vm->msr_map);
181     struct v3_msr_hook * hook = NULL;
182
183     list_for_each_entry(hook, &(msr_map->hook_list), link) {
184         V3_Print("MSR HOOK (MSR=0x%x) (read=0x%p) (write=0x%p)\n",
185                    hook->msr, hook->read, hook->write);
186     }
187 }