VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/darwin/SUPLib-darwin.cpp@ 100097

最後變更 在這個檔案從100097是 100097,由 vboxsync 提交於 22 月 前

HostDrivers/Support/darwin: Implement suplibOsIsNemSupportedWhenNoVtxOrAmdV() and query kern.hv.supported in order to determine whether NEM is available. Allows to mark hardware virtualization as supported so the GUI allows setting the CPU count for a VM , bugref:10454

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.0 KB
 
1/* $Id: SUPLib-darwin.cpp 100097 2023-06-07 17:45:33Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Darwin specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP LOG_GROUP_SUP
42#ifdef IN_SUP_HARDENED_R3
43# undef DEBUG /* Warning: disables RT_STRICT */
44# ifndef LOG_DISABLED
45# define LOG_DISABLED
46# endif
47# define RTLOG_REL_DISABLED
48# include <iprt/log.h>
49#endif
50
51#include <VBox/types.h>
52#include <VBox/sup.h>
53#include <VBox/param.h>
54#include <VBox/err.h>
55#include <VBox/log.h>
56#include <iprt/path.h>
57#include <iprt/assert.h>
58#include <iprt/err.h>
59#include <iprt/string.h>
60#include "../SUPLibInternal.h"
61#include "../SUPDrvIOC.h"
62
63#include <sys/fcntl.h>
64#include <sys/ioctl.h>
65#include <sys/types.h>
66#include <sys/sysctl.h>
67#include <errno.h>
68#include <unistd.h>
69#include <stdlib.h>
70#include <mach/mach_port.h>
71#include <IOKit/IOKitLib.h>
72
73
74/*********************************************************************************************************************************
75* Defined Constants And Macros *
76*********************************************************************************************************************************/
77/** System device name. */
78#define DEVICE_NAME_SYS "/dev/vboxdrv"
79/** User device name. */
80#define DEVICE_NAME_USR "/dev/vboxdrvu"
81/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
82#define IOCLASS_NAME "org_virtualbox_SupDrv"
83
84
85
86/**
87 * Opens the BSD device node.
88 *
89 * @returns VBox status code.
90 */
91static int suplibDarwinOpenDevice(PSUPLIBDATA pThis, bool fUnrestricted)
92{
93 /*
94 * Open the BSD device.
95 * This will connect to the session created when the SupDrvClient was
96 * started, so it has to be done after opening the service (IOC v9.1+).
97 */
98 int hDevice = open(fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, O_RDWR, 0);
99 if (hDevice < 0)
100 {
101 int rc;
102 switch (errno)
103 {
104 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
105 case EPERM:
106 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
107 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
108 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
109 }
110 LogRel(("SUP: Failed to open \"%s\", errno=%d, rc=%Rrc\n", fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, errno, rc));
111 return rc;
112 }
113
114 /*
115 * Mark the file handle close on exec.
116 */
117 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
118 {
119#ifdef IN_SUP_HARDENED_R3
120 int rc = VERR_INTERNAL_ERROR;
121#else
122 int err = errno;
123 int rc = RTErrConvertFromErrno(err);
124 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
125#endif
126 close(hDevice);
127 return rc;
128 }
129
130 pThis->hDevice = hDevice;
131 pThis->fUnrestricted = fUnrestricted;
132 return VINF_SUCCESS;
133}
134
135
136/**
137 * Opens the IOKit service, instantiating org_virtualbox_SupDrvClient.
138 *
139 * @returns VBox status code.
140 */
141static int suplibDarwinOpenService(PSUPLIBDATA pThis)
142{
143 /*
144 * Open the IOKit client first - The first step is finding the service.
145 */
146 mach_port_t MasterPort;
147 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
148 if (kr != kIOReturnSuccess)
149 {
150 LogRel(("IOMasterPort -> %d\n", kr));
151 return VERR_GENERAL_FAILURE;
152 }
153
154 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
155 if (!ClassToMatch)
156 {
157 LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
158 return VERR_GENERAL_FAILURE;
159 }
160
161 /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
162 io_iterator_t Iterator;
163 kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
164 if (kr != kIOReturnSuccess)
165 {
166 LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
167 return VERR_GENERAL_FAILURE;
168 }
169
170 /* Get the first item in the iterator and release it. */
171 io_service_t ServiceObject = IOIteratorNext(Iterator);
172 IOObjectRelease(Iterator);
173 if (!ServiceObject)
174 {
175 LogRel(("SUP: Couldn't find any matches. The kernel module is probably not loaded.\n"));
176 return VERR_VM_DRIVER_NOT_INSTALLED;
177 }
178
179 /*
180 * Open the service.
181 *
182 * This will cause the user client class in SUPDrv-darwin.cpp to be
183 * instantiated and create a session for this process.
184 */
185 io_connect_t Connection = 0;
186 kr = IOServiceOpen(ServiceObject, mach_task_self(), SUP_DARWIN_IOSERVICE_COOKIE, &Connection);
187 IOObjectRelease(ServiceObject);
188 if (kr != kIOReturnSuccess)
189 {
190 LogRel(("SUP: IOServiceOpen returned %d. Driver open failed.\n", kr));
191 pThis->uConnection = 0;
192 return VERR_VM_DRIVER_OPEN_ERROR;
193 }
194
195 AssertCompile(sizeof(pThis->uConnection) >= sizeof(Connection));
196 pThis->uConnection = Connection;
197 return VINF_SUCCESS;
198}
199
200
201DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
202{
203 RT_NOREF(penmWhat, pErrInfo);
204
205 /*
206 * Nothing to do if pre-inited.
207 */
208 if (fPreInited)
209 return VINF_SUCCESS;
210
211 /*
212 * Driverless?
213 */
214 if (fFlags & SUPR3INIT_F_DRIVERLESS)
215 {
216 pThis->fDriverless = true;
217 return VINF_SUCCESS;
218 }
219
220 /*
221 * Do the job.
222 */
223 Assert(pThis->hDevice == (intptr_t)NIL_RTFILE);
224 int rc = suplibDarwinOpenService(pThis);
225 if (RT_SUCCESS(rc))
226 {
227 rc = suplibDarwinOpenDevice(pThis, RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED));
228 if (RT_FAILURE(rc))
229 {
230 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
231 if (kr != kIOReturnSuccess)
232 {
233 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
234 AssertFailed();
235 }
236 pThis->uConnection = 0;
237 }
238 }
239 if ( RT_FAILURE(rc)
240 && fFlags & SUPR3INIT_F_DRIVERLESS_MASK)
241 {
242 LogRel(("Failed to open \"%s\", rc=%Rrc - Switching to driverless mode.\n", IOCLASS_NAME, rc));
243 pThis->fDriverless = true;
244 rc = VINF_SUCCESS;
245 }
246
247 return rc;
248}
249
250
251DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
252{
253 /*
254 * Close the connection to the IOService.
255 * This will cause the SUPDRVSESSION to be closed (starting IOC 9.1).
256 */
257 if (pThis->uConnection)
258 {
259 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
260 if (kr != kIOReturnSuccess)
261 {
262 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
263 AssertFailed();
264 }
265 pThis->uConnection = 0;
266 }
267
268 /*
269 * Check if we're inited at all.
270 */
271 if (pThis->hDevice != (intptr_t)NIL_RTFILE)
272 {
273 if (close(pThis->hDevice))
274 AssertFailed();
275 pThis->hDevice = (intptr_t)NIL_RTFILE;
276 }
277
278 return VINF_SUCCESS;
279}
280
281
282#ifndef IN_SUP_HARDENED_R3
283
284DECLHIDDEN(int) suplibOsInstall(void)
285{
286 return VERR_NOT_IMPLEMENTED;
287}
288
289
290DECLHIDDEN(int) suplibOsUninstall(void)
291{
292 return VERR_NOT_IMPLEMENTED;
293}
294
295
296DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
297{
298 RT_NOREF(cbReq);
299 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
300 return VINF_SUCCESS;
301 return RTErrConvertFromErrno(errno);
302}
303
304
305DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
306{
307 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
308 if (rc == -1)
309 rc = errno;
310 return rc;
311}
312
313
314DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
315{
316 RT_NOREF(pThis, fFlags);
317 *ppvPages = valloc(cPages << PAGE_SHIFT);
318 if (*ppvPages)
319 {
320 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
321 return VINF_SUCCESS;
322 }
323 return RTErrConvertFromErrno(errno);
324}
325
326
327DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
328{
329 NOREF(pThis);
330 free(pvPages);
331 return VINF_SUCCESS;
332}
333
334
335DECLHIDDEN(bool) suplibOsIsNemSupportedWhenNoVtxOrAmdV(void)
336{
337# if ARCH_BITS == 64
338 int fHvSupported = 0;
339 size_t cb = sizeof(fHvSupported);
340 int rc = sysctlbyname("kern.hv.supported", &fHvSupported, &cb, NULL, 0);
341 if ( !rc
342 && cb == sizeof(uint32_t))
343 return fHvSupported == 1;
344
345 return false;
346# else
347 return false;
348#endif
349}
350
351#endif /* !IN_SUP_HARDENED_R3 */
352
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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