VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/threadctxhooks-r0drv-linux.c@ 54808

Last change on this file since 54808 was 54802, checked in by vboxsync, 10 years ago

another place where we have to handle SMAP

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/* $Id: threadctxhooks-r0drv-linux.c 54802 2015-03-17 08:32:43Z vboxsync $ */
2/** @file
3 * IPRT - Thread-Context Hook, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2013 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/err.h>
38#include <iprt/asm.h>
39#include "internal/thread.h"
40
41/*
42 * Linux kernel 2.6.23 introduced thread-context hooks but RedHat 2.6.18 kernels
43 * got it backported.
44 */
45#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) && defined(CONFIG_PREEMPT_NOTIFIERS)
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * The internal thread-context object.
52 */
53typedef struct RTTHREADCTXINT
54{
55 /** Magic value (RTTHREADCTXINT_MAGIC). */
56 uint32_t volatile u32Magic;
57 /** The thread handle (owner) for which the context-hooks are registered. */
58 RTNATIVETHREAD hOwner;
59 /** The preemption notifier object. */
60 struct preempt_notifier hPreemptNotifier;
61 /** Whether this handle has any hooks registered or not. */
62 bool fRegistered;
63 /** Pointer to the registered thread-context hook. */
64 PFNRTTHREADCTXHOOK pfnThreadCtxHook;
65 /** User argument passed to the thread-context hook. */
66 void *pvUser;
67 /** The thread-context operations. */
68 struct preempt_ops hPreemptOps;
69 /** The reference count for this object. */
70 uint32_t volatile cRefs;
71} RTTHREADCTXINT, *PRTTHREADCTXINT;
72
73
74/**
75 * Hook function for the thread-preempting event.
76 *
77 * @param pPreemptNotifier Pointer to the preempt_notifier struct.
78 * @param pNext Pointer to the task that is preempting the
79 * current thread.
80 *
81 * @remarks Called with the rq (runqueue) lock held and with preemption and
82 * interrupts disabled!
83 */
84static void rtThreadCtxHooksLnxSchedOut(struct preempt_notifier *pPreemptNotifier, struct task_struct *pNext)
85{
86 PRTTHREADCTXINT pThis = RT_FROM_MEMBER(pPreemptNotifier, RTTHREADCTXINT, hPreemptNotifier);
87 AssertPtr(pThis);
88 AssertPtr(pThis->pfnThreadCtxHook);
89 Assert(pThis->fRegistered);
90 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
91
92 stac();
93 pThis->pfnThreadCtxHook(RTTHREADCTXEVENT_PREEMPTING, pThis->pvUser);
94 clac();
95}
96
97
98/**
99 * Hook function for the thread-resumed event.
100 *
101 * @param pPreemptNotifier Pointer to the preempt_notifier struct.
102 * @param iCpu The CPU this thread is scheduled on.
103 *
104 * @remarks Called without holding the rq (runqueue) lock and with preemption
105 * enabled!
106 */
107static void rtThreadCtxHooksLnxSchedIn(struct preempt_notifier *pPreemptNotifier, int iCpu)
108{
109 PRTTHREADCTXINT pThis = RT_FROM_MEMBER(pPreemptNotifier, RTTHREADCTXINT, hPreemptNotifier);
110 AssertPtr(pThis);
111 AssertPtr(pThis->pfnThreadCtxHook);
112 Assert(pThis->fRegistered);
113
114 stac();
115 pThis->pfnThreadCtxHook(RTTHREADCTXEVENT_RESUMED, pThis->pvUser);
116 clac();
117}
118
119
120/**
121 * Worker function for RTThreadCtxHooks(Deregister|Release)().
122 *
123 * @param pThis Pointer to the internal thread-context object.
124 */
125DECLINLINE(void) rtThreadCtxHooksDeregister(PRTTHREADCTXINT pThis)
126{
127 preempt_notifier_unregister(&pThis->hPreemptNotifier);
128 pThis->hPreemptOps.sched_out = NULL;
129 pThis->hPreemptOps.sched_in = NULL;
130 pThis->fRegistered = false;
131}
132
133
134RTDECL(int) RTThreadCtxHooksCreate(PRTTHREADCTX phThreadCtx)
135{
136 PRTTHREADCTXINT pThis;
137 Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
138
139 pThis = (PRTTHREADCTXINT)RTMemAllocZ(sizeof(*pThis));
140 if (RT_UNLIKELY(!pThis))
141 return VERR_NO_MEMORY;
142 pThis->u32Magic = RTTHREADCTXINT_MAGIC;
143 pThis->hOwner = RTThreadNativeSelf();
144 pThis->fRegistered = false;
145 preempt_notifier_init(&pThis->hPreemptNotifier, &pThis->hPreemptOps);
146 pThis->cRefs = 1;
147
148 *phThreadCtx = pThis;
149 return VINF_SUCCESS;
150}
151RT_EXPORT_SYMBOL(RTThreadCtxHooksCreate);
152
153
154RTDECL(uint32_t) RTThreadCtxHooksRetain(RTTHREADCTX hThreadCtx)
155{
156 /*
157 * Validate input.
158 */
159 uint32_t cRefs;
160 PRTTHREADCTXINT pThis = hThreadCtx;
161 AssertPtr(pThis);
162 AssertMsgReturn(pThis->u32Magic == RTTHREADCTXINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
163 UINT32_MAX);
164
165 cRefs = ASMAtomicIncU32(&pThis->cRefs);
166 Assert(cRefs < UINT32_MAX / 2);
167 return cRefs;
168}
169RT_EXPORT_SYMBOL(RTThreadCtxHooksRetain);
170
171
172
173RTDECL(uint32_t) RTThreadCtxHooksRelease(RTTHREADCTX hThreadCtx)
174{
175 /*
176 * Validate input.
177 */
178 uint32_t cRefs;
179 PRTTHREADCTXINT pThis = hThreadCtx;
180 if (pThis == NIL_RTTHREADCTX)
181 return 0;
182
183 AssertPtr(pThis);
184 AssertMsgReturn(pThis->u32Magic == RTTHREADCTXINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
185 UINT32_MAX);
186 Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
187
188 cRefs = ASMAtomicDecU32(&pThis->cRefs);
189 if (!cRefs)
190 {
191 /*
192 * If there's still a registered thread-context hook, deregister it now before destroying the object.
193 */
194 if (pThis->fRegistered)
195 rtThreadCtxHooksDeregister(pThis);
196
197 /*
198 * Paranoia... but since these are ring-0 threads we can't be too careful.
199 */
200 Assert(!pThis->fRegistered);
201 Assert(!pThis->hPreemptOps.sched_out);
202 Assert(!pThis->hPreemptOps.sched_in);
203
204 ASMAtomicWriteU32(&pThis->u32Magic, ~RTTHREADCTXINT_MAGIC);
205 RTMemFree(pThis);
206 }
207 else
208 Assert(cRefs < UINT32_MAX / 2);
209
210 return cRefs;
211}
212RT_EXPORT_SYMBOL(RTThreadCtxHooksRelease);
213
214
215RTDECL(int) RTThreadCtxHooksRegister(RTTHREADCTX hThreadCtx, PFNRTTHREADCTXHOOK pfnThreadCtxHook, void *pvUser)
216{
217 /*
218 * Validate input.
219 */
220 PRTTHREADCTXINT pThis = hThreadCtx;
221 if (pThis == NIL_RTTHREADCTX)
222 return VERR_INVALID_HANDLE;
223 AssertPtr(pThis);
224 AssertMsgReturn(pThis->u32Magic == RTTHREADCTXINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
225 VERR_INVALID_HANDLE);
226 Assert(pThis->hOwner == RTThreadNativeSelf());
227 Assert(!pThis->hPreemptOps.sched_out);
228 Assert(!pThis->hPreemptOps.sched_in);
229
230 /*
231 * Register the callback.
232 */
233 pThis->hPreemptOps.sched_out = rtThreadCtxHooksLnxSchedOut;
234 pThis->hPreemptOps.sched_in = rtThreadCtxHooksLnxSchedIn;
235 pThis->pvUser = pvUser;
236 pThis->pfnThreadCtxHook = pfnThreadCtxHook;
237 pThis->fRegistered = true;
238 preempt_notifier_register(&pThis->hPreemptNotifier);
239
240 return VINF_SUCCESS;
241}
242RT_EXPORT_SYMBOL(RTThreadCtxHooksRegister);
243
244
245RTDECL(int) RTThreadCtxHooksDeregister(RTTHREADCTX hThreadCtx)
246{
247 /*
248 * Validate input.
249 */
250 PRTTHREADCTXINT pThis = hThreadCtx;
251 if (pThis == NIL_RTTHREADCTX)
252 return VERR_INVALID_HANDLE;
253 AssertPtr(pThis);
254 AssertMsgReturn(pThis->u32Magic == RTTHREADCTXINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis),
255 VERR_INVALID_HANDLE);
256 Assert(pThis->hOwner == RTThreadNativeSelf());
257 Assert(pThis->fRegistered);
258
259 /*
260 * Deregister the callback.
261 */
262 rtThreadCtxHooksDeregister(pThis);
263 return VINF_SUCCESS;
264}
265RT_EXPORT_SYMBOL(RTThreadCtxHooksDeregister);
266
267
268RTDECL(bool) RTThreadCtxHooksAreRegistered(RTTHREADCTX hThreadCtx)
269{
270 /*
271 * Validate input.
272 */
273 PRTTHREADCTXINT pThis = hThreadCtx;
274 if (pThis == NIL_RTTHREADCTX)
275 return false;
276 AssertPtr(pThis);
277 AssertMsg(pThis->u32Magic == RTTHREADCTXINT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis));
278
279 return pThis->fRegistered;
280}
281
282#else /* Not supported / Not needed */
283
284RTDECL(int) RTThreadCtxHooksCreate(PRTTHREADCTX phThreadCtx)
285{
286 NOREF(phThreadCtx);
287 return VERR_NOT_SUPPORTED;
288}
289RT_EXPORT_SYMBOL(RTThreadCtxHooksCreate);
290
291
292RTDECL(uint32_t) RTThreadCtxHooksRetain(RTTHREADCTX hThreadCtx)
293{
294 NOREF(hThreadCtx);
295 return UINT32_MAX;
296}
297RT_EXPORT_SYMBOL(RTThreadCtxHooksRetain);
298
299
300RTDECL(uint32_t) RTThreadCtxHooksRelease(RTTHREADCTX hThreadCtx)
301{
302 NOREF(hThreadCtx);
303 return UINT32_MAX;
304}
305RT_EXPORT_SYMBOL(RTThreadCtxHooksRelease);
306
307
308RTDECL(int) RTThreadCtxHooksRegister(RTTHREADCTX hThreadCtx, PFNRTTHREADCTXHOOK pfnThreadCtxHook, void *pvUser)
309{
310 NOREF(hThreadCtx);
311 NOREF(pfnThreadCtxHook);
312 NOREF(pvUser);
313 return VERR_NOT_SUPPORTED;
314}
315RT_EXPORT_SYMBOL(RTThreadCtxHooksRegister);
316
317
318RTDECL(int) RTThreadCtxHooksDeregister(RTTHREADCTX hThreadCtx)
319{
320 NOREF(hThreadCtx);
321 return VERR_NOT_SUPPORTED;
322}
323RT_EXPORT_SYMBOL(RTThreadCtxHooksDeregister);
324
325
326RTDECL(bool) RTThreadCtxHooksAreRegistered(RTTHREADCTX hThreadCtx)
327{
328 NOREF(hThreadCtx);
329 return false;
330}
331RT_EXPORT_SYMBOL(RTThreadCtxHooksAreRegistered);
332
333#endif /* Not supported / Not needed */
334
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette