1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox Host Guest Shared Memory Interface (HGSMI).
|
---|
4 | * Host/Guest shared part.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * The contents of this file may alternatively be used under the terms
|
---|
19 | * of the Common Development and Distribution License Version 1.0
|
---|
20 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
21 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
22 | * CDDL are applicable instead of those of the GPL.
|
---|
23 | *
|
---|
24 | * You may elect to license modified versions of this file under the
|
---|
25 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | #ifndef ___VBox_HGSMI_HGSMI_h
|
---|
30 | #define ___VBox_HGSMI_HGSMI_h
|
---|
31 |
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/types.h>
|
---|
34 |
|
---|
35 | #include <VBox/HGSMI/HGSMIDefs.h>
|
---|
36 | #include <VBox/HGSMI/HGSMIChannels.h>
|
---|
37 | #include <VBox/HGSMI/HGSMIMemAlloc.h>
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Basic mechanism for the HGSMI is to prepare and pass data buffer to the host and the guest.
|
---|
41 | * Data inside these buffers are opaque for the HGSMI and are interpreted by higher levels.
|
---|
42 | *
|
---|
43 | * Every shared memory buffer passed between the guest/host has the following structure:
|
---|
44 | *
|
---|
45 | * HGSMIBUFFERHEADER header;
|
---|
46 | * uint8_t data[header.u32BufferSize];
|
---|
47 | * HGSMIBUFFERTAIL tail;
|
---|
48 | *
|
---|
49 | * Note: Offset of the 'header' in the memory is used for virtual hardware IO.
|
---|
50 | *
|
---|
51 | * Buffers are verifyed using the offset and the content of the header and the tail,
|
---|
52 | * which are constant during a call.
|
---|
53 | *
|
---|
54 | * Invalid buffers are ignored.
|
---|
55 | *
|
---|
56 | * Actual 'data' is not verifyed, as it is expected that the data can be changed by the
|
---|
57 | * called function.
|
---|
58 | *
|
---|
59 | * Since only the offset of the buffer is passed in a IO operation, the header and tail
|
---|
60 | * must contain:
|
---|
61 | * * size of data in this buffer;
|
---|
62 | * * checksum for buffer verification.
|
---|
63 | *
|
---|
64 | * For segmented transfers:
|
---|
65 | * * the sequence identifier;
|
---|
66 | * * offset of the current segment in the sequence;
|
---|
67 | * * total bytes in the transfer.
|
---|
68 | *
|
---|
69 | * Additionally contains:
|
---|
70 | * * the channel ID;
|
---|
71 | * * the channel information.
|
---|
72 | */
|
---|
73 |
|
---|
74 | /* Heap types. */
|
---|
75 | #define HGSMI_HEAP_TYPE_NULL 0 /* Heap not initialized. */
|
---|
76 | #define HGSMI_HEAP_TYPE_POINTER 1 /* Deprecated. RTHEAPSIMPLE. */
|
---|
77 | #define HGSMI_HEAP_TYPE_OFFSET 2 /* Deprecated. RTHEAPOFFSET. */
|
---|
78 | #define HGSMI_HEAP_TYPE_MA 3 /* Memory allocator. */
|
---|
79 |
|
---|
80 | #pragma pack(1)
|
---|
81 | typedef struct HGSMIHEAP
|
---|
82 | {
|
---|
83 | union
|
---|
84 | {
|
---|
85 | HGSMIMADATA ma; /* Memory Allocator */
|
---|
86 | RTHEAPSIMPLE hPtr; /* Pointer based heap. */
|
---|
87 | RTHEAPOFFSET hOff; /* Offset based heap. */
|
---|
88 | } u;
|
---|
89 | HGSMIAREA area; /* Description. */
|
---|
90 | int cRefs; /* Number of heap allocations. */
|
---|
91 | uint32_t u32HeapType; /* HGSMI_HEAP_TYPE_* */
|
---|
92 | } HGSMIHEAP;
|
---|
93 | #pragma pack()
|
---|
94 |
|
---|
95 | #pragma pack(1)
|
---|
96 | /* The size of the array of channels. Array indexes are uint8_t. Note: the value must not be changed. */
|
---|
97 | #define HGSMI_NUMBER_OF_CHANNELS 0x100
|
---|
98 |
|
---|
99 | /* Channel handler called when the guest submits a buffer. */
|
---|
100 | typedef DECLCALLBACK(int) FNHGSMICHANNELHANDLER(void *pvHandler, uint16_t u16ChannelInfo, void *pvBuffer, HGSMISIZE cbBuffer);
|
---|
101 | typedef FNHGSMICHANNELHANDLER *PFNHGSMICHANNELHANDLER;
|
---|
102 |
|
---|
103 | /* Information about a handler: pfn + context. */
|
---|
104 | typedef struct _HGSMICHANNELHANDLER
|
---|
105 | {
|
---|
106 | PFNHGSMICHANNELHANDLER pfnHandler;
|
---|
107 | void *pvHandler;
|
---|
108 | } HGSMICHANNELHANDLER;
|
---|
109 |
|
---|
110 | /* Channel description. */
|
---|
111 | typedef struct _HGSMICHANNEL
|
---|
112 | {
|
---|
113 | HGSMICHANNELHANDLER handler; /* The channel handler. */
|
---|
114 | const char *pszName; /* NULL for hardcoded channels or RTStrDup'ed name. */
|
---|
115 | uint8_t u8Channel; /* The channel id, equal to the channel index in the array. */
|
---|
116 | uint8_t u8Flags; /* HGSMI_CH_F_* */
|
---|
117 | } HGSMICHANNEL;
|
---|
118 |
|
---|
119 | typedef struct _HGSMICHANNELINFO
|
---|
120 | {
|
---|
121 | HGSMICHANNEL Channels[HGSMI_NUMBER_OF_CHANNELS]; /* Channel handlers indexed by the channel id.
|
---|
122 | * The array is accessed under the instance lock.
|
---|
123 | */
|
---|
124 | } HGSMICHANNELINFO;
|
---|
125 | #pragma pack()
|
---|
126 |
|
---|
127 |
|
---|
128 | RT_C_DECLS_BEGIN
|
---|
129 |
|
---|
130 | DECLINLINE(HGSMISIZE) HGSMIBufferMinimumSize (void)
|
---|
131 | {
|
---|
132 | return sizeof (HGSMIBUFFERHEADER) + sizeof (HGSMIBUFFERTAIL);
|
---|
133 | }
|
---|
134 |
|
---|
135 | DECLINLINE(uint8_t *) HGSMIBufferData (const HGSMIBUFFERHEADER *pHeader)
|
---|
136 | {
|
---|
137 | return (uint8_t *)pHeader + sizeof (HGSMIBUFFERHEADER);
|
---|
138 | }
|
---|
139 |
|
---|
140 | DECLINLINE(HGSMIBUFFERTAIL *) HGSMIBufferTail (const HGSMIBUFFERHEADER *pHeader)
|
---|
141 | {
|
---|
142 | return (HGSMIBUFFERTAIL *)(HGSMIBufferData (pHeader) + pHeader->u32DataSize);
|
---|
143 | }
|
---|
144 |
|
---|
145 | DECLINLINE(HGSMIBUFFERHEADER *) HGSMIBufferHeaderFromData (const void *pvData)
|
---|
146 | {
|
---|
147 | return (HGSMIBUFFERHEADER *)((uint8_t *)pvData - sizeof (HGSMIBUFFERHEADER));
|
---|
148 | }
|
---|
149 |
|
---|
150 | DECLINLINE(HGSMISIZE) HGSMIBufferRequiredSize (uint32_t u32DataSize)
|
---|
151 | {
|
---|
152 | return HGSMIBufferMinimumSize () + u32DataSize;
|
---|
153 | }
|
---|
154 |
|
---|
155 | DECLINLINE(HGSMIOFFSET) HGSMIPointerToOffset(const HGSMIAREA *pArea,
|
---|
156 | const void *pv)
|
---|
157 | {
|
---|
158 | return pArea->offBase + (HGSMIOFFSET)((uint8_t *)pv - pArea->pu8Base);
|
---|
159 | }
|
---|
160 |
|
---|
161 | DECLINLINE(void *) HGSMIOffsetToPointer(const HGSMIAREA *pArea,
|
---|
162 | HGSMIOFFSET offBuffer)
|
---|
163 | {
|
---|
164 | return pArea->pu8Base + (offBuffer - pArea->offBase);
|
---|
165 | }
|
---|
166 |
|
---|
167 | DECLINLINE(uint8_t *) HGSMIBufferDataFromOffset (const HGSMIAREA *pArea, HGSMIOFFSET offBuffer)
|
---|
168 | {
|
---|
169 | HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer(pArea, offBuffer);
|
---|
170 | Assert(pHeader);
|
---|
171 | if(pHeader)
|
---|
172 | return HGSMIBufferData(pHeader);
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 |
|
---|
176 | DECLINLINE(uint8_t *) HGSMIBufferDataAndChInfoFromOffset (const HGSMIAREA *pArea, HGSMIOFFSET offBuffer, uint16_t * pChInfo)
|
---|
177 | {
|
---|
178 | HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer (pArea, offBuffer);
|
---|
179 | Assert(pHeader);
|
---|
180 | if(pHeader)
|
---|
181 | {
|
---|
182 | *pChInfo = pHeader->u16ChannelInfo;
|
---|
183 | return HGSMIBufferData(pHeader);
|
---|
184 | }
|
---|
185 | return NULL;
|
---|
186 | }
|
---|
187 |
|
---|
188 | HGSMICHANNEL *HGSMIChannelFindById (HGSMICHANNELINFO * pChannelInfo, uint8_t u8Channel);
|
---|
189 |
|
---|
190 | uint32_t HGSMIChecksum (HGSMIOFFSET offBuffer,
|
---|
191 | const HGSMIBUFFERHEADER *pHeader,
|
---|
192 | const HGSMIBUFFERTAIL *pTail);
|
---|
193 |
|
---|
194 | int HGSMIAreaInitialize (HGSMIAREA *pArea,
|
---|
195 | void *pvBase,
|
---|
196 | HGSMISIZE cbArea,
|
---|
197 | HGSMIOFFSET offBase);
|
---|
198 |
|
---|
199 | void HGSMIAreaClear (HGSMIAREA *pArea);
|
---|
200 |
|
---|
201 | DECLINLINE(bool) HGSMIAreaContainsOffset(const HGSMIAREA *pArea, HGSMIOFFSET off)
|
---|
202 | {
|
---|
203 | return off >= pArea->offBase && off - pArea->offBase < pArea->cbArea;
|
---|
204 | }
|
---|
205 |
|
---|
206 | DECLINLINE(bool) HGSMIAreaContainsPointer(const HGSMIAREA *pArea, const void *pv)
|
---|
207 | {
|
---|
208 | return (uintptr_t)pv >= (uintptr_t)pArea->pu8Base && (uintptr_t)pv - (uintptr_t)pArea->pu8Base < pArea->cbArea;
|
---|
209 | }
|
---|
210 |
|
---|
211 | HGSMIOFFSET HGSMIBufferInitializeSingle (const HGSMIAREA *pArea,
|
---|
212 | HGSMIBUFFERHEADER *pHeader,
|
---|
213 | HGSMISIZE cbBuffer,
|
---|
214 | uint8_t u8Channel,
|
---|
215 | uint16_t u16ChannelInfo);
|
---|
216 |
|
---|
217 | int HGSMIHeapSetup (HGSMIHEAP *pHeap,
|
---|
218 | uint32_t u32HeapType,
|
---|
219 | void *pvBase,
|
---|
220 | HGSMISIZE cbArea,
|
---|
221 | HGSMIOFFSET offBase,
|
---|
222 | const HGSMIENV *pEnv);
|
---|
223 |
|
---|
224 | int HGSMIHeapRelocate (HGSMIHEAP *pHeap,
|
---|
225 | uint32_t u32HeapType,
|
---|
226 | void *pvBase,
|
---|
227 | uint32_t offHeapHandle,
|
---|
228 | uintptr_t offDelta,
|
---|
229 | HGSMISIZE cbArea,
|
---|
230 | HGSMIOFFSET offBase);
|
---|
231 |
|
---|
232 | int HGSMIHeapRestoreMA(HGSMIHEAP *pHeap,
|
---|
233 | void *pvBase,
|
---|
234 | HGSMISIZE cbArea,
|
---|
235 | HGSMIOFFSET offBase,
|
---|
236 | uint32_t cBlocks,
|
---|
237 | HGSMIOFFSET *paDescriptors,
|
---|
238 | HGSMISIZE cbMaxBlock,
|
---|
239 | HGSMIENV *pEnv);
|
---|
240 |
|
---|
241 | void HGSMIHeapSetupUninitialized (HGSMIHEAP *pHeap);
|
---|
242 |
|
---|
243 | void HGSMIHeapDestroy (HGSMIHEAP *pHeap);
|
---|
244 |
|
---|
245 | void* HGSMIHeapBufferAlloc (HGSMIHEAP *pHeap,
|
---|
246 | HGSMISIZE cbBuffer);
|
---|
247 |
|
---|
248 | void HGSMIHeapBufferFree(HGSMIHEAP *pHeap,
|
---|
249 | void *pvBuf);
|
---|
250 |
|
---|
251 | void *HGSMIHeapAlloc (HGSMIHEAP *pHeap,
|
---|
252 | HGSMISIZE cbData,
|
---|
253 | uint8_t u8Channel,
|
---|
254 | uint16_t u16ChannelInfo);
|
---|
255 |
|
---|
256 | HGSMIOFFSET HGSMIHeapBufferOffset (HGSMIHEAP *pHeap,
|
---|
257 | void *pvData);
|
---|
258 |
|
---|
259 | void HGSMIHeapFree (HGSMIHEAP *pHeap,
|
---|
260 | void *pvData);
|
---|
261 |
|
---|
262 | DECLINLINE(HGSMIOFFSET) HGSMIHeapOffset(HGSMIHEAP *pHeap)
|
---|
263 | {
|
---|
264 | return pHeap->area.offBase;
|
---|
265 | }
|
---|
266 |
|
---|
267 | #ifdef IN_RING3
|
---|
268 | /* Needed for heap relocation: offset of the heap handle relative to the start of heap area. */
|
---|
269 | DECLINLINE(HGSMIOFFSET) HGSMIHeapHandleLocationOffset(HGSMIHEAP *pHeap)
|
---|
270 | {
|
---|
271 | HGSMIOFFSET offHeapHandle;
|
---|
272 | if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_POINTER)
|
---|
273 | {
|
---|
274 | offHeapHandle = (HGSMIOFFSET)((uintptr_t)pHeap->u.hPtr - (uintptr_t)pHeap->area.pu8Base);
|
---|
275 | }
|
---|
276 | else if (pHeap->u32HeapType == HGSMI_HEAP_TYPE_OFFSET)
|
---|
277 | {
|
---|
278 | offHeapHandle = (HGSMIOFFSET)((uintptr_t)pHeap->u.hOff - (uintptr_t)pHeap->area.pu8Base);
|
---|
279 | }
|
---|
280 | else
|
---|
281 | {
|
---|
282 | offHeapHandle = HGSMIOFFSET_VOID;
|
---|
283 | }
|
---|
284 | return offHeapHandle;
|
---|
285 | }
|
---|
286 | #endif /* IN_RING3 */
|
---|
287 |
|
---|
288 | DECLINLINE(HGSMISIZE) HGSMIHeapSize(HGSMIHEAP *pHeap)
|
---|
289 | {
|
---|
290 | return pHeap->area.cbArea;
|
---|
291 | }
|
---|
292 |
|
---|
293 | int HGSMIChannelRegister (HGSMICHANNELINFO * pChannelInfo,
|
---|
294 | uint8_t u8Channel,
|
---|
295 | const char *pszName,
|
---|
296 | PFNHGSMICHANNELHANDLER pfnChannelHandler,
|
---|
297 | void *pvChannelHandler,
|
---|
298 | HGSMICHANNELHANDLER *pOldHandler);
|
---|
299 |
|
---|
300 | int HGSMIBufferProcess (HGSMIAREA *pArea,
|
---|
301 | HGSMICHANNELINFO * pChannelInfo,
|
---|
302 | HGSMIOFFSET offBuffer);
|
---|
303 | RT_C_DECLS_END
|
---|
304 |
|
---|
305 | #endif /* !___VBox_HGSMI_HGSMI_h */
|
---|
306 |
|
---|