1 | /* $Id: SUPLib-freebsd.cpp 69498 2017-10-28 15:07:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - FreeBSD specific parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
32 | #ifdef IN_SUP_HARDENED_R3
|
---|
33 | # undef DEBUG /* Warning: disables RT_STRICT */
|
---|
34 | # define LOG_DISABLED
|
---|
35 | # define RTLOG_REL_DISABLED
|
---|
36 | # include <iprt/log.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include <VBox/types.h>
|
---|
40 | #include <VBox/sup.h>
|
---|
41 | #include <VBox/param.h>
|
---|
42 | #include <VBox/err.h>
|
---|
43 | #include <VBox/log.h>
|
---|
44 | #include <iprt/path.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/err.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 | #include "../SUPLibInternal.h"
|
---|
50 | #include "../SUPDrvIOC.h"
|
---|
51 |
|
---|
52 | #include <sys/fcntl.h>
|
---|
53 | #include <sys/ioctl.h>
|
---|
54 | #include <errno.h>
|
---|
55 | #include <unistd.h>
|
---|
56 | #include <stdlib.h>
|
---|
57 | #include <stdio.h>
|
---|
58 |
|
---|
59 |
|
---|
60 | /*********************************************************************************************************************************
|
---|
61 | * Defined Constants And Macros *
|
---|
62 | *********************************************************************************************************************************/
|
---|
63 | /** System device name. */
|
---|
64 | #define DEVICE_NAME_SYS "/dev/vboxdrv"
|
---|
65 | /** User device name. */
|
---|
66 | #define DEVICE_NAME_USR "/dev/vboxdrvu"
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, bool fUnrestricted, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Nothing to do if pre-inited.
|
---|
74 | */
|
---|
75 | if (fPreInited)
|
---|
76 | return VINF_SUCCESS;
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Try open the BSD device.
|
---|
80 | */
|
---|
81 | int hDevice = open(fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, O_RDWR, 0);
|
---|
82 | if (hDevice < 0)
|
---|
83 | {
|
---|
84 | int rc;
|
---|
85 | switch (errno)
|
---|
86 | {
|
---|
87 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
88 | case EPERM:
|
---|
89 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
90 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
91 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
92 | }
|
---|
93 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR, errno, rc));
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Mark the file handle close on exec.
|
---|
99 | */
|
---|
100 | if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
101 | {
|
---|
102 | #ifdef IN_SUP_HARDENED_R3
|
---|
103 | int rc = VERR_INTERNAL_ERROR;
|
---|
104 | #else
|
---|
105 | int err = errno;
|
---|
106 | int rc = RTErrConvertFromErrno(err);
|
---|
107 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
|
---|
108 | #endif
|
---|
109 | close(hDevice);
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * We're done.
|
---|
115 | */
|
---|
116 | pThis->hDevice = hDevice;
|
---|
117 | pThis->fUnrestricted = fUnrestricted;
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | int suplibOsTerm(PSUPLIBDATA pThis)
|
---|
123 | {
|
---|
124 | /*
|
---|
125 | * Check if we're inited at all.
|
---|
126 | */
|
---|
127 | if (pThis->hDevice != (intptr_t)NIL_RTFILE)
|
---|
128 | {
|
---|
129 | if (close(pThis->hDevice))
|
---|
130 | AssertFailed();
|
---|
131 | pThis->hDevice = (intptr_t)NIL_RTFILE;
|
---|
132 | }
|
---|
133 | return VINF_SUCCESS;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | #ifndef IN_SUP_HARDENED_R3
|
---|
138 |
|
---|
139 | int suplibOsInstall(void)
|
---|
140 | {
|
---|
141 | return VERR_NOT_IMPLEMENTED;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | int suplibOsUninstall(void)
|
---|
146 | {
|
---|
147 | return VERR_NOT_IMPLEMENTED;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
152 | {
|
---|
153 | if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
|
---|
154 | return VINF_SUCCESS;
|
---|
155 | return RTErrConvertFromErrno(errno);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
|
---|
160 | {
|
---|
161 | int rc = ioctl(pThis->hDevice, uFunction, idCpu);
|
---|
162 | if (rc == -1)
|
---|
163 | rc = errno;
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
|
---|
169 | {
|
---|
170 | NOREF(pThis);
|
---|
171 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
172 | if (*ppvPages)
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | return RTErrConvertFromErrno(errno);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
|
---|
179 | {
|
---|
180 | NOREF(pThis);
|
---|
181 | RTMemPageFree(pvPages, cPages * PAGE_SIZE);
|
---|
182 | return VINF_SUCCESS;
|
---|
183 | }
|
---|
184 |
|
---|
185 | #endif /* !IN_SUP_HARDENED_R3 */
|
---|
186 |
|
---|