[22077] | 1 | /* $Id: SUPLib-freebsd.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[1890] | 2 | /** @file
|
---|
[11725] | 3 | * VirtualBox Support Library - FreeBSD specific parts.
|
---|
[1890] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1890] | 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
|
---|
[1890] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1890] | 41 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
[11725] | 42 | #ifdef IN_SUP_HARDENED_R3
|
---|
| 43 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
| 44 | # define LOG_DISABLED
|
---|
[55982] | 45 | # define RTLOG_REL_DISABLED
|
---|
[11725] | 46 | # include <iprt/log.h>
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
[1890] | 49 | #include <VBox/types.h>
|
---|
| 50 | #include <VBox/sup.h>
|
---|
| 51 | #include <VBox/param.h>
|
---|
| 52 | #include <VBox/err.h>
|
---|
| 53 | #include <VBox/log.h>
|
---|
| 54 | #include <iprt/path.h>
|
---|
| 55 | #include <iprt/assert.h>
|
---|
| 56 | #include <iprt/mem.h>
|
---|
| 57 | #include <iprt/err.h>
|
---|
| 58 | #include <iprt/string.h>
|
---|
[10256] | 59 | #include "../SUPLibInternal.h"
|
---|
| 60 | #include "../SUPDrvIOC.h"
|
---|
[1890] | 61 |
|
---|
| 62 | #include <sys/fcntl.h>
|
---|
| 63 | #include <sys/ioctl.h>
|
---|
| 64 | #include <errno.h>
|
---|
| 65 | #include <unistd.h>
|
---|
| 66 | #include <stdlib.h>
|
---|
[19342] | 67 | #include <stdio.h>
|
---|
[1890] | 68 |
|
---|
[57358] | 69 |
|
---|
| 70 | /*********************************************************************************************************************************
|
---|
| 71 | * Defined Constants And Macros *
|
---|
| 72 | *********************************************************************************************************************************/
|
---|
[44173] | 73 | /** System device name. */
|
---|
| 74 | #define DEVICE_NAME_SYS "/dev/vboxdrv"
|
---|
| 75 | /** User device name. */
|
---|
| 76 | #define DEVICE_NAME_USR "/dev/vboxdrvu"
|
---|
[1890] | 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
[92613] | 80 | DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
|
---|
[1890] | 81 | {
|
---|
| 82 | /*
|
---|
[11725] | 83 | * Nothing to do if pre-inited.
|
---|
[1890] | 84 | */
|
---|
[11725] | 85 | if (fPreInited)
|
---|
[1890] | 86 | return VINF_SUCCESS;
|
---|
| 87 |
|
---|
| 88 | /*
|
---|
| 89 | * Try open the BSD device.
|
---|
| 90 | */
|
---|
[92613] | 91 | const char * const *pszDeviceNm = fFlags & SUPR3INIT_F_UNRESTRICTED ? DEVICE_NAME_SYS : DEVICE_NAME_USR;
|
---|
| 92 | int hDevice = open(pszDeviceNm, O_RDWR, 0);
|
---|
[11725] | 93 | if (hDevice < 0)
|
---|
[1890] | 94 | {
|
---|
[4925] | 95 | int rc;
|
---|
| 96 | switch (errno)
|
---|
| 97 | {
|
---|
| 98 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
| 99 | case EPERM:
|
---|
| 100 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
| 101 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
| 102 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
| 103 | }
|
---|
[92613] | 104 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", pszDeviceNm, errno, rc));
|
---|
[4988] | 105 | return rc;
|
---|
[1890] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /*
|
---|
[4872] | 109 | * Mark the file handle close on exec.
|
---|
| 110 | */
|
---|
[11725] | 111 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
[4872] | 112 | {
|
---|
[11725] | 113 | #ifdef IN_SUP_HARDENED_R3
|
---|
| 114 | int rc = VERR_INTERNAL_ERROR;
|
---|
| 115 | #else
|
---|
| 116 | int err = errno;
|
---|
| 117 | int rc = RTErrConvertFromErrno(err);
|
---|
| 118 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
|
---|
| 119 | #endif
|
---|
| 120 | close(hDevice);
|
---|
| 121 | return rc;
|
---|
[4872] | 122 | }
|
---|
| 123 |
|
---|
| 124 | /*
|
---|
[1890] | 125 | * We're done.
|
---|
| 126 | */
|
---|
[44173] | 127 | pThis->hDevice = hDevice;
|
---|
[92613] | 128 | pThis->fUnrestricted = RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED);
|
---|
[1890] | 129 | return VINF_SUCCESS;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
[85129] | 133 | DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
|
---|
[1890] | 134 | {
|
---|
| 135 | /*
|
---|
[33540] | 136 | * Check if we're inited at all.
|
---|
[1890] | 137 | */
|
---|
[37596] | 138 | if (pThis->hDevice != (intptr_t)NIL_RTFILE)
|
---|
[1890] | 139 | {
|
---|
[11725] | 140 | if (close(pThis->hDevice))
|
---|
[1890] | 141 | AssertFailed();
|
---|
[37596] | 142 | pThis->hDevice = (intptr_t)NIL_RTFILE;
|
---|
[1890] | 143 | }
|
---|
| 144 | return VINF_SUCCESS;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[66573] | 148 | #ifndef IN_SUP_HARDENED_R3
|
---|
| 149 |
|
---|
[85129] | 150 | DECLHIDDEN(int) suplibOsInstall(void)
|
---|
[1890] | 151 | {
|
---|
| 152 | return VERR_NOT_IMPLEMENTED;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
[85129] | 156 | DECLHIDDEN(int) suplibOsUninstall(void)
|
---|
[1890] | 157 | {
|
---|
| 158 | return VERR_NOT_IMPLEMENTED;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
[85129] | 162 | DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
[1890] | 163 | {
|
---|
[11725] | 164 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
[19564] | 165 | return VINF_SUCCESS;
|
---|
[1890] | 166 | return RTErrConvertFromErrno(errno);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[4811] | 169 |
|
---|
[85129] | 170 | DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
[1890] | 171 | {
|
---|
[13865] | 172 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
[1890] | 173 | if (rc == -1)
|
---|
| 174 | rc = errno;
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[92556] | 179 | DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
|
---|
[1890] | 180 | {
|
---|
[92556] | 181 | RT_NOREF(pThis, fFlags);
|
---|
[1890] | 182 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
| 183 | if (*ppvPages)
|
---|
| 184 | return VINF_SUCCESS;
|
---|
| 185 | return RTErrConvertFromErrno(errno);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
[85129] | 189 | DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
|
---|
[1890] | 190 | {
|
---|
[11725] | 191 | NOREF(pThis);
|
---|
[28317] | 192 | RTMemPageFree(pvPages, cPages * PAGE_SIZE);
|
---|
[1890] | 193 | return VINF_SUCCESS;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[11725] | 196 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
| 197 |
|
---|