VirtualBox

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

Last change on this file since 78363 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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