VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/mp-darwin.cpp@ 99196

Last change on this file since 99196 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.1 KB
Line 
1/* $Id: mp-darwin.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_DEFAULT /*RTLOGGROUP_SYSTEM*/
42#include <iprt/types.h>
43
44#include <unistd.h>
45#include <stdio.h>
46#include <sys/sysctl.h>
47#include <sys/stat.h>
48#include <sys/fcntl.h>
49#include <errno.h>
50#include <mach/mach.h>
51
52#include <CoreFoundation/CFBase.h>
53#include <IOKit/IOKitLib.h>
54/*#include <IOKit/IOBSD.h>
55#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
56#include <IOKit/storage/IOBlockStorageDevice.h>
57#include <IOKit/storage/IOMedia.h>
58#include <IOKit/storage/IOCDMedia.h>
59#include <IOKit/scsi/SCSITaskLib.h>
60#include <SystemConfiguration/SystemConfiguration.h>
61#include <mach/mach_error.h>
62#include <sys/param.h>
63#include <paths.h>*/
64
65#include <iprt/mp.h>
66#include <iprt/assert.h>
67#include <iprt/cpuset.h>
68#include <iprt/log.h>
69#include <iprt/string.h>
70
71
72/**
73 * Internal worker that determines the max possible logical CPU count (hyperthreads).
74 *
75 * @returns Max cpus.
76 */
77static RTCPUID rtMpDarwinMaxLogicalCpus(void)
78{
79 int cCpus = -1;
80 size_t cb = sizeof(cCpus);
81 int rc = sysctlbyname("hw.logicalcpu_max", &cCpus, &cb, NULL, 0);
82 if (rc != -1 && cCpus >= 1)
83 return cCpus;
84 AssertFailed();
85 return 1;
86}
87
88/**
89 * Internal worker that determines the max possible physical core count.
90 *
91 * @returns Max cpus.
92 */
93static RTCPUID rtMpDarwinMaxPhysicalCpus(void)
94{
95 int cCpus = -1;
96 size_t cb = sizeof(cCpus);
97 int rc = sysctlbyname("hw.physicalcpu_max", &cCpus, &cb, NULL, 0);
98 if (rc != -1 && cCpus >= 1)
99 return cCpus;
100 AssertFailed();
101 return 1;
102}
103
104
105#if 0 /* unused */
106/**
107 * Internal worker that determines the current number of logical CPUs (hyperthreads).
108 *
109 * @returns Max cpus.
110 */
111static RTCPUID rtMpDarwinOnlineLogicalCpus(void)
112{
113 int cCpus = -1;
114 size_t cb = sizeof(cCpus);
115 int rc = sysctlbyname("hw.logicalcpu", &cCpus, &cb, NULL, 0);
116 if (rc != -1 && cCpus >= 1)
117 return cCpus;
118 AssertFailed();
119 return 1;
120}
121#endif /* unused */
122
123
124/**
125 * Internal worker that determines the current number of physical CPUs.
126 *
127 * @returns Max cpus.
128 */
129static RTCPUID rtMpDarwinOnlinePhysicalCpus(void)
130{
131 int cCpus = -1;
132 size_t cb = sizeof(cCpus);
133 int rc = sysctlbyname("hw.physicalcpu", &cCpus, &cb, NULL, 0);
134 if (rc != -1 && cCpus >= 1)
135 return cCpus;
136 AssertFailed();
137 return 1;
138}
139
140
141#if defined(RT_ARCH_ARM64)
142RTDECL(RTCPUID) RTMpCpuId(void)
143{
144 /* xnu-7195.50.7.100.1/osfmk/arm64/start.s and machine_routines.c sets TPIDRRO_EL0
145 to the cpu_data_t::cpu_id value. */
146 uint64_t u64Ret;
147 __asm__ __volatile__("mrs %0,TPIDRRO_EL0\n\t" : "=r" (u64Ret));
148 return (RTCPUID)u64Ret;
149}
150#elif defined(RT_ARCH_ARM32)
151RTDECL(RTCPUID) RTMpCpuId(void)
152{
153 /* xnu-7195.50.7.100.1/osfmk/arm/start.s and machine_routines.c sets TPIDRURO
154 to the cpu_data_t::cpu_id value. */
155 uint32_t u32Ret;
156 __asm__ __volatile__("mrs p15, 0, %0, c13, c0, 3\n\t" : "=r" (u32Ret));
157 return (RTCPUID)u32Ret;
158}
159#endif
160
161
162RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
163{
164 return idCpu < RTCPUSET_MAX_CPUS && idCpu < rtMpDarwinMaxLogicalCpus() ? idCpu : -1;
165}
166
167
168RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
169{
170 return (unsigned)iCpu < rtMpDarwinMaxLogicalCpus() ? iCpu : NIL_RTCPUID;
171}
172
173
174RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
175{
176 return rtMpDarwinMaxLogicalCpus() - 1;
177}
178
179
180RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
181{
182#if 0
183 return RTMpIsCpuPossible(idCpu);
184#else
185 /** @todo proper ring-3 support on darwin, see @bugref{3014}. */
186 natural_t cCpus;
187 processor_basic_info_t paInfo;
188 mach_msg_type_number_t cInfo;
189 kern_return_t krc = host_processor_info(mach_host_self(), PROCESSOR_BASIC_INFO,
190 &cCpus, (processor_info_array_t*)&paInfo, &cInfo);
191 AssertReturn(krc == KERN_SUCCESS, true);
192 bool const fIsOnline = idCpu < cCpus ? paInfo[idCpu].running : false;
193 vm_deallocate(mach_task_self(), (vm_address_t)paInfo, cInfo * sizeof(paInfo[0]));
194 return fIsOnline;
195#endif
196}
197
198
199RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
200{
201 return idCpu != NIL_RTCPUID
202 && idCpu < rtMpDarwinMaxLogicalCpus();
203}
204
205
206RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
207{
208#if 0
209 RTCPUID cCpus = rtMpDarwinMaxLogicalCpus();
210 return RTCpuSetFromU64(RT_BIT_64(cCpus) - 1);
211
212#else
213 RTCpuSetEmpty(pSet);
214 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
215 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
216 if (RTMpIsCpuPossible(idCpu))
217 RTCpuSetAdd(pSet, idCpu);
218 return pSet;
219#endif
220}
221
222
223RTDECL(RTCPUID) RTMpGetCount(void)
224{
225 return rtMpDarwinMaxLogicalCpus();
226}
227
228
229RTDECL(RTCPUID) RTMpGetCoreCount(void)
230{
231 return rtMpDarwinMaxPhysicalCpus();
232}
233
234
235RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
236{
237 RTCpuSetEmpty(pSet);
238#if 0
239 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
240 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
241 if (RTMpIsCpuOnline(idCpu))
242 RTCpuSetAdd(pSet, idCpu);
243#else
244 natural_t cCpus = 0;
245 processor_basic_info_t paInfo = NULL;
246 mach_msg_type_number_t cInfo = 0;
247 kern_return_t krc = host_processor_info(mach_host_self(), PROCESSOR_BASIC_INFO,
248 &cCpus, (processor_info_array_t *)&paInfo, &cInfo);
249 AssertReturn(krc == KERN_SUCCESS, pSet);
250
251 AssertStmt(cCpus <= RTCPUSET_MAX_CPUS, cCpus = RTCPUSET_MAX_CPUS);
252 for (natural_t idCpu = 0; idCpu < cCpus; idCpu++)
253 if (paInfo[idCpu].running)
254 RTCpuSetAdd(pSet, idCpu);
255
256 vm_deallocate(mach_task_self(), (vm_address_t)paInfo, cInfo * sizeof(paInfo[0]));
257#endif
258 return pSet;
259}
260
261
262RTDECL(RTCPUID) RTMpGetOnlineCount(void)
263{
264 RTCPUSET Set;
265 RTMpGetOnlineSet(&Set);
266 return RTCpuSetCount(&Set);
267}
268
269
270RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
271{
272 return rtMpDarwinOnlinePhysicalCpus();
273}
274
275
276RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
277{
278 /** @todo figure out how to get the current cpu speed on darwin. Have to
279 * check what powermanagement does. The powermetrics uses a private
280 * IOReportXxxx interface and *seems* (guessing) to calculate the frequency
281 * based on the frequency distribution over the last report period... This
282 * means that it's not really an suitable API for here. */
283 NOREF(idCpu);
284 return 0;
285}
286
287
288/**
289 * Worker for RTMpGetMaxFrequency.
290 * @returns Non-zero frequency in MHz on success, 0 on failure.
291 */
292static uint32_t rtMpDarwinGetMaxFrequencyFromIOService(io_service_t hCpu)
293{
294 io_struct_inband_t Buf = {0};
295 uint32_t cbActual = sizeof(Buf);
296 kern_return_t krc = IORegistryEntryGetProperty(hCpu, "clock-frequency", Buf, &cbActual);
297 Log2(("rtMpDarwinGetMaxFrequencyFromIOService: krc=%d; cbActual=%#x %.16Rhxs\n", krc, cbActual, Buf));
298 if (krc == kIOReturnSuccess)
299 {
300 switch (cbActual)
301 {
302 case sizeof(uint32_t):
303 return RT_BE2H_U32(*(uint32_t *)Buf) / 1000;
304 case sizeof(uint64_t):
305 AssertFailed();
306 return RT_BE2H_U64(*(uint64_t *)Buf) / 1000;
307 default:
308 AssertFailed();
309 }
310 }
311 return 0;
312}
313
314
315RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
316{
317 if (!RTMpIsCpuOnline(idCpu))
318 return 0;
319
320 /*
321 * Try the 'hw.cpufrequency_max' one.
322 */
323 uint64_t CpuFrequencyMax = 0;
324 size_t cb = sizeof(CpuFrequencyMax);
325 int rc = sysctlbyname("hw.cpufrequency_max", &CpuFrequencyMax, &cb, NULL, 0);
326 if (!rc)
327 return (CpuFrequencyMax + 999999) / 1000000;
328
329 /*
330 * Use the deprecated one.
331 */
332 int aiMib[2];
333 aiMib[0] = CTL_HW;
334 aiMib[1] = HW_CPU_FREQ;
335 int iDeprecatedFrequency = -1;
336 cb = sizeof(iDeprecatedFrequency);
337 rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &iDeprecatedFrequency, &cb, NULL, 0);
338 if (rc != -1 && iDeprecatedFrequency >= 1)
339 return iDeprecatedFrequency;
340
341 /*
342 * The above does not work for Apple M1 / xnu 20.1.0, so go look at the I/O registry instead.
343 *
344 * A sample ARM layout:
345 * | +-o cpu1@1 <class IOPlatformDevice, id 0x100000110, registered, matched, active, busy 0 (182 ms), retain 8>
346 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x10000021b, registered, matched, active, busy 0 (1 ms), retain 6>
347 * | +-o cpu2@2 <class IOPlatformDevice, id 0x100000111, registered, matched, active, busy 0 (175 ms), retain 8>
348 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x10000021c, registered, matched, active, busy 0 (3 ms), retain 6>
349 * | +-o cpu3@3 <class IOPlatformDevice, id 0x100000112, registered, matched, active, busy 0 (171 ms), retain 8>
350 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x10000021d, registered, matched, active, busy 0 (1 ms), retain 6>
351 * | +-o cpu4@100 <class IOPlatformDevice, id 0x100000113, registered, matched, active, busy 0 (171 ms), retain 8>
352 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x10000021e, registered, matched, active, busy 0 (1 ms), retain 6>
353 * | +-o cpu5@101 <class IOPlatformDevice, id 0x100000114, registered, matched, active, busy 0 (179 ms), retain 8>
354 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x10000021f, registered, matched, active, busy 0 (9 ms), retain 6>
355 * | +-o cpu6@102 <class IOPlatformDevice, id 0x100000115, registered, matched, active, busy 0 (172 ms), retain 8>
356 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x100000220, registered, matched, active, busy 0 (1 ms), retain 6>
357 * | +-o cpu7@103 <class IOPlatformDevice, id 0x100000116, registered, matched, active, busy 0 (175 ms), retain 8>
358 * | | +-o AppleARMCPU <class AppleARMCPU, id 0x100000221, registered, matched, active, busy 0 (5 ms), retain 6>
359 * | +-o cpus <class IOPlatformDevice, id 0x10000010e, registered, matched, active, busy 0 (12 ms), retain 15>
360 *
361 */
362
363#if 1 /* simpler way to get at it inspired by powermetrics, this is also used
364 in the arm version of RTMpGetDescription. */
365 /* Assume names on the form "cpu<N>" are only for CPUs. */
366 char szCpuPath[64];
367# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
368 RTStrPrintf(szCpuPath, sizeof(szCpuPath), "IODeviceTree:/cpus/CPU%X", idCpu);
369# else
370 RTStrPrintf(szCpuPath, sizeof(szCpuPath), "IODeviceTree:/cpus/cpu%x", idCpu); /** @todo Hex? M1 Max only has 10 cores... */
371# endif
372 io_registry_entry_t hIoRegEntry = IORegistryEntryFromPath(kIOMasterPortDefault, szCpuPath);
373 if (hIoRegEntry != MACH_PORT_NULL)
374 {
375 uint32_t uCpuFrequency = rtMpDarwinGetMaxFrequencyFromIOService(hIoRegEntry);
376 IOObjectRelease(hIoRegEntry);
377 if (uCpuFrequency)
378 return uCpuFrequency;
379 }
380
381#else
382 /* Assume names on the form "cpu<N>" are only for CPUs. */
383 char szCpuName[32];
384 RTStrPrintf(szCpuName, sizeof(szCpuName), "cpu%u", idCpu);
385 CFMutableDictionaryRef hMatchingDict = IOServiceNameMatching(szCpuName);
386 AssertReturn(hMatchingDict, 0);
387
388 /* Just get the first one. */
389 io_object_t hCpu = IOServiceGetMatchingService(kIOMasterPortDefault, hMatchingDict);
390 if (hCpu != 0)
391 {
392 uint32_t uCpuFrequency = rtMpDarwinGetMaxFrequencyFromIOService(hCpu);
393 IOObjectRelease(hCpu);
394 if (uCpuFrequency)
395 return uCpuFrequency;
396 }
397
398# if 1 /* Just in case... */
399 /* Create a matching dictionary for searching for CPU services in the IOKit. */
400# if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
401 hMatchingDict = IOServiceMatching("AppleARMCPU");
402# elif defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
403 hMatchingDict = IOServiceMatching("AppleACPICPU");
404# else
405# error "Port me!"
406# endif
407 AssertReturn(hMatchingDict, 0);
408
409 /* Perform the search and get a collection of Apple CPU services. */
410 io_iterator_t hCpuServices = IO_OBJECT_NULL;
411 IOReturn irc = IOServiceGetMatchingServices(kIOMasterPortDefault, hMatchingDict, &hCpuServices);
412 AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%d\n", irc), 0);
413 hMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
414
415 /* Enumerate the matching services. */
416 uint32_t uCpuFrequency = 0;
417 io_object_t hCurCpu;
418 while (uCpuFrequency == 0 && (hCurCpu = IOIteratorNext(hCpuServices)) != IO_OBJECT_NULL)
419 {
420 io_object_t hParent = (io_object_t)0;
421 irc = IORegistryEntryGetParentEntry(hCurCpu, kIOServicePlane, &hParent);
422 if (irc == kIOReturnSuccess && hParent)
423 {
424 uCpuFrequency = rtMpDarwinGetMaxFrequencyFromIOService(hParent);
425 IOObjectRelease(hParent);
426 }
427 IOObjectRelease(hCurCpu);
428 }
429 IOObjectRelease(hCpuServices);
430# endif
431#endif
432 AssertFailed();
433 return 0;
434}
435
Note: See TracBrowser for help on using the repository browser.

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