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