1 | /* $Id: initterm-r0drv.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Initialization & Termination, R0 Driver, Common.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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/initterm.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 | #include <iprt/mp.h>
|
---|
48 | #include <iprt/thread.h>
|
---|
49 | #ifndef IN_GUEST /* play safe for now */
|
---|
50 | # include "r0drv/mp-r0drv.h"
|
---|
51 | # include "r0drv/power-r0drv.h"
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #include "internal/initterm.h"
|
---|
55 | #include "internal/mem.h"
|
---|
56 | #include "internal/thread.h"
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Global Variables *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 | /** Count of current IPRT users.
|
---|
63 | * In ring-0 several drivers / kmods / kexts / wossnames may share the
|
---|
64 | * same runtime code. So, we need to keep count in order not to terminate
|
---|
65 | * it prematurely. */
|
---|
66 | static int32_t volatile g_crtR0Users = 0;
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Initializes the ring-0 driver runtime library.
|
---|
71 | *
|
---|
72 | * @returns iprt status code.
|
---|
73 | * @param fReserved Flags reserved for the future.
|
---|
74 | */
|
---|
75 | RTR0DECL(int) RTR0Init(unsigned fReserved)
|
---|
76 | {
|
---|
77 | int rc;
|
---|
78 | uint32_t cNewUsers;
|
---|
79 | Assert(fReserved == 0); RT_NOREF_PV(fReserved);
|
---|
80 | #ifndef RT_OS_SOLARIS /* On Solaris our thread preemption information is only obtained in rtR0InitNative().*/
|
---|
81 | RT_ASSERT_PREEMPTIBLE();
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * The first user initializes it.
|
---|
86 | * We rely on the module loader to ensure that there are no
|
---|
87 | * initialization races should two modules share the IPRT.
|
---|
88 | */
|
---|
89 | cNewUsers = ASMAtomicIncS32(&g_crtR0Users);
|
---|
90 | if (cNewUsers != 1)
|
---|
91 | {
|
---|
92 | if (cNewUsers > 1)
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | ASMAtomicDecS32(&g_crtR0Users);
|
---|
95 | return VERR_INTERNAL_ERROR_3;
|
---|
96 | }
|
---|
97 |
|
---|
98 | rc = rtR0InitNative();
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
102 | rtR0MemEfInit();
|
---|
103 | #endif
|
---|
104 | rc = rtThreadInit();
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | #ifndef IN_GUEST /* play safe for now */
|
---|
108 | rc = rtR0MpNotificationInit();
|
---|
109 | if (RT_SUCCESS(rc))
|
---|
110 | {
|
---|
111 | rc = rtR0PowerNotificationInit();
|
---|
112 | if (RT_SUCCESS(rc))
|
---|
113 | return rc;
|
---|
114 | rtR0MpNotificationTerm();
|
---|
115 | }
|
---|
116 | #else
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | return rc;
|
---|
119 | #endif
|
---|
120 | rtThreadTerm();
|
---|
121 | }
|
---|
122 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
123 | rtR0MemEfTerm();
|
---|
124 | #endif
|
---|
125 | rtR0TermNative();
|
---|
126 | }
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTR0Init);
|
---|
130 |
|
---|
131 |
|
---|
132 | static void rtR0Term(void)
|
---|
133 | {
|
---|
134 | rtThreadTerm();
|
---|
135 | #ifndef IN_GUEST /* play safe for now */
|
---|
136 | rtR0PowerNotificationTerm();
|
---|
137 | rtR0MpNotificationTerm();
|
---|
138 | #endif
|
---|
139 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
140 | rtR0MemEfTerm();
|
---|
141 | #endif
|
---|
142 | rtR0TermNative();
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Terminates the ring-0 driver runtime library.
|
---|
148 | */
|
---|
149 | RTR0DECL(void) RTR0Term(void)
|
---|
150 | {
|
---|
151 | int32_t cNewUsers;
|
---|
152 | RT_ASSERT_PREEMPTIBLE();
|
---|
153 |
|
---|
154 | cNewUsers = ASMAtomicDecS32(&g_crtR0Users);
|
---|
155 | Assert(cNewUsers >= 0);
|
---|
156 | if (cNewUsers == 0)
|
---|
157 | rtR0Term();
|
---|
158 | else if (cNewUsers < 0)
|
---|
159 | ASMAtomicIncS32(&g_crtR0Users);
|
---|
160 | }
|
---|
161 | RT_EXPORT_SYMBOL(RTR0Term);
|
---|
162 |
|
---|
163 |
|
---|
164 | /* Note! Should *not* be exported since it's only for static linking. */
|
---|
165 | RTR0DECL(void) RTR0TermForced(void)
|
---|
166 | {
|
---|
167 | RT_ASSERT_PREEMPTIBLE();
|
---|
168 |
|
---|
169 | AssertMsg(g_crtR0Users == 1, ("%d\n", g_crtR0Users));
|
---|
170 | ASMAtomicWriteS32(&g_crtR0Users, 0);
|
---|
171 |
|
---|
172 | rtR0Term();
|
---|
173 | }
|
---|
174 |
|
---|