3
* VBox host drivers - Ring-0 support drivers - Darwin host:
4
* Darwin implementations for support library
8
* Copyright (C) 2006-2007 innotek GmbH
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.
20
/*******************************************************************************
22
*******************************************************************************/
23
#define LOG_GROUP LOG_GROUP_SUP
24
#include <VBox/types.h>
26
#include <VBox/param.h>
29
#include <iprt/path.h>
30
#include <iprt/assert.h>
32
#include <iprt/string.h>
33
#include "SUPLibInternal.h"
34
#include "SUPDRVIOC.h"
36
#include <sys/fcntl.h>
37
#include <sys/ioctl.h>
41
#include <mach/mach_port.h>
42
#include <IOKit/IOKitLib.h>
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"
54
/*******************************************************************************
56
*******************************************************************************/
57
/** Handle to the open device. */
58
static int g_hDevice = -1;
59
/** The IOMasterPort. */
60
static mach_port_t g_MasterPort = 0;
61
/** The current service connection. */
62
static io_connect_t g_Connection = NULL;
65
int suplibOsInit(size_t cbReserve)
68
* Check if already initialized.
74
* Open the IOKit client first - The first step is finding the service.
76
mach_port_t MasterPort;
77
kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
78
if (kr != kIOReturnSuccess)
80
LogRel(("IOMasterPort -> %d\n", kr));
81
return VERR_GENERAL_FAILURE;
84
CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
87
LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
88
return VERR_GENERAL_FAILURE;
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)
96
LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
97
return VERR_GENERAL_FAILURE;
100
/* Get the first item in the iterator and release it. */
101
io_service_t ServiceObject = IOIteratorNext(Iterator);
102
IOObjectRelease(Iterator);
105
LogRel(("Couldn't find any matches.\n"));
106
return VERR_GENERAL_FAILURE;
111
* This will cause the user client class in SUPDrv-darwin.cpp to be instantiated.
113
kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &g_Connection);
114
IOObjectRelease(ServiceObject);
115
if (kr != kIOReturnSuccess)
117
LogRel(("IOServiceOpen returned %d\n", kr));
118
return VERR_GENERAL_FAILURE;
122
* Now, try open the BSD device.
124
g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
128
LogRel(("Failed to open \"%s\", errno=%d\n", rc));
129
kr = IOServiceClose(g_Connection);
130
if (kr != kIOReturnSuccess)
131
LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
132
return RTErrConvertFromErrno(rc);
143
int suplibOsTerm(void)
146
* Check if we're initited at all.
150
if (close(g_hDevice))
156
* Close the connection to the IOService and destroy the connection handle.
160
kern_return_t kr = IOServiceClose(g_Connection);
161
if (kr != kIOReturnSuccess)
163
LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
174
* Installs anything required by the support library.
176
* @returns 0 on success.
177
* @returns error code on failure.
179
int suplibOsInstall(void)
181
// int rc = mknod(DEVICE_NAME, S_IFCHR, );
183
return VERR_NOT_IMPLEMENTED;
188
* Installs anything required by the support library.
190
* @returns 0 on success.
191
* @returns error code on failure.
193
int suplibOsUninstall(void)
195
// int rc = unlink(DEVICE_NAME);
197
return VERR_NOT_IMPLEMENTED;
202
* Send a I/O Control request to the device.
204
* @returns 0 on success.
205
* @returns VBOX error code on failure.
206
* @param uFunction IO Control function.
207
* @param pvIn Input data buffer.
208
* @param cbIn Size of input data.
209
* @param pvOut Output data buffer.
210
* @param cbOut Size of output data.
212
int suplibOsIOCtl(unsigned uFunction, void *pvIn, size_t cbIn, void *pvOut, size_t cbOut)
214
AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
216
* Issue device iocontrol.
218
SUPDRVIOCTLDATA Args;
224
if (ioctl(g_hDevice, uFunction, &Args) >= 0)
226
/* This is the reverse operation of the one found in SUPDrv-linux.c */
229
case EACCES: return VERR_GENERAL_FAILURE;
230
case EINVAL: return VERR_INVALID_PARAMETER;
231
case ENOSYS: return VERR_INVALID_MAGIC;
232
case ENXIO: return VERR_INVALID_HANDLE;
233
case EFAULT: return VERR_INVALID_POINTER;
234
case ENOLCK: return VERR_LOCK_FAILED;
235
case EEXIST: return VERR_ALREADY_LOADED;
238
return RTErrConvertFromErrno(errno);
241
#ifdef VBOX_WITHOUT_IDT_PATCHING
242
int suplibOSIOCtlFast(unsigned uFunction)
244
int rc = ioctl(g_hDevice, uFunction, NULL);
253
* Allocate a number of zero-filled pages in user space.
255
* @returns VBox status code.
256
* @param cPages Number of pages to allocate.
257
* @param ppvPages Where to return the base pointer.
259
int suplibOsPageAlloc(size_t cPages, void **ppvPages)
261
*ppvPages = valloc(cPages << PAGE_SHIFT);
264
memset(*ppvPages, 0, cPages << PAGE_SHIFT);
267
return RTErrConvertFromErrno(errno);
272
* Frees pages allocated by suplibOsPageAlloc().
274
* @returns VBox status code.
275
* @param pvPages Pointer to pages.
277
int suplibOsPageFree(void *pvPages, size_t /* cPages */)