VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR0LibInit.cpp@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: VBoxGuestR0LibInit.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBoxGuestLibR0 - Library initialization.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include "VBoxGuestR0LibInternal.h"
36
37#include <iprt/string.h>
38#include <iprt/assert.h>
39#include <iprt/semaphore.h>
40#include <VBox/err.h>
41
42
43/*********************************************************************************************************************************
44* Global Variables *
45*********************************************************************************************************************************/
46/** The global VBGL instance data. */
47VBGLDATA g_vbgldata;
48
49
50/**
51 * Used by vbglR0QueryDriverInfo and VbglInit to try get the host feature mask
52 * and version information (g_vbgldata::hostVersion).
53 *
54 * This was first implemented by the host in 3.1 and we quietly ignore failures
55 * for that reason.
56 */
57static void vbglR0QueryHostVersion(void)
58{
59 VMMDevReqHostVersion *pReq;
60 int rc = VbglR0GRAlloc((VMMDevRequestHeader **) &pReq, sizeof (*pReq), VMMDevReq_GetHostVersion);
61 if (RT_SUCCESS(rc))
62 {
63 rc = VbglR0GRPerform(&pReq->header);
64 if (RT_SUCCESS(rc))
65 {
66 g_vbgldata.hostVersion = *pReq;
67 Log(("vbglR0QueryHostVersion: %u.%u.%ur%u %#x\n",
68 pReq->major, pReq->minor, pReq->build, pReq->revision, pReq->features));
69 }
70
71 VbglR0GRFree(&pReq->header);
72 }
73}
74
75
76#ifndef VBGL_VBOXGUEST
77/**
78 * The guest library uses lazy initialization for VMMDev port and memory,
79 * because these values are provided by the VBoxGuest driver and it might
80 * be loaded later than other drivers.
81 *
82 * The VbglEnter checks the current library status, tries to retrieve these
83 * values and fails if they are unavailable.
84 */
85static int vbglR0QueryDriverInfo(void)
86{
87# ifdef VBGLDATA_USE_FAST_MUTEX
88 int rc = RTSemFastMutexRequest(g_vbgldata.hMtxIdcSetup);
89# else
90 int rc = RTSemMutexRequest(g_vbgldata.hMtxIdcSetup, RT_INDEFINITE_WAIT);
91# endif
92 if (RT_SUCCESS(rc))
93 {
94 if (g_vbgldata.status == VbglStatusReady)
95 { /* likely */ }
96 else
97 {
98 rc = VbglR0IdcOpen(&g_vbgldata.IdcHandle,
99 VBGL_IOC_VERSION /*uReqVersion*/,
100 VBGL_IOC_VERSION & UINT32_C(0xffff0000) /*uMinVersion*/,
101 NULL /*puSessionVersion*/, NULL /*puDriverVersion*/, NULL /*puDriverRevision*/);
102 if (RT_SUCCESS(rc))
103 {
104 /*
105 * Try query the port info.
106 */
107 VBGLIOCGETVMMDEVIOINFO PortInfo;
108 RT_ZERO(PortInfo);
109 VBGLREQHDR_INIT(&PortInfo.Hdr, GET_VMMDEV_IO_INFO);
110 rc = VbglR0IdcCall(&g_vbgldata.IdcHandle, VBGL_IOCTL_GET_VMMDEV_IO_INFO, &PortInfo.Hdr, sizeof(PortInfo));
111 if (RT_SUCCESS(rc))
112 {
113 dprintf(("Port I/O = 0x%04x, MMIO = %p\n", PortInfo.u.Out.IoPort, PortInfo.u.Out.pvVmmDevMapping));
114
115 g_vbgldata.portVMMDev = PortInfo.u.Out.IoPort;
116 g_vbgldata.pVMMDevMemory = (VMMDevMemory *)PortInfo.u.Out.pvVmmDevMapping;
117 g_vbgldata.status = VbglStatusReady;
118
119 vbglR0QueryHostVersion();
120 }
121 }
122
123 dprintf(("vbglQueryDriverInfo rc = %Rrc\n", rc));
124 }
125
126# ifdef VBGLDATA_USE_FAST_MUTEX
127 RTSemFastMutexRelease(g_vbgldata.hMtxIdcSetup);
128# else
129 RTSemMutexRelease(g_vbgldata.hMtxIdcSetup);
130# endif
131 }
132 return rc;
133}
134#endif /* !VBGL_VBOXGUEST */
135
136/**
137 * Checks if VBGL has been initialized.
138 *
139 * The client library, this will lazily complete the initialization.
140 *
141 * @return VINF_SUCCESS or VERR_VBGL_NOT_INITIALIZED.
142 */
143int vbglR0Enter(void)
144{
145 if (g_vbgldata.status == VbglStatusReady)
146 return VINF_SUCCESS;
147
148#ifndef VBGL_VBOXGUEST
149 if (g_vbgldata.status == VbglStatusInitializing)
150 {
151 vbglR0QueryDriverInfo();
152 if (g_vbgldata.status == VbglStatusReady)
153 return VINF_SUCCESS;
154 }
155#endif
156 return VERR_VBGL_NOT_INITIALIZED;
157}
158
159
160static int vbglR0InitCommon(void)
161{
162 int rc;
163
164 RT_ZERO(g_vbgldata);
165 g_vbgldata.status = VbglStatusInitializing;
166
167 rc = VbglR0PhysHeapInit();
168 if (RT_SUCCESS(rc))
169 {
170 dprintf(("vbglR0InitCommon: returns rc = %d\n", rc));
171 return rc;
172 }
173
174 LogRel(("vbglR0InitCommon: VbglR0PhysHeapInit failed: rc=%Rrc\n", rc));
175 g_vbgldata.status = VbglStatusNotInitialized;
176 return rc;
177}
178
179
180static void vbglR0TerminateCommon(void)
181{
182 VbglR0PhysHeapTerminate();
183 g_vbgldata.status = VbglStatusNotInitialized;
184}
185
186#ifdef VBGL_VBOXGUEST
187
188DECLR0VBGL(int) VbglR0InitPrimary(RTIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory, uint32_t *pfFeatures)
189{
190 int rc;
191
192# ifdef RT_OS_WINDOWS /** @todo r=bird: this doesn't make sense. Is there something special going on on windows? */
193 dprintf(("vbglInit: starts g_vbgldata.status %d\n", g_vbgldata.status));
194
195 if ( g_vbgldata.status == VbglStatusInitializing
196 || g_vbgldata.status == VbglStatusReady)
197 {
198 /* Initialization is already in process. */
199 return VINF_SUCCESS;
200 }
201# else
202 dprintf(("vbglInit: starts\n"));
203# endif
204
205 rc = vbglR0InitCommon();
206 if (RT_SUCCESS(rc))
207 {
208 g_vbgldata.portVMMDev = portVMMDev;
209 g_vbgldata.pVMMDevMemory = pVMMDevMemory;
210 g_vbgldata.status = VbglStatusReady;
211
212 vbglR0QueryHostVersion();
213 *pfFeatures = g_vbgldata.hostVersion.features;
214 return VINF_SUCCESS;
215 }
216
217 g_vbgldata.status = VbglStatusNotInitialized;
218 return rc;
219}
220
221DECLR0VBGL(void) VbglR0TerminatePrimary(void)
222{
223 vbglR0TerminateCommon();
224}
225
226
227#else /* !VBGL_VBOXGUEST */
228
229DECLR0VBGL(int) VbglR0InitClient(void)
230{
231 int rc;
232
233 /** @todo r=bird: explain why we need to be doing this, please... */
234 if ( g_vbgldata.status == VbglStatusInitializing
235 || g_vbgldata.status == VbglStatusReady)
236 {
237 /* Initialization is already in process. */
238 return VINF_SUCCESS;
239 }
240
241 rc = vbglR0InitCommon();
242 if (RT_SUCCESS(rc))
243 {
244# ifdef VBGLDATA_USE_FAST_MUTEX
245 rc = RTSemFastMutexCreate(&g_vbgldata.hMtxIdcSetup);
246# else
247 rc = RTSemMutexCreate(&g_vbgldata.hMtxIdcSetup);
248# endif
249 if (RT_SUCCESS(rc))
250 {
251 /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
252 vbglR0QueryDriverInfo();
253
254# ifdef VBOX_WITH_HGCM
255 rc = VbglR0HGCMInit();
256# endif
257 if (RT_SUCCESS(rc))
258 return VINF_SUCCESS;
259
260# ifdef VBGLDATA_USE_FAST_MUTEX
261 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
262 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
263# else
264 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
265 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
266# endif
267 }
268 vbglR0TerminateCommon();
269 }
270
271 return rc;
272}
273
274DECLR0VBGL(void) VbglR0TerminateClient(void)
275{
276# ifdef VBOX_WITH_HGCM
277 VbglR0HGCMTerminate();
278# endif
279
280 /* driver open could fail, which does not prevent VbglInit from succeeding,
281 * close the driver only if it is opened */
282 VbglR0IdcClose(&g_vbgldata.IdcHandle);
283# ifdef VBGLDATA_USE_FAST_MUTEX
284 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
285 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
286# else
287 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
288 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
289# endif
290
291 /* note: do vbglR0TerminateCommon as a last step since it zeroez up the g_vbgldata
292 * conceptually, doing vbglR0TerminateCommon last is correct
293 * since this is the reverse order to how init is done */
294 vbglR0TerminateCommon();
295}
296
297
298int VBOXCALL vbglR0QueryIdcHandle(PVBGLIDCHANDLE *ppIdcHandle)
299{
300 if (g_vbgldata.status == VbglStatusReady)
301 { /* likely */ }
302 else
303 {
304 vbglR0QueryDriverInfo();
305 if (g_vbgldata.status != VbglStatusReady)
306 {
307 *ppIdcHandle = NULL;
308 return VERR_TRY_AGAIN;
309 }
310 }
311
312 *ppIdcHandle = &g_vbgldata.IdcHandle;
313 return VINF_SUCCESS;
314}
315
316
317DECLR0VBGL(int) VbglR0QueryHostFeatures(uint32_t *pfHostFeatures)
318{
319 if (g_vbgldata.status == VbglStatusReady)
320 *pfHostFeatures = g_vbgldata.hostVersion.features;
321 else
322 {
323 int rc = vbglR0QueryDriverInfo();
324 if (g_vbgldata.status != VbglStatusReady)
325 return rc;
326 *pfHostFeatures = g_vbgldata.hostVersion.features;
327 }
328
329 return VINF_SUCCESS;
330}
331
332#endif /* !VBGL_VBOXGUEST */
333
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