[55401] | 1 | /* $Id: VBoxGuestR0LibGenericRequest.cpp 100267 2023-06-23 14:57:53Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[21211] | 3 | * VBoxGuestLibR0 - Generic VMMDev request management.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[72627] | 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:
|
---|
[8155] | 17 | *
|
---|
[72627] | 18 | * The above copyright notice and this permission notice shall be
|
---|
| 19 | * included in all copies or substantial portions of the Software.
|
---|
[26425] | 20 | *
|
---|
[72627] | 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.
|
---|
[1] | 29 | */
|
---|
| 30 |
|
---|
[68653] | 31 |
|
---|
| 32 | /*********************************************************************************************************************************
|
---|
| 33 | * Header Files *
|
---|
| 34 | *********************************************************************************************************************************/
|
---|
[68645] | 35 | #include "VBoxGuestR0LibInternal.h"
|
---|
[1] | 36 | #include <iprt/asm.h>
|
---|
[100267] | 37 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
| 38 | # include <iprt/asm-amd64-x86.h>
|
---|
| 39 | #endif
|
---|
[21211] | 40 | #include <iprt/assert.h>
|
---|
[2196] | 41 | #include <iprt/string.h>
|
---|
[76419] | 42 | #include <VBox/err.h>
|
---|
[1] | 43 |
|
---|
[58178] | 44 |
|
---|
[68654] | 45 | DECLR0VBGL(int) VbglGR0Verify(const VMMDevRequestHeader *pReq, size_t cbReq)
|
---|
[23916] | 46 | {
|
---|
[31720] | 47 | size_t cbReqExpected;
|
---|
| 48 |
|
---|
[58178] | 49 | if (RT_UNLIKELY(!pReq || cbReq < sizeof(VMMDevRequestHeader)))
|
---|
[23916] | 50 | {
|
---|
[68654] | 51 | dprintf(("VbglGR0Verify: Invalid parameter: pReq = %p, cbReq = %zu\n", pReq, cbReq));
|
---|
[23916] | 52 | return VERR_INVALID_PARAMETER;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[58178] | 55 | if (RT_UNLIKELY(pReq->size > cbReq))
|
---|
[23916] | 56 | {
|
---|
[68654] | 57 | dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
|
---|
[23916] | 58 | return VERR_INVALID_PARAMETER;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /* The request size must correspond to the request type. */
|
---|
[31720] | 62 | cbReqExpected = vmmdevGetRequestSize(pReq->requestType);
|
---|
[58178] | 63 | if (RT_UNLIKELY(cbReq < cbReqExpected))
|
---|
[23916] | 64 | {
|
---|
[68654] | 65 | dprintf(("VbglGR0Verify: buffer size %zu < expected size %zu\n", cbReq, cbReqExpected));
|
---|
[23916] | 66 | return VERR_INVALID_PARAMETER;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | if (cbReqExpected == cbReq)
|
---|
| 70 | {
|
---|
[58178] | 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.
|
---|
[23916] | 74 | */
|
---|
[58178] | 75 | if (RT_UNLIKELY(pReq->size != cbReqExpected))
|
---|
[23916] | 76 | {
|
---|
[68654] | 77 | dprintf(("VbglGR0Verify: request size %u != expected size %zu\n", pReq->size, cbReqExpected));
|
---|
[23916] | 78 | return VERR_INVALID_PARAMETER;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return VINF_SUCCESS;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[47294] | 84 | /*
|
---|
| 85 | * This can be a variable size request. Check the request type and limit the size
|
---|
[26493] | 86 | * to VMMDEV_MAX_VMMDEVREQ_SIZE, which is max size supported by the host.
|
---|
[47294] | 87 | *
|
---|
| 88 | * Note: Keep this list sorted for easier human lookup!
|
---|
[23916] | 89 | */
|
---|
[47294] | 90 | if ( pReq->requestType == VMMDevReq_ChangeMemBalloon
|
---|
[72352] | 91 | || pReq->requestType == VMMDevReq_GetDisplayChangeRequestMulti
|
---|
[23916] | 92 | #ifdef VBOX_WITH_64_BITS_GUESTS
|
---|
| 93 | || pReq->requestType == VMMDevReq_HGCMCall64
|
---|
[58178] | 94 | #endif
|
---|
[77056] | 95 | || pReq->requestType == VMMDevReq_HGCMCall32
|
---|
[47294] | 96 | || pReq->requestType == VMMDevReq_RegisterSharedModule
|
---|
| 97 | || pReq->requestType == VMMDevReq_ReportGuestUserState
|
---|
| 98 | || pReq->requestType == VMMDevReq_LogString
|
---|
| 99 | || pReq->requestType == VMMDevReq_SetPointerShape
|
---|
[83142] | 100 | || pReq->requestType == VMMDevReq_VideoSetVisibleRegion
|
---|
| 101 | || pReq->requestType == VMMDevReq_VideoUpdateMonitorPositions)
|
---|
[23916] | 102 | {
|
---|
[58178] | 103 | if (RT_UNLIKELY(cbReq > VMMDEV_MAX_VMMDEVREQ_SIZE))
|
---|
[23916] | 104 | {
|
---|
[68654] | 105 | dprintf(("VbglGR0Verify: VMMDevReq_LogString: buffer size %zu too big\n", cbReq));
|
---|
[58178] | 106 | return VERR_BUFFER_OVERFLOW; /** @todo is this error code ok? */
|
---|
[23916] | 107 | }
|
---|
| 108 | }
|
---|
| 109 | else
|
---|
| 110 | {
|
---|
[68654] | 111 | dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
|
---|
[58178] | 112 | return VERR_IO_BAD_LENGTH; /** @todo is this error code ok? */
|
---|
[23916] | 113 | }
|
---|
| 114 |
|
---|
| 115 | return VINF_SUCCESS;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[68654] | 118 | DECLR0VBGL(int) VbglR0GRAlloc(VMMDevRequestHeader **ppReq, size_t cbReq, VMMDevRequestType enmReqType)
|
---|
[1] | 119 | {
|
---|
[58178] | 120 | int rc = vbglR0Enter();
|
---|
| 121 | if (RT_SUCCESS(rc))
|
---|
[1] | 122 | {
|
---|
[58183] | 123 | if ( ppReq
|
---|
| 124 | && cbReq >= sizeof(VMMDevRequestHeader)
|
---|
| 125 | && cbReq == (uint32_t)cbReq)
|
---|
[58178] | 126 | {
|
---|
[68654] | 127 | VMMDevRequestHeader *pReq = (VMMDevRequestHeader *)VbglR0PhysHeapAlloc((uint32_t)cbReq);
|
---|
| 128 | AssertMsgReturn(pReq, ("VbglR0GRAlloc: no memory (cbReq=%u)\n", cbReq), VERR_NO_MEMORY);
|
---|
[58178] | 129 | memset(pReq, 0xAA, cbReq);
|
---|
[1] | 130 |
|
---|
[58183] | 131 | pReq->size = (uint32_t)cbReq;
|
---|
[58178] | 132 | pReq->version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
| 133 | pReq->requestType = enmReqType;
|
---|
| 134 | pReq->rc = VERR_GENERAL_FAILURE;
|
---|
| 135 | pReq->reserved1 = 0;
|
---|
[70873] | 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
|
---|
[1] | 141 |
|
---|
[70873] | 142 | | VMMDEV_REQUESTOR_CON_DONT_KNOW | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
|
---|
[58178] | 143 | *ppReq = pReq;
|
---|
| 144 | rc = VINF_SUCCESS;
|
---|
| 145 | }
|
---|
| 146 | else
|
---|
| 147 | {
|
---|
[68654] | 148 | dprintf(("VbglR0GRAlloc: Invalid parameter: ppReq=%p cbReq=%u\n", ppReq, cbReq));
|
---|
[58178] | 149 | rc = VERR_INVALID_PARAMETER;
|
---|
| 150 | }
|
---|
[1] | 151 | }
|
---|
| 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[68654] | 155 | DECLR0VBGL(int) VbglR0GRPerform(VMMDevRequestHeader *pReq)
|
---|
[1] | 156 | {
|
---|
[58178] | 157 | int rc = vbglR0Enter();
|
---|
| 158 | if (RT_SUCCESS(rc))
|
---|
[1] | 159 | {
|
---|
[58178] | 160 | if (pReq)
|
---|
| 161 | {
|
---|
[68654] | 162 | RTCCPHYS PhysAddr = VbglR0PhysHeapGetPhysAddr(pReq);
|
---|
[100267] | 163 | if (PhysAddr != 0)
|
---|
[58178] | 164 | {
|
---|
[100267] | 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 |
|
---|
[58178] | 183 | /* Make the compiler aware that the host has changed memory. */
|
---|
[100267] | 184 | if (RT_SUCCESS(rc))
|
---|
| 185 | {
|
---|
| 186 | ASMCompilerBarrier();
|
---|
| 187 | rc = pReq->rc;
|
---|
| 188 | }
|
---|
[58178] | 189 | }
|
---|
| 190 | else
|
---|
| 191 | rc = VERR_VBGL_INVALID_ADDR;
|
---|
| 192 | }
|
---|
| 193 | else
|
---|
| 194 | rc = VERR_INVALID_PARAMETER;
|
---|
[1] | 195 | }
|
---|
| 196 | return rc;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[68654] | 199 | DECLR0VBGL(void) VbglR0GRFree(VMMDevRequestHeader *pReq)
|
---|
[1] | 200 | {
|
---|
[58178] | 201 | int rc = vbglR0Enter();
|
---|
| 202 | if (RT_SUCCESS(rc))
|
---|
[68654] | 203 | VbglR0PhysHeapFree(pReq);
|
---|
[1] | 204 | }
|
---|