1 | /* $Id: threadctxhooks-r0drv-linux.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Context Switching Hook, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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 | #include "the-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
40 | # include <iprt/asm-amd64-x86.h>
|
---|
41 | #endif
|
---|
42 | #include "internal/thread.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Linux kernel 2.6.23 introduced preemption notifiers but RedHat 2.6.18 kernels
|
---|
47 | * got it backported.
|
---|
48 | */
|
---|
49 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) && defined(CONFIG_PREEMPT_NOTIFIERS)
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /**
|
---|
56 | * The internal hook object for linux.
|
---|
57 | */
|
---|
58 | typedef struct RTTHREADCTXHOOKINT
|
---|
59 | {
|
---|
60 | /** Magic value (RTTHREADCTXHOOKINT_MAGIC). */
|
---|
61 | uint32_t volatile u32Magic;
|
---|
62 | /** The thread handle (owner) for which the hook is registered. */
|
---|
63 | RTNATIVETHREAD hOwner;
|
---|
64 | /** The preemption notifier object. */
|
---|
65 | struct preempt_notifier LnxPreemptNotifier;
|
---|
66 | /** Whether the hook is enabled or not. If enabled, the LnxPreemptNotifier
|
---|
67 | * is linked into the owning thread's list of preemption callouts. */
|
---|
68 | bool fEnabled;
|
---|
69 | /** Pointer to the user callback. */
|
---|
70 | PFNRTTHREADCTXHOOK pfnCallback;
|
---|
71 | /** User argument passed to the callback. */
|
---|
72 | void *pvUser;
|
---|
73 | /** The linux callbacks. */
|
---|
74 | struct preempt_ops PreemptOps;
|
---|
75 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 19) && defined(RT_ARCH_AMD64)
|
---|
76 | /** Starting with 3.1.19, the linux kernel doesn't restore kernel RFLAGS during
|
---|
77 | * task switch, so we have to do that ourselves. (x86 code is not affected.) */
|
---|
78 | RTCCUINTREG fSavedRFlags;
|
---|
79 | #endif
|
---|
80 | } RTTHREADCTXHOOKINT;
|
---|
81 | typedef RTTHREADCTXHOOKINT *PRTTHREADCTXHOOKINT;
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Hook function for the thread schedule out event.
|
---|
86 | *
|
---|
87 | * @param pPreemptNotifier Pointer to the preempt_notifier struct.
|
---|
88 | * @param pNext Pointer to the task that is being scheduled
|
---|
89 | * instead of the current thread.
|
---|
90 | *
|
---|
91 | * @remarks Called with the rq (runqueue) lock held and with preemption and
|
---|
92 | * interrupts disabled!
|
---|
93 | */
|
---|
94 | static void rtThreadCtxHooksLnxSchedOut(struct preempt_notifier *pPreemptNotifier, struct task_struct *pNext)
|
---|
95 | {
|
---|
96 | PRTTHREADCTXHOOKINT pThis = RT_FROM_MEMBER(pPreemptNotifier, RTTHREADCTXHOOKINT, LnxPreemptNotifier);
|
---|
97 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
98 | RTCCUINTREG fSavedEFlags = ASMGetFlags();
|
---|
99 | stac();
|
---|
100 | #endif
|
---|
101 | RT_NOREF_PV(pNext);
|
---|
102 |
|
---|
103 | AssertPtr(pThis);
|
---|
104 | AssertPtr(pThis->pfnCallback);
|
---|
105 | Assert(pThis->fEnabled);
|
---|
106 | Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
107 |
|
---|
108 | pThis->pfnCallback(RTTHREADCTXEVENT_OUT, pThis->pvUser);
|
---|
109 |
|
---|
110 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
111 | ASMSetFlags(fSavedEFlags);
|
---|
112 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 19) && defined(RT_ARCH_AMD64)
|
---|
113 | pThis->fSavedRFlags = fSavedEFlags;
|
---|
114 | # endif
|
---|
115 | #endif
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Hook function for the thread schedule in event.
|
---|
121 | *
|
---|
122 | * @param pPreemptNotifier Pointer to the preempt_notifier struct.
|
---|
123 | * @param iCpu The CPU this thread is being scheduled on.
|
---|
124 | *
|
---|
125 | * @remarks Called without holding the rq (runqueue) lock and with preemption
|
---|
126 | * enabled!
|
---|
127 | * @todo r=bird: Preemption is of course disabled when it is called.
|
---|
128 | */
|
---|
129 | static void rtThreadCtxHooksLnxSchedIn(struct preempt_notifier *pPreemptNotifier, int iCpu)
|
---|
130 | {
|
---|
131 | PRTTHREADCTXHOOKINT pThis = RT_FROM_MEMBER(pPreemptNotifier, RTTHREADCTXHOOKINT, LnxPreemptNotifier);
|
---|
132 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
133 | RTCCUINTREG fSavedEFlags = ASMGetFlags();
|
---|
134 | stac();
|
---|
135 | #endif
|
---|
136 | RT_NOREF_PV(iCpu);
|
---|
137 |
|
---|
138 | AssertPtr(pThis);
|
---|
139 | AssertPtr(pThis->pfnCallback);
|
---|
140 | Assert(pThis->fEnabled);
|
---|
141 |
|
---|
142 | pThis->pfnCallback(RTTHREADCTXEVENT_IN, pThis->pvUser);
|
---|
143 |
|
---|
144 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
145 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 19) && defined(RT_ARCH_AMD64)
|
---|
146 | fSavedEFlags &= ~RT_BIT_64(18) /*X86_EFL_AC*/;
|
---|
147 | fSavedEFlags |= pThis->fSavedRFlags & RT_BIT_64(18) /*X86_EFL_AC*/;
|
---|
148 | # endif
|
---|
149 | ASMSetFlags(fSavedEFlags);
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Worker function for RTThreadCtxHooks(Deregister|Release)().
|
---|
156 | *
|
---|
157 | * @param pThis Pointer to the internal thread-context object.
|
---|
158 | */
|
---|
159 | DECLINLINE(void) rtThreadCtxHookDisable(PRTTHREADCTXHOOKINT pThis)
|
---|
160 | {
|
---|
161 | Assert(pThis->PreemptOps.sched_out == rtThreadCtxHooksLnxSchedOut);
|
---|
162 | Assert(pThis->PreemptOps.sched_in == rtThreadCtxHooksLnxSchedIn);
|
---|
163 | preempt_disable();
|
---|
164 | preempt_notifier_unregister(&pThis->LnxPreemptNotifier);
|
---|
165 | pThis->fEnabled = false;
|
---|
166 | preempt_enable();
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | RTDECL(int) RTThreadCtxHookCreate(PRTTHREADCTXHOOK phCtxHook, uint32_t fFlags, PFNRTTHREADCTXHOOK pfnCallback, void *pvUser)
|
---|
171 | {
|
---|
172 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Validate input.
|
---|
176 | */
|
---|
177 | PRTTHREADCTXHOOKINT pThis;
|
---|
178 | Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
179 | AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
|
---|
180 | AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Allocate and initialize a new hook. We don't register it yet, just
|
---|
184 | * create it.
|
---|
185 | */
|
---|
186 | pThis = (PRTTHREADCTXHOOKINT)RTMemAllocZ(sizeof(*pThis));
|
---|
187 | if (RT_UNLIKELY(!pThis))
|
---|
188 | {
|
---|
189 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
190 | return VERR_NO_MEMORY;
|
---|
191 | }
|
---|
192 | pThis->u32Magic = RTTHREADCTXHOOKINT_MAGIC;
|
---|
193 | pThis->hOwner = RTThreadNativeSelf();
|
---|
194 | pThis->fEnabled = false;
|
---|
195 | pThis->pfnCallback = pfnCallback;
|
---|
196 | pThis->pvUser = pvUser;
|
---|
197 | preempt_notifier_init(&pThis->LnxPreemptNotifier, &pThis->PreemptOps);
|
---|
198 | pThis->PreemptOps.sched_out = rtThreadCtxHooksLnxSchedOut;
|
---|
199 | pThis->PreemptOps.sched_in = rtThreadCtxHooksLnxSchedIn;
|
---|
200 |
|
---|
201 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
|
---|
202 | preempt_notifier_inc();
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | *phCtxHook = pThis;
|
---|
206 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
207 | return VINF_SUCCESS;
|
---|
208 | }
|
---|
209 | RT_EXPORT_SYMBOL(RTThreadCtxHookCreate);
|
---|
210 |
|
---|
211 |
|
---|
212 | RTDECL(int ) RTThreadCtxHookDestroy(RTTHREADCTXHOOK hCtxHook)
|
---|
213 | {
|
---|
214 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Validate input.
|
---|
218 | */
|
---|
219 | PRTTHREADCTXHOOKINT pThis = hCtxHook;
|
---|
220 | if (pThis == NIL_RTTHREADCTXHOOK)
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | AssertPtr(pThis);
|
---|
223 | AssertMsgReturn(pThis->u32Magic == RTTHREADCTXHOOKINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
|
---|
224 | VERR_INVALID_HANDLE);
|
---|
225 | Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
226 | Assert(!pThis->fEnabled || pThis->hOwner == RTThreadNativeSelf());
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * If there's still a registered thread-context hook, deregister it now before destroying the object.
|
---|
230 | */
|
---|
231 | if (pThis->fEnabled)
|
---|
232 | {
|
---|
233 | Assert(pThis->hOwner == RTThreadNativeSelf());
|
---|
234 | rtThreadCtxHookDisable(pThis);
|
---|
235 | Assert(!pThis->fEnabled); /* paranoia */
|
---|
236 | }
|
---|
237 |
|
---|
238 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
|
---|
239 | preempt_notifier_dec();
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTTHREADCTXHOOKINT_MAGIC);
|
---|
243 | RTMemFree(pThis);
|
---|
244 |
|
---|
245 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
246 | return VINF_SUCCESS;
|
---|
247 | }
|
---|
248 | RT_EXPORT_SYMBOL(RTThreadCtxHookDestroy);
|
---|
249 |
|
---|
250 |
|
---|
251 | RTDECL(int) RTThreadCtxHookEnable(RTTHREADCTXHOOK hCtxHook)
|
---|
252 | {
|
---|
253 | /*
|
---|
254 | * Validate input.
|
---|
255 | */
|
---|
256 | PRTTHREADCTXHOOKINT pThis = hCtxHook;
|
---|
257 | AssertPtr(pThis);
|
---|
258 | AssertMsgReturn(pThis->u32Magic == RTTHREADCTXHOOKINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
|
---|
259 | VERR_INVALID_HANDLE);
|
---|
260 | Assert(pThis->hOwner == RTThreadNativeSelf());
|
---|
261 | Assert(!pThis->fEnabled);
|
---|
262 | if (!pThis->fEnabled)
|
---|
263 | {
|
---|
264 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
265 | Assert(pThis->PreemptOps.sched_out == rtThreadCtxHooksLnxSchedOut);
|
---|
266 | Assert(pThis->PreemptOps.sched_in == rtThreadCtxHooksLnxSchedIn);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Register the callback.
|
---|
270 | */
|
---|
271 | preempt_disable();
|
---|
272 | pThis->fEnabled = true;
|
---|
273 | preempt_notifier_register(&pThis->LnxPreemptNotifier);
|
---|
274 | preempt_enable();
|
---|
275 |
|
---|
276 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
277 | }
|
---|
278 |
|
---|
279 | return VINF_SUCCESS;
|
---|
280 | }
|
---|
281 | RT_EXPORT_SYMBOL(RTThreadCtxHookEnable);
|
---|
282 |
|
---|
283 |
|
---|
284 | RTDECL(int) RTThreadCtxHookDisable(RTTHREADCTXHOOK hCtxHook)
|
---|
285 | {
|
---|
286 | /*
|
---|
287 | * Validate input.
|
---|
288 | */
|
---|
289 | PRTTHREADCTXHOOKINT pThis = hCtxHook;
|
---|
290 | if (pThis != NIL_RTTHREADCTXHOOK)
|
---|
291 | {
|
---|
292 | AssertPtr(pThis);
|
---|
293 | AssertMsgReturn(pThis->u32Magic == RTTHREADCTXHOOKINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
|
---|
294 | VERR_INVALID_HANDLE);
|
---|
295 | Assert(pThis->hOwner == RTThreadNativeSelf());
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Deregister the callback.
|
---|
299 | */
|
---|
300 | if (pThis->fEnabled)
|
---|
301 | {
|
---|
302 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
303 | rtThreadCtxHookDisable(pThis);
|
---|
304 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return VINF_SUCCESS;
|
---|
308 | }
|
---|
309 | RT_EXPORT_SYMBOL(RTThreadCtxHookDisable);
|
---|
310 |
|
---|
311 |
|
---|
312 | RTDECL(bool) RTThreadCtxHookIsEnabled(RTTHREADCTXHOOK hCtxHook)
|
---|
313 | {
|
---|
314 | /*
|
---|
315 | * Validate input.
|
---|
316 | */
|
---|
317 | PRTTHREADCTXHOOKINT pThis = hCtxHook;
|
---|
318 | if (pThis == NIL_RTTHREADCTXHOOK)
|
---|
319 | return false;
|
---|
320 | AssertPtr(pThis);
|
---|
321 | AssertMsgReturn(pThis->u32Magic == RTTHREADCTXHOOKINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
|
---|
322 | false);
|
---|
323 |
|
---|
324 | return pThis->fEnabled;
|
---|
325 | }
|
---|
326 |
|
---|
327 | #else /* Not supported / Not needed */
|
---|
328 | # include "../generic/threadctxhooks-r0drv-generic.cpp"
|
---|
329 | #endif /* Not supported / Not needed */
|
---|
330 |
|
---|