VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/os2/SUPDrv-os2.cpp

最後變更 在這個檔案是 98106,由 vboxsync 提交於 23 月 前

Manual (C) year updates.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.7 KB
 
1/* $Id: SUPDrv-os2.cpp 98106 2023-01-17 22:43:07Z vboxsync $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - 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 LOG_GROUP LOG_GROUP_SUP_DRV
36#define __STDC_CONSTANT_MACROS
37#define __STDC_LIMIT_MACROS
38
39#include <os2ddk/bsekee.h>
40#undef RT_MAX
41
42#include "SUPDrvInternal.h"
43#include <VBox/version.h>
44#include <iprt/initterm.h>
45#include <iprt/string.h>
46#include <iprt/spinlock.h>
47#include <iprt/process.h>
48#include <iprt/assert.h>
49#include <VBox/log.h>
50#include <iprt/param.h>
51#include <VBox/version.h>
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57/**
58 * Device extention & session data association structure.
59 */
60static SUPDRVDEVEXT g_DevExt;
61/** Spinlock protecting g_apSessionHashTab. */
62static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
63/** Hash table */
64static PSUPDRVSESSION g_apSessionHashTab[19];
65/** Calculates the index into g_apSessionHashTab.*/
66#define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
67
68RT_C_DECLS_BEGIN
69/* Defined in SUPDrvA-os2.asm */
70extern uint16_t g_offLogHead;
71extern uint16_t volatile g_offLogTail;
72extern uint16_t const g_cchLogMax;
73extern char g_szLog[];
74/* (init only:) */
75extern char g_szInitText[];
76extern uint16_t g_cchInitText;
77extern uint16_t g_cchInitTextMax;
78RT_C_DECLS_END
79
80
81/*********************************************************************************************************************************
82* Internal Functions *
83*********************************************************************************************************************************/
84
85
86
87/**
88 * 32-bit Ring-0 initialization.
89 *
90 * @returns 0 on success, non-zero on failure.
91 * @param pszArgs Pointer to the device arguments.
92 */
93DECLASM(int) VBoxDrvInit(const char *pszArgs)
94{
95 /*
96 * Initialize the runtime.
97 */
98 int rc = RTR0Init(0);
99 if (RT_SUCCESS(rc))
100 {
101 Log(("VBoxDrvInit: pszArgs=%s\n", pszArgs));
102
103 /*
104 * Initialize the device extension.
105 */
106 rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
107 if (RT_SUCCESS(rc))
108 {
109 /*
110 * Initialize the session hash table.
111 */
112 rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDrvOS2");
113 if (RT_SUCCESS(rc))
114 {
115 /*
116 * Process the commandline. Later.
117 */
118 bool fVerbose = true;
119
120 /*
121 * Success
122 */
123 if (fVerbose)
124 {
125 strcpy(&g_szInitText[0],
126 "\r\n"
127 "VirtualBox.org Support Driver for OS/2 version " VBOX_VERSION_STRING "\r\n"
128 "Copyright (C) 2007 Knut St. Osmundsen\r\n"
129 "Copyright (C) 2007-" VBOX_C_YEAR " Oracle Corporation\r\n");
130 g_cchInitText = strlen(&g_szInitText[0]);
131 }
132 return VINF_SUCCESS;
133 }
134 g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: RTSpinlockCreate failed, rc=%Rrc\n", rc);
135 supdrvDeleteDevExt(&g_DevExt);
136 }
137 else
138 g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: supdrvInitDevExt failed, rc=%Rrc\n", rc);
139 RTR0Term();
140 }
141 else
142 g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: RTR0Init failed, rc=%Rrc\n", rc);
143 return rc;
144}
145
146
147DECLASM(int) VBoxDrvOpen(uint16_t sfn)
148{
149 int rc;
150 PSUPDRVSESSION pSession;
151
152 /*
153 * Create a new session.
154 */
155 rc = supdrvCreateSession(&g_DevExt, true /* fUser */, true /*fUnrestricted*/, &pSession);
156 if (RT_SUCCESS(rc))
157 {
158 pSession->sfn = sfn;
159
160 /*
161 * Insert it into the hash table.
162 */
163 unsigned iHash = SESSION_HASH(sfn);
164 RTSpinlockAcquire(g_Spinlock);
165 pSession->pNextHash = g_apSessionHashTab[iHash];
166 g_apSessionHashTab[iHash] = pSession;
167 RTSpinlockRelease(g_Spinlock);
168 }
169
170 Log(("VBoxDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
171 return rc;
172}
173
174
175DECLASM(int) VBoxDrvClose(uint16_t sfn)
176{
177 Log(("VBoxDrvClose: pid=%d sfn=%d\n", (int)RTProcSelf(), sfn));
178
179 /*
180 * Remove from the hash table.
181 */
182 PSUPDRVSESSION pSession;
183 const RTPROCESS Process = RTProcSelf();
184 const unsigned iHash = SESSION_HASH(sfn);
185 RTSpinlockAcquire(g_Spinlock);
186
187 pSession = g_apSessionHashTab[iHash];
188 if (pSession)
189 {
190 if ( pSession->sfn == sfn
191 && pSession->Process == Process)
192 {
193 g_apSessionHashTab[iHash] = pSession->pNextHash;
194 pSession->pNextHash = NULL;
195 }
196 else
197 {
198 PSUPDRVSESSION pPrev = pSession;
199 pSession = pSession->pNextHash;
200 while (pSession)
201 {
202 if ( pSession->sfn == sfn
203 && pSession->Process == Process)
204 {
205 pPrev->pNextHash = pSession->pNextHash;
206 pSession->pNextHash = NULL;
207 break;
208 }
209
210 /* next */
211 pPrev = pSession;
212 pSession = pSession->pNextHash;
213 }
214 }
215 }
216 RTSpinlockRelease(g_Spinlock);
217 if (!pSession)
218 {
219 OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d sfn=%d\n", (int)Process, sfn));
220 return VERR_INVALID_PARAMETER;
221 }
222
223 /*
224 * Close the session.
225 */
226 supdrvSessionRelease(pSession);
227 return 0;
228}
229
230
231DECLASM(int) VBoxDrvIOCtlFast(uint16_t sfn, uint8_t iFunction)
232{
233 /*
234 * Find the session.
235 */
236 const RTPROCESS Process = RTProcSelf();
237 const unsigned iHash = SESSION_HASH(sfn);
238 PSUPDRVSESSION pSession;
239
240 RTSpinlockAcquire(g_Spinlock);
241 pSession = g_apSessionHashTab[iHash];
242 if (pSession && pSession->Process != Process)
243 {
244 do pSession = pSession->pNextHash;
245 while ( pSession
246 && ( pSession->sfn != sfn
247 || pSession->Process != Process));
248
249 if (RT_LIKELY(pSession))
250 supdrvSessionRetain(pSession);
251 }
252 RTSpinlockRelease(g_Spinlock);
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 int rc;
263 if ((unsigned)(iFunction - SUP_IOCTL_FAST_DO_FIRST) < (unsigned)32)
264 rc = supdrvIOCtlFast(iFunction, 0, &g_DevExt, pSession);
265 else
266 rc = VERR_INVALID_FUNCTION;
267 supdrvSessionRelease(pSession);
268 return rc;
269}
270
271
272DECLASM(int) VBoxDrvIOCtl(uint16_t sfn, uint8_t iCat, uint8_t iFunction, void *pvParm, void *pvData, uint16_t *pcbParm, uint16_t *pcbData)
273{
274 /*
275 * Find the session.
276 */
277 const RTPROCESS Process = RTProcSelf();
278 const unsigned iHash = SESSION_HASH(sfn);
279 PSUPDRVSESSION pSession;
280
281 RTSpinlockAcquire(g_Spinlock);
282 pSession = g_apSessionHashTab[iHash];
283 if (pSession && pSession->Process != Process)
284 {
285 do pSession = pSession->pNextHash;
286 while ( pSession
287 && ( pSession->sfn != sfn
288 || pSession->Process != Process));
289
290 if (RT_LIKELY(pSession))
291 supdrvSessionRetain(pSession);
292 }
293 RTSpinlockRelease(g_Spinlock);
294 if (!pSession)
295 {
296 OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
297 return VERR_INVALID_PARAMETER;
298 }
299
300 /*
301 * Verify the category and dispatch the IOCtl.
302 */
303 int rc;
304 if (RT_LIKELY(iCat == SUP_CTL_CATEGORY))
305 {
306 Log(("VBoxDrvIOCtl: pSession=%p iFunction=%#x pvParm=%p pvData=%p *pcbParm=%d *pcbData=%d\n", pSession, iFunction, pvParm, pvData, *pcbParm, *pcbData));
307 Assert(pvParm);
308 Assert(!pvData);
309
310 /*
311 * Lock the header.
312 */
313 PSUPREQHDR pHdr = (PSUPREQHDR)pvParm;
314 AssertReturn(*pcbParm == sizeof(*pHdr), VERR_INVALID_PARAMETER);
315 KernVMLock_t Lock;
316 rc = KernVMLock(VMDHL_WRITE, pHdr, *pcbParm, &Lock, (KernPageList_t *)-1, NULL);
317 AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, *pcbParm, &Lock, rc), VERR_LOCK_FAILED);
318
319 /*
320 * Validate the header.
321 */
322 if (RT_LIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) == SUPREQHDR_FLAGS_MAGIC))
323 {
324 uint32_t cbReq = RT_MAX(pHdr->cbIn, pHdr->cbOut);
325 if (RT_LIKELY( pHdr->cbIn >= sizeof(*pHdr)
326 && pHdr->cbOut >= sizeof(*pHdr)
327 && cbReq <= _1M*16))
328 {
329 /*
330 * Lock the rest of the buffer if necessary.
331 */
332 if (((uintptr_t)pHdr & PAGE_OFFSET_MASK) + cbReq > PAGE_SIZE)
333 {
334 rc = KernVMUnlock(&Lock);
335 AssertMsgReturn(!rc, ("KernVMUnlock(Lock) -> %#x\n", rc), VERR_LOCK_FAILED);
336
337 rc = KernVMLock(VMDHL_WRITE, pHdr, cbReq, &Lock, (KernPageList_t *)-1, NULL);
338 AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, cbReq, &Lock, rc), VERR_LOCK_FAILED);
339 }
340
341 /*
342 * Process the IOCtl.
343 */
344 rc = supdrvIOCtl(iFunction, &g_DevExt, pSession, pHdr, cbReq);
345 }
346 else
347 {
348 OSDBGPRINT(("VBoxDrvIOCtl: max(%#x,%#x); iCmd=%#x\n", pHdr->cbIn, pHdr->cbOut, iFunction));
349 rc = VERR_INVALID_PARAMETER;
350 }
351 }
352 else
353 {
354 OSDBGPRINT(("VBoxDrvIOCtl: bad magic fFlags=%#x; iCmd=%#x\n", pHdr->fFlags, iFunction));
355 rc = VERR_INVALID_PARAMETER;
356 }
357
358 /*
359 * Unlock and return.
360 */
361 int rc2 = KernVMUnlock(&Lock);
362 AssertMsg(!rc2, ("rc2=%d\n", rc2)); NOREF(rc2);
363 }
364 else
365 rc = VERR_NOT_SUPPORTED;
366
367 supdrvSessionRelease(pSession);
368 Log2(("VBoxDrvIOCtl: returns %d\n", rc));
369 return rc;
370}
371
372
373void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
374{
375 NOREF(pDevExt);
376 NOREF(pSession);
377}
378
379
380void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
381{
382 NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
383}
384
385
386void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
387{
388 NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
389}
390
391
392void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
393{
394 NOREF(pObj);
395 NOREF(pSession);
396}
397
398
399bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
400{
401 NOREF(pObj);
402 NOREF(pSession);
403 NOREF(pszObjName);
404 NOREF(prc);
405 return false;
406}
407
408
409bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
410{
411 NOREF(pDevExt);
412 return false;
413}
414
415
416bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void)
417{
418 return false;
419}
420
421
422bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
423{
424 return false;
425}
426
427
428int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
429{
430 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
431 return VERR_NOT_SUPPORTED;
432}
433
434
435int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv,
436 const uint8_t *pbImageBits, const char *pszSymbol)
437{
438 NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits); NOREF(pszSymbol);
439 return VERR_NOT_SUPPORTED;
440}
441
442
443int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
444{
445 NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
446 return VERR_NOT_SUPPORTED;
447}
448
449
450void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
451{
452 NOREF(pDevExt); NOREF(pImage);
453}
454
455
456void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
457{
458 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
459}
460
461
462void VBOXCALL supdrvOSLdrNotifyUnloaded(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
463{
464 NOREF(pDevExt); NOREF(pImage);
465}
466
467
468int VBOXCALL supdrvOSLdrQuerySymbol(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage,
469 const char *pszSymbol, size_t cchSymbol, void **ppvSymbol)
470{
471 RT_NOREF(pDevExt, pImage, pszSymbol, cchSymbol, ppvSymbol);
472 return VERR_WRONG_ORDER;
473}
474
475
476void VBOXCALL supdrvOSLdrRetainWrapperModule(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
477{
478 RT_NOREF(pDevExt, pImage);
479 AssertFailed();
480}
481
482
483void VBOXCALL supdrvOSLdrReleaseWrapperModule(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
484{
485 RT_NOREF(pDevExt, pImage);
486 AssertFailed();
487}
488
489#ifdef SUPDRV_WITH_MSR_PROBER
490
491int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
492{
493 NOREF(uMsr); NOREF(idCpu); NOREF(puValue);
494 return VERR_NOT_SUPPORTED;
495}
496
497
498int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
499{
500 NOREF(uMsr); NOREF(idCpu); NOREF(uValue);
501 return VERR_NOT_SUPPORTED;
502}
503
504
505int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
506{
507 NOREF(idCpu); NOREF(pReq);
508 return VERR_NOT_SUPPORTED;
509}
510
511#endif /* SUPDRV_WITH_MSR_PROBER */
512
513
514/**
515 * Callback for writing to the log buffer.
516 *
517 * @returns number of bytes written.
518 * @param pvArg Unused.
519 * @param pachChars Pointer to an array of utf-8 characters.
520 * @param cbChars Number of bytes in the character array pointed to by pachChars.
521 */
522static DECLCALLBACK(size_t) VBoxDrvLogOutput(void *pvArg, const char *pachChars, size_t cbChars)
523{
524 size_t cchWritten = 0;
525 while (cbChars-- > 0)
526 {
527 const uint16_t offLogHead = g_offLogHead;
528 const uint16_t offLogHeadNext = (offLogHead + 1) & (g_cchLogMax - 1);
529 if (offLogHeadNext == g_offLogTail)
530 break; /* no */
531 g_szLog[offLogHead] = *pachChars++;
532 g_offLogHead = offLogHeadNext;
533 cchWritten++;
534 }
535 return cchWritten;
536}
537
538
539SUPR0DECL(int) SUPR0PrintfV(const char *pszFormat, va_list va)
540{
541#if 0 //def DEBUG_bird
542 va_list va2;
543 va_copy(va2, va);
544 RTLogComPrintfV(pszFormat, va2);
545 va_end(va2);
546#endif
547
548 RTLogFormatV(VBoxDrvLogOutput, NULL, pszFormat, va);
549 return 0;
550}
551
552
553SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
554{
555 return 0;
556}
557
558
559SUPR0DECL(bool) SUPR0FpuBegin(bool fCtxHook)
560{
561 RT_NOREF(fCtxHook);
562 return false;
563}
564
565
566SUPR0DECL(void) SUPR0FpuEnd(bool fCtxHook)
567{
568 RT_NOREF(fCtxHook);
569}
570
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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