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 init_custom(struct v3_vm_info *vm)
41 EAX = maxid supported (1)
42 EBX = first 4 bytes of string
43 EDX = second 4 bytes of string
44 ECX = third 4 bytes of string
51 uint32_t ebx, ecx, edx;
53 memcpy(&ebx,"Virt",4);
54 memcpy(&edx,"ualV",4);
55 memcpy(&ecx,"3VEE",4);
58 // In the Intel Space, we are a VirtualV3VEE
59 // and our maximum cpuid is 0x1
60 v3_cpuid_add_fields(vm,0x0, // ID 0 (Vendor)
61 0xffffffff, 1, // Max CPUID is one
66 // In the AMD Space, we are a Virtual V3VEE
67 // and our maximum cpuid is 0x80000001
68 // other than the maximum cpuid, this is identical to Intel 0x0
70 v3_cpuid_add_fields(vm,0x80000000, // ID 8...0 (Vendor - AMD variant)
71 0xffffffff, 0x80000001, // Max CPUID is one
76 /* CPUID 1, EAX - Family, Model, Stepping
77 We are Family 16, Model 1, Stepping 1 (family 16 puts us in x86-64)
79 27:20 = extended family (extfam)
80 19:16 = extended model (extmod)
82 11:8 = base family (basfam)
83 7:4 = base model (basmod)
86 family = extfam+basefam, model=extmod:basmod
87 but we need to "top out" basefam first (0xf)
89 So we want: 0x00100f11
91 EBX is probably bogus here, since we need the apic ids
92 of the vcores, not the pcores
95 // in Intel Space, we are family 16, model 1, stepping 1
96 // and our other features are passthrough
97 v3_cpuid_add_fields(vm,0x1,
98 0xffffffff, 0x00100f11,
103 // In the AMD space, we are family 16, model 1, stepping 1
104 // with other features passthrough
105 // These other fields are *different* from Intel's 0x1, however
106 // in particular, long mode is here, even if it's an Intel...
107 v3_cpuid_add_fields(vm,0x80000001, // AMD variant
108 0xffffffff, 0x00100f11,
115 void v3_init_cpuid_map(struct v3_vm_info * vm) {
116 vm->cpuid_map.map.rb_node = NULL;
118 // Setup default cpuid entries
120 #ifdef V3_CONFIG_CUSTOM_CPUID
124 // Disable XSAVE (cpuid 0x01, ECX bit 26)
125 v3_cpuid_add_fields(vm, 0x01, 0, 0, 0, 0, (1 << 26), 0, 0, 0);
127 // Disable MONITOR/MWAIT (cpuid 0x01, ECX bit 3)
128 v3_cpuid_add_fields(vm, 0x01, 0, 0, 0, 0, (1 << 3), 0, 0, 0);
132 v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, 0, 0, (1 << 12), 0);
134 v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, 0, 0, (1 << 16), 0);
136 v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, 0, 0, (1 << 28), 0);
139 v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, (1 << 21), 0, 0, 0);
142 // Demarcate machine as a VM
143 v3_cpuid_add_fields(vm, 0x00000001,
146 0x80000000, 0x80000000,
152 v3_cpuid_add_fields(vm, 0x00000006, (1 << 2), 0, 0, 0, 0, 0, 0, 0);
159 int v3_deinit_cpuid_map(struct v3_vm_info * vm) {
160 struct rb_node * node = v3_rb_first(&(vm->cpuid_map.map));
161 struct v3_cpuid_hook * hook = NULL;
162 struct rb_node * tmp_node = NULL;
166 hook = rb_entry(node, struct v3_cpuid_hook, tree_node);
168 node = v3_rb_next(node);
170 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
179 static inline struct v3_cpuid_hook * __insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
180 struct rb_node ** p = &(vm->cpuid_map.map.rb_node);
181 struct rb_node * parent = NULL;
182 struct v3_cpuid_hook * tmp_hook = NULL;
186 tmp_hook = rb_entry(parent, struct v3_cpuid_hook, tree_node);
188 if (hook->cpuid < tmp_hook->cpuid) {
190 } else if (hook->cpuid > tmp_hook->cpuid) {
196 rb_link_node(&(hook->tree_node), parent, p);
202 static inline struct v3_cpuid_hook * insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
203 struct v3_cpuid_hook * ret;
205 if ((ret = __insert_cpuid_hook(vm, hook))) {
209 v3_rb_insert_color(&(hook->tree_node), &(vm->cpuid_map.map));
216 static struct v3_cpuid_hook * get_cpuid_hook(struct v3_vm_info * vm, uint32_t cpuid) {
217 struct rb_node * n = vm->cpuid_map.map.rb_node;
218 struct v3_cpuid_hook * hook = NULL;
221 hook = rb_entry(n, struct v3_cpuid_hook, tree_node);
223 if (cpuid < hook->cpuid) {
225 } else if (cpuid > hook->cpuid) {
237 static int mask_hook(struct guest_info * core, uint32_t cpuid,
238 uint32_t * eax, uint32_t * ebx,
239 uint32_t * ecx, uint32_t * edx,
241 struct masked_cpuid * mask = (struct masked_cpuid *)priv_data;
243 v3_cpuid(cpuid, eax, ebx, ecx, edx);
245 *eax &= ~(mask->rax_mask);
246 *eax |= (mask->rax & mask->rax_mask);
248 *ebx &= ~(mask->rbx_mask);
249 *ebx |= (mask->rbx & mask->rbx_mask);
251 *ecx &= ~(mask->rcx_mask);
252 *ecx |= (mask->rcx & mask->rcx_mask);
254 *edx &= ~(mask->rdx_mask);
255 *edx |= (mask->rdx & mask->rdx_mask);
262 /* This function allows you to reserve a set of bits in a given cpuid value
263 * For each cpuid return register you specify which bits you want to reserve in the mask.
264 * The value of those bits is set in the reg param.
265 * The values of the reserved bits are returned to the guest, when it reads the cpuid
267 int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid,
268 uint32_t rax_mask, uint32_t rax,
269 uint32_t rbx_mask, uint32_t rbx,
270 uint32_t rcx_mask, uint32_t rcx,
271 uint32_t rdx_mask, uint32_t rdx) {
272 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
275 if ((~rax_mask & rax) || (~rbx_mask & rbx) ||
276 (~rcx_mask & rcx) || (~rdx_mask & rdx)) {
277 PrintError(vm, VCORE_NONE, "Invalid cpuid reg value (mask overrun)\n");
283 struct masked_cpuid * mask = V3_Malloc(sizeof(struct masked_cpuid));
286 PrintError(vm, VCORE_NONE, "Unable to alocate space for cpu id mask\n");
290 memset(mask, 0, sizeof(struct masked_cpuid));
292 mask->rax_mask = rax_mask;
294 mask->rbx_mask = rbx_mask;
296 mask->rcx_mask = rcx_mask;
298 mask->rdx_mask = rdx_mask;
301 if (v3_hook_cpuid(vm, cpuid, mask_hook, mask) == -1) {
302 PrintError(vm, VCORE_NONE, "Error hooking cpuid %d\n", cpuid);
307 struct masked_cpuid * mask = NULL;
308 uint32_t tmp_val = 0;
310 if (hook->hook_fn != mask_hook) {
311 PrintError(vm, VCORE_NONE, "trying to add fields to a fully hooked cpuid (%d)\n", cpuid);
315 mask = (struct masked_cpuid *)(hook->private_data);
317 if ((mask->rax_mask & rax_mask) ||
318 (mask->rbx_mask & rbx_mask) ||
319 (mask->rcx_mask & rcx_mask) ||
320 (mask->rdx_mask & rdx_mask)) {
321 PrintError(vm, VCORE_NONE, "Trying to add fields that have already been masked\n");
325 mask->rax_mask |= rax_mask;
326 mask->rbx_mask |= rbx_mask;
327 mask->rcx_mask |= rcx_mask;
328 mask->rdx_mask |= rdx_mask;
331 tmp_val = (~rax_mask | rax);
332 mask->rax &= tmp_val;
335 tmp_val = (~rbx_mask | rbx);
336 mask->rbx &= tmp_val;
339 tmp_val = (~rcx_mask | rcx);
340 mask->rcx &= tmp_val;
343 tmp_val = (~rdx_mask | rdx);
344 mask->rdx &= tmp_val;
351 int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) {
352 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
355 PrintError(vm, VCORE_NONE, "Could not find cpuid to unhook (0x%x)\n", cpuid);
359 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
366 int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid,
367 int (*hook_fn)(struct guest_info * info, uint32_t cpuid, \
368 uint32_t * eax, uint32_t * ebx, \
369 uint32_t * ecx, uint32_t * edx, \
370 void * private_data),
371 void * private_data) {
372 struct v3_cpuid_hook * hook = NULL;
374 if (hook_fn == NULL) {
375 PrintError(vm, VCORE_NONE, "CPUID hook requested with null handler\n");
379 hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook));
382 PrintError(vm, VCORE_NONE, "Cannot allocate memory to hook cpu id\n");
387 hook->private_data = private_data;
388 hook->hook_fn = hook_fn;
390 if (insert_cpuid_hook(vm, hook)) {
391 PrintError(vm, VCORE_NONE, "Could not hook cpuid 0x%x (already hooked)\n", cpuid);
399 int v3_handle_cpuid(struct guest_info * info) {
400 uint32_t cpuid = info->vm_regs.rax;
401 struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid);
403 //PrintDebug(info->vm_info, info, "CPUID called for 0x%x\n", cpuid);
406 //PrintDebug(info->vm_info, info, "Calling passthrough handler\n");
407 // call the passthrough handler
409 (uint32_t *)&(info->vm_regs.rax),
410 (uint32_t *)&(info->vm_regs.rbx),
411 (uint32_t *)&(info->vm_regs.rcx),
412 (uint32_t *)&(info->vm_regs.rdx));
414 // PrintDebug(info->vm_info, info, "Calling hook function\n");
416 if (hook->hook_fn(info, cpuid,
417 (uint32_t *)&(info->vm_regs.rax),
418 (uint32_t *)&(info->vm_regs.rbx),
419 (uint32_t *)&(info->vm_regs.rcx),
420 (uint32_t *)&(info->vm_regs.rdx),
421 hook->private_data) == -1) {
422 PrintError(info->vm_info, info, "Error in cpuid handler for 0x%x\n", cpuid);
427 // PrintDebug(info->vm_info, info, "Cleaning up register contents\n");
429 info->vm_regs.rax &= 0x00000000ffffffffLL;
430 info->vm_regs.rbx &= 0x00000000ffffffffLL;
431 info->vm_regs.rcx &= 0x00000000ffffffffLL;
432 info->vm_regs.rdx &= 0x00000000ffffffffLL;