VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/powernotification-r0drv.c

Last change on this file was 109128, checked in by vboxsync, 6 days ago

IPRT/r0drv: Build adjustments for darwin.arm64. jiraref:VBP-1653

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: powernotification-r0drv.c 109128 2025-05-01 01:31:56Z vboxsync $ */
2/** @file
3 * IPRT - Power Management, Ring-0 Driver, Event Notifications.
4 */
5
6/*
7 * Copyright (C) 2008-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/power.h>
42#include "internal/iprt.h"
43
44#include <iprt/asm.h>
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
48# include <iprt/asm-arm.h>
49#endif
50#include <iprt/assert.h>
51#include <iprt/err.h>
52#include <iprt/mem.h>
53#include <iprt/spinlock.h>
54#include <iprt/string.h>
55#include <iprt/thread.h>
56#include "r0drv/mp-r0drv.h"
57#include "r0drv/power-r0drv.h"
58
59
60/*********************************************************************************************************************************
61* Structures and Typedefs *
62*********************************************************************************************************************************/
63/**
64 * Notification registration record tracking
65 * RTPowerRegisterNotification() calls.
66 */
67typedef struct RTPOWERNOTIFYREG
68{
69 /** Pointer to the next record. */
70 struct RTPOWERNOTIFYREG * volatile pNext;
71 /** The callback. */
72 PFNRTPOWERNOTIFICATION pfnCallback;
73 /** The user argument. */
74 void *pvUser;
75 /** Bit mask indicating whether we've done this callback or not. */
76 uint8_t bmDone[sizeof(void *)];
77} RTPOWERNOTIFYREG;
78/** Pointer to a registration record. */
79typedef RTPOWERNOTIFYREG *PRTPOWERNOTIFYREG;
80
81
82/*********************************************************************************************************************************
83* Global Variables *
84*********************************************************************************************************************************/
85/** The spinlock protecting the list. */
86static RTSPINLOCK volatile g_hRTPowerNotifySpinLock = NIL_RTSPINLOCK;
87/** List of callbacks, in registration order. */
88static PRTPOWERNOTIFYREG volatile g_pRTPowerCallbackHead = NULL;
89/** The current done bit. */
90static uint32_t volatile g_iRTPowerDoneBit;
91/** The list generation.
92 * This is increased whenever the list has been modified. The callback routine
93 * make use of this to avoid having restart at the list head after each callback. */
94static uint32_t volatile g_iRTPowerGeneration;
95
96
97
98
99RTDECL(int) RTPowerSignalEvent(RTPOWEREVENT enmEvent)
100{
101 PRTPOWERNOTIFYREG pCur;
102 RTSPINLOCK hSpinlock;
103
104 /*
105 * This is a little bit tricky as we cannot be holding the spinlock
106 * while calling the callback. This means that the list might change
107 * while we're walking it, and that multiple events might be running
108 * concurrently (depending on the OS).
109 *
110 * So, the first measure is to employ a 32-bitmask for each
111 * record where we'll use a bit that rotates for each call to
112 * this function to indicate which records that has been
113 * processed. This will take care of both changes to the list
114 * and a reasonable amount of concurrent events.
115 *
116 * In order to avoid having to restart the list walks for every
117 * callback we make, we'll make use a list generation number that is
118 * incremented everytime the list is changed. So, if it remains
119 * unchanged over a callback we can safely continue the iteration.
120 */
121 uint32_t iDone = ASMAtomicIncU32(&g_iRTPowerDoneBit);
122 iDone %= RT_SIZEOFMEMB(RTPOWERNOTIFYREG, bmDone) * 8;
123
124 hSpinlock = g_hRTPowerNotifySpinLock;
125 if (hSpinlock == NIL_RTSPINLOCK)
126 return VERR_ACCESS_DENIED;
127 RTSpinlockAcquire(hSpinlock);
128
129 /* Clear the bit. */
130 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
131 ASMAtomicBitClear(&pCur->bmDone[0], iDone);
132
133 /* Iterate the records and perform the callbacks. */
134 do
135 {
136 uint32_t const iGeneration = ASMAtomicUoReadU32(&g_iRTPowerGeneration);
137
138 pCur = g_pRTPowerCallbackHead;
139 while (pCur)
140 {
141 if (!ASMAtomicBitTestAndSet(&pCur->bmDone[0], iDone))
142 {
143 PFNRTPOWERNOTIFICATION pfnCallback = pCur->pfnCallback;
144 void *pvUser = pCur->pvUser;
145 pCur = pCur->pNext;
146 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
147
148 pfnCallback(enmEvent, pvUser);
149
150 /* carefully require the lock here, see RTR0MpNotificationTerm(). */
151 hSpinlock = g_hRTPowerNotifySpinLock;
152 if (hSpinlock == NIL_RTSPINLOCK)
153 return VERR_ACCESS_DENIED;
154 RTSpinlockAcquire(hSpinlock);
155 if (ASMAtomicUoReadU32(&g_iRTPowerGeneration) != iGeneration)
156 break;
157 }
158 else
159 pCur = pCur->pNext;
160 }
161 } while (pCur);
162
163 RTSpinlockRelease(hSpinlock);
164 return VINF_SUCCESS;
165}
166RT_EXPORT_SYMBOL(RTPowerSignalEvent);
167
168
169RTDECL(int) RTPowerNotificationRegister(PFNRTPOWERNOTIFICATION pfnCallback, void *pvUser)
170{
171 PRTPOWERNOTIFYREG pCur;
172 PRTPOWERNOTIFYREG pNew;
173
174 /*
175 * Validation.
176 */
177 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
178 AssertReturn(g_hRTPowerNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
179 RT_ASSERT_PREEMPTIBLE();
180
181 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
182 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
183 if ( pCur->pvUser == pvUser
184 && pCur->pfnCallback == pfnCallback)
185 break;
186 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
187 AssertMsgReturn(!pCur, ("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
188
189 /*
190 * Allocate a new record and attempt to insert it.
191 */
192 pNew = (PRTPOWERNOTIFYREG)RTMemAlloc(sizeof(*pNew));
193 if (!pNew)
194 return VERR_NO_MEMORY;
195
196 pNew->pNext = NULL;
197 pNew->pfnCallback = pfnCallback;
198 pNew->pvUser = pvUser;
199 memset(&pNew->bmDone[0], 0xff, sizeof(pNew->bmDone));
200
201 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
202
203 pCur = g_pRTPowerCallbackHead;
204 if (!pCur)
205 g_pRTPowerCallbackHead = pNew;
206 else
207 {
208 for (pCur = g_pRTPowerCallbackHead; ; pCur = pCur->pNext)
209 if ( pCur->pvUser == pvUser
210 && pCur->pfnCallback == pfnCallback)
211 break;
212 else if (!pCur->pNext)
213 {
214 pCur->pNext = pNew;
215 pCur = NULL;
216 break;
217 }
218 }
219
220 ASMAtomicIncU32(&g_iRTPowerGeneration);
221
222 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
223
224 /* duplicate? */
225 if (pCur)
226 {
227 RTMemFree(pCur);
228 AssertMsgFailedReturn(("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
229 }
230
231 return VINF_SUCCESS;
232}
233RT_EXPORT_SYMBOL(RTPowerNotificationRegister);
234
235
236RTDECL(int) RTPowerNotificationDeregister(PFNRTPOWERNOTIFICATION pfnCallback, void *pvUser)
237{
238 PRTPOWERNOTIFYREG pPrev;
239 PRTPOWERNOTIFYREG pCur;
240
241 /*
242 * Validation.
243 */
244 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
245 AssertReturn(g_hRTPowerNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
246 RT_ASSERT_INTS_ON();
247
248 /*
249 * Find and unlink the record from the list.
250 */
251 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
252 pPrev = NULL;
253 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
254 {
255 if ( pCur->pvUser == pvUser
256 && pCur->pfnCallback == pfnCallback)
257 break;
258 pPrev = pCur;
259 }
260 if (pCur)
261 {
262 if (pPrev)
263 pPrev->pNext = pCur->pNext;
264 else
265 g_pRTPowerCallbackHead = pCur->pNext;
266 ASMAtomicIncU32(&g_iRTPowerGeneration);
267 }
268 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
269
270 if (!pCur)
271 return VERR_NOT_FOUND;
272
273 /*
274 * Invalidate and free the record.
275 */
276 pCur->pNext = NULL;
277 pCur->pfnCallback = NULL;
278 RTMemFree(pCur);
279
280 return VINF_SUCCESS;
281}
282RT_EXPORT_SYMBOL(RTPowerNotificationDeregister);
283
284
285DECLHIDDEN(int) rtR0PowerNotificationInit(void)
286{
287 int rc = RTSpinlockCreate((PRTSPINLOCK)&g_hRTPowerNotifySpinLock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTR0Power");
288 if (RT_SUCCESS(rc))
289 {
290 /** @todo OS specific init here */
291 return rc;
292#if 0
293 RTSpinlockDestroy(g_hRTPowerNotifySpinLock);
294 g_hRTPowerNotifySpinLock = NIL_RTSPINLOCK;
295#endif
296 }
297 return rc;
298}
299
300
301DECLHIDDEN(void) rtR0PowerNotificationTerm(void)
302{
303 PRTPOWERNOTIFYREG pHead;
304 RTSPINLOCK hSpinlock = g_hRTPowerNotifySpinLock;
305 AssertReturnVoid(hSpinlock != NIL_RTSPINLOCK);
306
307 /** @todo OS specific term here */
308
309 /* pick up the list and the spinlock. */
310 RTSpinlockAcquire(hSpinlock);
311 ASMAtomicWriteHandle(&g_hRTPowerNotifySpinLock, NIL_RTSPINLOCK);
312 pHead = g_pRTPowerCallbackHead;
313 g_pRTPowerCallbackHead = NULL;
314 ASMAtomicIncU32(&g_iRTPowerGeneration);
315 RTSpinlockRelease(hSpinlock);
316
317 /* free the list. */
318 while (pHead)
319 {
320 PRTPOWERNOTIFYREG pFree = pHead;
321 pHead = pHead->pNext;
322
323 pFree->pNext = NULL;
324 pFree->pfnCallback = NULL;
325 RTMemFree(pFree);
326 }
327
328 RTSpinlockDestroy(hSpinlock);
329}
330
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