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
43 // Disable XSAVE (cpuid 0x01, ECX bit 26)
44 v3_cpuid_add_fields(vm, 0x01, 0, 0, 0, 0, (1 << 26), 0, 0, 0);
51 int v3_deinit_cpuid_map(struct v3_vm_info * vm) {
52 struct rb_node * node = v3_rb_first(&(vm->cpuid_map.map));
53 struct v3_cpuid_hook * hook = NULL;
54 struct rb_node * tmp_node = NULL;
58 hook = rb_entry(node, struct v3_cpuid_hook, tree_node);
60 node = v3_rb_next(node);
62 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
71 static inline struct v3_cpuid_hook * __insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
72 struct rb_node ** p = &(vm->cpuid_map.map.rb_node);
73 struct rb_node * parent = NULL;
74 struct v3_cpuid_hook * tmp_hook = NULL;
78 tmp_hook = rb_entry(parent, struct v3_cpuid_hook, tree_node);
80 if (hook->cpuid < tmp_hook->cpuid) {
82 } else if (hook->cpuid > tmp_hook->cpuid) {
88 rb_link_node(&(hook->tree_node), parent, p);
94 static inline struct v3_cpuid_hook * insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
95 struct v3_cpuid_hook * ret;
97 if ((ret = __insert_cpuid_hook(vm, hook))) {
101 v3_rb_insert_color(&(hook->tree_node), &(vm->cpuid_map.map));
108 static struct v3_cpuid_hook * get_cpuid_hook(struct v3_vm_info * vm, uint32_t cpuid) {
109 struct rb_node * n = vm->cpuid_map.map.rb_node;
110 struct v3_cpuid_hook * hook = NULL;
113 hook = rb_entry(n, struct v3_cpuid_hook, tree_node);
115 if (cpuid < hook->cpuid) {
117 } else if (cpuid > hook->cpuid) {
129 static int mask_hook(struct guest_info * core, uint32_t cpuid,
130 uint32_t * eax, uint32_t * ebx,
131 uint32_t * ecx, uint32_t * edx,
133 struct masked_cpuid * mask = (struct masked_cpuid *)priv_data;
135 v3_cpuid(cpuid, eax, ebx, ecx, edx);
137 *eax &= ~(mask->rax_mask);
138 *eax |= (mask->rax & mask->rax_mask);
140 *ebx &= ~(mask->rbx_mask);
141 *ebx |= (mask->rbx & mask->rbx_mask);
143 *ecx &= ~(mask->rcx_mask);
144 *ecx |= (mask->rcx & mask->rcx_mask);
146 *edx &= ~(mask->rdx_mask);
147 *edx |= (mask->rdx & mask->rdx_mask);
154 /* This function allows you to reserve a set of bits in a given cpuid value
155 * For each cpuid return register you specify which bits you want to reserve in the mask.
156 * The value of those bits is set in the reg param.
157 * The values of the reserved bits are returned to the guest, when it reads the cpuid
159 int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid,
160 uint32_t rax_mask, uint32_t rax,
161 uint32_t rbx_mask, uint32_t rbx,
162 uint32_t rcx_mask, uint32_t rcx,
163 uint32_t rdx_mask, uint32_t rdx) {
164 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
167 if ((~rax_mask & rax) || (~rbx_mask & rbx) ||
168 (~rcx_mask & rcx) || (~rdx_mask & rdx)) {
169 PrintError("Invalid cpuid reg value (mask overrun)\n");
175 struct masked_cpuid * mask = V3_Malloc(sizeof(struct masked_cpuid));
178 PrintError("Unable to alocate space for cpu id mask\n");
182 memset(mask, 0, sizeof(struct masked_cpuid));
184 mask->rax_mask = rax_mask;
186 mask->rbx_mask = rbx_mask;
188 mask->rcx_mask = rcx_mask;
190 mask->rdx_mask = rdx_mask;
193 if (v3_hook_cpuid(vm, cpuid, mask_hook, mask) == -1) {
194 PrintError("Error hooking cpuid %d\n", cpuid);
199 struct masked_cpuid * mask = NULL;
200 uint32_t tmp_val = 0;
202 if (hook->hook_fn != mask_hook) {
203 PrintError("trying to add fields to a fully hooked cpuid (%d)\n", cpuid);
207 mask = (struct masked_cpuid *)(hook->private_data);
209 if ((mask->rax_mask & rax_mask) ||
210 (mask->rbx_mask & rbx_mask) ||
211 (mask->rcx_mask & rcx_mask) ||
212 (mask->rdx_mask & rdx_mask)) {
213 PrintError("Trying to add fields that have already been masked\n");
217 mask->rax_mask |= rax_mask;
218 mask->rbx_mask |= rbx_mask;
219 mask->rcx_mask |= rcx_mask;
220 mask->rdx_mask |= rdx_mask;
223 tmp_val = (~rax_mask | rax);
224 mask->rax &= tmp_val;
227 tmp_val = (~rbx_mask | rbx);
228 mask->rbx &= tmp_val;
231 tmp_val = (~rcx_mask | rcx);
232 mask->rcx &= tmp_val;
235 tmp_val = (~rdx_mask | rdx);
236 mask->rdx &= tmp_val;
243 int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) {
244 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
247 PrintError("Could not find cpuid to unhook (0x%x)\n", cpuid);
251 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
258 int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid,
259 int (*hook_fn)(struct guest_info * info, uint32_t cpuid, \
260 uint32_t * eax, uint32_t * ebx, \
261 uint32_t * ecx, uint32_t * edx, \
262 void * private_data),
263 void * private_data) {
264 struct v3_cpuid_hook * hook = NULL;
266 if (hook_fn == NULL) {
267 PrintError("CPUID hook requested with null handler\n");
271 hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook));
274 PrintError("Cannot allocate memory to hook cpu id\n");
279 hook->private_data = private_data;
280 hook->hook_fn = hook_fn;
282 if (insert_cpuid_hook(vm, hook)) {
283 PrintError("Could not hook cpuid 0x%x (already hooked)\n", cpuid);
291 int v3_handle_cpuid(struct guest_info * info) {
292 uint32_t cpuid = info->vm_regs.rax;
293 struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid);
295 //PrintDebug("CPUID called for 0x%x\n", cpuid);
298 //PrintDebug("Calling passthrough handler\n");
299 // call the passthrough handler
301 (uint32_t *)&(info->vm_regs.rax),
302 (uint32_t *)&(info->vm_regs.rbx),
303 (uint32_t *)&(info->vm_regs.rcx),
304 (uint32_t *)&(info->vm_regs.rdx));
306 // PrintDebug("Calling hook function\n");
308 if (hook->hook_fn(info, cpuid,
309 (uint32_t *)&(info->vm_regs.rax),
310 (uint32_t *)&(info->vm_regs.rbx),
311 (uint32_t *)&(info->vm_regs.rcx),
312 (uint32_t *)&(info->vm_regs.rdx),
313 hook->private_data) == -1) {
314 PrintError("Error in cpuid handler for 0x%x\n", cpuid);
319 // PrintDebug("Cleaning up register contents\n");
321 info->vm_regs.rax &= 0x00000000ffffffffLL;
322 info->vm_regs.rbx &= 0x00000000ffffffffLL;
323 info->vm_regs.rcx &= 0x00000000ffffffffLL;
324 info->vm_regs.rdx &= 0x00000000ffffffffLL;