VirtualBox

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

Last change on this file since 107044 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: SUPLib-darwin.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Darwin specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.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 RT_GCC_NO_WARN_DEPRECATED_BEGIN
148 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort); /* Deprecated since 12.0. */
149 RT_GCC_NO_WARN_DEPRECATED_END
150 if (kr != kIOReturnSuccess)
151 {
152 LogRel(("IOMasterPort -> %d\n", kr));
153 return VERR_GENERAL_FAILURE;
154 }
155
156 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
157 if (!ClassToMatch)
158 {
159 LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
160 return VERR_GENERAL_FAILURE;
161 }
162
163 /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
164 io_iterator_t Iterator;
165 kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
166 if (kr != kIOReturnSuccess)
167 {
168 LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
169 return VERR_GENERAL_FAILURE;
170 }
171
172 /* Get the first item in the iterator and release it. */
173 io_service_t ServiceObject = IOIteratorNext(Iterator);
174 IOObjectRelease(Iterator);
175 if (!ServiceObject)
176 {
177 LogRel(("SUP: Couldn't find any matches. The kernel module is probably not loaded.\n"));
178 return VERR_VM_DRIVER_NOT_INSTALLED;
179 }
180
181 /*
182 * Open the service.
183 *
184 * This will cause the user client class in SUPDrv-darwin.cpp to be
185 * instantiated and create a session for this process.
186 */
187 io_connect_t Connection = 0;
188 kr = IOServiceOpen(ServiceObject, mach_task_self(), SUP_DARWIN_IOSERVICE_COOKIE, &Connection);
189 IOObjectRelease(ServiceObject);
190 if (kr != kIOReturnSuccess)
191 {
192 LogRel(("SUP: IOServiceOpen returned %d. Driver open failed.\n", kr));
193 pThis->uConnection = 0;
194 return VERR_VM_DRIVER_OPEN_ERROR;
195 }
196
197 AssertCompile(sizeof(pThis->uConnection) >= sizeof(Connection));
198 pThis->uConnection = Connection;
199 return VINF_SUCCESS;
200}
201
202
203DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
204{
205 RT_NOREF(penmWhat, pErrInfo);
206
207 /*
208 * Nothing to do if pre-inited.
209 */
210 if (fPreInited)
211 return VINF_SUCCESS;
212
213 /*
214 * Driverless?
215 */
216 if (fFlags & SUPR3INIT_F_DRIVERLESS)
217 {
218 pThis->fDriverless = true;
219 return VINF_SUCCESS;
220 }
221
222 /*
223 * Do the job.
224 */
225 Assert(pThis->hDevice == (intptr_t)NIL_RTFILE);
226 int rc = suplibDarwinOpenService(pThis);
227 if (RT_SUCCESS(rc))
228 {
229 rc = suplibDarwinOpenDevice(pThis, RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED));
230 if (RT_FAILURE(rc))
231 {
232 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
233 if (kr != kIOReturnSuccess)
234 {
235 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
236 AssertFailed();
237 }
238 pThis->uConnection = 0;
239 }
240 }
241 if ( RT_FAILURE(rc)
242 && fFlags & SUPR3INIT_F_DRIVERLESS_MASK)
243 {
244 LogRel(("Failed to open \"%s\", rc=%Rrc - Switching to driverless mode.\n", IOCLASS_NAME, rc));
245 pThis->fDriverless = true;
246 rc = VINF_SUCCESS;
247 }
248
249 return rc;
250}
251
252
253DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
254{
255 /*
256 * Close the connection to the IOService.
257 * This will cause the SUPDRVSESSION to be closed (starting IOC 9.1).
258 */
259 if (pThis->uConnection)
260 {
261 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
262 if (kr != kIOReturnSuccess)
263 {
264 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
265 AssertFailed();
266 }
267 pThis->uConnection = 0;
268 }
269
270 /*
271 * Check if we're inited at all.
272 */
273 if (pThis->hDevice != (intptr_t)NIL_RTFILE)
274 {
275 if (close(pThis->hDevice))
276 AssertFailed();
277 pThis->hDevice = (intptr_t)NIL_RTFILE;
278 }
279
280 return VINF_SUCCESS;
281}
282
283
284#ifndef IN_SUP_HARDENED_R3
285
286DECLHIDDEN(int) suplibOsInstall(void)
287{
288 return VERR_NOT_IMPLEMENTED;
289}
290
291
292DECLHIDDEN(int) suplibOsUninstall(void)
293{
294 return VERR_NOT_IMPLEMENTED;
295}
296
297
298DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
299{
300 RT_NOREF(cbReq);
301 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
302 return VINF_SUCCESS;
303 return RTErrConvertFromErrno(errno);
304}
305
306
307DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
308{
309 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
310 if (rc == -1)
311 rc = errno;
312 return rc;
313}
314
315
316DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
317{
318 RT_NOREF(pThis, fFlags);
319 *ppvPages = valloc(cPages << PAGE_SHIFT);
320 if (*ppvPages)
321 {
322 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
323 return VINF_SUCCESS;
324 }
325 return RTErrConvertFromErrno(errno);
326}
327
328
329DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
330{
331 NOREF(pThis);
332 free(pvPages);
333 return VINF_SUCCESS;
334}
335
336
337DECLHIDDEN(bool) suplibOsIsNemSupportedWhenNoVtxOrAmdV(void)
338{
339# if ARCH_BITS == 64
340 int fHvSupported = 0;
341 size_t cb = sizeof(fHvSupported);
342 int rc = sysctlbyname("kern.hv.supported", &fHvSupported, &cb, NULL, 0);
343 if ( !rc
344 && cb == sizeof(uint32_t))
345 return fHvSupported == 1;
346
347 return false;
348# else
349 return false;
350#endif
351}
352
353#endif /* !IN_SUP_HARDENED_R3 */
354
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