VirtualBox

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

Last change on this file since 49032 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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