VirtualBox

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

Last change on this file since 87198 was 87198, checked in by vboxsync, 4 years ago

IPRT/mp-darwin.cpp: Added RTMpCpuId implementations for bugref:9898.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.9 KB
Line 
1/* $Id: mp-darwin.cpp 87198 2021-01-08 14:58:51Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_SYSTEM
32#include <iprt/types.h>
33
34#include <unistd.h>
35#include <stdio.h>
36#include <sys/sysctl.h>
37#include <sys/stat.h>
38#include <sys/fcntl.h>
39#include <errno.h>
40#include <mach/mach.h>
41
42#include <iprt/mp.h>
43#include <iprt/cpuset.h>
44#include <iprt/assert.h>
45#include <iprt/string.h>
46
47
48/**
49 * Internal worker that determines the max possible logical CPU count (hyperthreads).
50 *
51 * @returns Max cpus.
52 */
53static RTCPUID rtMpDarwinMaxLogicalCpus(void)
54{
55 int cCpus = -1;
56 size_t cb = sizeof(cCpus);
57 int rc = sysctlbyname("hw.logicalcpu_max", &cCpus, &cb, NULL, 0);
58 if (rc != -1 && cCpus >= 1)
59 return cCpus;
60 AssertFailed();
61 return 1;
62}
63
64/**
65 * Internal worker that determines the max possible physical core count.
66 *
67 * @returns Max cpus.
68 */
69static RTCPUID rtMpDarwinMaxPhysicalCpus(void)
70{
71 int cCpus = -1;
72 size_t cb = sizeof(cCpus);
73 int rc = sysctlbyname("hw.physicalcpu_max", &cCpus, &cb, NULL, 0);
74 if (rc != -1 && cCpus >= 1)
75 return cCpus;
76 AssertFailed();
77 return 1;
78}
79
80
81#if 0 /* unused */
82/**
83 * Internal worker that determines the current number of logical CPUs (hyperthreads).
84 *
85 * @returns Max cpus.
86 */
87static RTCPUID rtMpDarwinOnlineLogicalCpus(void)
88{
89 int cCpus = -1;
90 size_t cb = sizeof(cCpus);
91 int rc = sysctlbyname("hw.logicalcpu", &cCpus, &cb, NULL, 0);
92 if (rc != -1 && cCpus >= 1)
93 return cCpus;
94 AssertFailed();
95 return 1;
96}
97#endif /* unused */
98
99
100/**
101 * Internal worker that determines the current number of physical CPUs.
102 *
103 * @returns Max cpus.
104 */
105static RTCPUID rtMpDarwinOnlinePhysicalCpus(void)
106{
107 int cCpus = -1;
108 size_t cb = sizeof(cCpus);
109 int rc = sysctlbyname("hw.physicalcpu", &cCpus, &cb, NULL, 0);
110 if (rc != -1 && cCpus >= 1)
111 return cCpus;
112 AssertFailed();
113 return 1;
114}
115
116
117#if defined(RT_ARCH_ARM64)
118RTDECL(RTCPUID) RTMpCpuId(void)
119{
120 /* xnu-7195.50.7.100.1/osfmk/arm64/start.s and machine_routines.c sets TPIDRRO_EL0
121 to the cpu_data_t::cpu_id value. */
122 uint64_t u64Ret;
123 __asm__ __volatile__("mrs %0,TPIDRRO_EL0\n\t" : "=r" (u64Ret));
124 return (RTCPUID)u64Ret;
125}
126#elif defined(RT_ARCH_ARM32)
127RTDECL(RTCPUID) RTMpCpuId(void)
128{
129 /* xnu-7195.50.7.100.1/osfmk/arm/start.s and machine_routines.c sets TPIDRURO
130 to the cpu_data_t::cpu_id value. */
131 uint32_t u32Ret;
132 __asm__ __volatile__("mrs p15, 0, %0, c13, c0, 3\n\t" : "=r" (u32Ret));
133 return (RTCPUID)u32Ret;
134}
135#endif
136
137
138RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
139{
140 return idCpu < RTCPUSET_MAX_CPUS && idCpu < rtMpDarwinMaxLogicalCpus() ? idCpu : -1;
141}
142
143
144RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
145{
146 return (unsigned)iCpu < rtMpDarwinMaxLogicalCpus() ? iCpu : NIL_RTCPUID;
147}
148
149
150RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
151{
152 return rtMpDarwinMaxLogicalCpus() - 1;
153}
154
155
156RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
157{
158#if 0
159 return RTMpIsCpuPossible(idCpu);
160#else
161 /** @todo proper ring-3 support on darwin, see @bugref{3014}. */
162 natural_t nCpus;
163 processor_basic_info_t pinfo;
164 mach_msg_type_number_t count;
165 kern_return_t krc = host_processor_info(mach_host_self(),
166 PROCESSOR_BASIC_INFO, &nCpus, (processor_info_array_t*)&pinfo, &count);
167 AssertReturn (krc == KERN_SUCCESS, true);
168 bool isOnline = idCpu < nCpus ? pinfo[idCpu].running : false;
169 vm_deallocate(mach_task_self(), (vm_address_t)pinfo, count * sizeof(*pinfo));
170 return isOnline;
171#endif
172}
173
174
175RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
176{
177 return idCpu != NIL_RTCPUID
178 && idCpu < rtMpDarwinMaxLogicalCpus();
179}
180
181
182RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
183{
184#if 0
185 RTCPUID cCpus = rtMpDarwinMaxLogicalCpus();
186 return RTCpuSetFromU64(RT_BIT_64(cCpus) - 1);
187
188#else
189 RTCpuSetEmpty(pSet);
190 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
191 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
192 if (RTMpIsCpuPossible(idCpu))
193 RTCpuSetAdd(pSet, idCpu);
194 return pSet;
195#endif
196}
197
198
199RTDECL(RTCPUID) RTMpGetCount(void)
200{
201 return rtMpDarwinMaxLogicalCpus();
202}
203
204
205RTDECL(RTCPUID) RTMpGetCoreCount(void)
206{
207 return rtMpDarwinMaxPhysicalCpus();
208}
209
210
211RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
212{
213#if 0
214 return RTMpGetSet(pSet);
215#else
216 RTCpuSetEmpty(pSet);
217 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
218 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
219 if (RTMpIsCpuOnline(idCpu))
220 RTCpuSetAdd(pSet, idCpu);
221 return pSet;
222#endif
223}
224
225
226RTDECL(RTCPUID) RTMpGetOnlineCount(void)
227{
228 RTCPUSET Set;
229 RTMpGetOnlineSet(&Set);
230 return RTCpuSetCount(&Set);
231}
232
233
234RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
235{
236 return rtMpDarwinOnlinePhysicalCpus();
237}
238
239
240RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
241{
242 /** @todo figure out how to get the current cpu speed on darwin. Have to check what powermanagement does. */
243 NOREF(idCpu);
244 return 0;
245}
246
247
248RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
249{
250 if (!RTMpIsCpuOnline(idCpu))
251 return 0;
252
253 /*
254 * Try the 'hw.cpufrequency_max' one.
255 */
256 uint64_t CpuFrequencyMax = 0;
257 size_t cb = sizeof(CpuFrequencyMax);
258 int rc = sysctlbyname("hw.cpufrequency_max", &CpuFrequencyMax, &cb, NULL, 0);
259 if (!rc)
260 return (CpuFrequencyMax + 999999) / 1000000;
261
262 /*
263 * Use the deprecated one.
264 */
265 int aiMib[2];
266 aiMib[0] = CTL_HW;
267 aiMib[1] = HW_CPU_FREQ;
268 int cCpus = -1;
269 cb = sizeof(cCpus);
270 rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
271 if (rc != -1 && cCpus >= 1)
272 return cCpus;
273 AssertFailed();
274 return 0;
275}
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