VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/Init.cpp@ 42004

Last change on this file since 42004 was 42004, checked in by vboxsync, 13 years ago

vbgl: close driver only when openned on uninit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Revision: 42004 $ */
2/** @file
3 * VBoxGuestLibR0 - Library initialization.
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* Header Files *
29*******************************************************************************/
30#define VBGL_DECL_DATA
31#include "VBGLInternal.h"
32
33#include <iprt/string.h>
34#include <iprt/assert.h>
35#include <iprt/semaphore.h>
36
37/*******************************************************************************
38* Global Variables *
39*******************************************************************************/
40/** The global VBGL instance data. */
41VBGLDATA g_vbgldata;
42
43/**
44 * Used by vbglQueryDriverInfo and VbglInit to try get the host feature mask and
45 * version information (g_vbgldata::hostVersion).
46 *
47 * This was first implemented by the host in 3.1 and we quietly ignore failures
48 * for that reason.
49 */
50static void vbglR0QueryHostVersion (void)
51{
52 VMMDevReqHostVersion *pReq;
53
54 int rc = VbglGRAlloc ((VMMDevRequestHeader **) &pReq, sizeof (*pReq), VMMDevReq_GetHostVersion);
55
56 if (RT_SUCCESS (rc))
57 {
58 rc = VbglGRPerform (&pReq->header);
59
60 if (RT_SUCCESS (rc))
61 {
62 g_vbgldata.hostVersion = *pReq;
63 Log (("vbglR0QueryHostVersion: %u.%u.%ur%u %#x\n",
64 pReq->major, pReq->minor, pReq->build, pReq->revision, pReq->features));
65 }
66
67 VbglGRFree (&pReq->header);
68 }
69}
70
71#ifndef VBGL_VBOXGUEST
72/**
73 * The guest library uses lazy initialization for VMMDev port and memory,
74 * because these values are provided by the VBoxGuest driver and it might
75 * be loaded later than other drivers.
76 *
77 * The VbglEnter checks the current library status, tries to retrieve these
78 * values and fails if they are unavailable.
79 *
80 */
81static void vbglQueryDriverInfo (void)
82{
83 int rc = VINF_SUCCESS;
84
85 rc = RTSemFastMutexRequest(g_vbgldata.mutexDriverInit);
86
87 if (RT_FAILURE(rc))
88 return;
89
90 if (g_vbgldata.status == VbglStatusReady)
91 {
92 RTSemFastMutexRelease(g_vbgldata.mutexDriverInit);
93 return;
94 }
95
96 rc = vbglDriverOpen(&g_vbgldata.driver);
97
98 if (RT_SUCCESS(rc))
99 {
100 /*
101 * Try query the port info.
102 */
103 VBoxGuestPortInfo port;
104
105 rc = vbglDriverIOCtl (&g_vbgldata.driver,
106 VBOXGUEST_IOCTL_GETVMMDEVPORT, &port,
107 sizeof (port));
108
109 if (RT_SUCCESS (rc))
110 {
111 dprintf (("port = 0x%04X, mem = %p\n", port.portAddress, port.pVMMDevMemory));
112
113 g_vbgldata.portVMMDev = port.portAddress;
114 g_vbgldata.pVMMDevMemory = port.pVMMDevMemory;
115
116 g_vbgldata.status = VbglStatusReady;
117
118 vbglR0QueryHostVersion();
119 }
120 }
121 RTSemFastMutexRelease(g_vbgldata.mutexDriverInit);
122 dprintf (("vbglQueryDriverInfo rc = %d\n", rc));
123}
124#endif /* !VBGL_VBOXGUEST */
125
126/**
127 * Checks if VBGL has been initialized.
128 *
129 * The client library, this will lazily complete the initialization.
130 *
131 * @return VINF_SUCCESS or VERR_VBGL_NOT_INITIALIZED.
132 */
133int vbglR0Enter (void)
134{
135 int rc;
136
137#ifndef VBGL_VBOXGUEST
138 if (g_vbgldata.status == VbglStatusInitializing)
139 {
140 vbglQueryDriverInfo ();
141 }
142#endif
143
144 rc = g_vbgldata.status == VbglStatusReady? VINF_SUCCESS: VERR_VBGL_NOT_INITIALIZED;
145
146 // dprintf(("VbglEnter: rc = %d\n", rc));
147
148 return rc;
149}
150
151int vbglInitCommon (void)
152{
153 int rc = VINF_SUCCESS;
154
155 RT_ZERO(g_vbgldata);
156
157 g_vbgldata.status = VbglStatusInitializing;
158
159 rc = VbglPhysHeapInit ();
160
161 if (RT_SUCCESS(rc))
162 {
163 /* other subsystems, none yet */
164 ;
165 }
166 else
167 LogRel(("vbglInitCommon: VbglPhysHeapInit failed. rc=%Rrc\n", rc));
168
169 dprintf(("vbglInitCommon: rc = %d\n", rc));
170
171 return rc;
172}
173
174DECLVBGL(void) vbglTerminateCommon (void)
175{
176 VbglPhysHeapTerminate ();
177
178 RT_ZERO(g_vbgldata);
179
180 return;
181}
182
183#ifdef VBGL_VBOXGUEST
184
185DECLVBGL(int) VbglInit (VBGLIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory)
186{
187 int rc = VINF_SUCCESS;
188
189# ifdef RT_OS_WINDOWS /** @todo r=bird: this doesn't make sense. Is there something special going on on windows? */
190 dprintf(("vbglInit: starts g_vbgldata.status %d\n", g_vbgldata.status));
191
192 if (g_vbgldata.status == VbglStatusInitializing
193 || g_vbgldata.status == VbglStatusReady)
194 {
195 /* Initialization is already in process. */
196 return rc;
197 }
198# else
199 dprintf(("vbglInit: starts\n"));
200# endif
201
202 rc = vbglInitCommon ();
203
204 if (RT_SUCCESS(rc))
205 {
206 g_vbgldata.portVMMDev = portVMMDev;
207 g_vbgldata.pVMMDevMemory = pVMMDevMemory;
208
209 g_vbgldata.status = VbglStatusReady;
210
211 vbglR0QueryHostVersion();
212 }
213 else
214 {
215 g_vbgldata.status = VbglStatusNotInitialized;
216 }
217
218 return rc;
219}
220
221DECLVBGL(void) VbglTerminate (void)
222{
223 vbglTerminateCommon ();
224
225 return;
226}
227
228
229#else /* !VBGL_VBOXGUEST */
230
231DECLVBGL(int) VbglInit (void)
232{
233 int rc = VINF_SUCCESS;
234
235 if (g_vbgldata.status == VbglStatusInitializing
236 || g_vbgldata.status == VbglStatusReady)
237 {
238 /* Initialization is already in process. */
239 return rc;
240 }
241
242 rc = vbglInitCommon ();
243
244 if (RT_SUCCESS(rc))
245 {
246 rc = RTSemFastMutexCreate(&g_vbgldata.mutexDriverInit);
247 if (RT_SUCCESS(rc))
248 {
249 /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
250 vbglQueryDriverInfo ();
251
252# ifdef VBOX_WITH_HGCM
253 rc = vbglR0HGCMInit ();
254# endif /* VBOX_WITH_HGCM */
255
256 if (RT_FAILURE(rc))
257 {
258 RTSemFastMutexDestroy(g_vbgldata.mutexDriverInit);
259 g_vbgldata.mutexDriverInit = NIL_RTSEMFASTMUTEX;
260 }
261 }
262
263 if (RT_FAILURE(rc))
264 {
265 vbglTerminateCommon ();
266 }
267
268 }
269
270 return rc;
271}
272
273DECLVBGL(bool) VbglIsReady(void)
274{
275 return(g_vbgldata.status == VbglStatusReady);
276}
277
278DECLVBGL(void) VbglTerminate (void)
279{
280# ifdef VBOX_WITH_HGCM
281 vbglR0HGCMTerminate ();
282# endif
283
284 vbglTerminateCommon ();
285 /* driver open could fail, which does not prevent VbglInit from succeeding,
286 * close the driver only if it is opened */
287 if (vbglDriverIsOpened(&g_vbgldata.driver))
288 vbglDriverClose(&g_vbgldata.driver);
289 RTSemFastMutexDestroy(g_vbgldata.mutexDriverInit);
290 g_vbgldata.mutexDriverInit = NIL_RTSEMFASTMUTEX;
291
292 return;
293}
294
295int vbglGetDriver(VBGLDRIVER **ppDriver)
296{
297 if (g_vbgldata.status != VbglStatusReady)
298 {
299 vbglQueryDriverInfo();
300 if (g_vbgldata.status != VbglStatusReady)
301 return VERR_TRY_AGAIN;
302 }
303 *ppDriver = &g_vbgldata.driver;
304 return VINF_SUCCESS;
305}
306
307#endif /* !VBGL_VBOXGUEST */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette