1 | /* $Id: initterm-r0drv.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Initialization & Termination, R0 Driver, Common.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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 | RT_ASSERT_PREEMPTIBLE();
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * The first user initializes it.
|
---|
73 | * We rely on the module loader to ensure that there are no
|
---|
74 | * initialization races should two modules share the IPRT.
|
---|
75 | */
|
---|
76 | cNewUsers = ASMAtomicIncS32(&g_crtR0Users);
|
---|
77 | if (cNewUsers != 1)
|
---|
78 | {
|
---|
79 | if (cNewUsers > 1)
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | ASMAtomicDecS32(&g_crtR0Users);
|
---|
82 | return VERR_INTERNAL_ERROR_3;
|
---|
83 | }
|
---|
84 |
|
---|
85 | rc = rtR0InitNative();
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | rc = rtThreadInit();
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | #ifndef IN_GUEST /* play safe for now */
|
---|
92 | rc = rtR0MpNotificationInit();
|
---|
93 | if (RT_SUCCESS(rc))
|
---|
94 | {
|
---|
95 | rc = rtR0PowerNotificationInit();
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | return rc;
|
---|
98 | rtR0MpNotificationTerm();
|
---|
99 | }
|
---|
100 | #else
|
---|
101 | if (RT_SUCCESS(rc))
|
---|
102 | return rc;
|
---|
103 | #endif
|
---|
104 | rtThreadTerm();
|
---|
105 | }
|
---|
106 | rtR0TermNative();
|
---|
107 | }
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 | RT_EXPORT_SYMBOL(RTR0Init);
|
---|
111 |
|
---|
112 |
|
---|
113 | static void rtR0Term(void)
|
---|
114 | {
|
---|
115 | rtThreadTerm();
|
---|
116 | #ifndef IN_GUEST /* play safe for now */
|
---|
117 | rtR0PowerNotificationTerm();
|
---|
118 | rtR0MpNotificationTerm();
|
---|
119 | #endif
|
---|
120 | rtR0TermNative();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Terminates the ring-0 driver runtime library.
|
---|
126 | */
|
---|
127 | RTR0DECL(void) RTR0Term(void)
|
---|
128 | {
|
---|
129 | int32_t cNewUsers;
|
---|
130 | RT_ASSERT_PREEMPTIBLE();
|
---|
131 |
|
---|
132 | cNewUsers = ASMAtomicDecS32(&g_crtR0Users);
|
---|
133 | Assert(cNewUsers >= 0);
|
---|
134 | if (cNewUsers == 0)
|
---|
135 | rtR0Term();
|
---|
136 | else if (cNewUsers < 0)
|
---|
137 | ASMAtomicIncS32(&g_crtR0Users);
|
---|
138 | }
|
---|
139 | RT_EXPORT_SYMBOL(RTR0Term);
|
---|
140 |
|
---|
141 |
|
---|
142 | /* Note! Should *not* be exported since it's only for static linking. */
|
---|
143 | RTR0DECL(void) RTR0TermForced(void)
|
---|
144 | {
|
---|
145 | RT_ASSERT_PREEMPTIBLE();
|
---|
146 |
|
---|
147 | AssertMsg(g_crtR0Users == 1, ("%d\n", g_crtR0Users));
|
---|
148 | ASMAtomicWriteS32(&g_crtR0Users, 0);
|
---|
149 |
|
---|
150 | rtR0Term();
|
---|
151 | }
|
---|
152 |
|
---|