VirtualBox

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

Last change on this file was 100267, checked in by vboxsync, 10 months ago

Additions: Make the R0 physical heap configurable to allow for allocations >= 4GiB if supported by the VBox device (the MMIO request path is available), add support for the MMIO request path required for ARM, bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: VBoxGuestR0LibGenericRequest.cpp 100267 2023-06-23 14:57:53Z vboxsync $ */
2/** @file
3 * VBoxGuestLibR0 - Generic VMMDev request management.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
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#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
38# include <iprt/asm-amd64-x86.h>
39#endif
40#include <iprt/assert.h>
41#include <iprt/string.h>
42#include <VBox/err.h>
43
44
45DECLR0VBGL(int) VbglGR0Verify(const VMMDevRequestHeader *pReq, size_t cbReq)
46{
47 size_t cbReqExpected;
48
49 if (RT_UNLIKELY(!pReq || cbReq < sizeof(VMMDevRequestHeader)))
50 {
51 dprintf(("VbglGR0Verify: Invalid parameter: pReq = %p, cbReq = %zu\n", pReq, cbReq));
52 return VERR_INVALID_PARAMETER;
53 }
54
55 if (RT_UNLIKELY(pReq->size > cbReq))
56 {
57 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
58 return VERR_INVALID_PARAMETER;
59 }
60
61 /* The request size must correspond to the request type. */
62 cbReqExpected = vmmdevGetRequestSize(pReq->requestType);
63 if (RT_UNLIKELY(cbReq < cbReqExpected))
64 {
65 dprintf(("VbglGR0Verify: buffer size %zu < expected size %zu\n", cbReq, cbReqExpected));
66 return VERR_INVALID_PARAMETER;
67 }
68
69 if (cbReqExpected == cbReq)
70 {
71 /*
72 * This is most likely a fixed size request, and in this case the
73 * request size must be also equal to the expected size.
74 */
75 if (RT_UNLIKELY(pReq->size != cbReqExpected))
76 {
77 dprintf(("VbglGR0Verify: request size %u != expected size %zu\n", pReq->size, cbReqExpected));
78 return VERR_INVALID_PARAMETER;
79 }
80
81 return VINF_SUCCESS;
82 }
83
84 /*
85 * This can be a variable size request. Check the request type and limit the size
86 * to VMMDEV_MAX_VMMDEVREQ_SIZE, which is max size supported by the host.
87 *
88 * Note: Keep this list sorted for easier human lookup!
89 */
90 if ( pReq->requestType == VMMDevReq_ChangeMemBalloon
91 || pReq->requestType == VMMDevReq_GetDisplayChangeRequestMulti
92#ifdef VBOX_WITH_64_BITS_GUESTS
93 || pReq->requestType == VMMDevReq_HGCMCall64
94#endif
95 || pReq->requestType == VMMDevReq_HGCMCall32
96 || pReq->requestType == VMMDevReq_RegisterSharedModule
97 || pReq->requestType == VMMDevReq_ReportGuestUserState
98 || pReq->requestType == VMMDevReq_LogString
99 || pReq->requestType == VMMDevReq_SetPointerShape
100 || pReq->requestType == VMMDevReq_VideoSetVisibleRegion
101 || pReq->requestType == VMMDevReq_VideoUpdateMonitorPositions)
102 {
103 if (RT_UNLIKELY(cbReq > VMMDEV_MAX_VMMDEVREQ_SIZE))
104 {
105 dprintf(("VbglGR0Verify: VMMDevReq_LogString: buffer size %zu too big\n", cbReq));
106 return VERR_BUFFER_OVERFLOW; /** @todo is this error code ok? */
107 }
108 }
109 else
110 {
111 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
112 return VERR_IO_BAD_LENGTH; /** @todo is this error code ok? */
113 }
114
115 return VINF_SUCCESS;
116}
117
118DECLR0VBGL(int) VbglR0GRAlloc(VMMDevRequestHeader **ppReq, size_t cbReq, VMMDevRequestType enmReqType)
119{
120 int rc = vbglR0Enter();
121 if (RT_SUCCESS(rc))
122 {
123 if ( ppReq
124 && cbReq >= sizeof(VMMDevRequestHeader)
125 && cbReq == (uint32_t)cbReq)
126 {
127 VMMDevRequestHeader *pReq = (VMMDevRequestHeader *)VbglR0PhysHeapAlloc((uint32_t)cbReq);
128 AssertMsgReturn(pReq, ("VbglR0GRAlloc: no memory (cbReq=%u)\n", cbReq), VERR_NO_MEMORY);
129 memset(pReq, 0xAA, cbReq);
130
131 pReq->size = (uint32_t)cbReq;
132 pReq->version = VMMDEV_REQUEST_HEADER_VERSION;
133 pReq->requestType = enmReqType;
134 pReq->rc = VERR_GENERAL_FAILURE;
135 pReq->reserved1 = 0;
136#ifdef VBGL_VBOXGUEST
137 pReq->fRequestor = VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV
138#else
139 pReq->fRequestor = VMMDEV_REQUESTOR_KERNEL | VMMDEV_REQUESTOR_USR_DRV_OTHER
140#endif
141
142 | VMMDEV_REQUESTOR_CON_DONT_KNOW | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
143 *ppReq = pReq;
144 rc = VINF_SUCCESS;
145 }
146 else
147 {
148 dprintf(("VbglR0GRAlloc: Invalid parameter: ppReq=%p cbReq=%u\n", ppReq, cbReq));
149 rc = VERR_INVALID_PARAMETER;
150 }
151 }
152 return rc;
153}
154
155DECLR0VBGL(int) VbglR0GRPerform(VMMDevRequestHeader *pReq)
156{
157 int rc = vbglR0Enter();
158 if (RT_SUCCESS(rc))
159 {
160 if (pReq)
161 {
162 RTCCPHYS PhysAddr = VbglR0PhysHeapGetPhysAddr(pReq);
163 if (PhysAddr != 0)
164 {
165 if (g_vbgldata.pMmioReq)
166 {
167 Assert((uintptr_t)PhysAddr == PhysAddr);
168 *g_vbgldata.pMmioReq = (uintptr_t)PhysAddr;
169 }
170 else if (PhysAddr < _4G) /* Port IO is 32 bit. */
171 {
172#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
173 ASMOutU32(g_vbgldata.portVMMDev + VMMDEV_PORT_OFF_REQUEST, (uint32_t)PhysAddr);
174#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
175 rc = VERR_INVALID_STATE;
176#else
177# error "I have no memory of this architecture"
178#endif
179 }
180 else
181 rc = VERR_VBGL_INVALID_ADDR;
182
183 /* Make the compiler aware that the host has changed memory. */
184 if (RT_SUCCESS(rc))
185 {
186 ASMCompilerBarrier();
187 rc = pReq->rc;
188 }
189 }
190 else
191 rc = VERR_VBGL_INVALID_ADDR;
192 }
193 else
194 rc = VERR_INVALID_PARAMETER;
195 }
196 return rc;
197}
198
199DECLR0VBGL(void) VbglR0GRFree(VMMDevRequestHeader *pReq)
200{
201 int rc = vbglR0Enter();
202 if (RT_SUCCESS(rc))
203 VbglR0PhysHeapFree(pReq);
204}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use