VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/Mouse/vboxms.c@ 68639

Last change on this file since 68639 was 68639, checked in by vboxsync, 7 years ago

solaris build fix

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

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