1 | /* $Id: mpnotification-r0drv-linux.c 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor Event Notifications, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2016 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/asm-amd64-x86.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/cpuset.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include "r0drv/mp-r0drv.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71) && defined(CONFIG_SMP)
|
---|
42 |
|
---|
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 | * The native callback.
|
---|
73 | *
|
---|
74 | * @returns NOTIFY_DONE.
|
---|
75 | * @param pNotifierBlock Pointer to g_NotifierBlock.
|
---|
76 | * @param ulNativeEvent The native event.
|
---|
77 | * @param pvCpu The cpu id cast into a pointer value.
|
---|
78 | *
|
---|
79 | * @remarks This can fire with preemption enabled and on any CPU.
|
---|
80 | */
|
---|
81 | static int rtMpNotificationLinuxCallback(struct notifier_block *pNotifierBlock, unsigned long ulNativeEvent, void *pvCpu)
|
---|
82 | {
|
---|
83 | bool fProcessEvent = false;
|
---|
84 | RTCPUID idCpu = (uintptr_t)pvCpu;
|
---|
85 | NOREF(pNotifierBlock);
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Note that redhat/CentOS ported _some_ of the FROZEN macros
|
---|
89 | * back to their 2.6.18-92.1.10.el5 kernel but actually don't
|
---|
90 | * use them. Thus we have to test for both CPU_TASKS_FROZEN and
|
---|
91 | * the individual event variants.
|
---|
92 | */
|
---|
93 | switch (ulNativeEvent)
|
---|
94 | {
|
---|
95 | /*
|
---|
96 | * Pick up online events or failures to go offline.
|
---|
97 | * Ignore failure events for CPUs we didn't see go offline.
|
---|
98 | */
|
---|
99 | # ifdef CPU_DOWN_FAILED
|
---|
100 | case CPU_DOWN_FAILED:
|
---|
101 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
|
---|
102 | case CPU_DOWN_FAILED_FROZEN:
|
---|
103 | # endif
|
---|
104 | if (!RTCpuSetIsMember(&g_MpPendingOfflineSet, idCpu))
|
---|
105 | break; /* fProcessEvents = false */
|
---|
106 | /* fall thru */
|
---|
107 | # endif
|
---|
108 | case CPU_ONLINE:
|
---|
109 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
110 | case CPU_ONLINE_FROZEN:
|
---|
111 | # endif
|
---|
112 | # ifdef CPU_DOWN_FAILED
|
---|
113 | RTCpuSetDel(&g_MpPendingOfflineSet, idCpu);
|
---|
114 | # endif
|
---|
115 | fProcessEvent = true;
|
---|
116 | break;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Pick the earliest possible offline event.
|
---|
120 | * The only important thing here is that we get the event and that
|
---|
121 | * it's exactly one.
|
---|
122 | */
|
---|
123 | # ifdef CPU_DOWN_PREPARE
|
---|
124 | case CPU_DOWN_PREPARE:
|
---|
125 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
|
---|
126 | case CPU_DOWN_PREPARE_FROZEN:
|
---|
127 | # endif
|
---|
128 | fProcessEvent = true;
|
---|
129 | # else
|
---|
130 | case CPU_DEAD:
|
---|
131 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DEAD_FROZEN)
|
---|
132 | case CPU_DEAD_FROZEN:
|
---|
133 | # endif
|
---|
134 | /* Don't process CPU_DEAD notifications. */
|
---|
135 | # endif
|
---|
136 | # ifdef CPU_DOWN_FAILED
|
---|
137 | RTCpuSetAdd(&g_MpPendingOfflineSet, idCpu);
|
---|
138 | # endif
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (!fProcessEvent)
|
---|
143 | return NOTIFY_DONE;
|
---|
144 |
|
---|
145 | switch (ulNativeEvent)
|
---|
146 | {
|
---|
147 | # ifdef CPU_DOWN_FAILED
|
---|
148 | case CPU_DOWN_FAILED:
|
---|
149 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
|
---|
150 | case CPU_DOWN_FAILED_FROZEN:
|
---|
151 | # endif
|
---|
152 | # endif
|
---|
153 | case CPU_ONLINE:
|
---|
154 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
155 | case CPU_ONLINE_FROZEN:
|
---|
156 | # endif
|
---|
157 | rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE, idCpu);
|
---|
158 | break;
|
---|
159 |
|
---|
160 | # ifdef CPU_DOWN_PREPARE
|
---|
161 | case CPU_DOWN_PREPARE:
|
---|
162 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
|
---|
163 | case CPU_DOWN_PREPARE_FROZEN:
|
---|
164 | # endif
|
---|
165 | rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE, idCpu);
|
---|
166 | break;
|
---|
167 | # endif
|
---|
168 | }
|
---|
169 |
|
---|
170 | return NOTIFY_DONE;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
175 | {
|
---|
176 | int rc;
|
---|
177 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
178 |
|
---|
179 | # ifdef CPU_DOWN_FAILED
|
---|
180 | RTCpuSetEmpty(&g_MpPendingOfflineSet);
|
---|
181 | # endif
|
---|
182 |
|
---|
183 | rc = register_cpu_notifier(&g_NotifierBlock);
|
---|
184 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
185 | AssertMsgReturn(!rc, ("%d\n", rc), RTErrConvertFromErrno(rc));
|
---|
186 | return VINF_SUCCESS;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
191 | {
|
---|
192 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
193 | unregister_cpu_notifier(&g_NotifierBlock);
|
---|
194 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
195 | }
|
---|
196 |
|
---|
197 | #else /* Not supported / Not needed */
|
---|
198 |
|
---|
199 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
200 | {
|
---|
201 | return VINF_SUCCESS;
|
---|
202 | }
|
---|
203 |
|
---|
204 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
205 | {
|
---|
206 | }
|
---|
207 |
|
---|
208 | #endif /* Not supported / Not needed */
|
---|
209 |
|
---|