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) 2011, Jack Lange <jacklange@cs.pitt.edu>
11 * All rights reserved.
13 * Author: Jack Lange <jacklange@cs.pitt.edu>
15 * This is free software. You are permitted to use,
16 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19 #include <palacios/vmm.h>
20 #include <palacios/vmm_cpuid.h>
21 #include <palacios/vmm_lowlevel.h>
22 #include <palacios/vm_guest.h>
37 void v3_init_cpuid_map(struct v3_vm_info * vm) {
38 vm->cpuid_map.map.rb_node = NULL;
40 // Setup default cpuid entries
47 int v3_deinit_cpuid_map(struct v3_vm_info * vm) {
48 struct rb_node * node = v3_rb_first(&(vm->cpuid_map.map));
49 struct v3_cpuid_hook * hook = NULL;
50 struct rb_node * tmp_node = NULL;
54 hook = rb_entry(node, struct v3_cpuid_hook, tree_node);
56 node = v3_rb_next(node);
58 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
67 static inline struct v3_cpuid_hook * __insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
68 struct rb_node ** p = &(vm->cpuid_map.map.rb_node);
69 struct rb_node * parent = NULL;
70 struct v3_cpuid_hook * tmp_hook = NULL;
74 tmp_hook = rb_entry(parent, struct v3_cpuid_hook, tree_node);
76 if (hook->cpuid < tmp_hook->cpuid) {
78 } else if (hook->cpuid > tmp_hook->cpuid) {
84 rb_link_node(&(hook->tree_node), parent, p);
90 static inline struct v3_cpuid_hook * insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
91 struct v3_cpuid_hook * ret;
93 if ((ret = __insert_cpuid_hook(vm, hook))) {
97 v3_rb_insert_color(&(hook->tree_node), &(vm->cpuid_map.map));
104 static struct v3_cpuid_hook * get_cpuid_hook(struct v3_vm_info * vm, uint32_t cpuid) {
105 struct rb_node * n = vm->cpuid_map.map.rb_node;
106 struct v3_cpuid_hook * hook = NULL;
109 hook = rb_entry(n, struct v3_cpuid_hook, tree_node);
111 if (cpuid < hook->cpuid) {
113 } else if (cpuid > hook->cpuid) {
125 static int mask_hook(struct guest_info * core, uint32_t cpuid,
126 uint32_t * eax, uint32_t * ebx,
127 uint32_t * ecx, uint32_t * edx,
129 struct masked_cpuid * mask = (struct masked_cpuid *)priv_data;
131 v3_cpuid(cpuid, eax, ebx, ecx, edx);
133 *eax &= ~(mask->rax_mask);
136 *ebx &= ~(mask->rbx_mask);
139 *ecx &= ~(mask->rcx_mask);
142 *edx &= ~(mask->rdx_mask);
148 int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid,
149 uint32_t rax_mask, uint32_t rax,
150 uint32_t rbx_mask, uint32_t rbx,
151 uint32_t rcx_mask, uint32_t rcx,
152 uint32_t rdx_mask, uint32_t rdx) {
153 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
156 struct masked_cpuid * mask = V3_Malloc(sizeof(struct masked_cpuid));
157 memset(mask, 0, sizeof(struct masked_cpuid));
159 mask->rax_mask = rax_mask;
161 mask->rbx_mask = rbx_mask;
163 mask->rcx_mask = rcx_mask;
165 mask->rdx_mask = rdx_mask;
168 if (v3_hook_cpuid(vm, cpuid, mask_hook, mask) == -1) {
169 PrintError("Error hooking cpuid %d\n", cpuid);
173 struct masked_cpuid * mask = NULL;
174 uint32_t tmp_val = 0;
176 if (hook->hook_fn != mask_hook) {
177 PrintError("trying to add fields to a fully hooked cpuid (%d)\n", cpuid);
181 mask = (struct masked_cpuid *)(hook->private_data);
183 if ((mask->rax_mask & rax_mask) ||
184 (mask->rbx_mask & rbx_mask) ||
185 (mask->rcx_mask & rcx_mask) ||
186 (mask->rdx_mask & rdx_mask)) {
187 PrintError("Trying to add fields that have already been masked\n");
191 if ((~rax_mask & rax) || (~rbx_mask & rbx) ||
192 (~rcx_mask & rcx) || (~rdx_mask & rdx)) {
193 PrintError("Invalid cpuid reg value (mask overrun)\n");
197 mask->rax_mask |= rax_mask;
198 mask->rbx_mask |= rbx_mask;
199 mask->rcx_mask |= rcx_mask;
200 mask->rdx_mask |= rdx_mask;
203 tmp_val = (~rax_mask | rax);
204 mask->rax &= tmp_val;
207 tmp_val = (~rbx_mask | rbx);
208 mask->rbx &= tmp_val;
211 tmp_val = (~rcx_mask | rcx);
212 mask->rcx &= tmp_val;
215 tmp_val = (~rdx_mask | rdx);
216 mask->rdx &= tmp_val;
223 int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) {
224 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
227 PrintError("Could not find cpuid to unhook (0x%x)\n", cpuid);
231 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
238 int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid,
239 int (*hook_fn)(struct guest_info * info, uint32_t cpuid, \
240 uint32_t * eax, uint32_t * ebx, \
241 uint32_t * ecx, uint32_t * edx, \
242 void * private_data),
243 void * private_data) {
244 struct v3_cpuid_hook * hook = NULL;
246 if (hook_fn == NULL) {
247 PrintError("CPUID hook requested with null handler\n");
251 hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook));
253 hook->private_data = private_data;
254 hook->hook_fn = hook_fn;
256 if (insert_cpuid_hook(vm, hook)) {
257 PrintError("Could not hook cpuid 0x%x (already hooked)\n", cpuid);
265 int v3_handle_cpuid(struct guest_info * info) {
266 uint32_t cpuid = info->vm_regs.rax;
267 struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid);
269 //PrintDebug("CPUID called for 0x%x\n", cpuid);
272 //PrintDebug("Calling passthrough handler\n");
273 // call the passthrough handler
275 (uint32_t *)&(info->vm_regs.rax),
276 (uint32_t *)&(info->vm_regs.rbx),
277 (uint32_t *)&(info->vm_regs.rcx),
278 (uint32_t *)&(info->vm_regs.rdx));
280 // PrintDebug("Calling hook function\n");
282 if (hook->hook_fn(info, cpuid,
283 (uint32_t *)&(info->vm_regs.rax),
284 (uint32_t *)&(info->vm_regs.rbx),
285 (uint32_t *)&(info->vm_regs.rcx),
286 (uint32_t *)&(info->vm_regs.rdx),
287 hook->private_data) == -1) {
288 PrintError("Error in cpuid handler for 0x%x\n", cpuid);
293 // PrintDebug("Cleaning up register contents\n");
295 info->vm_regs.rax &= 0x00000000ffffffffLL;
296 info->vm_regs.rbx &= 0x00000000ffffffffLL;
297 info->vm_regs.rcx &= 0x00000000ffffffffLL;
298 info->vm_regs.rdx &= 0x00000000ffffffffLL;