X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_cpuid.c;h=c1700898b3e75636e97e95bd41ac28f226479951;hb=02d617c8f625ad5da6c4c9b78b224e84baac5d08;hp=ce7c24440c855552361bcbeab44766171556f363;hpb=72182c5e1c7d08d4b274bf300d49b523d1983b3e;p=palacios.git diff --git a/palacios/src/palacios/vmm_cpuid.c b/palacios/src/palacios/vmm_cpuid.c index ce7c244..c170089 100644 --- a/palacios/src/palacios/vmm_cpuid.c +++ b/palacios/src/palacios/vmm_cpuid.c @@ -43,6 +43,30 @@ void v3_init_cpuid_map(struct v3_vm_info * vm) { // Disable XSAVE (cpuid 0x01, ECX bit 26) v3_cpuid_add_fields(vm, 0x01, 0, 0, 0, 0, (1 << 26), 0, 0, 0); + // Disable MONITOR/MWAIT (cpuid 0x01, ECX bit 3) + v3_cpuid_add_fields(vm, 0x01, 0, 0, 0, 0, (1 << 3), 0, 0, 0); + + + // disable MTRR + v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, 0, 0, (1 << 12), 0); + // disable PAT + v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, 0, 0, (1 << 16), 0); + // disable X2APIC + v3_cpuid_add_fields(vm, 0x00000001, 0, 0, 0, 0, (1 << 21), 0, 0, 0); + + + // Demarcate machine as a VM + v3_cpuid_add_fields(vm, 0x00000001, + 0, 0, + 0, 0, + 0x80000000, 0x80000000, + 0, 0 + ); + + + // disable ARAT + v3_cpuid_add_fields(vm, 0x00000006, (1 << 2), 0, 0, 0, 0, 0, 0, 0); + } @@ -135,20 +159,27 @@ static int mask_hook(struct guest_info * core, uint32_t cpuid, v3_cpuid(cpuid, eax, ebx, ecx, edx); *eax &= ~(mask->rax_mask); - *eax |= mask->rax; + *eax |= (mask->rax & mask->rax_mask); *ebx &= ~(mask->rbx_mask); - *ebx |= mask->rbx; + *ebx |= (mask->rbx & mask->rbx_mask); *ecx &= ~(mask->rcx_mask); - *ecx |= mask->rcx; + *ecx |= (mask->rcx & mask->rcx_mask); *edx &= ~(mask->rdx_mask); - *edx |= mask->rdx; + *edx |= (mask->rdx & mask->rdx_mask); return 0; } + + +/* This function allows you to reserve a set of bits in a given cpuid value + * For each cpuid return register you specify which bits you want to reserve in the mask. + * The value of those bits is set in the reg param. + * The values of the reserved bits are returned to the guest, when it reads the cpuid + */ int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid, uint32_t rax_mask, uint32_t rax, uint32_t rbx_mask, uint32_t rbx, @@ -156,8 +187,22 @@ int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid, uint32_t rdx_mask, uint32_t rdx) { struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid); + + if ((~rax_mask & rax) || (~rbx_mask & rbx) || + (~rcx_mask & rcx) || (~rdx_mask & rdx)) { + PrintError(vm, VCORE_NONE, "Invalid cpuid reg value (mask overrun)\n"); + return -1; + } + + if (hook == NULL) { struct masked_cpuid * mask = V3_Malloc(sizeof(struct masked_cpuid)); + + if (!mask) { + PrintError(vm, VCORE_NONE, "Unable to alocate space for cpu id mask\n"); + return -1; + } + memset(mask, 0, sizeof(struct masked_cpuid)); mask->rax_mask = rax_mask; @@ -170,7 +215,8 @@ int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid, mask->rdx = rdx; if (v3_hook_cpuid(vm, cpuid, mask_hook, mask) == -1) { - PrintError("Error hooking cpuid %d\n", cpuid); + PrintError(vm, VCORE_NONE, "Error hooking cpuid %d\n", cpuid); + V3_Free(mask); return -1; } } else { @@ -178,7 +224,7 @@ int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid, uint32_t tmp_val = 0; if (hook->hook_fn != mask_hook) { - PrintError("trying to add fields to a fully hooked cpuid (%d)\n", cpuid); + PrintError(vm, VCORE_NONE, "trying to add fields to a fully hooked cpuid (%d)\n", cpuid); return -1; } @@ -188,13 +234,7 @@ int v3_cpuid_add_fields(struct v3_vm_info * vm, uint32_t cpuid, (mask->rbx_mask & rbx_mask) || (mask->rcx_mask & rcx_mask) || (mask->rdx_mask & rdx_mask)) { - PrintError("Trying to add fields that have already been masked\n"); - return -1; - } - - if ((~rax_mask & rax) || (~rbx_mask & rbx) || - (~rcx_mask & rcx) || (~rdx_mask & rdx)) { - PrintError("Invalid cpuid reg value (mask overrun)\n"); + PrintError(vm, VCORE_NONE, "Trying to add fields that have already been masked\n"); return -1; } @@ -228,7 +268,7 @@ int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) { struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid); if (hook == NULL) { - PrintError("Could not find cpuid to unhook (0x%x)\n", cpuid); + PrintError(vm, VCORE_NONE, "Could not find cpuid to unhook (0x%x)\n", cpuid); return -1; } @@ -248,17 +288,23 @@ int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid, struct v3_cpuid_hook * hook = NULL; if (hook_fn == NULL) { - PrintError("CPUID hook requested with null handler\n"); + PrintError(vm, VCORE_NONE, "CPUID hook requested with null handler\n"); return -1; } hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook)); + + if (!hook) { + PrintError(vm, VCORE_NONE, "Cannot allocate memory to hook cpu id\n"); + return -1; + } + hook->cpuid = cpuid; hook->private_data = private_data; hook->hook_fn = hook_fn; if (insert_cpuid_hook(vm, hook)) { - PrintError("Could not hook cpuid 0x%x (already hooked)\n", cpuid); + PrintError(vm, VCORE_NONE, "Could not hook cpuid 0x%x (already hooked)\n", cpuid); V3_Free(hook); return -1; } @@ -270,10 +316,10 @@ int v3_handle_cpuid(struct guest_info * info) { uint32_t cpuid = info->vm_regs.rax; struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid); - //PrintDebug("CPUID called for 0x%x\n", cpuid); + //PrintDebug(info->vm_info, info, "CPUID called for 0x%x\n", cpuid); if (hook == NULL) { - //PrintDebug("Calling passthrough handler\n"); + //PrintDebug(info->vm_info, info, "Calling passthrough handler\n"); // call the passthrough handler v3_cpuid(cpuid, (uint32_t *)&(info->vm_regs.rax), @@ -281,7 +327,7 @@ int v3_handle_cpuid(struct guest_info * info) { (uint32_t *)&(info->vm_regs.rcx), (uint32_t *)&(info->vm_regs.rdx)); } else { - // PrintDebug("Calling hook function\n"); + // PrintDebug(info->vm_info, info, "Calling hook function\n"); if (hook->hook_fn(info, cpuid, (uint32_t *)&(info->vm_regs.rax), @@ -289,12 +335,12 @@ int v3_handle_cpuid(struct guest_info * info) { (uint32_t *)&(info->vm_regs.rcx), (uint32_t *)&(info->vm_regs.rdx), hook->private_data) == -1) { - PrintError("Error in cpuid handler for 0x%x\n", cpuid); + PrintError(info->vm_info, info, "Error in cpuid handler for 0x%x\n", cpuid); return -1; } } - // PrintDebug("Cleaning up register contents\n"); + // PrintDebug(info->vm_info, info, "Cleaning up register contents\n"); info->vm_regs.rax &= 0x00000000ffffffffLL; info->vm_regs.rbx &= 0x00000000ffffffffLL;