summaryrefslogtreecommitdiff
path: root/src/VBox/VMM/include/TMInline.h
blob: 9f4010e8b8538a77d3f1fdb3edf86e0dea91dd14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* $Id$ */
/** @file
 * TM - Common Inlined functions.
 */

/*
 * Copyright (C) 2006-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef VMM_INCLUDED_SRC_include_TMInline_h
#define VMM_INCLUDED_SRC_include_TMInline_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif


DECLINLINE(PTMTIMER) tmTimerQueueGetHead(PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueueShared)
{
#ifdef IN_RING3
    RT_NOREF(pQueueShared);
    uint32_t const idx = pQueueCC->idxActive;
#else
    uint32_t const idx = pQueueShared->idxActive;
#endif
    if (idx < pQueueCC->cTimersAlloc)
        return &pQueueCC->paTimers[idx];
    return NULL;
}


DECLINLINE(void) tmTimerQueueSetHead(PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueueShared, PTMTIMER pHead)
{
    uint32_t idx;
    if (pHead)
    {
        idx = (uint32_t)(pHead - &pQueueCC->paTimers[0]);
        AssertMsgStmt(idx < pQueueCC->cTimersAlloc,
                      ("idx=%u (%s) cTimersAlloc=%u\n", idx, pHead->szName, pQueueCC->cTimersAlloc),
                      idx = UINT32_MAX);
    }
    else
        idx = UINT32_MAX;
#ifndef IN_RING3
    pQueueShared->idxActive = idx;
#else
    pQueueCC->idxActive     = idx;
    RT_NOREF(pQueueShared);
#endif
}


/**
 * Get the previous timer - translates TMTIMER::idxPrev.
 */
DECLINLINE(PTMTIMER) tmTimerGetPrev(PTMTIMERQUEUECC pQueueCC, PTMTIMER pTimer)
{
    uint32_t const idxPrev = pTimer->idxPrev;
    Assert(idxPrev);
    if (idxPrev < pQueueCC->cTimersAlloc)
        return &pQueueCC->paTimers[idxPrev];
    Assert(idxPrev == UINT32_MAX);
    return NULL;
}


/**
 * Get the next timer - translates TMTIMER::idxNext.
 */
DECLINLINE(PTMTIMER) tmTimerGetNext(PTMTIMERQUEUECC pQueueCC, PTMTIMER pTimer)
{
    uint32_t const idxNext = pTimer->idxNext;
    Assert(idxNext);
    if (idxNext < pQueueCC->cTimersAlloc)
        return &pQueueCC->paTimers[idxNext];
    Assert(idxNext == UINT32_MAX);
    return NULL;
}


/**
 * Set the previous timer link (TMTIMER::idxPrev).
 */
DECLINLINE(void) tmTimerSetPrev(PTMTIMERQUEUECC pQueueCC, PTMTIMER pTimer, PTMTIMER pPrev)
{
    uint32_t idxPrev;
    if (pPrev)
    {
        idxPrev = (uint32_t)(pPrev - &pQueueCC->paTimers[0]);
        Assert(idxPrev);
        AssertMsgStmt(idxPrev < pQueueCC->cTimersAlloc,
                      ("idxPrev=%u (%s) cTimersAlloc=%u\n", idxPrev, pPrev->szName, pQueueCC->cTimersAlloc),
                      idxPrev = UINT32_MAX);
    }
    else
        idxPrev = UINT32_MAX;
    pTimer->idxPrev = idxPrev;
}


/**
 * Set the next timer link (TMTIMER::idxNext).
 */
DECLINLINE(void) tmTimerSetNext(PTMTIMERQUEUECC pQueueCC, PTMTIMER pTimer, PTMTIMER pNext)
{
    uint32_t idxNext;
    if (pNext)
    {
        idxNext = (uint32_t)(pNext - &pQueueCC->paTimers[0]);
        Assert(idxNext);
        AssertMsgStmt(idxNext < pQueueCC->cTimersAlloc,
                      ("idxNext=%u (%s) cTimersAlloc=%u\n", idxNext, pNext->szName, pQueueCC->cTimersAlloc),
                      idxNext = UINT32_MAX);
    }
    else
        idxNext = UINT32_MAX;
    pTimer->idxNext = idxNext;
}


/**
 * Used to unlink a timer from the active list.
 *
 * @param   pVM         The cross context VM structure.
 * @param   pQueueCC    The context specific queue data (same as @a pQueue for
 *                      ring-3).
 * @param   pQueue      The shared timer queue data.
 * @param   pTimer      The timer that needs linking.
 *
 * @remarks Called while owning the relevant queue lock.
 */
DECL_FORCE_INLINE(void) tmTimerQueueUnlinkActive(PVMCC pVM, PTMTIMERQUEUECC pQueueCC, PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
{
#ifdef VBOX_STRICT
    TMTIMERSTATE const enmState = pTimer->enmState;
    Assert(  pQueue->enmClock == TMCLOCK_VIRTUAL_SYNC
           ? enmState == TMTIMERSTATE_ACTIVE
           : enmState == TMTIMERSTATE_PENDING_SCHEDULE || enmState == TMTIMERSTATE_PENDING_STOP_SCHEDULE);
#endif
    RT_NOREF(pVM);

    const PTMTIMER pPrev = tmTimerGetPrev(pQueueCC, pTimer);
    const PTMTIMER pNext = tmTimerGetNext(pQueueCC, pTimer);
    if (pPrev)
        tmTimerSetNext(pQueueCC, pPrev, pNext);
    else
    {
        tmTimerQueueSetHead(pQueueCC, pQueue, pNext);
        pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
        DBGFTRACE_U64_TAG(pVM, pQueue->u64Expire, "tmTimerQueueUnlinkActive");
    }
    if (pNext)
        tmTimerSetPrev(pQueueCC, pNext, pPrev);
    pTimer->idxNext = UINT32_MAX;
    pTimer->idxPrev = UINT32_MAX;
}

/** @def TMTIMER_HANDLE_TO_VARS_RETURN_EX
 * Converts a timer handle to a timer pointer, returning @a a_rcRet if the
 * handle is invalid.
 *
 * This defines the following variables:
 *      - idxQueue: The queue index.
 *      - pQueueCC: Pointer to the context specific queue data.
 *      - pTimer:   The timer pointer.
 *      - idxTimer: The timer index.
 *
 * @param   a_pVM           The cross context VM structure.
 * @param   a_hTimer        The timer handle to translate.
 * @param   a_rcRet         What to return on failure.
 *
 * @note    This macro has no scoping, so careful when using it around
 *          conditional statements!
 */
#ifdef IN_RING3
# define TMTIMER_HANDLE_TO_VARS_RETURN_EX(a_pVM, a_hTimer, a_rcRet) \
        uintptr_t const idxQueue = (uintptr_t)((a_hTimer) >> TMTIMERHANDLE_QUEUE_IDX_SHIFT) \
                                 & (uintptr_t)TMTIMERHANDLE_QUEUE_IDX_SMASK; \
        AssertReturn(idxQueue < RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues), a_rcRet); \
        PTMTIMERQUEUE const pQueue = &(a_pVM)->tm.s.aTimerQueues[idxQueue]; \
        PTMTIMERQUEUE const pQueueCC = pQueue; RT_NOREF(pQueueCC); \
        \
        uintptr_t const idxTimer = (uintptr_t)((a_hTimer) & TMTIMERHANDLE_TIMER_IDX_MASK); \
        AssertReturn(idxQueue < pQueue->cTimersAlloc, a_rcRet); \
        \
        PTMTIMER const pTimer = &pQueue->paTimers[idxTimer]; \
        AssertReturn(pTimer->hSelf == a_hTimer, a_rcRet)
#else
# define TMTIMER_HANDLE_TO_VARS_RETURN_EX(a_pVM, a_hTimer, a_rcRet) \
        uintptr_t const idxQueue = (uintptr_t)((a_hTimer) >> TMTIMERHANDLE_QUEUE_IDX_SHIFT) \
                                 & (uintptr_t)TMTIMERHANDLE_QUEUE_IDX_SMASK; \
        AssertReturn(idxQueue < RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues), a_rcRet); \
        AssertCompile(RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues) == RT_ELEMENTS((a_pVM)->tmr0.s.aTimerQueues)); \
        PTMTIMERQUEUE const   pQueue   = &(a_pVM)->tm.s.aTimerQueues[idxQueue]; RT_NOREF(pQueue); \
        PTMTIMERQUEUER0 const pQueueCC = &(a_pVM)->tmr0.s.aTimerQueues[idxQueue]; \
        \
        uintptr_t const idxTimer = (uintptr_t)((a_hTimer) & TMTIMERHANDLE_TIMER_IDX_MASK); \
        AssertReturn(idxQueue < pQueueCC->cTimersAlloc, a_rcRet); \
        \
        PTMTIMER const pTimer = &pQueueCC->paTimers[idxTimer]; \
        AssertReturn(pTimer->hSelf == a_hTimer, a_rcRet); \
        Assert(pTimer->fFlags & TMTIMER_FLAGS_RING0); \
        Assert(VM_IS_EMT(pVM))
#endif

/** @def TMTIMER_HANDLE_TO_VARS_RETURN
 * Converts a timer handle to a timer pointer, returning VERR_INVALID_HANDLE if
 * the handle is invalid.
 *
 * This defines the following variables:
 *      - idxQueue: The queue index.
 *      - pQueueCC: Pointer to the context specific queue data.
 *      - pTimer:   The timer pointer.
 *      - idxTimer: The timer index.
 *
 * @param   a_pVM       The cross context VM structure.
 * @param   a_hTimer    The timer handle to translate.
 *
 * @note    This macro has no scoping, so careful when using it around
 *          conditional statements!
 */
#define TMTIMER_HANDLE_TO_VARS_RETURN(a_pVM, a_hTimer) TMTIMER_HANDLE_TO_VARS_RETURN_EX(a_pVM, a_hTimer, VERR_INVALID_HANDLE)

/** @def TMTIMER_HANDLE_TO_VARS_RETURN_VOID
 * Converts a timer handle to a timer pointer, returning void if the
 * handle is invalid.
 *
 * This defines the following variables:
 *      - idxQueue: The queue index.
 *      - pQueueCC: Pointer to the context specific queue data.
 *      - pTimer:   The timer pointer.
 *      - idxTimer: The timer index.
 *
 * @param   a_pVM           The cross context VM structure.
 * @param   a_hTimer        The timer handle to translate.
 *
 * @note    This macro has no scoping, so careful when using it around
 *          conditional statements!
 */
#ifdef IN_RING3
# define TMTIMER_HANDLE_TO_VARS_RETURN_VOID(a_pVM, a_hTimer) \
        uintptr_t const idxQueue = (uintptr_t)((a_hTimer) >> TMTIMERHANDLE_QUEUE_IDX_SHIFT) \
                                 & (uintptr_t)TMTIMERHANDLE_QUEUE_IDX_SMASK; \
        AssertReturnVoid(idxQueue < RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues)); \
        PTMTIMERQUEUE const pQueue = &(a_pVM)->tm.s.aTimerQueues[idxQueue]; \
        PTMTIMERQUEUE const pQueueCC = pQueue; RT_NOREF(pQueueCC); \
        \
        uintptr_t const idxTimer = (uintptr_t)((a_hTimer) & TMTIMERHANDLE_TIMER_IDX_MASK); \
        AssertReturnVoid(idxQueue < pQueue->cTimersAlloc); \
        \
        PTMTIMER const pTimer = &pQueue->paTimers[idxTimer]; \
        AssertReturnVoid(pTimer->hSelf == a_hTimer)
#else
# define TMTIMER_HANDLE_TO_VARS_RETURN_VOID(a_pVM, a_hTimer) \
        uintptr_t const idxQueue = (uintptr_t)((a_hTimer) >> TMTIMERHANDLE_QUEUE_IDX_SHIFT) \
                                 & (uintptr_t)TMTIMERHANDLE_QUEUE_IDX_SMASK; \
        AssertReturnVoid(idxQueue < RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues)); \
        AssertCompile(RT_ELEMENTS((a_pVM)->tm.s.aTimerQueues) == RT_ELEMENTS((a_pVM)->tmr0.s.aTimerQueues)); \
        PTMTIMERQUEUE const   pQueue   = &(a_pVM)->tm.s.aTimerQueues[idxQueue]; RT_NOREF(pQueue); \
        PTMTIMERQUEUER0 const pQueueCC = &(a_pVM)->tmr0.s.aTimerQueues[idxQueue]; \
        \
        uintptr_t const idxTimer = (uintptr_t)((a_hTimer) & TMTIMERHANDLE_TIMER_IDX_MASK); \
        AssertReturnVoid(idxQueue < pQueueCC->cTimersAlloc); \
        \
        PTMTIMER const pTimer = &pQueueCC->paTimers[idxTimer]; \
        AssertReturnVoid(pTimer->hSelf == a_hTimer); \
        Assert(pTimer->fFlags & TMTIMER_FLAGS_RING0); \
        Assert(VM_IS_EMT(pVM))
#endif

#endif /* !VMM_INCLUDED_SRC_include_TMInline_h */