VirtualBox

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

Last change on this file since 99901 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1/* $Id: SUPLib-darwin.cpp 98103 2023-01-17 14:15:46Z 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.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 <errno.h>
66#include <unistd.h>
67#include <stdlib.h>
68#include <mach/mach_port.h>
69#include <IOKit/IOKitLib.h>
70
71
72/*********************************************************************************************************************************
73* Defined Constants And Macros *
74*********************************************************************************************************************************/
75/** System device name. */
76#define DEVICE_NAME_SYS "/dev/vboxdrv"
77/** User device name. */
78#define DEVICE_NAME_USR "/dev/vboxdrvu"
79/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
80#define IOCLASS_NAME "org_virtualbox_SupDrv"
81
82
83
84/**
85 * Opens the BSD device node.
86 *
87 * @returns VBox status code.
88 */
89static int suplibDarwinOpenDevice(PSUPLIBDATA pThis, bool fUnrestricted)
90{
91 /*
92 * Open the BSD device.
93 * This will connect to the session created when the SupDrvClient was
94 * started, so it has to be done after opening the service (IOC v9.1+).
95 */
96 int hDevice = open(fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, O_RDWR, 0);
97 if (hDevice < 0)
98 {
99 int rc;
100 switch (errno)
101 {
102 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
103 case EPERM:
104 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
105 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
106 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
107 }
108 LogRel(("SUP: Failed to open \"%s\", errno=%d, rc=%Rrc\n", fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, errno, rc));
109 return rc;
110 }
111
112 /*
113 * Mark the file handle close on exec.
114 */
115 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
116 {
117#ifdef IN_SUP_HARDENED_R3
118 int rc = VERR_INTERNAL_ERROR;
119#else
120 int err = errno;
121 int rc = RTErrConvertFromErrno(err);
122 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
123#endif
124 close(hDevice);
125 return rc;
126 }
127
128 pThis->hDevice = hDevice;
129 pThis->fUnrestricted = fUnrestricted;
130 return VINF_SUCCESS;
131}
132
133
134/**
135 * Opens the IOKit service, instantiating org_virtualbox_SupDrvClient.
136 *
137 * @returns VBox status code.
138 */
139static int suplibDarwinOpenService(PSUPLIBDATA pThis)
140{
141 /*
142 * Open the IOKit client first - The first step is finding the service.
143 */
144 mach_port_t MasterPort;
145 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
146 if (kr != kIOReturnSuccess)
147 {
148 LogRel(("IOMasterPort -> %d\n", kr));
149 return VERR_GENERAL_FAILURE;
150 }
151
152 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
153 if (!ClassToMatch)
154 {
155 LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
156 return VERR_GENERAL_FAILURE;
157 }
158
159 /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
160 io_iterator_t Iterator;
161 kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
162 if (kr != kIOReturnSuccess)
163 {
164 LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
165 return VERR_GENERAL_FAILURE;
166 }
167
168 /* Get the first item in the iterator and release it. */
169 io_service_t ServiceObject = IOIteratorNext(Iterator);
170 IOObjectRelease(Iterator);
171 if (!ServiceObject)
172 {
173 LogRel(("SUP: Couldn't find any matches. The kernel module is probably not loaded.\n"));
174 return VERR_VM_DRIVER_NOT_INSTALLED;
175 }
176
177 /*
178 * Open the service.
179 *
180 * This will cause the user client class in SUPDrv-darwin.cpp to be
181 * instantiated and create a session for this process.
182 */
183 io_connect_t Connection = 0;
184 kr = IOServiceOpen(ServiceObject, mach_task_self(), SUP_DARWIN_IOSERVICE_COOKIE, &Connection);
185 IOObjectRelease(ServiceObject);
186 if (kr != kIOReturnSuccess)
187 {
188 LogRel(("SUP: IOServiceOpen returned %d. Driver open failed.\n", kr));
189 pThis->uConnection = 0;
190 return VERR_VM_DRIVER_OPEN_ERROR;
191 }
192
193 AssertCompile(sizeof(pThis->uConnection) >= sizeof(Connection));
194 pThis->uConnection = Connection;
195 return VINF_SUCCESS;
196}
197
198
199DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
200{
201 RT_NOREF(penmWhat, pErrInfo);
202
203 /*
204 * Nothing to do if pre-inited.
205 */
206 if (fPreInited)
207 return VINF_SUCCESS;
208
209 /*
210 * Driverless?
211 */
212 if (fFlags & SUPR3INIT_F_DRIVERLESS)
213 {
214 pThis->fDriverless = true;
215 return VINF_SUCCESS;
216 }
217
218 /*
219 * Do the job.
220 */
221 Assert(pThis->hDevice == (intptr_t)NIL_RTFILE);
222 int rc = suplibDarwinOpenService(pThis);
223 if (RT_SUCCESS(rc))
224 {
225 rc = suplibDarwinOpenDevice(pThis, RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED));
226 if (RT_FAILURE(rc))
227 {
228 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
229 if (kr != kIOReturnSuccess)
230 {
231 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
232 AssertFailed();
233 }
234 pThis->uConnection = 0;
235 }
236 }
237 if ( RT_FAILURE(rc)
238 && fFlags & SUPR3INIT_F_DRIVERLESS_MASK)
239 {
240 LogRel(("Failed to open \"%s\", rc=%Rrc - Switching to driverless mode.\n", IOCLASS_NAME, rc));
241 pThis->fDriverless = true;
242 rc = VINF_SUCCESS;
243 }
244
245 return rc;
246}
247
248
249DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
250{
251 /*
252 * Close the connection to the IOService.
253 * This will cause the SUPDRVSESSION to be closed (starting IOC 9.1).
254 */
255 if (pThis->uConnection)
256 {
257 kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
258 if (kr != kIOReturnSuccess)
259 {
260 LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
261 AssertFailed();
262 }
263 pThis->uConnection = 0;
264 }
265
266 /*
267 * Check if we're inited at all.
268 */
269 if (pThis->hDevice != (intptr_t)NIL_RTFILE)
270 {
271 if (close(pThis->hDevice))
272 AssertFailed();
273 pThis->hDevice = (intptr_t)NIL_RTFILE;
274 }
275
276 return VINF_SUCCESS;
277}
278
279
280#ifndef IN_SUP_HARDENED_R3
281
282DECLHIDDEN(int) suplibOsInstall(void)
283{
284 return VERR_NOT_IMPLEMENTED;
285}
286
287
288DECLHIDDEN(int) suplibOsUninstall(void)
289{
290 return VERR_NOT_IMPLEMENTED;
291}
292
293
294DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
295{
296 RT_NOREF(cbReq);
297 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
298 return VINF_SUCCESS;
299 return RTErrConvertFromErrno(errno);
300}
301
302
303DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
304{
305 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
306 if (rc == -1)
307 rc = errno;
308 return rc;
309}
310
311
312DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
313{
314 RT_NOREF(pThis, fFlags);
315 *ppvPages = valloc(cPages << PAGE_SHIFT);
316 if (*ppvPages)
317 {
318 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
319 return VINF_SUCCESS;
320 }
321 return RTErrConvertFromErrno(errno);
322}
323
324
325DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
326{
327 NOREF(pThis);
328 free(pvPages);
329 return VINF_SUCCESS;
330}
331
332#endif /* !IN_SUP_HARDENED_R3 */
333
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