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