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