1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox stream I/O devices:
|
---|
4 | * Generic char driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | /*******************************************************************************
|
---|
26 | * Header Files *
|
---|
27 | *******************************************************************************/
|
---|
28 | #define LOG_GROUP LOG_GROUP_DRV_CHAR
|
---|
29 | #include <VBox/pdm.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 |
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 |
|
---|
38 | #include "Builtins.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /** Size of the send fifo queue (in bytes) */
|
---|
42 | #define CHAR_MAX_SEND_QUEUE 0x80
|
---|
43 | #define CHAR_MAX_SEND_QUEUE_MASK 0x7f
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Char driver instance data.
|
---|
51 | */
|
---|
52 | typedef struct DRVCHAR
|
---|
53 | {
|
---|
54 | /** Pointer to the driver instance structure. */
|
---|
55 | PPDMDRVINS pDrvIns;
|
---|
56 | /** Pointer to the char port interface of the driver/device above us. */
|
---|
57 | PPDMICHARPORT pDrvCharPort;
|
---|
58 | /** Pointer to the stream interface of the driver below us. */
|
---|
59 | PPDMISTREAM pDrvStream;
|
---|
60 | /** Our char interface. */
|
---|
61 | PDMICHAR IChar;
|
---|
62 | /** Flag to notify the receive thread it should terminate. */
|
---|
63 | volatile bool fShutdown;
|
---|
64 | /** Receive thread ID. */
|
---|
65 | RTTHREAD ReceiveThread;
|
---|
66 | /** Send thread ID. */
|
---|
67 | RTTHREAD SendThread;
|
---|
68 | /** Send event semephore */
|
---|
69 | RTSEMEVENT SendSem;
|
---|
70 |
|
---|
71 | /** Internal send FIFO queue */
|
---|
72 | uint8_t aSendQueue[CHAR_MAX_SEND_QUEUE];
|
---|
73 | uint32_t iSendQueueHead;
|
---|
74 | uint32_t iSendQueueTail;
|
---|
75 |
|
---|
76 | /** Read/write statistics */
|
---|
77 | STAMCOUNTER StatBytesRead;
|
---|
78 | STAMCOUNTER StatBytesWritten;
|
---|
79 | } DRVCHAR, *PDRVCHAR;
|
---|
80 |
|
---|
81 |
|
---|
82 | /** Converts a pointer to DRVCHAR::IChar to a PDRVCHAR. */
|
---|
83 | #define PDMICHAR_2_DRVCHAR(pInterface) ( (PDRVCHAR)((uintptr_t)pInterface - RT_OFFSETOF(DRVCHAR, IChar)) )
|
---|
84 |
|
---|
85 |
|
---|
86 | /* -=-=-=-=- IBase -=-=-=-=- */
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Queries an interface to the driver.
|
---|
90 | *
|
---|
91 | * @returns Pointer to interface.
|
---|
92 | * @returns NULL if the interface was not supported by the driver.
|
---|
93 | * @param pInterface Pointer to this interface structure.
|
---|
94 | * @param enmInterface The requested interface identification.
|
---|
95 | */
|
---|
96 | static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
97 | {
|
---|
98 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
99 | PDRVCHAR pData = PDMINS2DATA(pDrvIns, PDRVCHAR);
|
---|
100 | switch (enmInterface)
|
---|
101 | {
|
---|
102 | case PDMINTERFACE_BASE:
|
---|
103 | return &pDrvIns->IBase;
|
---|
104 | case PDMINTERFACE_CHAR:
|
---|
105 | return &pData->IChar;
|
---|
106 | default:
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /* -=-=-=-=- IChar -=-=-=-=- */
|
---|
113 |
|
---|
114 | /** @copydoc PDMICHAR::pfnWrite */
|
---|
115 | static DECLCALLBACK(int) drvCharWrite(PPDMICHAR pInterface, const void *pvBuf, size_t cbWrite)
|
---|
116 | {
|
---|
117 | PDRVCHAR pData = PDMICHAR_2_DRVCHAR(pInterface);
|
---|
118 | const char *pBuffer = (const char *)pvBuf;
|
---|
119 | int rc = VINF_SUCCESS;
|
---|
120 |
|
---|
121 | LogFlow(("%s: pvBuf=%#p cbWrite=%d\n", __FUNCTION__, pvBuf, cbWrite));
|
---|
122 |
|
---|
123 | for (uint32_t i=0;i<cbWrite;i++)
|
---|
124 | {
|
---|
125 | uint32_t idx = pData->iSendQueueHead;
|
---|
126 |
|
---|
127 | pData->aSendQueue[idx] = pBuffer[i];
|
---|
128 | idx = (idx + 1) & CHAR_MAX_SEND_QUEUE_MASK;
|
---|
129 |
|
---|
130 | STAM_COUNTER_INC(&pData->StatBytesWritten);
|
---|
131 | ASMAtomicXchgU32(&pData->iSendQueueHead, idx);
|
---|
132 | }
|
---|
133 | RTSemEventSignal(pData->SendSem);
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /* -=-=-=-=- receive thread -=-=-=-=- */
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Send thread loop.
|
---|
142 | *
|
---|
143 | * @returns 0 on success.
|
---|
144 | * @param ThreadSelf Thread handle to this thread.
|
---|
145 | * @param pvUser User argument.
|
---|
146 | */
|
---|
147 | static DECLCALLBACK(int) drvCharSendLoop(RTTHREAD ThreadSelf, void *pvUser)
|
---|
148 | {
|
---|
149 | PDRVCHAR pData = (PDRVCHAR)pvUser;
|
---|
150 |
|
---|
151 | for(;;)
|
---|
152 | {
|
---|
153 | int rc = RTSemEventWait(pData->SendSem, RT_INDEFINITE_WAIT);
|
---|
154 | if (VBOX_FAILURE(rc))
|
---|
155 | break;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Write the character to the attached stream (if present).
|
---|
159 | */
|
---|
160 | if ( !pData->fShutdown
|
---|
161 | && pData->pDrvStream)
|
---|
162 | {
|
---|
163 | while (pData->iSendQueueTail != pData->iSendQueueHead)
|
---|
164 | {
|
---|
165 | size_t cbProcessed = 1;
|
---|
166 |
|
---|
167 | rc = pData->pDrvStream->pfnWrite(pData->pDrvStream, &pData->aSendQueue[pData->iSendQueueTail], &cbProcessed);
|
---|
168 | if (VBOX_SUCCESS(rc))
|
---|
169 | {
|
---|
170 | Assert(cbProcessed);
|
---|
171 | pData->iSendQueueTail++;
|
---|
172 | pData->iSendQueueTail &= CHAR_MAX_SEND_QUEUE_MASK;
|
---|
173 | }
|
---|
174 | else if (rc == VERR_TIMEOUT)
|
---|
175 | {
|
---|
176 | /* Normal case, just means that the stream didn't accept a new
|
---|
177 | * character before the timeout elapsed. Just retry. */
|
---|
178 | rc = VINF_SUCCESS;
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | Log(("Write failed with %Vrc; skipping\n", rc));
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else
|
---|
188 | break;
|
---|
189 | }
|
---|
190 |
|
---|
191 | pData->SendThread = NIL_RTTHREAD;
|
---|
192 |
|
---|
193 | return VINF_SUCCESS;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /* -=-=-=-=- receive thread -=-=-=-=- */
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Receive thread loop.
|
---|
201 | *
|
---|
202 | * @returns 0 on success.
|
---|
203 | * @param ThreadSelf Thread handle to this thread.
|
---|
204 | * @param pvUser User argument.
|
---|
205 | */
|
---|
206 | static DECLCALLBACK(int) drvCharReceiveLoop(RTTHREAD ThreadSelf, void *pvUser)
|
---|
207 | {
|
---|
208 | PDRVCHAR pData = (PDRVCHAR)pvUser;
|
---|
209 | char aBuffer[256], *pBuffer;
|
---|
210 | size_t cbRemaining, cbProcessed;
|
---|
211 | int rc;
|
---|
212 |
|
---|
213 | cbRemaining = 0;
|
---|
214 | pBuffer = aBuffer;
|
---|
215 | while (!pData->fShutdown)
|
---|
216 | {
|
---|
217 | if (!cbRemaining)
|
---|
218 | {
|
---|
219 | /* Get block of data from stream driver. */
|
---|
220 | if (pData->pDrvStream)
|
---|
221 | {
|
---|
222 | cbRemaining = sizeof(aBuffer);
|
---|
223 | rc = pData->pDrvStream->pfnRead(pData->pDrvStream, aBuffer, &cbRemaining);
|
---|
224 | AssertRC(rc);
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | cbRemaining = 0;
|
---|
229 | RTThreadSleep(100);
|
---|
230 | }
|
---|
231 | pBuffer = aBuffer;
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | /* Send data to guest. */
|
---|
236 | cbProcessed = cbRemaining;
|
---|
237 | rc = pData->pDrvCharPort->pfnNotifyRead(pData->pDrvCharPort, pBuffer, &cbProcessed);
|
---|
238 | if (VBOX_SUCCESS(rc))
|
---|
239 | {
|
---|
240 | Assert(cbProcessed);
|
---|
241 | pBuffer += cbProcessed;
|
---|
242 | cbRemaining -= cbProcessed;
|
---|
243 | STAM_COUNTER_ADD(&pData->StatBytesRead, cbProcessed);
|
---|
244 | }
|
---|
245 | else if (rc == VERR_TIMEOUT)
|
---|
246 | {
|
---|
247 | /* Normal case, just means that the guest didn't accept a new
|
---|
248 | * character before the timeout elapsed. Just retry. */
|
---|
249 | rc = VINF_SUCCESS;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | AssertRC(rc);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | pData->ReceiveThread = NIL_RTTHREAD;
|
---|
257 |
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | /* -=-=-=-=- driver interface -=-=-=-=- */
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Construct a char driver instance.
|
---|
266 | *
|
---|
267 | * @returns VBox status.
|
---|
268 | * @param pDrvIns The driver instance data.
|
---|
269 | * If the registration structure is needed,
|
---|
270 | * pDrvIns->pDrvReg points to it.
|
---|
271 | * @param pCfgHandle Configuration node handle for the driver. Use this to
|
---|
272 | * obtain the configuration of the driver instance. It's
|
---|
273 | * also found in pDrvIns->pCfgHandle as it's expected to
|
---|
274 | * be used frequently in this function.
|
---|
275 | */
|
---|
276 | static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
277 | {
|
---|
278 | PDRVCHAR pData = PDMINS2DATA(pDrvIns, PDRVCHAR);
|
---|
279 | LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Init basic data members and interfaces.
|
---|
283 | */
|
---|
284 | pData->ReceiveThread = NIL_RTTHREAD;
|
---|
285 | pData->fShutdown = false;
|
---|
286 | /* IBase. */
|
---|
287 | pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
|
---|
288 | /* IChar. */
|
---|
289 | pData->IChar.pfnWrite = drvCharWrite;
|
---|
290 |
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * Get the ICharPort interface of the above driver/device.
|
---|
294 | */
|
---|
295 | pData->pDrvCharPort = (PPDMICHARPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_CHAR_PORT);
|
---|
296 | if (!pData->pDrvCharPort)
|
---|
297 | return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Char#%d has no char port interface above"), pDrvIns->iInstance);
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Attach driver below and query its stream interface.
|
---|
301 | */
|
---|
302 | PPDMIBASE pBase;
|
---|
303 | int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBase);
|
---|
304 | if (VBOX_FAILURE(rc))
|
---|
305 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d failed to attach driver below"), pDrvIns->iInstance);
|
---|
306 | pData->pDrvStream = (PPDMISTREAM)pBase->pfnQueryInterface(pBase, PDMINTERFACE_STREAM);
|
---|
307 | if (!pData->pDrvStream)
|
---|
308 | return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS, N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
|
---|
309 |
|
---|
310 | rc = RTThreadCreate(&pData->ReceiveThread, drvCharReceiveLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Char Receive");
|
---|
311 | if (VBOX_FAILURE(rc))
|
---|
312 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create receive thread"), pDrvIns->iInstance);
|
---|
313 |
|
---|
314 | rc = RTSemEventCreate(&pData->SendSem);
|
---|
315 | AssertRC(rc);
|
---|
316 |
|
---|
317 | rc = RTThreadCreate(&pData->SendThread, drvCharSendLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Char Send");
|
---|
318 | if (VBOX_FAILURE(rc))
|
---|
319 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create send thread"), pDrvIns->iInstance);
|
---|
320 |
|
---|
321 |
|
---|
322 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
|
---|
323 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
|
---|
324 |
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Destruct a char driver instance.
|
---|
331 | *
|
---|
332 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
333 | * any non-VM resources can be freed correctly.
|
---|
334 | *
|
---|
335 | * @param pDrvIns The driver instance data.
|
---|
336 | */
|
---|
337 | static DECLCALLBACK(void) drvCharDestruct(PPDMDRVINS pDrvIns)
|
---|
338 | {
|
---|
339 | PDRVCHAR pData = PDMINS2DATA(pDrvIns, PDRVCHAR);
|
---|
340 |
|
---|
341 | LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
|
---|
342 |
|
---|
343 | pData->fShutdown = true;
|
---|
344 | RTThreadWait(pData->ReceiveThread, 1000, NULL);
|
---|
345 | if (pData->ReceiveThread != NIL_RTTHREAD)
|
---|
346 | LogRel(("Char%d: receive thread did not terminate\n", pDrvIns->iInstance));
|
---|
347 |
|
---|
348 | /* Empty the send queue */
|
---|
349 | pData->iSendQueueTail = pData->iSendQueueHead = 0;
|
---|
350 |
|
---|
351 | RTSemEventSignal(pData->SendSem);
|
---|
352 | RTSemEventDestroy(pData->SendSem);
|
---|
353 | pData->SendSem = NIL_RTSEMEVENT;
|
---|
354 |
|
---|
355 | RTThreadWait(pData->SendThread, 1000, NULL);
|
---|
356 | if (pData->SendThread != NIL_RTTHREAD)
|
---|
357 | LogRel(("Char%d: send thread did not terminate\n", pDrvIns->iInstance));
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Char driver registration record.
|
---|
363 | */
|
---|
364 | const PDMDRVREG g_DrvChar =
|
---|
365 | {
|
---|
366 | /* u32Version */
|
---|
367 | PDM_DRVREG_VERSION,
|
---|
368 | /* szDriverName */
|
---|
369 | "Char",
|
---|
370 | /* pszDescription */
|
---|
371 | "Generic char driver.",
|
---|
372 | /* fFlags */
|
---|
373 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
374 | /* fClass. */
|
---|
375 | PDM_DRVREG_CLASS_CHAR,
|
---|
376 | /* cMaxInstances */
|
---|
377 | ~0,
|
---|
378 | /* cbInstance */
|
---|
379 | sizeof(DRVCHAR),
|
---|
380 | /* pfnConstruct */
|
---|
381 | drvCharConstruct,
|
---|
382 | /* pfnDestruct */
|
---|
383 | drvCharDestruct,
|
---|
384 | /* pfnIOCtl */
|
---|
385 | NULL,
|
---|
386 | /* pfnPowerOn */
|
---|
387 | NULL,
|
---|
388 | /* pfnReset */
|
---|
389 | NULL,
|
---|
390 | /* pfnSuspend */
|
---|
391 | NULL,
|
---|
392 | /* pfnResume */
|
---|
393 | NULL,
|
---|
394 | /* pfnDetach */
|
---|
395 | NULL,
|
---|
396 | /** pfnPowerOff */
|
---|
397 | NULL
|
---|
398 | };
|
---|
399 |
|
---|