VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR0LibHGCM.cpp@ 68688

Last change on this file since 68688 was 68654, checked in by vboxsync, 8 years ago

VBoxGuestR0Lib: Function prefix and DECL macro cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: VBoxGuestR0LibHGCM.cpp 68654 2017-09-05 17:22:12Z vboxsync $ */
2/** @file
3 * VBoxGuestLib - Host-Guest Communication Manager, ring-0 client drivers.
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-2016 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/*********************************************************************************************************************************
31* Header Files *
32*********************************************************************************************************************************/
33#include "VBoxGuestR0LibInternal.h"
34
35#include <iprt/assert.h>
36#include <iprt/semaphore.h>
37#include <iprt/string.h>
38
39#ifdef VBGL_VBOXGUEST
40# error "This file shouldn't be part of the VBoxGuestR0LibBase library that is linked into VBoxGuest. It's client code."
41#endif
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47#define VBGL_HGCM_ASSERT_MSG AssertReleaseMsg
48
49/*********************************************************************************************************************************
50* Global Variables *
51*********************************************************************************************************************************/
52/**
53 * Fast heap for HGCM handles data.
54 * @{
55 */
56static RTSEMFASTMUTEX g_hMtxHGCMHandleData;
57static struct VBGLHGCMHANDLEDATA g_aHGCMHandleData[64];
58/** @} */
59
60
61/**
62 * Initializes the HGCM VBGL bits.
63 *
64 * @return VBox status code.
65 */
66DECLR0VBGL(int) VbglR0HGCMInit(void)
67{
68 AssertReturn(g_hMtxHGCMHandleData == NIL_RTSEMFASTMUTEX, VINF_ALREADY_INITIALIZED);
69 return RTSemFastMutexCreate(&g_hMtxHGCMHandleData);
70}
71
72/**
73 * Initializes the HGCM VBGL bits.
74 *
75 * @return VBox status code.
76 */
77DECLR0VBGL(int) VbglR0HGCMTerminate(void)
78{
79 RTSemFastMutexDestroy(g_hMtxHGCMHandleData);
80 g_hMtxHGCMHandleData = NIL_RTSEMFASTMUTEX;
81
82 return VINF_SUCCESS;
83}
84
85DECLINLINE(int) vbglR0HandleHeapEnter(void)
86{
87 int rc = RTSemFastMutexRequest(g_hMtxHGCMHandleData);
88
89 VBGL_HGCM_ASSERT_MSG(RT_SUCCESS(rc), ("Failed to request handle heap mutex, rc = %Rrc\n", rc));
90
91 return rc;
92}
93
94DECLINLINE(void) vbglR0HandleHeapLeave(void)
95{
96 RTSemFastMutexRelease(g_hMtxHGCMHandleData);
97}
98
99struct VBGLHGCMHANDLEDATA *vbglR0HGCMHandleAlloc(void)
100{
101 struct VBGLHGCMHANDLEDATA *p = NULL;
102 int rc = vbglR0HandleHeapEnter();
103 if (RT_SUCCESS(rc))
104 {
105 uint32_t i;
106
107 /* Simple linear search in array. This will be called not so often, only connect/disconnect. */
108 /** @todo bitmap for faster search and other obvious optimizations. */
109 for (i = 0; i < RT_ELEMENTS(g_aHGCMHandleData); i++)
110 {
111 if (!g_aHGCMHandleData[i].fAllocated)
112 {
113 p = &g_aHGCMHandleData[i];
114 p->fAllocated = 1;
115 break;
116 }
117 }
118
119 vbglR0HandleHeapLeave();
120
121 VBGL_HGCM_ASSERT_MSG(p != NULL, ("Not enough HGCM handles.\n"));
122 }
123 return p;
124}
125
126void vbglR0HGCMHandleFree(struct VBGLHGCMHANDLEDATA *pHandle)
127{
128 if (pHandle)
129 {
130 int rc = vbglR0HandleHeapEnter();
131 if (RT_SUCCESS(rc))
132 {
133 VBGL_HGCM_ASSERT_MSG(pHandle->fAllocated, ("Freeing not allocated handle.\n"));
134
135 RT_ZERO(*pHandle);
136 vbglR0HandleHeapLeave();
137 }
138 }
139}
140
141DECLR0VBGL(int) VbglR0HGCMConnect(VBGLHGCMHANDLE *pHandle, const char *pszServiceName, HGCMCLIENTID *pidClient)
142{
143 int rc;
144 if (pHandle && pszServiceName && pidClient)
145 {
146 struct VBGLHGCMHANDLEDATA *pHandleData = vbglR0HGCMHandleAlloc();
147 if (pHandleData)
148 {
149 rc = VbglR0IdcOpen(&pHandleData->IdcHandle,
150 VBGL_IOC_VERSION /*uReqVersion*/,
151 VBGL_IOC_VERSION & UINT32_C(0xffff0000) /*uMinVersion*/,
152 NULL /*puSessionVersion*/, NULL /*puDriverVersion*/, NULL /*uDriverRevision*/);
153 if (RT_SUCCESS(rc))
154 {
155 VBGLIOCHGCMCONNECT Info;
156 RT_ZERO(Info);
157 VBGLREQHDR_INIT(&Info.Hdr, HGCM_CONNECT);
158 Info.u.In.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
159 rc = RTStrCopy(Info.u.In.Loc.u.host.achName, sizeof(Info.u.In.Loc.u.host.achName), pszServiceName);
160 if (RT_SUCCESS(rc))
161 {
162 rc = VbglR0IdcCall(&pHandleData->IdcHandle, VBGL_IOCTL_HGCM_CONNECT, &Info.Hdr, sizeof(Info));
163 if (RT_SUCCESS(rc))
164 {
165 *pidClient = Info.u.Out.idClient;
166 *pHandle = pHandleData;
167 return rc;
168 }
169 }
170
171 VbglR0IdcClose(&pHandleData->IdcHandle);
172 }
173
174 vbglR0HGCMHandleFree(pHandleData);
175 }
176 else
177 rc = VERR_NO_MEMORY;
178 }
179 else
180 rc = VERR_INVALID_PARAMETER;
181 return rc;
182}
183
184DECLR0VBGL(int) VbglR0HGCMDisconnect(VBGLHGCMHANDLE handle, HGCMCLIENTID idClient)
185{
186 int rc;
187 VBGLIOCHGCMDISCONNECT Info;
188
189 RT_ZERO(Info);
190 VBGLREQHDR_INIT(&Info.Hdr, HGCM_DISCONNECT);
191 Info.u.In.idClient = idClient;
192 rc = VbglR0IdcCall(&handle->IdcHandle, VBGL_IOCTL_HGCM_DISCONNECT, &Info.Hdr, sizeof(Info));
193
194 VbglR0IdcClose(&handle->IdcHandle);
195
196 vbglR0HGCMHandleFree(handle);
197
198 return rc;
199}
200
201DECLR0VBGL(int) VbglR0HGCMCallRaw(VBGLHGCMHANDLE handle, PVBGLIOCHGCMCALL pData, uint32_t cbData)
202{
203 VBGL_HGCM_ASSERT_MSG(cbData >= sizeof(VBGLIOCHGCMCALL) + pData->cParms * sizeof(HGCMFunctionParameter),
204 ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms,
205 sizeof(VBGLIOCHGCMCALL) + pData->cParms * sizeof(VBGLIOCHGCMCALL)));
206
207 return VbglR0IdcCallRaw(&handle->IdcHandle, VBGL_IOCTL_HGCM_CALL(cbData), &pData->Hdr, cbData);
208}
209
210DECLR0VBGL(int) VbglR0HGCMCall(VBGLHGCMHANDLE handle, PVBGLIOCHGCMCALL pData, uint32_t cbData)
211{
212 int rc = VbglR0HGCMCallRaw(handle, pData, cbData);
213 if (RT_SUCCESS(rc))
214 rc = pData->Hdr.rc;
215 return rc;
216}
217
218DECLR0VBGL(int) VbglR0HGCMCallUserDataRaw(VBGLHGCMHANDLE handle, PVBGLIOCHGCMCALL pData, uint32_t cbData)
219{
220 VBGL_HGCM_ASSERT_MSG(cbData >= sizeof(VBGLIOCHGCMCALL) + pData->cParms * sizeof(HGCMFunctionParameter),
221 ("cbData = %d, cParms = %d (calculated size %d)\n", cbData, pData->cParms,
222 sizeof(VBGLIOCHGCMCALL) + pData->cParms * sizeof(VBGLIOCHGCMCALL)));
223
224 return VbglR0IdcCallRaw(&handle->IdcHandle, VBGL_IOCTL_HGCM_CALL_WITH_USER_DATA(cbData), &pData->Hdr, cbData);
225}
226
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette