VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/freebsd/SUPDrv-freebsd.c@ 3626

最後變更 在這個檔案從3626是 3626,由 vboxsync 提交於 18 年 前

FreeBSD kernel module (at the hello world stage).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 8.2 KB
 
1/* $Id: SUPDrv-freebsd.c 3626 2007-07-16 02:34:05Z 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*******************************************************************************/
60static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg);
61static int VBoxDrvFreeBSDLoad(void);
62static int VBoxDrvFreeBSDUnload(void);
63static d_fdopen_t VBoxDrvFreeBSDOpen;
64static d_close_t VBoxDrvFreeBSDClose;
65static d_ioctl_t VBoxDrvFreeBSDIOCtl;
66static int VBoxDrvFreeBsdErr2Native(int rc);
67
68
69/*******************************************************************************
70* Global Variables *
71*******************************************************************************/
72/**
73 * Module info structure used by the kernel.
74 */
75static moduledata_t g_VBoxDrvFreeBSDModule =
76{
77 "vboxdrv",
78 VBoxDrvFreeBSDModuleEvent,
79 NULL
80};
81
82/** Declare the module as a pseudo device. */
83DECLARE_MODULE(vboxdrv, g_VBoxDrvFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
84
85/**
86 * The /dev/vboxdrv character device entry points.
87 */
88static 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. */
99static struct cdev *g_pVBoxDrvFreeBSDChrDev;
100
101/** The device extention. */
102static SUPDRVDEVEXT g_DevExt;
103
104/** Spinlock protecting g_apSessionHashTab. */
105static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
106/** Hash table */
107static 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 */
123static 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
148static int VBoxDrvFreeBSDLoad(void)
149{
150 dprintf(("VBoxDrvFreeBSDLoad:\n"));
151
152 /*
153 * Initialize the runtime.
154 */
155 int rc = VINF_SUCCESS;/// @todo 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 /// @todo 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 }
189 else
190 printf("vboxdrv: RTSpinlockCreate failed, rc=%d\n", rc);
191 /// @todo supdrvDeleteDevExt(&g_DevExt);
192 }
193 else
194 printf("vboxdrv: supdrvInitDevExt failed, rc=%d\n", rc);
195 /// @todo RTR0Term();
196 }
197 else
198 printf("vboxdrv: RTR0Init failed, rc=%d\n", rc);
199 return rc;
200}
201
202static int VBoxDrvFreeBSDUnload(void)
203{
204 int rc;
205 dprintf(("VBoxDrvFreeBSDUnload:\n"));
206
207 /** @todo verify that FreeBSD does reference counting. */
208
209 /*
210 * Reserve what we did in VBoxDrvFreeBSDInit.
211 */
212 if (g_pVBoxDrvFreeBSDChrDev)
213 {
214 destroy_dev(g_pVBoxDrvFreeBSDChrDev);
215 g_pVBoxDrvFreeBSDChrDev = NULL;
216 }
217
218 rc = 0; /// @todo supdrvDeleteDevExt(&g_DevExt);
219 /// @todo AssertRC(rc);
220
221 /// @todo rc = RTSpinlockDestroy(g_Spinlock);
222 /// @todo AssertRC(rc);
223 g_Spinlock = NIL_RTSPINLOCK;
224
225 /// @todo RTR0Term();
226
227 memset(&g_DevExt, 0, sizeof(g_DevExt));
228
229 dprintf(("VBoxDrvFreeBSDUnload: returns\n"));
230 return VINF_SUCCESS;
231}
232
233
234static int VBoxDrvFreeBSDOpen(struct cdev *dev, int oflags, struct thread *td, int fdidx)
235{
236 dprintf(("VBoxDrvFreeBSDOpen:\n"));
237 return EOPNOTSUPP;
238}
239
240static int VBoxDrvFreeBSDClose(struct cdev *dev, int fflag, int devtype, struct thread *td)
241{
242 dprintf(("VBoxDrvFreeBSDClose:\n"));
243 return EBADF;
244}
245
246static int VBoxDrvFreeBSDIOCtl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
247{
248 dprintf(("VBoxDrvFreeBSDIOCtl:\n"));
249 return EINVAL;
250}
251
252
253/**
254 * Converts an supdrv error code to a FreeBSD error code.
255 *
256 * @returns corresponding FreeBSD error code.
257 * @param rc supdrv error code (SUPDRV_ERR_* defines).
258 */
259static int VBoxDrvFreeBsdErr2Native(int rc)
260{
261 switch (rc)
262 {
263 case 0: return 0;
264 case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
265 case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
266 case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
267 case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
268 case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
269 case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
270 case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
271 case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
272 case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
273 }
274
275 return EPERM;
276}
277
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette