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