VirtualBox

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

Last change on this file since 75588 was 75588, checked in by vboxsync, 6 years ago

VBoxGuest: Make use of new IRQ ACK port when present.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: VBoxGuestR0LibInit.cpp 75588 2018-11-19 18:07:03Z vboxsync $ */
2/** @file
3 * VBoxGuestLibR0 - Library initialization.
4 */
5
6/*
7 * Copyright (C) 2006-2018 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
41
42/*********************************************************************************************************************************
43* Global Variables *
44*********************************************************************************************************************************/
45/** The global VBGL instance data. */
46VBGLDATA g_vbgldata;
47
48
49/**
50 * Used by vbglR0QueryDriverInfo and VbglInit to try get the host feature mask
51 * and version information (g_vbgldata::hostVersion).
52 *
53 * This was first implemented by the host in 3.1 and we quietly ignore failures
54 * for that reason.
55 */
56static void vbglR0QueryHostVersion(void)
57{
58 VMMDevReqHostVersion *pReq;
59 int rc = VbglR0GRAlloc((VMMDevRequestHeader **) &pReq, sizeof (*pReq), VMMDevReq_GetHostVersion);
60 if (RT_SUCCESS(rc))
61 {
62 rc = VbglR0GRPerform(&pReq->header);
63 if (RT_SUCCESS(rc))
64 {
65 g_vbgldata.hostVersion = *pReq;
66 Log(("vbglR0QueryHostVersion: %u.%u.%ur%u %#x\n",
67 pReq->major, pReq->minor, pReq->build, pReq->revision, pReq->features));
68 }
69
70 VbglR0GRFree(&pReq->header);
71 }
72}
73
74
75#ifndef VBGL_VBOXGUEST
76/**
77 * The guest library uses lazy initialization for VMMDev port and memory,
78 * because these values are provided by the VBoxGuest driver and it might
79 * be loaded later than other drivers.
80 *
81 * The VbglEnter checks the current library status, tries to retrieve these
82 * values and fails if they are unavailable.
83 */
84static void vbglR0QueryDriverInfo(void)
85{
86# ifdef VBGLDATA_USE_FAST_MUTEX
87 int rc = RTSemFastMutexRequest(g_vbgldata.hMtxIdcSetup);
88# else
89 int rc = RTSemMutexRequest(g_vbgldata.hMtxIdcSetup, RT_INDEFINITE_WAIT);
90# endif
91 if (RT_SUCCESS(rc))
92 {
93 if (g_vbgldata.status == VbglStatusReady)
94 { /* likely */ }
95 else
96 {
97 rc = VbglR0IdcOpen(&g_vbgldata.IdcHandle,
98 VBGL_IOC_VERSION /*uReqVersion*/,
99 VBGL_IOC_VERSION & UINT32_C(0xffff0000) /*uMinVersion*/,
100 NULL /*puSessionVersion*/, NULL /*puDriverVersion*/, NULL /*puDriverRevision*/);
101 if (RT_SUCCESS(rc))
102 {
103 /*
104 * Try query the port info.
105 */
106 VBGLIOCGETVMMDEVIOINFO PortInfo;
107 RT_ZERO(PortInfo);
108 VBGLREQHDR_INIT(&PortInfo.Hdr, GET_VMMDEV_IO_INFO);
109 rc = VbglR0IdcCall(&g_vbgldata.IdcHandle, VBGL_IOCTL_GET_VMMDEV_IO_INFO, &PortInfo.Hdr, sizeof(PortInfo));
110 if (RT_SUCCESS(rc))
111 {
112 dprintf(("Port I/O = 0x%04x, MMIO = %p\n", PortInfo.u.Out.IoPort, PortInfo.u.Out.pvVmmDevMapping));
113
114 g_vbgldata.portVMMDev = PortInfo.u.Out.IoPort;
115 g_vbgldata.pVMMDevMemory = (VMMDevMemory *)PortInfo.u.Out.pvVmmDevMapping;
116 g_vbgldata.status = VbglStatusReady;
117
118 vbglR0QueryHostVersion();
119 }
120 }
121
122 dprintf(("vbglQueryDriverInfo rc = %Rrc\n", rc));
123 }
124
125# ifdef VBGLDATA_USE_FAST_MUTEX
126 RTSemFastMutexRelease(g_vbgldata.hMtxIdcSetup);
127# else
128 RTSemMutexRelease(g_vbgldata.hMtxIdcSetup);
129# endif
130 }
131}
132#endif /* !VBGL_VBOXGUEST */
133
134/**
135 * Checks if VBGL has been initialized.
136 *
137 * The client library, this will lazily complete the initialization.
138 *
139 * @return VINF_SUCCESS or VERR_VBGL_NOT_INITIALIZED.
140 */
141int vbglR0Enter(void)
142{
143 if (g_vbgldata.status == VbglStatusReady)
144 return VINF_SUCCESS;
145
146#ifndef VBGL_VBOXGUEST
147 if (g_vbgldata.status == VbglStatusInitializing)
148 {
149 vbglR0QueryDriverInfo();
150 if (g_vbgldata.status == VbglStatusReady)
151 return VINF_SUCCESS;
152 }
153#endif
154 return VERR_VBGL_NOT_INITIALIZED;
155}
156
157
158static int vbglR0InitCommon(void)
159{
160 int rc;
161
162 RT_ZERO(g_vbgldata);
163 g_vbgldata.status = VbglStatusInitializing;
164
165 rc = VbglR0PhysHeapInit();
166 if (RT_SUCCESS(rc))
167 {
168 dprintf(("vbglR0InitCommon: returns rc = %d\n", rc));
169 return rc;
170 }
171
172 LogRel(("vbglR0InitCommon: VbglR0PhysHeapInit failed: rc=%Rrc\n", rc));
173 g_vbgldata.status = VbglStatusNotInitialized;
174 return rc;
175}
176
177
178static void vbglR0TerminateCommon(void)
179{
180 VbglR0PhysHeapTerminate();
181 g_vbgldata.status = VbglStatusNotInitialized;
182}
183
184#ifdef VBGL_VBOXGUEST
185
186DECLR0VBGL(int) VbglR0InitPrimary(RTIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory, uint32_t *pfFeatures)
187{
188 int rc;
189
190# ifdef RT_OS_WINDOWS /** @todo r=bird: this doesn't make sense. Is there something special going on on windows? */
191 dprintf(("vbglInit: starts g_vbgldata.status %d\n", g_vbgldata.status));
192
193 if ( g_vbgldata.status == VbglStatusInitializing
194 || g_vbgldata.status == VbglStatusReady)
195 {
196 /* Initialization is already in process. */
197 return VINF_SUCCESS;
198 }
199# else
200 dprintf(("vbglInit: starts\n"));
201# endif
202
203 rc = vbglR0InitCommon();
204 if (RT_SUCCESS(rc))
205 {
206 g_vbgldata.portVMMDev = portVMMDev;
207 g_vbgldata.pVMMDevMemory = pVMMDevMemory;
208 g_vbgldata.status = VbglStatusReady;
209
210 vbglR0QueryHostVersion();
211 *pfFeatures = g_vbgldata.hostVersion.features;
212 return VINF_SUCCESS;
213 }
214
215 g_vbgldata.status = VbglStatusNotInitialized;
216 return rc;
217}
218
219DECLR0VBGL(void) VbglR0TerminatePrimary(void)
220{
221 vbglR0TerminateCommon();
222}
223
224
225#else /* !VBGL_VBOXGUEST */
226
227DECLR0VBGL(int) VbglR0InitClient(void)
228{
229 int rc;
230
231 /** @todo r=bird: explain why we need to be doing this, please... */
232 if ( g_vbgldata.status == VbglStatusInitializing
233 || g_vbgldata.status == VbglStatusReady)
234 {
235 /* Initialization is already in process. */
236 return VINF_SUCCESS;
237 }
238
239 rc = vbglR0InitCommon();
240 if (RT_SUCCESS(rc))
241 {
242# ifdef VBGLDATA_USE_FAST_MUTEX
243 rc = RTSemFastMutexCreate(&g_vbgldata.hMtxIdcSetup);
244# else
245 rc = RTSemMutexCreate(&g_vbgldata.hMtxIdcSetup);
246# endif
247 if (RT_SUCCESS(rc))
248 {
249 /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
250 vbglR0QueryDriverInfo();
251
252# ifdef VBOX_WITH_HGCM
253 rc = VbglR0HGCMInit();
254# endif
255 if (RT_SUCCESS(rc))
256 return VINF_SUCCESS;
257
258# ifdef VBGLDATA_USE_FAST_MUTEX
259 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
260 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
261# else
262 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
263 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
264# endif
265 }
266 vbglR0TerminateCommon();
267 }
268
269 return rc;
270}
271
272DECLR0VBGL(void) VbglR0TerminateClient(void)
273{
274# ifdef VBOX_WITH_HGCM
275 VbglR0HGCMTerminate();
276# endif
277
278 /* driver open could fail, which does not prevent VbglInit from succeeding,
279 * close the driver only if it is opened */
280 VbglR0IdcClose(&g_vbgldata.IdcHandle);
281# ifdef VBGLDATA_USE_FAST_MUTEX
282 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
283 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
284# else
285 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
286 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
287# endif
288
289 /* note: do vbglR0TerminateCommon as a last step since it zeroez up the g_vbgldata
290 * conceptually, doing vbglR0TerminateCommon last is correct
291 * since this is the reverse order to how init is done */
292 vbglR0TerminateCommon();
293}
294
295
296int VBOXCALL vbglR0QueryIdcHandle(PVBGLIDCHANDLE *ppIdcHandle)
297{
298 if (g_vbgldata.status == VbglStatusReady)
299 { /* likely */ }
300 else
301 {
302 vbglR0QueryDriverInfo();
303 if (g_vbgldata.status != VbglStatusReady)
304 {
305 *ppIdcHandle = NULL;
306 return VERR_TRY_AGAIN;
307 }
308 }
309
310 *ppIdcHandle = &g_vbgldata.IdcHandle;
311 return VINF_SUCCESS;
312}
313
314#endif /* !VBGL_VBOXGUEST */
315
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