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 symbiotic interface and a number of other small changes
[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 guest_info * info) {
27     struct v3_msr_map * msr_map  = &(info->msr_map);
28
29     INIT_LIST_HEAD(&(msr_map->hook_list));
30     msr_map->num_hooks = 0;
31
32     msr_map->arch_data = NULL;
33     msr_map->update_map = NULL;
34 }
35
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;
40
41     PrintDebug("MSR write for msr 0x%x\n", msr_num);
42
43     hook = v3_get_msr_hook(info, msr_num);
44
45     if (!hook) {
46         PrintError("Hook for MSR write %d not found\n", msr_num);
47         return -1;
48     }
49
50     msr_val.value = 0;
51     msr_val.lo = info->vm_regs.rax;
52     msr_val.hi = info->vm_regs.rdx;
53
54     if (hook->write(msr_num, msr_val, hook->priv_data) == -1) {
55         PrintError("Error in MSR hook Write\n");
56         return -1;
57     }
58
59     info->rip += 2;
60
61     return 0;
62 }
63
64
65 int v3_handle_msr_read(struct guest_info * info) {
66     uint_t msr_num = info->vm_regs.rcx;
67     struct v3_msr msr_val;
68     struct v3_msr_hook * hook = NULL;
69
70     hook = v3_get_msr_hook(info, msr_num);
71
72     if (!hook) {
73         PrintError("Hook for MSR read %d not found\n", msr_num);
74         return -1;
75     }
76
77     msr_val.value = 0;
78
79     if (hook->read(msr_num, &msr_val, hook->priv_data) == -1) {
80         PrintError("Error in MSR hook Read\n");
81         return -1;
82     }
83
84     info->vm_regs.rax = msr_val.lo;
85     info->vm_regs.rdx = msr_val.hi;
86
87     info->rip += 2;
88     return 0;
89 }
90
91 int v3_hook_msr(struct guest_info * info, uint_t msr, 
92                 int (*read)(uint_t msr, struct v3_msr * dst, void * priv_data),
93                 int (*write)(uint_t msr, struct v3_msr src, void * priv_data),
94                 void * priv_data) {
95
96     struct v3_msr_map * msr_map = &(info->msr_map);
97     struct v3_msr_hook * hook = NULL;
98
99     hook = (struct v3_msr_hook *)V3_Malloc(sizeof(struct v3_msr_hook));
100
101     if (hook == NULL) {
102         PrintError("Could not allocate msr hook for MSR %d\n", msr);
103         return -1;
104     }
105
106     hook->read = read;
107     hook->write = write;
108     hook->msr = msr;
109     hook->priv_data = priv_data;
110
111     msr_map->num_hooks++;
112
113     list_add(&(hook->link), &(msr_map->hook_list));
114
115     if (msr_map->update_map) {
116         msr_map->update_map(info, msr, 
117                             (read == NULL) ? 0 : 1,
118                             (write == NULL) ? 0 : 1);
119     }
120
121     return 0;
122 }
123
124
125 int v3_unhook_msr(struct guest_info * info, uint_t msr) {
126     PrintError("Unhooking MSRs currently not supported\n");
127     return -1;
128 }
129
130
131
132 struct v3_msr_hook * v3_get_msr_hook(struct guest_info * info, uint_t msr) {
133     struct v3_msr_map * msr_map = &(info->msr_map);
134     struct v3_msr_hook * hook = NULL;
135
136     list_for_each_entry(hook, &(msr_map->hook_list), link) {
137         if (hook->msr == msr) {
138             return hook;
139         }
140     }
141
142     return NULL;
143 }
144
145
146 void v3_refresh_msr_map(struct guest_info * info) {
147     struct v3_msr_map * msr_map = &(info->msr_map);
148     struct v3_msr_hook * hook = NULL;
149
150     if (msr_map->update_map == NULL) {
151         PrintError("Trying to refresh an MSR map with no backend\n");
152         return;
153     }
154
155     list_for_each_entry(hook, &(msr_map->hook_list), link) {
156         PrintDebug("updating MSR map for msr %d\n", hook->msr);
157         msr_map->update_map(info, hook->msr,    
158                             (hook->read == NULL) ? 0 : 1,
159                             (hook->write == NULL) ? 0 : 1);
160     }
161 }
162
163 void v3_print_msr_map(struct guest_info * info) {
164     struct v3_msr_map * msr_map = &(info->msr_map);
165     struct v3_msr_hook * hook = NULL;
166
167     list_for_each_entry(hook, &(msr_map->hook_list), link) {
168         V3_Print("MSR HOOK (MSR=0x%x) (read=0x%p) (write=0x%p)\n",
169                    hook->msr, hook->read, hook->write);
170     }
171 }