[25975] | 1 | /* $Id: VBoxServiceCpuHotPlug.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[58052] | 3 | * VBoxService - Guest Additions CPU Hot-Plugging Service.
|
---|
[25975] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[25975] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[25975] | 26 | */
|
---|
| 27 |
|
---|
[58054] | 28 | /** @page pg_vgsvc_cpuhotplug VBoxService - CPU Hot-Plugging
|
---|
[58052] | 29 | *
|
---|
| 30 | * The CPU Hot-Plugging subservice helps execute and coordinate CPU hot-plugging
|
---|
| 31 | * between the guest OS and the VMM.
|
---|
| 32 | *
|
---|
| 33 | * CPU Hot-Plugging is useful for reallocating CPU resources from one VM to
|
---|
| 34 | * other VMs or/and the host. It talks to the VMM via VMMDev, new hot-plugging
|
---|
| 35 | * events being signalled with an interrupt (no polling).
|
---|
| 36 | *
|
---|
| 37 | * Currently only supported for linux guests.
|
---|
| 38 | */
|
---|
[57358] | 39 |
|
---|
[62470] | 40 |
|
---|
[57358] | 41 | /*********************************************************************************************************************************
|
---|
| 42 | * Header Files *
|
---|
| 43 | *********************************************************************************************************************************/
|
---|
[26061] | 44 | #include <iprt/assert.h>
|
---|
| 45 | #include <iprt/dir.h>
|
---|
[25975] | 46 | #include <iprt/file.h>
|
---|
| 47 | #include <iprt/mem.h>
|
---|
[33468] | 48 | #include <iprt/path.h>
|
---|
[26061] | 49 | #include <iprt/string.h>
|
---|
| 50 | #include <iprt/thread.h>
|
---|
[25975] | 51 | #include <VBox/VBoxGuestLib.h>
|
---|
| 52 | #include "VBoxServiceInternal.h"
|
---|
| 53 |
|
---|
| 54 | #ifdef RT_OS_LINUX
|
---|
| 55 | # include <iprt/linux/sysfs.h>
|
---|
| 56 | # include <errno.h> /* For the sysfs API */
|
---|
| 57 | #endif
|
---|
| 58 |
|
---|
| 59 |
|
---|
[57358] | 60 | /*********************************************************************************************************************************
|
---|
| 61 | * Defined Constants And Macros *
|
---|
| 62 | *********************************************************************************************************************************/
|
---|
[26061] | 63 | #ifdef RT_OS_LINUX
|
---|
[58029] | 64 |
|
---|
[26061] | 65 | /** @name Paths to access the CPU device
|
---|
| 66 | * @{
|
---|
| 67 | */
|
---|
[29503] | 68 | # define SYSFS_ACPI_CPU_PATH "/sys/devices"
|
---|
[26061] | 69 | # define SYSFS_CPU_PATH "/sys/devices/system/cpu"
|
---|
| 70 | /** @} */
|
---|
[29503] | 71 |
|
---|
| 72 | /** Path component for the ACPI CPU path. */
|
---|
| 73 | typedef struct SYSFSCPUPATHCOMP
|
---|
| 74 | {
|
---|
| 75 | /** Flag whether the name is suffixed with a number */
|
---|
| 76 | bool fNumberedSuffix;
|
---|
| 77 | /** Name of the component */
|
---|
| 78 | const char *pcszName;
|
---|
| 79 | } SYSFSCPUPATHCOMP, *PSYSFSCPUPATHCOMP;
|
---|
| 80 | /** Pointer to a const component. */
|
---|
| 81 | typedef const SYSFSCPUPATHCOMP *PCSYSFSCPUPATHCOMP;
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Structure which defines how the entries are assembled.
|
---|
| 85 | */
|
---|
| 86 | typedef struct SYSFSCPUPATH
|
---|
| 87 | {
|
---|
| 88 | /** Id when probing for the correct path. */
|
---|
| 89 | uint32_t uId;
|
---|
| 90 | /** Array holding the possible components. */
|
---|
| 91 | PCSYSFSCPUPATHCOMP aComponentsPossible;
|
---|
| 92 | /** Number of entries in the array, excluding the terminator. */
|
---|
| 93 | unsigned cComponents;
|
---|
| 94 | /** Directory handle */
|
---|
[69753] | 95 | RTDIR hDir;
|
---|
[29503] | 96 | /** Current directory to try. */
|
---|
| 97 | char *pszPath;
|
---|
| 98 | } SYSFSCPUPATH, *PSYSFSCPUPATH;
|
---|
| 99 |
|
---|
| 100 | /** Content of uId if the path wasn't probed yet. */
|
---|
[58029] | 101 | # define ACPI_CPU_PATH_NOT_PROBED UINT32_MAX
|
---|
| 102 | #endif /* RT_OS_LINUX*/
|
---|
[29503] | 103 |
|
---|
[58029] | 104 |
|
---|
| 105 | /*********************************************************************************************************************************
|
---|
| 106 | * Global Variables *
|
---|
| 107 | *********************************************************************************************************************************/
|
---|
| 108 | #ifdef RT_OS_LINUX
|
---|
[29503] | 109 | /** Possible combinations of all path components for level 1. */
|
---|
[58029] | 110 | static const SYSFSCPUPATHCOMP g_aAcpiCpuPathLvl1[] =
|
---|
[29503] | 111 | {
|
---|
| 112 | /** LNXSYSTEM:<id> */
|
---|
[58029] | 113 | { true, "LNXSYSTM:*" }
|
---|
[29503] | 114 | };
|
---|
| 115 |
|
---|
| 116 | /** Possible combinations of all path components for level 2. */
|
---|
[58029] | 117 | static const SYSFSCPUPATHCOMP g_aAcpiCpuPathLvl2[] =
|
---|
[29503] | 118 | {
|
---|
| 119 | /** device:<id> */
|
---|
[58052] | 120 | { true, "device:*" },
|
---|
[29503] | 121 | /** LNXSYBUS:<id> */
|
---|
[58052] | 122 | { true, "LNXSYBUS:*" }
|
---|
[29503] | 123 | };
|
---|
| 124 |
|
---|
| 125 | /** Possible combinations of all path components for level 3 */
|
---|
[58029] | 126 | static const SYSFSCPUPATHCOMP g_aAcpiCpuPathLvl3[] =
|
---|
[29503] | 127 | {
|
---|
| 128 | /** ACPI0004:<id> */
|
---|
[58052] | 129 | { true, "ACPI0004:*" }
|
---|
[29503] | 130 | };
|
---|
| 131 |
|
---|
| 132 | /** Possible combinations of all path components for level 4 */
|
---|
[58029] | 133 | static const SYSFSCPUPATHCOMP g_aAcpiCpuPathLvl4[] =
|
---|
[29503] | 134 | {
|
---|
| 135 | /** LNXCPU:<id> */
|
---|
[58052] | 136 | { true, "LNXCPU:*" },
|
---|
[29503] | 137 | /** ACPI_CPU:<id> */
|
---|
[58052] | 138 | { true, "ACPI_CPU:*" }
|
---|
[29503] | 139 | };
|
---|
| 140 |
|
---|
| 141 | /** All possible combinations. */
|
---|
[58029] | 142 | static SYSFSCPUPATH g_aAcpiCpuPath[] =
|
---|
[29503] | 143 | {
|
---|
| 144 | /** Level 1 */
|
---|
[58052] | 145 | { ACPI_CPU_PATH_NOT_PROBED, g_aAcpiCpuPathLvl1, RT_ELEMENTS(g_aAcpiCpuPathLvl1), NULL, NULL },
|
---|
[29503] | 146 | /** Level 2 */
|
---|
[58052] | 147 | { ACPI_CPU_PATH_NOT_PROBED, g_aAcpiCpuPathLvl2, RT_ELEMENTS(g_aAcpiCpuPathLvl2), NULL, NULL },
|
---|
[29503] | 148 | /** Level 3 */
|
---|
[58052] | 149 | { ACPI_CPU_PATH_NOT_PROBED, g_aAcpiCpuPathLvl3, RT_ELEMENTS(g_aAcpiCpuPathLvl3), NULL, NULL },
|
---|
[29503] | 150 | /** Level 4 */
|
---|
[58052] | 151 | { ACPI_CPU_PATH_NOT_PROBED, g_aAcpiCpuPathLvl4, RT_ELEMENTS(g_aAcpiCpuPathLvl4), NULL, NULL },
|
---|
[29503] | 152 | };
|
---|
[50286] | 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Possible directories to get to the topology directory for reading core and package id.
|
---|
| 156 | *
|
---|
| 157 | * @remark: This is not part of the path above because the eject file is not in one of the directories
|
---|
| 158 | * below and would make the hot unplug code fail.
|
---|
| 159 | */
|
---|
[58029] | 160 | static const char *g_apszTopologyPath[] =
|
---|
[50286] | 161 | {
|
---|
| 162 | "sysdev",
|
---|
| 163 | "physical_node"
|
---|
| 164 | };
|
---|
[25975] | 165 |
|
---|
[58029] | 166 | #endif /* RT_OS_LINUX*/
|
---|
| 167 |
|
---|
| 168 |
|
---|
[25975] | 169 | #ifdef RT_OS_LINUX
|
---|
[58029] | 170 |
|
---|
[25975] | 171 | /**
|
---|
[29503] | 172 | * Probes for the correct path to the ACPI CPU object in sysfs for the
|
---|
| 173 | * various different kernel versions and distro's.
|
---|
| 174 | *
|
---|
| 175 | * @returns VBox status code.
|
---|
| 176 | */
|
---|
[58029] | 177 | static int vgsvcCpuHotPlugProbePath(void)
|
---|
[29503] | 178 | {
|
---|
| 179 | int rc = VINF_SUCCESS;
|
---|
| 180 |
|
---|
| 181 | /* Probe for the correct path if we didn't already. */
|
---|
| 182 | if (RT_UNLIKELY(g_aAcpiCpuPath[0].uId == ACPI_CPU_PATH_NOT_PROBED))
|
---|
| 183 | {
|
---|
| 184 | char *pszPath = NULL; /** < Current path, increasing while we dig deeper. */
|
---|
| 185 |
|
---|
| 186 | pszPath = RTStrDup(SYSFS_ACPI_CPU_PATH);
|
---|
| 187 | if (!pszPath)
|
---|
| 188 | return VERR_NO_MEMORY;
|
---|
| 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Simple algorithm to find the path.
|
---|
| 192 | * Performance is not a real problem because it is
|
---|
| 193 | * only executed once.
|
---|
| 194 | */
|
---|
| 195 | for (unsigned iLvlCurr = 0; iLvlCurr < RT_ELEMENTS(g_aAcpiCpuPath); iLvlCurr++)
|
---|
| 196 | {
|
---|
| 197 | PSYSFSCPUPATH pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];
|
---|
| 198 |
|
---|
| 199 | for (unsigned iCompCurr = 0; iCompCurr < pAcpiCpuPathLvl->cComponents; iCompCurr++)
|
---|
| 200 | {
|
---|
| 201 | PCSYSFSCPUPATHCOMP pPathComponent = &pAcpiCpuPathLvl->aComponentsPossible[iCompCurr];
|
---|
| 202 |
|
---|
| 203 | /* Open the directory */
|
---|
[69753] | 204 | RTDIR hDirCurr = NIL_RTDIR;
|
---|
[33464] | 205 | char *pszPathTmp = RTPathJoinA(pszPath, pPathComponent->pcszName);
|
---|
| 206 | if (pszPathTmp)
|
---|
[29503] | 207 | {
|
---|
[69753] | 208 | rc = RTDirOpenFiltered(&hDirCurr, pszPathTmp, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
[29503] | 209 | RTStrFree(pszPathTmp);
|
---|
[33464] | 210 | }
|
---|
| 211 | else
|
---|
| 212 | rc = VERR_NO_STR_MEMORY;
|
---|
| 213 | if (RT_FAILURE(rc))
|
---|
[29503] | 214 | break;
|
---|
| 215 |
|
---|
| 216 | /* Search if the current directory contains one of the possible parts. */
|
---|
[33464] | 217 | size_t cchName = strlen(pPathComponent->pcszName);
|
---|
[29503] | 218 | RTDIRENTRY DirFolderContent;
|
---|
[33464] | 219 | bool fFound = false;
|
---|
[43377] | 220 |
|
---|
| 221 | /* Get rid of the * filter which is in the path component. */
|
---|
| 222 | if (pPathComponent->fNumberedSuffix)
|
---|
| 223 | cchName--;
|
---|
| 224 |
|
---|
[69753] | 225 | while (RT_SUCCESS(RTDirRead(hDirCurr, &DirFolderContent, NULL))) /* Assumption that szName has always enough space */
|
---|
[29503] | 226 | {
|
---|
[33464] | 227 | if ( DirFolderContent.cbName >= cchName
|
---|
| 228 | && !strncmp(DirFolderContent.szName, pPathComponent->pcszName, cchName))
|
---|
[29503] | 229 | {
|
---|
| 230 | /* Found, use the complete name to dig deeper. */
|
---|
| 231 | fFound = true;
|
---|
| 232 | pAcpiCpuPathLvl->uId = iCompCurr;
|
---|
[33464] | 233 | char *pszPathLvl = RTPathJoinA(pszPath, DirFolderContent.szName);
|
---|
| 234 | if (pszPathLvl)
|
---|
[29503] | 235 | {
|
---|
| 236 | RTStrFree(pszPath);
|
---|
| 237 | pszPath = pszPathLvl;
|
---|
| 238 | }
|
---|
[33464] | 239 | else
|
---|
| 240 | rc = VERR_NO_STR_MEMORY;
|
---|
[29503] | 241 | break;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
[69753] | 244 | RTDirClose(hDirCurr);
|
---|
[29503] | 245 |
|
---|
| 246 | if (fFound)
|
---|
| 247 | break;
|
---|
| 248 | } /* For every possible component. */
|
---|
| 249 |
|
---|
| 250 | /* No matching component for this part, no need to continue */
|
---|
| 251 | if (RT_FAILURE(rc))
|
---|
| 252 | break;
|
---|
| 253 | } /* For every level */
|
---|
| 254 |
|
---|
[58029] | 255 | VGSvcVerbose(1, "Final path after probing %s rc=%Rrc\n", pszPath, rc);
|
---|
[29503] | 256 | RTStrFree(pszPath);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return rc;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[58029] | 262 |
|
---|
[29503] | 263 | /**
|
---|
[25975] | 264 | * Returns the path of the ACPI CPU device with the given core and package ID.
|
---|
| 265 | *
|
---|
| 266 | * @returns VBox status code.
|
---|
| 267 | * @param ppszPath Where to store the path.
|
---|
| 268 | * @param idCpuCore The core ID of the CPU.
|
---|
| 269 | * @param idCpuPackage The package ID of the CPU.
|
---|
| 270 | */
|
---|
[58029] | 271 | static int vgsvcCpuHotPlugGetACPIDevicePath(char **ppszPath, uint32_t idCpuCore, uint32_t idCpuPackage)
|
---|
[25975] | 272 | {
|
---|
[29503] | 273 | int rc = VINF_SUCCESS;
|
---|
[26061] | 274 |
|
---|
[29503] | 275 | AssertPtrReturn(ppszPath, VERR_INVALID_PARAMETER);
|
---|
| 276 |
|
---|
[58029] | 277 | rc = vgsvcCpuHotPlugProbePath();
|
---|
[25975] | 278 | if (RT_SUCCESS(rc))
|
---|
| 279 | {
|
---|
[29503] | 280 | /* Build the path from all components. */
|
---|
[26595] | 281 | bool fFound = false;
|
---|
[29503] | 282 | unsigned iLvlCurr = 0;
|
---|
| 283 | char *pszPath = NULL;
|
---|
| 284 | char *pszPathDir = NULL;
|
---|
| 285 | PSYSFSCPUPATH pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];
|
---|
[26595] | 286 |
|
---|
[29503] | 287 | /* Init everything. */
|
---|
| 288 | Assert(pAcpiCpuPathLvl->uId != ACPI_CPU_PATH_NOT_PROBED);
|
---|
[33464] | 289 | pszPath = RTPathJoinA(SYSFS_ACPI_CPU_PATH, pAcpiCpuPathLvl->aComponentsPossible[pAcpiCpuPathLvl->uId].pcszName);
|
---|
| 290 | if (!pszPath)
|
---|
| 291 | return VERR_NO_STR_MEMORY;
|
---|
[29503] | 292 |
|
---|
| 293 | pAcpiCpuPathLvl->pszPath = RTStrDup(SYSFS_ACPI_CPU_PATH);
|
---|
| 294 | if (!pAcpiCpuPathLvl->pszPath)
|
---|
[33464] | 295 | {
|
---|
| 296 | RTStrFree(pszPath);
|
---|
| 297 | return VERR_NO_STR_MEMORY;
|
---|
| 298 | }
|
---|
[29503] | 299 |
|
---|
| 300 | /* Open the directory */
|
---|
[69753] | 301 | rc = RTDirOpenFiltered(&pAcpiCpuPathLvl->hDir, pszPath, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
[29503] | 302 | if (RT_SUCCESS(rc))
|
---|
[25975] | 303 | {
|
---|
[29503] | 304 | RTStrFree(pszPath);
|
---|
[86188] | 305 | pszPath = NULL;
|
---|
[29503] | 306 |
|
---|
| 307 | /* Search for CPU */
|
---|
| 308 | while (!fFound)
|
---|
[25975] | 309 | {
|
---|
[29503] | 310 | /* Get the next directory. */
|
---|
| 311 | RTDIRENTRY DirFolderContent;
|
---|
[69753] | 312 | rc = RTDirRead(pAcpiCpuPathLvl->hDir, &DirFolderContent, NULL);
|
---|
[29503] | 313 | if (RT_SUCCESS(rc))
|
---|
| 314 | {
|
---|
| 315 | /* Create the new path. */
|
---|
[33464] | 316 | char *pszPathCurr = RTPathJoinA(pAcpiCpuPathLvl->pszPath, DirFolderContent.szName);
|
---|
| 317 | if (!pszPathCurr)
|
---|
| 318 | {
|
---|
| 319 | rc = VERR_NO_STR_MEMORY;
|
---|
[29503] | 320 | break;
|
---|
[33464] | 321 | }
|
---|
[26595] | 322 |
|
---|
[29503] | 323 | /* If this is the last level check for the given core and package id. */
|
---|
| 324 | if (iLvlCurr == RT_ELEMENTS(g_aAcpiCpuPath) - 1)
|
---|
[26595] | 325 | {
|
---|
[29503] | 326 | /* Get the sysdev */
|
---|
[50286] | 327 | uint32_t idCore = 0;
|
---|
| 328 | uint32_t idPackage = 0;
|
---|
| 329 |
|
---|
| 330 | for (unsigned i = 0; i < RT_ELEMENTS(g_apszTopologyPath); i++)
|
---|
| 331 | {
|
---|
[60374] | 332 | int64_t i64Core = 0;
|
---|
| 333 | int64_t i64Package = 0;
|
---|
[50286] | 334 |
|
---|
[60374] | 335 | int rc2 = RTLinuxSysFsReadIntFile(10, &i64Core, "%s/%s/topology/core_id",
|
---|
| 336 | pszPathCurr, g_apszTopologyPath[i]);
|
---|
| 337 | if (RT_SUCCESS(rc2))
|
---|
| 338 | rc2 = RTLinuxSysFsReadIntFile(10, &i64Package, "%s/%s/topology/physical_package_id",
|
---|
| 339 | pszPathCurr, g_apszTopologyPath[i]);
|
---|
| 340 |
|
---|
| 341 | if (RT_SUCCESS(rc2))
|
---|
[50286] | 342 | {
|
---|
| 343 | idCore = (uint32_t)i64Core;
|
---|
| 344 | idPackage = (uint32_t)i64Package;
|
---|
| 345 | break;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[29503] | 349 | if ( idCore == idCpuCore
|
---|
| 350 | && idPackage == idCpuPackage)
|
---|
[26595] | 351 | {
|
---|
[29503] | 352 | /* Return the path */
|
---|
| 353 | pszPath = pszPathCurr;
|
---|
| 354 | fFound = true;
|
---|
[58029] | 355 | VGSvcVerbose(3, "CPU found\n");
|
---|
[29503] | 356 | break;
|
---|
[26595] | 357 | }
|
---|
[29503] | 358 | else
|
---|
| 359 | {
|
---|
| 360 | /* Get the next directory. */
|
---|
| 361 | RTStrFree(pszPathCurr);
|
---|
[86188] | 362 | pszPathCurr = NULL;
|
---|
[58029] | 363 | VGSvcVerbose(3, "CPU doesn't match, next directory\n");
|
---|
[29503] | 364 | }
|
---|
[26595] | 365 | }
|
---|
[29503] | 366 | else
|
---|
| 367 | {
|
---|
| 368 | /* Go deeper */
|
---|
| 369 | iLvlCurr++;
|
---|
[26595] | 370 |
|
---|
[58029] | 371 | VGSvcVerbose(3, "Going deeper (iLvlCurr=%u)\n", iLvlCurr);
|
---|
[29503] | 372 |
|
---|
| 373 | pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];
|
---|
| 374 |
|
---|
[69753] | 375 | Assert(pAcpiCpuPathLvl->hDir == NIL_RTDIR);
|
---|
[29503] | 376 | Assert(!pAcpiCpuPathLvl->pszPath);
|
---|
| 377 | pAcpiCpuPathLvl->pszPath = pszPathCurr;
|
---|
| 378 | PCSYSFSCPUPATHCOMP pPathComponent = &pAcpiCpuPathLvl->aComponentsPossible[pAcpiCpuPathLvl->uId];
|
---|
| 379 |
|
---|
| 380 | Assert(pAcpiCpuPathLvl->uId != ACPI_CPU_PATH_NOT_PROBED);
|
---|
| 381 |
|
---|
[33464] | 382 | pszPathDir = RTPathJoinA(pszPathCurr, pPathComponent->pcszName);
|
---|
| 383 | if (!pszPathDir)
|
---|
| 384 | {
|
---|
| 385 | rc = VERR_NO_STR_MEMORY;
|
---|
[29503] | 386 | break;
|
---|
[33464] | 387 | }
|
---|
[29503] | 388 |
|
---|
[58029] | 389 | VGSvcVerbose(3, "New path %s\n", pszPathDir);
|
---|
[29503] | 390 |
|
---|
| 391 | /* Open the directory */
|
---|
[69753] | 392 | rc = RTDirOpenFiltered(&pAcpiCpuPathLvl->hDir, pszPathDir, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
[86189] | 393 | RTStrFree(pszPathDir);
|
---|
| 394 | pszPathDir = NULL;
|
---|
[29503] | 395 | if (RT_FAILURE(rc))
|
---|
| 396 | break;
|
---|
| 397 | }
|
---|
[25975] | 398 | }
|
---|
[29503] | 399 | else
|
---|
| 400 | {
|
---|
[69753] | 401 | RTDirClose(pAcpiCpuPathLvl->hDir);
|
---|
[29503] | 402 | RTStrFree(pAcpiCpuPathLvl->pszPath);
|
---|
[69753] | 403 | pAcpiCpuPathLvl->hDir = NIL_RTDIR;
|
---|
[29503] | 404 | pAcpiCpuPathLvl->pszPath = NULL;
|
---|
| 405 |
|
---|
[86188] | 406 | /*
|
---|
| 407 | * If we reached the end we didn't find the matching path
|
---|
| 408 | * meaning the CPU is already offline.
|
---|
| 409 | */
|
---|
| 410 | if (!iLvlCurr)
|
---|
| 411 | {
|
---|
| 412 | rc = VERR_NOT_FOUND;
|
---|
| 413 | break;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[29503] | 416 | iLvlCurr--;
|
---|
| 417 | pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];
|
---|
[58029] | 418 | VGSvcVerbose(3, "Directory not found, going back (iLvlCurr=%u)\n", iLvlCurr);
|
---|
[29503] | 419 | }
|
---|
| 420 | } /* while not found */
|
---|
| 421 | } /* Successful init */
|
---|
| 422 |
|
---|
| 423 | /* Cleanup */
|
---|
| 424 | for (unsigned i = 0; i < RT_ELEMENTS(g_aAcpiCpuPath); i++)
|
---|
| 425 | {
|
---|
[69753] | 426 | if (g_aAcpiCpuPath[i].hDir)
|
---|
| 427 | RTDirClose(g_aAcpiCpuPath[i].hDir);
|
---|
[29503] | 428 | if (g_aAcpiCpuPath[i].pszPath)
|
---|
| 429 | RTStrFree(g_aAcpiCpuPath[i].pszPath);
|
---|
[69753] | 430 | g_aAcpiCpuPath[i].hDir = NIL_RTDIR;
|
---|
[29503] | 431 | g_aAcpiCpuPath[i].pszPath = NULL;
|
---|
[25975] | 432 | }
|
---|
[29503] | 433 | if (pszPathDir)
|
---|
| 434 | RTStrFree(pszPathDir);
|
---|
| 435 | if (RT_FAILURE(rc) && pszPath)
|
---|
| 436 | RTStrFree(pszPath);
|
---|
[25975] | 437 |
|
---|
[29503] | 438 | if (RT_SUCCESS(rc))
|
---|
| 439 | *ppszPath = pszPath;
|
---|
[25975] | 440 | }
|
---|
| 441 |
|
---|
| 442 | return rc;
|
---|
| 443 | }
|
---|
[58029] | 444 |
|
---|
[26061] | 445 | #endif /* RT_OS_LINUX */
|
---|
[25975] | 446 |
|
---|
[26061] | 447 | /**
|
---|
| 448 | * Handles VMMDevCpuEventType_Plug.
|
---|
| 449 | *
|
---|
| 450 | * @param idCpuCore The CPU core ID.
|
---|
| 451 | * @param idCpuPackage The CPU package ID.
|
---|
| 452 | */
|
---|
[58029] | 453 | static void vgsvcCpuHotPlugHandlePlugEvent(uint32_t idCpuCore, uint32_t idCpuPackage)
|
---|
[26061] | 454 | {
|
---|
| 455 | #ifdef RT_OS_LINUX
|
---|
| 456 | /*
|
---|
[26599] | 457 | * The topology directory (containing the physical and core id properties)
|
---|
| 458 | * is not available until the CPU is online. So we just iterate over all directories
|
---|
[86188] | 459 | * and enable the first CPU which is not online already.
|
---|
[26599] | 460 | * Because the directory might not be available immediately we try a few times.
|
---|
| 461 | *
|
---|
[26061] | 462 | */
|
---|
[58029] | 463 | /** @todo Maybe use udev to monitor hot-add events from the kernel */
|
---|
[26599] | 464 | bool fCpuOnline = false;
|
---|
| 465 | unsigned cTries = 5;
|
---|
| 466 |
|
---|
| 467 | do
|
---|
[26061] | 468 | {
|
---|
[69753] | 469 | RTDIR hDirDevices = NULL;
|
---|
| 470 | int rc = RTDirOpen(&hDirDevices, SYSFS_CPU_PATH);
|
---|
[26599] | 471 | if (RT_SUCCESS(rc))
|
---|
[26061] | 472 | {
|
---|
[26599] | 473 | RTDIRENTRY DirFolderContent;
|
---|
[69753] | 474 | while (RT_SUCCESS(RTDirRead(hDirDevices, &DirFolderContent, NULL))) /* Assumption that szName has always enough space */
|
---|
[26061] | 475 | {
|
---|
[86188] | 476 | /* Check if this is a CPU object which can be brought online. */
|
---|
| 477 | if (RTLinuxSysFsExists("%s/%s/online", SYSFS_CPU_PATH, DirFolderContent.szName))
|
---|
[26061] | 478 | {
|
---|
[86188] | 479 | /* Check the status of the CPU by reading the online flag. */
|
---|
| 480 | int64_t i64Status = 0;
|
---|
| 481 | rc = RTLinuxSysFsReadIntFile(10 /*uBase*/, &i64Status, "%s/%s/online", SYSFS_CPU_PATH, DirFolderContent.szName);
|
---|
| 482 | if ( RT_SUCCESS(rc)
|
---|
| 483 | && i64Status == 0)
|
---|
[26061] | 484 | {
|
---|
[86188] | 485 | /* CPU is offline, turn it on. */
|
---|
| 486 | rc = RTLinuxSysFsWriteU8File(10 /*uBase*/, 1, "%s/%s/online", SYSFS_CPU_PATH, DirFolderContent.szName);
|
---|
[26599] | 487 | if (RT_SUCCESS(rc))
|
---|
| 488 | {
|
---|
[58029] | 489 | VGSvcVerbose(1, "CpuHotPlug: CPU %u/%u was brought online\n", idCpuPackage, idCpuCore);
|
---|
[26599] | 490 | fCpuOnline = true;
|
---|
| 491 | break;
|
---|
| 492 | }
|
---|
[26061] | 493 | }
|
---|
[86188] | 494 | else if (RT_FAILURE(rc))
|
---|
[58029] | 495 | VGSvcError("CpuHotPlug: Failed to open '%s/%s/online' rc=%Rrc\n",
|
---|
| 496 | SYSFS_CPU_PATH, DirFolderContent.szName, rc);
|
---|
[86188] | 497 | else
|
---|
| 498 | {
|
---|
| 499 | /*
|
---|
| 500 | * Check whether the topology matches what we got (which means someone raced us and brought the CPU
|
---|
| 501 | * online already).
|
---|
| 502 | */
|
---|
| 503 | int64_t i64Core = 0;
|
---|
| 504 | int64_t i64Package = 0;
|
---|
| 505 |
|
---|
| 506 | int rc2 = RTLinuxSysFsReadIntFile(10, &i64Core, "%s/%s/topology/core_id",
|
---|
| 507 | SYSFS_CPU_PATH, DirFolderContent.szName);
|
---|
| 508 | if (RT_SUCCESS(rc2))
|
---|
| 509 | rc2 = RTLinuxSysFsReadIntFile(10, &i64Package, "%s/%s/topology/physical_package_id",
|
---|
| 510 | SYSFS_CPU_PATH, DirFolderContent.szName);
|
---|
| 511 | if ( RT_SUCCESS(rc2)
|
---|
| 512 | && idCpuPackage == i64Package
|
---|
| 513 | && idCpuCore == i64Core)
|
---|
| 514 | {
|
---|
| 515 | VGSvcVerbose(1, "CpuHotPlug: '%s' is already online\n", DirFolderContent.szName);
|
---|
| 516 | fCpuOnline = true;
|
---|
| 517 | break;
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
[26061] | 520 | }
|
---|
| 521 | }
|
---|
[69753] | 522 | RTDirClose(hDirDevices);
|
---|
[26061] | 523 | }
|
---|
[26599] | 524 | else
|
---|
[58029] | 525 | VGSvcError("CpuHotPlug: Failed to open path %s rc=%Rrc\n", SYSFS_CPU_PATH, rc);
|
---|
[26599] | 526 |
|
---|
| 527 | /* Sleep a bit */
|
---|
| 528 | if (!fCpuOnline)
|
---|
[86188] | 529 | RTThreadSleep(100);
|
---|
[26599] | 530 |
|
---|
| 531 | } while ( !fCpuOnline
|
---|
| 532 | && cTries-- > 0);
|
---|
[26061] | 533 | #else
|
---|
| 534 | # error "Port me"
|
---|
| 535 | #endif
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 |
|
---|
| 539 | /**
|
---|
| 540 | * Handles VMMDevCpuEventType_Unplug.
|
---|
| 541 | *
|
---|
| 542 | * @param idCpuCore The CPU core ID.
|
---|
| 543 | * @param idCpuPackage The CPU package ID.
|
---|
| 544 | */
|
---|
[58029] | 545 | static void vgsvcCpuHotPlugHandleUnplugEvent(uint32_t idCpuCore, uint32_t idCpuPackage)
|
---|
[26061] | 546 | {
|
---|
| 547 | #ifdef RT_OS_LINUX
|
---|
| 548 | char *pszCpuDevicePath = NULL;
|
---|
[58029] | 549 | int rc = vgsvcCpuHotPlugGetACPIDevicePath(&pszCpuDevicePath, idCpuCore, idCpuPackage);
|
---|
[26061] | 550 | if (RT_SUCCESS(rc))
|
---|
| 551 | {
|
---|
| 552 | RTFILE hFileCpuEject;
|
---|
[58029] | 553 | rc = RTFileOpenF(&hFileCpuEject, RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE, "%s/eject", pszCpuDevicePath);
|
---|
[26061] | 554 | if (RT_SUCCESS(rc))
|
---|
| 555 | {
|
---|
| 556 | /* Write a 1 to eject the CPU */
|
---|
| 557 | rc = RTFileWrite(hFileCpuEject, "1", 1, NULL);
|
---|
| 558 | if (RT_SUCCESS(rc))
|
---|
[58029] | 559 | VGSvcVerbose(1, "CpuHotPlug: CPU %u/%u was ejected\n", idCpuPackage, idCpuCore);
|
---|
[26061] | 560 | else
|
---|
[58029] | 561 | VGSvcError("CpuHotPlug: Failed to eject CPU %u/%u rc=%Rrc\n", idCpuPackage, idCpuCore, rc);
|
---|
[26061] | 562 |
|
---|
| 563 | RTFileClose(hFileCpuEject);
|
---|
| 564 | }
|
---|
| 565 | else
|
---|
[58029] | 566 | VGSvcError("CpuHotPlug: Failed to open '%s/eject' rc=%Rrc\n", pszCpuDevicePath, rc);
|
---|
[26061] | 567 | RTStrFree(pszCpuDevicePath);
|
---|
| 568 | }
|
---|
[86188] | 569 | else if (rc == VERR_NOT_FOUND)
|
---|
| 570 | VGSvcVerbose(1, "CpuHotPlug: CPU %u/%u was aleady ejected by someone else!\n", idCpuPackage, idCpuCore);
|
---|
[26061] | 571 | else
|
---|
[58029] | 572 | VGSvcError("CpuHotPlug: Failed to get CPU device path rc=%Rrc\n", rc);
|
---|
[26061] | 573 | #else
|
---|
| 574 | # error "Port me"
|
---|
| 575 | #endif
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 |
|
---|
[58029] | 579 | /** @interface_method_impl{VBOXSERVICE,pfnWorker} */
|
---|
| 580 | static DECLCALLBACK(int) vgsvcCpuHotPlugWorker(bool volatile *pfShutdown)
|
---|
[25975] | 581 | {
|
---|
| 582 | /*
|
---|
| 583 | * Tell the control thread that it can continue spawning services.
|
---|
| 584 | */
|
---|
| 585 | RTThreadUserSignal(RTThreadSelf());
|
---|
| 586 |
|
---|
| 587 | /*
|
---|
| 588 | * Enable the CPU hotplug notifier.
|
---|
| 589 | */
|
---|
[26061] | 590 | int rc = VbglR3CpuHotPlugInit();
|
---|
[25975] | 591 | if (RT_FAILURE(rc))
|
---|
| 592 | return rc;
|
---|
| 593 |
|
---|
| 594 | /*
|
---|
| 595 | * The Work Loop.
|
---|
| 596 | */
|
---|
| 597 | for (;;)
|
---|
| 598 | {
|
---|
[58052] | 599 | /* Wait for CPU hot-plugging event. */
|
---|
[26061] | 600 | uint32_t idCpuCore;
|
---|
| 601 | uint32_t idCpuPackage;
|
---|
| 602 | VMMDevCpuEventType enmEventType;
|
---|
| 603 | rc = VbglR3CpuHotPlugWaitForEvent(&enmEventType, &idCpuCore, &idCpuPackage);
|
---|
| 604 | if (RT_SUCCESS(rc))
|
---|
[25975] | 605 | {
|
---|
[58029] | 606 | VGSvcVerbose(3, "CpuHotPlug: Event happened idCpuCore=%u idCpuPackage=%u enmEventType=%d\n",
|
---|
| 607 | idCpuCore, idCpuPackage, enmEventType);
|
---|
[26061] | 608 | switch (enmEventType)
|
---|
| 609 | {
|
---|
| 610 | case VMMDevCpuEventType_Plug:
|
---|
[58029] | 611 | vgsvcCpuHotPlugHandlePlugEvent(idCpuCore, idCpuPackage);
|
---|
[26061] | 612 | break;
|
---|
[25975] | 613 |
|
---|
[26061] | 614 | case VMMDevCpuEventType_Unplug:
|
---|
[58029] | 615 | vgsvcCpuHotPlugHandleUnplugEvent(idCpuCore, idCpuPackage);
|
---|
[26061] | 616 | break;
|
---|
[25975] | 617 |
|
---|
[26061] | 618 | default:
|
---|
[25975] | 619 | {
|
---|
[26061] | 620 | static uint32_t s_iErrors = 0;
|
---|
| 621 | if (s_iErrors++ < 10)
|
---|
[58029] | 622 | VGSvcError("CpuHotPlug: Unknown event: idCpuCore=%u idCpuPackage=%u enmEventType=%d\n",
|
---|
| 623 | idCpuCore, idCpuPackage, enmEventType);
|
---|
[26061] | 624 | break;
|
---|
[25975] | 625 | }
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
[26061] | 628 | else if (rc != VERR_INTERRUPTED && rc != VERR_TRY_AGAIN)
|
---|
[25975] | 629 | {
|
---|
[58029] | 630 | VGSvcError("CpuHotPlug: VbglR3CpuHotPlugWaitForEvent returned %Rrc\n", rc);
|
---|
[26061] | 631 | break;
|
---|
[25975] | 632 | }
|
---|
| 633 |
|
---|
| 634 | if (*pfShutdown)
|
---|
| 635 | break;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[26061] | 638 | VbglR3CpuHotPlugTerm();
|
---|
[25975] | 639 | return rc;
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 |
|
---|
[58029] | 643 | /** @interface_method_impl{VBOXSERVICE,pfnStop} */
|
---|
| 644 | static DECLCALLBACK(void) vgsvcCpuHotPlugStop(void)
|
---|
[25975] | 645 | {
|
---|
| 646 | VbglR3InterruptEventWaits();
|
---|
| 647 | return;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 |
|
---|
| 651 | /**
|
---|
[51570] | 652 | * The 'CpuHotPlug' service description.
|
---|
[25975] | 653 | */
|
---|
[26061] | 654 | VBOXSERVICE g_CpuHotPlug =
|
---|
[25975] | 655 | {
|
---|
| 656 | /* pszName. */
|
---|
| 657 | "cpuhotplug",
|
---|
| 658 | /* pszDescription. */
|
---|
[58052] | 659 | "CPU hot-plugging monitor",
|
---|
[25975] | 660 | /* pszUsage. */
|
---|
| 661 | NULL,
|
---|
| 662 | /* pszOptions. */
|
---|
| 663 | NULL,
|
---|
| 664 | /* methods */
|
---|
[58029] | 665 | VGSvcDefaultPreInit,
|
---|
| 666 | VGSvcDefaultOption,
|
---|
| 667 | VGSvcDefaultInit,
|
---|
| 668 | vgsvcCpuHotPlugWorker,
|
---|
| 669 | vgsvcCpuHotPlugStop,
|
---|
| 670 | VGSvcDefaultTerm
|
---|
[25975] | 671 | };
|
---|
| 672 |
|
---|