1 | /* $Id: SUPDrv-freebsd.c 3677 2007-07-18 04:30:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - FreeBSD specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | /* Deal with conflicts first. */
|
---|
36 | #include <sys/param.h>
|
---|
37 | #undef PVM
|
---|
38 | #include <sys/types.h>
|
---|
39 | #include <sys/module.h>
|
---|
40 | #include <sys/systm.h>
|
---|
41 | #include <sys/errno.h>
|
---|
42 | #include <sys/kernel.h>
|
---|
43 | #include <sys/conf.h>
|
---|
44 | #include <sys/uio.h>
|
---|
45 |
|
---|
46 | #include "SUPDRV.h"
|
---|
47 | #include <VBox/version.h>
|
---|
48 | #include <iprt/initterm.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/spinlock.h>
|
---|
51 | #include <iprt/process.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/log.h>
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Internal Functions *
|
---|
59 | *******************************************************************************/
|
---|
60 | static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg);
|
---|
61 | static int VBoxDrvFreeBSDLoad(void);
|
---|
62 | static int VBoxDrvFreeBSDUnload(void);
|
---|
63 | static d_fdopen_t VBoxDrvFreeBSDOpen;
|
---|
64 | static d_close_t VBoxDrvFreeBSDClose;
|
---|
65 | static d_ioctl_t VBoxDrvFreeBSDIOCtl;
|
---|
66 | static int VBoxDrvFreeBsdErr2Native(int rc);
|
---|
67 |
|
---|
68 |
|
---|
69 | /*******************************************************************************
|
---|
70 | * Global Variables *
|
---|
71 | *******************************************************************************/
|
---|
72 | /**
|
---|
73 | * Module info structure used by the kernel.
|
---|
74 | */
|
---|
75 | static moduledata_t g_VBoxDrvFreeBSDModule =
|
---|
76 | {
|
---|
77 | "vboxdrv",
|
---|
78 | VBoxDrvFreeBSDModuleEvent,
|
---|
79 | NULL
|
---|
80 | };
|
---|
81 |
|
---|
82 | /** Declare the module as a pseudo device. */
|
---|
83 | DECLARE_MODULE(vboxdrv, g_VBoxDrvFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * The /dev/vboxdrv character device entry points.
|
---|
87 | */
|
---|
88 | static struct cdevsw g_VBoxDrvFreeBSDChrDevSW =
|
---|
89 | {
|
---|
90 | .d_version = D_VERSION,
|
---|
91 | .d_flags = D_TRACKCLOSE,
|
---|
92 | .d_fdopen = VBoxDrvFreeBSDOpen,
|
---|
93 | .d_close = VBoxDrvFreeBSDClose,
|
---|
94 | .d_ioctl = VBoxDrvFreeBSDIOCtl,
|
---|
95 | .d_name = "vboxdrv"
|
---|
96 | };
|
---|
97 |
|
---|
98 | /** The make_dev result. */
|
---|
99 | static struct cdev *g_pVBoxDrvFreeBSDChrDev;
|
---|
100 |
|
---|
101 | /** The device extention. */
|
---|
102 | static SUPDRVDEVEXT g_DevExt;
|
---|
103 |
|
---|
104 | /** Spinlock protecting g_apSessionHashTab. */
|
---|
105 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
106 | /** Hash table */
|
---|
107 | static PSUPDRVSESSION g_apSessionHashTab[19];
|
---|
108 | /** Calculates the index into g_apSessionHashTab.*/
|
---|
109 | #define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Module event handler.
|
---|
116 | *
|
---|
117 | * @param pMod The module structure.
|
---|
118 | * @param enmEventType The event type (modeventtype_t).
|
---|
119 | * @param pvArg Module argument. NULL.
|
---|
120 | *
|
---|
121 | * @return 0 on success, errno.h status code on failure.
|
---|
122 | */
|
---|
123 | static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg)
|
---|
124 | {
|
---|
125 | int rc;
|
---|
126 | switch (enmEventType)
|
---|
127 | {
|
---|
128 | case MOD_LOAD:
|
---|
129 | rc = VBoxDrvFreeBSDLoad();
|
---|
130 | break;
|
---|
131 |
|
---|
132 | case MOD_UNLOAD:
|
---|
133 | rc = VBoxDrvFreeBSDUnload();
|
---|
134 | break;
|
---|
135 |
|
---|
136 | case MOD_SHUTDOWN:
|
---|
137 | case MOD_QUIESCE:
|
---|
138 | default:
|
---|
139 | return EOPNOTSUPP;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (RT_SUCCESS(rc))
|
---|
143 | return 0;
|
---|
144 | return VBoxDrvFreeBsdErr2Native(rc);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | static int VBoxDrvFreeBSDLoad(void)
|
---|
149 | {
|
---|
150 | dprintf(("VBoxDrvFreeBSDLoad:\n"));
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Initialize the runtime.
|
---|
154 | */
|
---|
155 | int rc = RTR0Init(0);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * Initialize the device extension.
|
---|
160 | */
|
---|
161 | /// @todo rc = supdrvInitDevExt(&g_DevExt);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Initialize the session hash table.
|
---|
166 | */
|
---|
167 | rc = RTSpinlockCreate(&g_Spinlock);
|
---|
168 | if (RT_SUCCESS(rc))
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Create our device node.
|
---|
172 | */
|
---|
173 | /** @todo find a way to fix this 0666 permission issue. Perhaps by defining some vboxusers group with a fixed gid? */
|
---|
174 | g_pVBoxDrvFreeBSDChrDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW,
|
---|
175 | 0,
|
---|
176 | UID_ROOT,
|
---|
177 | GID_WHEEL,
|
---|
178 | 0666,
|
---|
179 | "vboxdrv");
|
---|
180 | if (g_pVBoxDrvFreeBSDChrDev)
|
---|
181 | {
|
---|
182 | dprintf(("VBoxDrvFreeBSDLoad: returns successfully\n"));
|
---|
183 | return VINF_SUCCESS;
|
---|
184 | }
|
---|
185 |
|
---|
186 | printf("vboxdrv: make_dev failed\n");
|
---|
187 | rc = SUPDRV_ERR_ALREADY_LOADED;
|
---|
188 | RTSpinlockDestroy(g_Spinlock);
|
---|
189 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
190 | }
|
---|
191 | else
|
---|
192 | printf("vboxdrv: RTSpinlockCreate failed, rc=%d\n", rc);
|
---|
193 | /// @todo supdrvDeleteDevExt(&g_DevExt);
|
---|
194 | }
|
---|
195 | else
|
---|
196 | printf("vboxdrv: supdrvInitDevExt failed, rc=%d\n", rc);
|
---|
197 | RTR0Term();
|
---|
198 | }
|
---|
199 | else
|
---|
200 | printf("vboxdrv: RTR0Init failed, rc=%d\n", rc);
|
---|
201 | return rc;
|
---|
202 | }
|
---|
203 |
|
---|
204 | static int VBoxDrvFreeBSDUnload(void)
|
---|
205 | {
|
---|
206 | int rc;
|
---|
207 | dprintf(("VBoxDrvFreeBSDUnload:\n"));
|
---|
208 |
|
---|
209 | /** @todo verify that FreeBSD does reference counting. */
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Reserve what we did in VBoxDrvFreeBSDInit.
|
---|
213 | */
|
---|
214 | if (g_pVBoxDrvFreeBSDChrDev)
|
---|
215 | {
|
---|
216 | destroy_dev(g_pVBoxDrvFreeBSDChrDev);
|
---|
217 | g_pVBoxDrvFreeBSDChrDev = NULL;
|
---|
218 | }
|
---|
219 |
|
---|
220 | rc = 0; /// @todo supdrvDeleteDevExt(&g_DevExt);
|
---|
221 | AssertRC(rc);
|
---|
222 |
|
---|
223 | rc = RTSpinlockDestroy(g_Spinlock);
|
---|
224 | AssertRC(rc);
|
---|
225 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
226 |
|
---|
227 | RTR0Term();
|
---|
228 |
|
---|
229 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
230 |
|
---|
231 | dprintf(("VBoxDrvFreeBSDUnload: returns\n"));
|
---|
232 | return VINF_SUCCESS;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | static int VBoxDrvFreeBSDOpen(struct cdev *dev, int oflags, struct thread *td, int fdidx)
|
---|
237 | {
|
---|
238 | dprintf(("VBoxDrvFreeBSDOpen:\n"));
|
---|
239 | return EOPNOTSUPP;
|
---|
240 | }
|
---|
241 |
|
---|
242 | static int VBoxDrvFreeBSDClose(struct cdev *dev, int fflag, int devtype, struct thread *td)
|
---|
243 | {
|
---|
244 | dprintf(("VBoxDrvFreeBSDClose:\n"));
|
---|
245 | return EBADF;
|
---|
246 | }
|
---|
247 |
|
---|
248 | static int VBoxDrvFreeBSDIOCtl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
|
---|
249 | {
|
---|
250 | dprintf(("VBoxDrvFreeBSDIOCtl:\n"));
|
---|
251 | return EINVAL;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Converts an supdrv error code to a FreeBSD error code.
|
---|
257 | *
|
---|
258 | * @returns corresponding FreeBSD error code.
|
---|
259 | * @param rc supdrv error code (SUPDRV_ERR_* defines).
|
---|
260 | */
|
---|
261 | static int VBoxDrvFreeBsdErr2Native(int rc)
|
---|
262 | {
|
---|
263 | switch (rc)
|
---|
264 | {
|
---|
265 | case 0: return 0;
|
---|
266 | case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
|
---|
267 | case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
|
---|
268 | case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
|
---|
269 | case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
|
---|
270 | case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
|
---|
271 | case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
|
---|
272 | case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
|
---|
273 | case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
|
---|
274 | case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
|
---|
275 | }
|
---|
276 |
|
---|
277 | return EPERM;
|
---|
278 | }
|
---|
279 |
|
---|