VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR0LibGenericRequest.cpp@ 75803

Last change on this file since 75803 was 72627, checked in by vboxsync, 6 years ago

Additions: relicence components needed for Linux shared folders to MIT.
bugref:9109: Shared folders: update to match in-kernel code more closely
This change makes the code on which the Linux kernel shared folder patch is
based MIT-licenced, so that the version in the Linux kernel can be too. This
would make it easier to move code back and forth.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: VBoxGuestR0LibGenericRequest.cpp 72627 2018-06-20 13:53:28Z vboxsync $ */
2/** @file
3 * VBoxGuestLibR0 - Generic VMMDev request management.
4 */
5
6/*
7 * Copyright (C) 2006-2018 Oracle Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include "VBoxGuestR0LibInternal.h"
36#include <iprt/asm.h>
37#include <iprt/asm-amd64-x86.h>
38#include <iprt/assert.h>
39#include <iprt/string.h>
40
41
42DECLR0VBGL(int) VbglGR0Verify(const VMMDevRequestHeader *pReq, size_t cbReq)
43{
44 size_t cbReqExpected;
45
46 if (RT_UNLIKELY(!pReq || cbReq < sizeof(VMMDevRequestHeader)))
47 {
48 dprintf(("VbglGR0Verify: Invalid parameter: pReq = %p, cbReq = %zu\n", pReq, cbReq));
49 return VERR_INVALID_PARAMETER;
50 }
51
52 if (RT_UNLIKELY(pReq->size > cbReq))
53 {
54 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
55 return VERR_INVALID_PARAMETER;
56 }
57
58 /* The request size must correspond to the request type. */
59 cbReqExpected = vmmdevGetRequestSize(pReq->requestType);
60 if (RT_UNLIKELY(cbReq < cbReqExpected))
61 {
62 dprintf(("VbglGR0Verify: buffer size %zu < expected size %zu\n", cbReq, cbReqExpected));
63 return VERR_INVALID_PARAMETER;
64 }
65
66 if (cbReqExpected == cbReq)
67 {
68 /*
69 * This is most likely a fixed size request, and in this case the
70 * request size must be also equal to the expected size.
71 */
72 if (RT_UNLIKELY(pReq->size != cbReqExpected))
73 {
74 dprintf(("VbglGR0Verify: request size %u != expected size %zu\n", pReq->size, cbReqExpected));
75 return VERR_INVALID_PARAMETER;
76 }
77
78 return VINF_SUCCESS;
79 }
80
81 /*
82 * This can be a variable size request. Check the request type and limit the size
83 * to VMMDEV_MAX_VMMDEVREQ_SIZE, which is max size supported by the host.
84 *
85 * Note: Keep this list sorted for easier human lookup!
86 */
87 if ( pReq->requestType == VMMDevReq_ChangeMemBalloon
88 || pReq->requestType == VMMDevReq_GetDisplayChangeRequestMulti
89#ifdef VBOX_WITH_64_BITS_GUESTS
90 || pReq->requestType == VMMDevReq_HGCMCall32
91 || pReq->requestType == VMMDevReq_HGCMCall64
92#else
93 || pReq->requestType == VMMDevReq_HGCMCall
94#endif
95 || pReq->requestType == VMMDevReq_RegisterSharedModule
96 || pReq->requestType == VMMDevReq_ReportGuestUserState
97 || pReq->requestType == VMMDevReq_LogString
98 || pReq->requestType == VMMDevReq_SetPointerShape
99 || pReq->requestType == VMMDevReq_VideoSetVisibleRegion)
100 {
101 if (RT_UNLIKELY(cbReq > VMMDEV_MAX_VMMDEVREQ_SIZE))
102 {
103 dprintf(("VbglGR0Verify: VMMDevReq_LogString: buffer size %zu too big\n", cbReq));
104 return VERR_BUFFER_OVERFLOW; /** @todo is this error code ok? */
105 }
106 }
107 else
108 {
109 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
110 return VERR_IO_BAD_LENGTH; /** @todo is this error code ok? */
111 }
112
113 return VINF_SUCCESS;
114}
115
116DECLR0VBGL(int) VbglR0GRAlloc(VMMDevRequestHeader **ppReq, size_t cbReq, VMMDevRequestType enmReqType)
117{
118 int rc = vbglR0Enter();
119 if (RT_SUCCESS(rc))
120 {
121 if ( ppReq
122 && cbReq >= sizeof(VMMDevRequestHeader)
123 && cbReq == (uint32_t)cbReq)
124 {
125 VMMDevRequestHeader *pReq = (VMMDevRequestHeader *)VbglR0PhysHeapAlloc((uint32_t)cbReq);
126 AssertMsgReturn(pReq, ("VbglR0GRAlloc: no memory (cbReq=%u)\n", cbReq), VERR_NO_MEMORY);
127 memset(pReq, 0xAA, cbReq);
128
129 pReq->size = (uint32_t)cbReq;
130 pReq->version = VMMDEV_REQUEST_HEADER_VERSION;
131 pReq->requestType = enmReqType;
132 pReq->rc = VERR_GENERAL_FAILURE;
133 pReq->reserved1 = 0;
134#ifdef VBGL_VBOXGUEST
135 pReq->fRequestor = VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV
136#else
137 pReq->fRequestor = VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV_OTHER
138#endif
139
140 | VMMDEV_REQUESTOR_CON_DONT_KNOW | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
141 *ppReq = pReq;
142 rc = VINF_SUCCESS;
143 }
144 else
145 {
146 dprintf(("VbglR0GRAlloc: Invalid parameter: ppReq=%p cbReq=%u\n", ppReq, cbReq));
147 rc = VERR_INVALID_PARAMETER;
148 }
149 }
150 return rc;
151}
152
153DECLR0VBGL(int) VbglR0GRPerform(VMMDevRequestHeader *pReq)
154{
155 int rc = vbglR0Enter();
156 if (RT_SUCCESS(rc))
157 {
158 if (pReq)
159 {
160 RTCCPHYS PhysAddr = VbglR0PhysHeapGetPhysAddr(pReq);
161 if ( PhysAddr != 0
162 && PhysAddr < _4G) /* Port IO is 32 bit. */
163 {
164 ASMOutU32(g_vbgldata.portVMMDev + VMMDEV_PORT_OFF_REQUEST, (uint32_t)PhysAddr);
165 /* Make the compiler aware that the host has changed memory. */
166 ASMCompilerBarrier();
167 rc = pReq->rc;
168 }
169 else
170 rc = VERR_VBGL_INVALID_ADDR;
171 }
172 else
173 rc = VERR_INVALID_PARAMETER;
174 }
175 return rc;
176}
177
178DECLR0VBGL(void) VbglR0GRFree(VMMDevRequestHeader *pReq)
179{
180 int rc = vbglR0Enter();
181 if (RT_SUCCESS(rc))
182 VbglR0PhysHeapFree(pReq);
183}
184
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette