VirtualBox

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

Last change on this file since 34079 was 33109, checked in by vboxsync, 14 years ago

Additions/solaris: Initialize IPRT as early as possible not on Attach. Fixes calling vbi_is_preempt_enabled without initializing vbi errors/warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.9 KB
Line 
1/* $Id: VBoxGuest-solaris.c 33109 2010-10-13 15:11:10Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for Solaris.
4 */
5
6/*
7 * Copyright (C) 2007 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <sys/conf.h>
23#include <sys/modctl.h>
24#include <sys/mutex.h>
25#include <sys/pci.h>
26#include <sys/stat.h>
27#include <sys/ddi.h>
28#include <sys/ddi_intr.h>
29#include <sys/sunddi.h>
30#include <sys/open.h>
31#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
32
33#include "VBoxGuestInternal.h"
34#include <VBox/log.h>
35#include <VBox/version.h>
36#include <iprt/assert.h>
37#include <iprt/initterm.h>
38#include <iprt/process.h>
39#include <iprt/mem.h>
40#include <iprt/cdefs.h>
41#include <iprt/asm.h>
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** The module name. */
48#define DEVICE_NAME "vboxguest"
49/** The module description as seen in 'modinfo'. */
50#define DEVICE_DESC "VirtualBox GstDrv"
51
52
53/*******************************************************************************
54* Internal Functions *
55*******************************************************************************/
56static int VBoxGuestSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
57static int VBoxGuestSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
58static int VBoxGuestSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
59static int VBoxGuestSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
60static int VBoxGuestSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
61static int VBoxGuestSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead);
62
63static int VBoxGuestSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
64static int VBoxGuestSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
65static int VBoxGuestSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
66
67static int VBoxGuestSolarisAddIRQ(dev_info_t *pDip);
68static void VBoxGuestSolarisRemoveIRQ(dev_info_t *pDip);
69static uint_t VBoxGuestSolarisISR(caddr_t Arg);
70
71
72/*******************************************************************************
73* Structures and Typedefs *
74*******************************************************************************/
75/**
76 * cb_ops: for drivers that support char/block entry points
77 */
78static struct cb_ops g_VBoxGuestSolarisCbOps =
79{
80 VBoxGuestSolarisOpen,
81 VBoxGuestSolarisClose,
82 nodev, /* b strategy */
83 nodev, /* b dump */
84 nodev, /* b print */
85 VBoxGuestSolarisRead,
86 VBoxGuestSolarisWrite,
87 VBoxGuestSolarisIOCtl,
88 nodev, /* c devmap */
89 nodev, /* c mmap */
90 nodev, /* c segmap */
91 VBoxGuestSolarisPoll,
92 ddi_prop_op, /* property ops */
93 NULL, /* streamtab */
94 D_NEW | D_MP, /* compat. flag */
95 CB_REV /* revision */
96};
97
98/**
99 * dev_ops: for driver device operations
100 */
101static struct dev_ops g_VBoxGuestSolarisDevOps =
102{
103 DEVO_REV, /* driver build revision */
104 0, /* ref count */
105 VBoxGuestSolarisGetInfo,
106 nulldev, /* identify */
107 nulldev, /* probe */
108 VBoxGuestSolarisAttach,
109 VBoxGuestSolarisDetach,
110 nodev, /* reset */
111 &g_VBoxGuestSolarisCbOps,
112 (struct bus_ops *)0,
113 nodev /* power */
114};
115
116/**
117 * modldrv: export driver specifics to the kernel
118 */
119static struct modldrv g_VBoxGuestSolarisModule =
120{
121 &mod_driverops, /* extern from kernel */
122 DEVICE_DESC " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
123 &g_VBoxGuestSolarisDevOps
124};
125
126/**
127 * modlinkage: export install/remove/info to the kernel
128 */
129static struct modlinkage g_VBoxGuestSolarisModLinkage =
130{
131 MODREV_1, /* loadable module system revision */
132 &g_VBoxGuestSolarisModule,
133 NULL /* terminate array of linkage structures */
134};
135
136/**
137 * State info for each open file handle.
138 */
139typedef struct
140{
141 /** Pointer to the session handle. */
142 PVBOXGUESTSESSION pSession;
143 /** The process reference for posting signals */
144 void *pvProcRef;
145} vboxguest_state_t;
146
147
148/*******************************************************************************
149* Global Variables *
150*******************************************************************************/
151/** Device handle (we support only one instance). */
152static dev_info_t *g_pDip = NULL;
153/** Opaque pointer to file-descriptor states */
154static void *g_pVBoxGuestSolarisState = NULL;
155/** Device extention & session data association structure. */
156static VBOXGUESTDEVEXT g_DevExt;
157/** IO port handle. */
158static ddi_acc_handle_t g_PciIOHandle;
159/** MMIO handle. */
160static ddi_acc_handle_t g_PciMMIOHandle;
161/** IO Port. */
162static uint16_t g_uIOPortBase;
163/** Address of the MMIO region.*/
164static caddr_t g_pMMIOBase;
165/** Size of the MMIO region. */
166static off_t g_cbMMIO;
167/** Pointer to the interrupt handle vector */
168static ddi_intr_handle_t *g_pIntr;
169/** Number of actually allocated interrupt handles */
170static size_t g_cIntrAllocated;
171/** The pollhead structure */
172static pollhead_t g_PollHead;
173/** The IRQ Mutex */
174static kmutex_t g_IrqMtx;
175
176/**
177 * Kernel entry points
178 */
179int _init(void)
180{
181 /*
182 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
183 */
184 int rc = RTR0Init(0);
185 if (RT_SUCCESS(rc))
186 {
187 PRTLOGGER pRelLogger;
188 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
189 rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
190 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
191 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
192 if (RT_SUCCESS(rc))
193 RTLogRelSetDefaultInstance(pRelLogger);
194 else
195 cmn_err(CE_NOTE, "failed to initialize driver logging rc=%d!\n", rc);
196
197 /*
198 * Prevent module autounloading.
199 */
200 modctl_t *pModCtl = mod_getctl(&g_VBoxGuestSolarisModLinkage);
201 if (pModCtl)
202 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
203 else
204 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
205
206 rc = ddi_soft_state_init(&g_pVBoxGuestSolarisState, sizeof(vboxguest_state_t), 1);
207 if (!rc)
208 {
209 rc = mod_install(&g_VBoxGuestSolarisModLinkage);
210 if (rc)
211 ddi_soft_state_fini(&g_pVBoxGuestSolarisState);
212 }
213 }
214 else
215 {
216 cmn_err(CE_NOTE, "_init: RTR0Init failed. rc=%d\n", rc);
217 return EINVAL;
218 }
219
220 return rc;
221}
222
223
224int _fini(void)
225{
226 LogFlow((DEVICE_NAME ":_fini\n"));
227 int rc = mod_remove(&g_VBoxGuestSolarisModLinkage);
228 if (!rc)
229 ddi_soft_state_fini(&g_pVBoxGuestSolarisState);
230
231 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
232 RTLogDestroy(RTLogSetDefaultInstance(NULL));
233
234 RTR0Term();
235 return rc;
236}
237
238
239int _info(struct modinfo *pModInfo)
240{
241 LogFlow((DEVICE_NAME ":_info\n"));
242 return mod_info(&g_VBoxGuestSolarisModLinkage, pModInfo);
243}
244
245
246/**
247 * Attach entry point, to attach a device to the system or resume it.
248 *
249 * @param pDip The module structure instance.
250 * @param enmCmd Attach type (ddi_attach_cmd_t)
251 *
252 * @return corresponding solaris error code.
253 */
254static int VBoxGuestSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
255{
256 LogFlow((DEVICE_NAME "::Attach\n"));
257 switch (enmCmd)
258 {
259 case DDI_ATTACH:
260 {
261 if (g_pDip)
262 {
263 LogRel((DEVICE_NAME "::Attach: Only one instance supported.\n"));
264 return DDI_FAILURE;
265 }
266
267 int instance = ddi_get_instance(pDip);
268
269 /*
270 * Enable resources for PCI access.
271 */
272 ddi_acc_handle_t PciHandle;
273 int rc = pci_config_setup(pDip, &PciHandle);
274 if (rc == DDI_SUCCESS)
275 {
276 /*
277 * Map the register address space.
278 */
279 caddr_t baseAddr;
280 ddi_device_acc_attr_t deviceAttr;
281 deviceAttr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
282 deviceAttr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
283 deviceAttr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
284 deviceAttr.devacc_attr_access = DDI_DEFAULT_ACC;
285 rc = ddi_regs_map_setup(pDip, 1, &baseAddr, 0, 0, &deviceAttr, &g_PciIOHandle);
286 if (rc == DDI_SUCCESS)
287 {
288 /*
289 * Read size of the MMIO region.
290 */
291 g_uIOPortBase = (uintptr_t)baseAddr;
292 rc = ddi_dev_regsize(pDip, 2, &g_cbMMIO);
293 if (rc == DDI_SUCCESS)
294 {
295 rc = ddi_regs_map_setup(pDip, 2, &g_pMMIOBase, 0, g_cbMMIO, &deviceAttr,
296 &g_PciMMIOHandle);
297 if (rc == DDI_SUCCESS)
298 {
299 /*
300 * Add IRQ of VMMDev.
301 */
302 rc = VBoxGuestSolarisAddIRQ(pDip);
303 if (rc == DDI_SUCCESS)
304 {
305 /*
306 * Call the common device extension initializer.
307 */
308 rc = VBoxGuestInitDevExt(&g_DevExt, g_uIOPortBase, g_pMMIOBase,
309 g_cbMMIO, VBOXOSTYPE_Solaris,
310 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
311 if (RT_SUCCESS(rc))
312 {
313 rc = ddi_create_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0);
314 if (rc == DDI_SUCCESS)
315 {
316 g_pDip = pDip;
317 pci_config_teardown(&PciHandle);
318 return DDI_SUCCESS;
319 }
320
321 LogRel((DEVICE_NAME "::Attach: ddi_create_minor_node failed.\n"));
322 VBoxGuestDeleteDevExt(&g_DevExt);
323 }
324 else
325 LogRel((DEVICE_NAME "::Attach: VBoxGuestInitDevExt failed.\n"));
326 VBoxGuestSolarisRemoveIRQ(pDip);
327 }
328 else
329 LogRel((DEVICE_NAME "::Attach: VBoxGuestSolarisAddIRQ failed.\n"));
330 ddi_regs_map_free(&g_PciMMIOHandle);
331 }
332 else
333 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for MMIO region failed.\n"));
334 }
335 else
336 LogRel((DEVICE_NAME "::Attach: ddi_dev_regsize for MMIO region failed.\n"));
337 ddi_regs_map_free(&g_PciIOHandle);
338 }
339 else
340 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for IOport failed.\n"));
341 pci_config_teardown(&PciHandle);
342 }
343 else
344 LogRel((DEVICE_NAME "::Attach: pci_config_setup failed rc=%d.\n", rc));
345 return DDI_FAILURE;
346 }
347
348 case DDI_RESUME:
349 {
350 /** @todo implement resume for guest driver. */
351 return DDI_SUCCESS;
352 }
353
354 default:
355 return DDI_FAILURE;
356 }
357}
358
359
360/**
361 * Detach entry point, to detach a device to the system or suspend it.
362 *
363 * @param pDip The module structure instance.
364 * @param enmCmd Attach type (ddi_attach_cmd_t)
365 *
366 * @return corresponding solaris error code.
367 */
368static int VBoxGuestSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
369{
370 LogFlow((DEVICE_NAME "::Detach\n"));
371 switch (enmCmd)
372 {
373 case DDI_DETACH:
374 {
375 VBoxGuestSolarisRemoveIRQ(pDip);
376 ddi_regs_map_free(&g_PciIOHandle);
377 ddi_regs_map_free(&g_PciMMIOHandle);
378 ddi_remove_minor_node(pDip, NULL);
379 VBoxGuestDeleteDevExt(&g_DevExt);
380 g_pDip = NULL;
381 return DDI_SUCCESS;
382 }
383
384 case DDI_SUSPEND:
385 {
386 /** @todo implement suspend for guest driver. */
387 return DDI_SUCCESS;
388 }
389
390 default:
391 return DDI_FAILURE;
392 }
393}
394
395
396/**
397 * Info entry point, called by solaris kernel for obtaining driver info.
398 *
399 * @param pDip The module structure instance (do not use).
400 * @param enmCmd Information request type.
401 * @param pvArg Type specific argument.
402 * @param ppvResult Where to store the requested info.
403 *
404 * @return corresponding solaris error code.
405 */
406static int VBoxGuestSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
407{
408 LogFlow((DEVICE_NAME "::GetInfo\n"));
409
410 int rc = DDI_SUCCESS;
411 switch (enmCmd)
412 {
413 case DDI_INFO_DEVT2DEVINFO:
414 *ppvResult = (void *)g_pDip;
415 break;
416
417 case DDI_INFO_DEVT2INSTANCE:
418 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_pDip);
419 break;
420
421 default:
422 rc = DDI_FAILURE;
423 break;
424 }
425
426 NOREF(pvArg);
427 return rc;
428}
429
430
431/**
432 * User context entry points
433 */
434static int VBoxGuestSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
435{
436 int rc;
437 PVBOXGUESTSESSION pSession = NULL;
438
439 LogFlow((DEVICE_NAME "::Open\n"));
440
441 /*
442 * Verify we are being opened as a character device.
443 */
444 if (fType != OTYP_CHR)
445 return EINVAL;
446
447 vboxguest_state_t *pState = NULL;
448 unsigned iOpenInstance;
449 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
450 {
451 if ( !ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance) /* faster */
452 && ddi_soft_state_zalloc(g_pVBoxGuestSolarisState, iOpenInstance) == DDI_SUCCESS)
453 {
454 pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance);
455 break;
456 }
457 }
458 if (!pState)
459 {
460 Log((DEVICE_NAME "::Open: too many open instances."));
461 return ENXIO;
462 }
463
464 /*
465 * Create a new session.
466 */
467 rc = VBoxGuestCreateUserSession(&g_DevExt, &pSession);
468 if (RT_SUCCESS(rc))
469 {
470 pState->pvProcRef = proc_ref();
471 pState->pSession = pSession;
472 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
473 Log((DEVICE_NAME "::Open: pSession=%p pState=%p pid=%d\n", pSession, pState, (int)RTProcSelf()));
474 return 0;
475 }
476
477 /* Failed, clean up. */
478 ddi_soft_state_free(g_pVBoxGuestSolarisState, iOpenInstance);
479
480 LogRel((DEVICE_NAME "::Open: VBoxGuestCreateUserSession failed. rc=%d\n", rc));
481 return EFAULT;
482}
483
484
485static int VBoxGuestSolarisClose(dev_t Dev, int flag, int fType, cred_t *pCred)
486{
487 LogFlow((DEVICE_NAME "::Close pid=%d\n", (int)RTProcSelf()));
488
489 PVBOXGUESTSESSION pSession = NULL;
490 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
491 if (!pState)
492 {
493 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
494 return EFAULT;
495 }
496
497 proc_unref(pState->pvProcRef);
498 pSession = pState->pSession;
499 pState->pSession = NULL;
500 Log((DEVICE_NAME "::Close: pSession=%p pState=%p\n", pSession, pState));
501 ddi_soft_state_free(g_pVBoxGuestSolarisState, getminor(Dev));
502 if (!pSession)
503 {
504 Log((DEVICE_NAME "::Close: failed to get pSession.\n"));
505 return EFAULT;
506 }
507
508 /*
509 * Close the session.
510 */
511 VBoxGuestCloseSession(&g_DevExt, pSession);
512 return 0;
513}
514
515
516static int VBoxGuestSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
517{
518 LogFlow((DEVICE_NAME "::Read\n"));
519
520 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
521 if (!pState)
522 {
523 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
524 return EFAULT;
525 }
526
527 PVBOXGUESTSESSION pSession = pState->pSession;
528 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
529 if (pSession->u32MousePosChangedSeq != u32CurSeq)
530 pSession->u32MousePosChangedSeq = u32CurSeq;
531
532 return 0;
533}
534
535
536static int VBoxGuestSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
537{
538 LogFlow((DEVICE_NAME "::Write\n"));
539 return 0;
540}
541
542
543/** @def IOCPARM_LEN
544 * Gets the length from the ioctl number.
545 * This is normally defined by sys/ioccom.h on BSD systems...
546 */
547#ifndef IOCPARM_LEN
548# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
549#endif
550
551
552/**
553 * Driver ioctl, an alternate entry point for this character driver.
554 *
555 * @param Dev Device number
556 * @param Cmd Operation identifier
557 * @param pArg Arguments from user to driver
558 * @param Mode Information bitfield (read/write, address space etc.)
559 * @param pCred User credentials
560 * @param pVal Return value for calling process.
561 *
562 * @return corresponding solaris error code.
563 */
564static int VBoxGuestSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
565{
566 LogFlow((DEVICE_NAME ":VBoxGuestSolarisIOCtl\n"));
567
568 /*
569 * Get the session from the soft state item.
570 */
571 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
572 if (!pState)
573 {
574 LogRel((DEVICE_NAME "::IOCtl: no state data for %d\n", getminor(Dev)));
575 return EINVAL;
576 }
577
578 PVBOXGUESTSESSION pSession = pState->pSession;
579 if (!pSession)
580 {
581 LogRel((DEVICE_NAME "::IOCtl: no session data for %d\n", getminor(Dev)));
582 return EINVAL;
583 }
584
585 /*
586 * Read and validate the request wrapper.
587 */
588 VBGLBIGREQ ReqWrap;
589 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
590 {
591 LogRel((DEVICE_NAME "::IOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd), sizeof(ReqWrap)));
592 return ENOTTY;
593 }
594
595 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
596 if (RT_UNLIKELY(rc))
597 {
598 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%#x.\n", pArg, Cmd, rc));
599 return EINVAL;
600 }
601
602 if (ReqWrap.u32Magic != VBGLBIGREQ_MAGIC)
603 {
604 LogRel((DEVICE_NAME "::IOCtl: bad magic %#x; pArg=%p Cmd=%#x.\n", ReqWrap.u32Magic, pArg, Cmd));
605 return EINVAL;
606 }
607 if (RT_UNLIKELY(ReqWrap.cbData > _1M*16))
608 {
609 LogRel((DEVICE_NAME "::IOCtl: bad size %#x; pArg=%p Cmd=%#x.\n", ReqWrap.cbData, pArg, Cmd));
610 return EINVAL;
611 }
612
613 /*
614 * Read the request payload if any; requests like VBOXGUEST_IOCTL_CANCEL_ALL_WAITEVENTS have no data payload.
615 */
616 void *pvBuf = NULL;
617 if (RT_LIKELY(ReqWrap.cbData > 0))
618 {
619 pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
620 if (RT_UNLIKELY(!pvBuf))
621 {
622 LogRel((DEVICE_NAME "::IOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
623 return ENOMEM;
624 }
625
626 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
627 if (RT_UNLIKELY(rc))
628 {
629 RTMemTmpFree(pvBuf);
630 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
631 return EFAULT;
632 }
633 if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
634 {
635 RTMemTmpFree(pvBuf);
636 LogRel((DEVICE_NAME "::IOCtl: pvBuf invalid pointer %p\n", pvBuf));
637 return EINVAL;
638 }
639 }
640 Log((DEVICE_NAME "::IOCtl: pSession=%p pid=%d.\n", pSession, (int)RTProcSelf()));
641
642 /*
643 * Process the IOCtl.
644 */
645 size_t cbDataReturned = 0;
646 rc = VBoxGuestCommonIOCtl(Cmd, &g_DevExt, pSession, pvBuf, ReqWrap.cbData, &cbDataReturned);
647 if (RT_SUCCESS(rc))
648 {
649 rc = 0;
650 if (RT_UNLIKELY(cbDataReturned > ReqWrap.cbData))
651 {
652 LogRel((DEVICE_NAME "::IOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap.cbData));
653 cbDataReturned = ReqWrap.cbData;
654 }
655 if (cbDataReturned > 0)
656 {
657 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataReturned, Mode);
658 if (RT_UNLIKELY(rc))
659 {
660 LogRel((DEVICE_NAME "::IOCtl: ddi_copyout failed; pvBuf=%p pArg=%p cbDataReturned=%u Cmd=%d. rc=%d\n",
661 pvBuf, pArg, cbDataReturned, Cmd, rc));
662 rc = EFAULT;
663 }
664 }
665 }
666 else
667 {
668 /*
669 * We Log() instead of LogRel() here because VBOXGUEST_IOCTL_WAITEVENT can return VERR_TIMEOUT,
670 * VBOXGUEST_IOCTL_CANCEL_ALL_EVENTS can return VERR_INTERRUPTED and possibly more in the future;
671 * which are not really failures that require logging.
672 */
673 Log((DEVICE_NAME "::IOCtl: VBoxGuestCommonIOCtl failed. Cmd=%#x rc=%d\n", Cmd, rc));
674 rc = RTErrConvertToErrno(rc);
675 }
676 *pVal = rc;
677 if (pvBuf)
678 RTMemTmpFree(pvBuf);
679 return rc;
680}
681
682
683static int VBoxGuestSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
684{
685 LogFlow((DEVICE_NAME "::Poll: fEvents=%d fAnyYet=%d\n", fEvents, fAnyYet));
686
687 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
688 if (RT_LIKELY(pState))
689 {
690 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pState->pSession;
691 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
692 if (pSession->u32MousePosChangedSeq != u32CurSeq)
693 {
694 *pReqEvents |= (POLLIN | POLLRDNORM);
695 pSession->u32MousePosChangedSeq = u32CurSeq;
696 }
697 else
698 {
699 *pReqEvents = 0;
700 if (!fAnyYet)
701 *ppPollHead = &g_PollHead;
702 }
703
704 return 0;
705 }
706 else
707 {
708 Log((DEVICE_NAME "::Poll: no state data for %d\n", getminor(Dev)));
709 return EINVAL;
710 }
711}
712
713
714/**
715 * Sets IRQ for VMMDev.
716 *
717 * @returns Solaris error code.
718 * @param pDip Pointer to the device info structure.
719 */
720static int VBoxGuestSolarisAddIRQ(dev_info_t *pDip)
721{
722 LogFlow((DEVICE_NAME "::AddIRQ: pDip=%p\n", pDip));
723
724 int IntrType = 0;
725 int rc = ddi_intr_get_supported_types(pDip, &IntrType);
726 if (rc == DDI_SUCCESS)
727 {
728 /* We won't need to bother about MSIs. */
729 if (IntrType & DDI_INTR_TYPE_FIXED)
730 {
731 int IntrCount = 0;
732 rc = ddi_intr_get_nintrs(pDip, IntrType, &IntrCount);
733 if ( rc == DDI_SUCCESS
734 && IntrCount > 0)
735 {
736 int IntrAvail = 0;
737 rc = ddi_intr_get_navail(pDip, IntrType, &IntrAvail);
738 if ( rc == DDI_SUCCESS
739 && IntrAvail > 0)
740 {
741 /* Allocated kernel memory for the interrupt handles. The allocation size is stored internally. */
742 g_pIntr = RTMemAlloc(IntrCount * sizeof(ddi_intr_handle_t));
743 if (g_pIntr)
744 {
745 int IntrAllocated;
746 rc = ddi_intr_alloc(pDip, g_pIntr, IntrType, 0, IntrCount, &IntrAllocated, DDI_INTR_ALLOC_NORMAL);
747 if ( rc == DDI_SUCCESS
748 && IntrAllocated > 0)
749 {
750 g_cIntrAllocated = IntrAllocated;
751 uint_t uIntrPriority;
752 rc = ddi_intr_get_pri(g_pIntr[0], &uIntrPriority);
753 if (rc == DDI_SUCCESS)
754 {
755 /* Initialize the mutex. */
756 mutex_init(&g_IrqMtx, NULL, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
757
758 /* Assign interrupt handler functions and enable interrupts. */
759 for (int i = 0; i < IntrAllocated; i++)
760 {
761 rc = ddi_intr_add_handler(g_pIntr[i], (ddi_intr_handler_t *)VBoxGuestSolarisISR,
762 NULL /* No Private Data */, NULL);
763 if (rc == DDI_SUCCESS)
764 rc = ddi_intr_enable(g_pIntr[i]);
765 if (rc != DDI_SUCCESS)
766 {
767 /* Changing local IntrAllocated to hold so-far allocated handles for freeing. */
768 IntrAllocated = i;
769 break;
770 }
771 }
772 if (rc == DDI_SUCCESS)
773 return rc;
774
775 /* Remove any assigned handlers */
776 LogRel((DEVICE_NAME ":failed to assign IRQs allocated=%d\n", IntrAllocated));
777 for (int x = 0; x < IntrAllocated; x++)
778 ddi_intr_remove_handler(g_pIntr[x]);
779 }
780 else
781 LogRel((DEVICE_NAME "::AddIRQ: failed to get priority of interrupt. rc=%d\n", rc));
782
783 /* Remove allocated IRQs, too bad we can free only one handle at a time. */
784 for (int k = 0; k < g_cIntrAllocated; k++)
785 ddi_intr_free(g_pIntr[k]);
786 }
787 else
788 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
789 RTMemFree(g_pIntr);
790 }
791 else
792 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
793 }
794 else
795 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient available IRQs. rc=%d IntrAvail=%d\n", rc, IntrAvail));
796 }
797 else
798 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient number of IRQs. rc=%d IntrCount=%d\n", rc, IntrCount));
799 }
800 else
801 LogRel((DEVICE_NAME "::AddIRQ: invalid irq type. IntrType=%#x\n", IntrType));
802 }
803 else
804 LogRel((DEVICE_NAME "::AddIRQ: failed to get supported interrupt types\n"));
805 return rc;
806}
807
808
809/**
810 * Removes IRQ for VMMDev.
811 *
812 * @param pDip Pointer to the device info structure.
813 */
814static void VBoxGuestSolarisRemoveIRQ(dev_info_t *pDip)
815{
816 LogFlow((DEVICE_NAME "::RemoveIRQ:\n"));
817
818 for (int i = 0; i < g_cIntrAllocated; i++)
819 {
820 int rc = ddi_intr_disable(g_pIntr[i]);
821 if (rc == DDI_SUCCESS)
822 {
823 rc = ddi_intr_remove_handler(g_pIntr[i]);
824 if (rc == DDI_SUCCESS)
825 ddi_intr_free(g_pIntr[i]);
826 }
827 }
828 RTMemFree(g_pIntr);
829 mutex_destroy(&g_IrqMtx);
830}
831
832
833/**
834 * Interrupt Service Routine for VMMDev.
835 *
836 * @param Arg Private data (unused, will be NULL).
837 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
838 */
839static uint_t VBoxGuestSolarisISR(caddr_t Arg)
840{
841 LogFlow((DEVICE_NAME "::ISR:\n"));
842
843 mutex_enter(&g_IrqMtx);
844 bool fOurIRQ = VBoxGuestCommonISR(&g_DevExt);
845 mutex_exit(&g_IrqMtx);
846
847 return fOurIRQ ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED;
848}
849
850
851void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
852{
853 LogFlow((DEVICE_NAME "::NativeISRMousePollEvent:\n"));
854
855 /*
856 * Wake up poll waiters.
857 */
858 pollwakeup(&g_PollHead, POLLIN | POLLRDNORM);
859}
860
861
862/* Common code that depend on g_DevExt. */
863#include "VBoxGuestIDC-unix.c.h"
864
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