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