1 | /* $Id: SUPLib-freebsd.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - FreeBSD specific parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
42 | #ifdef IN_SUP_HARDENED_R3
|
---|
43 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
44 | # define LOG_DISABLED
|
---|
45 | # define RTLOG_REL_DISABLED
|
---|
46 | # include <iprt/log.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
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>
|
---|
59 | #include "../SUPLibInternal.h"
|
---|
60 | #include "../SUPDrvIOC.h"
|
---|
61 |
|
---|
62 | #include <sys/fcntl.h>
|
---|
63 | #include <sys/ioctl.h>
|
---|
64 | #include <errno.h>
|
---|
65 | #include <unistd.h>
|
---|
66 | #include <stdlib.h>
|
---|
67 | #include <stdio.h>
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Defined Constants And Macros *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 | /** System device name. */
|
---|
74 | #define DEVICE_NAME_SYS "/dev/vboxdrv"
|
---|
75 | /** User device name. */
|
---|
76 | #define DEVICE_NAME_USR "/dev/vboxdrvu"
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Nothing to do if pre-inited.
|
---|
84 | */
|
---|
85 | if (fPreInited)
|
---|
86 | return VINF_SUCCESS;
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Try open the BSD device.
|
---|
90 | */
|
---|
91 | const char * const *pszDeviceNm = fFlags & SUPR3INIT_F_UNRESTRICTED ? DEVICE_NAME_SYS : DEVICE_NAME_USR;
|
---|
92 | int hDevice = open(pszDeviceNm, O_RDWR, 0);
|
---|
93 | if (hDevice < 0)
|
---|
94 | {
|
---|
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 | }
|
---|
104 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", pszDeviceNm, errno, rc));
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Mark the file handle close on exec.
|
---|
110 | */
|
---|
111 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
112 | {
|
---|
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;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * We're done.
|
---|
126 | */
|
---|
127 | pThis->hDevice = hDevice;
|
---|
128 | pThis->fUnrestricted = RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED);
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * Check if we're inited at all.
|
---|
137 | */
|
---|
138 | if (pThis->hDevice != (intptr_t)NIL_RTFILE)
|
---|
139 | {
|
---|
140 | if (close(pThis->hDevice))
|
---|
141 | AssertFailed();
|
---|
142 | pThis->hDevice = (intptr_t)NIL_RTFILE;
|
---|
143 | }
|
---|
144 | return VINF_SUCCESS;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | #ifndef IN_SUP_HARDENED_R3
|
---|
149 |
|
---|
150 | DECLHIDDEN(int) suplibOsInstall(void)
|
---|
151 | {
|
---|
152 | return VERR_NOT_IMPLEMENTED;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | DECLHIDDEN(int) suplibOsUninstall(void)
|
---|
157 | {
|
---|
158 | return VERR_NOT_IMPLEMENTED;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
163 | {
|
---|
164 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
165 | return VINF_SUCCESS;
|
---|
166 | return RTErrConvertFromErrno(errno);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
171 | {
|
---|
172 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
173 | if (rc == -1)
|
---|
174 | rc = errno;
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
|
---|
180 | {
|
---|
181 | RT_NOREF(pThis, fFlags);
|
---|
182 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
183 | if (*ppvPages)
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | return RTErrConvertFromErrno(errno);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
|
---|
190 | {
|
---|
191 | NOREF(pThis);
|
---|
192 | RTMemPageFree(pvPages, cPages * PAGE_SIZE);
|
---|
193 | return VINF_SUCCESS;
|
---|
194 | }
|
---|
195 |
|
---|
196 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
197 |
|
---|