1 | /* $Id: VBoxGuestR0LibInit.cpp 72627 2018-06-20 13:53:28Z 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. */
|
---|
46 | VBGLDATA 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 | */
|
---|
56 | static 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 | */
|
---|
84 | static 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 | */
|
---|
141 | int 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 |
|
---|
158 | static 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 |
|
---|
178 | static void vbglR0TerminateCommon(void)
|
---|
179 | {
|
---|
180 | VbglR0PhysHeapTerminate();
|
---|
181 | g_vbgldata.status = VbglStatusNotInitialized;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #ifdef VBGL_VBOXGUEST
|
---|
185 |
|
---|
186 | DECLVBGL(int) VbglR0InitPrimary(RTIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory)
|
---|
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 | return VINF_SUCCESS;
|
---|
212 | }
|
---|
213 |
|
---|
214 | g_vbgldata.status = VbglStatusNotInitialized;
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 | DECLVBGL(void) VbglR0TerminatePrimary(void)
|
---|
219 | {
|
---|
220 | vbglR0TerminateCommon();
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | #else /* !VBGL_VBOXGUEST */
|
---|
225 |
|
---|
226 | DECLVBGL(int) VbglR0InitClient(void)
|
---|
227 | {
|
---|
228 | int rc;
|
---|
229 |
|
---|
230 | /** @todo r=bird: explain why we need to be doing this, please... */
|
---|
231 | if ( g_vbgldata.status == VbglStatusInitializing
|
---|
232 | || g_vbgldata.status == VbglStatusReady)
|
---|
233 | {
|
---|
234 | /* Initialization is already in process. */
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | }
|
---|
237 |
|
---|
238 | rc = vbglR0InitCommon();
|
---|
239 | if (RT_SUCCESS(rc))
|
---|
240 | {
|
---|
241 | # ifdef VBGLDATA_USE_FAST_MUTEX
|
---|
242 | rc = RTSemFastMutexCreate(&g_vbgldata.hMtxIdcSetup);
|
---|
243 | # else
|
---|
244 | rc = RTSemMutexCreate(&g_vbgldata.hMtxIdcSetup);
|
---|
245 | # endif
|
---|
246 | if (RT_SUCCESS(rc))
|
---|
247 | {
|
---|
248 | /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
|
---|
249 | vbglR0QueryDriverInfo();
|
---|
250 |
|
---|
251 | # ifdef VBOX_WITH_HGCM
|
---|
252 | rc = VbglR0HGCMInit();
|
---|
253 | # endif
|
---|
254 | if (RT_SUCCESS(rc))
|
---|
255 | return VINF_SUCCESS;
|
---|
256 |
|
---|
257 | # ifdef VBGLDATA_USE_FAST_MUTEX
|
---|
258 | RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
|
---|
259 | g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
|
---|
260 | # else
|
---|
261 | RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
|
---|
262 | g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
|
---|
263 | # endif
|
---|
264 | }
|
---|
265 | vbglR0TerminateCommon();
|
---|
266 | }
|
---|
267 |
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | DECLVBGL(void) VbglR0TerminateClient(void)
|
---|
272 | {
|
---|
273 | # ifdef VBOX_WITH_HGCM
|
---|
274 | VbglR0HGCMTerminate();
|
---|
275 | # endif
|
---|
276 |
|
---|
277 | /* driver open could fail, which does not prevent VbglInit from succeeding,
|
---|
278 | * close the driver only if it is opened */
|
---|
279 | VbglR0IdcClose(&g_vbgldata.IdcHandle);
|
---|
280 | # ifdef VBGLDATA_USE_FAST_MUTEX
|
---|
281 | RTSemFastMutexDestroy(g_vbgldata.hMtxIdcSetup);
|
---|
282 | g_vbgldata.hMtxIdcSetup = NIL_RTSEMFASTMUTEX;
|
---|
283 | # else
|
---|
284 | RTSemMutexDestroy(g_vbgldata.hMtxIdcSetup);
|
---|
285 | g_vbgldata.hMtxIdcSetup = NIL_RTSEMMUTEX;
|
---|
286 | # endif
|
---|
287 |
|
---|
288 | /* note: do vbglR0TerminateCommon as a last step since it zeroez up the g_vbgldata
|
---|
289 | * conceptually, doing vbglR0TerminateCommon last is correct
|
---|
290 | * since this is the reverse order to how init is done */
|
---|
291 | vbglR0TerminateCommon();
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | int VBOXCALL vbglR0QueryIdcHandle(PVBGLIDCHANDLE *ppIdcHandle)
|
---|
296 | {
|
---|
297 | if (g_vbgldata.status == VbglStatusReady)
|
---|
298 | { /* likely */ }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | vbglR0QueryDriverInfo();
|
---|
302 | if (g_vbgldata.status != VbglStatusReady)
|
---|
303 | {
|
---|
304 | *ppIdcHandle = NULL;
|
---|
305 | return VERR_TRY_AGAIN;
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | *ppIdcHandle = &g_vbgldata.IdcHandle;
|
---|
310 | return VINF_SUCCESS;
|
---|
311 | }
|
---|
312 |
|
---|
313 | #endif /* !VBGL_VBOXGUEST */
|
---|
314 |
|
---|