VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/HGCM.cpp@ 35263

Last change on this file since 35263 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Revision: 28800 $ */
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 */
46int 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 */
56int vbglR0HGCMTerminate (void)
57{
58 RTSemFastMutexDestroy(g_vbgldata.mutexHGCMHandle);
59 g_vbgldata.mutexHGCMHandle = NIL_RTSEMFASTMUTEX;
60
61 return VINF_SUCCESS;
62}
63
64DECLINLINE(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
74DECLINLINE(void) vbglHandleHeapLeave (void)
75{
76 RTSemFastMutexRelease(g_vbgldata.mutexHGCMHandle);
77}
78
79struct 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
112void 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
132DECLVBGL(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
175DECLVBGL(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
188DECLVBGL(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
200DECLVBGL(int) VbglHGCMCallTimed (VBGLHGCMHANDLE handle,
201 VBoxGuestHGCMCallInfoTimed *pData, uint32_t cbData)
202{
203 int rc = VINF_SUCCESS;
204
205 uint32_t cbExpected = sizeof (VBoxGuestHGCMCallInfoTimed)
206 + pData->info.cParms * sizeof (HGCMFunctionParameter);
207 VBGL_HGCM_ASSERTMsg(cbData >= cbExpected,
208 ("cbData = %d, cParms = %d (calculated size %d)\n",
209 cbData, pData->info.cParms, cbExpected));
210
211 rc = vbglDriverIOCtl (&handle->driver, VBOXGUEST_IOCTL_HGCM_CALL_TIMED(cbData),
212 pData, cbData);
213
214 return rc;
215}
216
217#endif /* !VBGL_VBOXGUEST */
218
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use