VirtualBox

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

Last change on this file since 65929 was 63463, checked in by vboxsync, 8 years ago

SUP: warnings

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