[1] | 1 | /* $Id: initterm-r0drv.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Initialization & Termination, R0 Driver, Common.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[207] | 41 | #include <iprt/initterm.h>
|
---|
[21337] | 42 | #include "internal/iprt.h"
|
---|
| 43 |
|
---|
[10390] | 44 | #include <iprt/asm.h>
|
---|
[1] | 45 | #include <iprt/assert.h>
|
---|
[403] | 46 | #include <iprt/err.h>
|
---|
[22052] | 47 | #include <iprt/mp.h>
|
---|
| 48 | #include <iprt/thread.h>
|
---|
[9602] | 49 | #ifndef IN_GUEST /* play safe for now */
|
---|
| 50 | # include "r0drv/mp-r0drv.h"
|
---|
[13466] | 51 | # include "r0drv/power-r0drv.h"
|
---|
[9602] | 52 | #endif
|
---|
[403] | 53 |
|
---|
[207] | 54 | #include "internal/initterm.h"
|
---|
[58269] | 55 | #include "internal/mem.h"
|
---|
[403] | 56 | #include "internal/thread.h"
|
---|
[1] | 57 |
|
---|
| 58 |
|
---|
[57358] | 59 | /*********************************************************************************************************************************
|
---|
| 60 | * Global Variables *
|
---|
| 61 | *********************************************************************************************************************************/
|
---|
[10390] | 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 |
|
---|
[1] | 69 | /**
|
---|
[33540] | 70 | * Initializes the ring-0 driver runtime library.
|
---|
[1] | 71 | *
|
---|
| 72 | * @returns iprt status code.
|
---|
| 73 | * @param fReserved Flags reserved for the future.
|
---|
| 74 | */
|
---|
| 75 | RTR0DECL(int) RTR0Init(unsigned fReserved)
|
---|
| 76 | {
|
---|
[403] | 77 | int rc;
|
---|
[47115] | 78 | uint32_t cNewUsers;
|
---|
[62567] | 79 | Assert(fReserved == 0); RT_NOREF_PV(fReserved);
|
---|
[52553] | 80 | #ifndef RT_OS_SOLARIS /* On Solaris our thread preemption information is only obtained in rtR0InitNative().*/
|
---|
[22052] | 81 | RT_ASSERT_PREEMPTIBLE();
|
---|
[52553] | 82 | #endif
|
---|
[10390] | 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 | */
|
---|
[47115] | 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 | }
|
---|
[10390] | 97 |
|
---|
[403] | 98 | rc = rtR0InitNative();
|
---|
| 99 | if (RT_SUCCESS(rc))
|
---|
| 100 | {
|
---|
[58269] | 101 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
| 102 | rtR0MemEfInit();
|
---|
| 103 | #endif
|
---|
[403] | 104 | rc = rtThreadInit();
|
---|
| 105 | if (RT_SUCCESS(rc))
|
---|
[9588] | 106 | {
|
---|
| 107 | #ifndef IN_GUEST /* play safe for now */
|
---|
[9602] | 108 | rc = rtR0MpNotificationInit();
|
---|
[13476] | 109 | if (RT_SUCCESS(rc))
|
---|
[24179] | 110 | {
|
---|
[13476] | 111 | rc = rtR0PowerNotificationInit();
|
---|
[24179] | 112 | if (RT_SUCCESS(rc))
|
---|
| 113 | return rc;
|
---|
| 114 | rtR0MpNotificationTerm();
|
---|
| 115 | }
|
---|
| 116 | #else
|
---|
[9588] | 117 | if (RT_SUCCESS(rc))
|
---|
| 118 | return rc;
|
---|
[24179] | 119 | #endif
|
---|
| 120 | rtThreadTerm();
|
---|
[9588] | 121 | }
|
---|
[58269] | 122 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
| 123 | rtR0MemEfTerm();
|
---|
| 124 | #endif
|
---|
[403] | 125 | rtR0TermNative();
|
---|
| 126 | }
|
---|
| 127 | return rc;
|
---|
[1] | 128 | }
|
---|
[21337] | 129 | RT_EXPORT_SYMBOL(RTR0Init);
|
---|
[1] | 130 |
|
---|
| 131 |
|
---|
[24179] | 132 | static void rtR0Term(void)
|
---|
| 133 | {
|
---|
| 134 | rtThreadTerm();
|
---|
| 135 | #ifndef IN_GUEST /* play safe for now */
|
---|
| 136 | rtR0PowerNotificationTerm();
|
---|
| 137 | rtR0MpNotificationTerm();
|
---|
| 138 | #endif
|
---|
[58269] | 139 | #ifdef RTR0MEM_WITH_EF_APIS
|
---|
| 140 | rtR0MemEfTerm();
|
---|
| 141 | #endif
|
---|
[24179] | 142 | rtR0TermNative();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
[1] | 146 | /**
|
---|
| 147 | * Terminates the ring-0 driver runtime library.
|
---|
| 148 | */
|
---|
| 149 | RTR0DECL(void) RTR0Term(void)
|
---|
| 150 | {
|
---|
[22052] | 151 | int32_t cNewUsers;
|
---|
| 152 | RT_ASSERT_PREEMPTIBLE();
|
---|
| 153 |
|
---|
| 154 | cNewUsers = ASMAtomicDecS32(&g_crtR0Users);
|
---|
[10390] | 155 | Assert(cNewUsers >= 0);
|
---|
[24179] | 156 | if (cNewUsers == 0)
|
---|
| 157 | rtR0Term();
|
---|
[47115] | 158 | else if (cNewUsers < 0)
|
---|
| 159 | ASMAtomicIncS32(&g_crtR0Users);
|
---|
[1] | 160 | }
|
---|
[21337] | 161 | RT_EXPORT_SYMBOL(RTR0Term);
|
---|
[1] | 162 |
|
---|
[24179] | 163 |
|
---|
| 164 | /* Note! Should *not* be exported since it's only for static linking. */
|
---|
| 165 | RTR0DECL(void) RTR0TermForced(void)
|
---|
| 166 | {
|
---|
| 167 | RT_ASSERT_PREEMPTIBLE();
|
---|
[47115] | 168 |
|
---|
[24179] | 169 | AssertMsg(g_crtR0Users == 1, ("%d\n", g_crtR0Users));
|
---|
[47115] | 170 | ASMAtomicWriteS32(&g_crtR0Users, 0);
|
---|
[24179] | 171 |
|
---|
| 172 | rtR0Term();
|
---|
| 173 | }
|
---|
| 174 |
|
---|