1 | /* $Id: CPUMR0.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * CPUM - Host Context Ring 0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_CPUM
|
---|
27 | #include <VBox/cpum.h>
|
---|
28 | #include "CPUMInternal.h"
|
---|
29 | #include <VBox/vm.h>
|
---|
30 | #include <VBox/x86.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Does Ring-0 CPUM initialization.
|
---|
41 | *
|
---|
42 | * This is mainly to check that the Host CPU mode is compatible
|
---|
43 | * with VBox.
|
---|
44 | *
|
---|
45 | * @returns VBox status code.
|
---|
46 | * @param pVM The VM to operate on.
|
---|
47 | */
|
---|
48 | CPUMR0DECL(int) CPUMR0Init(PVM pVM)
|
---|
49 | {
|
---|
50 | LogFlow(("CPUMR0Init: %p\n", pVM));
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Check CR0 & CR4 flags.
|
---|
54 | */
|
---|
55 | uint32_t u32CR0 = ASMGetCR0();
|
---|
56 | if ((u32CR0 & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PE | X86_CR0_PG)) /* a bit paranoid perhaps.. */
|
---|
57 | {
|
---|
58 | Log(("CPUMR0Init: PE or PG not set. cr0=%#x\n", u32CR0));
|
---|
59 | return VERR_UNSUPPORTED_CPU_MODE;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Check for sysenter if it's used.
|
---|
64 | */
|
---|
65 | if (ASMHasCpuId())
|
---|
66 | {
|
---|
67 | uint32_t u32CpuVersion;
|
---|
68 | uint32_t u32Dummy;
|
---|
69 | uint32_t u32Features;
|
---|
70 | ASMCpuId(1, &u32CpuVersion, &u32Dummy, &u32Dummy, &u32Features);
|
---|
71 | uint32_t u32Family = u32CpuVersion >> 8;
|
---|
72 | uint32_t u32Model = (u32CpuVersion >> 4) & 0xF;
|
---|
73 | uint32_t u32Stepping = u32CpuVersion & 0xF;
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Intel docs claim you should test both the flag and family, model & stepping.
|
---|
77 | * Some Pentium Pro cpus have the SEP cpuid flag set, but don't support it.
|
---|
78 | */
|
---|
79 | if ( (u32Features & X86_CPUID_FEATURE_EDX_SEP)
|
---|
80 | && !(u32Family == 6 && u32Model < 3 && u32Stepping < 3))
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Read the MSR and see if it's in use or not.
|
---|
84 | */
|
---|
85 | uint32_t u32 = ASMRdMsr_Low(MSR_IA32_SYSENTER_CS);
|
---|
86 | if (u32)
|
---|
87 | {
|
---|
88 | pVM->cpum.s.fUseFlags |= CPUM_USE_SYSENTER;
|
---|
89 | Log(("CPUMR0Init: host uses sysenter cs=%08x%08x\n", ASMRdMsr_High(MSR_IA32_SYSENTER_CS), u32));
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** @todo check for AMD and syscall!!!!!! */
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Check if debug registers are armed.
|
---|
99 | */
|
---|
100 | uint32_t u32DR7 = ASMGetDR7();
|
---|
101 | if (u32DR7 & X86_DR7_ENABLED_MASK)
|
---|
102 | {
|
---|
103 | pVM->cpum.s.fUseFlags |= CPUM_USE_DEBUG_REGS_HOST;
|
---|
104 | Log(("CPUMR0Init: host uses debug registers (dr7=%x)\n", u32DR7));
|
---|
105 | }
|
---|
106 |
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|