1 | /* $Id: SUPLib-solaris.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - Solaris specific parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | * 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
|
---|
31 | #ifdef IN_SUP_HARDENED_R3
|
---|
32 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
33 | # define LOG_DISABLED
|
---|
34 | /** @todo RTLOGREL_DISABLED */
|
---|
35 | # include <iprt/log.h>
|
---|
36 | # undef LogRelIt
|
---|
37 | # define LogRelIt(pvInst, fFlags, iGroup, fmtargs) do { } while (0)
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include <VBox/types.h>
|
---|
41 | #include <VBox/sup.h>
|
---|
42 | #include <VBox/param.h>
|
---|
43 | #include <VBox/err.h>
|
---|
44 | #include <VBox/log.h>
|
---|
45 | #include <iprt/path.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/err.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include "../SUPLibInternal.h"
|
---|
51 | #include "../SUPDrvIOC.h"
|
---|
52 |
|
---|
53 | #include <sys/fcntl.h>
|
---|
54 | #include <sys/ioctl.h>
|
---|
55 |
|
---|
56 | #include <fcntl.h>
|
---|
57 | #include <errno.h>
|
---|
58 | #include <unistd.h>
|
---|
59 | #include <sys/mman.h>
|
---|
60 | #include <stdlib.h>
|
---|
61 | #include <stdio.h>
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Defined Constants And Macros *
|
---|
66 | *******************************************************************************/
|
---|
67 | /** Solaris device link. */
|
---|
68 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited)
|
---|
73 | {
|
---|
74 | /*
|
---|
75 | * Nothing to do if pre-inited.
|
---|
76 | */
|
---|
77 | if (fPreInited)
|
---|
78 | return VINF_SUCCESS;
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Open dummy files to preallocate file descriptors, see #4650.
|
---|
82 | */
|
---|
83 | for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
|
---|
84 | {
|
---|
85 | pThis->ahDummy[i] = -1;
|
---|
86 | int hDummy = open("/dev/null", O_RDWR, 0);
|
---|
87 | if (hDummy >= 0)
|
---|
88 | {
|
---|
89 | if (fcntl(hDummy, F_SETFD, FD_CLOEXEC) == 0)
|
---|
90 | pThis->ahDummy[i] = hDummy;
|
---|
91 | else
|
---|
92 | {
|
---|
93 | close(hDummy);
|
---|
94 | LogRel(("Failed to set close on exec [%d] /dev/null! errno=%d\n", i, errno));
|
---|
95 | }
|
---|
96 | }
|
---|
97 | else
|
---|
98 | LogRel(("Failed to open[%d] /dev/null! errno=%d\n", i, errno));
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Try to open the device.
|
---|
103 | */
|
---|
104 | int hDevice = open(DEVICE_NAME, O_RDWR, 0);
|
---|
105 | if (hDevice < 0)
|
---|
106 | {
|
---|
107 | int rc;
|
---|
108 | switch (errno)
|
---|
109 | {
|
---|
110 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
111 | case EPERM:
|
---|
112 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
113 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
114 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
115 | }
|
---|
116 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", DEVICE_NAME, errno, rc));
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Mark the file handle close on exec.
|
---|
122 | */
|
---|
123 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
124 | {
|
---|
125 | #ifdef IN_SUP_HARDENED_R3
|
---|
126 | int rc = VERR_INTERNAL_ERROR;
|
---|
127 | #else
|
---|
128 | int err = errno;
|
---|
129 | int rc = RTErrConvertFromErrno(err);
|
---|
130 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
|
---|
131 | #endif
|
---|
132 | close(hDevice);
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | pThis->hDevice = hDevice;
|
---|
137 | return VINF_SUCCESS;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | #ifndef IN_SUP_HARDENED_R3
|
---|
142 |
|
---|
143 | int suplibOsTerm(PSUPLIBDATA pThis)
|
---|
144 | {
|
---|
145 | /*
|
---|
146 | * Close the dummy files first.
|
---|
147 | */
|
---|
148 | for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
|
---|
149 | {
|
---|
150 | if (pThis->ahDummy[i] != -1)
|
---|
151 | {
|
---|
152 | close(pThis->ahDummy[i]);
|
---|
153 | pThis->ahDummy[i] = -1;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Check if we're initialized
|
---|
159 | */
|
---|
160 | if (pThis->hDevice != NIL_RTFILE)
|
---|
161 | {
|
---|
162 | if (close(pThis->hDevice))
|
---|
163 | AssertFailed();
|
---|
164 | pThis->hDevice = NIL_RTFILE;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return VINF_SUCCESS;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | int suplibOsInstall(void)
|
---|
172 | {
|
---|
173 | return VERR_NOT_IMPLEMENTED;
|
---|
174 | }
|
---|
175 |
|
---|
176 | int suplibOsUninstall(void)
|
---|
177 | {
|
---|
178 | return VERR_NOT_IMPLEMENTED;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
183 | {
|
---|
184 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | return RTErrConvertFromErrno(errno);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
191 | {
|
---|
192 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
193 | if (rc == -1)
|
---|
194 | rc = errno;
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
|
---|
200 | {
|
---|
201 | NOREF(pThis);
|
---|
202 | *ppvPages = mmap(NULL, cPages * PAGE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
|
---|
203 | MAP_PRIVATE | MAP_ANON, -1, 0);
|
---|
204 | if (*ppvPages != (void *)-1)
|
---|
205 | return VINF_SUCCESS;
|
---|
206 | if (errno == EAGAIN)
|
---|
207 | return VERR_NO_MEMORY;
|
---|
208 | return RTErrConvertFromErrno(errno);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
|
---|
213 | {
|
---|
214 | NOREF(pThis);
|
---|
215 | munmap(pvPages, cPages * PAGE_SIZE);
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
220 |
|
---|