1 | /* $Revision: 36740 $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestLibR0 - Generic VMMDev request management.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 | #include "VBGLInternal.h"
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <iprt/asm-amd64-x86.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 |
|
---|
33 | DECLVBGL(int) VbglGRVerify (const VMMDevRequestHeader *pReq, size_t cbReq)
|
---|
34 | {
|
---|
35 | size_t cbReqExpected;
|
---|
36 |
|
---|
37 | if (!pReq || cbReq < sizeof (VMMDevRequestHeader))
|
---|
38 | {
|
---|
39 | dprintf(("VbglGRVerify: Invalid parameter: pReq = %p, cbReq = %d\n", pReq, cbReq));
|
---|
40 | return VERR_INVALID_PARAMETER;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (pReq->size > cbReq)
|
---|
44 | {
|
---|
45 | dprintf(("VbglGRVerify: request size %d > buffer size %d\n", pReq->size, cbReq));
|
---|
46 | return VERR_INVALID_PARAMETER;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* The request size must correspond to the request type. */
|
---|
50 | cbReqExpected = vmmdevGetRequestSize(pReq->requestType);
|
---|
51 |
|
---|
52 | if (cbReq < cbReqExpected)
|
---|
53 | {
|
---|
54 | dprintf(("VbglGRVerify: buffer size %d < expected size %d\n", cbReq, cbReqExpected));
|
---|
55 | return VERR_INVALID_PARAMETER;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (cbReqExpected == cbReq)
|
---|
59 | {
|
---|
60 | /* This is most likely a fixed size request, and in this case the request size
|
---|
61 | * must be also equal to the expected size.
|
---|
62 | */
|
---|
63 | if (pReq->size != cbReqExpected)
|
---|
64 | {
|
---|
65 | dprintf(("VbglGRVerify: request size %d != expected size %d\n", pReq->size, cbReqExpected));
|
---|
66 | return VERR_INVALID_PARAMETER;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return VINF_SUCCESS;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* This can be a variable size request. Check the request type and limit the size
|
---|
73 | * to VMMDEV_MAX_VMMDEVREQ_SIZE, which is max size supported by the host.
|
---|
74 | */
|
---|
75 | if ( pReq->requestType == VMMDevReq_LogString
|
---|
76 | || pReq->requestType == VMMDevReq_VideoSetVisibleRegion
|
---|
77 | || pReq->requestType == VMMDevReq_SetPointerShape
|
---|
78 | #ifdef VBOX_WITH_64_BITS_GUESTS
|
---|
79 | || pReq->requestType == VMMDevReq_HGCMCall32
|
---|
80 | || pReq->requestType == VMMDevReq_HGCMCall64
|
---|
81 | #else
|
---|
82 | || pReq->requestType == VMMDevReq_HGCMCall
|
---|
83 | #endif /* VBOX_WITH_64_BITS_GUESTS */
|
---|
84 | || pReq->requestType == VMMDevReq_ChangeMemBalloon
|
---|
85 | || pReq->requestType == VMMDevReq_RegisterSharedModule)
|
---|
86 | {
|
---|
87 | if (cbReq > VMMDEV_MAX_VMMDEVREQ_SIZE)
|
---|
88 | {
|
---|
89 | dprintf(("VbglGRVerify: VMMDevReq_LogString: buffer size %d too big\n", cbReq));
|
---|
90 | return VERR_BUFFER_OVERFLOW; /* @todo is this error code ok? */
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | dprintf(("VbglGRVerify: request size %d > buffer size %d\n", pReq->size, cbReq));
|
---|
96 | return VERR_IO_BAD_LENGTH; /* @todo is this error code ok? */
|
---|
97 | }
|
---|
98 |
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | }
|
---|
101 |
|
---|
102 | DECLVBGL(int) VbglGRAlloc (VMMDevRequestHeader **ppReq, uint32_t cbSize, VMMDevRequestType reqType)
|
---|
103 | {
|
---|
104 | VMMDevRequestHeader *pReq;
|
---|
105 | int rc = vbglR0Enter ();
|
---|
106 |
|
---|
107 | if (RT_FAILURE(rc))
|
---|
108 | return rc;
|
---|
109 |
|
---|
110 | if (!ppReq || cbSize < sizeof (VMMDevRequestHeader))
|
---|
111 | {
|
---|
112 | dprintf(("VbglGRAlloc: Invalid parameter: ppReq = %p, cbSize = %u\n", ppReq, cbSize));
|
---|
113 | return VERR_INVALID_PARAMETER;
|
---|
114 | }
|
---|
115 |
|
---|
116 | pReq = (VMMDevRequestHeader *)VbglPhysHeapAlloc (cbSize);
|
---|
117 | if (!pReq)
|
---|
118 | {
|
---|
119 | AssertMsgFailed(("VbglGRAlloc: no memory\n"));
|
---|
120 | rc = VERR_NO_MEMORY;
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | memset(pReq, 0xAA, cbSize);
|
---|
125 |
|
---|
126 | pReq->size = cbSize;
|
---|
127 | pReq->version = VMMDEV_REQUEST_HEADER_VERSION;
|
---|
128 | pReq->requestType = reqType;
|
---|
129 | pReq->rc = VERR_GENERAL_FAILURE;
|
---|
130 | pReq->reserved1 = 0;
|
---|
131 | pReq->reserved2 = 0;
|
---|
132 |
|
---|
133 | *ppReq = pReq;
|
---|
134 | }
|
---|
135 |
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 | DECLVBGL(int) VbglGRPerform (VMMDevRequestHeader *pReq)
|
---|
140 | {
|
---|
141 | RTCCPHYS physaddr;
|
---|
142 | int rc = vbglR0Enter ();
|
---|
143 |
|
---|
144 | if (RT_FAILURE(rc))
|
---|
145 | return rc;
|
---|
146 |
|
---|
147 | if (!pReq)
|
---|
148 | return VERR_INVALID_PARAMETER;
|
---|
149 |
|
---|
150 | physaddr = VbglPhysHeapGetPhysAddr (pReq);
|
---|
151 | if ( !physaddr
|
---|
152 | || (physaddr >> 32) != 0) /* Port IO is 32 bit. */
|
---|
153 | {
|
---|
154 | rc = VERR_VBGL_INVALID_ADDR;
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | ASMOutU32(g_vbgldata.portVMMDev + VMMDEV_PORT_OFF_REQUEST, (uint32_t)physaddr);
|
---|
159 | /* Make the compiler aware that the host has changed memory. */
|
---|
160 | ASMCompilerBarrier();
|
---|
161 | rc = pReq->rc;
|
---|
162 | }
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 | DECLVBGL(void) VbglGRFree (VMMDevRequestHeader *pReq)
|
---|
167 | {
|
---|
168 | int rc = vbglR0Enter ();
|
---|
169 |
|
---|
170 | if (RT_FAILURE(rc))
|
---|
171 | return;
|
---|
172 |
|
---|
173 | VbglPhysHeapFree (pReq);
|
---|
174 | }
|
---|
175 |
|
---|