VirtualBox

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

Last change on this file since 71695 was 70873, checked in by vboxsync, 7 years ago

VMMDev,VBoxGuest: Classify who is calling the host (part 1). bugref:9105

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