VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/linux/SUPLib-linux.cpp@ 64584

Last change on this file since 64584 was 62877, checked in by vboxsync, 9 years ago

HostDrivers: gcc warnings.

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

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