VirtualBox

Ignore:
Timestamp:
Sep 19, 2020 3:33:04 PM (4 years ago)
Author:
vboxsync
Message:

Runtime/mp-r0drv-nt.cpp: Dynamically determine the size of the KAFFINITY_EX structure as it is not static across Windows versions (increased lately with W10 20H2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r0drv/nt/mp-r0drv-nt.cpp

    r86175 r86176  
    3737#include <iprt/log.h>
    3838#include <iprt/mem.h>
     39#include <iprt/string.h>
    3940#include <iprt/time.h>
    4041#include "r0drv/mp-r0drv.h"
     
    122123/** The handle of the rtR0NtMpProcessorChangeCallback registration. */
    123124static PVOID                            g_pvMpCpuChangeCallback = NULL;
     125/** Size of the KAFFINITY_EX structure.
     126 * This increased from 20 to 32 bitmap words in the 2020 H2 windows 10 release
     127 * (i.e. 1280 to 2048 CPUs).  We expect this to increase in the future. */
     128static size_t                           g_cbRtMpNtKaffinityEx = RT_UOFFSETOF(KAFFINITY_EX, Bitmap)
     129                                                              + RT_SIZEOFMEMB(KAFFINITY_EX, Bitmap[0]) * 256;
     130/** The size value of the KAFFINITY_EX structure. */
     131static uint16_t                         g_cRtMpNtKaffinityExEntries = 256;
    124132
    125133
     
    598606        && g_pfnrtKeGetProcessorIndexFromNumber)
    599607    {
    600         g_pfnrtMpPokeCpuWorker = rtMpPokeCpuUsingHalRequestIpiW7Plus;
    601         DbgPrint("IPRT: RTMpPoke => rtMpPokeCpuUsingHalRequestIpiW7Plus\n");
    602     }
    603     else if (pOsVerInfo->uMajorVer >= 6 && g_pfnrtKeIpiGenericCall)
     608        /* Determine the real size of the KAFFINITY_EX structure. */
     609        union
     610        {
     611            KAFFINITY_EX Struct;
     612            uint8_t      ab[_4K + 8]; /* This should suffice for determining the real size. */
     613        } u;
     614        RT_ZERO(u);
     615        size_t const cMaxEntries = (sizeof(u) - RT_UOFFSETOF(KAFFINITY_EX, Bitmap[0])) / sizeof(u.Struct.Bitmap[0]);
     616        g_pfnrtKeInitializeAffinityEx(&u.Struct);
     617        if (u.Struct.Size > 1 && u.Struct.Size <= cMaxEntries)
     618        {
     619            g_cRtMpNtKaffinityExEntries = u.Struct.Size;
     620            g_cbRtMpNtKaffinityEx       = u.Struct.Size * sizeof(u.Struct.Bitmap[0]) + RT_UOFFSETOF(KAFFINITY_EX, Bitmap[0]);
     621            g_pfnrtMpPokeCpuWorker      = rtMpPokeCpuUsingHalRequestIpiW7Plus;
     622            DbgPrint("IPRT: RTMpPoke => rtMpPokeCpuUsingHalRequestIpiW7Plus\n");
     623            return VINF_SUCCESS;
     624        }
     625        DbgPrint("IPRT: RTMpPoke can't use rtMpPokeCpuUsingHalRequestIpiW7Plus! u.Struct.Size=%u\n", u.Struct.Size);
     626        AssertReleaseMsg(u.Struct.Size <= cMaxEntries, ("%#x\n", u.Struct.Size)); /* stack is toast if larger (32768 CPUs). */
     627    }
     628
     629    if (pOsVerInfo->uMajorVer >= 6 && g_pfnrtKeIpiGenericCall)
    604630    {
    605631        DbgPrint("IPRT: RTMpPoke => rtMpPokeCpuUsingBroadcastIpi\n");
     
    18611887{
    18621888    /* idCpu is an HAL processor index, so we can use it directly. */
    1863     KAFFINITY_EX Target;
    1864     g_pfnrtKeInitializeAffinityEx(&Target);
    1865     g_pfnrtKeAddProcessorAffinityEx(&Target, idCpu);
    1866 
    1867     g_pfnrtHalRequestIpiW7Plus(0, &Target);
     1889    PKAFFINITY_EX pTarget = (PKAFFINITY_EX)alloca(g_cbRtMpNtKaffinityEx);
     1890    pTarget->Size = g_cRtMpNtKaffinityExEntries; /* (just in case KeInitializeAffinityEx starts using it) */
     1891    g_pfnrtKeInitializeAffinityEx(pTarget);
     1892    g_pfnrtKeAddProcessorAffinityEx(pTarget, idCpu);
     1893
     1894    g_pfnrtHalRequestIpiW7Plus(0, pTarget);
    18681895    return VINF_SUCCESS;
    18691896}
Note: See TracChangeset for help on using the changeset viewer.

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