1 | /* $Id: SUPLib-linux.cpp 22632 2009-09-01 07:57:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - GNU/Linux specific parts.
|
---|
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.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 | * 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
|
---|
35 | #ifdef IN_SUP_HARDENED_R3
|
---|
36 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
37 | # define LOG_DISABLED
|
---|
38 | /** @todo RTLOGREL_DISABLED */
|
---|
39 | # include <iprt/log.h>
|
---|
40 | # undef LogRelIt
|
---|
41 | # define LogRelIt(pvInst, fFlags, iGroup, fmtargs) do { } while (0)
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <sys/fcntl.h>
|
---|
45 | #include <sys/ioctl.h>
|
---|
46 | #include <sys/mman.h>
|
---|
47 | #include <errno.h>
|
---|
48 | #include <unistd.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 | #include <malloc.h>
|
---|
51 |
|
---|
52 | #include <VBox/log.h>
|
---|
53 | #include <VBox/sup.h>
|
---|
54 | #include <iprt/path.h>
|
---|
55 | #include <iprt/assert.h>
|
---|
56 | #include <VBox/types.h>
|
---|
57 | #include <iprt/string.h>
|
---|
58 | #include <iprt/system.h>
|
---|
59 | #include <VBox/err.h>
|
---|
60 | #include <VBox/param.h>
|
---|
61 | #include "../SUPLibInternal.h"
|
---|
62 | #include "../SUPDrvIOC.h"
|
---|
63 |
|
---|
64 |
|
---|
65 | /*******************************************************************************
|
---|
66 | * Defined Constants And Macros *
|
---|
67 | *******************************************************************************/
|
---|
68 | /** Unix Device name. */
|
---|
69 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
70 |
|
---|
71 | /* define MADV_DONTFORK if it's missing from the system headers. */
|
---|
72 | #ifndef MADV_DONTFORK
|
---|
73 | # define MADV_DONTFORK 10
|
---|
74 | #endif
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 | int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited)
|
---|
79 | {
|
---|
80 | /*
|
---|
81 | * Nothing to do if pre-inited.
|
---|
82 | */
|
---|
83 | if (fPreInited)
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | Assert(pThis->hDevice == NIL_RTFILE);
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Check if madvise works.
|
---|
89 | */
|
---|
90 | void *pv = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
91 | if (pv == MAP_FAILED)
|
---|
92 | return VERR_NO_MEMORY;
|
---|
93 | pThis->fSysMadviseWorks = (0 == madvise(pv, PAGE_SIZE, MADV_DONTFORK));
|
---|
94 | munmap(pv, PAGE_SIZE);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Try open the device.
|
---|
98 | */
|
---|
99 | int hDevice = open(DEVICE_NAME, O_RDWR, 0);
|
---|
100 | if (hDevice < 0)
|
---|
101 | {
|
---|
102 | /*
|
---|
103 | * Try load the device.
|
---|
104 | */
|
---|
105 | hDevice = open(DEVICE_NAME, O_RDWR, 0);
|
---|
106 | if (hDevice < 0)
|
---|
107 | {
|
---|
108 | int rc;
|
---|
109 | switch (errno)
|
---|
110 | {
|
---|
111 | case ENXIO: /* see man 2 open, ENODEV is actually a kernel bug */
|
---|
112 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
113 | case EPERM:
|
---|
114 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
115 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
116 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
117 | }
|
---|
118 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", DEVICE_NAME, errno, rc));
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Mark the file handle close on exec.
|
---|
125 | */
|
---|
126 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) == -1)
|
---|
127 | {
|
---|
128 | close(hDevice);
|
---|
129 | #ifdef IN_SUP_HARDENED_R3
|
---|
130 | return VERR_INTERNAL_ERROR;
|
---|
131 | #else
|
---|
132 | return RTErrConvertFromErrno(errno);
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * We're done.
|
---|
138 | */
|
---|
139 | pThis->hDevice = hDevice;
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | #ifndef IN_SUP_HARDENED_R3
|
---|
145 |
|
---|
146 | int suplibOsTerm(PSUPLIBDATA pThis)
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | * Close the device if it's actually open.
|
---|
150 | */
|
---|
151 | if (pThis->hDevice != NIL_RTFILE)
|
---|
152 | {
|
---|
153 | if (close(pThis->hDevice))
|
---|
154 | AssertFailed();
|
---|
155 | pThis->hDevice = NIL_RTFILE;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | int suplibOsInstall(void)
|
---|
163 | {
|
---|
164 | // nothing to do on Linux
|
---|
165 | return VERR_NOT_IMPLEMENTED;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | int suplibOsUninstall(void)
|
---|
170 | {
|
---|
171 | // nothing to do on Linux
|
---|
172 | return VERR_NOT_IMPLEMENTED;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
177 | {
|
---|
178 | AssertMsg(pThis->hDevice != NIL_RTFILE, ("SUPLIB not initiated successfully!\n"));
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Issue device iocontrol.
|
---|
182 | */
|
---|
183 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
184 | return VINF_SUCCESS;
|
---|
185 |
|
---|
186 | /* This is the reverse operation of the one found in SUPDrv-linux.c */
|
---|
187 | switch (errno)
|
---|
188 | {
|
---|
189 | case EACCES: return VERR_GENERAL_FAILURE;
|
---|
190 | case EINVAL: return VERR_INVALID_PARAMETER;
|
---|
191 | case EILSEQ: return VERR_INVALID_MAGIC;
|
---|
192 | case ENXIO: return VERR_INVALID_HANDLE;
|
---|
193 | case EFAULT: return VERR_INVALID_POINTER;
|
---|
194 | case ENOLCK: return VERR_LOCK_FAILED;
|
---|
195 | case EEXIST: return VERR_ALREADY_LOADED;
|
---|
196 | case EPERM: return VERR_PERMISSION_DENIED;
|
---|
197 | case ENOSYS: return VERR_VERSION_MISMATCH;
|
---|
198 | case 1000: return VERR_IDT_FAILED;
|
---|
199 | }
|
---|
200 |
|
---|
201 | return RTErrConvertFromErrno(errno);
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
206 | {
|
---|
207 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
208 | if (rc == -1)
|
---|
209 | rc = -errno;
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
|
---|
215 | {
|
---|
216 | size_t cbMmap = (pThis->fSysMadviseWorks ? cPages : cPages + 2) << PAGE_SHIFT;
|
---|
217 | char *pvPages = (char *)mmap(NULL, cbMmap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
218 | if (pvPages == MAP_FAILED)
|
---|
219 | return VERR_NO_MEMORY;
|
---|
220 |
|
---|
221 | if (pThis->fSysMadviseWorks)
|
---|
222 | {
|
---|
223 | /*
|
---|
224 | * It is not fatal if we fail here but a forked child (e.g. the ALSA sound server)
|
---|
225 | * could crash. Linux < 2.6.16 does not implement madvise(MADV_DONTFORK) but the
|
---|
226 | * kernel seems to split bigger VMAs and that is all that we want -- later we set the
|
---|
227 | * VM_DONTCOPY attribute in supdrvOSLockMemOne().
|
---|
228 | */
|
---|
229 | if (madvise (pvPages, cbMmap, MADV_DONTFORK))
|
---|
230 | LogRel(("SUPLib: madvise %p-%p failed\n", pvPages, cbMmap));
|
---|
231 | *ppvPages = pvPages;
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | /*
|
---|
236 | * madvise(MADV_DONTFORK) is not available (most probably Linux 2.4). Enclose any
|
---|
237 | * mmapped region by two unmapped pages to guarantee that there is exactly one VM
|
---|
238 | * area struct of the very same size as the mmap area.
|
---|
239 | */
|
---|
240 | mprotect(pvPages, PAGE_SIZE, PROT_NONE);
|
---|
241 | mprotect(pvPages + cbMmap - PAGE_SIZE, PAGE_SIZE, PROT_NONE);
|
---|
242 | *ppvPages = pvPages + PAGE_SIZE;
|
---|
243 | }
|
---|
244 | memset(*ppvPages, 0, cPages << PAGE_SHIFT);
|
---|
245 | return VINF_SUCCESS;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
|
---|
250 | {
|
---|
251 | munmap(pvPages, cPages << PAGE_SHIFT);
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /** Check if the host kernel supports VT-x or not.
|
---|
257 | *
|
---|
258 | * Older Linux kernels clear the VMXE bit in the CR4 register (function
|
---|
259 | * tlb_flush_all()) leading to a host kernel panic.
|
---|
260 | */
|
---|
261 | int suplibOsQueryVTxSupported(void)
|
---|
262 | {
|
---|
263 | char szBuf[256];
|
---|
264 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szBuf, sizeof(szBuf));
|
---|
265 |
|
---|
266 | if (RT_SUCCESS(rc))
|
---|
267 | {
|
---|
268 | char *pszNext;
|
---|
269 | uint32_t uA, uB, uC;
|
---|
270 |
|
---|
271 | rc = RTStrToUInt32Ex(szBuf, &pszNext, 10, &uA);
|
---|
272 | if ( RT_SUCCESS(rc)
|
---|
273 | && *pszNext == '.')
|
---|
274 | {
|
---|
275 | rc = RTStrToUInt32Ex(pszNext+1, &pszNext, 10, &uB);
|
---|
276 | if ( RT_SUCCESS(rc)
|
---|
277 | && *pszNext == '.')
|
---|
278 | {
|
---|
279 | rc = RTStrToUInt32Ex(pszNext+1, &pszNext, 10, &uC);
|
---|
280 | if (RT_SUCCESS(rc))
|
---|
281 | {
|
---|
282 | uint32_t uLinuxVersion = (uA << 16) + (uB << 8) + uC;
|
---|
283 | if (uLinuxVersion >= (2 << 16) + (6 << 8) + 13)
|
---|
284 | return VINF_SUCCESS;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | return VERR_SUPDRV_KERNEL_TOO_OLD_FOR_VTX;
|
---|
291 | }
|
---|
292 |
|
---|
293 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
294 |
|
---|