1 | /* $Id: SUPR0IdcClient.c 10258 2008-07-04 23:31:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Driver - IDC Client Lib, Core.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "SUPR0IdcClientInternal.h"
|
---|
35 | #include <VBox/err.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Global Variables *
|
---|
40 | *******************************************************************************/
|
---|
41 | static PSUPDRVIDCHANDLE volatile g_pMainHandle = NULL;
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Opens the IDC interface of the support driver.
|
---|
46 | *
|
---|
47 | * This will perform basic version negotiations and fail if the
|
---|
48 | * minmum requirements aren't met.
|
---|
49 | *
|
---|
50 | * @returns VBox status code.
|
---|
51 | * @param pHandle The handle structure (output).
|
---|
52 | * @param uReqVersion The requested version. Pass 0 for default.
|
---|
53 | * @param uMinVersion The minimum required version. Pass 0 for default.
|
---|
54 | * @param puVersion Where to store the version of the IDC interface. Optional.
|
---|
55 | */
|
---|
56 | SUPR0DECL(int) SUPR0IdcOpen(PSUPDRVIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion, uint32_t *puVersion)
|
---|
57 | {
|
---|
58 | unsigned uDefaultMinVersion;
|
---|
59 | SUPDRVIDCREQCONNECT Req;
|
---|
60 | int rc;
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Validate and set failure return values.
|
---|
64 | */
|
---|
65 | AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
|
---|
66 | pHandle->s.pSession = NULL;
|
---|
67 | AssertPtrNullReturn(puVersion, VERR_INVALID_POINTER);
|
---|
68 | if (puVersion)
|
---|
69 | *puVersion = 0;
|
---|
70 | AssertReturn(!uMinVersion || (uMinVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
|
---|
71 | AssertReturn(!uReqVersion || (uReqVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Handle default version input and enforce minimum requirements made
|
---|
75 | * by this library.
|
---|
76 | *
|
---|
77 | * The clients will pass defaults (0), and only in the case that some
|
---|
78 | * special API feature was just added will they set an actual version.
|
---|
79 | * So, this is the place where can easily enforce a minimum IDC version
|
---|
80 | * on bugs and similar. It corresponds a bit to what SUPInit is
|
---|
81 | * responsible for.
|
---|
82 | */
|
---|
83 | uDefaultMinVersion = SUPDRV_IDC_VERSION & UINT32_C(0xffff0000);
|
---|
84 | if (!uMinVersion || uMinVersion < uDefaultMinVersion)
|
---|
85 | uMinVersion = uDefaultMinVersion;
|
---|
86 | if (!uReqVersion || uReqVersion < uDefaultMinVersion)
|
---|
87 | uReqVersion = uDefaultMinVersion;
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Setup the connect request packet and call the OS specific function.
|
---|
91 | */
|
---|
92 | Req.Hdr.cb = sizeof(Req);
|
---|
93 | Req.Hdr.rc = VERR_WRONG_ORDER;
|
---|
94 | Req.Hdr.pSession = NULL;
|
---|
95 | Req.u.In.u32MagicCookie = SUPDRVIDCREQ_CONNECT_MAGIC_COOKIE;
|
---|
96 | Req.u.In.uMinVersion = uMinVersion;
|
---|
97 | Req.u.In.uReqVersion = uReqVersion;
|
---|
98 | rc = supR0IdcNativeOpen(pHandle, &Req);
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | pHandle->s.pSession = Req.u.Out.pSession;
|
---|
102 | if (puVersion)
|
---|
103 | *puVersion = Req.u.Out.uVersion;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * We don't really trust anyone, make sure the returned
|
---|
107 | * session and version values actually makes sense.
|
---|
108 | */
|
---|
109 | if ( VALID_PTR(Req.u.Out.pSession)
|
---|
110 | && Req.u.Out.uVersion >= uMinVersion
|
---|
111 | && (Req.u.Out.uVersion & UINT32_C(0xffff0000)) == (SUPDRV_IDC_VERSION & UINT32_C(0xffff0000)))
|
---|
112 | {
|
---|
113 | ASMAtomicCmpXchgPtr((void * volatile *)&g_pMainHandle, pHandle, NULL);
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 | AssertMsgFailed(("pSession=%p uVersion=0x%x\n", Req.u.Out.pSession, Req.u.Out.uVersion));
|
---|
118 | rc = VERR_VERSION_MISMATCH;
|
---|
119 | SUPR0IdcClose(pHandle);
|
---|
120 | }
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Closes a IDC connection established by SUPR0IdcOpen.
|
---|
128 | *
|
---|
129 | * @returns VBox status code.
|
---|
130 | * @param pHandle The IDC handle.
|
---|
131 | */
|
---|
132 | SUPR0DECL(int) SUPR0IdcClose(PSUPDRVIDCHANDLE pHandle)
|
---|
133 | {
|
---|
134 | SUPDRVIDCREQHDR Req;
|
---|
135 | int rc;
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Catch closed handles and check that the session is valid.
|
---|
139 | */
|
---|
140 | AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
|
---|
141 | if (!pHandle->s.pSession)
|
---|
142 | return VERR_INVALID_HANDLE;
|
---|
143 | AssertPtrReturn(pHandle->s.pSession, VERR_INVALID_HANDLE);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Create the request and hand it to the OS specific code.
|
---|
147 | */
|
---|
148 | Req.cb = sizeof(Req);
|
---|
149 | Req.rc = VERR_WRONG_ORDER;
|
---|
150 | Req.pSession = pHandle->s.pSession;
|
---|
151 | rc = supR0IdcNativeClose(pHandle, &Req);
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | {
|
---|
154 | pHandle->s.pSession = NULL;
|
---|
155 | ASMAtomicCmpXchgPtr((void * volatile *)&g_pMainHandle, NULL, pHandle);
|
---|
156 | }
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Get the SUPDRV session for the IDC connection.
|
---|
163 | *
|
---|
164 | * This is for use with SUPDRV and component APIs that requires a valid
|
---|
165 | * session handle.
|
---|
166 | *
|
---|
167 | * @returns The session handle on success, NULL if the IDC handle is invalid.
|
---|
168 | *
|
---|
169 | * @param pHandle The IDC handle.
|
---|
170 | */
|
---|
171 | SUPR0DECL(PSUPDRVSESSION) SUPR0IdcGetSession(PSUPDRVIDCHANDLE pHandle)
|
---|
172 | {
|
---|
173 | PSUPDRVSESSION pSession;
|
---|
174 | AssertPtrReturn(pHandle, NULL);
|
---|
175 | pSession = pHandle->s.pSession;
|
---|
176 | AssertPtrReturn(pSession, NULL);
|
---|
177 | return pSession;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Looks up a IDC handle by session.
|
---|
183 | *
|
---|
184 | * @returns The IDC handle on success, NULL on failure.
|
---|
185 | * @param pSession The session to lookup.
|
---|
186 | *
|
---|
187 | * @internal
|
---|
188 | */
|
---|
189 | PSUPDRVIDCHANDLE supR0IdcGetHandleFromSession(PSUPDRVSESSION pSession)
|
---|
190 | {
|
---|
191 | PSUPDRVIDCHANDLE pHandle = (PSUPDRVIDCHANDLE)ASMAtomicUoReadPtr((void * volatile *)&g_pMainHandle);
|
---|
192 | if ( VALID_PTR(pHandle)
|
---|
193 | && pHandle->s.pSession == pSession)
|
---|
194 | return pHandle;
|
---|
195 | return NULL;
|
---|
196 | }
|
---|
197 |
|
---|