1 | /* $Id: VBoxDev-haiku.c 43369 2012-09-20 13:09:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuest kernel driver, Haiku Guest Additions, implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * VirtualBox Guest Additions for Haiku.
|
---|
22 | * Copyright (c) 2011 Mike Smith <[email protected]>
|
---|
23 | * François Revol <[email protected]>
|
---|
24 | *
|
---|
25 | * Permission is hereby granted, free of charge, to any person
|
---|
26 | * obtaining a copy of this software and associated documentation
|
---|
27 | * files (the "Software"), to deal in the Software without
|
---|
28 | * restriction, including without limitation the rights to use,
|
---|
29 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
30 | * copies of the Software, and to permit persons to whom the
|
---|
31 | * Software is furnished to do so, subject to the following
|
---|
32 | * conditions:
|
---|
33 | *
|
---|
34 | * The above copyright notice and this permission notice shall be
|
---|
35 | * included in all copies or substantial portions of the Software.
|
---|
36 | *
|
---|
37 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
38 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
39 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
40 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
41 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
42 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
43 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
44 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
45 | */
|
---|
46 |
|
---|
47 |
|
---|
48 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
49 | //#undef LOG_DISABLED
|
---|
50 | //#define LOG_ENABLED
|
---|
51 | //#define LOG_ENABLE_FLOW
|
---|
52 | //#define DO_LOG
|
---|
53 | #include <sys/param.h>
|
---|
54 | #include <sys/types.h>
|
---|
55 | #include <sys/uio.h>
|
---|
56 | #include <OS.h>
|
---|
57 | #include <Drivers.h>
|
---|
58 | #include <KernelExport.h>
|
---|
59 | #include <PCI.h>
|
---|
60 |
|
---|
61 | #include "VBoxGuest-haiku.h"
|
---|
62 | #include "VBoxGuestInternal.h"
|
---|
63 | #include <VBox/log.h>
|
---|
64 | #include <iprt/assert.h>
|
---|
65 | #include <iprt/initterm.h>
|
---|
66 | #include <iprt/process.h>
|
---|
67 | #include <iprt/mem.h>
|
---|
68 | #include <iprt/asm.h>
|
---|
69 |
|
---|
70 | #define DRIVER_NAME "vboxdev"
|
---|
71 | #define DEVICE_NAME "misc/vboxguest"
|
---|
72 | #define MODULE_NAME "generic/vboxguest"
|
---|
73 |
|
---|
74 | static status_t VBoxGuestHaikuOpen(const char *name, uint32 flags, void **cookie);
|
---|
75 | static status_t VBoxGuestHaikuClose(void *cookie);
|
---|
76 | static status_t VBoxGuestHaikuFree(void *cookie);
|
---|
77 | static status_t VBoxGuestHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len);
|
---|
78 | static status_t VBoxGuestHaikuSelect(void *cookie, uint8 event, uint32 ref, selectsync *sync);
|
---|
79 | static status_t VBoxGuestHaikuDeselect(void *cookie, uint8 event, selectsync *sync);
|
---|
80 | static status_t VBoxGuestHaikuWrite(void *cookie, off_t position, const void *data, size_t *numBytes);
|
---|
81 | static status_t VBoxGuestHaikuRead(void *cookie, off_t position, void *data, size_t *numBytes);
|
---|
82 |
|
---|
83 | static device_hooks g_VBoxGuestHaikuDeviceHooks =
|
---|
84 | {
|
---|
85 | VBoxGuestHaikuOpen,
|
---|
86 | VBoxGuestHaikuClose,
|
---|
87 | VBoxGuestHaikuFree,
|
---|
88 | VBoxGuestHaikuIOCtl,
|
---|
89 | VBoxGuestHaikuRead,
|
---|
90 | VBoxGuestHaikuWrite,
|
---|
91 | VBoxGuestHaikuSelect,
|
---|
92 | VBoxGuestHaikuDeselect,
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * File open handler
|
---|
98 | *
|
---|
99 | */
|
---|
100 | static status_t VBoxGuestHaikuOpen(const char *name, uint32 flags, void **cookie)
|
---|
101 | {
|
---|
102 | int rc;
|
---|
103 | PVBOXGUESTSESSION pSession;
|
---|
104 |
|
---|
105 | LogFlow((DRIVER_NAME ":VBoxGuestHaikuOpen\n"));
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Create a new session.
|
---|
109 | */
|
---|
110 | rc = VBoxGuestCreateUserSession(&g_DevExt, &pSession);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | Log((DRIVER_NAME ":VBoxGuestHaikuOpen success: g_DevExt=%p pSession=%p rc=%d pid=%d\n",&g_DevExt, pSession, rc,(int)RTProcSelf()));
|
---|
114 | ASMAtomicIncU32(&cUsers);
|
---|
115 | *cookie = pSession;
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | LogRel((DRIVER_NAME ":VBoxGuestHaikuOpen: failed. rc=%d\n", rc));
|
---|
120 | return RTErrConvertToErrno(rc);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * File close handler
|
---|
126 | *
|
---|
127 | */
|
---|
128 | static status_t VBoxGuestHaikuClose(void *cookie)
|
---|
129 | {
|
---|
130 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
131 | Log(("VBoxGuestHaikuClose: pSession=%p\n", pSession));
|
---|
132 |
|
---|
133 | /** @todo r=ramshankar: should we really be using the session spinlock here? */
|
---|
134 | RTSpinlockAcquire(g_DevExt.SessionSpinlock);
|
---|
135 |
|
---|
136 | //XXX: we don't know if it belongs to this session !
|
---|
137 | if (sState.selectSync)
|
---|
138 | {
|
---|
139 | //dprintf(DRIVER_NAME "close: unblocking select %p %x\n", sState.selectSync, sState.selectEvent);
|
---|
140 | notify_select_event(sState.selectSync, sState.selectEvent);
|
---|
141 | sState.selectEvent = (uint8_t)0;
|
---|
142 | sState.selectRef = (uint32_t)0;
|
---|
143 | sState.selectSync = (void *)NULL;
|
---|
144 | }
|
---|
145 |
|
---|
146 | RTSpinlockRelease(g_DevExt.SessionSpinlock);
|
---|
147 |
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * File free handler
|
---|
154 | *
|
---|
155 | */
|
---|
156 | static status_t VBoxGuestHaikuFree(void *cookie)
|
---|
157 | {
|
---|
158 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
159 | Log(("VBoxGuestHaikuFree: pSession=%p\n", pSession));
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Close the session if it's still hanging on to the device...
|
---|
163 | */
|
---|
164 | if (VALID_PTR(pSession))
|
---|
165 | {
|
---|
166 | VBoxGuestCloseSession(&g_DevExt, pSession);
|
---|
167 | ASMAtomicDecU32(&cUsers);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | Log(("VBoxGuestHaikuFree: si_drv1=%p!\n", pSession));
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * IOCTL handler
|
---|
177 | *
|
---|
178 | */
|
---|
179 | static status_t VBoxGuestHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len)
|
---|
180 | {
|
---|
181 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
182 | //Log(("VBoxGuestHaikuFree: pSession=%p\n", pSession));
|
---|
183 | //LogFlow((DRIVER_NAME ":VBoxGuestHaikuIOCtl(, 0x%08x, %p, %d)\n", op, data, len));
|
---|
184 | Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl(, 0x%08x, %p, %d)\n", op, data, len));
|
---|
185 |
|
---|
186 | int rc = 0;
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Validate the input.
|
---|
190 | */
|
---|
191 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
192 | return EINVAL;
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Validate the request wrapper.
|
---|
196 | */
|
---|
197 | #if 0
|
---|
198 | if (IOCPARM_LEN(ulCmd) != sizeof(VBGLBIGREQ))
|
---|
199 | {
|
---|
200 | Log((DRIVER_NAME ": VBoxGuestHaikuIOCtl: bad request %lu size=%lu expected=%d\n", ulCmd, IOCPARM_LEN(ulCmd), sizeof(VBGLBIGREQ)));
|
---|
201 | return ENOTTY;
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | if (RT_UNLIKELY(len > _1M * 16))
|
---|
206 | {
|
---|
207 | dprintf(DRIVER_NAME ": VBoxGuestHaikuIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", (unsigned)len, data, op);
|
---|
208 | return EINVAL;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Read the request.
|
---|
213 | */
|
---|
214 | void *pvBuf = NULL;
|
---|
215 | if (RT_LIKELY(len > 0))
|
---|
216 | {
|
---|
217 | pvBuf = RTMemTmpAlloc(len);
|
---|
218 | if (RT_UNLIKELY(!pvBuf))
|
---|
219 | {
|
---|
220 | LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", len));
|
---|
221 | return ENOMEM;
|
---|
222 | }
|
---|
223 |
|
---|
224 | rc = user_memcpy(pvBuf, data, len);
|
---|
225 | if (RT_UNLIKELY(rc < 0))
|
---|
226 | {
|
---|
227 | RTMemTmpFree(pvBuf);
|
---|
228 | LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: user_memcpy failed; pvBuf=%p data=%p op=%d. rc=%d\n", pvBuf, data, op, rc));
|
---|
229 | return EFAULT;
|
---|
230 | }
|
---|
231 | if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
|
---|
232 | {
|
---|
233 | RTMemTmpFree(pvBuf);
|
---|
234 | LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: pvBuf invalid pointer %p\n", pvBuf));
|
---|
235 | return EINVAL;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: pSession=%p pid=%d.\n", pSession,(int)RTProcSelf()));
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Process the IOCtl.
|
---|
242 | */
|
---|
243 | size_t cbDataReturned;
|
---|
244 | rc = VBoxGuestCommonIOCtl(op, &g_DevExt, pSession, pvBuf, len, &cbDataReturned);
|
---|
245 | if (RT_SUCCESS(rc))
|
---|
246 | {
|
---|
247 | rc = 0;
|
---|
248 | if (RT_UNLIKELY(cbDataReturned > len))
|
---|
249 | {
|
---|
250 | Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: too much output data %d expected %d\n", cbDataReturned, len));
|
---|
251 | cbDataReturned = len;
|
---|
252 | }
|
---|
253 | if (cbDataReturned > 0)
|
---|
254 | {
|
---|
255 | rc = user_memcpy(data, pvBuf, cbDataReturned);
|
---|
256 | if (RT_UNLIKELY(rc < 0))
|
---|
257 | {
|
---|
258 | Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: user_memcpy failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, data, op, rc));
|
---|
259 | rc = EFAULT;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | else
|
---|
264 | {
|
---|
265 | Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: VBoxGuestCommonIOCtl failed. rc=%d\n", rc));
|
---|
266 | rc = EFAULT;
|
---|
267 | }
|
---|
268 | RTMemTmpFree(pvBuf);
|
---|
269 | return rc;
|
---|
270 | #if 0
|
---|
271 | #endif
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | static status_t VBoxGuestHaikuSelect(void *cookie, uint8 event, uint32 ref, selectsync *sync)
|
---|
276 | {
|
---|
277 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
278 | status_t err = B_OK;
|
---|
279 | //dprintf(DRIVER_NAME "select(,%d,%p)\n", event, sync);
|
---|
280 |
|
---|
281 |
|
---|
282 | switch (event)
|
---|
283 | {
|
---|
284 | case B_SELECT_READ:
|
---|
285 | //case B_SELECT_ERROR:
|
---|
286 | break;
|
---|
287 | default:
|
---|
288 | return EINVAL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | RTSpinlockAcquire(g_DevExt.SessionSpinlock);
|
---|
292 |
|
---|
293 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
294 | if (pSession->u32MousePosChangedSeq != u32CurSeq)
|
---|
295 | {
|
---|
296 | //dprintf(DRIVER_NAME "select: notifying now: %p %x\n", sync, event);
|
---|
297 | pSession->u32MousePosChangedSeq = u32CurSeq;
|
---|
298 | notify_select_event(sync, event);
|
---|
299 | }
|
---|
300 | else if (sState.selectSync == NULL)
|
---|
301 | {
|
---|
302 | //dprintf(DRIVER_NAME "select: caching: %p %x\n", sync, event);
|
---|
303 | sState.selectEvent = (uint8_t)event;
|
---|
304 | sState.selectRef = (uint32_t)ref;
|
---|
305 | sState.selectSync = (void *)sync;
|
---|
306 | }
|
---|
307 | else
|
---|
308 | {
|
---|
309 | //dprintf(DRIVER_NAME "select: dropping: %p %x\n", sync, event);
|
---|
310 | err = B_WOULD_BLOCK;
|
---|
311 | }
|
---|
312 |
|
---|
313 | RTSpinlockRelease(g_DevExt.SessionSpinlock);
|
---|
314 |
|
---|
315 | return err;
|
---|
316 | #if 0
|
---|
317 | int fEventsProcessed;
|
---|
318 |
|
---|
319 | LogFlow((DRIVER_NAME "::Poll: fEvents=%d\n", fEvents));
|
---|
320 |
|
---|
321 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
322 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
323 | {
|
---|
324 | Log((DRIVER_NAME "::Poll: no state data for %s\n", devtoname(pDev)));
|
---|
325 | return (fEvents & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
|
---|
326 | }
|
---|
327 |
|
---|
328 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
329 | if (pSession->u32MousePosChangedSeq != u32CurSeq)
|
---|
330 | {
|
---|
331 | fEventsProcessed = fEvents & (POLLIN | POLLRDNORM);
|
---|
332 | pSession->u32MousePosChangedSeq = u32CurSeq;
|
---|
333 | }
|
---|
334 | else
|
---|
335 | {
|
---|
336 | fEventsProcessed = 0;
|
---|
337 |
|
---|
338 | selrecord(td, &g_SelInfo);
|
---|
339 | }
|
---|
340 |
|
---|
341 | return fEventsProcessed;
|
---|
342 | #endif
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | static status_t VBoxGuestHaikuDeselect(void *cookie, uint8 event, selectsync *sync)
|
---|
347 | {
|
---|
348 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
349 | status_t err = B_OK;
|
---|
350 | //dprintf(DRIVER_NAME "deselect(,%d,%p)\n", event, sync);
|
---|
351 |
|
---|
352 | RTSpinlockAcquire(g_DevExt.SessionSpinlock);
|
---|
353 |
|
---|
354 | if (sState.selectSync == sync)
|
---|
355 | {
|
---|
356 | //dprintf(DRIVER_NAME "deselect: dropping: %p %x\n", sState.selectSync, sState.selectEvent);
|
---|
357 | sState.selectEvent = (uint8_t)0;
|
---|
358 | sState.selectRef = (uint32_t)0;
|
---|
359 | sState.selectSync = NULL;
|
---|
360 | }
|
---|
361 | else
|
---|
362 | err = B_OK;
|
---|
363 |
|
---|
364 | RTSpinlockRelease(g_DevExt.SessionSpinlock);
|
---|
365 | return err;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | static status_t VBoxGuestHaikuWrite(void *cookie, off_t position, const void *data, size_t *numBytes)
|
---|
370 | {
|
---|
371 | *numBytes = 0;
|
---|
372 | return 0;
|
---|
373 | }
|
---|
374 |
|
---|
375 |
|
---|
376 | static status_t VBoxGuestHaikuRead(void *cookie, off_t position, void *data, size_t *numBytes)
|
---|
377 | {
|
---|
378 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
|
---|
379 |
|
---|
380 | //dprintf(DRIVER_NAME "read(,,%d)\n", *numBytes);
|
---|
381 |
|
---|
382 | if (*numBytes == 0)
|
---|
383 | return 0;
|
---|
384 |
|
---|
385 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
386 | if (pSession->u32MousePosChangedSeq != u32CurSeq)
|
---|
387 | {
|
---|
388 | pSession->u32MousePosChangedSeq = u32CurSeq;
|
---|
389 | //dprintf(DRIVER_NAME "read: giving 1 byte\n");
|
---|
390 | *numBytes = 1;
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | *numBytes = 0;
|
---|
395 | return 0;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | int32 api_version = B_CUR_DRIVER_API_VERSION;
|
---|
400 |
|
---|
401 | status_t init_hardware()
|
---|
402 | {
|
---|
403 | return get_module(MODULE_NAME, (module_info **)&g_VBoxGuest);
|
---|
404 | }
|
---|
405 |
|
---|
406 | status_t init_driver()
|
---|
407 | {
|
---|
408 | return B_OK;
|
---|
409 | }
|
---|
410 |
|
---|
411 | device_hooks* find_device(const char *name)
|
---|
412 | {
|
---|
413 | static device_hooks g_VBoxGuestHaikuDeviceHooks =
|
---|
414 | {
|
---|
415 | VBoxGuestHaikuOpen,
|
---|
416 | VBoxGuestHaikuClose,
|
---|
417 | VBoxGuestHaikuFree,
|
---|
418 | VBoxGuestHaikuIOCtl,
|
---|
419 | VBoxGuestHaikuRead,
|
---|
420 | VBoxGuestHaikuWrite,
|
---|
421 | VBoxGuestHaikuSelect,
|
---|
422 | VBoxGuestHaikuDeselect
|
---|
423 | };
|
---|
424 | return &g_VBoxGuestHaikuDeviceHooks;
|
---|
425 | }
|
---|
426 |
|
---|
427 | const char** publish_devices()
|
---|
428 | {
|
---|
429 | static const char *devices[] = { DEVICE_NAME, NULL };
|
---|
430 | return devices;
|
---|
431 | }
|
---|
432 |
|
---|
433 | void uninit_driver()
|
---|
434 | {
|
---|
435 | put_module(MODULE_NAME);
|
---|
436 | }
|
---|
437 |
|
---|