1 | /* $Revision: 40483 $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLib - Host-Guest Communication Manager.
|
---|
4 | *
|
---|
5 | * These public functions can be only used by other drivers. They all
|
---|
6 | * do an IOCTL to VBoxGuest via IDC.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | *
|
---|
20 | * The contents of this file may alternatively be used under the terms
|
---|
21 | * of the Common Development and Distribution License Version 1.0
|
---|
22 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | * CDDL are applicable instead of those of the GPL.
|
---|
25 | *
|
---|
26 | * You may elect to license modified versions of this file under the
|
---|
27 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /* Entire file is ifdef'ed with !VBGL_VBOXGUEST */
|
---|
31 | #ifndef VBGL_VBOXGUEST
|
---|
32 |
|
---|
33 | #include "VBGLInternal.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #define VBGL_HGCM_ASSERTMsg AssertReleaseMsg
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Initializes the HGCM VBGL bits.
|
---|
43 | *
|
---|
44 | * @return VBox status code.
|
---|
45 | */
|
---|
46 | int vbglR0HGCMInit (void)
|
---|
47 | {
|
---|
48 | return RTSemFastMutexCreate(&g_vbgldata.mutexHGCMHandle);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Initializes the HGCM VBGL bits.
|
---|
53 | *
|
---|
54 | * @return VBox status code.
|
---|
55 | */
|
---|
56 | int vbglR0HGCMTerminate (void)
|
---|
57 | {
|
---|
58 | RTSemFastMutexDestroy(g_vbgldata.mutexHGCMHandle);
|
---|
59 | g_vbgldata.mutexHGCMHandle = NIL_RTSEMFASTMUTEX;
|
---|
60 |
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 | DECLINLINE(int) vbglHandleHeapEnter (void)
|
---|
65 | {
|
---|
66 | int rc = RTSemFastMutexRequest(g_vbgldata.mutexHGCMHandle);
|
---|
67 |
|
---|
68 | VBGL_HGCM_ASSERTMsg(RT_SUCCESS(rc),
|
---|
69 | ("Failed to request handle heap mutex, rc = %Rrc\n", rc));
|
---|
70 |
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 | DECLINLINE(void) vbglHandleHeapLeave (void)
|
---|
75 | {
|
---|
76 | RTSemFastMutexRelease(g_vbgldata.mutexHGCMHandle);
|
---|
77 | }
|
---|
78 |
|
---|
79 | struct VBGLHGCMHANDLEDATA *vbglHGCMHandleAlloc (void)
|
---|
80 | {
|
---|
81 | struct VBGLHGCMHANDLEDATA *p;
|
---|
82 | int rc = vbglHandleHeapEnter ();
|
---|
83 | uint32_t i;
|
---|
84 |
|
---|
85 | if (RT_FAILURE (rc))
|
---|
86 | return NULL;
|
---|
87 |
|
---|
88 | p = NULL;
|
---|
89 |
|
---|
90 | /** Simple linear search in array. This will be called not so often, only connect/disconnect.
|
---|
91 | * @todo bitmap for faster search and other obvious optimizations.
|
---|
92 | */
|
---|
93 |
|
---|
94 | for (i = 0; i < RT_ELEMENTS(g_vbgldata.aHGCMHandleData); i++)
|
---|
95 | {
|
---|
96 | if (!g_vbgldata.aHGCMHandleData[i].fAllocated)
|
---|
97 | {
|
---|
98 | p = &g_vbgldata.aHGCMHandleData[i];
|
---|
99 | p->fAllocated = 1;
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | vbglHandleHeapLeave ();
|
---|
105 |
|
---|
106 | VBGL_HGCM_ASSERTMsg(p != NULL,
|
---|
107 | ("Not enough HGCM handles.\n"));
|
---|
108 |
|
---|
109 | return p;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void vbglHGCMHandleFree (struct VBGLHGCMHANDLEDATA *pHandle)
|
---|
113 | {
|
---|
114 | int rc;
|
---|
115 |
|
---|
116 | if (!pHandle)
|
---|
117 | return;
|
---|
118 |
|
---|
119 | rc = vbglHandleHeapEnter ();
|
---|
120 |
|
---|
121 | if (RT_FAILURE (rc))
|
---|
122 | return;
|
---|
123 |
|
---|
124 | VBGL_HGCM_ASSERTMsg(pHandle->fAllocated,
|
---|
125 | ("Freeing not allocated handle.\n"));
|
---|
126 |
|
---|
127 | memset(pHandle, 0, sizeof (struct VBGLHGCMHANDLEDATA));
|
---|
128 | vbglHandleHeapLeave ();
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | DECLVBGL(int) VbglHGCMConnect (VBGLHGCMHANDLE *pHandle, VBoxGuestHGCMConnectInfo *pData)
|
---|
133 | {
|
---|
134 | int rc;
|
---|
135 | struct VBGLHGCMHANDLEDATA *pHandleData;
|
---|
136 |
|
---|
137 | if (!pHandle || !pData)
|
---|
138 | return VERR_INVALID_PARAMETER;
|
---|
139 |
|
---|
140 | pHandleData = vbglHGCMHandleAlloc ();
|
---|
141 |
|
---|
142 | rc = VINF_SUCCESS;
|
---|
143 |
|
---|
144 | if (!pHandleData)
|
---|
145 | {
|
---|
146 | rc = VERR_NO_MEMORY;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | rc = vbglDriverOpen (&pHandleData->driver);
|
---|
151 |
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | {
|
---|
154 | rc = vbglDriverIOCtl (&pHandleData->driver, VBOXGUEST_IOCTL_HGCM_CONNECT, pData, sizeof (*pData));
|
---|
155 |
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | *pHandle = pHandleData;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | vbglDriverClose (&pHandleData->driver);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (RT_FAILURE(rc))
|
---|
167 | {
|
---|
168 | vbglHGCMHandleFree (pHandleData);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | DECLVBGL(int) VbglHGCMDisconnect (VBGLHGCMHANDLE handle, VBoxGuestHGCMDisconnectInfo *pData)
|
---|
176 | {
|
---|
177 | int rc = VINF_SUCCESS;
|
---|
178 |
|
---|
179 | rc = vbglDriverIOCtl (&handle->driver, VBOXGUEST_IOCTL_HGCM_DISCONNECT, pData, sizeof (*pData));
|
---|
180 |
|
---|
181 | vbglDriverClose (&handle->driver);
|
---|
182 |
|
---|
183 | vbglHGCMHandleFree (handle);
|
---|
184 |
|
---|
185 | return rc;
|
---|
186 | }
|
---|
187 |
|
---|
188 | DECLVBGL(int) VbglHGCMCall (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData)
|
---|
189 | {
|
---|
190 | int rc = VINF_SUCCESS;
|
---|
191 |
|
---|
192 | VBGL_HGCM_ASSERTMsg(cbData >= sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (HGCMFunctionParameter),
|
---|
193 | ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms, sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (VBoxGuestHGCMCallInfo)));
|
---|
194 |
|
---|
195 | rc = vbglDriverIOCtl (&handle->driver, VBOXGUEST_IOCTL_HGCM_CALL(cbData), pData, cbData);
|
---|
196 |
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 | DECLVBGL(int) VbglHGCMCallUserData (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData)
|
---|
201 | {
|
---|
202 | int rc = VINF_SUCCESS;
|
---|
203 |
|
---|
204 | VBGL_HGCM_ASSERTMsg(cbData >= sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (HGCMFunctionParameter),
|
---|
205 | ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms, sizeof (VBoxGuestHGCMCallInfo) + pData->cParms * sizeof (VBoxGuestHGCMCallInfo)));
|
---|
206 |
|
---|
207 | rc = vbglDriverIOCtl (&handle->driver, VBOXGUEST_IOCTL_HGCM_CALL_USERDATA(cbData), pData, cbData);
|
---|
208 |
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | DECLVBGL(int) VbglHGCMCallTimed (VBGLHGCMHANDLE handle,
|
---|
214 | VBoxGuestHGCMCallInfoTimed *pData, uint32_t cbData)
|
---|
215 | {
|
---|
216 | int rc = VINF_SUCCESS;
|
---|
217 |
|
---|
218 | uint32_t cbExpected = sizeof (VBoxGuestHGCMCallInfoTimed)
|
---|
219 | + pData->info.cParms * sizeof (HGCMFunctionParameter);
|
---|
220 | VBGL_HGCM_ASSERTMsg(cbData >= cbExpected,
|
---|
221 | ("cbData = %d, cParms = %d (calculated size %d)\n",
|
---|
222 | cbData, pData->info.cParms, cbExpected));
|
---|
223 |
|
---|
224 | rc = vbglDriverIOCtl (&handle->driver, VBOXGUEST_IOCTL_HGCM_CALL_TIMED(cbData),
|
---|
225 | pData, cbData);
|
---|
226 |
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 | #endif /* !VBGL_VBOXGUEST */
|
---|
231 |
|
---|