VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-solaris.c@ 68793

Last change on this file since 68793 was 68793, checked in by vboxsync, 8 years ago

VBoxGuest/solaris: fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.5 KB
Line 
1/* $Id: VBoxGuest-solaris.c 68793 2017-09-19 15:46:36Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for Solaris.
4 */
5
6/*
7 * Copyright (C) 2007-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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <sys/conf.h>
32#include <sys/modctl.h>
33#include <sys/mutex.h>
34#include <sys/pci.h>
35#include <sys/stat.h>
36#include <sys/ddi.h>
37#include <sys/ddi_intr.h>
38#include <sys/sunddi.h>
39#include <sys/open.h>
40#include <sys/sunldi.h>
41#include <sys/file.h>
42#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
43
44#include "VBoxGuestInternal.h"
45#include <VBox/log.h>
46#include <VBox/version.h>
47#include <iprt/assert.h>
48#include <iprt/initterm.h>
49#include <iprt/process.h>
50#include <iprt/mem.h>
51#include <iprt/cdefs.h>
52#include <iprt/asm.h>
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58/** The module name. */
59#define DEVICE_NAME "vboxguest"
60/** The module description as seen in 'modinfo'. */
61#define DEVICE_DESC "VirtualBox GstDrv"
62
63
64/*********************************************************************************************************************************
65* Internal Functions *
66*********************************************************************************************************************************/
67static int vgdrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
68static int vgdrvSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
69static int vgdrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
70static int vgdrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
71static int vgdrvSolarisIOCtl(dev_t Dev, int iCmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
72static int vgdrvSolarisIOCtlSlow(PVBOXGUESTSESSION pSession, int iCmd, int Mode, intptr_t iArgs);
73static int vgdrvSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead);
74
75static int vgdrvSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
76static int vgdrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
77static int vgdrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
78static int vgdrvSolarisQuiesce(dev_info_t *pDip);
79
80static int vgdrvSolarisAddIRQ(dev_info_t *pDip);
81static void vgdrvSolarisRemoveIRQ(dev_info_t *pDip);
82static uint_t vgdrvSolarisHighLevelISR(caddr_t Arg);
83static uint_t vgdrvSolarisISR(caddr_t Arg);
84
85
86/*********************************************************************************************************************************
87* Structures and Typedefs *
88*********************************************************************************************************************************/
89/**
90 * cb_ops: for drivers that support char/block entry points
91 */
92static struct cb_ops g_vgdrvSolarisCbOps =
93{
94 vgdrvSolarisOpen,
95 vgdrvSolarisClose,
96 nodev, /* b strategy */
97 nodev, /* b dump */
98 nodev, /* b print */
99 vgdrvSolarisRead,
100 vgdrvSolarisWrite,
101 vgdrvSolarisIOCtl,
102 nodev, /* c devmap */
103 nodev, /* c mmap */
104 nodev, /* c segmap */
105 vgdrvSolarisPoll,
106 ddi_prop_op, /* property ops */
107 NULL, /* streamtab */
108 D_NEW | D_MP, /* compat. flag */
109 CB_REV /* revision */
110};
111
112/**
113 * dev_ops: for driver device operations
114 */
115static struct dev_ops g_vgdrvSolarisDevOps =
116{
117 DEVO_REV, /* driver build revision */
118 0, /* ref count */
119 vgdrvSolarisGetInfo,
120 nulldev, /* identify */
121 nulldev, /* probe */
122 vgdrvSolarisAttach,
123 vgdrvSolarisDetach,
124 nodev, /* reset */
125 &g_vgdrvSolarisCbOps,
126 (struct bus_ops *)0,
127 nodev, /* power */
128 vgdrvSolarisQuiesce
129};
130
131/**
132 * modldrv: export driver specifics to the kernel
133 */
134static struct modldrv g_vgdrvSolarisModule =
135{
136 &mod_driverops, /* extern from kernel */
137 DEVICE_DESC " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
138 &g_vgdrvSolarisDevOps
139};
140
141/**
142 * modlinkage: export install/remove/info to the kernel
143 */
144static struct modlinkage g_vgdrvSolarisModLinkage =
145{
146 MODREV_1, /* loadable module system revision */
147 &g_vgdrvSolarisModule,
148 NULL /* terminate array of linkage structures */
149};
150
151/**
152 * State info for each open file handle.
153 */
154typedef struct
155{
156 /** Pointer to the session handle. */
157 PVBOXGUESTSESSION pSession;
158 /** The process reference for posting signals */
159 void *pvProcRef;
160} vboxguest_state_t;
161
162
163/*********************************************************************************************************************************
164* Global Variables *
165*********************************************************************************************************************************/
166/** Device handle (we support only one instance). */
167static dev_info_t *g_pDip = NULL;
168/** Opaque pointer to file-descriptor states */
169static void *g_pvgdrvSolarisState = NULL;
170/** Device extention & session data association structure. */
171static VBOXGUESTDEVEXT g_DevExt;
172/** IO port handle. */
173static ddi_acc_handle_t g_PciIOHandle;
174/** MMIO handle. */
175static ddi_acc_handle_t g_PciMMIOHandle;
176/** IO Port. */
177static uint16_t g_uIOPortBase;
178/** Address of the MMIO region.*/
179static caddr_t g_pMMIOBase;
180/** Size of the MMIO region. */
181static off_t g_cbMMIO;
182/** Pointer to an array of interrupt handles. */
183static ddi_intr_handle_t *g_pahIntrs;
184/** Handle to the soft interrupt. */
185static ddi_softint_handle_t g_hSoftIntr;
186/** The pollhead structure */
187static pollhead_t g_PollHead;
188/** The IRQ Mutex */
189static kmutex_t g_IrqMtx;
190/** The IRQ high-level Mutex. */
191static kmutex_t g_HighLevelIrqMtx;
192/** Whether soft-ints are setup. */
193static bool g_fSoftIntRegistered = false;
194
195/** Additional IPRT function we need to drag in for vboxfs. */
196PFNRT g_Deps[] =
197{
198 (PFNRT)RTErrConvertToErrno,
199};
200
201
202/**
203 * Kernel entry points
204 */
205int _init(void)
206{
207 /*
208 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
209 */
210 int rc = RTR0Init(0);
211 if (RT_SUCCESS(rc))
212 {
213 PRTLOGGER pRelLogger;
214 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
215 rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
216 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
217 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
218 if (RT_SUCCESS(rc))
219 RTLogRelSetDefaultInstance(pRelLogger);
220 else
221 cmn_err(CE_NOTE, "failed to initialize driver logging rc=%d!\n", rc);
222
223 /*
224 * Prevent module autounloading.
225 */
226 modctl_t *pModCtl = mod_getctl(&g_vgdrvSolarisModLinkage);
227 if (pModCtl)
228 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
229 else
230 LogRel((DEVICE_NAME ": failed to disable autounloading!\n"));
231
232 rc = ddi_soft_state_init(&g_pvgdrvSolarisState, sizeof(vboxguest_state_t), 1);
233 if (!rc)
234 {
235 rc = mod_install(&g_vgdrvSolarisModLinkage);
236 if (rc)
237 ddi_soft_state_fini(&g_pvgdrvSolarisState);
238 }
239 }
240 else
241 {
242 cmn_err(CE_NOTE, "_init: RTR0Init failed. rc=%d\n", rc);
243 return EINVAL;
244 }
245
246 return rc;
247}
248
249
250int _fini(void)
251{
252 LogFlow((DEVICE_NAME ":_fini\n"));
253 int rc = mod_remove(&g_vgdrvSolarisModLinkage);
254 if (!rc)
255 ddi_soft_state_fini(&g_pvgdrvSolarisState);
256
257 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
258 RTLogDestroy(RTLogSetDefaultInstance(NULL));
259
260 if (!rc)
261 RTR0Term();
262 return rc;
263}
264
265
266int _info(struct modinfo *pModInfo)
267{
268 /* LogFlow((DEVICE_NAME ":_info\n")); - Called too early, causing RTThreadPreemtIsEnabled warning. */
269 return mod_info(&g_vgdrvSolarisModLinkage, pModInfo);
270}
271
272
273/**
274 * Attach entry point, to attach a device to the system or resume it.
275 *
276 * @param pDip The module structure instance.
277 * @param enmCmd Attach type (ddi_attach_cmd_t)
278 *
279 * @return corresponding solaris error code.
280 */
281static int vgdrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
282{
283 LogFlow(("vgdrvSolarisAttach:\n"));
284 switch (enmCmd)
285 {
286 case DDI_ATTACH:
287 {
288 if (g_pDip)
289 {
290 LogRel(("vgdrvSolarisAttach: Only one instance supported.\n"));
291 return DDI_FAILURE;
292 }
293
294 int instance = ddi_get_instance(pDip);
295
296 /*
297 * Enable resources for PCI access.
298 */
299 ddi_acc_handle_t PciHandle;
300 int rc = pci_config_setup(pDip, &PciHandle);
301 if (rc == DDI_SUCCESS)
302 {
303 /*
304 * Map the register address space.
305 */
306 caddr_t baseAddr;
307 ddi_device_acc_attr_t deviceAttr;
308 deviceAttr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
309 deviceAttr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
310 deviceAttr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
311 deviceAttr.devacc_attr_access = DDI_DEFAULT_ACC;
312 rc = ddi_regs_map_setup(pDip, 1, &baseAddr, 0, 0, &deviceAttr, &g_PciIOHandle);
313 if (rc == DDI_SUCCESS)
314 {
315 /*
316 * Read size of the MMIO region.
317 */
318 g_uIOPortBase = (uintptr_t)baseAddr;
319 rc = ddi_dev_regsize(pDip, 2, &g_cbMMIO);
320 if (rc == DDI_SUCCESS)
321 {
322 rc = ddi_regs_map_setup(pDip, 2, &g_pMMIOBase, 0, g_cbMMIO, &deviceAttr,
323 &g_PciMMIOHandle);
324 if (rc == DDI_SUCCESS)
325 {
326 /*
327 * Add IRQ of VMMDev.
328 */
329 rc = vgdrvSolarisAddIRQ(pDip);
330 if (rc == DDI_SUCCESS)
331 {
332 /*
333 * Call the common device extension initializer.
334 */
335 rc = VGDrvCommonInitDevExt(&g_DevExt, g_uIOPortBase, g_pMMIOBase, g_cbMMIO,
336#if ARCH_BITS == 64
337 VBOXOSTYPE_Solaris_x64,
338#else
339 VBOXOSTYPE_Solaris,
340#endif
341 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
342 if (RT_SUCCESS(rc))
343 {
344 rc = ddi_create_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0 /* fFlags */);
345 if (rc == DDI_SUCCESS)
346 {
347 g_pDip = pDip;
348 pci_config_teardown(&PciHandle);
349 return DDI_SUCCESS;
350 }
351
352 LogRel((DEVICE_NAME "::Attach: ddi_create_minor_node failed.\n"));
353 VGDrvCommonDeleteDevExt(&g_DevExt);
354 }
355 else
356 LogRel((DEVICE_NAME "::Attach: VGDrvCommonInitDevExt failed.\n"));
357
358 vgdrvSolarisRemoveIRQ(pDip);
359 }
360 else
361 LogRel((DEVICE_NAME "::Attach: vgdrvSolarisAddIRQ failed.\n"));
362 ddi_regs_map_free(&g_PciMMIOHandle);
363 }
364 else
365 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for MMIO region failed.\n"));
366 }
367 else
368 LogRel((DEVICE_NAME "::Attach: ddi_dev_regsize for MMIO region failed.\n"));
369 ddi_regs_map_free(&g_PciIOHandle);
370 }
371 else
372 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for IOport failed.\n"));
373 pci_config_teardown(&PciHandle);
374 }
375 else
376 LogRel((DEVICE_NAME "::Attach: pci_config_setup failed rc=%d.\n", rc));
377 return DDI_FAILURE;
378 }
379
380 case DDI_RESUME:
381 {
382 /** @todo implement resume for guest driver. */
383 return DDI_SUCCESS;
384 }
385
386 default:
387 return DDI_FAILURE;
388 }
389}
390
391
392/**
393 * Detach entry point, to detach a device to the system or suspend it.
394 *
395 * @param pDip The module structure instance.
396 * @param enmCmd Attach type (ddi_attach_cmd_t)
397 *
398 * @return corresponding solaris error code.
399 */
400static int vgdrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
401{
402 LogFlow(("vgdrvSolarisDetach:\n"));
403 switch (enmCmd)
404 {
405 case DDI_DETACH:
406 {
407 vgdrvSolarisRemoveIRQ(pDip);
408 ddi_regs_map_free(&g_PciIOHandle);
409 ddi_regs_map_free(&g_PciMMIOHandle);
410 ddi_remove_minor_node(pDip, NULL);
411 VGDrvCommonDeleteDevExt(&g_DevExt);
412 g_pDip = NULL;
413 return DDI_SUCCESS;
414 }
415
416 case DDI_SUSPEND:
417 {
418 /** @todo implement suspend for guest driver. */
419 return DDI_SUCCESS;
420 }
421
422 default:
423 return DDI_FAILURE;
424 }
425}
426
427
428/**
429 * Quiesce entry point, called by solaris kernel for disabling the device from
430 * generating any interrupts or doing in-bound DMA.
431 *
432 * @param pDip The module structure instance.
433 *
434 * @return corresponding solaris error code.
435 */
436static int vgdrvSolarisQuiesce(dev_info_t *pDip)
437{
438 int rc = ddi_intr_disable(g_pahIntrs[0]);
439 if (rc != DDI_SUCCESS)
440 return DDI_FAILURE;
441
442 /** @todo What about HGCM/HGSMI touching guest-memory? */
443
444 return DDI_SUCCESS;
445}
446
447
448/**
449 * Info entry point, called by solaris kernel for obtaining driver info.
450 *
451 * @param pDip The module structure instance (do not use).
452 * @param enmCmd Information request type.
453 * @param pvArg Type specific argument.
454 * @param ppvResult Where to store the requested info.
455 *
456 * @return corresponding solaris error code.
457 */
458static int vgdrvSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
459{
460 LogFlow(("vgdrvSolarisGetInfo:\n"));
461
462 int rc = DDI_SUCCESS;
463 switch (enmCmd)
464 {
465 case DDI_INFO_DEVT2DEVINFO:
466 *ppvResult = (void *)g_pDip;
467 break;
468
469 case DDI_INFO_DEVT2INSTANCE:
470 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_pDip);
471 break;
472
473 default:
474 rc = DDI_FAILURE;
475 break;
476 }
477
478 NOREF(pvArg);
479 return rc;
480}
481
482
483/**
484 * User context entry points
485 */
486static int vgdrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
487{
488 int rc;
489 PVBOXGUESTSESSION pSession = NULL;
490
491 LogFlow(("vgdrvSolarisOpen:\n"));
492
493 /*
494 * Verify we are being opened as a character device.
495 */
496 if (fType != OTYP_CHR)
497 return EINVAL;
498
499 vboxguest_state_t *pState = NULL;
500 unsigned iOpenInstance;
501 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
502 {
503 if ( !ddi_get_soft_state(g_pvgdrvSolarisState, iOpenInstance) /* faster */
504 && ddi_soft_state_zalloc(g_pvgdrvSolarisState, iOpenInstance) == DDI_SUCCESS)
505 {
506 pState = ddi_get_soft_state(g_pvgdrvSolarisState, iOpenInstance);
507 break;
508 }
509 }
510 if (!pState)
511 {
512 Log(("vgdrvSolarisOpen: too many open instances."));
513 return ENXIO;
514 }
515
516 /*
517 * Create a new session.
518 */
519 rc = VGDrvCommonCreateUserSession(&g_DevExt, &pSession);
520 if (RT_SUCCESS(rc))
521 {
522 pState->pvProcRef = proc_ref();
523 pState->pSession = pSession;
524 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
525 Log(("vgdrvSolarisOpen: pSession=%p pState=%p pid=%d\n", pSession, pState, (int)RTProcSelf()));
526 return 0;
527 }
528
529 /* Failed, clean up. */
530 ddi_soft_state_free(g_pvgdrvSolarisState, iOpenInstance);
531
532 LogRel((DEVICE_NAME "::Open: VGDrvCommonCreateUserSession failed. rc=%d\n", rc));
533 return EFAULT;
534}
535
536
537static int vgdrvSolarisClose(dev_t Dev, int flag, int fType, cred_t *pCred)
538{
539 LogFlow(("vgdrvSolarisClose: pid=%d\n", (int)RTProcSelf()));
540
541 PVBOXGUESTSESSION pSession = NULL;
542 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
543 if (!pState)
544 {
545 Log(("vgdrvSolarisClose: failed to get pState.\n"));
546 return EFAULT;
547 }
548
549 proc_unref(pState->pvProcRef);
550 pSession = pState->pSession;
551 pState->pSession = NULL;
552 Log(("vgdrvSolarisClose: pSession=%p pState=%p\n", pSession, pState));
553 ddi_soft_state_free(g_pvgdrvSolarisState, getminor(Dev));
554 if (!pSession)
555 {
556 Log(("vgdrvSolarisClose: failed to get pSession.\n"));
557 return EFAULT;
558 }
559
560 /*
561 * Close the session.
562 */
563 VGDrvCommonCloseSession(&g_DevExt, pSession);
564 return 0;
565}
566
567
568static int vgdrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
569{
570 LogFlow((DEVICE_NAME "::Read\n"));
571
572 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
573 if (!pState)
574 {
575 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
576 return EFAULT;
577 }
578
579 PVBOXGUESTSESSION pSession = pState->pSession;
580 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
581 if (pSession->u32MousePosChangedSeq != u32CurSeq)
582 pSession->u32MousePosChangedSeq = u32CurSeq;
583
584 return 0;
585}
586
587
588static int vgdrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
589{
590 LogFlow(("vgdrvSolarisWrite:\n"));
591 return 0;
592}
593
594
595/**
596 * Driver ioctl, an alternate entry point for this character driver.
597 *
598 * @param Dev Device number
599 * @param iCmd Operation identifier
600 * @param iArgs Arguments from user to driver
601 * @param Mode Information bitfield (read/write, address space etc.)
602 * @param pCred User credentials
603 * @param pVal Return value for calling process.
604 *
605 * @return corresponding solaris error code.
606 */
607static int vgdrvSolarisIOCtl(dev_t Dev, int iCmd, intptr_t iArgs, int Mode, cred_t *pCred, int *pVal)
608{
609 /*
610 * Get the session from the soft state item.
611 */
612 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
613 if (!pState)
614 {
615 LogRel(("vgdrvSolarisIOCtl: no state data for %#x (%d)\n", Dev, getminor(Dev)));
616 return EINVAL;
617 }
618
619 PVBOXGUESTSESSION pSession = pState->pSession;
620 if (!pSession)
621 {
622 LogRel(("vgdrvSolarisIOCtl: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
623 return DDI_SUCCESS;
624 }
625
626 /*
627 * Deal with fast requests.
628 */
629 if (VBGL_IOCTL_IS_FAST(iCmd))
630 {
631 *pVal = VGDrvCommonIoCtlFast(iCmd, &g_DevExt, pSession);
632 return 0;
633 }
634
635 return vgdrvSolarisIOCtlSlow(pSession, iCmd, Mode, iArgs);
636}
637
638
639/** @def IOCPARM_LEN
640 * Gets the length from the ioctl number.
641 * This is normally defined by sys/ioccom.h on BSD systems...
642 */
643#ifndef IOCPARM_LEN
644# define IOCPARM_LEN(x) ( ((x) >> 16) & IOCPARM_MASK )
645#endif
646
647
648/**
649 * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
650 *
651 * @returns Solaris errno.
652 *
653 * @param pSession The session.
654 * @param iCmd The IOCtl command.
655 * @param Mode Information bitfield (for specifying ownership of data)
656 * @param iArg User space address of the request buffer.
657 */
658static int vgdrvSolarisIOCtlSlow(PVBOXGUESTSESSION pSession, int iCmd, int Mode, intptr_t iArg)
659{
660 int rc;
661 uint32_t cbBuf = 0;
662 union
663 {
664 VBGLREQHDR Hdr;
665 uint8_t abBuf[64];
666 } StackBuf;
667 PVBGLREQHDR pHdr;
668
669
670 /*
671 * Read the header.
672 */
673 if (RT_UNLIKELY(IOCPARM_LEN(iCmd) != sizeof(StackBuf.Hdr)))
674 {
675 LogRel(("vgdrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(StackBuf.Hdr)));
676 return EINVAL;
677 }
678 rc = ddi_copyin((void *)iArg, &StackBuf.Hdr, sizeof(StackBuf.Hdr), Mode);
679 if (RT_UNLIKELY(rc))
680 {
681 LogRel(("vgdrvSolarisIOCtlSlow: ddi_copyin(,%#lx,) failed; iCmd=%#x. rc=%d\n", iArg, iCmd, rc));
682 return EFAULT;
683 }
684 if (RT_UNLIKELY(StackBuf.Hdr.uVersion != VBGLREQHDR_VERSION))
685 {
686 LogRel(("vgdrvSolarisIOCtlSlow: bad header version %#x; iCmd=%#x\n", StackBuf.Hdr.uVersion, iCmd));
687 return EINVAL;
688 }
689 cbBuf = RT_MAX(StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut);
690 if (RT_UNLIKELY( StackBuf.Hdr.cbIn < sizeof(StackBuf.Hdr)
691 || (StackBuf.Hdr.cbOut < sizeof(StackBuf.Hdr) && StackBuf.Hdr.cbOut != 0)
692 || cbBuf > _1M*16))
693 {
694 LogRel(("vgdrvSolarisIOCtlSlow: max(%#x,%#x); iCmd=%#x\n", StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut, iCmd));
695 return EINVAL;
696 }
697
698 /*
699 * Buffer the request.
700 *
701 * Note! Common code revalidates the header sizes and version. So it's
702 * fine to read it once more.
703 */
704 if (cbBuf <= sizeof(StackBuf))
705 pHdr = &StackBuf.Hdr;
706 else
707 {
708 pHdr = RTMemTmpAlloc(cbBuf);
709 if (RT_UNLIKELY(!pHdr))
710 {
711 LogRel(("vgdrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
712 return ENOMEM;
713 }
714 }
715 rc = ddi_copyin((void *)iArg, pHdr, cbBuf, Mode);
716 if (RT_UNLIKELY(rc))
717 {
718 LogRel(("vgdrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, cbBuf, iCmd, rc));
719 if (pHdr != &StackBuf.Hdr)
720 RTMemFree(pHdr);
721 return EFAULT;
722 }
723
724 /*
725 * Process the IOCtl.
726 */
727 rc = VGDrvCommonIoCtl(iCmd, &g_DevExt, pSession, pHdr, cbBuf);
728
729 /*
730 * Copy ioctl data and output buffer back to user space.
731 */
732 if (RT_SUCCESS(rc))
733 {
734 uint32_t cbOut = pHdr->cbOut;
735 if (RT_UNLIKELY(cbOut > cbBuf))
736 {
737 LogRel(("vgdrvSolarisIOCtlSlow: too much output! %#x > %#x; iCmd=%#x!\n", cbOut, cbBuf, iCmd));
738 cbOut = cbBuf;
739 }
740 rc = ddi_copyout(pHdr, (void *)iArg, cbOut, Mode);
741 if (RT_UNLIKELY(rc != 0))
742 {
743 /* this is really bad */
744 LogRel(("vgdrvSolarisIOCtlSlow: ddi_copyout(,%p,%d) failed. rc=%d\n", (void *)iArg, cbBuf, rc));
745 rc = EFAULT;
746 }
747 }
748 else
749 rc = EINVAL;
750
751 if (pHdr != &StackBuf.Hdr)
752 RTMemTmpFree(pHdr);
753 return rc;
754}
755
756
757/**
758 * @note This code is duplicated on other platforms with variations, so please
759 * keep them all up to date when making changes!
760 */
761int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
762{
763 /*
764 * Simple request validation (common code does the rest).
765 */
766 int rc;
767 if ( RT_VALID_PTR(pReqHdr)
768 && cbReq >= sizeof(*pReqHdr))
769 {
770 /*
771 * All requests except the connect one requires a valid session.
772 */
773 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
774 if (pSession)
775 {
776 if ( RT_VALID_PTR(pSession)
777 && pSession->pDevExt == &g_DevExt)
778 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
779 else
780 rc = VERR_INVALID_HANDLE;
781 }
782 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
783 {
784 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
785 if (RT_SUCCESS(rc))
786 {
787 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
788 if (RT_FAILURE(rc))
789 VGDrvCommonCloseSession(&g_DevExt, pSession);
790 }
791 }
792 else
793 rc = VERR_INVALID_HANDLE;
794 }
795 else
796 rc = VERR_INVALID_POINTER;
797 return rc;
798}
799
800
801
802static int vgdrvSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
803{
804 LogFlow(("vgdrvSolarisPoll: fEvents=%d fAnyYet=%d\n", fEvents, fAnyYet));
805
806 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
807 if (RT_LIKELY(pState))
808 {
809 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pState->pSession;
810 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
811 if (pSession->u32MousePosChangedSeq != u32CurSeq)
812 {
813 *pReqEvents |= (POLLIN | POLLRDNORM);
814 pSession->u32MousePosChangedSeq = u32CurSeq;
815 }
816 else
817 {
818 *pReqEvents = 0;
819 if (!fAnyYet)
820 *ppPollHead = &g_PollHead;
821 }
822
823 return 0;
824 }
825
826 Log(("vgdrvSolarisPoll: no state data for %d\n", getminor(Dev)));
827 return EINVAL;
828}
829
830
831/**
832 * Sets IRQ for VMMDev.
833 *
834 * @returns Solaris error code.
835 * @param pDip Pointer to the device info structure.
836 */
837static int vgdrvSolarisAddIRQ(dev_info_t *pDip)
838{
839 LogFlow(("vgdrvSolarisAddIRQ: pDip=%p\n", pDip));
840
841 /* Get the types of interrupt supported for this hardware. */
842 int fIntrType = 0;
843 int rc = ddi_intr_get_supported_types(pDip, &fIntrType);
844 if (rc == DDI_SUCCESS)
845 {
846 /* We only support fixed interrupts at this point, not MSIs. */
847 if (fIntrType & DDI_INTR_TYPE_FIXED)
848 {
849 /* Verify the number of interrupts supported by this device. There can only be one fixed interrupt. */
850 int cIntrCount = 0;
851 rc = ddi_intr_get_nintrs(pDip, fIntrType, &cIntrCount);
852 if ( rc == DDI_SUCCESS
853 && cIntrCount == 1)
854 {
855 /* Allocated kernel memory for the interrupt handle. The allocation size is stored internally. */
856 g_pahIntrs = RTMemAllocZ(cIntrCount * sizeof(ddi_intr_handle_t));
857 if (g_pahIntrs)
858 {
859 /* Allocate the interrupt for this device and verify the allocation. */
860 int cIntrAllocated;
861 rc = ddi_intr_alloc(pDip, g_pahIntrs, fIntrType, 0 /* interrupt number */, cIntrCount, &cIntrAllocated,
862 DDI_INTR_ALLOC_NORMAL);
863 if ( rc == DDI_SUCCESS
864 && cIntrAllocated == 1)
865 {
866 /* Get the interrupt priority assigned by the system. */
867 uint_t uIntrPriority;
868 rc = ddi_intr_get_pri(g_pahIntrs[0], &uIntrPriority);
869 if (rc == DDI_SUCCESS)
870 {
871 /* Check if the interrupt priority is scheduler level or above, if so we need to use a high-level
872 and low-level interrupt handlers with corresponding mutexes. */
873 cmn_err(CE_CONT, "!vboxguest: uIntrPriority=%d hilevel_pri=%d\n", uIntrPriority, ddi_intr_get_hilevel_pri());
874 if (uIntrPriority >= ddi_intr_get_hilevel_pri())
875 {
876 /* Initialize the high-level mutex. */
877 mutex_init(&g_HighLevelIrqMtx, NULL /* pszDesc */, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
878
879 /* Assign interrupt handler function to the interrupt handle. */
880 rc = ddi_intr_add_handler(g_pahIntrs[0], (ddi_intr_handler_t *)&vgdrvSolarisHighLevelISR,
881 NULL /* pvArg1 */, NULL /* pvArg2 */);
882
883 if (rc == DDI_SUCCESS)
884 {
885 /* Add the low-level interrupt handler. */
886 rc = ddi_intr_add_softint(pDip, &g_hSoftIntr, DDI_INTR_SOFTPRI_MAX,
887 (ddi_intr_handler_t *)&vgdrvSolarisISR, NULL /* pvArg1 */);
888 if (rc == DDI_SUCCESS)
889 {
890 /* Initialize the low-level mutex at the corresponding level. */
891 mutex_init(&g_IrqMtx, NULL /* pszDesc */, MUTEX_DRIVER,
892 DDI_INTR_PRI(DDI_INTR_SOFTPRI_MAX));
893
894 g_fSoftIntRegistered = true;
895 /* Enable the high-level interrupt. */
896 rc = ddi_intr_enable(g_pahIntrs[0]);
897 if (rc == DDI_SUCCESS)
898 return rc;
899
900 LogRel((DEVICE_NAME "::AddIRQ: failed to enable interrupt. rc=%d\n", rc));
901 mutex_destroy(&g_IrqMtx);
902 }
903 else
904 LogRel((DEVICE_NAME "::AddIRQ: failed to add soft interrupt handler. rc=%d\n", rc));
905
906 ddi_intr_remove_handler(g_pahIntrs[0]);
907 }
908 else
909 LogRel((DEVICE_NAME "::AddIRQ: failed to add high-level interrupt handler. rc=%d\n", rc));
910
911 mutex_destroy(&g_HighLevelIrqMtx);
912 }
913 else
914 {
915 /* Interrupt handler runs at reschedulable level, initialize the mutex at the given priority. */
916 mutex_init(&g_IrqMtx, NULL /* pszDesc */, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
917
918 /* Assign interrupt handler function to the interrupt handle. */
919 rc = ddi_intr_add_handler(g_pahIntrs[0], (ddi_intr_handler_t *)vgdrvSolarisISR,
920 NULL /* pvArg1 */, NULL /* pvArg2 */);
921 if (rc == DDI_SUCCESS)
922 {
923 /* Enable the interrupt. */
924 rc = ddi_intr_enable(g_pahIntrs[0]);
925 if (rc == DDI_SUCCESS)
926 return rc;
927
928 LogRel((DEVICE_NAME "::AddIRQ: failed to enable interrupt. rc=%d\n", rc));
929 mutex_destroy(&g_IrqMtx);
930 }
931 }
932 }
933 else
934 LogRel((DEVICE_NAME "::AddIRQ: failed to get priority of interrupt. rc=%d\n", rc));
935
936 Assert(cIntrAllocated == 1);
937 ddi_intr_free(g_pahIntrs[0]);
938 }
939 else
940 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", cIntrCount));
941 RTMemFree(g_pahIntrs);
942 }
943 else
944 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", cIntrCount));
945 }
946 else
947 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient number of IRQs. rc=%d cIntrCount=%d\n", rc, cIntrCount));
948 }
949 else
950 LogRel((DEVICE_NAME "::AddIRQ: fixed-type interrupts not supported. IntrType=%#x\n", fIntrType));
951 }
952 else
953 LogRel((DEVICE_NAME "::AddIRQ: failed to get supported interrupt types. rc=%d\n", rc));
954 return rc;
955}
956
957
958/**
959 * Removes IRQ for VMMDev.
960 *
961 * @param pDip Pointer to the device info structure.
962 */
963static void vgdrvSolarisRemoveIRQ(dev_info_t *pDip)
964{
965 LogFlow(("vgdrvSolarisRemoveIRQ:\n"));
966
967 int rc = ddi_intr_disable(g_pahIntrs[0]);
968 if (rc == DDI_SUCCESS)
969 {
970 rc = ddi_intr_remove_handler(g_pahIntrs[0]);
971 if (rc == DDI_SUCCESS)
972 ddi_intr_free(g_pahIntrs[0]);
973 }
974
975 if (g_fSoftIntRegistered)
976 {
977 ddi_intr_remove_softint(g_hSoftIntr);
978 mutex_destroy(&g_HighLevelIrqMtx);
979 g_fSoftIntRegistered = false;
980 }
981
982 mutex_destroy(&g_IrqMtx);
983 RTMemFree(g_pahIntrs);
984}
985
986
987/**
988 * High-level Interrupt Service Routine for VMMDev.
989 *
990 * This routine simply dispatches a soft-interrupt at an acceptable IPL as
991 * VGDrvCommonISR() cannot be called at a high IPL (scheduler level or higher)
992 * due to pollwakeup() in VGDrvNativeISRMousePollEvent().
993 *
994 * @param Arg Private data (unused, will be NULL).
995 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
996 */
997static uint_t vgdrvSolarisHighLevelISR(caddr_t Arg)
998{
999 bool const fOurIrq = VGDrvCommonIsOurIRQ(&g_DevExt);
1000 if (fOurIrq)
1001 {
1002 ddi_intr_trigger_softint(g_hSoftIntr, NULL /* Arg */);
1003 return DDI_INTR_CLAIMED;
1004 }
1005 return DDI_INTR_UNCLAIMED;
1006}
1007
1008
1009/**
1010 * Interrupt Service Routine for VMMDev.
1011 *
1012 * @param Arg Private data (unused, will be NULL).
1013 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
1014 */
1015static uint_t vgdrvSolarisISR(caddr_t Arg)
1016{
1017 LogFlow(("vgdrvSolarisISR:\n"));
1018
1019 /* The mutex is required to protect against parallel executions (if possible?) and also the
1020 mouse notify registeration race between VGDrvNativeSetMouseNotifyCallback() and VGDrvCommonISR(). */
1021 mutex_enter(&g_IrqMtx);
1022 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
1023 mutex_exit(&g_IrqMtx);
1024
1025 return fOurIRQ ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED;
1026}
1027
1028
1029/**
1030 * Poll notifier for mouse poll events.
1031 *
1032 * @param pDevExt Pointer to the device extension.
1033 *
1034 * @remarks This must be called without holding any spinlocks.
1035 */
1036void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
1037{
1038 LogFlow(("VGDrvNativeISRMousePollEvent:\n"));
1039
1040 /*
1041 * Wake up poll waiters.
1042 */
1043 pollwakeup(&g_PollHead, POLLIN | POLLRDNORM);
1044}
1045
1046
1047/**
1048 * Sets the mouse notification callback.
1049 *
1050 * @returns VBox status code.
1051 * @param pDevExt Pointer to the device extension.
1052 * @param pNotify Pointer to the mouse notify struct.
1053 */
1054int VGDrvNativeSetMouseNotifyCallback(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCSETMOUSENOTIFYCALLBACK pNotify)
1055{
1056 /* Take the mutex here so as to not race with VGDrvCommonISR() which invokes the mouse notify callback. */
1057 mutex_enter(&g_IrqMtx);
1058 pDevExt->pfnMouseNotifyCallback = pNotify->u.In.pfnNotify;
1059 pDevExt->pvMouseNotifyCallbackArg = pNotify->u.In.pvUser;
1060 mutex_exit(&g_IrqMtx);
1061 return VINF_SUCCESS;
1062}
1063
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