1 | /* $Id: VBoxGuestR0LibIdc.cpp 68653 2017-09-05 14:53:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLib - Ring-0 Support Library for VBoxGuest, IDC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "VBoxGuestR0LibInternal.h"
|
---|
32 | #include <VBox/err.h>
|
---|
33 | #include <VBox/VBoxGuest.h>
|
---|
34 | /*#include <iprt/asm.h>*/
|
---|
35 |
|
---|
36 | #ifdef VBGL_VBOXGUEST
|
---|
37 | # error "This file shouldn't be part of the VBoxGuestR0LibBase library that is linked into VBoxGuest. It's client code."
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /*static PVBGLIDCHANDLE volatile g_pMainHandle = NULL;*/
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Opens the IDC interface of the support driver.
|
---|
50 | *
|
---|
51 | * This will perform basic version negotiations and fail if the
|
---|
52 | * minimum requirements aren't met.
|
---|
53 | *
|
---|
54 | * @returns VBox status code.
|
---|
55 | * @param pHandle The handle structure (output).
|
---|
56 | * @param uReqVersion The requested version. Pass 0 for default.
|
---|
57 | * @param uMinVersion The minimum required version. Pass 0 for default.
|
---|
58 | * @param puSessionVersion Where to store the session version. Optional.
|
---|
59 | * @param puDriverVersion Where to store the session version. Optional.
|
---|
60 | * @param puDriverRevision Where to store the SVN revision of the driver. Optional.
|
---|
61 | */
|
---|
62 | DECLR0VBGL(int) VbglR0IdcOpen(PVBGLIDCHANDLE pHandle, uint32_t uReqVersion, uint32_t uMinVersion,
|
---|
63 | uint32_t *puSessionVersion, uint32_t *puDriverVersion, uint32_t *puDriverRevision)
|
---|
64 | {
|
---|
65 | unsigned uDefaultMinVersion;
|
---|
66 | VBGLIOCIDCCONNECT Req;
|
---|
67 | int rc;
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Validate and set failure return values.
|
---|
71 | */
|
---|
72 | AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
|
---|
73 | pHandle->s.pvSession = NULL;
|
---|
74 |
|
---|
75 | AssertPtrNullReturn(puSessionVersion, VERR_INVALID_POINTER);
|
---|
76 | if (puSessionVersion)
|
---|
77 | *puSessionVersion = 0;
|
---|
78 |
|
---|
79 | AssertPtrNullReturn(puDriverVersion, VERR_INVALID_POINTER);
|
---|
80 | if (puDriverVersion)
|
---|
81 | *puDriverVersion = 0;
|
---|
82 |
|
---|
83 | AssertPtrNullReturn(puDriverRevision, VERR_INVALID_POINTER);
|
---|
84 | if (puDriverRevision)
|
---|
85 | *puDriverRevision = 0;
|
---|
86 |
|
---|
87 | AssertReturn(!uMinVersion || (uMinVersion & UINT32_C(0xffff0000)) == (VBGL_IOC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
|
---|
88 | AssertReturn(!uReqVersion || (uReqVersion & UINT32_C(0xffff0000)) == (VBGL_IOC_VERSION & UINT32_C(0xffff0000)), VERR_INVALID_PARAMETER);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Handle default version input and enforce minimum requirements made
|
---|
92 | * by this library.
|
---|
93 | *
|
---|
94 | * The clients will pass defaults (0), and only in the case that some
|
---|
95 | * special API feature was just added will they set an actual version.
|
---|
96 | * So, this is the place where can easily enforce a minimum IDC version
|
---|
97 | * on bugs and similar. It corresponds a bit to what SUPR3Init is
|
---|
98 | * responsible for.
|
---|
99 | */
|
---|
100 | uDefaultMinVersion = VBGL_IOC_VERSION & UINT32_C(0xffff0000);
|
---|
101 | if (!uMinVersion || uMinVersion < uDefaultMinVersion)
|
---|
102 | uMinVersion = uDefaultMinVersion;
|
---|
103 | if (!uReqVersion || uReqVersion < uDefaultMinVersion)
|
---|
104 | uReqVersion = uDefaultMinVersion;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Setup the connect request packet and call the OS specific function.
|
---|
108 | */
|
---|
109 | VBGLREQHDR_INIT(&Req.Hdr, IDC_CONNECT);
|
---|
110 | Req.u.In.u32MagicCookie = VBGL_IOCTL_IDC_CONNECT_MAGIC_COOKIE;
|
---|
111 | Req.u.In.uMinVersion = uMinVersion;
|
---|
112 | Req.u.In.uReqVersion = uReqVersion;
|
---|
113 | Req.u.In.uReserved = 0;
|
---|
114 | rc = vbglR0IdcNativeOpen(pHandle, &Req);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | rc = Req.Hdr.rc;
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | {
|
---|
119 | pHandle->s.pvSession = Req.u.Out.pvSession;
|
---|
120 | if (puSessionVersion)
|
---|
121 | *puSessionVersion = Req.u.Out.uSessionVersion;
|
---|
122 | if (puDriverVersion)
|
---|
123 | *puDriverVersion = Req.u.Out.uDriverVersion;
|
---|
124 | if (puDriverRevision)
|
---|
125 | *puDriverRevision = Req.u.Out.uDriverRevision;
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * We don't really trust anyone, make sure the returned
|
---|
129 | * session and version values actually makes sense.
|
---|
130 | */
|
---|
131 | if ( RT_VALID_PTR(Req.u.Out.pvSession)
|
---|
132 | && Req.u.Out.uSessionVersion >= uMinVersion
|
---|
133 | && (Req.u.Out.uSessionVersion & UINT32_C(0xffff0000)) == (VBGL_IOC_VERSION & UINT32_C(0xffff0000)))
|
---|
134 | {
|
---|
135 | /*ASMAtomicCmpXchgPtr(&g_pMainHandle, pHandle, NULL);*/
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 | AssertMsgFailed(("pSession=%p uSessionVersion=0x%x (r%u)\n", Req.u.Out.pvSession, Req.u.Out.uSessionVersion, Req.u.Out.uDriverRevision));
|
---|
140 | rc = VERR_VERSION_MISMATCH;
|
---|
141 | VbglR0IdcClose(pHandle);
|
---|
142 | }
|
---|
143 |
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Closes a IDC connection established by VbglR0IdcOpen.
|
---|
150 | *
|
---|
151 | * @returns VBox status code.
|
---|
152 | * @param pHandle The IDC handle.
|
---|
153 | */
|
---|
154 | DECLR0VBGL(int) VbglR0IdcClose(PVBGLIDCHANDLE pHandle)
|
---|
155 | {
|
---|
156 | VBGLIOCIDCDISCONNECT Req;
|
---|
157 | int rc;
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Catch closed handles and check that the session is valid.
|
---|
161 | */
|
---|
162 | AssertPtrReturn(pHandle, VERR_INVALID_POINTER);
|
---|
163 | if (!pHandle->s.pvSession)
|
---|
164 | return VERR_INVALID_HANDLE;
|
---|
165 | AssertPtrReturn(pHandle->s.pvSession, VERR_INVALID_HANDLE);
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Create the request and hand it to the OS specific code.
|
---|
169 | */
|
---|
170 | VBGLREQHDR_INIT(&Req.Hdr, IDC_DISCONNECT);
|
---|
171 | Req.u.In.pvSession = pHandle->s.pvSession;
|
---|
172 | rc = vbglR0IdcNativeClose(pHandle, &Req);
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | rc = Req.Hdr.rc;
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | pHandle->s.pvSession = NULL;
|
---|
178 | /*ASMAtomicCmpXchgPtr(&g_pMainHandle, NULL, pHandle);*/
|
---|
179 | }
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Makes an IDC call, returning the request status.
|
---|
186 | *
|
---|
187 | * @returns VBox status code. Request status if the I/O control succeeds,
|
---|
188 | * otherwise the I/O control failure status.
|
---|
189 | * @param pHandle The IDC handle.
|
---|
190 | * @param uReq The request number.
|
---|
191 | * @param pReqHdr The request header.
|
---|
192 | * @param cbReq The request size.
|
---|
193 | */
|
---|
194 | DECLR0VBGL(int) VbglR0IdcCall(PVBGLIDCHANDLE pHandle, uintptr_t uReq, PVBGLREQHDR pReqHdr, uint32_t cbReq)
|
---|
195 | {
|
---|
196 | int rc = VbglR0IdcCallRaw(pHandle, uReq, pReqHdr, cbReq);
|
---|
197 | if (RT_SUCCESS(rc))
|
---|
198 | rc = pReqHdr->rc;
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|