/* $Id: $ */ /** @file * VirtualBox Support Library - Darwin specific parts. */ /* * Copyright (C) 2006-2007 Sun Microsystems, Inc. * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * The contents of this file may alternatively be used under the terms * of the Common Development and Distribution License Version 1.0 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the * VirtualBox OSE distribution, in which case the provisions of the * CDDL are applicable instead of those of the GPL. * * You may elect to license modified versions of this file under the * terms and conditions of either the GPL or the CDDL or both. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 USA or visit http://www.sun.com if you need * additional information or have any questions. */ /******************************************************************************* * Header Files * *******************************************************************************/ #define LOG_GROUP LOG_GROUP_SUP #ifdef IN_SUP_HARDENED_R3 # undef DEBUG /* Warning: disables RT_STRICT */ # define LOG_DISABLED /** @todo RTLOGREL_DISABLED */ # include # undef LogRelIt # define LogRelIt(pvInst, fFlags, iGroup, fmtargs) do { } while (0) #endif #include #include #include #include #include #include #include #include #include #include "../SUPLibInternal.h" #include "../SUPDrvIOC.h" #include #include #include #include #include #include #include /******************************************************************************* * Defined Constants And Macros * *******************************************************************************/ /** BSD Device name. */ #define DEVICE_NAME "/dev/vboxdrv" /** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */ #define IOCLASS_NAME "org_virtualbox_SupDrv" /** * Opens the BSD device node. * * @returns VBox status code. */ static int suplibDarwinOpenDevice(PSUPLIBDATA pThis) { /* * Open the BSD device. * This will connect to the session created when the SupDrvClient was * started, so it has to be done after opening the service (IOC v9.1+). */ int hDevice = open(DEVICE_NAME, O_RDWR, 0); if (hDevice < 0) { int rc; switch (errno) { case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break; case EPERM: case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break; case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break; default: rc = VERR_VM_DRIVER_OPEN_ERROR; break; } LogRel(("SUP: Failed to open \"%s\", errno=%d, rc=%Rrc\n", DEVICE_NAME, errno, rc)); return rc; } /* * Mark the file handle close on exec. */ if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0) { #ifdef IN_SUP_HARDENED_R3 int rc = VERR_INTERNAL_ERROR; #else int err = errno; int rc = RTErrConvertFromErrno(err); LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc)); #endif close(hDevice); return rc; } pThis->hDevice = hDevice; return VINF_SUCCESS; } /** * Opens the IOKit service, instantiating org_virtualbox_SupDrvClient. * * @returns VBox status code. */ static int suplibDarwinOpenService(PSUPLIBDATA pThis) { /* * Open the IOKit client first - The first step is finding the service. */ mach_port_t MasterPort; kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort); if (kr != kIOReturnSuccess) { LogRel(("IOMasterPort -> %d\n", kr)); return VERR_GENERAL_FAILURE; } CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME); if (!ClassToMatch) { LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME)); return VERR_GENERAL_FAILURE; } /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */ io_iterator_t Iterator; kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator); if (kr != kIOReturnSuccess) { LogRel(("IOServiceGetMatchingServices returned %d\n", kr)); return VERR_GENERAL_FAILURE; } /* Get the first item in the iterator and release it. */ io_service_t ServiceObject = IOIteratorNext(Iterator); IOObjectRelease(Iterator); if (!ServiceObject) { LogRel(("SUP: Couldn't find any matches. The kernel module is probably not loaded.\n")); return VERR_VM_DRIVER_NOT_INSTALLED; } /* * Open the service. * * This will cause the user client class in SUPDrv-darwin.cpp to be * instantiated and create a session for this process. */ io_connect_t Connection = NULL; kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &Connection); IOObjectRelease(ServiceObject); if (kr != kIOReturnSuccess) { LogRel(("SUP: IOServiceOpen returned %d. Driver open failed.\n", kr)); pThis->pvConnection = NULL; return VERR_VM_DRIVER_OPEN_ERROR; } AssertCompile(sizeof(void *) == sizeof(Connection)); pThis->pvConnection = (void *)Connection; return VINF_SUCCESS; } int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited) { /* * Nothing to do if pre-inited. */ if (fPreInited) return VINF_SUCCESS; /* * Do the job. */ int rc = suplibDarwinOpenService(pThis); if (RT_SUCCESS(rc)) { rc = suplibDarwinOpenDevice(pThis); if (RT_FAILURE(rc)) { kern_return_t kr = IOServiceClose((io_connect_t)pThis->pvConnection); if (kr != kIOReturnSuccess) { LogRel(("Warning: IOServiceClose(%p) returned %d\n", pThis->pvConnection, kr)); AssertFailed(); } pThis->pvConnection = NULL; } } return rc; } #ifndef IN_SUP_HARDENED_R3 int suplibOsTerm(PSUPLIBDATA pThis) { /* * Close the connection to the IOService. * This will cause the SUPDRVSESSION to be closed (starting IOC 9.1). */ if (pThis->pvConnection) { kern_return_t kr = IOServiceClose((io_connect_t)pThis->pvConnection); if (kr != kIOReturnSuccess) { LogRel(("Warning: IOServiceClose(%p) returned %d\n", pThis->pvConnection, kr)); AssertFailed(); } pThis->pvConnection = NULL; } /* * Check if we're initited at all. */ if (pThis->hDevice >= 0) { if (close(pThis->hDevice)) AssertFailed(); pThis->hDevice = NIL_RTFILE; } return VINF_SUCCESS; } int suplibOsInstall(void) { return VERR_NOT_IMPLEMENTED; } int suplibOsUninstall(void) { return VERR_NOT_IMPLEMENTED; } int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq) { if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0)) return VINF_SUCCESS; return RTErrConvertFromErrno(errno); } int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction) { int rc = ioctl(pThis->hDevice, uFunction, NULL); if (rc == -1) rc = errno; return rc; } int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages) { NOREF(pThis); *ppvPages = valloc(cPages << PAGE_SHIFT); if (*ppvPages) { memset(*ppvPages, 0, cPages << PAGE_SHIFT); return VINF_SUCCESS; } return RTErrConvertFromErrno(errno); } int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */) { NOREF(pThis); free(pvPages); return VINF_SUCCESS; } #endif /* !IN_SUP_HARDENED_R3 */