VirtualBox

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

Last change on this file since 57358 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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

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