1 | /* $Id: SUPDrv-os2.cpp 4826 2007-09-15 14:06:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - OS/2 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 | #define __STDC_CONSTANT_MACROS
|
---|
36 | #define __STDC_LIMIT_MACROS
|
---|
37 |
|
---|
38 | #include <os2ddk/bsekee.h>
|
---|
39 | #undef RT_MAX
|
---|
40 |
|
---|
41 | #include "SUPDRV.h"
|
---|
42 | #include <VBox/version.h>
|
---|
43 | #include <iprt/initterm.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/spinlock.h>
|
---|
46 | #include <iprt/process.h>
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/log.h>
|
---|
49 | #include <iprt/param.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | /*******************************************************************************
|
---|
53 | * Global Variables *
|
---|
54 | *******************************************************************************/
|
---|
55 | /**
|
---|
56 | * Device extention & session data association structure.
|
---|
57 | */
|
---|
58 | static SUPDRVDEVEXT g_DevExt;
|
---|
59 | /** Spinlock protecting g_apSessionHashTab. */
|
---|
60 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
61 | /** Hash table */
|
---|
62 | static PSUPDRVSESSION g_apSessionHashTab[19];
|
---|
63 | /** Calculates the index into g_apSessionHashTab.*/
|
---|
64 | #define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
|
---|
65 |
|
---|
66 | __BEGIN_DECLS
|
---|
67 | /* Defined in SUPDrvA-os2.asm */
|
---|
68 | extern uint16_t g_offLogHead;
|
---|
69 | extern uint16_t volatile g_offLogTail;
|
---|
70 | extern uint16_t const g_cchLogMax;
|
---|
71 | extern char g_szLog[];
|
---|
72 | /* (init only:) */
|
---|
73 | extern char g_szInitText[];
|
---|
74 | extern uint16_t g_cchInitText;
|
---|
75 | extern uint16_t g_cchInitTextMax;
|
---|
76 | __END_DECLS
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Internal Functions *
|
---|
81 | *******************************************************************************/
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * 32-bit Ring-0 initialization.
|
---|
87 | *
|
---|
88 | * @returns 0 on success, non-zero on failure.
|
---|
89 | * @param pszArgs Pointer to the device arguments.
|
---|
90 | */
|
---|
91 | DECLASM(int) VBoxDrvInit(const char *pszArgs)
|
---|
92 | {
|
---|
93 | dprintf(("VBoxDrvInit: pszArgs=%s\n", pszArgs));
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Initialize the runtime.
|
---|
97 | */
|
---|
98 | int rc = RTR0Init(0);
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | /*
|
---|
102 | * Initialize the device extension.
|
---|
103 | */
|
---|
104 | rc = supdrvInitDevExt(&g_DevExt);
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | /*
|
---|
108 | * Initialize the session hash table.
|
---|
109 | */
|
---|
110 | rc = RTSpinlockCreate(&g_Spinlock);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Process the commandline. Later.
|
---|
115 | */
|
---|
116 | bool fVerbose = true;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Success
|
---|
120 | */
|
---|
121 | if (fVerbose)
|
---|
122 | {
|
---|
123 | strcpy(&g_szInitText[0],
|
---|
124 | "\r\n"
|
---|
125 | "VirtualBox.org Support Driver for OS/2 version " VBOX_VERSION_STRING "\r\n"
|
---|
126 | "Copyright (C) 2007 Knut St. Osmundsen\r\n"
|
---|
127 | "Copyright (C) 2007 innotek GmbH\r\n");
|
---|
128 | g_cchInitText = strlen(&g_szInitText[0]);
|
---|
129 | }
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 | g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: RTSpinlockCreate failed, rc=%Vrc\n", rc);
|
---|
133 | supdrvDeleteDevExt(&g_DevExt);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: supdrvInitDevExt failed, rc=%Vrc\n", rc);
|
---|
137 | RTR0Term();
|
---|
138 | }
|
---|
139 | else
|
---|
140 | g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: RTR0Init failed, rc=%Vrc\n", rc);
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | DECLASM(int) VBoxDrvOpen(uint16_t sfn)
|
---|
146 | {
|
---|
147 | int rc;
|
---|
148 | PSUPDRVSESSION pSession;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Create a new session.
|
---|
152 | */
|
---|
153 | rc = supdrvCreateSession(&g_DevExt, &pSession);
|
---|
154 | if (RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | pSession->Process = RTProcSelf();
|
---|
157 | pSession->R0Process = RTR0ProcHandleSelf();
|
---|
158 | pSession->sfn = sfn;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Insert it into the hash table.
|
---|
162 | */
|
---|
163 | unsigned iHash = SESSION_HASH(sfn);
|
---|
164 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
165 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
166 | pSession->pNextHash = g_apSessionHashTab[iHash];
|
---|
167 | g_apSessionHashTab[iHash] = pSession;
|
---|
168 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
169 | }
|
---|
170 |
|
---|
171 | dprintf(("VBoxDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | DECLASM(int) VBoxDrvClose(uint16_t sfn)
|
---|
177 | {
|
---|
178 | dprintf(("VBoxDrvClose: pid=%d sfn=%d\n", (int)RTProcSelf(), sfn));
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Remove from the hash table.
|
---|
182 | */
|
---|
183 | PSUPDRVSESSION pSession;
|
---|
184 | const RTPROCESS Process = RTProcSelf();
|
---|
185 | const unsigned iHash = SESSION_HASH(sfn);
|
---|
186 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
187 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
188 |
|
---|
189 | pSession = g_apSessionHashTab[iHash];
|
---|
190 | if (pSession)
|
---|
191 | {
|
---|
192 | if ( pSession->sfn == sfn
|
---|
193 | && pSession->Process == Process)
|
---|
194 | {
|
---|
195 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
196 | pSession->pNextHash = NULL;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | PSUPDRVSESSION pPrev = pSession;
|
---|
201 | pSession = pSession->pNextHash;
|
---|
202 | while (pSession)
|
---|
203 | {
|
---|
204 | if ( pSession->sfn == sfn
|
---|
205 | && pSession->Process == Process)
|
---|
206 | {
|
---|
207 | pPrev->pNextHash = pSession->pNextHash;
|
---|
208 | pSession->pNextHash = NULL;
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* next */
|
---|
213 | pPrev = pSession;
|
---|
214 | pSession = pSession->pNextHash;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
219 | if (!pSession)
|
---|
220 | {
|
---|
221 | OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d sfn=%d\n", (int)Process, sfn));
|
---|
222 | return VERR_INVALID_PARAMETER;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Close the session.
|
---|
227 | */
|
---|
228 | supdrvCloseSession(&g_DevExt, pSession);
|
---|
229 | return 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | DECLASM(int) VBoxDrvIOCtlFast(uint16_t sfn, uint8_t iFunction, int32_t *prc)
|
---|
234 | {
|
---|
235 | /*
|
---|
236 | * Find the session.
|
---|
237 | */
|
---|
238 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
239 | const RTPROCESS Process = RTProcSelf();
|
---|
240 | const unsigned iHash = SESSION_HASH(sfn);
|
---|
241 | PSUPDRVSESSION pSession;
|
---|
242 |
|
---|
243 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
244 | pSession = g_apSessionHashTab[iHash];
|
---|
245 | if (pSession && pSession->Process != Process)
|
---|
246 | {
|
---|
247 | do pSession = pSession->pNextHash;
|
---|
248 | while ( pSession
|
---|
249 | && ( pSession->sfn != sfn
|
---|
250 | || pSession->Process != Process));
|
---|
251 | }
|
---|
252 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
253 | if (RT_UNLIKELY(!pSession))
|
---|
254 | {
|
---|
255 | OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
|
---|
256 | return VERR_INVALID_PARAMETER;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Dispatch the fast IOCtl.
|
---|
261 | */
|
---|
262 | *prc = supdrvIOCtlFast(iFunction, &g_DevExt, pSession);
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | DECLASM(int) VBoxDrvIOCtl(uint16_t sfn, uint8_t iCat, uint8_t iFunction, void *pvParm, void *pvData, uint16_t *pcbParm, uint16_t *pcbData)
|
---|
268 | {
|
---|
269 | /*
|
---|
270 | * Find the session.
|
---|
271 | */
|
---|
272 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
273 | const RTPROCESS Process = RTProcSelf();
|
---|
274 | const unsigned iHash = SESSION_HASH(sfn);
|
---|
275 | PSUPDRVSESSION pSession;
|
---|
276 |
|
---|
277 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
278 | pSession = g_apSessionHashTab[iHash];
|
---|
279 | if (pSession && pSession->Process != Process)
|
---|
280 | {
|
---|
281 | do pSession = pSession->pNextHash;
|
---|
282 | while ( pSession
|
---|
283 | && ( pSession->sfn != sfn
|
---|
284 | || pSession->Process != Process));
|
---|
285 | }
|
---|
286 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
287 | if (!pSession)
|
---|
288 | {
|
---|
289 | OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
|
---|
290 | return VERR_INVALID_PARAMETER;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Verify the category and dispatch the IOCtl.
|
---|
295 | */
|
---|
296 | if (RT_LIKELY(iCat == SUP_CTL_CATEGORY))
|
---|
297 | {
|
---|
298 | dprintf(("VBoxDrvIOCtl: pSession=%p iFunction=%#x pvParm=%p pvData=%p *pcbParm=%d *pcbData=%d\n", pSession, iFunction, pvParm, pvData, *pcbParm, *pcbData));
|
---|
299 | Assert(pvParm);
|
---|
300 | Assert(!pvData);
|
---|
301 |
|
---|
302 | /*
|
---|
303 | * Lock the header.
|
---|
304 | */
|
---|
305 | PSUPREQHDR pHdr = (PSUPREQHDR)pvParm;
|
---|
306 | AssertReturn(*pcbParm == sizeof(*pHdr), VERR_INVALID_PARAMETER);
|
---|
307 | KernVMLock_t Lock;
|
---|
308 | int rc = KernVMLock(VMDHL_WRITE, pHdr, *pcbParm, &Lock, (KernPageList_t *)-1, NULL);
|
---|
309 | AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, *pcbParm, &Lock, rc), VERR_LOCK_FAILED);
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * Validate the header.
|
---|
313 | */
|
---|
314 | if (RT_LIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) == SUPREQHDR_FLAGS_MAGIC))
|
---|
315 | {
|
---|
316 | uint32_t cbReq = RT_MAX(pHdr->cbIn, pHdr->cbOut);
|
---|
317 | if (RT_LIKELY( pHdr->cbIn >= sizeof(*pHdr)
|
---|
318 | && pHdr->cbOut >= sizeof(*pHdr)
|
---|
319 | && cbReq <= _1M*16))
|
---|
320 | {
|
---|
321 | /*
|
---|
322 | * Lock the rest of the buffer if necessary.
|
---|
323 | */
|
---|
324 | if (((uintptr_t)pHdr & PAGE_OFFSET_MASK) + cbReq > PAGE_SIZE)
|
---|
325 | {
|
---|
326 | rc = KernVMUnlock(&Lock);
|
---|
327 | AssertMsgReturn(!rc, ("KernVMUnlock(Lock) -> %#x\n", rc), VERR_LOCK_FAILED);
|
---|
328 |
|
---|
329 | rc = KernVMLock(VMDHL_WRITE, pHdr, cbReq, &Lock, (KernPageList_t *)-1, NULL);
|
---|
330 | AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, cbReq, &Lock, rc), VERR_LOCK_FAILED);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * Process the IOCtl.
|
---|
335 | */
|
---|
336 | rc = supdrvIOCtl(iFunction, &g_DevExt, pSession, pHdr);
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | OSDBGPRINT(("VBoxDrvIOCtl: max(%#x,%#x); iCmd=%#x\n", pHdr->cbIn, pHdr->cbOut, iFunction));
|
---|
341 | rc = VERR_INVALID_PARAMETER;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | OSDBGPRINT(("VBoxDrvIOCtl: bad magic fFlags=%#x; iCmd=%#x\n", pHdr->fFlags, iFunction));
|
---|
347 | rc = VERR_INVALID_PARAMETER;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Unlock and return.
|
---|
352 | */
|
---|
353 | int rc2 = KernVMUnlock(&Lock);
|
---|
354 | AssertMsg(!rc2, ("rc2=%d\n", rc2));
|
---|
355 |
|
---|
356 | dprintf2(("VBoxDrvIOCtl: returns %d\n", rc));
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 | return VERR_NOT_SUPPORTED;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
364 | {
|
---|
365 | NOREF(pObj);
|
---|
366 | NOREF(pSession);
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
371 | {
|
---|
372 | NOREF(pObj);
|
---|
373 | NOREF(pSession);
|
---|
374 | NOREF(pszObjName);
|
---|
375 | NOREF(prc);
|
---|
376 | return false;
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Callback for writing to the log buffer.
|
---|
382 | *
|
---|
383 | * @returns number of bytes written.
|
---|
384 | * @param pvArg Unused.
|
---|
385 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
386 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
387 | */
|
---|
388 | static DECLCALLBACK(size_t) VBoxDrvLogOutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
389 | {
|
---|
390 | size_t cchWritten = 0;
|
---|
391 | while (cbChars-- > 0)
|
---|
392 | {
|
---|
393 | const uint16_t offLogHead = g_offLogHead;
|
---|
394 | const uint16_t offLogHeadNext = (offLogHead + 1) & (g_cchLogMax - 1);
|
---|
395 | if (offLogHeadNext == g_offLogTail)
|
---|
396 | break; /* no */
|
---|
397 | g_szLog[offLogHead] = *pachChars++;
|
---|
398 | g_offLogHead = offLogHeadNext;
|
---|
399 | cchWritten++;
|
---|
400 | }
|
---|
401 | return cchWritten;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
406 | {
|
---|
407 | va_list va;
|
---|
408 |
|
---|
409 | #if 0 //def DEBUG_bird
|
---|
410 | va_start(va, pszFormat);
|
---|
411 | RTLogComPrintfV(pszFormat, va);
|
---|
412 | va_end(va);
|
---|
413 | #endif
|
---|
414 |
|
---|
415 | va_start(va, pszFormat);
|
---|
416 | int cch = RTLogFormatV(VBoxDrvLogOutput, NULL, pszFormat, va);
|
---|
417 | va_end(va);
|
---|
418 |
|
---|
419 | return cch;
|
---|
420 | }
|
---|