VirtualBox

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

Last change on this file since 25360 was 22077, checked in by vboxsync, 15 years ago

HostDrivers/Support: header and svn props cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: SUPLib-darwin.cpp 22077 2009-08-07 16:01:57Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Darwin specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SUP
35#ifdef IN_SUP_HARDENED_R3
36# undef DEBUG /* Warning: disables RT_STRICT */
37# define LOG_DISABLED
38 /** @todo RTLOGREL_DISABLED */
39# include <iprt/log.h>
40# undef LogRelIt
41# define LogRelIt(pvInst, fFlags, iGroup, fmtargs) do { } while (0)
42#endif
43
44#include <VBox/types.h>
45#include <VBox/sup.h>
46#include <VBox/param.h>
47#include <VBox/err.h>
48#include <VBox/log.h>
49#include <iprt/path.h>
50#include <iprt/assert.h>
51#include <iprt/err.h>
52#include <iprt/string.h>
53#include "../SUPLibInternal.h"
54#include "../SUPDrvIOC.h"
55
56#include <sys/fcntl.h>
57#include <sys/ioctl.h>
58#include <errno.h>
59#include <unistd.h>
60#include <stdlib.h>
61#include <mach/mach_port.h>
62#include <IOKit/IOKitLib.h>
63
64
65/*******************************************************************************
66* Defined Constants And Macros *
67*******************************************************************************/
68/** BSD Device name. */
69#define DEVICE_NAME "/dev/vboxdrv"
70/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
71#define IOCLASS_NAME "org_virtualbox_SupDrv"
72
73
74
75/**
76 * Opens the BSD device node.
77 *
78 * @returns VBox status code.
79 */
80static int suplibDarwinOpenDevice(PSUPLIBDATA pThis)
81{
82 /*
83 * Open the BSD device.
84 * This will connect to the session created when the SupDrvClient was
85 * started, so it has to be done after opening the service (IOC v9.1+).
86 */
87 int hDevice = open(DEVICE_NAME, O_RDWR, 0);
88 if (hDevice < 0)
89 {
90 int rc;
91 switch (errno)
92 {
93 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
94 case EPERM:
95 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
96 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
97 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
98 }
99 LogRel(("SUP: Failed to open \"%s\", errno=%d, rc=%Rrc\n", DEVICE_NAME, errno, rc));
100 return rc;
101 }
102
103 /*
104 * Mark the file handle close on exec.
105 */
106 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
107 {
108#ifdef IN_SUP_HARDENED_R3
109 int rc = VERR_INTERNAL_ERROR;
110#else
111 int err = errno;
112 int rc = RTErrConvertFromErrno(err);
113 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
114#endif
115 close(hDevice);
116 return rc;
117 }
118
119 pThis->hDevice = hDevice;
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 = NULL;
174 kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &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)
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 == NIL_RTFILE);
201 int rc = suplibDarwinOpenService(pThis);
202 if (RT_SUCCESS(rc))
203 {
204 rc = suplibDarwinOpenDevice(pThis);
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 initited at all.
242 */
243 if (pThis->hDevice != NIL_RTFILE)
244 {
245 if (close(pThis->hDevice))
246 AssertFailed();
247 pThis->hDevice = 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 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
269 return VINF_SUCCESS;
270 return RTErrConvertFromErrno(errno);
271}
272
273
274int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
275{
276 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
277 if (rc == -1)
278 rc = errno;
279 return rc;
280}
281
282
283int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
284{
285 NOREF(pThis);
286 *ppvPages = valloc(cPages << PAGE_SHIFT);
287 if (*ppvPages)
288 {
289 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
290 return VINF_SUCCESS;
291 }
292 return RTErrConvertFromErrno(errno);
293}
294
295
296int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
297{
298 NOREF(pThis);
299 free(pvPages);
300 return VINF_SUCCESS;
301}
302
303#endif /* !IN_SUP_HARDENED_R3 */
304
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