1 | /* $Id: mp-r0drv-linux.c 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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 | #include "the-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/mp.h>
|
---|
35 | #include <iprt/cpuset.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include "r0drv/mp-r0drv.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | RTDECL(RTCPUID) RTMpCpuId(void)
|
---|
42 | {
|
---|
43 | return smp_processor_id();
|
---|
44 | }
|
---|
45 | RT_EXPORT_SYMBOL(RTMpCpuId);
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
49 | {
|
---|
50 | return idCpu < NR_CPUS ? (int)idCpu : -1;
|
---|
51 | }
|
---|
52 | RT_EXPORT_SYMBOL(RTMpCpuIdToSetIndex);
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
56 | {
|
---|
57 | return iCpu < NR_CPUS ? (RTCPUID)iCpu : NIL_RTCPUID;
|
---|
58 | }
|
---|
59 | RT_EXPORT_SYMBOL(RTMpCpuIdFromSetIndex);
|
---|
60 |
|
---|
61 |
|
---|
62 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
63 | {
|
---|
64 | return NR_CPUS - 1; //???
|
---|
65 | }
|
---|
66 | RT_EXPORT_SYMBOL(RTMpGetMaxCpuId);
|
---|
67 |
|
---|
68 |
|
---|
69 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
70 | {
|
---|
71 | #if defined(CONFIG_SMP)
|
---|
72 | if (RT_UNLIKELY(idCpu >= NR_CPUS))
|
---|
73 | return false;
|
---|
74 |
|
---|
75 | # if defined(cpu_possible)
|
---|
76 | return cpu_possible(idCpu);
|
---|
77 | # else /* < 2.5.29 */
|
---|
78 | return idCpu < (RTCPUID)smp_num_cpus;
|
---|
79 | # endif
|
---|
80 | #else
|
---|
81 | return idCpu == RTMpCpuId();
|
---|
82 | #endif
|
---|
83 | }
|
---|
84 | RT_EXPORT_SYMBOL(RTMpIsCpuPossible);
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
88 | {
|
---|
89 | RTCPUID idCpu;
|
---|
90 |
|
---|
91 | RTCpuSetEmpty(pSet);
|
---|
92 | idCpu = RTMpGetMaxCpuId();
|
---|
93 | do
|
---|
94 | {
|
---|
95 | if (RTMpIsCpuPossible(idCpu))
|
---|
96 | RTCpuSetAdd(pSet, idCpu);
|
---|
97 | } while (idCpu-- > 0);
|
---|
98 | return pSet;
|
---|
99 | }
|
---|
100 | RT_EXPORT_SYMBOL(RTMpGetSet);
|
---|
101 |
|
---|
102 |
|
---|
103 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
104 | {
|
---|
105 | #ifdef CONFIG_SMP
|
---|
106 | # if defined(CONFIG_HOTPLUG_CPU) /* introduced & uses cpu_present */
|
---|
107 | return num_present_cpus();
|
---|
108 | # elif defined(num_possible_cpus)
|
---|
109 | return num_possible_cpus();
|
---|
110 | # elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
|
---|
111 | return smp_num_cpus;
|
---|
112 | # else
|
---|
113 | RTCPUSET Set;
|
---|
114 | RTMpGetSet(&Set);
|
---|
115 | return RTCpuSetCount(&Set);
|
---|
116 | # endif
|
---|
117 | #else
|
---|
118 | return 1;
|
---|
119 | #endif
|
---|
120 | }
|
---|
121 | RT_EXPORT_SYMBOL(RTMpGetCount);
|
---|
122 |
|
---|
123 |
|
---|
124 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
125 | {
|
---|
126 | #ifdef CONFIG_SMP
|
---|
127 | if (RT_UNLIKELY(idCpu >= NR_CPUS))
|
---|
128 | return false;
|
---|
129 | # ifdef cpu_online
|
---|
130 | return cpu_online(idCpu);
|
---|
131 | # else /* 2.4: */
|
---|
132 | return cpu_online_map & RT_BIT_64(idCpu);
|
---|
133 | # endif
|
---|
134 | #else
|
---|
135 | return idCpu == RTMpCpuId();
|
---|
136 | #endif
|
---|
137 | }
|
---|
138 | RT_EXPORT_SYMBOL(RTMpIsCpuOnline);
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
142 | {
|
---|
143 | #ifdef CONFIG_SMP
|
---|
144 | RTCPUID idCpu;
|
---|
145 |
|
---|
146 | RTCpuSetEmpty(pSet);
|
---|
147 | idCpu = RTMpGetMaxCpuId();
|
---|
148 | do
|
---|
149 | {
|
---|
150 | if (RTMpIsCpuOnline(idCpu))
|
---|
151 | RTCpuSetAdd(pSet, idCpu);
|
---|
152 | } while (idCpu-- > 0);
|
---|
153 | #else
|
---|
154 | RTCpuSetEmpty(pSet);
|
---|
155 | RTCpuSetAdd(pSet, RTMpCpuId());
|
---|
156 | #endif
|
---|
157 | return pSet;
|
---|
158 | }
|
---|
159 | RT_EXPORT_SYMBOL(RTMpGetOnlineSet);
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
163 | {
|
---|
164 | #ifdef CONFIG_SMP
|
---|
165 | # if defined(num_online_cpus)
|
---|
166 | return num_online_cpus();
|
---|
167 | # else
|
---|
168 | RTCPUSET Set;
|
---|
169 | RTMpGetOnlineSet(&Set);
|
---|
170 | return RTCpuSetCount(&Set);
|
---|
171 | # endif
|
---|
172 | #else
|
---|
173 | return 1;
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 | RT_EXPORT_SYMBOL(RTMpGetOnlineCount);
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(bool) RTMpIsCpuWorkPending(void)
|
---|
180 | {
|
---|
181 | /** @todo (not used on non-Windows platforms yet). */
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 | RT_EXPORT_SYMBOL(RTMpIsCpuWorkPending);
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Wrapper between the native linux per-cpu callbacks and PFNRTWORKER
|
---|
189 | *
|
---|
190 | * @param pvInfo Pointer to the RTMPARGS package.
|
---|
191 | */
|
---|
192 | static void rtmpLinuxWrapper(void *pvInfo)
|
---|
193 | {
|
---|
194 | PRTMPARGS pArgs = (PRTMPARGS)pvInfo;
|
---|
195 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
196 | pArgs->pfnWorker(RTMpCpuId(), pArgs->pvUser1, pArgs->pvUser2);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
201 | {
|
---|
202 | int rc;
|
---|
203 | RTMPARGS Args;
|
---|
204 |
|
---|
205 | Args.pfnWorker = pfnWorker;
|
---|
206 | Args.pvUser1 = pvUser1;
|
---|
207 | Args.pvUser2 = pvUser2;
|
---|
208 | Args.idCpu = NIL_RTCPUID;
|
---|
209 | Args.cHits = 0;
|
---|
210 |
|
---|
211 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
212 | rc = on_each_cpu(rtmpLinuxWrapper, &Args, 1 /* wait */);
|
---|
213 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
214 | rc = on_each_cpu(rtmpLinuxWrapper, &Args, 0 /* retry */, 1 /* wait */);
|
---|
215 |
|
---|
216 | #else /* older kernels */
|
---|
217 |
|
---|
218 | # ifdef preempt_disable
|
---|
219 | preempt_disable();
|
---|
220 | # endif
|
---|
221 | rc = smp_call_function(rtmpLinuxWrapper, &Args, 0 /* retry */, 1 /* wait */);
|
---|
222 | local_irq_disable();
|
---|
223 | rtmpLinuxWrapper(&Args);
|
---|
224 | local_irq_enable();
|
---|
225 | # ifdef preempt_enable
|
---|
226 | preempt_enable();
|
---|
227 | # endif
|
---|
228 | #endif /* older kernels */
|
---|
229 | Assert(rc == 0); NOREF(rc);
|
---|
230 | return VINF_SUCCESS;
|
---|
231 | }
|
---|
232 | RT_EXPORT_SYMBOL(RTMpOnAll);
|
---|
233 |
|
---|
234 |
|
---|
235 | RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
236 | {
|
---|
237 | int rc;
|
---|
238 | RTMPARGS Args;
|
---|
239 |
|
---|
240 | Args.pfnWorker = pfnWorker;
|
---|
241 | Args.pvUser1 = pvUser1;
|
---|
242 | Args.pvUser2 = pvUser2;
|
---|
243 | Args.idCpu = NIL_RTCPUID;
|
---|
244 | Args.cHits = 0;
|
---|
245 |
|
---|
246 | #ifdef preempt_disable
|
---|
247 | preempt_disable();
|
---|
248 | #endif
|
---|
249 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
250 | rc = smp_call_function(rtmpLinuxWrapper, &Args, 1 /* wait */);
|
---|
251 | #else /* older kernels */
|
---|
252 | rc = smp_call_function(rtmpLinuxWrapper, &Args, 0 /* retry */, 1 /* wait */);
|
---|
253 | #endif /* older kernels */
|
---|
254 | #ifdef preempt_enable
|
---|
255 | preempt_enable();
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | Assert(rc == 0); NOREF(rc);
|
---|
259 | return VINF_SUCCESS;
|
---|
260 | }
|
---|
261 | RT_EXPORT_SYMBOL(RTMpOnOthers);
|
---|
262 |
|
---|
263 |
|
---|
264 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
|
---|
265 | /**
|
---|
266 | * Wrapper between the native linux per-cpu callbacks and PFNRTWORKER
|
---|
267 | * employed by RTMpOnSpecific on older kernels that lacks smp_call_function_single.
|
---|
268 | *
|
---|
269 | * @param pvInfo Pointer to the RTMPARGS package.
|
---|
270 | */
|
---|
271 | static void rtmpOnSpecificLinuxWrapper(void *pvInfo)
|
---|
272 | {
|
---|
273 | PRTMPARGS pArgs = (PRTMPARGS)pvInfo;
|
---|
274 | RTCPUID idCpu = RTMpCpuId();
|
---|
275 |
|
---|
276 | if (idCpu == pArgs->idCpu)
|
---|
277 | {
|
---|
278 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
279 | ASMAtomicIncU32(&pArgs->cHits);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | #endif
|
---|
283 |
|
---|
284 |
|
---|
285 | RTDECL(int) RTMpOnSpecific(RTCPUID idCpu, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
286 | {
|
---|
287 | int rc;
|
---|
288 | RTMPARGS Args;
|
---|
289 |
|
---|
290 | Args.pfnWorker = pfnWorker;
|
---|
291 | Args.pvUser1 = pvUser1;
|
---|
292 | Args.pvUser2 = pvUser2;
|
---|
293 | Args.idCpu = idCpu;
|
---|
294 | Args.cHits = 0;
|
---|
295 |
|
---|
296 | if (!RTMpIsCpuPossible(idCpu))
|
---|
297 | return VERR_CPU_NOT_FOUND;
|
---|
298 |
|
---|
299 | # ifdef preempt_disable
|
---|
300 | preempt_disable();
|
---|
301 | # endif
|
---|
302 | if (idCpu != RTMpCpuId())
|
---|
303 | {
|
---|
304 | if (RTMpIsCpuOnline(idCpu))
|
---|
305 | {
|
---|
306 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
307 | rc = smp_call_function_single(idCpu, rtmpLinuxWrapper, &Args, 1 /* wait */);
|
---|
308 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
|
---|
309 | rc = smp_call_function_single(idCpu, rtmpLinuxWrapper, &Args, 0 /* retry */, 1 /* wait */);
|
---|
310 | #else /* older kernels */
|
---|
311 | rc = smp_call_function(rtmpOnSpecificLinuxWrapper, &Args, 0 /* retry */, 1 /* wait */);
|
---|
312 | #endif /* older kernels */
|
---|
313 | Assert(rc == 0);
|
---|
314 | rc = Args.cHits ? VINF_SUCCESS : VERR_CPU_OFFLINE;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | rc = VERR_CPU_OFFLINE;
|
---|
318 | }
|
---|
319 | else
|
---|
320 | {
|
---|
321 | rtmpLinuxWrapper(&Args);
|
---|
322 | rc = VINF_SUCCESS;
|
---|
323 | }
|
---|
324 | # ifdef preempt_enable
|
---|
325 | preempt_enable();
|
---|
326 | # endif
|
---|
327 |
|
---|
328 | NOREF(rc);
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 | RT_EXPORT_SYMBOL(RTMpOnSpecific);
|
---|
332 |
|
---|
333 |
|
---|
334 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
|
---|
335 | /**
|
---|
336 | * Dummy callback used by RTMpPokeCpu.
|
---|
337 | *
|
---|
338 | * @param pvInfo Ignored.
|
---|
339 | */
|
---|
340 | static void rtmpLinuxPokeCpuCallback(void *pvInfo)
|
---|
341 | {
|
---|
342 | NOREF(pvInfo);
|
---|
343 | }
|
---|
344 | #endif
|
---|
345 |
|
---|
346 |
|
---|
347 | RTDECL(int) RTMpPokeCpu(RTCPUID idCpu)
|
---|
348 | {
|
---|
349 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
|
---|
350 | int rc;
|
---|
351 |
|
---|
352 | if (!RTMpIsCpuPossible(idCpu))
|
---|
353 | return VERR_CPU_NOT_FOUND;
|
---|
354 | if (!RTMpIsCpuOnline(idCpu))
|
---|
355 | return VERR_CPU_OFFLINE;
|
---|
356 |
|
---|
357 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
|
---|
358 | rc = smp_call_function_single(idCpu, rtmpLinuxPokeCpuCallback, NULL, 0 /* wait */);
|
---|
359 | # elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
|
---|
360 | rc = smp_call_function_single(idCpu, rtmpLinuxPokeCpuCallback, NULL, 0 /* retry */, 0 /* wait */);
|
---|
361 | # else /* older kernels */
|
---|
362 | # error oops
|
---|
363 | # endif /* older kernels */
|
---|
364 | Assert(rc == 0);
|
---|
365 | return VINF_SUCCESS;
|
---|
366 |
|
---|
367 | #else /* older kernels */
|
---|
368 | /* no unicast here? */
|
---|
369 | return VERR_NOT_SUPPORTED;
|
---|
370 | #endif /* older kernels */
|
---|
371 | }
|
---|
372 | RT_EXPORT_SYMBOL(RTMpPokeCpu);
|
---|
373 |
|
---|