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