VirtualBox

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

Last change on this file since 29246 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.9 KB
Line 
1/* $Id: VBoxGuest-solaris.c 28800 2010-04-27 08:22:32Z 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 LogFlow((DEVICE_NAME ":_init\n"));
182
183 PRTLOGGER pRelLogger;
184 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
185 int rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
186 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
187 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER, NULL);
188 if (RT_SUCCESS(rc))
189 RTLogRelSetDefaultInstance(pRelLogger);
190 else
191 cmn_err(CE_NOTE, "failed to initialize driver logging rc=%d!\n", rc);
192
193 /*
194 * Prevent module autounloading.
195 */
196 modctl_t *pModCtl = mod_getctl(&g_VBoxGuestSolarisModLinkage);
197 if (pModCtl)
198 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
199 else
200 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
201
202
203 rc = ddi_soft_state_init(&g_pVBoxGuestSolarisState, sizeof(vboxguest_state_t), 1);
204 if (!rc)
205 {
206 rc = mod_install(&g_VBoxGuestSolarisModLinkage);
207 if (rc)
208 ddi_soft_state_fini(&g_pVBoxGuestSolarisState);
209 }
210 return rc;
211}
212
213
214int _fini(void)
215{
216 LogFlow((DEVICE_NAME ":_fini\n"));
217 int rc = mod_remove(&g_VBoxGuestSolarisModLinkage);
218 if (!rc)
219 ddi_soft_state_fini(&g_pVBoxGuestSolarisState);
220
221 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
222 RTLogDestroy(RTLogSetDefaultInstance(NULL));
223
224 return rc;
225}
226
227
228int _info(struct modinfo *pModInfo)
229{
230 LogFlow((DEVICE_NAME ":_info\n"));
231 return mod_info(&g_VBoxGuestSolarisModLinkage, pModInfo);
232}
233
234
235/**
236 * Attach entry point, to attach a device to the system or resume it.
237 *
238 * @param pDip The module structure instance.
239 * @param enmCmd Attach type (ddi_attach_cmd_t)
240 *
241 * @return corresponding solaris error code.
242 */
243static int VBoxGuestSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
244{
245 LogFlow((DEVICE_NAME "::Attach\n"));
246 switch (enmCmd)
247 {
248 case DDI_ATTACH:
249 {
250 if (g_pDip)
251 {
252 LogRel((DEVICE_NAME "::Attach: Only one instance supported.\n"));
253 return DDI_FAILURE;
254 }
255
256 int rc;
257 int instance;
258
259 instance = ddi_get_instance(pDip);
260
261 /*
262 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
263 */
264 rc = RTR0Init(0);
265 if (RT_FAILURE(rc))
266 {
267 Log((DEVICE_NAME "::Attach: RTR0Init failed.\n"));
268 return DDI_FAILURE;
269 }
270
271 /*
272 * Enable resources for PCI access.
273 */
274 ddi_acc_handle_t PciHandle;
275 rc = pci_config_setup(pDip, &PciHandle);
276 if (rc == DDI_SUCCESS)
277 {
278 /*
279 * Map the register address space.
280 */
281 caddr_t baseAddr;
282 ddi_device_acc_attr_t deviceAttr;
283 deviceAttr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
284 deviceAttr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
285 deviceAttr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
286 deviceAttr.devacc_attr_access = DDI_DEFAULT_ACC;
287 rc = ddi_regs_map_setup(pDip, 1, &baseAddr, 0, 0, &deviceAttr, &g_PciIOHandle);
288 if (rc == DDI_SUCCESS)
289 {
290 /*
291 * Read size of the MMIO region.
292 */
293 g_uIOPortBase = (uintptr_t)baseAddr;
294 rc = ddi_dev_regsize(pDip, 2, &g_cbMMIO);
295 if (rc == DDI_SUCCESS)
296 {
297 rc = ddi_regs_map_setup(pDip, 2, &g_pMMIOBase, 0, g_cbMMIO, &deviceAttr,
298 &g_PciMMIOHandle);
299 if (rc == DDI_SUCCESS)
300 {
301 /*
302 * Add IRQ of VMMDev.
303 */
304 rc = VBoxGuestSolarisAddIRQ(pDip);
305 if (rc == DDI_SUCCESS)
306 {
307 /*
308 * Call the common device extension initializer.
309 */
310 rc = VBoxGuestInitDevExt(&g_DevExt, g_uIOPortBase, g_pMMIOBase,
311 g_cbMMIO, VBOXOSTYPE_Solaris,
312 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
313 if (RT_SUCCESS(rc))
314 {
315 rc = ddi_create_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0);
316 if (rc == DDI_SUCCESS)
317 {
318 g_pDip = pDip;
319 pci_config_teardown(&PciHandle);
320 return DDI_SUCCESS;
321 }
322
323 LogRel((DEVICE_NAME "::Attach: ddi_create_minor_node failed.\n"));
324 VBoxGuestDeleteDevExt(&g_DevExt);
325 }
326 else
327 LogRel((DEVICE_NAME "::Attach: VBoxGuestInitDevExt failed.\n"));
328 VBoxGuestSolarisRemoveIRQ(pDip);
329 }
330 else
331 LogRel((DEVICE_NAME "::Attach: VBoxGuestSolarisAddIRQ failed.\n"));
332 ddi_regs_map_free(&g_PciMMIOHandle);
333 }
334 else
335 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for MMIO region failed.\n"));
336 }
337 else
338 LogRel((DEVICE_NAME "::Attach: ddi_dev_regsize for MMIO region failed.\n"));
339 ddi_regs_map_free(&g_PciIOHandle);
340 }
341 else
342 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for IOport failed.\n"));
343 pci_config_teardown(&PciHandle);
344 }
345 else
346 LogRel((DEVICE_NAME "::Attach: pci_config_setup failed rc=%d.\n", rc));
347
348 RTR0Term();
349 return DDI_FAILURE;
350 }
351
352 case DDI_RESUME:
353 {
354 /** @todo implement resume for guest driver. */
355 return DDI_SUCCESS;
356 }
357
358 default:
359 return DDI_FAILURE;
360 }
361}
362
363
364/**
365 * Detach entry point, to detach a device to the system or suspend it.
366 *
367 * @param pDip The module structure instance.
368 * @param enmCmd Attach type (ddi_attach_cmd_t)
369 *
370 * @return corresponding solaris error code.
371 */
372static int VBoxGuestSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
373{
374 LogFlow((DEVICE_NAME "::Detach\n"));
375 switch (enmCmd)
376 {
377 case DDI_DETACH:
378 {
379 VBoxGuestSolarisRemoveIRQ(pDip);
380 ddi_regs_map_free(&g_PciIOHandle);
381 ddi_regs_map_free(&g_PciMMIOHandle);
382 ddi_remove_minor_node(pDip, NULL);
383 VBoxGuestDeleteDevExt(&g_DevExt);
384 RTR0Term();
385 g_pDip = NULL;
386 return DDI_SUCCESS;
387 }
388
389 case DDI_SUSPEND:
390 {
391 /** @todo implement suspend for guest driver. */
392 return DDI_SUCCESS;
393 }
394
395 default:
396 return DDI_FAILURE;
397 }
398}
399
400
401/**
402 * Info entry point, called by solaris kernel for obtaining driver info.
403 *
404 * @param pDip The module structure instance (do not use).
405 * @param enmCmd Information request type.
406 * @param pvArg Type specific argument.
407 * @param ppvResult Where to store the requested info.
408 *
409 * @return corresponding solaris error code.
410 */
411static int VBoxGuestSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
412{
413 LogFlow((DEVICE_NAME "::GetInfo\n"));
414
415 int rc = DDI_SUCCESS;
416 switch (enmCmd)
417 {
418 case DDI_INFO_DEVT2DEVINFO:
419 *ppvResult = (void *)g_pDip;
420 break;
421
422 case DDI_INFO_DEVT2INSTANCE:
423 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_pDip);
424 break;
425
426 default:
427 rc = DDI_FAILURE;
428 break;
429 }
430
431 NOREF(pvArg);
432 return rc;
433}
434
435
436/**
437 * User context entry points
438 */
439static int VBoxGuestSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
440{
441 int rc;
442 PVBOXGUESTSESSION pSession = NULL;
443
444 LogFlow((DEVICE_NAME "::Open\n"));
445
446 /*
447 * Verify we are being opened as a character device.
448 */
449 if (fType != OTYP_CHR)
450 return EINVAL;
451
452 vboxguest_state_t *pState = NULL;
453 unsigned iOpenInstance;
454 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
455 {
456 if ( !ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance) /* faster */
457 && ddi_soft_state_zalloc(g_pVBoxGuestSolarisState, iOpenInstance) == DDI_SUCCESS)
458 {
459 pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance);
460 break;
461 }
462 }
463 if (!pState)
464 {
465 Log((DEVICE_NAME "::Open: too many open instances."));
466 return ENXIO;
467 }
468
469 /*
470 * Create a new session.
471 */
472 rc = VBoxGuestCreateUserSession(&g_DevExt, &pSession);
473 if (RT_SUCCESS(rc))
474 {
475 pState->pvProcRef = proc_ref();
476 pState->pSession = pSession;
477 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
478 Log((DEVICE_NAME "::Open: pSession=%p pState=%p pid=%d\n", pSession, pState, (int)RTProcSelf()));
479 return 0;
480 }
481
482 /* Failed, clean up. */
483 ddi_soft_state_free(g_pVBoxGuestSolarisState, iOpenInstance);
484
485 LogRel((DEVICE_NAME "::Open: VBoxGuestCreateUserSession failed. rc=%d\n", rc));
486 return EFAULT;
487}
488
489
490static int VBoxGuestSolarisClose(dev_t Dev, int flag, int fType, cred_t *pCred)
491{
492 LogFlow((DEVICE_NAME "::Close pid=%d\n", (int)RTProcSelf()));
493
494 PVBOXGUESTSESSION pSession = NULL;
495 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
496 if (!pState)
497 {
498 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
499 return EFAULT;
500 }
501
502 proc_unref(pState->pvProcRef);
503 pSession = pState->pSession;
504 pState->pSession = NULL;
505 Log((DEVICE_NAME "::Close: pSession=%p pState=%p\n", pSession, pState));
506 ddi_soft_state_free(g_pVBoxGuestSolarisState, getminor(Dev));
507 if (!pSession)
508 {
509 Log((DEVICE_NAME "::Close: failed to get pSession.\n"));
510 return EFAULT;
511 }
512
513 /*
514 * Close the session.
515 */
516 VBoxGuestCloseSession(&g_DevExt, pSession);
517 return 0;
518}
519
520
521static int VBoxGuestSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
522{
523 LogFlow((DEVICE_NAME "::Read\n"));
524
525 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
526 if (!pState)
527 {
528 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
529 return EFAULT;
530 }
531
532 PVBOXGUESTSESSION pSession = pState->pSession;
533 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
534 if (pSession->u32MousePosChangedSeq != u32CurSeq)
535 pSession->u32MousePosChangedSeq = u32CurSeq;
536
537 return 0;
538}
539
540
541static int VBoxGuestSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
542{
543 LogFlow((DEVICE_NAME "::Write\n"));
544 return 0;
545}
546
547
548/** @def IOCPARM_LEN
549 * Gets the length from the ioctl number.
550 * This is normally defined by sys/ioccom.h on BSD systems...
551 */
552#ifndef IOCPARM_LEN
553# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
554#endif
555
556
557/**
558 * Driver ioctl, an alternate entry point for this character driver.
559 *
560 * @param Dev Device number
561 * @param Cmd Operation identifier
562 * @param pArg Arguments from user to driver
563 * @param Mode Information bitfield (read/write, address space etc.)
564 * @param pCred User credentials
565 * @param pVal Return value for calling process.
566 *
567 * @return corresponding solaris error code.
568 */
569static int VBoxGuestSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
570{
571 LogFlow((DEVICE_NAME ":VBoxGuestSolarisIOCtl\n"));
572
573 /*
574 * Get the session from the soft state item.
575 */
576 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
577 if (!pState)
578 {
579 LogRel((DEVICE_NAME "::IOCtl: no state data for %d\n", getminor(Dev)));
580 return EINVAL;
581 }
582
583 PVBOXGUESTSESSION pSession = pState->pSession;
584 if (!pSession)
585 {
586 LogRel((DEVICE_NAME "::IOCtl: no session data for %d\n", getminor(Dev)));
587 return EINVAL;
588 }
589
590 /*
591 * Read and validate the request wrapper.
592 */
593 VBGLBIGREQ ReqWrap;
594 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
595 {
596 LogRel((DEVICE_NAME "::IOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd), sizeof(ReqWrap)));
597 return ENOTTY;
598 }
599
600 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
601 if (RT_UNLIKELY(rc))
602 {
603 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%#x.\n", pArg, Cmd, rc));
604 return EINVAL;
605 }
606
607 if (ReqWrap.u32Magic != VBGLBIGREQ_MAGIC)
608 {
609 LogRel((DEVICE_NAME "::IOCtl: bad magic %#x; pArg=%p Cmd=%#x.\n", ReqWrap.u32Magic, pArg, Cmd));
610 return EINVAL;
611 }
612 if (RT_UNLIKELY(ReqWrap.cbData > _1M*16))
613 {
614 LogRel((DEVICE_NAME "::IOCtl: bad size %#x; pArg=%p Cmd=%#x.\n", ReqWrap.cbData, pArg, Cmd));
615 return EINVAL;
616 }
617
618 /*
619 * Read the request payload if any; requests like VBOXGUEST_IOCTL_CANCEL_ALL_WAITEVENTS have no data payload.
620 */
621 void *pvBuf = NULL;
622 if (RT_LIKELY(ReqWrap.cbData > 0))
623 {
624 pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
625 if (RT_UNLIKELY(!pvBuf))
626 {
627 LogRel((DEVICE_NAME "::IOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
628 return ENOMEM;
629 }
630
631 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
632 if (RT_UNLIKELY(rc))
633 {
634 RTMemTmpFree(pvBuf);
635 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
636 return EFAULT;
637 }
638 if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
639 {
640 RTMemTmpFree(pvBuf);
641 LogRel((DEVICE_NAME "::IOCtl: pvBuf invalid pointer %p\n", pvBuf));
642 return EINVAL;
643 }
644 }
645 Log((DEVICE_NAME "::IOCtl: pSession=%p pid=%d.\n", pSession, (int)RTProcSelf()));
646
647 /*
648 * Process the IOCtl.
649 */
650 size_t cbDataReturned = 0;
651 rc = VBoxGuestCommonIOCtl(Cmd, &g_DevExt, pSession, pvBuf, ReqWrap.cbData, &cbDataReturned);
652 if (RT_SUCCESS(rc))
653 {
654 rc = 0;
655 if (RT_UNLIKELY(cbDataReturned > ReqWrap.cbData))
656 {
657 LogRel((DEVICE_NAME "::IOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap.cbData));
658 cbDataReturned = ReqWrap.cbData;
659 }
660 if (cbDataReturned > 0)
661 {
662 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataReturned, Mode);
663 if (RT_UNLIKELY(rc))
664 {
665 LogRel((DEVICE_NAME "::IOCtl: ddi_copyout failed; pvBuf=%p pArg=%p cbDataReturned=%u Cmd=%d. rc=%d\n",
666 pvBuf, pArg, cbDataReturned, Cmd, rc));
667 rc = EFAULT;
668 }
669 }
670 }
671 else
672 {
673 /*
674 * We Log() instead of LogRel() here because VBOXGUEST_IOCTL_WAITEVENT can return VERR_TIMEOUT,
675 * VBOXGUEST_IOCTL_CANCEL_ALL_EVENTS can return VERR_INTERRUPTED and possibly more in the future;
676 * which are not really failures that require logging.
677 */
678 Log((DEVICE_NAME "::IOCtl: VBoxGuestCommonIOCtl failed. Cmd=%#x rc=%d\n", Cmd, rc));
679 rc = RTErrConvertToErrno(rc);
680 }
681 *pVal = rc;
682 if (pvBuf)
683 RTMemTmpFree(pvBuf);
684 return rc;
685}
686
687
688static int VBoxGuestSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
689{
690 LogFlow((DEVICE_NAME "::Poll: fEvents=%d fAnyYet=%d\n", fEvents, fAnyYet));
691
692 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
693 if (RT_LIKELY(pState))
694 {
695 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pState->pSession;
696 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
697 if (pSession->u32MousePosChangedSeq != u32CurSeq)
698 {
699 *pReqEvents |= (POLLIN | POLLRDNORM);
700 pSession->u32MousePosChangedSeq = u32CurSeq;
701 }
702 else
703 {
704 *pReqEvents = 0;
705 if (!fAnyYet)
706 *ppPollHead = &g_PollHead;
707 }
708
709 return 0;
710 }
711 else
712 {
713 Log((DEVICE_NAME "::Poll: no state data for %d\n", getminor(Dev)));
714 return EINVAL;
715 }
716}
717
718
719/**
720 * Sets IRQ for VMMDev.
721 *
722 * @returns Solaris error code.
723 * @param pDip Pointer to the device info structure.
724 */
725static int VBoxGuestSolarisAddIRQ(dev_info_t *pDip)
726{
727 LogFlow((DEVICE_NAME "::AddIRQ: pDip=%p\n", pDip));
728
729 int IntrType = 0;
730 int rc = ddi_intr_get_supported_types(pDip, &IntrType);
731 if (rc == DDI_SUCCESS)
732 {
733 /* We won't need to bother about MSIs. */
734 if (IntrType & DDI_INTR_TYPE_FIXED)
735 {
736 int IntrCount = 0;
737 rc = ddi_intr_get_nintrs(pDip, IntrType, &IntrCount);
738 if ( rc == DDI_SUCCESS
739 && IntrCount > 0)
740 {
741 int IntrAvail = 0;
742 rc = ddi_intr_get_navail(pDip, IntrType, &IntrAvail);
743 if ( rc == DDI_SUCCESS
744 && IntrAvail > 0)
745 {
746 /* Allocated kernel memory for the interrupt handles. The allocation size is stored internally. */
747 g_pIntr = RTMemAlloc(IntrCount * sizeof(ddi_intr_handle_t));
748 if (g_pIntr)
749 {
750 int IntrAllocated;
751 rc = ddi_intr_alloc(pDip, g_pIntr, IntrType, 0, IntrCount, &IntrAllocated, DDI_INTR_ALLOC_NORMAL);
752 if ( rc == DDI_SUCCESS
753 && IntrAllocated > 0)
754 {
755 g_cIntrAllocated = IntrAllocated;
756 uint_t uIntrPriority;
757 rc = ddi_intr_get_pri(g_pIntr[0], &uIntrPriority);
758 if (rc == DDI_SUCCESS)
759 {
760 /* Initialize the mutex. */
761 mutex_init(&g_IrqMtx, NULL, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
762
763 /* Assign interrupt handler functions and enable interrupts. */
764 for (int i = 0; i < IntrAllocated; i++)
765 {
766 rc = ddi_intr_add_handler(g_pIntr[i], (ddi_intr_handler_t *)VBoxGuestSolarisISR,
767 NULL /* No Private Data */, NULL);
768 if (rc == DDI_SUCCESS)
769 rc = ddi_intr_enable(g_pIntr[i]);
770 if (rc != DDI_SUCCESS)
771 {
772 /* Changing local IntrAllocated to hold so-far allocated handles for freeing. */
773 IntrAllocated = i;
774 break;
775 }
776 }
777 if (rc == DDI_SUCCESS)
778 return rc;
779
780 /* Remove any assigned handlers */
781 LogRel((DEVICE_NAME ":failed to assign IRQs allocated=%d\n", IntrAllocated));
782 for (int x = 0; x < IntrAllocated; x++)
783 ddi_intr_remove_handler(g_pIntr[x]);
784 }
785 else
786 LogRel((DEVICE_NAME "::AddIRQ: failed to get priority of interrupt. rc=%d\n", rc));
787
788 /* Remove allocated IRQs, too bad we can free only one handle at a time. */
789 for (int k = 0; k < g_cIntrAllocated; k++)
790 ddi_intr_free(g_pIntr[k]);
791 }
792 else
793 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
794 RTMemFree(g_pIntr);
795 }
796 else
797 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
798 }
799 else
800 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient available IRQs. rc=%d IntrAvail=%d\n", rc, IntrAvail));
801 }
802 else
803 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient number of IRQs. rc=%d IntrCount=%d\n", rc, IntrCount));
804 }
805 else
806 LogRel((DEVICE_NAME "::AddIRQ: invalid irq type. IntrType=%#x\n", IntrType));
807 }
808 else
809 LogRel((DEVICE_NAME "::AddIRQ: failed to get supported interrupt types\n"));
810 return rc;
811}
812
813
814/**
815 * Removes IRQ for VMMDev.
816 *
817 * @param pDip Pointer to the device info structure.
818 */
819static void VBoxGuestSolarisRemoveIRQ(dev_info_t *pDip)
820{
821 LogFlow((DEVICE_NAME "::RemoveIRQ:\n"));
822
823 for (int i = 0; i < g_cIntrAllocated; i++)
824 {
825 int rc = ddi_intr_disable(g_pIntr[i]);
826 if (rc == DDI_SUCCESS)
827 {
828 rc = ddi_intr_remove_handler(g_pIntr[i]);
829 if (rc == DDI_SUCCESS)
830 ddi_intr_free(g_pIntr[i]);
831 }
832 }
833 RTMemFree(g_pIntr);
834 mutex_destroy(&g_IrqMtx);
835}
836
837
838/**
839 * Interrupt Service Routine for VMMDev.
840 *
841 * @param Arg Private data (unused, will be NULL).
842 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
843 */
844static uint_t VBoxGuestSolarisISR(caddr_t Arg)
845{
846 LogFlow((DEVICE_NAME "::ISR:\n"));
847
848 mutex_enter(&g_IrqMtx);
849 bool fOurIRQ = VBoxGuestCommonISR(&g_DevExt);
850 mutex_exit(&g_IrqMtx);
851
852 return fOurIRQ ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED;
853}
854
855
856void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
857{
858 LogFlow((DEVICE_NAME "::NativeISRMousePollEvent:\n"));
859
860 /*
861 * Wake up poll waiters.
862 */
863 pollwakeup(&g_PollHead, POLLIN | POLLRDNORM);
864}
865
866
867/* Common code that depend on g_DevExt. */
868#include "VBoxGuestIDC-unix.c.h"
869
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