VirtualBox

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

Last change on this file since 21845 was 21461, checked in by vboxsync, 16 years ago

VBoxGuestLib: Implemented detection of physical page list support.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Revision: 21461 $ */
2/** @file
3 * VBoxGuestLibR0 - Library initialization.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define VBGL_DECL_DATA
26#include "VBGLInternal.h"
27
28#include <iprt/string.h>
29#include <iprt/assert.h>
30
31/*******************************************************************************
32* Global Variables *
33*******************************************************************************/
34/** The global VBGL instance data. */
35VBGLDATA g_vbgldata;
36
37/**
38 * Used by vbglQueryVMMDevPort and VbglInit to try get the host feature mask and
39 * version information (g_vbgldata::hostVersion).
40 *
41 * This was first implemented by the host in 3.1 and we quietly ignore failures
42 * for that reason.
43 */
44static void vbglR0QueryHostVersion (void)
45{
46 VMMDevReqHostVersion *pReq;
47
48 int rc = VbglGRAlloc ((VMMDevRequestHeader **) &pReq, sizeof (*pReq), VMMDevReq_GetHostVersion);
49
50 if (RT_SUCCESS (rc))
51 {
52 rc = VbglGRPerform (&pReq->header);
53
54 if (RT_SUCCESS (rc))
55 {
56 g_vbgldata.hostVersion = *pReq;
57 Log (("vbglR0QueryHostVersion: %u.%u.%ur%u %#x\n",
58 pReq->major, pReq->minor, pReq->build, pReq->revision, pReq->features));
59 }
60
61 VbglGRFree (&pReq->header);
62 }
63}
64
65#ifndef VBGL_VBOXGUEST
66/**
67 * The guest library uses lazy initialization for VMMDev port and memory,
68 * because these values are provided by the VBoxGuest driver and it might
69 * be loaded later than other drivers.
70 *
71 * The VbglEnter checks the current library status, tries to retrive these
72 * values and fails if they are unavailable.
73 *
74 */
75static void vbglQueryVMMDevPort (void)
76{
77 int rc = VINF_SUCCESS;
78
79 VBGLDRIVER driver;
80
81 rc = vbglDriverOpen (&driver);
82
83 if (RT_SUCCESS(rc))
84 {
85 /*
86 * Try query the port info.
87 */
88 VBoxGuestPortInfo port;
89
90 rc = vbglDriverIOCtl (&driver, VBOXGUEST_IOCTL_GETVMMDEVPORT, &port, sizeof (port));
91
92 if (RT_SUCCESS (rc))
93 {
94 dprintf (("port = 0x%04X, mem = %p\n", port.portAddress, port.pVMMDevMemory));
95
96 g_vbgldata.portVMMDev = port.portAddress;
97 g_vbgldata.pVMMDevMemory = port.pVMMDevMemory;
98
99 g_vbgldata.status = VbglStatusReady;
100
101 vbglR0QueryHostVersion();
102 }
103
104 vbglDriverClose (&driver);
105 }
106
107 dprintf (("vbglQueryVMMDevPort rc = %d\n", rc));
108}
109#endif /* !VBGL_VBOXGUEST */
110
111/**
112 * Checks if VBGL has been initialized.
113 *
114 * The the client library, this will lazily complete the initialization.
115 *
116 * @return VINF_SUCCESS or VERR_VBGL_NOT_INITIALIZED.
117 */
118int vbglR0Enter (void)
119{
120 int rc;
121
122#ifndef VBGL_VBOXGUEST
123 if (g_vbgldata.status == VbglStatusInitializing)
124 {
125 vbglQueryVMMDevPort ();
126 }
127#endif
128
129 rc = g_vbgldata.status == VbglStatusReady? VINF_SUCCESS: VERR_VBGL_NOT_INITIALIZED;
130
131 // dprintf(("VbglEnter: rc = %d\n", rc));
132
133 return rc;
134}
135
136int vbglInitCommon (void)
137{
138 int rc = VINF_SUCCESS;
139
140 memset(&g_vbgldata, 0, sizeof (VBGLDATA));
141
142 g_vbgldata.status = VbglStatusInitializing;
143
144 rc = VbglPhysHeapInit ();
145
146 if (RT_SUCCESS(rc))
147 {
148 /* other subsystems, none yet */
149 ;
150 }
151
152 dprintf(("vbglInitCommon: rc = %d\n", rc));
153
154 return rc;
155}
156
157DECLVBGL(void) vbglTerminateCommon (void)
158{
159 VbglPhysHeapTerminate ();
160
161 memset(&g_vbgldata, 0, sizeof (VBGLDATA));
162
163 return;
164}
165
166#ifdef VBGL_VBOXGUEST
167
168DECLVBGL(int) VbglInit (VBGLIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory)
169{
170 int rc = VINF_SUCCESS;
171
172# ifdef RT_OS_WINDOWS /** @todo r=bird: this doesn't make sense. Is there something special going on on windows? */
173 dprintf(("vbglInit: starts g_vbgldata.status %d\n", g_vbgldata.status));
174
175 if (g_vbgldata.status == VbglStatusInitializing
176 || g_vbgldata.status == VbglStatusReady)
177 {
178 /* Initialization is already in process. */
179 return rc;
180 }
181# else
182 dprintf(("vbglInit: starts\n"));
183# endif
184
185 rc = vbglInitCommon ();
186
187 if (RT_SUCCESS(rc))
188 {
189 g_vbgldata.portVMMDev = portVMMDev;
190 g_vbgldata.pVMMDevMemory = pVMMDevMemory;
191
192 g_vbgldata.status = VbglStatusReady;
193
194 vbglR0QueryHostVersion();
195 }
196 else
197 {
198 g_vbgldata.status = VbglStatusNotInitialized;
199 }
200
201 return rc;
202}
203
204DECLVBGL(void) VbglTerminate (void)
205{
206 vbglTerminateCommon ();
207
208 return;
209}
210
211
212#else /* !VBGL_VBOXGUEST */
213
214DECLVBGL(int) VbglInit (void)
215{
216 int rc = VINF_SUCCESS;
217
218 if (g_vbgldata.status == VbglStatusInitializing
219 || g_vbgldata.status == VbglStatusReady)
220 {
221 /* Initialization is already in process. */
222 return rc;
223 }
224
225 rc = vbglInitCommon ();
226
227 if (RT_SUCCESS(rc))
228 {
229 /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
230 vbglQueryVMMDevPort ();
231
232# ifdef VBOX_WITH_HGCM
233 rc = vbglR0HGCMInit ();
234# endif /* VBOX_WITH_HGCM */
235
236 if (RT_FAILURE(rc))
237 {
238 vbglTerminateCommon ();
239 }
240 }
241
242 return rc;
243}
244
245DECLVBGL(void) VbglTerminate (void)
246{
247 vbglTerminateCommon ();
248
249# ifdef VBOX_WITH_HGCM
250 vbglR0HGCMTerminate ();
251# endif
252
253 return;
254}
255
256#endif /* !VBGL_VBOXGUEST */
257
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