]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - ubuntu/vbox/vboxguest/VBoxGuestR0LibGenericRequest.c
UBUNTU: ubuntu: vbox -- update to 5.2.2-dfsg-2
[mirror_ubuntu-bionic-kernel.git] / ubuntu / vbox / vboxguest / VBoxGuestR0LibGenericRequest.c
CommitLineData
6d209b23 1/* $Id: VBoxGuestR0LibGenericRequest.cpp $ */
056a1eb7
SF
2/** @file
3 * VBoxGuestLibR0 - Generic VMMDev request management.
4 */
5
6/*
26894aac 7 * Copyright (C) 2006-2017 Oracle Corporation
056a1eb7
SF
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
6d209b23
SF
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "VBoxGuestR0LibInternal.h"
056a1eb7
SF
32#include <iprt/asm.h>
33#include <iprt/asm-amd64-x86.h>
34#include <iprt/assert.h>
35#include <iprt/string.h>
36
37
6d209b23 38DECLR0VBGL(int) VbglGR0Verify(const VMMDevRequestHeader *pReq, size_t cbReq)
056a1eb7
SF
39{
40 size_t cbReqExpected;
41
42 if (RT_UNLIKELY(!pReq || cbReq < sizeof(VMMDevRequestHeader)))
43 {
6d209b23 44 dprintf(("VbglGR0Verify: Invalid parameter: pReq = %p, cbReq = %zu\n", pReq, cbReq));
056a1eb7
SF
45 return VERR_INVALID_PARAMETER;
46 }
47
48 if (RT_UNLIKELY(pReq->size > cbReq))
49 {
6d209b23 50 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
056a1eb7
SF
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 {
6d209b23 58 dprintf(("VbglGR0Verify: buffer size %zu < expected size %zu\n", cbReq, cbReqExpected));
056a1eb7
SF
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 {
6d209b23 70 dprintf(("VbglGR0Verify: request size %u != expected size %zu\n", pReq->size, cbReqExpected));
056a1eb7
SF
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 {
6d209b23 98 dprintf(("VbglGR0Verify: VMMDevReq_LogString: buffer size %zu too big\n", cbReq));
056a1eb7
SF
99 return VERR_BUFFER_OVERFLOW; /** @todo is this error code ok? */
100 }
101 }
102 else
103 {
6d209b23 104 dprintf(("VbglGR0Verify: request size %u > buffer size %zu\n", pReq->size, cbReq));
056a1eb7
SF
105 return VERR_IO_BAD_LENGTH; /** @todo is this error code ok? */
106 }
107
108 return VINF_SUCCESS;
109}
110
6d209b23 111DECLR0VBGL(int) VbglR0GRAlloc(VMMDevRequestHeader **ppReq, size_t cbReq, VMMDevRequestType enmReqType)
056a1eb7
SF
112{
113 int rc = vbglR0Enter();
114 if (RT_SUCCESS(rc))
115 {
116 if ( ppReq
117 && cbReq >= sizeof(VMMDevRequestHeader)
118 && cbReq == (uint32_t)cbReq)
119 {
6d209b23
SF
120 VMMDevRequestHeader *pReq = (VMMDevRequestHeader *)VbglR0PhysHeapAlloc((uint32_t)cbReq);
121 AssertMsgReturn(pReq, ("VbglR0GRAlloc: no memory (cbReq=%u)\n", cbReq), VERR_NO_MEMORY);
056a1eb7
SF
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 pReq->reserved2 = 0;
130
131 *ppReq = pReq;
132 rc = VINF_SUCCESS;
133 }
134 else
135 {
6d209b23 136 dprintf(("VbglR0GRAlloc: Invalid parameter: ppReq=%p cbReq=%u\n", ppReq, cbReq));
056a1eb7
SF
137 rc = VERR_INVALID_PARAMETER;
138 }
139 }
140 return rc;
141}
142
6d209b23 143DECLR0VBGL(int) VbglR0GRPerform(VMMDevRequestHeader *pReq)
056a1eb7
SF
144{
145 int rc = vbglR0Enter();
146 if (RT_SUCCESS(rc))
147 {
148 if (pReq)
149 {
6d209b23 150 RTCCPHYS PhysAddr = VbglR0PhysHeapGetPhysAddr(pReq);
056a1eb7
SF
151 if ( PhysAddr != 0
152 && PhysAddr < _4G) /* Port IO is 32 bit. */
153 {
154 ASMOutU32(g_vbgldata.portVMMDev + VMMDEV_PORT_OFF_REQUEST, (uint32_t)PhysAddr);
155 /* Make the compiler aware that the host has changed memory. */
156 ASMCompilerBarrier();
157 rc = pReq->rc;
158 }
159 else
160 rc = VERR_VBGL_INVALID_ADDR;
161 }
162 else
163 rc = VERR_INVALID_PARAMETER;
164 }
165 return rc;
166}
167
6d209b23 168DECLR0VBGL(void) VbglR0GRFree(VMMDevRequestHeader *pReq)
056a1eb7
SF
169{
170 int rc = vbglR0Enter();
171 if (RT_SUCCESS(rc))
6d209b23 172 VbglR0PhysHeapFree(pReq);
056a1eb7
SF
173}
174