1 | /* $Id: SUPDrv-solaris.c 54581 2015-03-02 14:56:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - The VirtualBox Support Driver - Solaris specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
31 | #ifdef DEBUG_ramshankar
|
---|
32 | # define LOG_ENABLED
|
---|
33 | # define LOG_INSTANCE RTLogRelDefaultInstance()
|
---|
34 | #endif
|
---|
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/kobj.h>
|
---|
42 | #include <sys/kobj_impl.h>
|
---|
43 | #include <sys/open.h>
|
---|
44 | #include <sys/conf.h>
|
---|
45 | #include <sys/cmn_err.h>
|
---|
46 | #include <sys/stat.h>
|
---|
47 | #include <sys/ddi.h>
|
---|
48 | #include <sys/sunddi.h>
|
---|
49 | #include <sys/file.h>
|
---|
50 | #include <sys/priv_names.h>
|
---|
51 | #undef u /* /usr/include/sys/user.h:249:1 is where this is defined to (curproc->p_user). very cool. */
|
---|
52 |
|
---|
53 | #include "../SUPDrvInternal.h"
|
---|
54 | #include <VBox/log.h>
|
---|
55 | #include <VBox/version.h>
|
---|
56 | #include <iprt/semaphore.h>
|
---|
57 | #include <iprt/spinlock.h>
|
---|
58 | #include <iprt/mp.h>
|
---|
59 | #include <iprt/path.h>
|
---|
60 | #include <iprt/power.h>
|
---|
61 | #include <iprt/process.h>
|
---|
62 | #include <iprt/thread.h>
|
---|
63 | #include <iprt/initterm.h>
|
---|
64 | #include <iprt/alloc.h>
|
---|
65 | #include <iprt/string.h>
|
---|
66 | #include <iprt/err.h>
|
---|
67 |
|
---|
68 | #include "dtrace/SUPDrv.h"
|
---|
69 |
|
---|
70 |
|
---|
71 | /*******************************************************************************
|
---|
72 | * Defined Constants And Macros *
|
---|
73 | *******************************************************************************/
|
---|
74 | /** The system device name. */
|
---|
75 | #define DEVICE_NAME_SYS "vboxdrv"
|
---|
76 | /** The user device name. */
|
---|
77 | #define DEVICE_NAME_USR "vboxdrvu"
|
---|
78 | /** The module description as seen in 'modinfo'. */
|
---|
79 | #define DEVICE_DESC "VirtualBox HostDrv"
|
---|
80 | /** Maximum number of driver instances. */
|
---|
81 | #define DEVICE_MAXINSTANCES 16
|
---|
82 |
|
---|
83 |
|
---|
84 | /*******************************************************************************
|
---|
85 | * Internal Functions *
|
---|
86 | *******************************************************************************/
|
---|
87 | static int VBoxDrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
|
---|
88 | static int VBoxDrvSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
|
---|
89 | static int VBoxDrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
|
---|
90 | static int VBoxDrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
|
---|
91 | static int VBoxDrvSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArgs, int mode, cred_t *pCred, int *pVal);
|
---|
92 |
|
---|
93 | static int VBoxDrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t Cmd);
|
---|
94 | static int VBoxDrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t Cmd);
|
---|
95 | static int VBoxDrvSolarisQuiesceNotNeeded(dev_info_t *pDip);
|
---|
96 |
|
---|
97 | static int VBoxSupDrvErr2SolarisErr(int rc);
|
---|
98 | static int VBoxDrvSolarisIOCtlSlow(PSUPDRVSESSION pSession, int Cmd, int Mode, intptr_t pArgs);
|
---|
99 |
|
---|
100 |
|
---|
101 | /*******************************************************************************
|
---|
102 | * Global Variables *
|
---|
103 | *******************************************************************************/
|
---|
104 | /**
|
---|
105 | * cb_ops: for drivers that support char/block entry points
|
---|
106 | */
|
---|
107 | static struct cb_ops g_VBoxDrvSolarisCbOps =
|
---|
108 | {
|
---|
109 | VBoxDrvSolarisOpen,
|
---|
110 | VBoxDrvSolarisClose,
|
---|
111 | nodev, /* b strategy */
|
---|
112 | nodev, /* b dump */
|
---|
113 | nodev, /* b print */
|
---|
114 | VBoxDrvSolarisRead,
|
---|
115 | VBoxDrvSolarisWrite,
|
---|
116 | VBoxDrvSolarisIOCtl,
|
---|
117 | nodev, /* c devmap */
|
---|
118 | nodev, /* c mmap */
|
---|
119 | nodev, /* c segmap */
|
---|
120 | nochpoll, /* c poll */
|
---|
121 | ddi_prop_op, /* property ops */
|
---|
122 | NULL, /* streamtab */
|
---|
123 | D_NEW | D_MP, /* compat. flag */
|
---|
124 | CB_REV /* revision */
|
---|
125 | };
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * dev_ops: for driver device operations
|
---|
129 | */
|
---|
130 | static struct dev_ops g_VBoxDrvSolarisDevOps =
|
---|
131 | {
|
---|
132 | DEVO_REV, /* driver build revision */
|
---|
133 | 0, /* ref count */
|
---|
134 | nulldev, /* get info */
|
---|
135 | nulldev, /* identify */
|
---|
136 | nulldev, /* probe */
|
---|
137 | VBoxDrvSolarisAttach,
|
---|
138 | VBoxDrvSolarisDetach,
|
---|
139 | nodev, /* reset */
|
---|
140 | &g_VBoxDrvSolarisCbOps,
|
---|
141 | (struct bus_ops *)0,
|
---|
142 | nodev, /* power */
|
---|
143 | VBoxDrvSolarisQuiesceNotNeeded
|
---|
144 | };
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * modldrv: export driver specifics to the kernel
|
---|
148 | */
|
---|
149 | static struct modldrv g_VBoxDrvSolarisModule =
|
---|
150 | {
|
---|
151 | &mod_driverops, /* extern from kernel */
|
---|
152 | DEVICE_DESC " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
|
---|
153 | &g_VBoxDrvSolarisDevOps
|
---|
154 | };
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * modlinkage: export install/remove/info to the kernel
|
---|
158 | */
|
---|
159 | static struct modlinkage g_VBoxDrvSolarisModLinkage =
|
---|
160 | {
|
---|
161 | MODREV_1, /* loadable module system revision */
|
---|
162 | {
|
---|
163 | &g_VBoxDrvSolarisModule,
|
---|
164 | NULL /* terminate array of linkage structures */
|
---|
165 | }
|
---|
166 | };
|
---|
167 |
|
---|
168 | #ifndef USE_SESSION_HASH
|
---|
169 | /**
|
---|
170 | * State info for each open file handle.
|
---|
171 | */
|
---|
172 | typedef struct
|
---|
173 | {
|
---|
174 | /**< Pointer to the session data. */
|
---|
175 | PSUPDRVSESSION pSession;
|
---|
176 | } vbox_devstate_t;
|
---|
177 | #else
|
---|
178 | /** State info. for each driver instance. */
|
---|
179 | typedef struct
|
---|
180 | {
|
---|
181 | dev_info_t *pDip; /* Device handle */
|
---|
182 | } vbox_devstate_t;
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | /** Opaque pointer to list of state */
|
---|
186 | static void *g_pVBoxDrvSolarisState;
|
---|
187 |
|
---|
188 | /** Device extention & session data association structure */
|
---|
189 | static SUPDRVDEVEXT g_DevExt;
|
---|
190 |
|
---|
191 | /** Hash table */
|
---|
192 | static PSUPDRVSESSION g_apSessionHashTab[19];
|
---|
193 | /** Spinlock protecting g_apSessionHashTab. */
|
---|
194 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
195 | /** Calculates bucket index into g_apSessionHashTab.*/
|
---|
196 | #define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Kernel entry points
|
---|
200 | */
|
---|
201 | int _init(void)
|
---|
202 | {
|
---|
203 | #if 0 /* No IPRT logging before RTR0Init() is done! */
|
---|
204 | LogFlowFunc(("vboxdrv:_init\n"));
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Prevent module autounloading.
|
---|
209 | */
|
---|
210 | modctl_t *pModCtl = mod_getctl(&g_VBoxDrvSolarisModLinkage);
|
---|
211 | if (pModCtl)
|
---|
212 | pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
|
---|
213 | else
|
---|
214 | cmn_err(CE_NOTE, "vboxdrv: failed to disable autounloading!\n");
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
|
---|
218 | */
|
---|
219 | int rc = RTR0Init(0);
|
---|
220 | if (RT_SUCCESS(rc))
|
---|
221 | {
|
---|
222 | /*
|
---|
223 | * Initialize the device extension
|
---|
224 | */
|
---|
225 | rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
|
---|
226 | if (RT_SUCCESS(rc))
|
---|
227 | {
|
---|
228 | cmn_err(CE_CONT, "!tsc::mode %s @ tentative %lu Hz\n", SUPGetGIPModeName(g_DevExt.pGip), g_DevExt.pGip->u64CpuHz);
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Initialize the session hash table.
|
---|
232 | */
|
---|
233 | memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab));
|
---|
234 | rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDrvSol");
|
---|
235 | if (RT_SUCCESS(rc))
|
---|
236 | {
|
---|
237 | rc = ddi_soft_state_init(&g_pVBoxDrvSolarisState, sizeof(vbox_devstate_t), 8);
|
---|
238 | if (!rc)
|
---|
239 | {
|
---|
240 | rc = mod_install(&g_VBoxDrvSolarisModLinkage);
|
---|
241 | if (!rc)
|
---|
242 | return rc; /* success */
|
---|
243 |
|
---|
244 | ddi_soft_state_fini(&g_pVBoxDrvSolarisState);
|
---|
245 | LogRel(("vboxdrv: mod_install failed! rc=%d\n", rc));
|
---|
246 | }
|
---|
247 | else
|
---|
248 | LogRel(("vboxdrv: failed to initialize soft state.\n"));
|
---|
249 |
|
---|
250 | RTSpinlockDestroy(g_Spinlock);
|
---|
251 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | LogRel(("VBoxDrvSolarisAttach: RTSpinlockCreate failed\n"));
|
---|
256 | rc = RTErrConvertToErrno(rc);
|
---|
257 | }
|
---|
258 | supdrvDeleteDevExt(&g_DevExt);
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | LogRel(("VBoxDrvSolarisAttach: supdrvInitDevExt failed\n"));
|
---|
263 | rc = EINVAL;
|
---|
264 | }
|
---|
265 | RTR0TermForced();
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | LogRel(("VBoxDrvSolarisAttach: failed to init R0Drv\n"));
|
---|
270 | rc = RTErrConvertToErrno(rc);
|
---|
271 | }
|
---|
272 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
273 |
|
---|
274 | return rc;
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | int _fini(void)
|
---|
279 | {
|
---|
280 | LogFlowFunc(("vboxdrv:_fini\n"));
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Undo the work we did at start (in the reverse order).
|
---|
284 | */
|
---|
285 | int rc = mod_remove(&g_VBoxDrvSolarisModLinkage);
|
---|
286 | if (rc != 0)
|
---|
287 | return rc;
|
---|
288 |
|
---|
289 | supdrvDeleteDevExt(&g_DevExt);
|
---|
290 |
|
---|
291 | rc = RTSpinlockDestroy(g_Spinlock);
|
---|
292 | AssertRC(rc);
|
---|
293 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
294 |
|
---|
295 | RTR0TermForced();
|
---|
296 |
|
---|
297 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
298 |
|
---|
299 | ddi_soft_state_fini(&g_pVBoxDrvSolarisState);
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | int _info(struct modinfo *pModInfo)
|
---|
305 | {
|
---|
306 | #if 0 /* No IPRT logging before RTR0Init() is done! And yes this is called before _init()!*/
|
---|
307 | LogFlowFunc(("vboxdrv:_init\n"));
|
---|
308 | #endif
|
---|
309 | int e = mod_info(&g_VBoxDrvSolarisModLinkage, pModInfo);
|
---|
310 | return e;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Attach entry point, to attach a device to the system or resume it.
|
---|
316 | *
|
---|
317 | * @param pDip The module structure instance.
|
---|
318 | * @param enmCmd Operation type (attach/resume).
|
---|
319 | *
|
---|
320 | * @return corresponding solaris error code.
|
---|
321 | */
|
---|
322 | static int VBoxDrvSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
|
---|
323 | {
|
---|
324 | LogFlowFunc(("VBoxDrvSolarisAttach\n"));
|
---|
325 |
|
---|
326 | switch (enmCmd)
|
---|
327 | {
|
---|
328 | case DDI_ATTACH:
|
---|
329 | {
|
---|
330 | int rc;
|
---|
331 | #ifdef USE_SESSION_HASH
|
---|
332 | int instance = ddi_get_instance(pDip);
|
---|
333 | vbox_devstate_t *pState;
|
---|
334 |
|
---|
335 | if (ddi_soft_state_zalloc(g_pVBoxDrvSolarisState, instance) != DDI_SUCCESS)
|
---|
336 | {
|
---|
337 | LogRel(("VBoxDrvSolarisAttach: state alloc failed\n"));
|
---|
338 | return DDI_FAILURE;
|
---|
339 | }
|
---|
340 |
|
---|
341 | pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
|
---|
342 | #endif
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Register for suspend/resume notifications
|
---|
346 | */
|
---|
347 | rc = ddi_prop_create(DDI_DEV_T_NONE, pDip, DDI_PROP_CANSLEEP /* kmem alloc can sleep */,
|
---|
348 | "pm-hardware-state", "needs-suspend-resume", sizeof("needs-suspend-resume"));
|
---|
349 | if (rc != DDI_PROP_SUCCESS)
|
---|
350 | LogRel(("vboxdrv: Suspend/Resume notification registration failed.\n"));
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Register ourselves as a character device, pseudo-driver
|
---|
354 | */
|
---|
355 | #ifdef VBOX_WITH_HARDENING
|
---|
356 | rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME_SYS, S_IFCHR, 0 /*minor*/, DDI_PSEUDO,
|
---|
357 | 0, NULL, NULL, 0600);
|
---|
358 | #else
|
---|
359 | rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME_SYS, S_IFCHR, 0 /*minor*/, DDI_PSEUDO,
|
---|
360 | 0, "none", "none", 0666);
|
---|
361 | #endif
|
---|
362 | if (rc == DDI_SUCCESS)
|
---|
363 | {
|
---|
364 | rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME_USR, S_IFCHR, 1 /*minor*/, DDI_PSEUDO,
|
---|
365 | 0, "none", "none", 0666);
|
---|
366 | if (rc == DDI_SUCCESS)
|
---|
367 | {
|
---|
368 | #ifdef USE_SESSION_HASH
|
---|
369 | pState->pDip = pDip;
|
---|
370 | #endif
|
---|
371 | ddi_report_dev(pDip);
|
---|
372 | return DDI_SUCCESS;
|
---|
373 | }
|
---|
374 | ddi_remove_minor_node(pDip, NULL);
|
---|
375 | }
|
---|
376 |
|
---|
377 | return DDI_FAILURE;
|
---|
378 | }
|
---|
379 |
|
---|
380 | case DDI_RESUME:
|
---|
381 | {
|
---|
382 | #if 0
|
---|
383 | RTSemFastMutexRequest(g_DevExt.mtxGip);
|
---|
384 | if (g_DevExt.pGipTimer)
|
---|
385 | RTTimerStart(g_DevExt.pGipTimer, 0);
|
---|
386 |
|
---|
387 | RTSemFastMutexRelease(g_DevExt.mtxGip);
|
---|
388 | #endif
|
---|
389 | RTPowerSignalEvent(RTPOWEREVENT_RESUME);
|
---|
390 | LogFlow(("vboxdrv: Awakened from suspend.\n"));
|
---|
391 | return DDI_SUCCESS;
|
---|
392 | }
|
---|
393 |
|
---|
394 | default:
|
---|
395 | return DDI_FAILURE;
|
---|
396 | }
|
---|
397 |
|
---|
398 | return DDI_FAILURE;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Detach entry point, to detach a device to the system or suspend it.
|
---|
404 | *
|
---|
405 | * @param pDip The module structure instance.
|
---|
406 | * @param enmCmd Operation type (detach/suspend).
|
---|
407 | *
|
---|
408 | * @return corresponding solaris error code.
|
---|
409 | */
|
---|
410 | static int VBoxDrvSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
|
---|
411 | {
|
---|
412 | LogFlowFunc(("VBoxDrvSolarisDetach\n"));
|
---|
413 | switch (enmCmd)
|
---|
414 | {
|
---|
415 | case DDI_DETACH:
|
---|
416 | {
|
---|
417 | #ifndef USE_SESSION_HASH
|
---|
418 | ddi_remove_minor_node(pDip, NULL);
|
---|
419 | #else
|
---|
420 | int instance = ddi_get_instance(pDip);
|
---|
421 | vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
|
---|
422 | ddi_remove_minor_node(pDip, NULL);
|
---|
423 | ddi_soft_state_free(g_pVBoxDrvSolarisState, instance);
|
---|
424 | #endif
|
---|
425 | ddi_prop_remove_all(pDip);
|
---|
426 | return DDI_SUCCESS;
|
---|
427 | }
|
---|
428 |
|
---|
429 | case DDI_SUSPEND:
|
---|
430 | {
|
---|
431 | #if 0
|
---|
432 | RTSemFastMutexRequest(g_DevExt.mtxGip);
|
---|
433 | if (g_DevExt.pGipTimer && g_DevExt.cGipUsers > 0)
|
---|
434 | RTTimerStop(g_DevExt.pGipTimer);
|
---|
435 |
|
---|
436 | RTSemFastMutexRelease(g_DevExt.mtxGip);
|
---|
437 | #endif
|
---|
438 | RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
|
---|
439 | LogFlow(("vboxdrv: Falling to suspend mode.\n"));
|
---|
440 | return DDI_SUCCESS;
|
---|
441 |
|
---|
442 | }
|
---|
443 |
|
---|
444 | default:
|
---|
445 | return DDI_FAILURE;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Quiesce not-needed entry point, as Solaris 10 doesn't have any
|
---|
452 | * ddi_quiesce_not_needed() function.
|
---|
453 | *
|
---|
454 | * @param pDip The module structure instance.
|
---|
455 | *
|
---|
456 | * @return corresponding solaris error code.
|
---|
457 | */
|
---|
458 | static int VBoxDrvSolarisQuiesceNotNeeded(dev_info_t *pDip)
|
---|
459 | {
|
---|
460 | return DDI_SUCCESS;
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * open() worker.
|
---|
466 | */
|
---|
467 | static int VBoxDrvSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
|
---|
468 | {
|
---|
469 | const bool fUnrestricted = getminor(*pDev) == 0;
|
---|
470 | PSUPDRVSESSION pSession;
|
---|
471 | int rc;
|
---|
472 |
|
---|
473 | LogFlowFunc(("VBoxDrvSolarisOpen: pDev=%p:%#x\n", pDev, *pDev));
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Validate input
|
---|
477 | */
|
---|
478 | if ( (getminor(*pDev) != 0 && getminor(*pDev) != 1)
|
---|
479 | || fType != OTYP_CHR)
|
---|
480 | return EINVAL; /* See mmopen for precedent. */
|
---|
481 |
|
---|
482 | #ifndef USE_SESSION_HASH
|
---|
483 | /*
|
---|
484 | * Locate a new device open instance.
|
---|
485 | *
|
---|
486 | * For each open call we'll allocate an item in the soft state of the device.
|
---|
487 | * The item index is stored in the dev_t. I hope this is ok...
|
---|
488 | */
|
---|
489 | vbox_devstate_t *pState = NULL;
|
---|
490 | unsigned iOpenInstance;
|
---|
491 | for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
|
---|
492 | {
|
---|
493 | if ( !ddi_get_soft_state(g_pVBoxDrvSolarisState, iOpenInstance) /* faster */
|
---|
494 | && ddi_soft_state_zalloc(g_pVBoxDrvSolarisState, iOpenInstance) == DDI_SUCCESS)
|
---|
495 | {
|
---|
496 | pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, iOpenInstance);
|
---|
497 | break;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | if (!pState)
|
---|
501 | {
|
---|
502 | LogRel(("VBoxDrvSolarisOpen: too many open instances.\n"));
|
---|
503 | return ENXIO;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /*
|
---|
507 | * Create a new session.
|
---|
508 | */
|
---|
509 | rc = supdrvCreateSession(&g_DevExt, true /* fUser */, fUnrestricted, &pSession);
|
---|
510 | if (RT_SUCCESS(rc))
|
---|
511 | {
|
---|
512 | pSession->Uid = crgetruid(pCred);
|
---|
513 | pSession->Gid = crgetrgid(pCred);
|
---|
514 |
|
---|
515 | pState->pSession = pSession;
|
---|
516 | *pDev = makedevice(getmajor(*pDev), iOpenInstance);
|
---|
517 | LogFlow(("VBoxDrvSolarisOpen: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
|
---|
518 | *pDev, pSession, RTProcSelf(), RTR0ProcHandleSelf(), RTThreadNativeSelf() ));
|
---|
519 | return 0;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /* failed - clean up */
|
---|
523 | ddi_soft_state_free(g_pVBoxDrvSolarisState, iOpenInstance);
|
---|
524 |
|
---|
525 | #else
|
---|
526 | /*
|
---|
527 | * Create a new session.
|
---|
528 | * Sessions in Solaris driver are mostly useless. It's however needed
|
---|
529 | * in VBoxDrvSolarisIOCtlSlow() while calling supdrvIOCtl()
|
---|
530 | */
|
---|
531 | rc = supdrvCreateSession(&g_DevExt, true /* fUser */, fUnrestricted, &pSession);
|
---|
532 | if (RT_SUCCESS(rc))
|
---|
533 | {
|
---|
534 | unsigned iHash;
|
---|
535 |
|
---|
536 | pSession->Uid = crgetruid(pCred);
|
---|
537 | pSession->Gid = crgetrgid(pCred);
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Insert it into the hash table.
|
---|
541 | */
|
---|
542 | # error "Only one entry per process!"
|
---|
543 | iHash = SESSION_HASH(pSession->Process);
|
---|
544 | RTSpinlockAcquire(g_Spinlock);
|
---|
545 | pSession->pNextHash = g_apSessionHashTab[iHash];
|
---|
546 | g_apSessionHashTab[iHash] = pSession;
|
---|
547 | RTSpinlockRelease(g_Spinlock);
|
---|
548 | LogFlow(("VBoxDrvSolarisOpen success\n"));
|
---|
549 | }
|
---|
550 |
|
---|
551 | int instance;
|
---|
552 | for (instance = 0; instance < DEVICE_MAXINSTANCES; instance++)
|
---|
553 | {
|
---|
554 | vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, instance);
|
---|
555 | if (pState)
|
---|
556 | break;
|
---|
557 | }
|
---|
558 |
|
---|
559 | if (instance >= DEVICE_MAXINSTANCES)
|
---|
560 | {
|
---|
561 | LogRel(("VBoxDrvSolarisOpen: All instances exhausted\n"));
|
---|
562 | return ENXIO;
|
---|
563 | }
|
---|
564 |
|
---|
565 | *pDev = makedevice(getmajor(*pDev), instance);
|
---|
566 | #endif
|
---|
567 |
|
---|
568 | return VBoxSupDrvErr2SolarisErr(rc);
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | static int VBoxDrvSolarisClose(dev_t Dev, int flag, int otyp, cred_t *cred)
|
---|
573 | {
|
---|
574 | LogFlowFunc(("VBoxDrvSolarisClose: Dev=%#x\n", Dev));
|
---|
575 |
|
---|
576 | #ifndef USE_SESSION_HASH
|
---|
577 | /*
|
---|
578 | * Get the session and free the soft state item.
|
---|
579 | */
|
---|
580 | vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, getminor(Dev));
|
---|
581 | if (!pState)
|
---|
582 | {
|
---|
583 | LogRel(("VBoxDrvSolarisClose: no state data for %#x (%d)\n", Dev, getminor(Dev)));
|
---|
584 | return EFAULT;
|
---|
585 | }
|
---|
586 |
|
---|
587 | PSUPDRVSESSION pSession = pState->pSession;
|
---|
588 | pState->pSession = NULL;
|
---|
589 | ddi_soft_state_free(g_pVBoxDrvSolarisState, getminor(Dev));
|
---|
590 |
|
---|
591 | if (!pSession)
|
---|
592 | {
|
---|
593 | LogRel(("VBoxDrvSolarisClose: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
|
---|
594 | return EFAULT;
|
---|
595 | }
|
---|
596 | LogFlow(("VBoxDrvSolarisClose: Dev=%#x pSession=%p pid=%d r0proc=%p thread=%p\n",
|
---|
597 | Dev, pSession, RTProcSelf(), RTR0ProcHandleSelf(), RTThreadNativeSelf() ));
|
---|
598 |
|
---|
599 | #else
|
---|
600 | const RTPROCESS Process = RTProcSelf();
|
---|
601 | const unsigned iHash = SESSION_HASH(Process);
|
---|
602 | PSUPDRVSESSION pSession;
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Remove from the hash table.
|
---|
606 | */
|
---|
607 | RTSpinlockAcquire(g_Spinlock);
|
---|
608 | pSession = g_apSessionHashTab[iHash];
|
---|
609 | if (pSession)
|
---|
610 | {
|
---|
611 | if (pSession->Process == Process)
|
---|
612 | {
|
---|
613 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
614 | pSession->pNextHash = NULL;
|
---|
615 | }
|
---|
616 | else
|
---|
617 | {
|
---|
618 | PSUPDRVSESSION pPrev = pSession;
|
---|
619 | pSession = pSession->pNextHash;
|
---|
620 | while (pSession)
|
---|
621 | {
|
---|
622 | if (pSession->Process == Process)
|
---|
623 | {
|
---|
624 | pPrev->pNextHash = pSession->pNextHash;
|
---|
625 | pSession->pNextHash = NULL;
|
---|
626 | break;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /* next */
|
---|
630 | pPrev = pSession;
|
---|
631 | pSession = pSession->pNextHash;
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 | RTSpinlockRelease(g_Spinlock);
|
---|
636 | if (!pSession)
|
---|
637 | {
|
---|
638 | LogRel(("VBoxDrvSolarisClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n", (int)Process));
|
---|
639 | return EFAULT;
|
---|
640 | }
|
---|
641 | #endif
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * Close the session.
|
---|
645 | */
|
---|
646 | supdrvSessionRelease(pSession);
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | static int VBoxDrvSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
|
---|
652 | {
|
---|
653 | LogFlowFunc(("VBoxDrvSolarisRead"));
|
---|
654 | return 0;
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | static int VBoxDrvSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
|
---|
659 | {
|
---|
660 | LogFlowFunc(("VBoxDrvSolarisWrite"));
|
---|
661 | return 0;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * Driver ioctl, an alternate entry point for this character driver.
|
---|
667 | *
|
---|
668 | * @param Dev Device number
|
---|
669 | * @param Cmd Operation identifier
|
---|
670 | * @param pArg Arguments from user to driver
|
---|
671 | * @param Mode Information bitfield (read/write, address space etc.)
|
---|
672 | * @param pCred User credentials
|
---|
673 | * @param pVal Return value for calling process.
|
---|
674 | *
|
---|
675 | * @return corresponding solaris error code.
|
---|
676 | */
|
---|
677 | static int VBoxDrvSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArgs, int Mode, cred_t *pCred, int *pVal)
|
---|
678 | {
|
---|
679 | #ifndef USE_SESSION_HASH
|
---|
680 | /*
|
---|
681 | * Get the session from the soft state item.
|
---|
682 | */
|
---|
683 | vbox_devstate_t *pState = ddi_get_soft_state(g_pVBoxDrvSolarisState, getminor(Dev));
|
---|
684 | if (!pState)
|
---|
685 | {
|
---|
686 | LogRel(("VBoxDrvSolarisIOCtl: no state data for %#x (%d)\n", Dev, getminor(Dev)));
|
---|
687 | return EINVAL;
|
---|
688 | }
|
---|
689 |
|
---|
690 | PSUPDRVSESSION pSession = pState->pSession;
|
---|
691 | if (!pSession)
|
---|
692 | {
|
---|
693 | LogRel(("VBoxDrvSolarisIOCtl: no session in state data for %#x (%d)\n", Dev, getminor(Dev)));
|
---|
694 | return DDI_SUCCESS;
|
---|
695 | }
|
---|
696 | #else
|
---|
697 | const RTPROCESS Process = RTProcSelf();
|
---|
698 | const unsigned iHash = SESSION_HASH(Process);
|
---|
699 | PSUPDRVSESSION pSession;
|
---|
700 | const bool fUnrestricted = getminor(Dev) == 0;
|
---|
701 |
|
---|
702 | /*
|
---|
703 | * Find the session.
|
---|
704 | */
|
---|
705 | RTSpinlockAcquire(g_Spinlock);
|
---|
706 | pSession = g_apSessionHashTab[iHash];
|
---|
707 | while (pSession && pSession->Process != Process && pSession->fUnrestricted == fUnrestricted);
|
---|
708 | pSession = pSession->pNextHash;
|
---|
709 | RTSpinlockRelease(g_Spinlock);
|
---|
710 | if (!pSession)
|
---|
711 | {
|
---|
712 | LogRel(("VBoxSupDrvIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x Dev=%#x\n",
|
---|
713 | (int)Process, Cmd, (int)Dev));
|
---|
714 | return EINVAL;
|
---|
715 | }
|
---|
716 | #endif
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
720 | * the session and iCmd, and only returns a VBox status code.
|
---|
721 | */
|
---|
722 | if ( ( Cmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
723 | || Cmd == SUP_IOCTL_FAST_DO_HM_RUN
|
---|
724 | || Cmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
725 | && pSession->fUnrestricted)
|
---|
726 | {
|
---|
727 | *pVal = supdrvIOCtlFast(Cmd, pArgs, &g_DevExt, pSession);
|
---|
728 | return 0;
|
---|
729 | }
|
---|
730 |
|
---|
731 | return VBoxDrvSolarisIOCtlSlow(pSession, Cmd, Mode, pArgs);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | /** @def IOCPARM_LEN
|
---|
736 | * Gets the length from the ioctl number.
|
---|
737 | * This is normally defined by sys/ioccom.h on BSD systems...
|
---|
738 | */
|
---|
739 | #ifndef IOCPARM_LEN
|
---|
740 | # define IOCPARM_LEN(x) ( ((x) >> 16) & IOCPARM_MASK )
|
---|
741 | #endif
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
|
---|
746 | *
|
---|
747 | * @returns Solaris errno.
|
---|
748 | *
|
---|
749 | * @param pSession The session.
|
---|
750 | * @param Cmd The IOCtl command.
|
---|
751 | * @param Mode Information bitfield (for specifying ownership of data)
|
---|
752 | * @param iArg User space address of the request buffer.
|
---|
753 | */
|
---|
754 | static int VBoxDrvSolarisIOCtlSlow(PSUPDRVSESSION pSession, int iCmd, int Mode, intptr_t iArg)
|
---|
755 | {
|
---|
756 | int rc;
|
---|
757 | uint32_t cbBuf = 0;
|
---|
758 | union
|
---|
759 | {
|
---|
760 | SUPREQHDR Hdr;
|
---|
761 | uint8_t abBuf[64];
|
---|
762 | } StackBuf;
|
---|
763 | PSUPREQHDR pHdr;
|
---|
764 |
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * Read the header.
|
---|
768 | */
|
---|
769 | if (RT_UNLIKELY(IOCPARM_LEN(iCmd) != sizeof(StackBuf.Hdr)))
|
---|
770 | {
|
---|
771 | LogRel(("VBoxDrvSolarisIOCtlSlow: iCmd=%#x len %d expected %d\n", iCmd, IOCPARM_LEN(iCmd), sizeof(StackBuf.Hdr)));
|
---|
772 | return EINVAL;
|
---|
773 | }
|
---|
774 | rc = ddi_copyin((void *)iArg, &StackBuf.Hdr, sizeof(StackBuf.Hdr), Mode);
|
---|
775 | if (RT_UNLIKELY(rc))
|
---|
776 | {
|
---|
777 | LogRel(("VBoxDrvSolarisIOCtlSlow: ddi_copyin(,%#lx,) failed; iCmd=%#x. rc=%d\n", iArg, iCmd, rc));
|
---|
778 | return EFAULT;
|
---|
779 | }
|
---|
780 | if (RT_UNLIKELY((StackBuf.Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
781 | {
|
---|
782 | LogRel(("VBoxDrvSolarisIOCtlSlow: bad header magic %#x; iCmd=%#x\n", StackBuf.Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, iCmd));
|
---|
783 | return EINVAL;
|
---|
784 | }
|
---|
785 | cbBuf = RT_MAX(StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut);
|
---|
786 | if (RT_UNLIKELY( StackBuf.Hdr.cbIn < sizeof(StackBuf.Hdr)
|
---|
787 | || StackBuf.Hdr.cbOut < sizeof(StackBuf.Hdr)
|
---|
788 | || cbBuf > _1M*16))
|
---|
789 | {
|
---|
790 | LogRel(("VBoxDrvSolarisIOCtlSlow: max(%#x,%#x); iCmd=%#x\n", StackBuf.Hdr.cbIn, StackBuf.Hdr.cbOut, iCmd));
|
---|
791 | return EINVAL;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /*
|
---|
795 | * Buffer the request.
|
---|
796 | */
|
---|
797 | if (cbBuf <= sizeof(StackBuf))
|
---|
798 | pHdr = &StackBuf.Hdr;
|
---|
799 | else
|
---|
800 | {
|
---|
801 | pHdr = RTMemTmpAlloc(cbBuf);
|
---|
802 | if (RT_UNLIKELY(!pHdr))
|
---|
803 | {
|
---|
804 | LogRel(("VBoxDrvSolarisIOCtlSlow: failed to allocate buffer of %d bytes for iCmd=%#x.\n", cbBuf, iCmd));
|
---|
805 | return ENOMEM;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | rc = ddi_copyin((void *)iArg, pHdr, cbBuf, Mode);
|
---|
809 | if (RT_UNLIKELY(rc))
|
---|
810 | {
|
---|
811 | LogRel(("VBoxDrvSolarisIOCtlSlow: copy_from_user(,%#lx, %#x) failed; iCmd=%#x. rc=%d\n", iArg, cbBuf, iCmd, rc));
|
---|
812 | if (pHdr != &StackBuf.Hdr)
|
---|
813 | RTMemFree(pHdr);
|
---|
814 | return EFAULT;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /*
|
---|
818 | * Process the IOCtl.
|
---|
819 | */
|
---|
820 | rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr, cbBuf);
|
---|
821 |
|
---|
822 | /*
|
---|
823 | * Copy ioctl data and output buffer back to user space.
|
---|
824 | */
|
---|
825 | if (RT_LIKELY(!rc))
|
---|
826 | {
|
---|
827 | uint32_t cbOut = pHdr->cbOut;
|
---|
828 | if (RT_UNLIKELY(cbOut > cbBuf))
|
---|
829 | {
|
---|
830 | LogRel(("VBoxDrvSolarisIOCtlSlow: too much output! %#x > %#x; iCmd=%#x!\n", cbOut, cbBuf, iCmd));
|
---|
831 | cbOut = cbBuf;
|
---|
832 | }
|
---|
833 | rc = ddi_copyout(pHdr, (void *)iArg, cbOut, Mode);
|
---|
834 | if (RT_UNLIKELY(rc != 0))
|
---|
835 | {
|
---|
836 | /* this is really bad */
|
---|
837 | LogRel(("VBoxDrvSolarisIOCtlSlow: ddi_copyout(,%p,%d) failed. rc=%d\n", (void *)iArg, cbBuf, rc));
|
---|
838 | rc = EFAULT;
|
---|
839 | }
|
---|
840 | }
|
---|
841 | else
|
---|
842 | rc = EINVAL;
|
---|
843 |
|
---|
844 | if (pHdr != &StackBuf.Hdr)
|
---|
845 | RTMemTmpFree(pHdr);
|
---|
846 | return rc;
|
---|
847 | }
|
---|
848 |
|
---|
849 |
|
---|
850 | /**
|
---|
851 | * The SUPDRV IDC entry point.
|
---|
852 | *
|
---|
853 | * @returns VBox status code, see supdrvIDC.
|
---|
854 | * @param iReq The request code.
|
---|
855 | * @param pReq The request.
|
---|
856 | */
|
---|
857 | int VBOXCALL SUPDrvSolarisIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
858 | {
|
---|
859 | PSUPDRVSESSION pSession;
|
---|
860 |
|
---|
861 | /*
|
---|
862 | * Some quick validations.
|
---|
863 | */
|
---|
864 | if (RT_UNLIKELY(!VALID_PTR(pReq)))
|
---|
865 | return VERR_INVALID_POINTER;
|
---|
866 |
|
---|
867 | pSession = pReq->pSession;
|
---|
868 | if (pSession)
|
---|
869 | {
|
---|
870 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
871 | return VERR_INVALID_PARAMETER;
|
---|
872 | if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
|
---|
873 | return VERR_INVALID_PARAMETER;
|
---|
874 | }
|
---|
875 | else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
|
---|
876 | return VERR_INVALID_PARAMETER;
|
---|
877 |
|
---|
878 | /*
|
---|
879 | * Do the job.
|
---|
880 | */
|
---|
881 | return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
|
---|
882 | }
|
---|
883 |
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Converts an supdrv error code to a solaris error code.
|
---|
887 | *
|
---|
888 | * @returns corresponding solaris error code.
|
---|
889 | * @param rc IPRT status code.
|
---|
890 | */
|
---|
891 | static int VBoxSupDrvErr2SolarisErr(int rc)
|
---|
892 | {
|
---|
893 | switch (rc)
|
---|
894 | {
|
---|
895 | case VINF_SUCCESS: return 0;
|
---|
896 | case VERR_GENERAL_FAILURE: return EACCES;
|
---|
897 | case VERR_INVALID_PARAMETER: return EINVAL;
|
---|
898 | case VERR_INVALID_MAGIC: return EILSEQ;
|
---|
899 | case VERR_INVALID_HANDLE: return ENXIO;
|
---|
900 | case VERR_INVALID_POINTER: return EFAULT;
|
---|
901 | case VERR_LOCK_FAILED: return ENOLCK;
|
---|
902 | case VERR_ALREADY_LOADED: return EEXIST;
|
---|
903 | case VERR_PERMISSION_DENIED: return EPERM;
|
---|
904 | case VERR_VERSION_MISMATCH: return ENOSYS;
|
---|
905 | }
|
---|
906 |
|
---|
907 | return EPERM;
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
|
---|
912 | {
|
---|
913 | NOREF(pDevExt);
|
---|
914 | NOREF(pSession);
|
---|
915 | }
|
---|
916 |
|
---|
917 |
|
---|
918 | void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
919 | {
|
---|
920 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
921 | }
|
---|
922 |
|
---|
923 |
|
---|
924 | void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
|
---|
925 | {
|
---|
926 | NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
|
---|
927 | }
|
---|
928 |
|
---|
929 |
|
---|
930 | /**
|
---|
931 | * Initializes any OS specific object creator fields.
|
---|
932 | */
|
---|
933 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
934 | {
|
---|
935 | NOREF(pObj);
|
---|
936 | NOREF(pSession);
|
---|
937 | }
|
---|
938 |
|
---|
939 |
|
---|
940 | /**
|
---|
941 | * Checks if the session can access the object.
|
---|
942 | *
|
---|
943 | * @returns true if a decision has been made.
|
---|
944 | * @returns false if the default access policy should be applied.
|
---|
945 | *
|
---|
946 | * @param pObj The object in question.
|
---|
947 | * @param pSession The session wanting to access the object.
|
---|
948 | * @param pszObjName The object name, can be NULL.
|
---|
949 | * @param prc Where to store the result when returning true.
|
---|
950 | */
|
---|
951 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
952 | {
|
---|
953 | NOREF(pObj);
|
---|
954 | NOREF(pSession);
|
---|
955 | NOREF(pszObjName);
|
---|
956 | NOREF(prc);
|
---|
957 | return false;
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
962 | {
|
---|
963 | return false;
|
---|
964 | }
|
---|
965 |
|
---|
966 |
|
---|
967 | bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void)
|
---|
968 | {
|
---|
969 | /** @todo verify this. */
|
---|
970 | return false;
|
---|
971 | }
|
---|
972 |
|
---|
973 |
|
---|
974 | bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
|
---|
975 | {
|
---|
976 | return false;
|
---|
977 | }
|
---|
978 |
|
---|
979 |
|
---|
980 | #if defined(VBOX_WITH_NATIVE_SOLARIS_LOADING) \
|
---|
981 | && !defined(VBOX_WITHOUT_NATIVE_R0_LOADER)
|
---|
982 |
|
---|
983 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
984 | {
|
---|
985 | pImage->idSolMod = -1;
|
---|
986 | pImage->pSolModCtl = NULL;
|
---|
987 |
|
---|
988 | # if 1 /* This approach requires _init/_fini/_info stubs. */
|
---|
989 | /*
|
---|
990 | * Construct a filename that escapes the module search path and let us
|
---|
991 | * specify a root path.
|
---|
992 | */
|
---|
993 | /** @todo change this to use modctl and use_path=0. */
|
---|
994 | const char *pszName = RTPathFilename(pszFilename);
|
---|
995 | AssertReturn(pszName, VERR_INVALID_PARAMETER);
|
---|
996 | char *pszSubDir = RTStrAPrintf2("../../../../../../../../../../..%.*s", pszName - pszFilename - 1, pszFilename);
|
---|
997 | if (!pszSubDir)
|
---|
998 | return VERR_NO_STR_MEMORY;
|
---|
999 | int idMod = modload(pszSubDir, pszName);
|
---|
1000 | if (idMod == -1)
|
---|
1001 | {
|
---|
1002 | /* This is an horrible hack for avoiding the mod-present check in
|
---|
1003 | modrload on S10. Fortunately, nobody else seems to be using that
|
---|
1004 | variable... */
|
---|
1005 | extern int swaploaded;
|
---|
1006 | int saved_swaploaded = swaploaded;
|
---|
1007 | swaploaded = 0;
|
---|
1008 | idMod = modload(pszSubDir, pszName);
|
---|
1009 | swaploaded = saved_swaploaded;
|
---|
1010 | }
|
---|
1011 | RTStrFree(pszSubDir);
|
---|
1012 | if (idMod == -1)
|
---|
1013 | {
|
---|
1014 | LogRel(("modload(,%s): failed, could be anything...\n", pszFilename));
|
---|
1015 | return VERR_LDR_GENERAL_FAILURE;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | modctl_t *pModCtl = mod_hold_by_id(idMod);
|
---|
1019 | if (!pModCtl)
|
---|
1020 | {
|
---|
1021 | LogRel(("mod_hold_by_id(,%s): failed, weird.\n", pszFilename));
|
---|
1022 | /* No point in calling modunload. */
|
---|
1023 | return VERR_LDR_GENERAL_FAILURE;
|
---|
1024 | }
|
---|
1025 | pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD | MOD_NOUNLOAD; /* paranoia */
|
---|
1026 |
|
---|
1027 | # else
|
---|
1028 |
|
---|
1029 | const int idMod = -1;
|
---|
1030 | modctl_t *pModCtl = mod_hold_by_name(pszFilename);
|
---|
1031 | if (!pModCtl)
|
---|
1032 | {
|
---|
1033 | LogRel(("mod_hold_by_name failed for '%s'\n", pszFilename));
|
---|
1034 | return VERR_LDR_GENERAL_FAILURE;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | int rc = kobj_load_module(pModCtl, 0 /*use_path*/);
|
---|
1038 | if (rc != 0)
|
---|
1039 | {
|
---|
1040 | LogRel(("kobj_load_module failed with rc=%d for '%s'\n", rc, pszFilename));
|
---|
1041 | mod_release_mod(pModCtl);
|
---|
1042 | return RTErrConvertFromErrno(rc);
|
---|
1043 | }
|
---|
1044 | # endif
|
---|
1045 |
|
---|
1046 | /*
|
---|
1047 | * Get the module info.
|
---|
1048 | *
|
---|
1049 | * Note! The text section is actually not at mi_base, but and the next
|
---|
1050 | * alignment boundrary and there seems to be no easy way of
|
---|
1051 | * getting at this address. This sabotages supdrvOSLdrLoad.
|
---|
1052 | * Bastards!
|
---|
1053 | */
|
---|
1054 | struct modinfo ModInfo;
|
---|
1055 | kobj_getmodinfo(pModCtl->mod_mp, &ModInfo);
|
---|
1056 | pImage->pvImage = ModInfo.mi_base;
|
---|
1057 | pImage->idSolMod = idMod;
|
---|
1058 | pImage->pSolModCtl = pModCtl;
|
---|
1059 |
|
---|
1060 | mod_release_mod(pImage->pSolModCtl);
|
---|
1061 | LogRel(("supdrvOSLdrOpen: succeeded for '%s' (mi_base=%p mi_size=%#x), id=%d ctl=%p\n",
|
---|
1062 | pszFilename, ModInfo.mi_base, ModInfo.mi_size, idMod, pModCtl));
|
---|
1063 | return VINF_SUCCESS;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 |
|
---|
1067 | void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
1068 | {
|
---|
1069 | NOREF(pDevExt); NOREF(pImage);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
1074 | {
|
---|
1075 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
1076 | if (kobj_addrcheck(pImage->pSolModCtl->mod_mp, pv))
|
---|
1077 | return VERR_INVALID_PARAMETER;
|
---|
1078 | return VINF_SUCCESS;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 |
|
---|
1082 | /**
|
---|
1083 | * Resolves a module entry point address.
|
---|
1084 | *
|
---|
1085 | * @returns VBox status code.
|
---|
1086 | * @param pImage The image.
|
---|
1087 | * @param pszSymbol The symbol name.
|
---|
1088 | * @param ppvValue Where to store the value. On input this holds
|
---|
1089 | * the symbol value SUPLib calculated.
|
---|
1090 | */
|
---|
1091 | static int supdrvSolLdrResolvEp(PSUPDRVLDRIMAGE pImage, const char *pszSymbol, void **ppvValue)
|
---|
1092 | {
|
---|
1093 | /* Don't try resolve symbols which, according to SUPLib, aren't there. */
|
---|
1094 | if (!*ppvValue)
|
---|
1095 | return VINF_SUCCESS;
|
---|
1096 |
|
---|
1097 | uintptr_t uValue = modlookup_by_modctl(pImage->pSolModCtl, pszSymbol);
|
---|
1098 | if (!uValue)
|
---|
1099 | {
|
---|
1100 | LogRel(("supdrvOSLdrLoad on %s failed to resolve %s\n", pImage->szName, pszSymbol));
|
---|
1101 | return VERR_SYMBOL_NOT_FOUND;
|
---|
1102 | }
|
---|
1103 | *ppvValue = (void *)uValue;
|
---|
1104 | return VINF_SUCCESS;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 |
|
---|
1108 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
1109 | {
|
---|
1110 | #if 0 /* This doesn't work because of text alignment. */
|
---|
1111 | /*
|
---|
1112 | * Comparing is very very difficult since text and data may be allocated
|
---|
1113 | * separately.
|
---|
1114 | */
|
---|
1115 | size_t cbCompare = RT_MIN(pImage->cbImageBits, 64);
|
---|
1116 | if (memcmp(pImage->pvImage, pbImageBits, cbCompare))
|
---|
1117 | {
|
---|
1118 | LogRel(("Image mismatch: %s (%p)\n", pImage->szName, pImage->pvImage));
|
---|
1119 | LogRel(("Native: %.*Rhxs\n", cbCompare, pImage->pvImage));
|
---|
1120 | LogRel(("SUPLib: %.*Rhxs\n", cbCompare, pbImageBits));
|
---|
1121 | return VERR_LDR_MISMATCH_NATIVE;
|
---|
1122 | }
|
---|
1123 | #endif
|
---|
1124 |
|
---|
1125 | /*
|
---|
1126 | * Get the exported symbol addresses.
|
---|
1127 | */
|
---|
1128 | int rc;
|
---|
1129 | modctl_t *pModCtl = mod_hold_by_id(pImage->idSolMod);
|
---|
1130 | if (pModCtl && pModCtl == pImage->pSolModCtl)
|
---|
1131 | {
|
---|
1132 | uint32_t iSym = pImage->cSymbols;
|
---|
1133 | while (iSym-- > 0)
|
---|
1134 | {
|
---|
1135 | const char *pszSymbol = &pImage->pachStrTab[pImage->paSymbols[iSym].offName];
|
---|
1136 | uintptr_t uValue = modlookup_by_modctl(pImage->pSolModCtl, pszSymbol);
|
---|
1137 | if (!uValue)
|
---|
1138 | {
|
---|
1139 | LogRel(("supdrvOSLdrLoad on %s failed to resolve the exported symbol: '%s'\n", pImage->szName, pszSymbol));
|
---|
1140 | break;
|
---|
1141 | }
|
---|
1142 | uintptr_t offSymbol = uValue - (uintptr_t)pImage->pvImage;
|
---|
1143 | pImage->paSymbols[iSym].offSymbol = offSymbol;
|
---|
1144 | if (pImage->paSymbols[iSym].offSymbol != (int32_t)offSymbol)
|
---|
1145 | {
|
---|
1146 | LogRel(("supdrvOSLdrLoad on %s symbol out of range: %p (%s) \n", pImage->szName, offSymbol, pszSymbol));
|
---|
1147 | break;
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | rc = iSym == UINT32_MAX ? VINF_SUCCESS : VERR_LDR_GENERAL_FAILURE;
|
---|
1152 |
|
---|
1153 | /*
|
---|
1154 | * Get the standard module entry points.
|
---|
1155 | */
|
---|
1156 | if (RT_SUCCESS(rc))
|
---|
1157 | {
|
---|
1158 | rc = supdrvSolLdrResolvEp(pImage, "ModuleInit", (void **)&pImage->pfnModuleInit);
|
---|
1159 | if (RT_SUCCESS(rc))
|
---|
1160 | rc = supdrvSolLdrResolvEp(pImage, "ModuleTerm", (void **)&pImage->pfnModuleTerm);
|
---|
1161 |
|
---|
1162 | switch (pReq->u.In.eEPType)
|
---|
1163 | {
|
---|
1164 | case SUPLDRLOADEP_VMMR0:
|
---|
1165 | {
|
---|
1166 | if (RT_SUCCESS(rc))
|
---|
1167 | rc = supdrvSolLdrResolvEp(pImage, "VMMR0EntryInt", (void **)&pReq->u.In.EP.VMMR0.pvVMMR0EntryInt);
|
---|
1168 | if (RT_SUCCESS(rc))
|
---|
1169 | rc = supdrvSolLdrResolvEp(pImage, "VMMR0EntryFast", (void **)&pReq->u.In.EP.VMMR0.pvVMMR0EntryFast);
|
---|
1170 | if (RT_SUCCESS(rc))
|
---|
1171 | rc = supdrvSolLdrResolvEp(pImage, "VMMR0EntryEx", (void **)&pReq->u.In.EP.VMMR0.pvVMMR0EntryEx);
|
---|
1172 | break;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | case SUPLDRLOADEP_SERVICE:
|
---|
1176 | {
|
---|
1177 | /** @todo we need the name of the entry point. */
|
---|
1178 | return VERR_NOT_SUPPORTED;
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | mod_release_mod(pImage->pSolModCtl);
|
---|
1184 | }
|
---|
1185 | else
|
---|
1186 | {
|
---|
1187 | LogRel(("mod_hold_by_id failed in supdrvOSLdrLoad on %s: %p\n", pImage->szName, pModCtl));
|
---|
1188 | rc = VERR_LDR_MISMATCH_NATIVE;
|
---|
1189 | }
|
---|
1190 | return rc;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
1195 | {
|
---|
1196 | # if 1
|
---|
1197 | pImage->pSolModCtl->mod_loadflags &= ~MOD_NOUNLOAD;
|
---|
1198 | int rc = modunload(pImage->idSolMod);
|
---|
1199 | if (rc)
|
---|
1200 | LogRel(("modunload(%u (%s)) failed: %d\n", pImage->idSolMod, pImage->szName, rc));
|
---|
1201 | # else
|
---|
1202 | kobj_unload_module(pImage->pSolModCtl);
|
---|
1203 | # endif
|
---|
1204 | pImage->pSolModCtl = NULL;
|
---|
1205 | pImage->idSolMod = NULL;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | #else /* !VBOX_WITH_NATIVE_SOLARIS_LOADING */
|
---|
1209 |
|
---|
1210 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
1211 | {
|
---|
1212 | NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
|
---|
1213 | return VERR_NOT_SUPPORTED;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 |
|
---|
1217 | void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
1218 | {
|
---|
1219 | NOREF(pDevExt); NOREF(pImage);
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 |
|
---|
1223 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
1224 | {
|
---|
1225 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
1226 | return VERR_NOT_SUPPORTED;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 |
|
---|
1230 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
1231 | {
|
---|
1232 | NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
|
---|
1233 | return VERR_NOT_SUPPORTED;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
1238 | {
|
---|
1239 | NOREF(pDevExt); NOREF(pImage);
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | #endif /* !VBOX_WITH_NATIVE_SOLARIS_LOADING */
|
---|
1243 |
|
---|
1244 |
|
---|
1245 | #ifdef SUPDRV_WITH_MSR_PROBER
|
---|
1246 |
|
---|
1247 | int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
|
---|
1248 | {
|
---|
1249 | /** @todo cmi_hdl_rdmsr can safely do this. there is also the on_trap() fun
|
---|
1250 | * for catching traps that could possibly be used directly. */
|
---|
1251 | NOREF(uMsr); NOREF(idCpu); NOREF(puValue);
|
---|
1252 | return VERR_NOT_SUPPORTED;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 |
|
---|
1256 | int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
|
---|
1257 | {
|
---|
1258 | /** @todo cmi_hdl_wrmsr can safely do this. */
|
---|
1259 | NOREF(uMsr); NOREF(idCpu); NOREF(uValue);
|
---|
1260 | return VERR_NOT_SUPPORTED;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 |
|
---|
1264 | int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
|
---|
1265 | {
|
---|
1266 | NOREF(idCpu); NOREF(pReq);
|
---|
1267 | return VERR_NOT_SUPPORTED;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | #endif /* SUPDRV_WITH_MSR_PROBER */
|
---|
1271 |
|
---|
1272 |
|
---|
1273 | RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
1274 | {
|
---|
1275 | va_list args;
|
---|
1276 | char szMsg[512];
|
---|
1277 |
|
---|
1278 | /* cmn_err() acquires adaptive mutexes. Not preemption safe, see @bugref{6657}. */
|
---|
1279 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
1280 | return 0;
|
---|
1281 |
|
---|
1282 | va_start(args, pszFormat);
|
---|
1283 | RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, args);
|
---|
1284 | va_end(args);
|
---|
1285 |
|
---|
1286 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
1287 | cmn_err(CE_CONT, "%s", szMsg);
|
---|
1288 | return 0;
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 |
|
---|
1292 | /**
|
---|
1293 | * Returns configuration flags of the host kernel.
|
---|
1294 | */
|
---|
1295 | SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
|
---|
1296 | {
|
---|
1297 | return 0;
|
---|
1298 | }
|
---|
1299 |
|
---|