1 | /** @file
|
---|
2 | * SUPLib - FreeBSD Hosts,
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 |
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
31 | #include <VBox/types.h>
|
---|
32 | #include <VBox/sup.h>
|
---|
33 | #include <VBox/param.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include "SUPLibInternal.h"
|
---|
42 | #include "SUPDRVIOC.h"
|
---|
43 |
|
---|
44 | #include <sys/fcntl.h>
|
---|
45 | #include <sys/ioctl.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <unistd.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Defined Constants And Macros *
|
---|
53 | *******************************************************************************/
|
---|
54 | /** FreeBSD base device name. */
|
---|
55 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
56 |
|
---|
57 |
|
---|
58 | /*******************************************************************************
|
---|
59 | * Global Variables *
|
---|
60 | *******************************************************************************/
|
---|
61 | /** Handle to the open device. */
|
---|
62 | static int g_hDevice = -1;
|
---|
63 |
|
---|
64 |
|
---|
65 | int suplibOsInit(size_t cbReserve)
|
---|
66 | {
|
---|
67 | /*
|
---|
68 | * Check if already initialized.
|
---|
69 | */
|
---|
70 | if (g_hDevice >= 0)
|
---|
71 | return VINF_SUCCESS;
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Try open the BSD device.
|
---|
75 | */
|
---|
76 | char szDevice[sizeof(DEVICE_NAME) + 16];
|
---|
77 | for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
|
---|
78 | {
|
---|
79 | errno = 0;
|
---|
80 | RTStrPrintf(szDevice, sizeof(szDevice), DEVICE_NAME "%d", iUnit);
|
---|
81 | g_hDevice = open(szDevice, O_RDWR, 0);
|
---|
82 | if (g_hDevice >= 0 || errno != EBUSY)
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | if (g_hDevice < 0)
|
---|
86 | {
|
---|
87 | int rc;
|
---|
88 | switch (errno)
|
---|
89 | {
|
---|
90 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
91 | case EPERM:
|
---|
92 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
93 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
94 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
95 | }
|
---|
96 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Vrc\n", szDevice, errno, rc));
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Mark the file handle close on exec.
|
---|
102 | */
|
---|
103 | if (fcntl(g_hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
104 | {
|
---|
105 | int rc = errno;
|
---|
106 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d\n", rc));
|
---|
107 | close(g_hDevice);
|
---|
108 | g_hDevice = -1;
|
---|
109 | return RTErrConvertFromErrno(rc);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * We're done.
|
---|
114 | */
|
---|
115 | NOREF(cbReserve);
|
---|
116 | return VINF_SUCCESS;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | int suplibOsTerm(void)
|
---|
121 | {
|
---|
122 | /*
|
---|
123 | * Check if we're initited at all.
|
---|
124 | */
|
---|
125 | if (g_hDevice >= 0)
|
---|
126 | {
|
---|
127 | if (close(g_hDevice))
|
---|
128 | AssertFailed();
|
---|
129 | g_hDevice = -1;
|
---|
130 | }
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | int suplibOsInstall(void)
|
---|
136 | {
|
---|
137 | return VERR_NOT_IMPLEMENTED;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | int suplibOsUninstall(void)
|
---|
142 | {
|
---|
143 | return VERR_NOT_IMPLEMENTED;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | int suplibOsIOCtl(uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
148 | {
|
---|
149 | AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
|
---|
150 |
|
---|
151 | if (RT_LIKELY(ioctl(g_hDevice, uFunction, pvReq) >= 0))
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | return RTErrConvertFromErrno(errno);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | int suplibOsIOCtlFast(uintptr_t uFunction)
|
---|
158 | {
|
---|
159 | int rc = ioctl(g_hDevice, uFunction, NULL);
|
---|
160 | if (rc == -1)
|
---|
161 | rc = errno;
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | int suplibOsPageAlloc(size_t cPages, void **ppvPages)
|
---|
167 | {
|
---|
168 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
169 | if (*ppvPages)
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | return RTErrConvertFromErrno(errno);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | int suplibOsPageFree(void *pvPages, size_t /* cPages */)
|
---|
176 | {
|
---|
177 | RTMemPageFree(pvPages);
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|