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