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