VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/solaris/SUPDrv-solaris.c@ 21604

Last change on this file since 21604 was 21604, checked in by vboxsync, 16 years ago

Solaris/vboxdrv: log change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.0 KB
Line 
1/* $Id: SUPDrv-solaris.c 21604 2009-07-15 11:54:00Z vboxsync $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - Solaris specifics.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SUP_DRV
35#include <sys/types.h>
36#include <sys/param.h>
37#include <sys/errno.h>
38#include <sys/uio.h>
39#include <sys/buf.h>
40#include <sys/modctl.h>
41#include <sys/open.h>
42#include <sys/conf.h>
43#include <sys/cmn_err.h>
44#include <sys/stat.h>
45#include <sys/ddi.h>
46#include <sys/sunddi.h>
47#include <sys/file.h>
48#include <sys/priv_names.h>
49#undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
50
51#include "../SUPDrvInternal.h"
52#include <VBox/log.h>
53#include <VBox/version.h>
54#include <iprt/semaphore.h>
55#include <iprt/spinlock.h>
56#include <iprt/mp.h>
57#include <iprt/power.h>
58#include <iprt/process.h>
59#include <iprt/thread.h>
60#include <iprt/initterm.h>
61#include <iprt/alloc.h>
62#include <iprt/string.h>
63#include <iprt/err.h>
64
65
66/*******************************************************************************
67* Defined Constants And Macros *
68*******************************************************************************/
69/** The module name. */
70#define DEVICE_NAME "vboxdrv"
71/** The module description as seen in 'modinfo'. */
72#define DEVICE_DESC "VirtualBox HostDrv"
73/** Maximum number of driver instances. */
74#define DEVICE_MAXINSTANCES 16
75
76
77/*******************************************************************************
78* Internal Functions *
79*******************************************************************************/
80static int VBoxDrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
81static int VBoxDrvSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
82static int VBoxDrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
83static int VBoxDrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
84static int VBoxDrvSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArgs, int mode, cred_t *pCred, int *pVal);
85
86static int VBoxDrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t Cmd);
87static int VBoxDrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t Cmd);
88
89static int VBoxSupDrvErr2SolarisErr(int rc);
90static int VBoxDrvSolarisIOCtlSlow(PSUPDRVSESSION pSession, int Cmd, int Mode, intptr_t pArgs);
91
92
93/*******************************************************************************
94* Global Variables *
95*******************************************************************************/
96/**
97 * cb_ops: for drivers that support char/block entry points
98 */
99static struct cb_ops g_VBoxDrvSolarisCbOps =
100{
101 VBoxDrvSolarisOpen,
102 VBoxDrvSolarisClose,
103 nodev, /* b strategy */
104 nodev, /* b dump */
105 nodev, /* b print */
106 VBoxDrvSolarisRead,
107 VBoxDrvSolarisWrite,
108 VBoxDrvSolarisIOCtl,
109 nodev, /* c devmap */
110 nodev, /* c mmap */
111 nodev, /* c segmap */
112 nochpoll, /* c poll */
113 ddi_prop_op, /* property ops */
114 NULL, /* streamtab */
115 D_NEW | D_MP, /* compat. flag */
116 CB_REV /* revision */
117};
118
119/**
120 * dev_ops: for driver device operations
121 */
122static struct dev_ops g_VBoxDrvSolarisDevOps =
123{
124 DEVO_REV, /* driver build revision */
125 0, /* ref count */
126 nulldev, /* get info */
127 nulldev, /* identify */
128 nulldev, /* probe */
129 VBoxDrvSolarisAttach,
130 VBoxDrvSolarisDetach,
131 nodev, /* reset */
132 &g_VBoxDrvSolarisCbOps,
133 (struct bus_ops *)0,
134 nodev /* power */
135};
136
137/**
138 * modldrv: export driver specifics to the kernel
139 */
140static struct modldrv g_VBoxDrvSolarisModule =
141{
142 &mod_driverops, /* extern from kernel */
143 DEVICE_DESC " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
144 &g_VBoxDrvSolarisDevOps
145};
146
147/**
148 * modlinkage: export install/remove/info to the kernel
149 */
150static struct modlinkage g_VBoxDrvSolarisModLinkage =
151{
152 MODREV_1, /* loadable module system revision */
153 &g_VBoxDrvSolarisModule,
154 NULL /* terminate array of linkage structures */
155};
156
157#ifndef USE_SESSION_HASH
158/**
159 * State info for each open file handle.
160 */
161typedef struct
162{
163 /**< Pointer to the session data. */
164 PSUPDRVSESSION pSession;
165} vbox_devstate_t;
166#else
167/** State info. for each driver instance. */
168typedef struct
169{
170 dev_info_t *pDip; /* Device handle */
171} vbox_devstate_t;
172#endif
173
174/** Opaque pointer to list of state */
175static void *g_pVBoxDrvSolarisState;
176
177/** Device extention & session data association structure */
178static SUPDRVDEVEXT g_DevExt;
179
180/** Hash table */
181static PSUPDRVSESSION g_apSessionHashTab[19];
182/** Spinlock protecting g_apSessionHashTab. */
183static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
184/** Calculates bucket index into g_apSessionHashTab.*/
185#define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
186
187/**
188 * Kernel entry points
189 */
190int _init(void)
191{
192 LogFlow((DEVICE_NAME ":_init\n"));
193
194 /*
195 * Prevent module autounloading.
196 */
197 modctl_t *pModCtl = mod_getctl(&g_VBoxDrvSolarisModLinkage);
198 if (pModCtl)
199 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
200 else
201 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
202
203 /*
204 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
205 */
206 int rc = RTR0Init(0);
207 if (RT_SUCCESS(rc))
208 {
209 /*
210 * Initialize the device extension
211 */
212 rc = supdrvInitDevExt(&g_DevExt);
213 if (RT_SUCCESS(rc))
214 {
215 /*
216 * Initialize the session hash table.
217 */
218 memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab));
219 rc = RTSpinlockCreate(&g_Spinlock);
220 if (RT_SUCCESS(rc))
221 {
222 int rc = ddi_soft_state_init(&g_pVBoxDrvSolarisState, sizeof(vbox_devstate_t), 8);
223 if (!rc)
224 {
225 rc = mod_install(&g_VBoxDrvSolarisModLinkage);
226 if (!rc)
227 return rc; /* success */
228
229 ddi_soft_state_fini(&g_pVBoxDrvSolarisState);
230 LogRel((DEVICE_NAME ":mod_install failed! rc=%d\n", rc));
231 }
232 else
233 LogRel((DEVICE_NAME ":failed to initialize soft state.\n"));
234
235 RTSpinlockDestroy(g_Spinlock);
236 g_Spinlock = NIL_RTSPINLOCK;
237 }
238 else
239 LogRel((DEVICE_NAME ":VBoxDrvSolarisAttach: RTSpinlockCreate failed\n"));
240 supdrvDeleteDevExt(&g_DevExt);
241 }
242 else
243 LogRel((DEVICE_NAME ":VBoxDrvSolarisAttach: supdrvInitDevExt failed\n"));
244 RTR0Term();
245 }
246 else
247 LogRel((DEVICE_NAME ":VBoxDrvSolarisAttach: failed to init R0Drv\n"));
248 memset(&g_DevExt, 0, sizeof(g_DevExt));
249
250 return RTErrConvertToErrno(rc);
251}
252
253
254int _fini(void)
255{
256 LogFlow((DEVICE_NAME ":_fini\n"));
257
258 /*
259 * Undo the work we did at start (in the reverse order).
260 */
261 int rc = mod_remove(&g_VBoxDrvSolarisModLinkage);
262 if (rc != 0)
263 return rc;
264
265 supdrvDeleteDevExt(&g_DevExt);
266
267 rc = RTSpinlockDestroy(g_Spinlock);
268 AssertRC(rc);
269 g_Spinlock = NIL_RTSPINLOCK;
270
271 RTR0Term();
272
273 memset(&g_DevExt, 0, sizeof(g_DevExt));
274
275 ddi_soft_state_fini(&g_pVBoxDrvSolarisState);
276 return 0;
277}
278
279
280int _info(struct modinfo *pModInfo)
281{
282 LogFlow((DEVICE_NAME ":_info\n"));
283 int e = mod_info(&g_VBoxDrvSolarisModLinkage, pModInfo);
284 return e;
285}
286
287
288/**
289 * Attach entry point, to attach a device to the system or resume it.
290 *
291 * @param pDip The module structure instance.
292 * @param enmCmd Operation type (attach/resume).
293 *
294 * @return corresponding solaris error code.
295 */
296static int VBoxDrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
297{
298 LogFlow((DEVICE_NAME ":VBoxDrvSolarisAttach\n"));
299
300 switch (enmCmd)
301 {
302 case DDI_ATTACH:
303 {
304 int rc;
305 int instance = ddi_get_instance(pDip);
306#ifdef USE_SESSION_HASH
307 vbox_devstate_t *pState;
308
309 if (ddi_soft_state_zalloc(g_pVBoxDrvSolarisState, instance) != DDI_SUCCESS)
310 {
311 LogRel((DEVICE_NAME ":VBoxDrvSolarisAttach: state alloc failed\n"));
312 return DDI_FAILURE;
313 }
314
315 pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
316#endif
317
318 /*
319 * Register for suspend/resume notifications
320 */
321 rc = ddi_prop_create(DDI_DEV_T_NONE, pDip, DDI_PROP_CANSLEEP /* kmem alloc can sleep */,
322 "pm-hardware-state", "needs-suspend-resume", sizeof("needs-suspend-resume"));
323 if (rc != DDI_PROP_SUCCESS)
324 LogRel((DEVICE_NAME ":Suspend/Resume notification registeration failed.\n"));
325
326 /*
327 * Register ourselves as a character device, pseudo-driver
328 */
329#ifdef VBOX_WITH_HARDENING
330 rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO,
331 0, NULL, NULL, 0600);
332#else
333 rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO,
334 0, "none", "none", 0666);
335#endif
336 if (rc == DDI_SUCCESS)
337 {
338#ifdef USE_SESSION_HASH
339 pState->pDip = pDip;
340#endif
341 ddi_report_dev(pDip);
342 return DDI_SUCCESS;
343 }
344
345 return DDI_FAILURE;
346 }
347
348 case DDI_RESUME:
349 {
350#if 0
351 RTSemFastMutexRequest(g_DevExt.mtxGip);
352 if (g_DevExt.pGipTimer)
353 RTTimerStart(g_DevExt.pGipTimer, 0);
354
355 RTSemFastMutexRelease(g_DevExt.mtxGip);
356#endif
357 RTPowerSignalEvent(RTPOWEREVENT_RESUME);
358 LogFlow((DEVICE_NAME ": Awakened from suspend.\n"));
359 return DDI_SUCCESS;
360 }
361
362 default:
363 return DDI_FAILURE;
364 }
365
366 return DDI_FAILURE;
367}
368
369
370/**
371 * Detach entry point, to detach a device to the system or suspend it.
372 *
373 * @param pDip The module structure instance.
374 * @param enmCmd Operation type (detach/suspend).
375 *
376 * @return corresponding solaris error code.
377 */
378static int VBoxDrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
379{
380 int rc = VINF_SUCCESS;
381
382 LogFlow((DEVICE_NAME ":VBoxDrvSolarisDetach\n"));
383 switch (enmCmd)
384 {
385 case DDI_DETACH:
386 {
387 int instance = ddi_get_instance(pDip);
388#ifndef USE_SESSION_HASH
389 ddi_remove_minor_node(pDip, NULL);
390#else
391 vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
392 ddi_remove_minor_node(pDip, NULL);
393 ddi_soft_state_free(g_pVBoxDrvSolarisState, instance);
394#endif
395 return DDI_SUCCESS;
396 }
397
398 case DDI_SUSPEND:
399 {
400#if 0
401 RTSemFastMutexRequest(g_DevExt.mtxGip);
402 if (g_DevExt.pGipTimer && g_DevExt.cGipUsers > 0)
403 RTTimerStop(g_DevExt.pGipTimer);
404
405 RTSemFastMutexRelease(g_DevExt.mtxGip);
406#endif
407 RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
408 LogFlow((DEVICE_NAME ": Falling to suspend mode.\n"));
409 return DDI_SUCCESS;
410
411 }
412
413 default:
414 return DDI_FAILURE;
415 }
416}
417
418
419
420/**
421 * User context entry points
422 */
423static int VBoxDrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
424{
425 int rc;
426 PSUPDRVSESSION pSession;
427 LogFlow((DEVICE_NAME ":VBoxDrvSolarisOpen: pDev=%p:%#x\n", pDev, *pDev));
428
429#ifndef USE_SESSION_HASH
430 /*
431 * Locate a new device open instance.
432 *
433 * For each open call we'll allocate an item in the soft state of the device.
434 * The item index is stored in the dev_t. I hope this is ok...
435 */
436 vbox_devstate_t *pState = NULL;
437 unsigned iOpenInstance;
438 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
439 {
440 if ( !ddi_get_soft_state(g_pVBoxDrvSolarisState, iOpenInstance) /* faster */
441 && ddi_soft_state_zalloc(g_pVBoxDrvSolarisState, iOpenInstance) == DDI_SUCCESS)
442 {
443 pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, iOpenInstance);
444 break;
445 }
446 }
447 if (!pState)
448 {
449 LogRel((DEVICE_NAME ":VBoxDrvSolarisOpen: too many open instances.\n"));
450 return ENXIO;
451 }
452
453 /*
454 * Create a new session.
455 */
456 rc = supdrvCreateSession(&g_DevExt, true /* fUser */, &pSession);
457 if (RT_SUCCESS(rc))
458 {
459 pSession->Uid = crgetruid(pCred);
460 pSession->Gid = crgetrgid(pCred);
461
462 pState->pSession = pSession;
463 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
464 LogFlow((DEVICE_NAME ":VBoxDrvSolarisOpen: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
465 *pDev, pSession, RTProcSelf(), RTR0ProcHandleSelf(), RTThreadNativeSelf() ));
466 return 0;
467 }
468
469 /* failed - clean up */
470 ddi_soft_state_free(g_pVBoxDrvSolarisState, iOpenInstance);
471
472#else
473 /*
474 * Create a new session.
475 * Sessions in Solaris driver are mostly useless. It's however needed
476 * in VBoxDrvSolarisIOCtlSlow() while calling supdrvIOCtl()
477 */
478 rc = supdrvCreateSession(&g_DevExt, true /* fUser */, &pSession);
479 if (RT_SUCCESS(rc))
480 {
481 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
482 unsigned iHash;
483
484 pSession->Uid = crgetruid(pCred);
485 pSession->Gid = crgetrgid(pCred);
486
487 /*
488 * Insert it into the hash table.
489 */
490 iHash = SESSION_HASH(pSession->Process);
491 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
492 pSession->pNextHash = g_apSessionHashTab[iHash];
493 g_apSessionHashTab[iHash] = pSession;
494 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
495 LogFlow((DEVICE_NAME ":VBoxDrvSolarisOpen success\n"));
496 }
497
498 int instance;
499 for (instance = 0; instance < DEVICE_MAXINSTANCES; instance++)
500 {
501 vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
502 if (pState)
503 break;
504 }
505
506 if (instance >= DEVICE_MAXINSTANCES)
507 {
508 LogRel((DEVICE_NAME ":VBoxDrvSolarisOpen: All instances exhausted\n"));
509 return ENXIO;
510 }
511
512 *pDev = makedevice(getmajor(*pDev), instance);
513
514 return VBoxSupDrvErr2SolarisErr(rc);
515#endif
516}
517
518
519static int VBoxDrvSolarisClose(dev_t Dev, int flag, int otyp, cred_t *cred)
520{
521 LogFlow((DEVICE_NAME ":VBoxDrvSolarisClose: Dev=%#x\n", Dev));
522
523#ifndef USE_SESSION_HASH
524 /*
525 * Get the session and free the soft state item.
526 */
527 vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, getminor(Dev));
528 if (!pState)
529 {
530 LogRel((DEVICE_NAME ":VBoxDrvSolarisClose: no state data for %#x (%d)\n", Dev, getminor(Dev)));
531 return EFAULT;
532 }
533
534 PSUPDRVSESSION pSession = pState->pSession;
535 pState->pSession = NULL;
536 ddi_soft_state_free(g_pVBoxDrvSolarisState, getminor(Dev));
537
538 if (!pSession)
539 {
540 LogRel((DEVICE_NAME ":VBoxDrvSolarisClose: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
541 return EFAULT;
542 }
543 LogFlow((DEVICE_NAME ":VBoxDrvSolarisClose: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
544 Dev, pSession, RTProcSelf(), RTR0ProcHandleSelf(), RTThreadNativeSelf() ));
545
546#else
547 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
548 const RTPROCESS Process = RTProcSelf();
549 const unsigned iHash = SESSION_HASH(Process);
550 PSUPDRVSESSION pSession;
551
552 /*
553 * Remove from the hash table.
554 */
555 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
556 pSession = g_apSessionHashTab[iHash];
557 if (pSession)
558 {
559 if (pSession->Process == Process)
560 {
561 g_apSessionHashTab[iHash] = pSession->pNextHash;
562 pSession->pNextHash = NULL;
563 }
564 else
565 {
566 PSUPDRVSESSION pPrev = pSession;
567 pSession = pSession->pNextHash;
568 while (pSession)
569 {
570 if (pSession->Process == Process)
571 {
572 pPrev->pNextHash = pSession->pNextHash;
573 pSession->pNextHash = NULL;
574 break;
575 }
576
577 /* next */
578 pPrev = pSession;
579 pSession = pSession->pNextHash;
580 }
581 }
582 }
583 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
584 if (!pSession)
585 {
586 LogRel((DEVICE_NAME ":VBoxDrvSolarisClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n",
587 (int)Process));
588 return EFAULT;
589 }
590#endif
591
592 /*
593 * Close the session.
594 */
595 supdrvCloseSession(&g_DevExt, pSession);
596 return 0;
597}
598
599
600static int VBoxDrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
601{
602 LogFlow((DEVICE_NAME ":VBoxDrvSolarisRead"));
603 return 0;
604}
605
606
607static int VBoxDrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
608{
609 LogFlow((DEVICE_NAME ":VBoxDrvSolarisWrite"));
610 return 0;
611}
612
613
614/**
615 * Driver ioctl, an alternate entry point for this character driver.
616 *
617 * @param Dev Device number
618 * @param Cmd Operation identifier
619 * @param pArg Arguments from user to driver
620 * @param Mode Information bitfield (read/write, address space etc.)
621 * @param pCred User credentials
622 * @param pVal Return value for calling process.
623 *
624 * @return corresponding solaris error code.
625 */
626static int VBoxDrvSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArgs, int Mode, cred_t *pCred, int *pVal)
627{
628#ifndef USE_SESSION_HASH
629 /*
630 * Get the session from the soft state item.
631 */
632 vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, getminor(Dev));
633 if (!pState)
634 {
635 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtl: no state data for %#x (%d)\n", Dev, getminor(Dev)));
636 return EINVAL;
637 }
638
639 PSUPDRVSESSION pSession = pState->pSession;
640 if (!pSession)
641 {
642 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtl: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
643 return DDI_SUCCESS;
644 }
645#else
646 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
647 const RTPROCESS Process = RTProcSelf();
648 const unsigned iHash = SESSION_HASH(Process);
649 PSUPDRVSESSION pSession;
650
651 /*
652 * Find the session.
653 */
654 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
655 pSession = g_apSessionHashTab[iHash];
656 if (pSession && pSession->Process != Process)
657 {
658 do pSession = pSession->pNextHash;
659 while (pSession && pSession->Process != Process);
660 }
661 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
662 if (!pSession)
663 {
664 LogRel((DEVICE_NAME ":VBoxSupDrvIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x\n",
665 (int)Process, Cmd));
666 return EINVAL;
667 }
668#endif
669
670 /*
671 * Deal with the two high-speed IOCtl that takes it's arguments from
672 * the session and iCmd, and only returns a VBox status code.
673 */
674 if ( Cmd == SUP_IOCTL_FAST_DO_RAW_RUN
675 || Cmd == SUP_IOCTL_FAST_DO_HWACC_RUN
676 || Cmd == SUP_IOCTL_FAST_DO_NOP)
677 {
678 *pVal = supdrvIOCtlFast(Cmd, pArgs, &g_DevExt, pSession);
679 return 0;
680 }
681
682 return VBoxDrvSolarisIOCtlSlow(pSession, Cmd, Mode, pArgs);
683}
684
685
686/** @def IOCPARM_LEN
687 * Gets the length from the ioctl number.
688 * This is normally defined by sys/ioccom.h on BSD systems...
689 */
690#ifndef IOCPARM_LEN
691# define IOCPARM_LEN(x) ( ((x) >> 16) & IOCPARM_MASK )
692#endif
693
694
695/**
696 * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
697 *
698 * @returns Solaris errno.
699 *
700 * @param pSession The session.
701 * @param Cmd The IOCtl command.
702 * @param Mode Information bitfield (for specifying ownership of data)
703 * @param iArg User space address of the request buffer.
704 */
705static int VBoxDrvSolarisIOCtlSlow(PSUPDRVSESSION pSession, int iCmd, int Mode, intptr_t iArg)
706{
707 int rc;
708 uint32_t cbBuf = 0;
709 union
710 {
711 SUPREQHDR Hdr;
712 uint8_t abBuf[64];
713 } StackBuf;
714 PSUPREQHDR pHdr;
715
716
717 /*
718 * Read the header.
719 */
720 if (RT_UNLIKELY(IOCPARM_LEN(iCmd) != sizeof(StackBuf.Hdr)))
721 {
722 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(StackBuf.Hdr)));
723 return EINVAL;
724 }
725 rc = ddi_copyin((void *)iArg, &StackBuf.Hdr, sizeof(StackBuf.Hdr), Mode);
726 if (RT_UNLIKELY(rc))
727 {
728 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: ddi_copyin(,%#lx,) failed; iCmd=%#x. rc=%d\n", iArg, iCmd, rc));
729 return EFAULT;
730 }
731 if (RT_UNLIKELY((StackBuf.Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
732 {
733 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: bad header magic %#x; iCmd=%#x\n", StackBuf.Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, iCmd));
734 return EINVAL;
735 }
736 cbBuf = RT_MAX(StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut);
737 if (RT_UNLIKELY( StackBuf.Hdr.cbIn < sizeof(StackBuf.Hdr)
738 || StackBuf.Hdr.cbOut < sizeof(StackBuf.Hdr)
739 || cbBuf > _1M*16))
740 {
741 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: max(%#x,%#x); iCmd=%#x\n", StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut, iCmd));
742 return EINVAL;
743 }
744
745 /*
746 * Buffer the request.
747 */
748 if (cbBuf <= sizeof(StackBuf))
749 pHdr = &StackBuf.Hdr;
750 else
751 {
752 pHdr = RTMemTmpAlloc(cbBuf);
753 if (RT_UNLIKELY(!pHdr))
754 {
755 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
756 return ENOMEM;
757 }
758 }
759 rc = ddi_copyin((void *)iArg, pHdr, cbBuf, Mode);
760 if (RT_UNLIKELY(rc))
761 {
762 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, cbBuf, iCmd, rc));
763 if (pHdr != &StackBuf.Hdr)
764 RTMemFree(pHdr);
765 return EFAULT;
766 }
767
768 /*
769 * Process the IOCtl.
770 */
771 rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr);
772
773 /*
774 * Copy ioctl data and output buffer back to user space.
775 */
776 if (RT_LIKELY(!rc))
777 {
778 uint32_t cbOut = pHdr->cbOut;
779 if (RT_UNLIKELY(cbOut > cbBuf))
780 {
781 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: too much output! %#x > %#x; iCmd=%#x!\n", cbOut, cbBuf, iCmd));
782 cbOut = cbBuf;
783 }
784 rc = ddi_copyout(pHdr, (void *)iArg, cbOut, Mode);
785 if (RT_UNLIKELY(rc != 0))
786 {
787 /* this is really bad */
788 LogRel((DEVICE_NAME ":VBoxDrvSolarisIOCtlSlow: ddi_copyout(,%p,%d) failed. rc=%d\n", (void *)iArg, cbBuf, rc));
789 rc = EFAULT;
790 }
791 }
792 else
793 rc = EINVAL;
794
795 if (pHdr != &StackBuf.Hdr)
796 RTMemTmpFree(pHdr);
797 return rc;
798}
799
800
801/**
802 * The SUPDRV IDC entry point.
803 *
804 * @returns VBox status code, see supdrvIDC.
805 * @param iReq The request code.
806 * @param pReq The request.
807 */
808int VBOXCALL SUPDrvSolarisIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
809{
810 PSUPDRVSESSION pSession;
811
812 /*
813 * Some quick validations.
814 */
815 if (RT_UNLIKELY(!VALID_PTR(pReq)))
816 return VERR_INVALID_POINTER;
817
818 pSession = pReq->pSession;
819 if (pSession)
820 {
821 if (RT_UNLIKELY(!VALID_PTR(pSession)))
822 return VERR_INVALID_PARAMETER;
823 if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
824 return VERR_INVALID_PARAMETER;
825 }
826 else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
827 return VERR_INVALID_PARAMETER;
828
829 /*
830 * Do the job.
831 */
832 return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
833}
834
835
836/**
837 * Converts an supdrv error code to a solaris error code.
838 *
839 * @returns corresponding solaris error code.
840 * @param rc supdrv error code (SUPDRV_ERR_* defines).
841 */
842static int VBoxSupDrvErr2SolarisErr(int rc)
843{
844 switch (rc)
845 {
846 case 0: return 0;
847 case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
848 case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
849 case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
850 case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
851 case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
852 case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
853 case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
854 case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
855 case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
856 }
857
858 return EPERM;
859}
860
861
862/**
863 * Initializes any OS specific object creator fields.
864 */
865void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
866{
867 NOREF(pObj);
868 NOREF(pSession);
869}
870
871
872/**
873 * Checks if the session can access the object.
874 *
875 * @returns true if a decision has been made.
876 * @returns false if the default access policy should be applied.
877 *
878 * @param pObj The object in question.
879 * @param pSession The session wanting to access the object.
880 * @param pszObjName The object name, can be NULL.
881 * @param prc Where to store the result when returning true.
882 */
883bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
884{
885 NOREF(pObj);
886 NOREF(pSession);
887 NOREF(pszObjName);
888 NOREF(prc);
889 return false;
890}
891
892
893bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
894{
895 return false;
896}
897
898
899RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
900{
901 va_list args;
902 char szMsg[512];
903
904 va_start(args, pszFormat);
905 RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, args);
906 va_end(args);
907
908 szMsg[sizeof(szMsg) - 1] = '\0';
909 cmn_err(CE_CONT, "%s", szMsg);
910 return 0;
911}
912
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette