VirtualBox

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

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

backed out r118835 as it incorrectly updated the 'This file is based on' file headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.7 KB
Line 
1/* $Id: VBoxGuest-solaris.c 69498 2017-10-28 15:07:25Z 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 *
486 * @remarks fFlags are the flags passed to open() or to ldi_open_by_name. In
487 * the latter case the FKLYR flag is added to indicate that the caller
488 * is a kernel component rather than user land.
489 */
490static int vgdrvSolarisOpen(dev_t *pDev, int fFlags, int fType, cred_t *pCred)
491{
492 int rc;
493 PVBOXGUESTSESSION pSession = NULL;
494
495 LogFlow(("vgdrvSolarisOpen:\n"));
496
497 /*
498 * Verify we are being opened as a character device.
499 */
500 if (fType != OTYP_CHR)
501 return EINVAL;
502
503 vboxguest_state_t *pState = NULL;
504 unsigned iOpenInstance;
505 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
506 {
507 if ( !ddi_get_soft_state(g_pvgdrvSolarisState, iOpenInstance) /* faster */
508 && ddi_soft_state_zalloc(g_pvgdrvSolarisState, iOpenInstance) == DDI_SUCCESS)
509 {
510 pState = ddi_get_soft_state(g_pvgdrvSolarisState, iOpenInstance);
511 break;
512 }
513 }
514 if (!pState)
515 {
516 Log(("vgdrvSolarisOpen: too many open instances."));
517 return ENXIO;
518 }
519
520 /*
521 * Create a new session.
522 */
523 if (!(fFlags & FKLYR))
524 rc = VGDrvCommonCreateUserSession(&g_DevExt, &pSession);
525 else
526 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
527 if (RT_SUCCESS(rc))
528 {
529 if (!(fFlags & FKLYR))
530 pState->pvProcRef = proc_ref();
531 else
532 pState->pvProcRef = NULL;
533 pState->pSession = pSession;
534 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
535 Log(("vgdrvSolarisOpen: pSession=%p pState=%p pid=%d\n", pSession, pState, (int)RTProcSelf()));
536 return 0;
537 }
538
539 /* Failed, clean up. */
540 ddi_soft_state_free(g_pvgdrvSolarisState, iOpenInstance);
541
542 LogRel((DEVICE_NAME "::Open: VGDrvCommonCreateUserSession failed. rc=%d\n", rc));
543 return EFAULT;
544}
545
546
547static int vgdrvSolarisClose(dev_t Dev, int flag, int fType, cred_t *pCred)
548{
549 LogFlow(("vgdrvSolarisClose: pid=%d\n", (int)RTProcSelf()));
550
551 PVBOXGUESTSESSION pSession = NULL;
552 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
553 if (!pState)
554 {
555 Log(("vgdrvSolarisClose: failed to get pState.\n"));
556 return EFAULT;
557 }
558
559 if (pState->pvProcRef != NULL)
560 {
561 proc_unref(pState->pvProcRef);
562 pState->pvProcRef = NULL;
563 }
564 pSession = pState->pSession;
565 pState->pSession = NULL;
566 Log(("vgdrvSolarisClose: pSession=%p pState=%p\n", pSession, pState));
567 ddi_soft_state_free(g_pvgdrvSolarisState, getminor(Dev));
568 if (!pSession)
569 {
570 Log(("vgdrvSolarisClose: failed to get pSession.\n"));
571 return EFAULT;
572 }
573
574 /*
575 * Close the session.
576 */
577 if (pSession)
578 VGDrvCommonCloseSession(&g_DevExt, pSession);
579 return 0;
580}
581
582
583static int vgdrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
584{
585 LogFlow((DEVICE_NAME "::Read\n"));
586
587 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
588 if (!pState)
589 {
590 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
591 return EFAULT;
592 }
593
594 PVBOXGUESTSESSION pSession = pState->pSession;
595 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
596 if (pSession->u32MousePosChangedSeq != u32CurSeq)
597 pSession->u32MousePosChangedSeq = u32CurSeq;
598
599 return 0;
600}
601
602
603static int vgdrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
604{
605 LogFlow(("vgdrvSolarisWrite:\n"));
606 return 0;
607}
608
609
610/** @def IOCPARM_LEN
611 * Gets the length from the ioctl number.
612 * This is normally defined by sys/ioccom.h on BSD systems...
613 */
614#ifndef IOCPARM_LEN
615# define IOCPARM_LEN(x) ( ((x) >> 16) & IOCPARM_MASK )
616#endif
617
618
619/**
620 * Driver ioctl, an alternate entry point for this character driver.
621 *
622 * @param Dev Device number
623 * @param iCmd Operation identifier
624 * @param iArgs Arguments from user to driver
625 * @param Mode Information bitfield (read/write, address space etc.)
626 * @param pCred User credentials
627 * @param pVal Return value for calling process.
628 *
629 * @return corresponding solaris error code.
630 */
631static int vgdrvSolarisIOCtl(dev_t Dev, int iCmd, intptr_t iArgs, int Mode, cred_t *pCred, int *pVal)
632{
633 /*
634 * Get the session from the soft state item.
635 */
636 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
637 if (!pState)
638 {
639 LogRel(("vgdrvSolarisIOCtl: no state data for %#x (%d)\n", Dev, getminor(Dev)));
640 return EINVAL;
641 }
642
643 PVBOXGUESTSESSION pSession = pState->pSession;
644 if (!pSession)
645 {
646 LogRel(("vgdrvSolarisIOCtl: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
647 return DDI_SUCCESS;
648 }
649
650 /*
651 * Deal with fast requests.
652 */
653 if (VBGL_IOCTL_IS_FAST(iCmd))
654 {
655 *pVal = VGDrvCommonIoCtlFast(iCmd, &g_DevExt, pSession);
656 return 0;
657 }
658
659 /*
660 * It's kind of simple if this is a kernel session, take slow path if user land.
661 */
662 if (pSession->R0Process == NIL_RTR0PROCESS)
663 {
664 if (IOCPARM_LEN(iCmd) == sizeof(VBGLREQHDR))
665 {
666 PVBGLREQHDR pHdr = (PVBGLREQHDR)iArgs;
667 int rc;
668 if (iCmd != VBGL_IOCTL_IDC_DISCONNECT)
669 rc =VGDrvCommonIoCtl(iCmd, &g_DevExt, pSession, pHdr, RT_MAX(pHdr->cbIn, pHdr->cbOut));
670 else
671 {
672 pState->pSession = NULL;
673 rc = VGDrvCommonIoCtl(iCmd, &g_DevExt, pSession, pHdr, RT_MAX(pHdr->cbIn, pHdr->cbOut));
674 if (RT_FAILURE(rc))
675 pState->pSession = pSession;
676 }
677 return rc;
678 }
679 }
680
681 return vgdrvSolarisIOCtlSlow(pSession, iCmd, Mode, iArgs);
682}
683
684
685/**
686 * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
687 *
688 * @returns Solaris errno.
689 *
690 * @param pSession The session.
691 * @param iCmd The IOCtl command.
692 * @param Mode Information bitfield (for specifying ownership of data)
693 * @param iArg User space address of the request buffer.
694 */
695static int vgdrvSolarisIOCtlSlow(PVBOXGUESTSESSION pSession, int iCmd, int Mode, intptr_t iArg)
696{
697 int rc;
698 uint32_t cbBuf = 0;
699 union
700 {
701 VBGLREQHDR Hdr;
702 uint8_t abBuf[64];
703 } StackBuf;
704 PVBGLREQHDR pHdr;
705
706
707 /*
708 * Read the header.
709 */
710 if (RT_UNLIKELY(IOCPARM_LEN(iCmd) != sizeof(StackBuf.Hdr)))
711 {
712 LogRel(("vgdrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(StackBuf.Hdr)));
713 return EINVAL;
714 }
715 rc = ddi_copyin((void *)iArg, &StackBuf.Hdr, sizeof(StackBuf.Hdr), Mode);
716 if (RT_UNLIKELY(rc))
717 {
718 LogRel(("vgdrvSolarisIOCtlSlow: ddi_copyin(,%#lx,) failed; iCmd=%#x. rc=%d\n", iArg, iCmd, rc));
719 return EFAULT;
720 }
721 if (RT_UNLIKELY(StackBuf.Hdr.uVersion != VBGLREQHDR_VERSION))
722 {
723 LogRel(("vgdrvSolarisIOCtlSlow: bad header version %#x; iCmd=%#x\n", StackBuf.Hdr.uVersion, iCmd));
724 return EINVAL;
725 }
726 cbBuf = RT_MAX(StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut);
727 if (RT_UNLIKELY( StackBuf.Hdr.cbIn < sizeof(StackBuf.Hdr)
728 || (StackBuf.Hdr.cbOut < sizeof(StackBuf.Hdr) && StackBuf.Hdr.cbOut != 0)
729 || cbBuf > _1M*16))
730 {
731 LogRel(("vgdrvSolarisIOCtlSlow: max(%#x,%#x); iCmd=%#x\n", StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut, iCmd));
732 return EINVAL;
733 }
734
735 /*
736 * Buffer the request.
737 *
738 * Note! Common code revalidates the header sizes and version. So it's
739 * fine to read it once more.
740 */
741 if (cbBuf <= sizeof(StackBuf))
742 pHdr = &StackBuf.Hdr;
743 else
744 {
745 pHdr = RTMemTmpAlloc(cbBuf);
746 if (RT_UNLIKELY(!pHdr))
747 {
748 LogRel(("vgdrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
749 return ENOMEM;
750 }
751 }
752 rc = ddi_copyin((void *)iArg, pHdr, cbBuf, Mode);
753 if (RT_UNLIKELY(rc))
754 {
755 LogRel(("vgdrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, cbBuf, iCmd, rc));
756 if (pHdr != &StackBuf.Hdr)
757 RTMemFree(pHdr);
758 return EFAULT;
759 }
760
761 /*
762 * Process the IOCtl.
763 */
764 rc = VGDrvCommonIoCtl(iCmd, &g_DevExt, pSession, pHdr, cbBuf);
765
766 /*
767 * Copy ioctl data and output buffer back to user space.
768 */
769 if (RT_SUCCESS(rc))
770 {
771 uint32_t cbOut = pHdr->cbOut;
772 if (RT_UNLIKELY(cbOut > cbBuf))
773 {
774 LogRel(("vgdrvSolarisIOCtlSlow: too much output! %#x > %#x; iCmd=%#x!\n", cbOut, cbBuf, iCmd));
775 cbOut = cbBuf;
776 }
777 rc = ddi_copyout(pHdr, (void *)iArg, cbOut, Mode);
778 if (RT_UNLIKELY(rc != 0))
779 {
780 /* this is really bad */
781 LogRel(("vgdrvSolarisIOCtlSlow: ddi_copyout(,%p,%d) failed. rc=%d\n", (void *)iArg, cbBuf, rc));
782 rc = EFAULT;
783 }
784 }
785 else
786 rc = EINVAL;
787
788 if (pHdr != &StackBuf.Hdr)
789 RTMemTmpFree(pHdr);
790 return rc;
791}
792
793
794#if 0
795/**
796 * @note This code is duplicated on other platforms with variations, so please
797 * keep them all up to date when making changes!
798 */
799int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
800{
801 /*
802 * Simple request validation (common code does the rest).
803 */
804 int rc;
805 if ( RT_VALID_PTR(pReqHdr)
806 && cbReq >= sizeof(*pReqHdr))
807 {
808 /*
809 * All requests except the connect one requires a valid session.
810 */
811 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
812 if (pSession)
813 {
814 if ( RT_VALID_PTR(pSession)
815 && pSession->pDevExt == &g_DevExt)
816 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
817 else
818 rc = VERR_INVALID_HANDLE;
819 }
820 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
821 {
822 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
823 if (RT_SUCCESS(rc))
824 {
825 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
826 if (RT_FAILURE(rc))
827 VGDrvCommonCloseSession(&g_DevExt, pSession);
828 }
829 }
830 else
831 rc = VERR_INVALID_HANDLE;
832 }
833 else
834 rc = VERR_INVALID_POINTER;
835 return rc;
836}
837#endif
838
839
840static int vgdrvSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
841{
842 LogFlow(("vgdrvSolarisPoll: fEvents=%d fAnyYet=%d\n", fEvents, fAnyYet));
843
844 vboxguest_state_t *pState = ddi_get_soft_state(g_pvgdrvSolarisState, getminor(Dev));
845 if (RT_LIKELY(pState))
846 {
847 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pState->pSession;
848 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
849 if (pSession->u32MousePosChangedSeq != u32CurSeq)
850 {
851 *pReqEvents |= (POLLIN | POLLRDNORM);
852 pSession->u32MousePosChangedSeq = u32CurSeq;
853 }
854 else
855 {
856 *pReqEvents = 0;
857 if (!fAnyYet)
858 *ppPollHead = &g_PollHead;
859 }
860
861 return 0;
862 }
863
864 Log(("vgdrvSolarisPoll: no state data for %d\n", getminor(Dev)));
865 return EINVAL;
866}
867
868
869/**
870 * Sets IRQ for VMMDev.
871 *
872 * @returns Solaris error code.
873 * @param pDip Pointer to the device info structure.
874 */
875static int vgdrvSolarisAddIRQ(dev_info_t *pDip)
876{
877 LogFlow(("vgdrvSolarisAddIRQ: pDip=%p\n", pDip));
878
879 /* Get the types of interrupt supported for this hardware. */
880 int fIntrType = 0;
881 int rc = ddi_intr_get_supported_types(pDip, &fIntrType);
882 if (rc == DDI_SUCCESS)
883 {
884 /* We only support fixed interrupts at this point, not MSIs. */
885 if (fIntrType & DDI_INTR_TYPE_FIXED)
886 {
887 /* Verify the number of interrupts supported by this device. There can only be one fixed interrupt. */
888 int cIntrCount = 0;
889 rc = ddi_intr_get_nintrs(pDip, fIntrType, &cIntrCount);
890 if ( rc == DDI_SUCCESS
891 && cIntrCount == 1)
892 {
893 /* Allocated kernel memory for the interrupt handle. The allocation size is stored internally. */
894 g_pahIntrs = RTMemAllocZ(cIntrCount * sizeof(ddi_intr_handle_t));
895 if (g_pahIntrs)
896 {
897 /* Allocate the interrupt for this device and verify the allocation. */
898 int cIntrAllocated;
899 rc = ddi_intr_alloc(pDip, g_pahIntrs, fIntrType, 0 /* interrupt number */, cIntrCount, &cIntrAllocated,
900 DDI_INTR_ALLOC_NORMAL);
901 if ( rc == DDI_SUCCESS
902 && cIntrAllocated == 1)
903 {
904 /* Get the interrupt priority assigned by the system. */
905 uint_t uIntrPriority;
906 rc = ddi_intr_get_pri(g_pahIntrs[0], &uIntrPriority);
907 if (rc == DDI_SUCCESS)
908 {
909 /* Check if the interrupt priority is scheduler level or above, if so we need to use a high-level
910 and low-level interrupt handlers with corresponding mutexes. */
911 cmn_err(CE_CONT, "!vboxguest: uIntrPriority=%d hilevel_pri=%d\n", uIntrPriority, ddi_intr_get_hilevel_pri());
912 if (uIntrPriority >= ddi_intr_get_hilevel_pri())
913 {
914 /* Initialize the high-level mutex. */
915 mutex_init(&g_HighLevelIrqMtx, NULL /* pszDesc */, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
916
917 /* Assign interrupt handler function to the interrupt handle. */
918 rc = ddi_intr_add_handler(g_pahIntrs[0], (ddi_intr_handler_t *)&vgdrvSolarisHighLevelISR,
919 NULL /* pvArg1 */, NULL /* pvArg2 */);
920
921 if (rc == DDI_SUCCESS)
922 {
923 /* Add the low-level interrupt handler. */
924 rc = ddi_intr_add_softint(pDip, &g_hSoftIntr, DDI_INTR_SOFTPRI_MAX,
925 (ddi_intr_handler_t *)&vgdrvSolarisISR, NULL /* pvArg1 */);
926 if (rc == DDI_SUCCESS)
927 {
928 /* Initialize the low-level mutex at the corresponding level. */
929 mutex_init(&g_IrqMtx, NULL /* pszDesc */, MUTEX_DRIVER,
930 DDI_INTR_PRI(DDI_INTR_SOFTPRI_MAX));
931
932 g_fSoftIntRegistered = true;
933 /* Enable the high-level interrupt. */
934 rc = ddi_intr_enable(g_pahIntrs[0]);
935 if (rc == DDI_SUCCESS)
936 return rc;
937
938 LogRel((DEVICE_NAME "::AddIRQ: failed to enable interrupt. rc=%d\n", rc));
939 mutex_destroy(&g_IrqMtx);
940 }
941 else
942 LogRel((DEVICE_NAME "::AddIRQ: failed to add soft interrupt handler. rc=%d\n", rc));
943
944 ddi_intr_remove_handler(g_pahIntrs[0]);
945 }
946 else
947 LogRel((DEVICE_NAME "::AddIRQ: failed to add high-level interrupt handler. rc=%d\n", rc));
948
949 mutex_destroy(&g_HighLevelIrqMtx);
950 }
951 else
952 {
953 /* Interrupt handler runs at reschedulable level, initialize the mutex at the given priority. */
954 mutex_init(&g_IrqMtx, NULL /* pszDesc */, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
955
956 /* Assign interrupt handler function to the interrupt handle. */
957 rc = ddi_intr_add_handler(g_pahIntrs[0], (ddi_intr_handler_t *)vgdrvSolarisISR,
958 NULL /* pvArg1 */, NULL /* pvArg2 */);
959 if (rc == DDI_SUCCESS)
960 {
961 /* Enable the interrupt. */
962 rc = ddi_intr_enable(g_pahIntrs[0]);
963 if (rc == DDI_SUCCESS)
964 return rc;
965
966 LogRel((DEVICE_NAME "::AddIRQ: failed to enable interrupt. rc=%d\n", rc));
967 mutex_destroy(&g_IrqMtx);
968 }
969 }
970 }
971 else
972 LogRel((DEVICE_NAME "::AddIRQ: failed to get priority of interrupt. rc=%d\n", rc));
973
974 Assert(cIntrAllocated == 1);
975 ddi_intr_free(g_pahIntrs[0]);
976 }
977 else
978 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", cIntrCount));
979 RTMemFree(g_pahIntrs);
980 }
981 else
982 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", cIntrCount));
983 }
984 else
985 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient number of IRQs. rc=%d cIntrCount=%d\n", rc, cIntrCount));
986 }
987 else
988 LogRel((DEVICE_NAME "::AddIRQ: fixed-type interrupts not supported. IntrType=%#x\n", fIntrType));
989 }
990 else
991 LogRel((DEVICE_NAME "::AddIRQ: failed to get supported interrupt types. rc=%d\n", rc));
992 return rc;
993}
994
995
996/**
997 * Removes IRQ for VMMDev.
998 *
999 * @param pDip Pointer to the device info structure.
1000 */
1001static void vgdrvSolarisRemoveIRQ(dev_info_t *pDip)
1002{
1003 LogFlow(("vgdrvSolarisRemoveIRQ:\n"));
1004
1005 int rc = ddi_intr_disable(g_pahIntrs[0]);
1006 if (rc == DDI_SUCCESS)
1007 {
1008 rc = ddi_intr_remove_handler(g_pahIntrs[0]);
1009 if (rc == DDI_SUCCESS)
1010 ddi_intr_free(g_pahIntrs[0]);
1011 }
1012
1013 if (g_fSoftIntRegistered)
1014 {
1015 ddi_intr_remove_softint(g_hSoftIntr);
1016 mutex_destroy(&g_HighLevelIrqMtx);
1017 g_fSoftIntRegistered = false;
1018 }
1019
1020 mutex_destroy(&g_IrqMtx);
1021 RTMemFree(g_pahIntrs);
1022}
1023
1024
1025/**
1026 * High-level Interrupt Service Routine for VMMDev.
1027 *
1028 * This routine simply dispatches a soft-interrupt at an acceptable IPL as
1029 * VGDrvCommonISR() cannot be called at a high IPL (scheduler level or higher)
1030 * due to pollwakeup() in VGDrvNativeISRMousePollEvent().
1031 *
1032 * @param Arg Private data (unused, will be NULL).
1033 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
1034 */
1035static uint_t vgdrvSolarisHighLevelISR(caddr_t Arg)
1036{
1037 bool const fOurIrq = VGDrvCommonIsOurIRQ(&g_DevExt);
1038 if (fOurIrq)
1039 {
1040 ddi_intr_trigger_softint(g_hSoftIntr, NULL /* Arg */);
1041 return DDI_INTR_CLAIMED;
1042 }
1043 return DDI_INTR_UNCLAIMED;
1044}
1045
1046
1047/**
1048 * Interrupt Service Routine for VMMDev.
1049 *
1050 * @param Arg Private data (unused, will be NULL).
1051 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
1052 */
1053static uint_t vgdrvSolarisISR(caddr_t Arg)
1054{
1055 LogFlow(("vgdrvSolarisISR:\n"));
1056
1057 /* The mutex is required to protect against parallel executions (if possible?) and also the
1058 mouse notify registeration race between VGDrvNativeSetMouseNotifyCallback() and VGDrvCommonISR(). */
1059 mutex_enter(&g_IrqMtx);
1060 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
1061 mutex_exit(&g_IrqMtx);
1062
1063 return fOurIRQ ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED;
1064}
1065
1066
1067/**
1068 * Poll notifier for mouse poll events.
1069 *
1070 * @param pDevExt Pointer to the device extension.
1071 *
1072 * @remarks This must be called without holding any spinlocks.
1073 */
1074void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
1075{
1076 LogFlow(("VGDrvNativeISRMousePollEvent:\n"));
1077
1078 /*
1079 * Wake up poll waiters.
1080 */
1081 pollwakeup(&g_PollHead, POLLIN | POLLRDNORM);
1082}
1083
1084
1085/**
1086 * Sets the mouse notification callback.
1087 *
1088 * @returns VBox status code.
1089 * @param pDevExt Pointer to the device extension.
1090 * @param pNotify Pointer to the mouse notify struct.
1091 */
1092int VGDrvNativeSetMouseNotifyCallback(PVBOXGUESTDEVEXT pDevExt, PVBGLIOCSETMOUSENOTIFYCALLBACK pNotify)
1093{
1094 /* Take the mutex here so as to not race with VGDrvCommonISR() which invokes the mouse notify callback. */
1095 mutex_enter(&g_IrqMtx);
1096 pDevExt->pfnMouseNotifyCallback = pNotify->u.In.pfnNotify;
1097 pDevExt->pvMouseNotifyCallbackArg = pNotify->u.In.pvUser;
1098 mutex_exit(&g_IrqMtx);
1099 return VINF_SUCCESS;
1100}
1101
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