1 | /* $Id: mpnotification-r0drv-linux.c 44529 2013-02-04 15:54:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor Event Notifications, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2011 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/asm-amd64-x86.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/cpuset.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include "r0drv/mp-r0drv.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71) && defined(CONFIG_SMP)
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Internal Functions *
|
---|
46 | *******************************************************************************/
|
---|
47 | static int rtMpNotificationLinuxCallback(struct notifier_block *pNotifierBlock, unsigned long ulNativeEvent, void *pvCpu);
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *******************************************************************************/
|
---|
53 | /**
|
---|
54 | * The notifier block we use for registering the callback.
|
---|
55 | */
|
---|
56 | static struct notifier_block g_NotifierBlock =
|
---|
57 | {
|
---|
58 | .notifier_call = rtMpNotificationLinuxCallback,
|
---|
59 | .next = NULL,
|
---|
60 | .priority = 0
|
---|
61 | };
|
---|
62 |
|
---|
63 | # ifdef CPU_DOWN_FAILED
|
---|
64 | /**
|
---|
65 | * The set of CPUs we've seen going offline recently.
|
---|
66 | */
|
---|
67 | static RTCPUSET g_MpPendingOfflineSet;
|
---|
68 | # endif
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Notification wrapper that updates CPU states and invokes our notification
|
---|
73 | * callbacks.
|
---|
74 | *
|
---|
75 | * @param idCpu The CPU Id.
|
---|
76 | * @param pvUser1 Pointer to the notifier_block (unused).
|
---|
77 | * @param pvUser2 The notification event.
|
---|
78 | * @remarks This can be invoked in interrupt context.
|
---|
79 | */
|
---|
80 | static DECLCALLBACK(void) rtMpNotificationLinuxOnCurrentCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
81 | {
|
---|
82 | unsigned long ulNativeEvent = *(unsigned long *)pvUser2;
|
---|
83 | NOREF(pvUser1);
|
---|
84 |
|
---|
85 | AssertRelease(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
86 | AssertReleaseMsg(idCpu == RTMpCpuId(), /* ASSUMES iCpu == RTCPUID */
|
---|
87 | ("idCpu=%u RTMpCpuId=%d ApicId=%d\n", idCpu, RTMpCpuId(), ASMGetApicId() ));
|
---|
88 |
|
---|
89 | switch (ulNativeEvent)
|
---|
90 | {
|
---|
91 | # ifdef CPU_DOWN_FAILED
|
---|
92 | case CPU_DOWN_FAILED:
|
---|
93 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
|
---|
94 | case CPU_DOWN_FAILED_FROZEN:
|
---|
95 | # endif
|
---|
96 | # endif
|
---|
97 | case CPU_ONLINE:
|
---|
98 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
99 | case CPU_ONLINE_FROZEN:
|
---|
100 | # endif
|
---|
101 | rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE, idCpu);
|
---|
102 | break;
|
---|
103 |
|
---|
104 | # ifdef CPU_DOWN_PREPARE
|
---|
105 | case CPU_DOWN_PREPARE:
|
---|
106 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
|
---|
107 | case CPU_DOWN_PREPARE_FROZEN:
|
---|
108 | # endif
|
---|
109 | rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE, idCpu);
|
---|
110 | break;
|
---|
111 | # endif
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * The native callback.
|
---|
118 | *
|
---|
119 | * @returns NOTIFY_DONE.
|
---|
120 | * @param pNotifierBlock Pointer to g_NotifierBlock.
|
---|
121 | * @param ulNativeEvent The native event.
|
---|
122 | * @param pvCpu The cpu id cast into a pointer value.
|
---|
123 | * @remarks This can fire with preemption enabled and on any CPU.
|
---|
124 | */
|
---|
125 | static int rtMpNotificationLinuxCallback(struct notifier_block *pNotifierBlock, unsigned long ulNativeEvent, void *pvCpu)
|
---|
126 | {
|
---|
127 | int rc;
|
---|
128 | bool fProcessEvent = false;
|
---|
129 | RTCPUID idCpu = (uintptr_t)pvCpu;
|
---|
130 | NOREF(pNotifierBlock);
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Note that redhat/CentOS ported _some_ of the FROZEN macros
|
---|
134 | * back to their 2.6.18-92.1.10.el5 kernel but actually don't
|
---|
135 | * use them. Thus we have to test for both CPU_TASKS_FROZEN and
|
---|
136 | * the individual event variants.
|
---|
137 | */
|
---|
138 | switch (ulNativeEvent)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Pick up online events or failures to go offline.
|
---|
142 | * Ignore failure events for CPUs we didn't see go offline.
|
---|
143 | */
|
---|
144 | # ifdef CPU_DOWN_FAILED
|
---|
145 | case CPU_DOWN_FAILED:
|
---|
146 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
|
---|
147 | case CPU_DOWN_FAILED_FROZEN:
|
---|
148 | # endif
|
---|
149 | if (!RTCpuSetIsMember(&g_MpPendingOfflineSet, idCpu))
|
---|
150 | break; /* fProcessEvents = false */
|
---|
151 | /* fall thru */
|
---|
152 | # endif
|
---|
153 | case CPU_ONLINE:
|
---|
154 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
155 | case CPU_ONLINE_FROZEN:
|
---|
156 | # endif
|
---|
157 | # ifdef CPU_DOWN_FAILED
|
---|
158 | RTCpuSetDel(&g_MpPendingOfflineSet, idCpu);
|
---|
159 | # endif
|
---|
160 | fProcessEvent = true;
|
---|
161 | break;
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Pick the earliest possible offline event.
|
---|
165 | * The only important thing here is that we get the event and that
|
---|
166 | * it's exactly one.
|
---|
167 | */
|
---|
168 | # ifdef CPU_DOWN_PREPARE
|
---|
169 | case CPU_DOWN_PREPARE:
|
---|
170 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
|
---|
171 | case CPU_DOWN_PREPARE_FROZEN:
|
---|
172 | # endif
|
---|
173 | fProcessEvent = true;
|
---|
174 | # else
|
---|
175 | case CPU_DEAD:
|
---|
176 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DEAD_FROZEN)
|
---|
177 | case CPU_DEAD_FROZEN:
|
---|
178 | # endif
|
---|
179 | /* Don't process CPU_DEAD notifications. */
|
---|
180 | # endif
|
---|
181 | # ifdef CPU_DOWN_FAILED
|
---|
182 | RTCpuSetAdd(&g_MpPendingOfflineSet, idCpu);
|
---|
183 | # endif
|
---|
184 | break;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!fProcessEvent)
|
---|
188 | return NOTIFY_DONE;
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Reschedule the callbacks to fire on the specific CPU with preemption disabled.
|
---|
192 | */
|
---|
193 | rc = RTMpOnSpecific(idCpu, rtMpNotificationLinuxOnCurrentCpu, pNotifierBlock, &ulNativeEvent);
|
---|
194 | Assert(RT_SUCCESS(rc)); NOREF(rc);
|
---|
195 | return NOTIFY_DONE;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
200 | {
|
---|
201 | int rc;
|
---|
202 |
|
---|
203 | # ifdef CPU_DOWN_FAILED
|
---|
204 | RTCpuSetEmpty(&g_MpPendingOfflineSet);
|
---|
205 | # endif
|
---|
206 |
|
---|
207 | rc = register_cpu_notifier(&g_NotifierBlock);
|
---|
208 | AssertMsgReturn(!rc, ("%d\n", rc), RTErrConvertFromErrno(rc));
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
214 | {
|
---|
215 | unregister_cpu_notifier(&g_NotifierBlock);
|
---|
216 | }
|
---|
217 |
|
---|
218 | #else /* Not supported / Not needed */
|
---|
219 |
|
---|
220 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
221 | {
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 |
|
---|
225 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
226 | {
|
---|
227 | }
|
---|
228 |
|
---|
229 | #endif /* Not supported / Not needed */
|
---|
230 |
|
---|