[22077] | 1 | /* $Id: SUPLib-darwin.cpp 100108 2023-06-07 20:05:13Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[11725] | 3 | * VirtualBox Support Library - Darwin specific parts.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[387] | 41 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
[11725] | 42 | #ifdef IN_SUP_HARDENED_R3
|
---|
| 43 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
[66526] | 44 | # ifndef LOG_DISABLED
|
---|
| 45 | # define LOG_DISABLED
|
---|
| 46 | # endif
|
---|
[55982] | 47 | # define RTLOG_REL_DISABLED
|
---|
[11725] | 48 | # include <iprt/log.h>
|
---|
| 49 | #endif
|
---|
| 50 |
|
---|
[1] | 51 | #include <VBox/types.h>
|
---|
| 52 | #include <VBox/sup.h>
|
---|
| 53 | #include <VBox/param.h>
|
---|
| 54 | #include <VBox/err.h>
|
---|
[387] | 55 | #include <VBox/log.h>
|
---|
[1] | 56 | #include <iprt/path.h>
|
---|
| 57 | #include <iprt/assert.h>
|
---|
| 58 | #include <iprt/err.h>
|
---|
| 59 | #include <iprt/string.h>
|
---|
[10256] | 60 | #include "../SUPLibInternal.h"
|
---|
| 61 | #include "../SUPDrvIOC.h"
|
---|
[1] | 62 |
|
---|
| 63 | #include <sys/fcntl.h>
|
---|
| 64 | #include <sys/ioctl.h>
|
---|
[100097] | 65 | #include <sys/types.h>
|
---|
| 66 | #include <sys/sysctl.h>
|
---|
[1] | 67 | #include <errno.h>
|
---|
| 68 | #include <unistd.h>
|
---|
| 69 | #include <stdlib.h>
|
---|
[387] | 70 | #include <mach/mach_port.h>
|
---|
| 71 | #include <IOKit/IOKitLib.h>
|
---|
[1] | 72 |
|
---|
| 73 |
|
---|
[57358] | 74 | /*********************************************************************************************************************************
|
---|
| 75 | * Defined Constants And Macros *
|
---|
| 76 | *********************************************************************************************************************************/
|
---|
[44173] | 77 | /** System device name. */
|
---|
| 78 | #define DEVICE_NAME_SYS "/dev/vboxdrv"
|
---|
| 79 | /** User device name. */
|
---|
| 80 | #define DEVICE_NAME_USR "/dev/vboxdrvu"
|
---|
[387] | 81 | /** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
|
---|
| 82 | #define IOCLASS_NAME "org_virtualbox_SupDrv"
|
---|
[1] | 83 |
|
---|
| 84 |
|
---|
| 85 |
|
---|
[10836] | 86 | /**
|
---|
| 87 | * Opens the BSD device node.
|
---|
| 88 | *
|
---|
| 89 | * @returns VBox status code.
|
---|
| 90 | */
|
---|
[44173] | 91 | static int suplibDarwinOpenDevice(PSUPLIBDATA pThis, bool fUnrestricted)
|
---|
[1] | 92 | {
|
---|
| 93 | /*
|
---|
[10836] | 94 | * Open the BSD device.
|
---|
| 95 | * This will connect to the session created when the SupDrvClient was
|
---|
| 96 | * started, so it has to be done after opening the service (IOC v9.1+).
|
---|
[1] | 97 | */
|
---|
[44173] | 98 | int hDevice = open(fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, O_RDWR, 0);
|
---|
[11725] | 99 | if (hDevice < 0)
|
---|
[10836] | 100 | {
|
---|
| 101 | int rc;
|
---|
| 102 | switch (errno)
|
---|
| 103 | {
|
---|
| 104 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
| 105 | case EPERM:
|
---|
| 106 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
| 107 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
| 108 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
| 109 | }
|
---|
[44173] | 110 | LogRel(("SUP: Failed to open \"%s\", errno=%d, rc=%Rrc\n", fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, errno, rc));
|
---|
[10836] | 111 | return rc;
|
---|
| 112 | }
|
---|
[1] | 113 |
|
---|
| 114 | /*
|
---|
[10836] | 115 | * Mark the file handle close on exec.
|
---|
| 116 | */
|
---|
[11725] | 117 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
[10836] | 118 | {
|
---|
[11725] | 119 | #ifdef IN_SUP_HARDENED_R3
|
---|
| 120 | int rc = VERR_INTERNAL_ERROR;
|
---|
| 121 | #else
|
---|
[10836] | 122 | int err = errno;
|
---|
| 123 | int rc = RTErrConvertFromErrno(err);
|
---|
| 124 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
|
---|
[11725] | 125 | #endif
|
---|
| 126 | close(hDevice);
|
---|
[10836] | 127 | return rc;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[44173] | 130 | pThis->hDevice = hDevice;
|
---|
| 131 | pThis->fUnrestricted = fUnrestricted;
|
---|
[10836] | 132 | return VINF_SUCCESS;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Opens the IOKit service, instantiating org_virtualbox_SupDrvClient.
|
---|
| 138 | *
|
---|
| 139 | * @returns VBox status code.
|
---|
| 140 | */
|
---|
[11725] | 141 | static int suplibDarwinOpenService(PSUPLIBDATA pThis)
|
---|
[10836] | 142 | {
|
---|
| 143 | /*
|
---|
[387] | 144 | * Open the IOKit client first - The first step is finding the service.
|
---|
[1] | 145 | */
|
---|
[387] | 146 | mach_port_t MasterPort;
|
---|
[100108] | 147 | RT_GCC_NO_WARN_DEPRECATED_BEGIN
|
---|
| 148 | kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort); /* Deprecated since 12.0. */
|
---|
| 149 | RT_GCC_NO_WARN_DEPRECATED_END
|
---|
[387] | 150 | if (kr != kIOReturnSuccess)
|
---|
| 151 | {
|
---|
| 152 | LogRel(("IOMasterPort -> %d\n", kr));
|
---|
| 153 | return VERR_GENERAL_FAILURE;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
|
---|
| 157 | if (!ClassToMatch)
|
---|
| 158 | {
|
---|
| 159 | LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
|
---|
| 160 | return VERR_GENERAL_FAILURE;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
|
---|
| 164 | io_iterator_t Iterator;
|
---|
[11725] | 165 | kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
|
---|
[387] | 166 | if (kr != kIOReturnSuccess)
|
---|
| 167 | {
|
---|
| 168 | LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
|
---|
| 169 | return VERR_GENERAL_FAILURE;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /* Get the first item in the iterator and release it. */
|
---|
| 173 | io_service_t ServiceObject = IOIteratorNext(Iterator);
|
---|
| 174 | IOObjectRelease(Iterator);
|
---|
| 175 | if (!ServiceObject)
|
---|
| 176 | {
|
---|
[4946] | 177 | LogRel(("SUP: Couldn't find any matches. The kernel module is probably not loaded.\n"));
|
---|
| 178 | return VERR_VM_DRIVER_NOT_INSTALLED;
|
---|
[387] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /*
|
---|
| 182 | * Open the service.
|
---|
[10836] | 183 | *
|
---|
| 184 | * This will cause the user client class in SUPDrv-darwin.cpp to be
|
---|
| 185 | * instantiated and create a session for this process.
|
---|
[387] | 186 | */
|
---|
[62305] | 187 | io_connect_t Connection = 0;
|
---|
[51488] | 188 | kr = IOServiceOpen(ServiceObject, mach_task_self(), SUP_DARWIN_IOSERVICE_COOKIE, &Connection);
|
---|
[387] | 189 | IOObjectRelease(ServiceObject);
|
---|
| 190 | if (kr != kIOReturnSuccess)
|
---|
| 191 | {
|
---|
[4946] | 192 | LogRel(("SUP: IOServiceOpen returned %d. Driver open failed.\n", kr));
|
---|
[16335] | 193 | pThis->uConnection = 0;
|
---|
[4946] | 194 | return VERR_VM_DRIVER_OPEN_ERROR;
|
---|
[387] | 195 | }
|
---|
| 196 |
|
---|
[16335] | 197 | AssertCompile(sizeof(pThis->uConnection) >= sizeof(Connection));
|
---|
| 198 | pThis->uConnection = Connection;
|
---|
[10836] | 199 | return VINF_SUCCESS;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[11725] | 202 |
|
---|
[92613] | 203 | DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
|
---|
[10836] | 204 | {
|
---|
[63463] | 205 | RT_NOREF(penmWhat, pErrInfo);
|
---|
| 206 |
|
---|
[387] | 207 | /*
|
---|
[11725] | 208 | * Nothing to do if pre-inited.
|
---|
[387] | 209 | */
|
---|
[11725] | 210 | if (fPreInited)
|
---|
[10836] | 211 | return VINF_SUCCESS;
|
---|
[4925] | 212 |
|
---|
[1] | 213 | /*
|
---|
[93030] | 214 | * Driverless?
|
---|
| 215 | */
|
---|
| 216 | if (fFlags & SUPR3INIT_F_DRIVERLESS)
|
---|
| 217 | {
|
---|
| 218 | pThis->fDriverless = true;
|
---|
| 219 | return VINF_SUCCESS;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /*
|
---|
[10836] | 223 | * Do the job.
|
---|
[4871] | 224 | */
|
---|
[37596] | 225 | Assert(pThis->hDevice == (intptr_t)NIL_RTFILE);
|
---|
[11725] | 226 | int rc = suplibDarwinOpenService(pThis);
|
---|
[10836] | 227 | if (RT_SUCCESS(rc))
|
---|
[4871] | 228 | {
|
---|
[92613] | 229 | rc = suplibDarwinOpenDevice(pThis, RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED));
|
---|
[10836] | 230 | if (RT_FAILURE(rc))
|
---|
| 231 | {
|
---|
[16335] | 232 | kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
|
---|
[10836] | 233 | if (kr != kIOReturnSuccess)
|
---|
| 234 | {
|
---|
[16335] | 235 | LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
|
---|
[10836] | 236 | AssertFailed();
|
---|
| 237 | }
|
---|
[16335] | 238 | pThis->uConnection = 0;
|
---|
[10836] | 239 | }
|
---|
[4871] | 240 | }
|
---|
[93030] | 241 | if ( RT_FAILURE(rc)
|
---|
| 242 | && fFlags & SUPR3INIT_F_DRIVERLESS_MASK)
|
---|
| 243 | {
|
---|
| 244 | LogRel(("Failed to open \"%s\", rc=%Rrc - Switching to driverless mode.\n", IOCLASS_NAME, rc));
|
---|
| 245 | pThis->fDriverless = true;
|
---|
| 246 | rc = VINF_SUCCESS;
|
---|
| 247 | }
|
---|
[4871] | 248 |
|
---|
[10836] | 249 | return rc;
|
---|
[1] | 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
[85129] | 253 | DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
|
---|
[1] | 254 | {
|
---|
| 255 | /*
|
---|
[10836] | 256 | * Close the connection to the IOService.
|
---|
| 257 | * This will cause the SUPDRVSESSION to be closed (starting IOC 9.1).
|
---|
[1] | 258 | */
|
---|
[16335] | 259 | if (pThis->uConnection)
|
---|
[387] | 260 | {
|
---|
[16335] | 261 | kern_return_t kr = IOServiceClose((io_connect_t)pThis->uConnection);
|
---|
[387] | 262 | if (kr != kIOReturnSuccess)
|
---|
| 263 | {
|
---|
[16335] | 264 | LogRel(("Warning: IOServiceClose(%RCv) returned %d\n", pThis->uConnection, kr));
|
---|
[387] | 265 | AssertFailed();
|
---|
| 266 | }
|
---|
[16335] | 267 | pThis->uConnection = 0;
|
---|
[387] | 268 | }
|
---|
| 269 |
|
---|
[10836] | 270 | /*
|
---|
[33540] | 271 | * Check if we're inited at all.
|
---|
[10836] | 272 | */
|
---|
[37596] | 273 | if (pThis->hDevice != (intptr_t)NIL_RTFILE)
|
---|
[10836] | 274 | {
|
---|
[11725] | 275 | if (close(pThis->hDevice))
|
---|
[10836] | 276 | AssertFailed();
|
---|
[37596] | 277 | pThis->hDevice = (intptr_t)NIL_RTFILE;
|
---|
[10836] | 278 | }
|
---|
| 279 |
|
---|
[1] | 280 | return VINF_SUCCESS;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 |
|
---|
[66573] | 284 | #ifndef IN_SUP_HARDENED_R3
|
---|
| 285 |
|
---|
[85129] | 286 | DECLHIDDEN(int) suplibOsInstall(void)
|
---|
[1] | 287 | {
|
---|
| 288 | return VERR_NOT_IMPLEMENTED;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 |
|
---|
[85129] | 292 | DECLHIDDEN(int) suplibOsUninstall(void)
|
---|
[1] | 293 | {
|
---|
| 294 | return VERR_NOT_IMPLEMENTED;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 |
|
---|
[85129] | 298 | DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
[1] | 299 | {
|
---|
[63463] | 300 | RT_NOREF(cbReq);
|
---|
[11725] | 301 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
[26512] | 302 | return VINF_SUCCESS;
|
---|
[1] | 303 | return RTErrConvertFromErrno(errno);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[4811] | 306 |
|
---|
[85129] | 307 | DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
[1] | 308 | {
|
---|
[20394] | 309 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
[1] | 310 | if (rc == -1)
|
---|
| 311 | rc = errno;
|
---|
| 312 | return rc;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 |
|
---|
[92556] | 316 | DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
|
---|
[1] | 317 | {
|
---|
[92556] | 318 | RT_NOREF(pThis, fFlags);
|
---|
[1] | 319 | *ppvPages = valloc(cPages << PAGE_SHIFT);
|
---|
| 320 | if (*ppvPages)
|
---|
| 321 | {
|
---|
| 322 | memset(*ppvPages, 0, cPages << PAGE_SHIFT);
|
---|
| 323 | return VINF_SUCCESS;
|
---|
| 324 | }
|
---|
| 325 | return RTErrConvertFromErrno(errno);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
[85129] | 329 | DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
|
---|
[1] | 330 | {
|
---|
[11725] | 331 | NOREF(pThis);
|
---|
[1] | 332 | free(pvPages);
|
---|
| 333 | return VINF_SUCCESS;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[100097] | 336 |
|
---|
| 337 | DECLHIDDEN(bool) suplibOsIsNemSupportedWhenNoVtxOrAmdV(void)
|
---|
| 338 | {
|
---|
| 339 | # if ARCH_BITS == 64
|
---|
| 340 | int fHvSupported = 0;
|
---|
| 341 | size_t cb = sizeof(fHvSupported);
|
---|
| 342 | int rc = sysctlbyname("kern.hv.supported", &fHvSupported, &cb, NULL, 0);
|
---|
| 343 | if ( !rc
|
---|
| 344 | && cb == sizeof(uint32_t))
|
---|
| 345 | return fHvSupported == 1;
|
---|
| 346 |
|
---|
| 347 | return false;
|
---|
| 348 | # else
|
---|
| 349 | return false;
|
---|
| 350 | #endif
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[11725] | 353 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
| 354 |
|
---|