1 | /** @file
|
---|
2 | * SUPLib - FreeBSD Hosts,
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
26 | #include <VBox/types.h>
|
---|
27 | #include <VBox/sup.h>
|
---|
28 | #include <VBox/param.h>
|
---|
29 | #include <VBox/err.h>
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/mem.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include "SUPLibInternal.h"
|
---|
37 | #include "SUPDRVIOC.h"
|
---|
38 |
|
---|
39 | #include <sys/fcntl.h>
|
---|
40 | #include <sys/ioctl.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <unistd.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** BSD Device name. */
|
---|
50 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*******************************************************************************
|
---|
54 | * Global Variables *
|
---|
55 | *******************************************************************************/
|
---|
56 | /** Handle to the open device. */
|
---|
57 | static int g_hDevice = -1;
|
---|
58 |
|
---|
59 |
|
---|
60 | int suplibOsInit(size_t cbReserve)
|
---|
61 | {
|
---|
62 | /*
|
---|
63 | * Check if already initialized.
|
---|
64 | */
|
---|
65 | if (g_hDevice >= 0)
|
---|
66 | return VINF_SUCCESS;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Try open the BSD device.
|
---|
70 | */
|
---|
71 | g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
|
---|
72 | if (g_hDevice < 0)
|
---|
73 | {
|
---|
74 | int rc = errno;
|
---|
75 | LogRel(("Failed to open \"%s\", errno=%d\n", rc));
|
---|
76 | return RTErrConvertFromErrno(rc);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * We're done.
|
---|
81 | */
|
---|
82 | NOREF(cbReserve);
|
---|
83 | return VINF_SUCCESS;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | int suplibOsTerm(void)
|
---|
88 | {
|
---|
89 | /*
|
---|
90 | * Check if we're initited at all.
|
---|
91 | */
|
---|
92 | if (g_hDevice >= 0)
|
---|
93 | {
|
---|
94 | if (close(g_hDevice))
|
---|
95 | AssertFailed();
|
---|
96 | g_hDevice = -1;
|
---|
97 | }
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Installs anything required by the support library.
|
---|
104 | *
|
---|
105 | * @returns 0 on success.
|
---|
106 | * @returns error code on failure.
|
---|
107 | */
|
---|
108 | int suplibOsInstall(void)
|
---|
109 | {
|
---|
110 | // int rc = mknod(DEVICE_NAME, S_IFCHR, );
|
---|
111 |
|
---|
112 | return VERR_NOT_IMPLEMENTED;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Installs anything required by the support library.
|
---|
118 | *
|
---|
119 | * @returns 0 on success.
|
---|
120 | * @returns error code on failure.
|
---|
121 | */
|
---|
122 | int suplibOsUninstall(void)
|
---|
123 | {
|
---|
124 | // int rc = unlink(DEVICE_NAME);
|
---|
125 |
|
---|
126 | return VERR_NOT_IMPLEMENTED;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Send a I/O Control request to the device.
|
---|
132 | *
|
---|
133 | * @returns 0 on success.
|
---|
134 | * @returns VBOX error code on failure.
|
---|
135 | * @param uFunction IO Control function.
|
---|
136 | * @param pvIn Input data buffer.
|
---|
137 | * @param cbIn Size of input data.
|
---|
138 | * @param pvOut Output data buffer.
|
---|
139 | * @param cbOut Size of output data.
|
---|
140 | */
|
---|
141 | int suplibOsIOCtl(unsigned uFunction, void *pvIn, size_t cbIn, void *pvOut, size_t cbOut)
|
---|
142 | {
|
---|
143 | AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
|
---|
144 | /*
|
---|
145 | * Issue device iocontrol.
|
---|
146 | */
|
---|
147 | SUPDRVIOCTLDATA Args;
|
---|
148 | Args.pvIn = pvIn;
|
---|
149 | Args.cbIn = cbIn;
|
---|
150 | Args.pvOut = pvOut;
|
---|
151 | Args.cbOut = cbOut;
|
---|
152 |
|
---|
153 | if (ioctl(g_hDevice, uFunction, &Args) >= 0)
|
---|
154 | return 0;
|
---|
155 | /* This is the reverse operation of the one found in SUPDrv-linux.c */
|
---|
156 | switch (errno)
|
---|
157 | {
|
---|
158 | case EACCES: return VERR_GENERAL_FAILURE;
|
---|
159 | case EINVAL: return VERR_INVALID_PARAMETER;
|
---|
160 | case ENOSYS: return VERR_INVALID_MAGIC;
|
---|
161 | case ENXIO: return VERR_INVALID_HANDLE;
|
---|
162 | case EFAULT: return VERR_INVALID_POINTER;
|
---|
163 | case ENOLCK: return VERR_LOCK_FAILED;
|
---|
164 | case EEXIST: return VERR_ALREADY_LOADED;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return RTErrConvertFromErrno(errno);
|
---|
168 | }
|
---|
169 |
|
---|
170 | #ifdef VBOX_WITHOUT_IDT_PATCHING
|
---|
171 | int suplibOSIOCtlFast(unsigned uFunction)
|
---|
172 | {
|
---|
173 | int rc = ioctl(g_hDevice, uFunction, NULL);
|
---|
174 | if (rc == -1)
|
---|
175 | rc = errno;
|
---|
176 | return rc;
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Allocate a number of zero-filled pages in user space.
|
---|
183 | *
|
---|
184 | * @returns VBox status code.
|
---|
185 | * @param cPages Number of pages to allocate.
|
---|
186 | * @param ppvPages Where to return the base pointer.
|
---|
187 | */
|
---|
188 | int suplibOsPageAlloc(size_t cPages, void **ppvPages)
|
---|
189 | {
|
---|
190 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
191 | if (*ppvPages)
|
---|
192 | return VINF_SUCCESS;
|
---|
193 | return RTErrConvertFromErrno(errno);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Frees pages allocated by suplibOsPageAlloc().
|
---|
199 | *
|
---|
200 | * @returns VBox status code.
|
---|
201 | * @param pvPages Pointer to pages.
|
---|
202 | */
|
---|
203 | int suplibOsPageFree(void *pvPages, size_t /* cPages */)
|
---|
204 | {
|
---|
205 | RTMemPageFree(pvPages);
|
---|
206 | return VINF_SUCCESS;
|
---|
207 | }
|
---|
208 |
|
---|