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