1 | /** @file
|
---|
2 | * SUPLib - FreeBSD Hosts,
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | *
|
---|
25 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
35 | #include <VBox/types.h>
|
---|
36 | #include <VBox/sup.h>
|
---|
37 | #include <VBox/param.h>
|
---|
38 | #include <VBox/err.h>
|
---|
39 | #include <VBox/log.h>
|
---|
40 | #include <iprt/path.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include "SUPLibInternal.h"
|
---|
46 | #include "SUPDRVIOC.h"
|
---|
47 |
|
---|
48 | #include <sys/fcntl.h>
|
---|
49 | #include <sys/ioctl.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <unistd.h>
|
---|
52 | #include <stdlib.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Defined Constants And Macros *
|
---|
57 | *******************************************************************************/
|
---|
58 | /** FreeBSD base device name. */
|
---|
59 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
60 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Global Variables *
|
---|
64 | *******************************************************************************/
|
---|
65 | /** Handle to the open device. */
|
---|
66 | static int g_hDevice = -1;
|
---|
67 |
|
---|
68 |
|
---|
69 | int suplibOsInit(size_t cbReserve)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * Check if already initialized.
|
---|
73 | */
|
---|
74 | if (g_hDevice >= 0)
|
---|
75 | return VINF_SUCCESS;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Try open the BSD device.
|
---|
79 | */
|
---|
80 | char szDevice[sizeof(DEVICE_NAME) + 16];
|
---|
81 | for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
|
---|
82 | {
|
---|
83 | errno = 0;
|
---|
84 | RTStrPrintf(szDevice, sizeof(szDevice), DEVICE_NAME "%d", iUnit);
|
---|
85 | g_hDevice = open(szDevice, O_RDWR, 0);
|
---|
86 | if (g_hDevice >= 0 || errno != EBUSY)
|
---|
87 | break;
|
---|
88 | }
|
---|
89 | if (g_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=%Vrc\n", szDevice, errno, rc));
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Mark the file handle close on exec.
|
---|
106 | */
|
---|
107 | if (fcntl(g_hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
108 | {
|
---|
109 | int rc = errno;
|
---|
110 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d\n", rc));
|
---|
111 | close(g_hDevice);
|
---|
112 | g_hDevice = -1;
|
---|
113 | return RTErrConvertFromErrno(rc);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * We're done.
|
---|
118 | */
|
---|
119 | NOREF(cbReserve);
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | int suplibOsTerm(void)
|
---|
125 | {
|
---|
126 | /*
|
---|
127 | * Check if we're initited at all.
|
---|
128 | */
|
---|
129 | if (g_hDevice >= 0)
|
---|
130 | {
|
---|
131 | if (close(g_hDevice))
|
---|
132 | AssertFailed();
|
---|
133 | g_hDevice = -1;
|
---|
134 | }
|
---|
135 | return VINF_SUCCESS;
|
---|
136 | }
|
---|
137 |
|
---|
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(uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
152 | {
|
---|
153 | AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
|
---|
154 |
|
---|
155 | if (RT_LIKELY(ioctl(g_hDevice, uFunction, pvReq) >= 0))
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | return RTErrConvertFromErrno(errno);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | int suplibOsIOCtlFast(uintptr_t uFunction)
|
---|
162 | {
|
---|
163 | int rc = ioctl(g_hDevice, uFunction, NULL);
|
---|
164 | if (rc == -1)
|
---|
165 | rc = errno;
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | int suplibOsPageAlloc(size_t cPages, void **ppvPages)
|
---|
171 | {
|
---|
172 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
173 | if (*ppvPages)
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | return RTErrConvertFromErrno(errno);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | int suplibOsPageFree(void *pvPages, size_t /* cPages */)
|
---|
180 | {
|
---|
181 | RTMemPageFree(pvPages);
|
---|
182 | return VINF_SUCCESS;
|
---|
183 | }
|
---|
184 |
|
---|