VirtualBox

Ignore:
Timestamp:
Feb 8, 2018 3:56:43 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
120745
Message:

SUPDrv,VMM: Added SUPR0GetRawModeUsability() for checking whether we're allowed to modify CR4 under Hyper-V. It's called from VMMR0/ModuleInit and the result is cached in a global variable and checked before we call into RC.

Location:
trunk/src/VBox/HostDrivers/Support
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/SUPDrv.cpp

    r69500 r70917  
    204204    { "SUPR0GetSvmUsability",                   (void *)(uintptr_t)SUPR0GetSvmUsability },
    205205    { "SUPR0GetVmxUsability",                   (void *)(uintptr_t)SUPR0GetVmxUsability },
     206    { "SUPR0GetRawModeUsability",               (void *)(uintptr_t)SUPR0GetRawModeUsability },
    206207    { "SUPR0LockMem",                           (void *)(uintptr_t)SUPR0LockMem },
    207208    { "SUPR0LowAlloc",                          (void *)(uintptr_t)SUPR0LowAlloc },
     
    22582259
    22592260            /* execute */
    2260             pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.Caps);
     2261            pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.fCaps);
    22612262            if (RT_FAILURE(pReq->Hdr.rc))
    22622263                pReq->Hdr.cbOut = sizeof(pReq->Hdr);
     
    24772478
    24782479            /* execute */
    2479             pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.Caps);
     2480            pReq->Hdr.rc = SUPR0QueryVTCaps(pSession, &pReq->u.Out.fCaps);
    24802481            if (RT_FAILURE(pReq->Hdr.rc))
    24812482                pReq->Hdr.cbOut = sizeof(pReq->Hdr);
     
    40624063
    40634064/**
     4065 * Checks if raw-mode is usable on this system.
     4066 *
     4067 * The reasons why raw-mode isn't safe to use are host specific.  For example on
     4068 * Windows the Hyper-V root partition may perhapse not allow important bits in
     4069 * CR4 to be changed, which would make it impossible to do a world switch.
     4070 *
     4071 * @returns VBox status code.
     4072 */
     4073SUPR0DECL(int) SUPR0GetRawModeUsability(void)
     4074{
     4075#ifdef RT_OS_WINDOWS
     4076    return supdrvOSGetRawModeUsability();
     4077#else
     4078    return VINF_SUCCESS;
     4079#endif
     4080}
     4081
     4082
     4083
     4084/**
    40644085 * Checks if Intel VT-x feature is usable on this CPU.
    40654086 *
     
    40774098SUPR0DECL(int) SUPR0GetVmxUsability(bool *pfIsSmxModeAmbiguous)
    40784099{
    4079     uint64_t   u64FeatMsr;
     4100    uint64_t   fFeatMsr;
    40804101    bool       fMaybeSmxMode;
    40814102    bool       fMsrLocked;
     
    40874108    Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
    40884109
    4089     u64FeatMsr          = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
     4110    fFeatMsr            = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
    40904111    fMaybeSmxMode       = RT_BOOL(ASMGetCR4() & X86_CR4_SMXE);
    4091     fMsrLocked          = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_LOCK);
    4092     fSmxVmxAllowed      = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
    4093     fVmxAllowed         = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_VMXON);
     4112    fMsrLocked          = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_LOCK);
     4113    fSmxVmxAllowed      = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
     4114    fVmxAllowed         = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_VMXON);
    40944115    fIsSmxModeAmbiguous = false;
    40954116    rc                  = VERR_INTERNAL_ERROR_5;
     
    41494170            fSmxVmxHwSupport = true;
    41504171
    4151         u64FeatMsr |= MSR_IA32_FEATURE_CONTROL_LOCK
    4152                     | MSR_IA32_FEATURE_CONTROL_VMXON;
     4172        fFeatMsr |= MSR_IA32_FEATURE_CONTROL_LOCK
     4173                 | MSR_IA32_FEATURE_CONTROL_VMXON;
    41534174        if (fSmxVmxHwSupport)
    4154             u64FeatMsr |= MSR_IA32_FEATURE_CONTROL_SMX_VMXON;
     4175            fFeatMsr |= MSR_IA32_FEATURE_CONTROL_SMX_VMXON;
    41554176
    41564177        /*
    41574178         * Commit.
    41584179         */
    4159         ASMWrMsr(MSR_IA32_FEATURE_CONTROL, u64FeatMsr);
     4180        ASMWrMsr(MSR_IA32_FEATURE_CONTROL, fFeatMsr);
    41604181
    41614182        /*
    41624183         * Verify.
    41634184         */
    4164         u64FeatMsr = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
    4165         fMsrLocked = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_LOCK);
     4185        fFeatMsr = ASMRdMsr(MSR_IA32_FEATURE_CONTROL);
     4186        fMsrLocked = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_LOCK);
    41664187        if (fMsrLocked)
    41674188        {
    4168             fSmxVmxAllowed = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
    4169             fVmxAllowed    = RT_BOOL(u64FeatMsr & MSR_IA32_FEATURE_CONTROL_VMXON);
     4189            fSmxVmxAllowed = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
     4190            fVmxAllowed    = RT_BOOL(fFeatMsr & MSR_IA32_FEATURE_CONTROL_VMXON);
    41704191            if (   fVmxAllowed
    41714192                && (   !fSmxVmxHwSupport
    41724193                    || fSmxVmxAllowed))
    4173             {
    41744194                rc = VINF_SUCCESS;
    4175             }
    41764195            else
    41774196                rc = !fSmxVmxHwSupport ? VERR_VMX_MSR_VMX_ENABLE_FAILED : VERR_VMX_MSR_SMX_VMX_ENABLE_FAILED;
  • trunk/src/VBox/HostDrivers/Support/SUPDrvIOC.h

    r69500 r70917  
    214214 * @remarks 0x002a0000 is used by 5.1. The next version number must be 0x002b0000.
    215215 */
    216 #define SUPDRV_IOC_VERSION                              0x00290000
     216#define SUPDRV_IOC_VERSION                              0x00290001
    217217
    218218/** SUP_IOCTL_COOKIE. */
     
    12091209        {
    12101210            /** The VT capability dword. */
    1211             uint32_t        Caps;
     1211            uint32_t        fCaps;
    12121212        } Out;
    12131213    } u;
  • trunk/src/VBox/HostDrivers/Support/SUPDrvInternal.h

    r69500 r70917  
    850850void VBOXCALL   supdrvOSResumeVTxOnCpu(bool fSuspended);
    851851int  VBOXCALL   supdrvOSGetCurrentGdtRw(RTHCUINTPTR *pGdtRw);
     852int  VBOXCALL   supdrvOSGetRawModeUsability(void);
    852853
    853854/**
  • trunk/src/VBox/HostDrivers/Support/SUPLib.cpp

    r69500 r70917  
    276276        strcpy(CookieReq.u.In.szMagic, SUPCOOKIE_MAGIC);
    277277        CookieReq.u.In.u32ReqVersion = SUPDRV_IOC_VERSION;
    278         const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x00280000
    279                                    ? 0x00280002
     278        const uint32_t uMinVersion = (SUPDRV_IOC_VERSION & 0xffff0000) == 0x00290000
     279                                   ? 0x00290001
    280280                                   : SUPDRV_IOC_VERSION & 0xffff0000;
    281281        CookieReq.u.In.u32MinVersion = uMinVersion;
     
    16891689    Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
    16901690    Req.Hdr.rc = VERR_INTERNAL_ERROR;
    1691     Req.u.Out.Caps = 0;
     1691    Req.u.Out.fCaps = 0;
    16921692    int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_VT_CAPS, &Req, SUP_IOCTL_VT_CAPS_SIZE);
    16931693    if (RT_SUCCESS(rc))
     
    16951695        rc = Req.Hdr.rc;
    16961696        if (RT_SUCCESS(rc))
    1697             *pfCaps = Req.u.Out.Caps;
     1697            *pfCaps = Req.u.Out.fCaps;
    16981698    }
    16991699    return rc;
  • trunk/src/VBox/HostDrivers/Support/win/SUPDrv-win.cpp

    r69500 r70917  
    4646#include <iprt/spinlock.h>
    4747#include <iprt/string.h>
     48#include <iprt/x86.h>
    4849#include <VBox/log.h>
    4950#include <VBox/err.h>
     
    18731874
    18741875
     1876/**
     1877 * Checks whether we're allowed by Hyper-V to modify CR4.
     1878 */
     1879int  VBOXCALL supdrvOSGetRawModeUsability(void)
     1880{
     1881    int rc = VINF_SUCCESS;
     1882
     1883#ifdef RT_ARCH_AMD64
     1884    /*
     1885     * Broadwell running W10 17083.100:
     1886     *        CR4: 0x170678
     1887     *  Evil mask: 0x170638
     1888     *      X86_CR4_SMEP        - evil
     1889     *      X86_CR4_FSGSBASE    - evil
     1890     *      X86_CR4_PCIDE       - evil
     1891     *      X86_CR4_OSXSAVE     - evil
     1892     *      X86_CR4_OSFXSR      - evil
     1893     *      X86_CR4_OSXMMEEXCPT - evil
     1894     *      X86_CR4_PSE         - evil
     1895     *      X86_CR4_PAE         - evil
     1896     *      X86_CR4_MCE         - okay
     1897     *      X86_CR4_DE          - evil
     1898     */
     1899    if (ASMHasCpuId())
     1900    {
     1901        uint32_t cStd = ASMCpuId_EAX(0);
     1902        if (ASMIsValidStdRange(cStd))
     1903        {
     1904            uint32_t uIgn         = 0;
     1905            uint32_t fEdxFeatures = 0;
     1906            uint32_t fEcxFeatures = 0;
     1907            ASMCpuIdExSlow(1, 0, 0, 0, &uIgn, &uIgn, &fEcxFeatures, &fEdxFeatures);
     1908            if (fEcxFeatures & X86_CPUID_FEATURE_ECX_HVP)
     1909            {
     1910                RTCCUINTREG  const fOldFlags    = ASMIntDisableFlags();
     1911                RTCCUINTXREG const fCr4         = ASMGetCR4();
     1912
     1913                RTCCUINTXREG const fSafeToClear = X86_CR4_TSD      | X86_CR4_DE     | X86_CR4_PGE  | X86_CR4_PCE
     1914                                                | X86_CR4_FSGSBASE | X86_CR4_PCIDE  | X86_CR4_SMEP | X86_CR4_SMAP
     1915                                                | X86_CR4_OSXSAVE  | X86_CR4_OSFXSR | X86_CR4_OSXMMEEXCPT;
     1916                RTCCUINTXREG       fLoadCr4     = fCr4 & ~fSafeToClear;
     1917                RTCCUINTXREG const fCleared     = fCr4 & fSafeToClear;
     1918                if (!(fCleared & X86_CR4_TSD) && (fEdxFeatures & X86_CPUID_FEATURE_EDX_TSC))
     1919                    fLoadCr4 |= X86_CR4_TSD;
     1920                if (!(fCleared & X86_CR4_PGE) && (fEdxFeatures & X86_CPUID_FEATURE_EDX_PGE))
     1921                    fLoadCr4 |= X86_CR4_PGE;
     1922                __try
     1923                {
     1924                    ASMSetCR4(fLoadCr4);
     1925                }
     1926                __except(EXCEPTION_EXECUTE_HANDLER)
     1927                {
     1928                    rc = VERR_SUPDRV_NO_RAW_MODE_HYPER_V_ROOT;
     1929                }
     1930                if (RT_SUCCESS(rc))
     1931                    ASMSetCR4(fCr4);
     1932                ASMSetFlags(fOldFlags);
     1933            }
     1934        }
     1935    }
     1936#endif
     1937    return rc;
     1938}
     1939
     1940
    18751941#define MY_SystemLoadGdiDriverInSystemSpaceInformation  54
    18761942#define MY_SystemUnloadGdiDriverInformation             27
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette