VirtualBox

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

Last change on this file since 4925 was 4925, checked in by vboxsync, 17 years ago

Added a LogRel with additional driver open info. Return the VERR_VM_DRIVER_* stuff on Solaris, Darwin, FreeBSD and OS/2 as well.

File size: 6.5 KB
Line 
1/** @file
2 *
3 * VBox host drivers - Ring-0 support drivers - Darwin host:
4 * Darwin implementations for support library
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_SUP
24#include <VBox/types.h>
25#include <VBox/sup.h>
26#include <VBox/param.h>
27#include <VBox/err.h>
28#include <VBox/log.h>
29#include <iprt/path.h>
30#include <iprt/assert.h>
31#include <iprt/err.h>
32#include <iprt/string.h>
33#include "SUPLibInternal.h"
34#include "SUPDRVIOC.h"
35
36#include <sys/fcntl.h>
37#include <sys/ioctl.h>
38#include <errno.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <mach/mach_port.h>
42#include <IOKit/IOKitLib.h>
43
44
45/*******************************************************************************
46* Defined Constants And Macros *
47*******************************************************************************/
48/** BSD Device name. */
49#define DEVICE_NAME "/dev/vboxdrv"
50/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
51#define IOCLASS_NAME "org_virtualbox_SupDrv"
52
53
54/*******************************************************************************
55* Global Variables *
56*******************************************************************************/
57/** Handle to the open device. */
58static int g_hDevice = -1;
59/** The IOMasterPort. */
60static mach_port_t g_MasterPort = 0;
61/** The current service connection. */
62static io_connect_t g_Connection = NULL;
63
64
65int suplibOsInit(size_t cbReserve)
66{
67 /*
68 * Check if already initialized.
69 */
70 if (g_hDevice >= 0)
71 return VINF_SUCCESS;
72
73 /*
74 * Open the IOKit client first - The first step is finding the service.
75 */
76 mach_port_t MasterPort;
77 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
78 if (kr != kIOReturnSuccess)
79 {
80 LogRel(("IOMasterPort -> %d\n", kr));
81 return VERR_GENERAL_FAILURE;
82 }
83
84 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
85 if (!ClassToMatch)
86 {
87 LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
88 return VERR_GENERAL_FAILURE;
89 }
90
91 /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
92 io_iterator_t Iterator;
93 kr = IOServiceGetMatchingServices(g_MasterPort, ClassToMatch, &Iterator);
94 if (kr != kIOReturnSuccess)
95 {
96 LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
97 return VERR_GENERAL_FAILURE;
98 }
99
100 /* Get the first item in the iterator and release it. */
101 io_service_t ServiceObject = IOIteratorNext(Iterator);
102 IOObjectRelease(Iterator);
103 if (!ServiceObject)
104 {
105 LogRel(("Couldn't find any matches.\n"));
106 return VERR_GENERAL_FAILURE;
107 }
108
109 /*
110 * Open the service.
111 * This will cause the user client class in SUPDrv-darwin.cpp to be instantiated.
112 */
113 kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &g_Connection);
114 IOObjectRelease(ServiceObject);
115 if (kr != kIOReturnSuccess)
116 {
117 LogRel(("IOServiceOpen returned %d\n", kr));
118 return VERR_GENERAL_FAILURE;
119 }
120
121 /*
122 * Now, try open the BSD device.
123 */
124 g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
125 if (g_hDevice < 0)
126 {
127 int rc;
128 switch (errno)
129 {
130 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
131 case EPERM:
132 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
133 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
134 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
135 }
136 LogRel(("Failed to open \"%s\", errno=%d, rc=%Vrc\n", DEVICE_NAME, errno, rc));
137
138 kr = IOServiceClose(g_Connection);
139 if (kr != kIOReturnSuccess)
140 LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
141 return rc;
142 }
143
144 /*
145 * Mark the file handle close on exec.
146 */
147 if (fcntl(g_hDevice, F_SETFD, FD_CLOEXEC) != 0)
148 {
149 int rc = errno;
150 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d\n", rc));
151 close(g_hDevice);
152 g_hDevice = -1;
153 return RTErrConvertFromErrno(rc);
154 }
155
156 /*
157 * We're done.
158 */
159 NOREF(cbReserve);
160 return VINF_SUCCESS;
161}
162
163
164int suplibOsTerm(void)
165{
166 /*
167 * Check if we're initited at all.
168 */
169 if (g_hDevice >= 0)
170 {
171 if (close(g_hDevice))
172 AssertFailed();
173 g_hDevice = -1;
174 }
175
176 /*
177 * Close the connection to the IOService and destroy the connection handle.
178 */
179 if (g_Connection)
180 {
181 kern_return_t kr = IOServiceClose(g_Connection);
182 if (kr != kIOReturnSuccess)
183 {
184 LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
185 AssertFailed();
186 }
187 g_Connection = NULL;
188 }
189
190 return VINF_SUCCESS;
191}
192
193
194int suplibOsInstall(void)
195{
196 return VERR_NOT_IMPLEMENTED;
197}
198
199
200int suplibOsUninstall(void)
201{
202 return VERR_NOT_IMPLEMENTED;
203}
204
205
206int suplibOsIOCtl(uintptr_t uFunction, void *pvReq, size_t cbReq)
207{
208 AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
209
210 if (RT_LIKELY(ioctl(g_hDevice, uFunction, pvReq) >= 0))
211 return VINF_SUCCESS;
212 return RTErrConvertFromErrno(errno);
213}
214
215
216int suplibOsIOCtlFast(uintptr_t uFunction)
217{
218 int rc = ioctl(g_hDevice, uFunction, NULL);
219 if (rc == -1)
220 rc = errno;
221 return rc;
222}
223
224
225int suplibOsPageAlloc(size_t cPages, void **ppvPages)
226{
227 *ppvPages = valloc(cPages << PAGE_SHIFT);
228 if (*ppvPages)
229 {
230 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
231 return VINF_SUCCESS;
232 }
233 return RTErrConvertFromErrno(errno);
234}
235
236
237int suplibOsPageFree(void *pvPages, size_t /* cPages */)
238{
239 free(pvPages);
240 return VINF_SUCCESS;
241}
242
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