VirtualBox

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

Last change on this file since 100267 was 100267, checked in by vboxsync, 12 months ago

Additions: Make the R0 physical heap configurable to allow for allocations >= 4GiB if supported by the VBox device (the MMIO request path is available), add support for the MMIO request path required for ARM, bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: VBoxGuestR0LibInit.cpp 100267 2023-06-23 14:57:53Z vboxsync $ */
2/** @file
3 * VBoxGuestLibR0 - Library initialization.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
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.pMmioReq = PortInfo.u.Out.pMmioReq;
118
119 rc = VbglR0PhysHeapInit(g_vbgldata.pMmioReq == NULL /*fAlloc32BitAddr*/);
120 if (RT_SUCCESS(rc))
121 {
122 g_vbgldata.status = VbglStatusReady;
123 vbglR0QueryHostVersion();
124 }
125 else
126 {
127 LogRel(("vbglR0QueryDriverInfo: VbglR0PhysHeapInit() -> %Rrc\n", rc));
128 g_vbgldata.status = VbglStatusNotInitialized;
129 }
130 }
131 }
132
133 dprintf(("vbglQueryDriverInfo rc = %Rrc\n", rc));
134 }
135
136# ifdef VBGLDATA_USE_FAST_MUTEX
137 RTSemFastMutexRelease(g_vbgldata.hMtxIdcSetup);
138# else
139 RTSemMutexRelease(g_vbgldata.hMtxIdcSetup);
140# endif
141 }
142 return rc;
143}
144#endif /* !VBGL_VBOXGUEST */
145
146/**
147 * Checks if VBGL has been initialized.
148 *
149 * The client library, this will lazily complete the initialization.
150 *
151 * @return VINF_SUCCESS or VERR_VBGL_NOT_INITIALIZED.
152 */
153int vbglR0Enter(void)
154{
155 if (g_vbgldata.status == VbglStatusReady)
156 return VINF_SUCCESS;
157
158#ifndef VBGL_VBOXGUEST
159 if (g_vbgldata.status == VbglStatusInitializing)
160 {
161 vbglR0QueryDriverInfo();
162 if (g_vbgldata.status == VbglStatusReady)
163 return VINF_SUCCESS;
164 }
165#endif
166 return VERR_VBGL_NOT_INITIALIZED;
167}
168
169
170static int vbglR0InitCommon(void)
171{
172 RT_ZERO(g_vbgldata);
173 g_vbgldata.status = VbglStatusInitializing;
174 return VINF_SUCCESS;
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, uintptr_t volatile *pMmioReq, 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.pMmioReq = pMmioReq;
209
210 rc = VbglR0PhysHeapInit(pMmioReq == NULL /*fAlloc32BitAddr*/);
211 if (RT_SUCCESS(rc))
212 {
213 g_vbgldata.status = VbglStatusReady;
214
215 vbglR0QueryHostVersion();
216 *pfFeatures = g_vbgldata.hostVersion.features;
217 return VINF_SUCCESS;
218 }
219 else
220 {
221 LogRel(("VbglR0InitPrimary: VbglR0PhysHeapInit() -> %Rrc\n", rc));
222 g_vbgldata.status = VbglStatusNotInitialized;
223 }
224 }
225
226 g_vbgldata.status = VbglStatusNotInitialized;
227 return rc;
228}
229
230DECLR0VBGL(void) VbglR0TerminatePrimary(void)
231{
232 vbglR0TerminateCommon();
233}
234
235
236#else /* !VBGL_VBOXGUEST */
237
238DECLR0VBGL(int) VbglR0InitClient(void)
239{
240 int rc;
241
242 /** @todo r=bird: explain why we need to be doing this, please... */
243 if ( g_vbgldata.status == VbglStatusInitializing
244 || g_vbgldata.status == VbglStatusReady)
245 {
246 /* Initialization is already in process. */
247 return VINF_SUCCESS;
248 }
249
250 rc = vbglR0InitCommon();
251 if (RT_SUCCESS(rc))
252 {
253# ifdef VBGLDATA_USE_FAST_MUTEX
254 rc = RTSemFastMutexCreate(&g_vbgldata.hMtxIdcSetup);
255# else
256 rc = RTSemMutexCreate(&g_vbgldata.hMtxIdcSetup);
257# endif
258 if (RT_SUCCESS(rc))
259 {
260 /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
261 vbglR0QueryDriverInfo();
262
263# ifdef VBOX_WITH_HGCM
264 rc = VbglR0HGCMInit();
265# endif
266 if (RT_SUCCESS(rc))
267 return VINF_SUCCESS;
268
269# ifdef VBGLDATA_USE_FAST_MUTEX
270 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
271 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
272# else
273 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
274 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
275# endif
276 }
277 vbglR0TerminateCommon();
278 }
279
280 return rc;
281}
282
283DECLR0VBGL(void) VbglR0TerminateClient(void)
284{
285# ifdef VBOX_WITH_HGCM
286 VbglR0HGCMTerminate();
287# endif
288
289 /* driver open could fail, which does not prevent VbglInit from succeeding,
290 * close the driver only if it is opened */
291 VbglR0IdcClose(&g_vbgldata.IdcHandle);
292# ifdef VBGLDATA_USE_FAST_MUTEX
293 RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
294 g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
295# else
296 RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
297 g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
298# endif
299
300 /* note: do vbglR0TerminateCommon as a last step since it zeroez up the g_vbgldata
301 * conceptually, doing vbglR0TerminateCommon last is correct
302 * since this is the reverse order to how init is done */
303 vbglR0TerminateCommon();
304}
305
306
307int VBOXCALL vbglR0QueryIdcHandle(PVBGLIDCHANDLE *ppIdcHandle)
308{
309 if (g_vbgldata.status == VbglStatusReady)
310 { /* likely */ }
311 else
312 {
313 vbglR0QueryDriverInfo();
314 if (g_vbgldata.status != VbglStatusReady)
315 {
316 *ppIdcHandle = NULL;
317 return VERR_TRY_AGAIN;
318 }
319 }
320
321 *ppIdcHandle = &g_vbgldata.IdcHandle;
322 return VINF_SUCCESS;
323}
324
325
326DECLR0VBGL(int) VbglR0QueryHostFeatures(uint32_t *pfHostFeatures)
327{
328 if (g_vbgldata.status == VbglStatusReady)
329 *pfHostFeatures = g_vbgldata.hostVersion.features;
330 else
331 {
332 int rc = vbglR0QueryDriverInfo();
333 if (g_vbgldata.status != VbglStatusReady)
334 return rc;
335 *pfHostFeatures = g_vbgldata.hostVersion.features;
336 }
337
338 return VINF_SUCCESS;
339}
340
341#endif /* !VBGL_VBOXGUEST */
342
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use