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