1 | /* $Id: VBoxServiceCpuHotPlug.cpp 69753 2017-11-19 14:27:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions CPU Hot-Plugging Service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2017 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 | RTDIR hDir;
|
---|
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 | RTDIR hDirCurr = NIL_RTDIR;
|
---|
195 | char *pszPathTmp = RTPathJoinA(pszPath, pPathComponent->pcszName);
|
---|
196 | if (pszPathTmp)
|
---|
197 | {
|
---|
198 | rc = RTDirOpenFiltered(&hDirCurr, pszPathTmp, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
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(hDirCurr, &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(hDirCurr);
|
---|
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->hDir, pszPath, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
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->hDir, &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->hDir == NIL_RTDIR);
|
---|
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->hDir, pszPathDir, RTDIRFILTER_WINNT, 0 /*fFlags*/);
|
---|
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->hDir);
|
---|
391 | RTStrFree(pAcpiCpuPathLvl->pszPath);
|
---|
392 | pAcpiCpuPathLvl->hDir = NIL_RTDIR;
|
---|
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].hDir)
|
---|
406 | RTDirClose(g_aAcpiCpuPath[i].hDir);
|
---|
407 | if (g_aAcpiCpuPath[i].pszPath)
|
---|
408 | RTStrFree(g_aAcpiCpuPath[i].pszPath);
|
---|
409 | g_aAcpiCpuPath[i].hDir = NIL_RTDIR;
|
---|
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 | RTDIR hDirDevices = NULL;
|
---|
449 | int rc = RTDirOpen(&hDirDevices, SYSFS_CPU_PATH);
|
---|
450 | if (RT_SUCCESS(rc))
|
---|
451 | {
|
---|
452 | RTDIRENTRY DirFolderContent;
|
---|
453 | while (RT_SUCCESS(RTDirRead(hDirDevices, &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 | RTDirClose(hDirDevices);
|
---|
497 | }
|
---|
498 | else
|
---|
499 | VGSvcError("CpuHotPlug: Failed to open path %s rc=%Rrc\n", SYSFS_CPU_PATH, rc);
|
---|
500 |
|
---|
501 | /* Sleep a bit */
|
---|
502 | if (!fCpuOnline)
|
---|
503 | RTThreadSleep(10);
|
---|
504 |
|
---|
505 | } while ( !fCpuOnline
|
---|
506 | && cTries-- > 0);
|
---|
507 | #else
|
---|
508 | # error "Port me"
|
---|
509 | #endif
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Handles VMMDevCpuEventType_Unplug.
|
---|
515 | *
|
---|
516 | * @param idCpuCore The CPU core ID.
|
---|
517 | * @param idCpuPackage The CPU package ID.
|
---|
518 | */
|
---|
519 | static void vgsvcCpuHotPlugHandleUnplugEvent(uint32_t idCpuCore, uint32_t idCpuPackage)
|
---|
520 | {
|
---|
521 | #ifdef RT_OS_LINUX
|
---|
522 | char *pszCpuDevicePath = NULL;
|
---|
523 | int rc = vgsvcCpuHotPlugGetACPIDevicePath(&pszCpuDevicePath, idCpuCore, idCpuPackage);
|
---|
524 | if (RT_SUCCESS(rc))
|
---|
525 | {
|
---|
526 | RTFILE hFileCpuEject;
|
---|
527 | rc = RTFileOpenF(&hFileCpuEject, RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE, "%s/eject", pszCpuDevicePath);
|
---|
528 | if (RT_SUCCESS(rc))
|
---|
529 | {
|
---|
530 | /* Write a 1 to eject the CPU */
|
---|
531 | rc = RTFileWrite(hFileCpuEject, "1", 1, NULL);
|
---|
532 | if (RT_SUCCESS(rc))
|
---|
533 | VGSvcVerbose(1, "CpuHotPlug: CPU %u/%u was ejected\n", idCpuPackage, idCpuCore);
|
---|
534 | else
|
---|
535 | VGSvcError("CpuHotPlug: Failed to eject CPU %u/%u rc=%Rrc\n", idCpuPackage, idCpuCore, rc);
|
---|
536 |
|
---|
537 | RTFileClose(hFileCpuEject);
|
---|
538 | }
|
---|
539 | else
|
---|
540 | VGSvcError("CpuHotPlug: Failed to open '%s/eject' rc=%Rrc\n", pszCpuDevicePath, rc);
|
---|
541 | RTStrFree(pszCpuDevicePath);
|
---|
542 | }
|
---|
543 | else
|
---|
544 | VGSvcError("CpuHotPlug: Failed to get CPU device path rc=%Rrc\n", rc);
|
---|
545 | #else
|
---|
546 | # error "Port me"
|
---|
547 | #endif
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | /** @interface_method_impl{VBOXSERVICE,pfnWorker} */
|
---|
552 | static DECLCALLBACK(int) vgsvcCpuHotPlugWorker(bool volatile *pfShutdown)
|
---|
553 | {
|
---|
554 | /*
|
---|
555 | * Tell the control thread that it can continue spawning services.
|
---|
556 | */
|
---|
557 | RTThreadUserSignal(RTThreadSelf());
|
---|
558 |
|
---|
559 | /*
|
---|
560 | * Enable the CPU hotplug notifier.
|
---|
561 | */
|
---|
562 | int rc = VbglR3CpuHotPlugInit();
|
---|
563 | if (RT_FAILURE(rc))
|
---|
564 | return rc;
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * The Work Loop.
|
---|
568 | */
|
---|
569 | for (;;)
|
---|
570 | {
|
---|
571 | /* Wait for CPU hot-plugging event. */
|
---|
572 | uint32_t idCpuCore;
|
---|
573 | uint32_t idCpuPackage;
|
---|
574 | VMMDevCpuEventType enmEventType;
|
---|
575 | rc = VbglR3CpuHotPlugWaitForEvent(&enmEventType, &idCpuCore, &idCpuPackage);
|
---|
576 | if (RT_SUCCESS(rc))
|
---|
577 | {
|
---|
578 | VGSvcVerbose(3, "CpuHotPlug: Event happened idCpuCore=%u idCpuPackage=%u enmEventType=%d\n",
|
---|
579 | idCpuCore, idCpuPackage, enmEventType);
|
---|
580 | switch (enmEventType)
|
---|
581 | {
|
---|
582 | case VMMDevCpuEventType_Plug:
|
---|
583 | vgsvcCpuHotPlugHandlePlugEvent(idCpuCore, idCpuPackage);
|
---|
584 | break;
|
---|
585 |
|
---|
586 | case VMMDevCpuEventType_Unplug:
|
---|
587 | vgsvcCpuHotPlugHandleUnplugEvent(idCpuCore, idCpuPackage);
|
---|
588 | break;
|
---|
589 |
|
---|
590 | default:
|
---|
591 | {
|
---|
592 | static uint32_t s_iErrors = 0;
|
---|
593 | if (s_iErrors++ < 10)
|
---|
594 | VGSvcError("CpuHotPlug: Unknown event: idCpuCore=%u idCpuPackage=%u enmEventType=%d\n",
|
---|
595 | idCpuCore, idCpuPackage, enmEventType);
|
---|
596 | break;
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 | else if (rc != VERR_INTERRUPTED && rc != VERR_TRY_AGAIN)
|
---|
601 | {
|
---|
602 | VGSvcError("CpuHotPlug: VbglR3CpuHotPlugWaitForEvent returned %Rrc\n", rc);
|
---|
603 | break;
|
---|
604 | }
|
---|
605 |
|
---|
606 | if (*pfShutdown)
|
---|
607 | break;
|
---|
608 | }
|
---|
609 |
|
---|
610 | VbglR3CpuHotPlugTerm();
|
---|
611 | return rc;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /** @interface_method_impl{VBOXSERVICE,pfnStop} */
|
---|
616 | static DECLCALLBACK(void) vgsvcCpuHotPlugStop(void)
|
---|
617 | {
|
---|
618 | VbglR3InterruptEventWaits();
|
---|
619 | return;
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * The 'CpuHotPlug' service description.
|
---|
625 | */
|
---|
626 | VBOXSERVICE g_CpuHotPlug =
|
---|
627 | {
|
---|
628 | /* pszName. */
|
---|
629 | "cpuhotplug",
|
---|
630 | /* pszDescription. */
|
---|
631 | "CPU hot-plugging monitor",
|
---|
632 | /* pszUsage. */
|
---|
633 | NULL,
|
---|
634 | /* pszOptions. */
|
---|
635 | NULL,
|
---|
636 | /* methods */
|
---|
637 | VGSvcDefaultPreInit,
|
---|
638 | VGSvcDefaultOption,
|
---|
639 | VGSvcDefaultInit,
|
---|
640 | vgsvcCpuHotPlugWorker,
|
---|
641 | vgsvcCpuHotPlugStop,
|
---|
642 | VGSvcDefaultTerm
|
---|
643 | };
|
---|
644 |
|
---|