VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/Mouse/vboxmouse.c@ 42145

Last change on this file since 42145 was 42043, checked in by vboxsync, 13 years ago

Additions/solaris/vboxmouse: preallocate GR request.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.1 KB
Line 
1/* $Id: vboxmouse.c 42043 2012-07-06 17:01:15Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for Solaris.
4 */
5
6/*
7 * Copyright (C) 2012 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#define LOG_GROUP LOG_GROUP_DRV_MOUSE
28
29/******************************************************************************
30* Header Files *
31******************************************************************************/
32
33#include <VBox/VBoxGuestLib.h>
34#include <VBox/log.h>
35#include <VBox/version.h>
36#include <iprt/assert.h>
37#include <iprt/asm.h>
38
39#ifndef TESTCASE
40# include <sys/modctl.h>
41# include <sys/msio.h>
42# include <sys/stat.h>
43# include <sys/ddi.h>
44# include <sys/strsun.h>
45# include <sys/stropts.h>
46# include <sys/sunddi.h>
47# include <sys/vuid_event.h>
48# include <sys/vuid_wheel.h>
49#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
50#else /* TESTCASE */
51# undef IN_RING3
52# define IN_RING0
53#endif /* TESTCASE */
54
55#ifdef TESTCASE /* Include this last as we . */
56# include "testcase/solaris.h"
57# include <iprt/test.h>
58#endif /* TESTCASE */
59
60
61/******************************************************************************
62* Defined Constants And Macros *
63******************************************************************************/
64
65/** The module name. */
66#define DEVICE_NAME "vboxmouse"
67/** The module description as seen in 'modinfo'. */
68#define DEVICE_DESC "VBoxMouseIntegr"
69
70
71/******************************************************************************
72* Internal functions used in global structures *
73******************************************************************************/
74
75static int vbmsSolAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
76static int vbmsSolDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
77static int vbmsSolGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg,
78 void **ppvResult);
79static int vbmsSolOpen(queue_t *pReadQueue, dev_t *pDev, int fFlag,
80 int fMode, cred_t *pCred);
81static int vbmsSolClose(queue_t *pReadQueue, int fFlag, cred_t *pCred);
82static int vbmsSolWPut(queue_t *pWriteQueue, mblk_t *pMBlk);
83
84
85/******************************************************************************
86* Driver global structures *
87******************************************************************************/
88
89#ifndef TESTCASE /* I see no value in including these in the test. */
90
91/*
92 * mod_info: STREAMS module information.
93 */
94static struct module_info g_vbmsSolModInfo =
95{
96 0, /* module id number */
97 "vboxms",
98 0, /* minimum packet size */
99 INFPSZ, /* maximum packet size accepted */
100 512, /* high water mark for data flow control */
101 128 /* low water mark */
102};
103
104/*
105 * rinit: read queue structure for handling messages coming from below. In
106 * our case this means the host and the virtual hardware, so we do not need
107 * the put and service procedures.
108 */
109static struct qinit g_vbmsSolRInit =
110{
111 NULL, /* put */
112 NULL, /* service thread procedure */
113 vbmsSolOpen,
114 vbmsSolClose,
115 NULL, /* reserved */
116 &g_vbmsSolModInfo,
117 NULL /* module statistics structure */
118};
119
120/*
121 * winit: write queue structure for handling messages coming from above. Above
122 * means user space applications: either Guest Additions user space tools or
123 * applications reading pointer input. Messages from the last most likely pass
124 * through at least the "consms" console mouse streams module which multiplexes
125 * hardware pointer drivers to a single virtual pointer.
126 */
127static struct qinit g_vbmsSolWInit =
128{
129 vbmsSolWPut,
130 NULL, /* service thread procedure */
131 NULL, /* open */
132 NULL, /* close */
133 NULL, /* reserved */
134 &g_vbmsSolModInfo,
135 NULL /* module statistics structure */
136};
137
138/**
139 * streamtab: for drivers that support char/block entry points.
140 */
141static struct streamtab g_vbmsSolStreamTab =
142{
143 &g_vbmsSolRInit,
144 &g_vbmsSolWInit,
145 NULL, /* MUX rinit */
146 NULL /* MUX winit */
147};
148
149/**
150 * cb_ops: for drivers that support char/block entry points
151 */
152static struct cb_ops g_vbmsSolCbOps =
153{
154 nodev, /* open */
155 nodev, /* close */
156 nodev, /* b strategy */
157 nodev, /* b dump */
158 nodev, /* b print */
159 nodev, /* c read */
160 nodev, /* c write */
161 nodev, /* c ioctl */
162 nodev, /* c devmap */
163 nodev, /* c mmap */
164 nodev, /* c segmap */
165 nochpoll, /* c poll */
166 ddi_prop_op, /* property ops */
167 &g_vbmsSolStreamTab,
168 D_MP,
169 CB_REV /* revision */
170};
171
172/**
173 * dev_ops: for driver device operations
174 */
175static struct dev_ops g_vbmsSolDevOps =
176{
177 DEVO_REV, /* driver build revision */
178 0, /* ref count */
179 vbmsSolGetInfo,
180 nulldev, /* identify */
181 nulldev, /* probe */
182 vbmsSolAttach,
183 vbmsSolDetach,
184 nodev, /* reset */
185 &g_vbmsSolCbOps,
186 NULL, /* bus operations */
187 nodev /* power */
188};
189
190/**
191 * modldrv: export driver specifics to the kernel
192 */
193static struct modldrv g_vbmsSolModule =
194{
195 &mod_driverops, /* extern from kernel */
196 DEVICE_DESC " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
197 &g_vbmsSolDevOps
198};
199
200/**
201 * modlinkage: export install/remove/info to the kernel.
202 */
203static struct modlinkage g_vbmsSolModLinkage =
204{
205 MODREV_1, /* loadable module system revision */
206 &g_vbmsSolModule,
207 NULL /* terminate array of linkage structures */
208};
209
210#else /* TESTCASE */
211static void *g_vbmsSolModLinkage;
212#endif /* TESTCASE */
213
214/**
215 * State info for each open file handle.
216 */
217typedef struct
218{
219 /** Device handle. */
220 dev_info_t *pDip;
221 /** Mutex protecting the guest library against multiple initialistation or
222 * uninitialisation. */
223 kmutex_t InitMtx;
224 /** Initialisation counter for the guest library. */
225 size_t cInits;
226 /** The STREAMS write queue which we need for sending messages up to
227 * user-space. */
228 queue_t *pWriteQueue;
229 /** Pre-allocated mouse status VMMDev request for use in the IRQ
230 * handler. */
231 VMMDevReqMouseStatus *pMouseStatusReq;
232 /* The current greatest horizontal pixel offset on the screen, used for
233 * absolute mouse position reporting.
234 */
235 int cMaxScreenX;
236 /* The current greatest vertical pixel offset on the screen, used for
237 * absolute mouse position reporting.
238 */
239 int cMaxScreenY;
240} VBMSSTATE, *PVBMSSTATE;
241
242
243/******************************************************************************
244* Global Variables *
245******************************************************************************/
246
247/** Global driver state. Actually this could be allocated dynamically. */
248static VBMSSTATE g_OpenNodeState /* = { 0 } */;
249
250
251/******************************************************************************
252* Kernel entry points *
253******************************************************************************/
254
255/** Driver initialisation. */
256int _init(void)
257{
258 int rc;
259 LogRelFlow((DEVICE_NAME ": built on " __DATE__ " at " __TIME__ "\n"));
260 mutex_init(&g_OpenNodeState.InitMtx, NULL, MUTEX_DRIVER, NULL);
261 /*
262 * Prevent module autounloading.
263 */
264 modctl_t *pModCtl = mod_getctl(&g_vbmsSolModLinkage);
265 if (pModCtl)
266 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
267 else
268 LogRel((DEVICE_NAME ": failed to disable autounloading!\n"));
269 rc = mod_install(&g_vbmsSolModLinkage);
270
271 LogRel((DEVICE_NAME ": initialisation returning %d.\n", rc));
272 return rc;
273}
274
275
276#ifdef TESTCASE
277/** Simple test of the flow through _init. */
278static void test_init(RTTEST hTest)
279{
280 RTTestSub(hTest, "Testing _init");
281 RTTEST_CHECK(hTest, _init() == 0);
282}
283#endif
284
285
286/** Driver cleanup. */
287int _fini(void)
288{
289 int rc;
290
291 LogRelFlow((DEVICE_NAME ":_fini\n"));
292 rc = mod_remove(&g_vbmsSolModLinkage);
293 mutex_destroy(&g_OpenNodeState.InitMtx);
294
295 return rc;
296}
297
298
299/** Driver identification. */
300int _info(struct modinfo *pModInfo)
301{
302 int rc;
303 LogRelFlow((DEVICE_NAME ":_info\n"));
304 rc = mod_info(&g_vbmsSolModLinkage, pModInfo);
305 LogRelFlow((DEVICE_NAME ":_info returning %d\n", rc));
306 return rc;
307}
308
309
310/******************************************************************************
311* Initialisation entry points *
312******************************************************************************/
313
314/**
315 * Attach entry point, to attach a device to the system or resume it.
316 *
317 * @param pDip The module structure instance.
318 * @param enmCmd Attach type (ddi_attach_cmd_t)
319 *
320 * @return corresponding solaris error code.
321 */
322int vbmsSolAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
323{
324 LogRelFlow((DEVICE_NAME "::Attach\n"));
325 switch (enmCmd)
326 {
327 case DDI_ATTACH:
328 {
329 int rc;
330 int instance = ddi_get_instance(pDip);
331 /* Only one instance supported. */
332 if (!ASMAtomicCmpXchgPtr(&g_OpenNodeState.pDip, pDip, NULL))
333 return DDI_FAILURE;
334 rc = ddi_create_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0);
335 if (rc == DDI_SUCCESS)
336 return DDI_SUCCESS;
337 ASMAtomicWritePtr(&g_OpenNodeState.pDip, NULL);
338 return DDI_FAILURE;
339 }
340
341 case DDI_RESUME:
342 {
343 /** @todo implement resume for guest driver. */
344 return DDI_SUCCESS;
345 }
346
347 default:
348 return DDI_FAILURE;
349 }
350}
351
352
353/**
354 * Detach entry point, to detach a device to the system or suspend it.
355 *
356 * @param pDip The module structure instance.
357 * @param enmCmd Attach type (ddi_attach_cmd_t)
358 *
359 * @return corresponding solaris error code.
360 */
361int vbmsSolDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
362{
363 LogRelFlow((DEVICE_NAME "::Detach\n"));
364 switch (enmCmd)
365 {
366 case DDI_DETACH:
367 {
368 ddi_remove_minor_node(pDip, NULL);
369 ASMAtomicWritePtr(&g_OpenNodeState.pDip, NULL);
370 return DDI_SUCCESS;
371 }
372
373 case DDI_SUSPEND:
374 {
375 /** @todo implement suspend for guest driver. */
376 return DDI_SUCCESS;
377 }
378
379 default:
380 return DDI_FAILURE;
381 }
382}
383
384
385/**
386 * Info entry point, called by solaris kernel for obtaining driver info.
387 *
388 * @param pDip The module structure instance (do not use).
389 * @param enmCmd Information request type.
390 * @param pvArg Type specific argument.
391 * @param ppvResult Where to store the requested info.
392 *
393 * @return corresponding solaris error code.
394 */
395int vbmsSolGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg,
396 void **ppvResult)
397{
398 LogRelFlow((DEVICE_NAME "::GetInfo\n"));
399
400 int rc = DDI_SUCCESS;
401 switch (enmCmd)
402 {
403 case DDI_INFO_DEVT2DEVINFO:
404 *ppvResult = (void *)g_OpenNodeState.pDip;
405 break;
406
407 case DDI_INFO_DEVT2INSTANCE:
408 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_OpenNodeState.pDip);
409 break;
410
411 default:
412 rc = DDI_FAILURE;
413 break;
414 }
415
416 NOREF(pvArg);
417 return rc;
418}
419
420
421/******************************************************************************
422* Main code *
423******************************************************************************/
424
425static void vbmsSolNotify(void *pvState);
426static void vbmsSolVUIDPutAbsEvent(PVBMSSTATE pState, ushort_t cEvent,
427 int cValue);
428
429/**
430 * Open callback for the read queue, which we use as a generic device open
431 * handler.
432 */
433int vbmsSolOpen(queue_t *pReadQueue, dev_t *pDev, int fFlag, int fMode,
434 cred_t *pCred)
435{
436 PVBMSSTATE pState = NULL;
437 int rc = VINF_SUCCESS;
438
439 NOREF(fFlag);
440 NOREF(pCred);
441 LogRelFlow((DEVICE_NAME "::Open\n"));
442
443 /*
444 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
445 */
446 mutex_enter(&g_OpenNodeState.InitMtx);
447 if (!g_OpenNodeState.cInits)
448 {
449 rc = VbglInit();
450 if (RT_SUCCESS(rc))
451 {
452 rc = VbglGRAlloc((VMMDevRequestHeader **)
453 &g_OpenNodeState.pMouseStatusReq,
454 sizeof(*g_OpenNodeState.pMouseStatusReq),
455 VMMDevReq_GetMouseStatus);
456 if (RT_FAILURE(rc))
457 VbglTerminate();
458 }
459 }
460 ++g_OpenNodeState.cInits;
461 mutex_exit(&g_OpenNodeState.InitMtx);
462 if (RT_FAILURE(rc))
463 {
464 cmn_err(CE_NOTE, "_init: VbglInit failed. rc=%d\n", rc);
465 return EINVAL;
466 }
467 /*
468 * Sanity check on the mode parameter - only open as a driver, not a
469 * module, and we do cloning ourselves.
470 */
471 if (fMode)
472 {
473 LogRelFlow((DEVICE_NAME "::Open: invalid attempt to clone device."));
474 return EINVAL;
475 }
476
477 if (!ASMAtomicCmpXchgPtr(&g_OpenNodeState.pWriteQueue,
478 WR(pReadQueue), NULL))
479 {
480 LogRelFlow((DEVICE_NAME "::Open: only one instance allowed."));
481 return ENXIO;
482 }
483 pState = &g_OpenNodeState;
484
485 /*
486 * Create a new session.
487 */
488 if (VbglIsReady())
489 {
490 /* Initialise user data for the queues to our state and vice-versa. */
491 WR(pReadQueue)->q_ptr = (char *)pState;
492 pReadQueue->q_ptr = (char *)pState;
493 /* Enable our IRQ handler. */
494 VbglSetMouseNotifyCallback(vbmsSolNotify, (void *)pState);
495 qprocson(pReadQueue);
496 LogRel((DEVICE_NAME "::Open\n"));
497 return 0;
498 }
499
500 /* Failed, clean up. */
501 ASMAtomicWriteNullPtr(&pState->pWriteQueue);
502
503 LogRel((DEVICE_NAME "::Open: VBoxGuestCreateUserSession failed. rc=EFAULT\n"));
504 return EFAULT;
505}
506
507
508/**
509 * Notification callback, called when the VBoxGuest mouse pointer is moved.
510 * We send a VUID event up to user space. We may send a miscalculated event
511 * if a resolution change is half-way through, but that is pretty much to be
512 * expected, so we won't worry about it.
513 */
514void vbmsSolNotify(void *pvState)
515{
516 PVBMSSTATE pState = (PVBMSSTATE)pvState;
517 int rc;
518 LogRelFlow((DEVICE_NAME "::NativeISRMousePollEvent:\n"));
519
520 pState->pMouseStatusReq->mouseFeatures = 0;
521 pState->pMouseStatusReq->pointerXPos = 0;
522 pState->pMouseStatusReq->pointerYPos = 0;
523 rc = VbglGRPerform(&pState->pMouseStatusReq->header);
524 if (RT_SUCCESS(rc))
525 {
526 int cMaxScreenX = pState->cMaxScreenX;
527 int cMaxScreenY = pState->cMaxScreenY;
528 int x = pState->pMouseStatusReq->pointerXPos;
529 int y = pState->pMouseStatusReq->pointerYPos;
530
531 if (cMaxScreenX && cMaxScreenY)
532 {
533 vbmsSolVUIDPutAbsEvent(pState, LOC_X_ABSOLUTE,
534 x * cMaxScreenX / VMMDEV_MOUSE_RANGE_MAX);
535 vbmsSolVUIDPutAbsEvent(pState, LOC_Y_ABSOLUTE,
536 y * cMaxScreenY / VMMDEV_MOUSE_RANGE_MAX);
537 }
538 }
539}
540
541
542void vbmsSolVUIDPutAbsEvent(PVBMSSTATE pState, ushort_t cEvent,
543 int cValue)
544{
545 queue_t *pReadQueue = RD(pState->pWriteQueue);
546 mblk_t *pMBlk = allocb(sizeof(Firm_event), BPRI_HI);
547 Firm_event *pEvent;
548 AssertReturnVoid(cEvent == LOC_X_ABSOLUTE || cEvent == LOC_Y_ABSOLUTE);
549 if (!pMBlk)
550 return; /* If kernel memory is short a missed event is acceptable! */
551 pEvent = (Firm_event *)pMBlk->b_wptr;
552 pEvent->id = cEvent;
553 pEvent->pair_type = FE_PAIR_DELTA;
554 pEvent->pair = cEvent == LOC_X_ABSOLUTE ? LOC_X_DELTA : LOC_Y_DELTA;
555 pEvent->value = cValue;
556 uniqtime32(&pEvent->time);
557 pMBlk->b_wptr += sizeof(Firm_event);
558 /* Put the message on the queue immediately if it is not blocked. */
559 if (canput(pReadQueue->q_next))
560 putnext(pReadQueue, pMBlk);
561 // else
562 // putq(pReadQueue, pMBlk);
563}
564
565
566/**
567 * Close callback for the read queue, which we use as a generic device close
568 * handler.
569 */
570int vbmsSolClose(queue_t *pReadQueue, int fFlag, cred_t *pCred)
571{
572 PVBMSSTATE pState = (PVBMSSTATE)pReadQueue->q_ptr;
573
574 LogRelFlow((DEVICE_NAME "::Close\n"));
575 NOREF(fFlag);
576 NOREF(pCred);
577
578 if (!pState)
579 {
580 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
581 return EFAULT;
582 }
583
584 VbglSetMouseStatus(0);
585 /* Disable our IRQ handler. */
586 VbglSetMouseNotifyCallback(NULL, NULL);
587 qprocsoff(pReadQueue);
588
589 /*
590 * Close the session.
591 */
592 ASMAtomicWriteNullPtr(&pState->pWriteQueue);
593 pReadQueue->q_ptr = NULL;
594 mutex_enter(&g_OpenNodeState.InitMtx);
595 --g_OpenNodeState.cInits;
596 if (!g_OpenNodeState.cInits)
597 {
598 VbglGRFree(&g_OpenNodeState.pMouseStatusReq->header);
599 VbglTerminate();
600 }
601 mutex_exit(&g_OpenNodeState.InitMtx);
602 return 0;
603}
604
605
606#ifdef TESTCASE
607/** Simple test of vbmsSolOpen and vbmsSolClose. */
608static void testOpenClose(RTTEST hTest)
609{
610 queue_t aQueues[2];
611 dev_t device = 0;
612 int rc;
613
614 RTTestSub(hTest, "Testing vbmsSolOpen and vbmsSolClose");
615 RT_ZERO(g_OpenNodeState);
616 RT_ZERO(aQueues);
617 doInitQueues(&aQueues[0]);
618 rc = vbmsSolOpen(RD(&aQueues[0]), &device, 0, 0, NULL);
619 RTTEST_CHECK(hTest, rc == 0);
620 RTTEST_CHECK(hTest, g_OpenNodeState.pWriteQueue == WR(&aQueues[0]));
621 vbmsSolClose(RD(&aQueues[0]), 0, NULL);
622}
623#endif
624
625
626/* Helper for vbmsSolWPut. */
627static int vbmsSolDispatchIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk);
628
629/**
630 * Handler for messages sent from above (user-space and upper modules) which
631 * land in our write queue.
632 */
633int vbmsSolWPut(queue_t *pWriteQueue, mblk_t *pMBlk)
634{
635 LogRelFlowFunc((DEVICE_NAME "::\n"));
636 switch (pMBlk->b_datap->db_type)
637 {
638 case M_FLUSH:
639 /* Flush the write queue if so requested. */
640 if (*pMBlk->b_rptr & FLUSHW)
641 flushq(pWriteQueue, FLUSHDATA);
642
643 /* Flush the read queue if so requested. */
644 if (*pMBlk->b_rptr & FLUSHR)
645 flushq(RD(pWriteQueue), FLUSHDATA);
646
647 /* We have no one below us to pass the message on to. */
648 return 0;
649 /* M_IOCDATA is additional data attached to (at least) transparent
650 * IOCtls. We handle the two together here and separate them further
651 * down. */
652 case M_IOCTL:
653 case M_IOCDATA:
654 {
655 int err = vbmsSolDispatchIOCtl(pWriteQueue, pMBlk);
656 if (!err)
657 qreply(pWriteQueue, pMBlk);
658 else
659 miocnak(pWriteQueue, pMBlk, 0, err);
660 break;
661 }
662 }
663 return 0;
664}
665
666
667#ifdef TESTCASE
668/* Constants, definitions and test functions for testWPut. */
669static const int g_ccTestWPutFirmEvent = VUID_FIRM_EVENT;
670# define PVGFORMAT (&g_ccTestWPutFirmEvent)
671# define CBGFORMAT (sizeof(g_ccTestWPutFirmEvent))
672static const Ms_screen_resolution g_TestResolution = { 640, 480 };
673# define PMSIOSRES (&g_TestResolution)
674# define CBMSIOSRES (sizeof(g_TestResolution))
675
676static inline bool testSetResolution(RTTEST hTest, queue_t *pWriteQueue,
677 struct msgb *pMBlk)
678{
679 PVBMSSTATE pState = (PVBMSSTATE)pWriteQueue->q_ptr;
680 RTTEST_CHECK_MSG_RET(hTest, pState->cMaxScreenX
681 == g_TestResolution.width - 1,
682 (hTest, "pState->cMaxScreenX=%d\n",
683 pState->cMaxScreenX), false);
684 RTTEST_CHECK_MSG_RET(hTest, pState->cMaxScreenY
685 == g_TestResolution.height - 1,
686 (hTest, "pState->cMaxScreenY=%d\n",
687 pState->cMaxScreenY), false);
688 return true;
689}
690
691/** Data table for testWPut. */
692static struct
693{
694 int iIOCCmd;
695 size_t cbData;
696 const void *pvDataIn;
697 size_t cbDataIn;
698 const void *pvDataOut;
699 size_t cbDataOut;
700 int rcExp;
701 bool (*pfnExtra)(RTTEST hTest, queue_t *pWriteQueue, struct msgb *pMBlk);
702 bool fCanTransparent;
703} g_asTestWPut[] =
704{
705 /* iIOCCmd cbData pvDataIn cbDataIn
706 pvDataOut cbDataOut rcExp pfnExtra fCanTransparent */
707 { VUIDGFORMAT, sizeof(int), NULL, 0,
708 PVGFORMAT, CBGFORMAT, 0, NULL, true },
709 { VUIDGFORMAT, sizeof(int) - 1, NULL, 0,
710 NULL, 0, EINVAL, NULL, false },
711 { VUIDGFORMAT, sizeof(int) + 1, NULL, 0,
712 PVGFORMAT, CBGFORMAT, 0, NULL, true },
713 { VUIDSFORMAT, sizeof(int), PVGFORMAT, CBGFORMAT,
714 NULL, 0, 0, NULL, true },
715 { MSIOSRESOLUTION, CBMSIOSRES, PMSIOSRES, CBMSIOSRES,
716 NULL, 0, 0, testSetResolution, true },
717 { VUIDGWHEELINFO, 0, NULL, 0,
718 NULL, 0, EINVAL, NULL, true }
719};
720
721# undef PVGFORMAT
722# undef CBGFORMAT
723# undef PMSIOSRES
724# undef CBMSIOSRES
725
726/* Helpers for testWPut. */
727static void testWPutStreams(RTTEST hTest, unsigned i);
728static void testWPutTransparent(RTTEST hTest, unsigned i);
729static void testWPutIOCDataIn(RTTEST hTest, unsigned i);
730static void testWPutIOCDataOut(RTTEST hTest, unsigned i);
731
732/** Test WPut's handling of different IOCtls, which is bulk of the logic in
733 * this file. */
734static void testWPut(RTTEST hTest)
735{
736 unsigned i;
737
738 RTTestSub(hTest, "Testing vbmsWPut");
739 for (i = 0; i < RT_ELEMENTS(g_asTestWPut); ++i)
740 {
741 AssertReturnVoid(g_asTestWPut[i].cbDataIn <= g_asTestWPut[i].cbData);
742 AssertReturnVoid(g_asTestWPut[i].cbDataOut <= g_asTestWPut[i].cbData);
743 testWPutStreams(hTest, i);
744 if (g_asTestWPut[i].fCanTransparent)
745 testWPutTransparent(hTest, i);
746 if (g_asTestWPut[i].fCanTransparent && g_asTestWPut[i].cbDataIn)
747 testWPutIOCDataIn(hTest, i);
748 if (g_asTestWPut[i].fCanTransparent && g_asTestWPut[i].cbDataOut)
749 testWPutIOCDataOut(hTest, i);
750 }
751}
752
753
754#define MSG_DATA_SIZE 1024
755
756/** Simulate sending a streams IOCtl to WPut with the parameters from table
757 * line @a i. */
758void testWPutStreams(RTTEST hTest, unsigned i)
759{
760 queue_t aQueues[2];
761 dev_t device = 0;
762 struct msgb *pMBlk = allocb(sizeof(struct iocblk), BPRI_MED);
763 struct msgb *pMBlkCont = allocb(MSG_DATA_SIZE, BPRI_MED);
764 struct iocblk *pIOCBlk = pMBlk ? (struct iocblk *)pMBlk->b_rptr : NULL;
765 int rc, cFormat = 0;
766
767 AssertReturnVoid(pMBlk);
768 AssertReturnVoidStmt(pMBlkCont, freemsg(pMBlk));
769 RT_ZERO(aQueues);
770 doInitQueues(&aQueues[0]);
771 rc = vbmsSolOpen(RD(&aQueues[0]), &device, 0, 0, NULL);
772 RTTEST_CHECK_MSG(hTest, rc == 0, (hTest, "i=%u, rc=%d\n", i, rc));
773 RTTEST_CHECK_MSG(hTest, g_OpenNodeState.pWriteQueue
774 == WR(&aQueues[0]), (hTest, "i=%u\n", i));
775 pMBlk->b_datap->db_type = M_IOCTL;
776 pIOCBlk->ioc_cmd = g_asTestWPut[i].iIOCCmd;
777 pIOCBlk->ioc_count = g_asTestWPut[i].cbData;
778 AssertReturnVoid(g_asTestWPut[i].cbData <= MSG_DATA_SIZE);
779 memcpy(pMBlkCont->b_rptr, g_asTestWPut[i].pvDataIn,
780 g_asTestWPut[i].cbDataIn);
781 pMBlk->b_cont = pMBlkCont;
782 rc = vbmsSolWPut(WR(&aQueues[0]), pMBlk);
783 RTTEST_CHECK_MSG(hTest, pIOCBlk->ioc_error == g_asTestWPut[i].rcExp,
784 (hTest, "i=%u, IOCBlk.ioc_error=%d\n", i,
785 pIOCBlk->ioc_error));
786 RTTEST_CHECK_MSG(hTest, pIOCBlk->ioc_count == g_asTestWPut[i].cbDataOut,
787 (hTest, "i=%u, ioc_count=%u\n", i, pIOCBlk->ioc_count));
788 RTTEST_CHECK_MSG(hTest, !memcmp(pMBlkCont->b_rptr,
789 g_asTestWPut[i].pvDataOut,
790 g_asTestWPut[i].cbDataOut),
791 (hTest, "i=%u\n", i));
792 /* Hack to ensure that miocpullup() gets called when needed. */
793 if (g_asTestWPut[i].cbData > 0)
794 RTTEST_CHECK_MSG(hTest, pMBlk->b_flag == 1, (hTest, "i=%u\n", i));
795 if (!g_asTestWPut[i].rcExp)
796 RTTEST_CHECK_MSG(hTest, RD(&aQueues[0])->q_first == pMBlk,
797 (hTest, "i=%u\n", i));
798 if (g_asTestWPut[i].pfnExtra)
799 if (!g_asTestWPut[i].pfnExtra(hTest, WR(&aQueues[0]), pMBlk))
800 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Called from %s.\n",
801 __PRETTY_FUNCTION__);
802 vbmsSolClose(RD(&aQueues[1]), 0, NULL);
803 freemsg(pMBlk);
804}
805
806
807#define USER_ADDRESS 0xfeedbacc
808
809/** Simulate sending a transparent IOCtl to WPut with the parameters from table
810 * line @a i. */
811void testWPutTransparent(RTTEST hTest, unsigned i)
812{
813 queue_t aQueues[2];
814 dev_t device = 0;
815 struct msgb *pMBlk = allocb(sizeof(struct iocblk), BPRI_MED);
816 struct msgb *pMBlkCont = allocb(sizeof(void *), BPRI_MED);
817 struct iocblk *pIOCBlk = pMBlk ? (struct iocblk *)pMBlk->b_rptr : NULL;
818 struct copyreq *pCopyReq;
819 int rc, cFormat = 0;
820
821 /* if (g_asTestWPut[i].cbDataIn == 0 && g_asTestWPut[i].cbDataOut != 0)
822 return; */ /* This case will be handled once the current ones work. */
823 AssertReturnVoid(pMBlk);
824 AssertReturnVoidStmt(pMBlkCont, freemsg(pMBlk));
825 RT_ZERO(aQueues);
826 doInitQueues(&aQueues[0]);
827 rc = vbmsSolOpen(RD(&aQueues[0]), &device, 0, 0, NULL);
828 RTTEST_CHECK_MSG(hTest, rc == 0, (hTest, "i=%u, rc=%d\n", i, rc));
829 RTTEST_CHECK_MSG(hTest, g_OpenNodeState.pWriteQueue
830 == WR(&aQueues[0]), (hTest, "i=%u\n", i));
831 pMBlk->b_datap->db_type = M_IOCTL;
832 pIOCBlk->ioc_cmd = g_asTestWPut[i].iIOCCmd;
833 pIOCBlk->ioc_count = TRANSPARENT;
834 *(void **)pMBlkCont->b_rptr = (void *)USER_ADDRESS;
835 pMBlk->b_cont = pMBlkCont;
836 rc = vbmsSolWPut(WR(&aQueues[0]), pMBlk);
837 pCopyReq = (struct copyreq *)pMBlk->b_rptr;
838 RTTEST_CHECK_MSG(hTest, ( ( g_asTestWPut[i].cbDataIn
839 && (pMBlk->b_datap->db_type == M_COPYIN))
840 || ( g_asTestWPut[i].cbDataOut
841 && (pMBlk->b_datap->db_type == M_COPYOUT))
842 || ( (g_asTestWPut[i].rcExp == 0)
843 && pMBlk->b_datap->db_type == M_IOCACK)
844 || (pMBlk->b_datap->db_type == M_IOCNAK)),
845 (hTest, "i=%u, db_type=%u\n", i,
846 (unsigned) pMBlk->b_datap->db_type));
847 /* Our TRANSPARENT IOCtls can only return non-zero if they have no payload.
848 * Others should either return zero or be non-TRANSPARENT only. */
849 if (pMBlk->b_datap->db_type == M_IOCNAK)
850 RTTEST_CHECK_MSG(hTest, pIOCBlk->ioc_error == g_asTestWPut[i].rcExp,
851 (hTest, "i=%u, IOCBlk.ioc_error=%d\n", i,
852 pIOCBlk->ioc_error));
853 if (g_asTestWPut[i].cbData)
854 {
855 RTTEST_CHECK_MSG(hTest, pCopyReq->cq_addr == (char *)USER_ADDRESS,
856 (hTest, "i=%u, cq_addr=%p\n", i, pCopyReq->cq_addr));
857 RTTEST_CHECK_MSG( hTest, pCopyReq->cq_size
858 == g_asTestWPut[i].cbDataIn
859 ? g_asTestWPut[i].cbDataIn
860 : g_asTestWPut[i].cbDataOut,
861 (hTest, "i=%u, cq_size=%llu\n", i,
862 (unsigned long long)pCopyReq->cq_size));
863 }
864 /* Implementation detail - check that the private pointer is correctly
865 * set to the user address *for two direction IOCtls* or NULL otherwise. */
866 if (g_asTestWPut[i].cbDataIn && g_asTestWPut[i].cbDataOut)
867 RTTEST_CHECK_MSG(hTest, pCopyReq->cq_private == (mblk_t *)USER_ADDRESS,
868 (hTest, "i=%u, cq_private=%p\n", i,
869 pCopyReq->cq_private));
870 else if ( (pMBlk->b_datap->db_type == M_COPYIN)
871 || (pMBlk->b_datap->db_type == M_COPYOUT))
872 RTTEST_CHECK_MSG(hTest, !pCopyReq->cq_private,
873 (hTest, "i=%u, cq_private=%p\n", i,
874 pCopyReq->cq_private));
875 if (!g_asTestWPut[i].rcExp)
876 RTTEST_CHECK_MSG(hTest, RD(&aQueues[0])->q_first == pMBlk,
877 (hTest, "i=%u\n", i));
878 if (g_asTestWPut[i].pfnExtra && !g_asTestWPut[i].cbData)
879 if (!g_asTestWPut[i].pfnExtra(hTest, WR(&aQueues[0]), pMBlk))
880 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Called from %s.\n",
881 __PRETTY_FUNCTION__);
882 vbmsSolClose(RD(&aQueues[1]), 0, NULL);
883 freemsg(pMBlk);
884}
885
886
887/** Simulate sending follow-on IOCData messages to a transparent IOCtl to WPut
888 * with the parameters from table line @a i. */
889void testWPutIOCDataIn(RTTEST hTest, unsigned i)
890{
891 queue_t aQueues[2];
892 dev_t device = 0;
893 struct msgb *pMBlk = allocb(sizeof(struct copyresp), BPRI_MED);
894 struct msgb *pMBlkCont = allocb(MSG_DATA_SIZE, BPRI_MED);
895 struct copyresp *pCopyResp = pMBlk ? (struct copyresp *)pMBlk->b_rptr
896 : NULL;
897 void *pvData = pMBlkCont ? pMBlkCont->b_rptr : NULL;
898 struct copyreq *pCopyReq;
899 int rc, cFormat = 0;
900
901 AssertReturnVoid(pMBlk);
902 AssertReturnVoidStmt(pMBlkCont, freemsg(pMBlk));
903 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%s: i=%u\n", __PRETTY_FUNCTION__,
904 i);
905 AssertReturnVoidStmt(g_asTestWPut[i].cbDataIn, freemsg(pMBlk));
906 RT_ZERO(aQueues);
907 doInitQueues(&aQueues[0]);
908 rc = vbmsSolOpen(RD(&aQueues[0]), &device, 0, 0, NULL);
909 RTTEST_CHECK_MSG(hTest, rc == 0, (hTest, "i=%u, rc=%d\n", i, rc));
910 RTTEST_CHECK_MSG(hTest, g_OpenNodeState.pWriteQueue
911 == WR(&aQueues[0]), (hTest, "i=%u\n", i));
912 pMBlk->b_datap->db_type = M_IOCDATA;
913 pCopyResp->cp_cmd = g_asTestWPut[i].iIOCCmd;
914 if (g_asTestWPut[i].cbDataOut)
915 pCopyResp->cp_private = USER_ADDRESS;
916 AssertReturnVoid(g_asTestWPut[i].cbData <= MSG_DATA_SIZE);
917 memcpy(pMBlkCont->b_rptr, g_asTestWPut[i].pvDataIn, g_asTestWPut[i].cbDataIn);
918 pMBlk->b_cont = pMBlkCont;
919 rc = vbmsSolWPut(WR(&aQueues[0]), pMBlk);
920 pCopyReq = (struct copyreq *)pMBlk->b_rptr;
921 RTTEST_CHECK_MSG(hTest, ( ( g_asTestWPut[i].cbDataOut
922 && (pMBlk->b_datap->db_type == M_COPYOUT))
923 || ( (g_asTestWPut[i].rcExp == 0)
924 && pMBlk->b_datap->db_type == M_IOCACK)
925 || (pMBlk->b_datap->db_type == M_IOCNAK)),
926 (hTest, "i=%u, db_type=%u\n", i,
927 (unsigned) pMBlk->b_datap->db_type));
928 if (g_asTestWPut[i].cbDataOut)
929 {
930 RTTEST_CHECK_MSG(hTest, pCopyReq->cq_addr == (char *)pvData,
931 (hTest, "i=%u, cq_addr=%p\n", i, pCopyReq->cq_addr));
932 RTTEST_CHECK_MSG(hTest, pCopyReq->cq_size == g_asTestWPut[i].cbData,
933 (hTest, "i=%u, cq_size=%llu\n", i,
934 (unsigned long long)pCopyReq->cq_size));
935 RTTEST_CHECK_MSG(hTest, !memcmp(pvData, g_asTestWPut[i].pvDataOut,
936 g_asTestWPut[i].cbDataOut),
937 (hTest, "i=%u\n", i));
938 }
939 RTTEST_CHECK_MSG(hTest, !pCopyReq->cq_private,
940 (hTest, "i=%u, cq_private=%p\n", i,
941 pCopyReq->cq_private));
942 if (!g_asTestWPut[i].rcExp)
943 RTTEST_CHECK_MSG(hTest, RD(&aQueues[0])->q_first == pMBlk,
944 (hTest, "i=%u\n", i));
945 if (g_asTestWPut[i].pfnExtra && !g_asTestWPut[i].cbDataOut)
946 if (!g_asTestWPut[i].pfnExtra(hTest, WR(&aQueues[0]), pMBlk))
947 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Called from %s.\n",
948 __PRETTY_FUNCTION__);
949 vbmsSolClose(RD(&aQueues[1]), 0, NULL);
950 freemsg(pMBlk);
951}
952
953
954/** Simulate sending follow-on IOCData messages to a transparent IOCtl to WPut
955 * with the parameters from table line @a i. */
956void testWPutIOCDataOut(RTTEST hTest, unsigned i)
957{
958 queue_t aQueues[2];
959 dev_t device = 0;
960 struct msgb *pMBlk = allocb(sizeof(struct copyresp), BPRI_MED);
961 struct copyresp *pCopyResp = pMBlk ? (struct copyresp *)pMBlk->b_rptr
962 : NULL;
963 int rc, cFormat = 0;
964
965 AssertReturnVoid(pMBlk);
966 AssertReturnVoidStmt(g_asTestWPut[i].cbDataOut, freemsg(pMBlk));
967 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%s: i=%u\n", __PRETTY_FUNCTION__,
968 i);
969 RT_ZERO(aQueues);
970 doInitQueues(&aQueues[0]);
971 rc = vbmsSolOpen(RD(&aQueues[0]), &device, 0, 0, NULL);
972 RTTEST_CHECK_MSG(hTest, rc == 0, (hTest, "i=%u, rc=%d\n", i, rc));
973 RTTEST_CHECK_MSG(hTest, g_OpenNodeState.pWriteQueue
974 == WR(&aQueues[0]), (hTest, "i=%u\n", i));
975 pMBlk->b_datap->db_type = M_IOCDATA;
976 pCopyResp->cp_cmd = g_asTestWPut[i].iIOCCmd;
977 rc = vbmsSolWPut(WR(&aQueues[0]), pMBlk);
978 RTTEST_CHECK_MSG(hTest, pMBlk->b_datap->db_type == M_IOCACK,
979 (hTest, "i=%u, db_type=%u\n", i,
980 (unsigned) pMBlk->b_datap->db_type));
981 if (!g_asTestWPut[i].rcExp)
982 RTTEST_CHECK_MSG(hTest, RD(&aQueues[0])->q_first == pMBlk,
983 (hTest, "i=%u\n", i));
984 vbmsSolClose(RD(&aQueues[1]), 0, NULL);
985 freemsg(pMBlk);
986}
987#endif
988
989
990/** Data transfer direction of an IOCtl. This is used for describing
991 * transparent IOCtls, and @a UNSPECIFIED is not a valid value for them. */
992enum IOCTLDIRECTION
993{
994 /** This IOCtl transfers no data. */
995 NONE,
996 /** This IOCtl only transfers data from user to kernel. */
997 IN,
998 /** This IOCtl only transfers data from kernel to user. */
999 OUT,
1000 /** This IOCtl transfers data from user to kernel and back. */
1001 BOTH,
1002 /** We aren't saying anything about how the IOCtl transfers data. */
1003 UNSPECIFIED
1004};
1005
1006/**
1007 * IOCtl handler function.
1008 * @returns 0 on success, error code on failure.
1009 * @param iCmd The IOCtl command number.
1010 * @param pvData Buffer for the user data.
1011 * @param cbBuffer Size of the buffer in @a pvData or zero.
1012 * @param pcbData Where to set the size of the data returned. Required for
1013 * handlers which return data.
1014 * @param prc Where to store the return code. Default is zero. Only
1015 * used for IOCtls without data for convenience of
1016 * implemention.
1017 */
1018typedef int FNVBMSSOLIOCTL(PVBMSSTATE pState, int iCmd, void *pvData,
1019 size_t cbBuffer, size_t *pcbData, int *prc);
1020typedef FNVBMSSOLIOCTL *PFNVBMSSOLIOCTL;
1021
1022/* Helpers for vbmsSolDispatchIOCtl. */
1023static int vbmsSolHandleIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1024 PFNVBMSSOLIOCTL pfnHandler,
1025 int iCmd, size_t cbTransparent,
1026 enum IOCTLDIRECTION enmDirection);
1027static int vbmsSolVUIDIOCtl(PVBMSSTATE pState, int iCmd, void *pvData,
1028 size_t cbBuffer, size_t *pcbData, int *prc);
1029
1030/** Table of supported VUID IOCtls. */
1031struct
1032{
1033 /** The IOCtl number. */
1034 int iCmd;
1035 /** The size of the buffer which needs to be copied between user and kernel
1036 * space, or zero if unknown (must be known for tranparent IOCtls). */
1037 size_t cbBuffer;
1038 /** The direction the buffer data needs to be copied. This must be
1039 * specified for transparent IOCtls. */
1040 enum IOCTLDIRECTION enmDirection;
1041} g_aVUIDIOCtlDescriptions[] =
1042{
1043 { VUIDGFORMAT, sizeof(int), OUT },
1044 { VUIDSFORMAT, sizeof(int), IN },
1045 { VUIDGADDR, 0, UNSPECIFIED },
1046 { VUIDGADDR, 0, UNSPECIFIED },
1047 { MSIOGETPARMS, sizeof(Ms_parms), OUT },
1048 { MSIOSETPARMS, sizeof(Ms_parms), IN },
1049 { MSIOSRESOLUTION, sizeof(Ms_screen_resolution), IN },
1050 { MSIOBUTTONS, sizeof(int), OUT },
1051 { VUIDGWHEELCOUNT, sizeof(int), OUT },
1052 { VUIDGWHEELINFO, 0, UNSPECIFIED },
1053 { VUIDGWHEELSTATE, 0, UNSPECIFIED },
1054 { VUIDSWHEELSTATE, 0, UNSPECIFIED }
1055};
1056
1057/**
1058 * Handle a STREAMS IOCtl message for our driver on the write stream. This
1059 * function takes care of the IOCtl logic only and does not call qreply() or
1060 * miocnak() at all - the caller must call these on success or failure
1061 * respectively.
1062 * @returns 0 on success or the IOCtl error code on failure.
1063 * @param pWriteQueue pointer to the STREAMS write queue structure.
1064 * @param pMBlk pointer to the STREAMS message block structure.
1065 */
1066static int vbmsSolDispatchIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk)
1067{
1068 struct iocblk *pIOCBlk = (struct iocblk *)pMBlk->b_rptr;
1069 int iCmd = pIOCBlk->ioc_cmd, iCmdType = iCmd & (0xff << 8);
1070 size_t cbBuffer;
1071 enum IOCTLDIRECTION enmDirection;
1072
1073 LogRelFlowFunc((DEVICE_NAME "::iCmdType=%c, iCmd=0x%x\n",
1074 (char) (iCmdType >> 8), (unsigned)iCmd));
1075 switch (iCmdType)
1076 {
1077 case MSIOC:
1078 case VUIOC:
1079 {
1080 unsigned i;
1081
1082 for (i = 0; i < RT_ELEMENTS(g_aVUIDIOCtlDescriptions); ++i)
1083 if (g_aVUIDIOCtlDescriptions[i].iCmd == iCmd)
1084 {
1085 cbBuffer = g_aVUIDIOCtlDescriptions[i].cbBuffer;
1086 enmDirection = g_aVUIDIOCtlDescriptions[i].enmDirection;
1087 return vbmsSolHandleIOCtl(pWriteQueue, pMBlk,
1088 vbmsSolVUIDIOCtl, iCmd,
1089 cbBuffer, enmDirection);
1090 }
1091 return EINVAL;
1092 }
1093 default:
1094 return ENOTTY;
1095 }
1096}
1097
1098
1099/* Helpers for vbmsSolHandleIOCtl. */
1100static int vbmsSolHandleIOCtlData(queue_t *pWriteQueue, mblk_t *pMBlk,
1101 PFNVBMSSOLIOCTL pfnHandler, int iCmd,
1102 size_t cbTransparent,
1103 enum IOCTLDIRECTION enmDirection);
1104
1105static int vbmsSolHandleTransparentIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1106 PFNVBMSSOLIOCTL pfnHandler,
1107 int iCmd, size_t cbTransparent,
1108 enum IOCTLDIRECTION enmDirection);
1109
1110static int vbmsSolHandleIStrIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1111 PFNVBMSSOLIOCTL pfnHandler, int iCmd);
1112
1113/**
1114 * Generic code for handling STREAMS-specific IOCtl logic and boilerplate. It
1115 * calls the IOCtl handler passed to it without the handler having to be aware
1116 * of STREAMS structures, or whether this is a transparent (traditional) or an
1117 * I_STR (using a STREAMS structure to describe the data) IOCtl. With the
1118 * caveat that we only support transparent IOCtls which pass all data in a
1119 * single buffer of a fixed size (I_STR IOCtls are restricted to a single
1120 * buffer anyway, but the caller can choose the buffer size).
1121 * @returns 0 on success or the IOCtl error code on failure.
1122 * @param pWriteQueue pointer to the STREAMS write queue structure.
1123 * @param pMBlk pointer to the STREAMS message block structure.
1124 * @param pfnHandler pointer to the right IOCtl handler function for this
1125 * IOCtl number.
1126 * @param iCmd IOCtl command number.
1127 * @param cbTransparent size of the user space buffer for this IOCtl number,
1128 * used for processing transparent IOCtls. Pass zero
1129 * for IOCtls with no maximum buffer size (which will
1130 * not be able to be handled as transparent) or with
1131 * no argument.
1132 * @param enmDirection data transfer direction of the IOCtl.
1133 */
1134static int vbmsSolHandleIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1135 PFNVBMSSOLIOCTL pfnHandler, int iCmd,
1136 size_t cbTransparent,
1137 enum IOCTLDIRECTION enmDirection)
1138{
1139 struct iocblk *pIOCBlk = (struct iocblk *)pMBlk->b_rptr;
1140
1141 LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d\n",
1142 (unsigned)iCmd, (int)cbTransparent, (int)enmDirection));
1143 if (pMBlk->b_datap->db_type == M_IOCDATA)
1144 return vbmsSolHandleIOCtlData(pWriteQueue, pMBlk, pfnHandler, iCmd,
1145 cbTransparent, enmDirection);
1146 else if ( pMBlk->b_datap->db_type == M_IOCTL
1147 && pIOCBlk->ioc_count == TRANSPARENT)
1148 return vbmsSolHandleTransparentIOCtl(pWriteQueue, pMBlk, pfnHandler,
1149 iCmd, cbTransparent,
1150 enmDirection);
1151 else if (pMBlk->b_datap->db_type == M_IOCTL)
1152 return vbmsSolHandleIStrIOCtl(pWriteQueue, pMBlk, pfnHandler, iCmd);
1153 return EINVAL;
1154}
1155
1156
1157/**
1158 * Helper for vbmsSolHandleIOCtl. This rather complicated-looking
1159 * code is basically the standard boilerplate for handling any streams IOCtl
1160 * additional data, which we currently only use for transparent IOCtls.
1161 * @copydoc vbmsSolHandleIOCtl
1162 */
1163static int vbmsSolHandleIOCtlData(queue_t *pWriteQueue, mblk_t *pMBlk,
1164 PFNVBMSSOLIOCTL pfnHandler, int iCmd,
1165 size_t cbTransparent,
1166 enum IOCTLDIRECTION enmDirection)
1167{
1168 struct copyresp *pCopyResp = (struct copyresp *)pMBlk->b_rptr;
1169 PVBMSSTATE pState = (PVBMSSTATE)pWriteQueue->q_ptr;
1170
1171 LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d, cp_rval=%d, cp_private=%p\n",
1172 (unsigned)iCmd, (int)cbTransparent, (int)enmDirection,
1173 (int)(uintptr_t)pCopyResp->cp_rval,
1174 (void *)pCopyResp->cp_private));
1175 if (pCopyResp->cp_rval) /* cp_rval is a pointer used as a boolean. */
1176 {
1177 freemsg(pMBlk);
1178 return EAGAIN;
1179 }
1180 if ((pCopyResp->cp_private && enmDirection == BOTH) || enmDirection == IN)
1181 {
1182 size_t cbData = 0;
1183 void *pvData = NULL;
1184 int err;
1185
1186 if (!pMBlk->b_cont)
1187 return EINVAL;
1188 if (enmDirection == BOTH && !pCopyResp->cp_private)
1189 return EINVAL;
1190 pvData = pMBlk->b_cont->b_rptr;
1191 err = pfnHandler(pState, iCmd, pvData, cbTransparent, &cbData, NULL);
1192 if (!err && enmDirection == BOTH)
1193 mcopyout(pMBlk, NULL, cbData, pCopyResp->cp_private, NULL);
1194 else if (!err && enmDirection == IN)
1195 miocack(pWriteQueue, pMBlk, 0, 0);
1196 return err;
1197 }
1198 else
1199 {
1200 AssertReturn(enmDirection == OUT || enmDirection == BOTH, EINVAL);
1201 miocack(pWriteQueue, pMBlk, 0, 0);
1202 return 0;
1203 }
1204}
1205
1206/**
1207 * Helper for vbmsSolHandleIOCtl. This rather complicated-looking
1208 * code is basically the standard boilerplate for handling transparent IOCtls,
1209 * that is, IOCtls which are not re-packed inside STREAMS IOCtls.
1210 * @copydoc vbmsSolHandleIOCtl
1211 */
1212int vbmsSolHandleTransparentIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1213 PFNVBMSSOLIOCTL pfnHandler, int iCmd,
1214 size_t cbTransparent,
1215 enum IOCTLDIRECTION enmDirection)
1216{
1217 int err = 0, rc = 0;
1218 size_t cbData = 0;
1219 PVBMSSTATE pState = (PVBMSSTATE)pWriteQueue->q_ptr;
1220
1221 LogFlowFunc(("iCmd=0x%x, cbBuffer=%d, enmDirection=%d\n",
1222 (unsigned)iCmd, (int)cbTransparent, (int)enmDirection));
1223 if ( (enmDirection != NONE && !pMBlk->b_cont)
1224 || enmDirection == UNSPECIFIED)
1225 return EINVAL;
1226 if (enmDirection == IN || enmDirection == BOTH)
1227 {
1228 void *pUserAddr = NULL;
1229 /* We only need state data if there is something to copy back. */
1230 if (enmDirection == BOTH)
1231 pUserAddr = *(void **)pMBlk->b_cont->b_rptr;
1232 mcopyin(pMBlk, pUserAddr /* state data */, cbTransparent, NULL);
1233 }
1234 else if (enmDirection == OUT)
1235 {
1236 mblk_t *pMBlkOut = allocb(cbTransparent, BPRI_MED);
1237 void *pvData;
1238
1239 if (!pMBlkOut)
1240 return EAGAIN;
1241 pvData = pMBlkOut->b_rptr;
1242 err = pfnHandler(pState, iCmd, pvData, cbTransparent, &cbData, NULL);
1243 if (!err)
1244 mcopyout(pMBlk, NULL, cbData, NULL, pMBlkOut);
1245 else
1246 freemsg(pMBlkOut);
1247 }
1248 else
1249 {
1250 AssertReturn(enmDirection == NONE, EINVAL);
1251 err = pfnHandler(pState, iCmd, NULL, 0, NULL, &rc);
1252 if (!err)
1253 miocack(pWriteQueue, pMBlk, 0, rc);
1254 }
1255 return err;
1256}
1257
1258/**
1259 * Helper for vbmsSolHandleIOCtl. This rather complicated-looking
1260 * code is basically the standard boilerplate for handling any streams IOCtl.
1261 * @copydoc vbmsSolHandleIOCtl
1262 */
1263static int vbmsSolHandleIStrIOCtl(queue_t *pWriteQueue, mblk_t *pMBlk,
1264 PFNVBMSSOLIOCTL pfnHandler, int iCmd)
1265{
1266 struct iocblk *pIOCBlk = (struct iocblk *)pMBlk->b_rptr;
1267 PVBMSSTATE pState = (PVBMSSTATE)pWriteQueue->q_ptr;
1268 uint_t cbBuffer = pIOCBlk->ioc_count;
1269 void *pvData = NULL;
1270 int err, rc = 0;
1271 size_t cbData = 0;
1272
1273 LogFlowFunc(("iCmd=0x%x, cbBuffer=%u, b_cont=%p\n",
1274 (unsigned)iCmd, cbBuffer, (void *)pMBlk->b_cont));
1275 if (cbBuffer && !pMBlk->b_cont)
1276 return EINVAL;
1277 /* Repack the whole buffer into a single message block if needed. */
1278 if (cbBuffer)
1279 {
1280 err = miocpullup(pMBlk, cbBuffer);
1281 if (err)
1282 return err;
1283 }
1284 if (pMBlk->b_cont) /* consms forgets to set ioc_count. */
1285 {
1286 pvData = pMBlk->b_cont->b_rptr;
1287 cbBuffer = pMBlk->b_cont->b_wptr - pMBlk->b_cont->b_rptr;
1288 }
1289 err = pfnHandler(pState, iCmd, pvData, cbBuffer, &cbData, &rc);
1290 if (!err)
1291 miocack(pWriteQueue, pMBlk, cbData, rc);
1292 return err;
1293}
1294
1295
1296/**
1297 * Handle a VUID input device IOCtl.
1298 * @copydoc FNVBMSSOLIOCTL
1299 */
1300static int vbmsSolVUIDIOCtl(PVBMSSTATE pState, int iCmd, void *pvData,
1301 size_t cbBuffer, size_t *pcbData, int *prc)
1302{
1303 LogRelFlowFunc((DEVICE_NAME ":: " /* no '\n' */));
1304 switch (iCmd)
1305 {
1306 case VUIDGFORMAT:
1307 {
1308 LogRelFlowFunc(("VUIDGFORMAT\n"));
1309 if (cbBuffer < sizeof(int))
1310 return EINVAL;
1311 *(int *)pvData = VUID_FIRM_EVENT;
1312 *pcbData = sizeof(int);
1313 return 0;
1314 }
1315 case VUIDSFORMAT:
1316 LogRelFlowFunc(("VUIDSFORMAT\n"));
1317 /* We define our native format to be VUID_FIRM_EVENT, so there
1318 * is nothing more to do and we exit here on success or on
1319 * failure. */
1320 return 0;
1321 case VUIDGADDR:
1322 case VUIDSADDR:
1323 LogRelFlowFunc(("VUIDGADDR/VUIDSADDR\n"));
1324 return ENOTTY;
1325 case MSIOGETPARMS:
1326 {
1327 Ms_parms parms = { 0 };
1328
1329 LogRelFlowFunc(("MSIOGETPARMS\n"));
1330 if (cbBuffer < sizeof(Ms_parms))
1331 return EINVAL;
1332 *(Ms_parms *)pvData = parms;
1333 *pcbData = sizeof(Ms_parms);
1334 return 0;
1335 }
1336 case MSIOSETPARMS:
1337 LogRelFlowFunc(("MSIOSETPARMS\n"));
1338 return 0;
1339 case MSIOSRESOLUTION:
1340 {
1341 Ms_screen_resolution *pResolution = (Ms_screen_resolution *)pvData;
1342 int rc;
1343
1344 LogRelFlowFunc(("MSIOSRESOLUTION, cbBuffer=%d, sizeof(Ms_screen_resolution)=%d\n",
1345 (int) cbBuffer,
1346 (int) sizeof(Ms_screen_resolution)));
1347 if (cbBuffer < sizeof(Ms_screen_resolution))
1348 return EINVAL;
1349 LogRelFlowFunc(("%dx%d\n", pResolution->width,
1350 pResolution->height));
1351 pState->cMaxScreenX = pResolution->width - 1;
1352 pState->cMaxScreenY = pResolution->height - 1;
1353 /* Note: we don't disable this again until session close. */
1354 rc = VbglSetMouseStatus( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
1355 | VMMDEV_MOUSE_NEW_PROTOCOL);
1356 if (RT_SUCCESS(rc))
1357 return 0;
1358 pState->cMaxScreenX = 0;
1359 pState->cMaxScreenY = 0;
1360 return ENODEV;
1361 }
1362 case MSIOBUTTONS:
1363 {
1364 LogRelFlowFunc(("MSIOBUTTONS\n"));
1365 if (cbBuffer < sizeof(int))
1366 return EINVAL;
1367 *(int *)pvData = 0;
1368 *pcbData = sizeof(int);
1369 return 0;
1370 }
1371 case VUIDGWHEELCOUNT:
1372 {
1373 LogRelFlowFunc(("VUIDGWHEELCOUNT\n"));
1374 if (cbBuffer < sizeof(int))
1375 return EINVAL;
1376 *(int *)pvData = 0;
1377 *pcbData = sizeof(int);
1378 return 0;
1379 }
1380 case VUIDGWHEELINFO:
1381 case VUIDGWHEELSTATE:
1382 case VUIDSWHEELSTATE:
1383 LogRelFlowFunc(("VUIDGWHEELINFO/VUIDGWHEELSTATE/VUIDSWHEELSTATE\n"));
1384 return EINVAL;
1385 default:
1386 LogRelFlowFunc(("Invalid IOCtl command %x\n", iCmd));
1387 return EINVAL;
1388 }
1389}
1390
1391
1392#ifdef TESTCASE
1393int main(void)
1394{
1395 RTTEST hTest;
1396 int rc = RTTestInitAndCreate("tstVBoxGuest-solaris", &hTest);
1397 if (rc)
1398 return rc;
1399 RTTestBanner(hTest);
1400 test_init(hTest);
1401 testOpenClose(hTest);
1402 testWPut(hTest);
1403
1404 /*
1405 * Summary.
1406 */
1407 return RTTestSummaryAndDestroy(hTest);
1408}
1409#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette