1 | /* $Id: VBoxGuestR0LibGenericRequest.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLibR0 - Generic VMMDev request management.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | #include <VBox/err.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | DECLR0VBGL(int) VbglGR0Verify(const VMMDevRequestHeader *pReq, size_t cbReq)
|
---|
44 | {
|
---|
45 | size_t cbReqExpected;
|
---|
46 |
|
---|
47 | if (RT_UNLIKELY(!pReq || cbReq < sizeof(VMMDevRequestHeader)))
|
---|
48 | {
|
---|
49 | dprintf(("VbglGR0Verify: Invalid parameter: pReq = %p, cbReq = %zu\n", pReq, cbReq));
|
---|
50 | return VERR_INVALID_PARAMETER;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (RT_UNLIKELY(pReq->size > cbReq))
|
---|
54 | {
|
---|
55 | dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
|
---|
56 | return VERR_INVALID_PARAMETER;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* The request size must correspond to the request type. */
|
---|
60 | cbReqExpected = vmmdevGetRequestSize(pReq->requestType);
|
---|
61 | if (RT_UNLIKELY(cbReq < cbReqExpected))
|
---|
62 | {
|
---|
63 | dprintf(("VbglGR0Verify: buffer size %zu < expected size %zu\n", cbReq, cbReqExpected));
|
---|
64 | return VERR_INVALID_PARAMETER;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (cbReqExpected == cbReq)
|
---|
68 | {
|
---|
69 | /*
|
---|
70 | * This is most likely a fixed size request, and in this case the
|
---|
71 | * request size must be also equal to the expected size.
|
---|
72 | */
|
---|
73 | if (RT_UNLIKELY(pReq->size != cbReqExpected))
|
---|
74 | {
|
---|
75 | dprintf(("VbglGR0Verify: request size %u != expected size %zu\n", pReq->size, cbReqExpected));
|
---|
76 | return VERR_INVALID_PARAMETER;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * This can be a variable size request. Check the request type and limit the size
|
---|
84 | * to VMMDEV_MAX_VMMDEVREQ_SIZE, which is max size supported by the host.
|
---|
85 | *
|
---|
86 | * Note: Keep this list sorted for easier human lookup!
|
---|
87 | */
|
---|
88 | if ( pReq->requestType == VMMDevReq_ChangeMemBalloon
|
---|
89 | || pReq->requestType == VMMDevReq_GetDisplayChangeRequestMulti
|
---|
90 | #ifdef VBOX_WITH_64_BITS_GUESTS
|
---|
91 | || pReq->requestType == VMMDevReq_HGCMCall64
|
---|
92 | #endif
|
---|
93 | || pReq->requestType == VMMDevReq_HGCMCall32
|
---|
94 | || pReq->requestType == VMMDevReq_RegisterSharedModule
|
---|
95 | || pReq->requestType == VMMDevReq_ReportGuestUserState
|
---|
96 | || pReq->requestType == VMMDevReq_LogString
|
---|
97 | || pReq->requestType == VMMDevReq_SetPointerShape
|
---|
98 | || pReq->requestType == VMMDevReq_VideoSetVisibleRegion
|
---|
99 | || pReq->requestType == VMMDevReq_VideoUpdateMonitorPositions)
|
---|
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 |
|
---|
116 | DECLR0VBGL(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 |
|
---|
153 | DECLR0VBGL(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 |
|
---|
178 | DECLR0VBGL(void) VbglR0GRFree(VMMDevRequestHeader *pReq)
|
---|
179 | {
|
---|
180 | int rc = vbglR0Enter();
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | VbglR0PhysHeapFree(pReq);
|
---|
183 | }
|
---|