VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPR0IdcClient.c

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: SUPR0IdcClient.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox Support Driver - IDC Client Lib, Core.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "SUPR0IdcClientInternal.h"
42#include <iprt/errcore.h>
43#include <iprt/asm.h>
44
45
46/*********************************************************************************************************************************
47* Global Variables *
48*********************************************************************************************************************************/
49static PSUPDRVIDCHANDLE volatile g_pMainHandle = NULL;
50
51
52/**
53 * Opens the IDC interface of the support driver.
54 *
55 * This will perform basic version negotiations and fail if the
56 * minimum requirements aren't met.
57 *
58 * @returns VBox status code.
59 * @param pHandle The handle structure (output).
60 * @param uReqVersion The requested version. Pass 0 for default.
61 * @param uMinVersion The minimum required version. Pass 0 for default.
62 * @param puSessionVersion Where to store the session version. Optional.
63 * @param puDriverVersion Where to store the session version. Optional.
64 * @param puDriverRevision Where to store the SVN revision of the driver. Optional.
65 */
66SUPR0DECL(int) SUPR0IdcOpen(PSUPDRVIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion,
67 uint32_t *puSessionVersion, uint32_t *puDriverVersion, uint32_t *puDriverRevision)
68{
69 unsigned uDefaultMinVersion;
70 SUPDRVIDCREQCONNECT Req;
71 int rc;
72
73 /*
74 * Validate and set failure return values.
75 */
76 AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
77 pHandle->s.pSession = NULL;
78
79 AssertPtrNullReturn(puSessionVersion, VERR_INVALID_POINTER);
80 if (puSessionVersion)
81 *puSessionVersion = 0;
82
83 AssertPtrNullReturn(puDriverVersion, VERR_INVALID_POINTER);
84 if (puDriverVersion)
85 *puDriverVersion = 0;
86
87 AssertPtrNullReturn(puDriverRevision, VERR_INVALID_POINTER);
88 if (puDriverRevision)
89 *puDriverRevision = 0;
90
91 AssertReturn(!uMinVersion || (uMinVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
92 AssertReturn(!uReqVersion || (uReqVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
93
94 /*
95 * Handle default version input and enforce minimum requirements made
96 * by this library.
97 *
98 * The clients will pass defaults (0), and only in the case that some
99 * special API feature was just added will they set an actual version.
100 * So, this is the place where can easily enforce a minimum IDC version
101 * on bugs and similar. It corresponds a bit to what SUPR3Init is
102 * responsible for.
103 */
104 uDefaultMinVersion = SUPDRV_IDC_VERSION & UINT32_C(0xffff0000);
105 if (!uMinVersion || uMinVersion < uDefaultMinVersion)
106 uMinVersion = uDefaultMinVersion;
107 if (!uReqVersion || uReqVersion < uDefaultMinVersion)
108 uReqVersion = uDefaultMinVersion;
109
110 /*
111 * Setup the connect request packet and call the OS specific function.
112 */
113 Req.Hdr.cb = sizeof(Req);
114 Req.Hdr.rc = VERR_WRONG_ORDER;
115 Req.Hdr.pSession = NULL;
116 Req.u.In.u32MagicCookie = SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE;
117 Req.u.In.uMinVersion = uMinVersion;
118 Req.u.In.uReqVersion = uReqVersion;
119 rc = supR0IdcNativeOpen(pHandle, &Req);
120 if (RT_SUCCESS(rc))
121 {
122 pHandle->s.pSession = Req.u.Out.pSession;
123 if (puSessionVersion)
124 *puSessionVersion = Req.u.Out.uSessionVersion;
125 if (puDriverVersion)
126 *puDriverVersion = Req.u.Out.uDriverVersion;
127 if (puDriverRevision)
128 *puDriverRevision = Req.u.Out.uDriverRevision;
129
130 /*
131 * We don't really trust anyone, make sure the returned
132 * session and version values actually makes sense.
133 */
134 if ( RT_VALID_PTR(Req.u.Out.pSession)
135 && Req.u.Out.uSessionVersion >= uMinVersion
136 && (Req.u.Out.uSessionVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)))
137 {
138 ASMAtomicCmpXchgPtr(&g_pMainHandle, pHandle, NULL);
139 return rc;
140 }
141
142 AssertMsgFailed(("pSession=%p uSessionVersion=0x%x (r%u)\n", Req.u.Out.pSession, Req.u.Out.uSessionVersion, Req.u.Out.uDriverRevision));
143 rc = VERR_VERSION_MISMATCH;
144 SUPR0IdcClose(pHandle);
145 }
146
147 return rc;
148}
149
150
151/**
152 * Closes a IDC connection established by SUPR0IdcOpen.
153 *
154 * @returns VBox status code.
155 * @param pHandle The IDC handle.
156 */
157SUPR0DECL(int) SUPR0IdcClose(PSUPDRVIDCHANDLE pHandle)
158{
159 SUPDRVIDCREQHDR Req;
160 int rc;
161
162 /*
163 * Catch closed handles and check that the session is valid.
164 */
165 AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
166 if (!pHandle->s.pSession)
167 return VERR_INVALID_HANDLE;
168 AssertPtrReturn(pHandle->s.pSession, VERR_INVALID_HANDLE);
169
170 /*
171 * Create the request and hand it to the OS specific code.
172 */
173 Req.cb = sizeof(Req);
174 Req.rc = VERR_WRONG_ORDER;
175 Req.pSession = pHandle->s.pSession;
176 rc = supR0IdcNativeClose(pHandle, &Req);
177 if (RT_SUCCESS(rc))
178 {
179 pHandle->s.pSession = NULL;
180 ASMAtomicCmpXchgPtr(&g_pMainHandle, NULL, pHandle);
181 }
182 return rc;
183}
184
185
186/**
187 * Get the SUPDRV session for the IDC connection.
188 *
189 * This is for use with SUPDRV and component APIs that requires a valid
190 * session handle.
191 *
192 * @returns The session handle on success, NULL if the IDC handle is invalid.
193 *
194 * @param pHandle The IDC handle.
195 */
196SUPR0DECL(PSUPDRVSESSION) SUPR0IdcGetSession(PSUPDRVIDCHANDLE pHandle)
197{
198 PSUPDRVSESSION pSession;
199 AssertPtrReturn(pHandle, NULL);
200 pSession = pHandle->s.pSession;
201 AssertPtrReturn(pSession, NULL);
202 return pSession;
203}
204
205
206/**
207 * Looks up a IDC handle by session.
208 *
209 * @returns The IDC handle on success, NULL on failure.
210 * @param pSession The session to lookup.
211 *
212 * @internal
213 */
214PSUPDRVIDCHANDLE supR0IdcGetHandleFromSession(PSUPDRVSESSION pSession)
215{
216 PSUPDRVIDCHANDLE pHandle = ASMAtomicUoReadPtrT(&g_pMainHandle, PSUPDRVIDCHANDLE);
217 if ( RT_VALID_PTR(pHandle)
218 && pHandle->s.pSession == pSession)
219 return pHandle;
220 return NULL;
221}
222
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use