VirtualBox

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

Last change on this file since 35517 was 34521, checked in by vboxsync, 14 years ago

VBoxGuest: Report 64-bit OS types (only linux got this right earlier).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.0 KB
Line 
1/* $Id: VBoxGuest-solaris.c 34521 2010-11-30 14:35:40Z 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, g_cbMMIO,
309#if ARCH_BITS == 64
310 VBOXOSTYPE_Solaris_x64,
311#else
312 VBOXOSTYPE_Solaris,
313#endif
314 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
315 if (RT_SUCCESS(rc))
316 {
317 rc = ddi_create_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0);
318 if (rc == DDI_SUCCESS)
319 {
320 g_pDip = pDip;
321 pci_config_teardown(&PciHandle);
322 return DDI_SUCCESS;
323 }
324
325 LogRel((DEVICE_NAME "::Attach: ddi_create_minor_node failed.\n"));
326 VBoxGuestDeleteDevExt(&g_DevExt);
327 }
328 else
329 LogRel((DEVICE_NAME "::Attach: VBoxGuestInitDevExt failed.\n"));
330 VBoxGuestSolarisRemoveIRQ(pDip);
331 }
332 else
333 LogRel((DEVICE_NAME "::Attach: VBoxGuestSolarisAddIRQ failed.\n"));
334 ddi_regs_map_free(&g_PciMMIOHandle);
335 }
336 else
337 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for MMIO region failed.\n"));
338 }
339 else
340 LogRel((DEVICE_NAME "::Attach: ddi_dev_regsize for MMIO region failed.\n"));
341 ddi_regs_map_free(&g_PciIOHandle);
342 }
343 else
344 LogRel((DEVICE_NAME "::Attach: ddi_regs_map_setup for IOport failed.\n"));
345 pci_config_teardown(&PciHandle);
346 }
347 else
348 LogRel((DEVICE_NAME "::Attach: pci_config_setup failed rc=%d.\n", rc));
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 g_pDip = NULL;
385 return DDI_SUCCESS;
386 }
387
388 case DDI_SUSPEND:
389 {
390 /** @todo implement suspend for guest driver. */
391 return DDI_SUCCESS;
392 }
393
394 default:
395 return DDI_FAILURE;
396 }
397}
398
399
400/**
401 * Info entry point, called by solaris kernel for obtaining driver info.
402 *
403 * @param pDip The module structure instance (do not use).
404 * @param enmCmd Information request type.
405 * @param pvArg Type specific argument.
406 * @param ppvResult Where to store the requested info.
407 *
408 * @return corresponding solaris error code.
409 */
410static int VBoxGuestSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
411{
412 LogFlow((DEVICE_NAME "::GetInfo\n"));
413
414 int rc = DDI_SUCCESS;
415 switch (enmCmd)
416 {
417 case DDI_INFO_DEVT2DEVINFO:
418 *ppvResult = (void *)g_pDip;
419 break;
420
421 case DDI_INFO_DEVT2INSTANCE:
422 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_pDip);
423 break;
424
425 default:
426 rc = DDI_FAILURE;
427 break;
428 }
429
430 NOREF(pvArg);
431 return rc;
432}
433
434
435/**
436 * User context entry points
437 */
438static int VBoxGuestSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
439{
440 int rc;
441 PVBOXGUESTSESSION pSession = NULL;
442
443 LogFlow((DEVICE_NAME "::Open\n"));
444
445 /*
446 * Verify we are being opened as a character device.
447 */
448 if (fType != OTYP_CHR)
449 return EINVAL;
450
451 vboxguest_state_t *pState = NULL;
452 unsigned iOpenInstance;
453 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
454 {
455 if ( !ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance) /* faster */
456 && ddi_soft_state_zalloc(g_pVBoxGuestSolarisState, iOpenInstance) == DDI_SUCCESS)
457 {
458 pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, iOpenInstance);
459 break;
460 }
461 }
462 if (!pState)
463 {
464 Log((DEVICE_NAME "::Open: too many open instances."));
465 return ENXIO;
466 }
467
468 /*
469 * Create a new session.
470 */
471 rc = VBoxGuestCreateUserSession(&g_DevExt, &pSession);
472 if (RT_SUCCESS(rc))
473 {
474 pState->pvProcRef = proc_ref();
475 pState->pSession = pSession;
476 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
477 Log((DEVICE_NAME "::Open: pSession=%p pState=%p pid=%d\n", pSession, pState, (int)RTProcSelf()));
478 return 0;
479 }
480
481 /* Failed, clean up. */
482 ddi_soft_state_free(g_pVBoxGuestSolarisState, iOpenInstance);
483
484 LogRel((DEVICE_NAME "::Open: VBoxGuestCreateUserSession failed. rc=%d\n", rc));
485 return EFAULT;
486}
487
488
489static int VBoxGuestSolarisClose(dev_t Dev, int flag, int fType, cred_t *pCred)
490{
491 LogFlow((DEVICE_NAME "::Close pid=%d\n", (int)RTProcSelf()));
492
493 PVBOXGUESTSESSION pSession = NULL;
494 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
495 if (!pState)
496 {
497 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
498 return EFAULT;
499 }
500
501 proc_unref(pState->pvProcRef);
502 pSession = pState->pSession;
503 pState->pSession = NULL;
504 Log((DEVICE_NAME "::Close: pSession=%p pState=%p\n", pSession, pState));
505 ddi_soft_state_free(g_pVBoxGuestSolarisState, getminor(Dev));
506 if (!pSession)
507 {
508 Log((DEVICE_NAME "::Close: failed to get pSession.\n"));
509 return EFAULT;
510 }
511
512 /*
513 * Close the session.
514 */
515 VBoxGuestCloseSession(&g_DevExt, pSession);
516 return 0;
517}
518
519
520static int VBoxGuestSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
521{
522 LogFlow((DEVICE_NAME "::Read\n"));
523
524 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
525 if (!pState)
526 {
527 Log((DEVICE_NAME "::Close: failed to get pState.\n"));
528 return EFAULT;
529 }
530
531 PVBOXGUESTSESSION pSession = pState->pSession;
532 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
533 if (pSession->u32MousePosChangedSeq != u32CurSeq)
534 pSession->u32MousePosChangedSeq = u32CurSeq;
535
536 return 0;
537}
538
539
540static int VBoxGuestSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
541{
542 LogFlow((DEVICE_NAME "::Write\n"));
543 return 0;
544}
545
546
547/** @def IOCPARM_LEN
548 * Gets the length from the ioctl number.
549 * This is normally defined by sys/ioccom.h on BSD systems...
550 */
551#ifndef IOCPARM_LEN
552# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
553#endif
554
555
556/**
557 * Driver ioctl, an alternate entry point for this character driver.
558 *
559 * @param Dev Device number
560 * @param Cmd Operation identifier
561 * @param pArg Arguments from user to driver
562 * @param Mode Information bitfield (read/write, address space etc.)
563 * @param pCred User credentials
564 * @param pVal Return value for calling process.
565 *
566 * @return corresponding solaris error code.
567 */
568static int VBoxGuestSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
569{
570 LogFlow((DEVICE_NAME ":VBoxGuestSolarisIOCtl\n"));
571
572 /*
573 * Get the session from the soft state item.
574 */
575 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
576 if (!pState)
577 {
578 LogRel((DEVICE_NAME "::IOCtl: no state data for %d\n", getminor(Dev)));
579 return EINVAL;
580 }
581
582 PVBOXGUESTSESSION pSession = pState->pSession;
583 if (!pSession)
584 {
585 LogRel((DEVICE_NAME "::IOCtl: no session data for %d\n", getminor(Dev)));
586 return EINVAL;
587 }
588
589 /*
590 * Read and validate the request wrapper.
591 */
592 VBGLBIGREQ ReqWrap;
593 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
594 {
595 LogRel((DEVICE_NAME "::IOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd), sizeof(ReqWrap)));
596 return ENOTTY;
597 }
598
599 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
600 if (RT_UNLIKELY(rc))
601 {
602 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%#x.\n", pArg, Cmd, rc));
603 return EINVAL;
604 }
605
606 if (ReqWrap.u32Magic != VBGLBIGREQ_MAGIC)
607 {
608 LogRel((DEVICE_NAME "::IOCtl: bad magic %#x; pArg=%p Cmd=%#x.\n", ReqWrap.u32Magic, pArg, Cmd));
609 return EINVAL;
610 }
611 if (RT_UNLIKELY(ReqWrap.cbData > _1M*16))
612 {
613 LogRel((DEVICE_NAME "::IOCtl: bad size %#x; pArg=%p Cmd=%#x.\n", ReqWrap.cbData, pArg, Cmd));
614 return EINVAL;
615 }
616
617 /*
618 * Read the request payload if any; requests like VBOXGUEST_IOCTL_CANCEL_ALL_WAITEVENTS have no data payload.
619 */
620 void *pvBuf = NULL;
621 if (RT_LIKELY(ReqWrap.cbData > 0))
622 {
623 pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
624 if (RT_UNLIKELY(!pvBuf))
625 {
626 LogRel((DEVICE_NAME "::IOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
627 return ENOMEM;
628 }
629
630 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
631 if (RT_UNLIKELY(rc))
632 {
633 RTMemTmpFree(pvBuf);
634 LogRel((DEVICE_NAME "::IOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
635 return EFAULT;
636 }
637 if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
638 {
639 RTMemTmpFree(pvBuf);
640 LogRel((DEVICE_NAME "::IOCtl: pvBuf invalid pointer %p\n", pvBuf));
641 return EINVAL;
642 }
643 }
644 Log((DEVICE_NAME "::IOCtl: pSession=%p pid=%d.\n", pSession, (int)RTProcSelf()));
645
646 /*
647 * Process the IOCtl.
648 */
649 size_t cbDataReturned = 0;
650 rc = VBoxGuestCommonIOCtl(Cmd, &g_DevExt, pSession, pvBuf, ReqWrap.cbData, &cbDataReturned);
651 if (RT_SUCCESS(rc))
652 {
653 rc = 0;
654 if (RT_UNLIKELY(cbDataReturned > ReqWrap.cbData))
655 {
656 LogRel((DEVICE_NAME "::IOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap.cbData));
657 cbDataReturned = ReqWrap.cbData;
658 }
659 if (cbDataReturned > 0)
660 {
661 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataReturned, Mode);
662 if (RT_UNLIKELY(rc))
663 {
664 LogRel((DEVICE_NAME "::IOCtl: ddi_copyout failed; pvBuf=%p pArg=%p cbDataReturned=%u Cmd=%d. rc=%d\n",
665 pvBuf, pArg, cbDataReturned, Cmd, rc));
666 rc = EFAULT;
667 }
668 }
669 }
670 else
671 {
672 /*
673 * We Log() instead of LogRel() here because VBOXGUEST_IOCTL_WAITEVENT can return VERR_TIMEOUT,
674 * VBOXGUEST_IOCTL_CANCEL_ALL_EVENTS can return VERR_INTERRUPTED and possibly more in the future;
675 * which are not really failures that require logging.
676 */
677 Log((DEVICE_NAME "::IOCtl: VBoxGuestCommonIOCtl failed. Cmd=%#x rc=%d\n", Cmd, rc));
678 rc = RTErrConvertToErrno(rc);
679 }
680 *pVal = rc;
681 if (pvBuf)
682 RTMemTmpFree(pvBuf);
683 return rc;
684}
685
686
687static int VBoxGuestSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
688{
689 LogFlow((DEVICE_NAME "::Poll: fEvents=%d fAnyYet=%d\n", fEvents, fAnyYet));
690
691 vboxguest_state_t *pState = ddi_get_soft_state(g_pVBoxGuestSolarisState, getminor(Dev));
692 if (RT_LIKELY(pState))
693 {
694 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pState->pSession;
695 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
696 if (pSession->u32MousePosChangedSeq != u32CurSeq)
697 {
698 *pReqEvents |= (POLLIN | POLLRDNORM);
699 pSession->u32MousePosChangedSeq = u32CurSeq;
700 }
701 else
702 {
703 *pReqEvents = 0;
704 if (!fAnyYet)
705 *ppPollHead = &g_PollHead;
706 }
707
708 return 0;
709 }
710 else
711 {
712 Log((DEVICE_NAME "::Poll: no state data for %d\n", getminor(Dev)));
713 return EINVAL;
714 }
715}
716
717
718/**
719 * Sets IRQ for VMMDev.
720 *
721 * @returns Solaris error code.
722 * @param pDip Pointer to the device info structure.
723 */
724static int VBoxGuestSolarisAddIRQ(dev_info_t *pDip)
725{
726 LogFlow((DEVICE_NAME "::AddIRQ: pDip=%p\n", pDip));
727
728 int IntrType = 0;
729 int rc = ddi_intr_get_supported_types(pDip, &IntrType);
730 if (rc == DDI_SUCCESS)
731 {
732 /* We won't need to bother about MSIs. */
733 if (IntrType & DDI_INTR_TYPE_FIXED)
734 {
735 int IntrCount = 0;
736 rc = ddi_intr_get_nintrs(pDip, IntrType, &IntrCount);
737 if ( rc == DDI_SUCCESS
738 && IntrCount > 0)
739 {
740 int IntrAvail = 0;
741 rc = ddi_intr_get_navail(pDip, IntrType, &IntrAvail);
742 if ( rc == DDI_SUCCESS
743 && IntrAvail > 0)
744 {
745 /* Allocated kernel memory for the interrupt handles. The allocation size is stored internally. */
746 g_pIntr = RTMemAlloc(IntrCount * sizeof(ddi_intr_handle_t));
747 if (g_pIntr)
748 {
749 int IntrAllocated;
750 rc = ddi_intr_alloc(pDip, g_pIntr, IntrType, 0, IntrCount, &IntrAllocated, DDI_INTR_ALLOC_NORMAL);
751 if ( rc == DDI_SUCCESS
752 && IntrAllocated > 0)
753 {
754 g_cIntrAllocated = IntrAllocated;
755 uint_t uIntrPriority;
756 rc = ddi_intr_get_pri(g_pIntr[0], &uIntrPriority);
757 if (rc == DDI_SUCCESS)
758 {
759 /* Initialize the mutex. */
760 mutex_init(&g_IrqMtx, NULL, MUTEX_DRIVER, DDI_INTR_PRI(uIntrPriority));
761
762 /* Assign interrupt handler functions and enable interrupts. */
763 for (int i = 0; i < IntrAllocated; i++)
764 {
765 rc = ddi_intr_add_handler(g_pIntr[i], (ddi_intr_handler_t *)VBoxGuestSolarisISR,
766 NULL /* No Private Data */, NULL);
767 if (rc == DDI_SUCCESS)
768 rc = ddi_intr_enable(g_pIntr[i]);
769 if (rc != DDI_SUCCESS)
770 {
771 /* Changing local IntrAllocated to hold so-far allocated handles for freeing. */
772 IntrAllocated = i;
773 break;
774 }
775 }
776 if (rc == DDI_SUCCESS)
777 return rc;
778
779 /* Remove any assigned handlers */
780 LogRel((DEVICE_NAME ":failed to assign IRQs allocated=%d\n", IntrAllocated));
781 for (int x = 0; x < IntrAllocated; x++)
782 ddi_intr_remove_handler(g_pIntr[x]);
783 }
784 else
785 LogRel((DEVICE_NAME "::AddIRQ: failed to get priority of interrupt. rc=%d\n", rc));
786
787 /* Remove allocated IRQs, too bad we can free only one handle at a time. */
788 for (int k = 0; k < g_cIntrAllocated; k++)
789 ddi_intr_free(g_pIntr[k]);
790 }
791 else
792 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
793 RTMemFree(g_pIntr);
794 }
795 else
796 LogRel((DEVICE_NAME "::AddIRQ: failed to allocated IRQs. count=%d\n", IntrCount));
797 }
798 else
799 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient available IRQs. rc=%d IntrAvail=%d\n", rc, IntrAvail));
800 }
801 else
802 LogRel((DEVICE_NAME "::AddIRQ: failed to get or insufficient number of IRQs. rc=%d IntrCount=%d\n", rc, IntrCount));
803 }
804 else
805 LogRel((DEVICE_NAME "::AddIRQ: invalid irq type. IntrType=%#x\n", IntrType));
806 }
807 else
808 LogRel((DEVICE_NAME "::AddIRQ: failed to get supported interrupt types\n"));
809 return rc;
810}
811
812
813/**
814 * Removes IRQ for VMMDev.
815 *
816 * @param pDip Pointer to the device info structure.
817 */
818static void VBoxGuestSolarisRemoveIRQ(dev_info_t *pDip)
819{
820 LogFlow((DEVICE_NAME "::RemoveIRQ:\n"));
821
822 for (int i = 0; i < g_cIntrAllocated; i++)
823 {
824 int rc = ddi_intr_disable(g_pIntr[i]);
825 if (rc == DDI_SUCCESS)
826 {
827 rc = ddi_intr_remove_handler(g_pIntr[i]);
828 if (rc == DDI_SUCCESS)
829 ddi_intr_free(g_pIntr[i]);
830 }
831 }
832 RTMemFree(g_pIntr);
833 mutex_destroy(&g_IrqMtx);
834}
835
836
837/**
838 * Interrupt Service Routine for VMMDev.
839 *
840 * @param Arg Private data (unused, will be NULL).
841 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
842 */
843static uint_t VBoxGuestSolarisISR(caddr_t Arg)
844{
845 LogFlow((DEVICE_NAME "::ISR:\n"));
846
847 mutex_enter(&g_IrqMtx);
848 bool fOurIRQ = VBoxGuestCommonISR(&g_DevExt);
849 mutex_exit(&g_IrqMtx);
850
851 return fOurIRQ ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED;
852}
853
854
855void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
856{
857 LogFlow((DEVICE_NAME "::NativeISRMousePollEvent:\n"));
858
859 /*
860 * Wake up poll waiters.
861 */
862 pollwakeup(&g_PollHead, POLLIN | POLLRDNORM);
863}
864
865
866/* Common code that depend on g_DevExt. */
867#include "VBoxGuestIDC-unix.c.h"
868
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