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