VirtualBox

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

Last change on this file since 54934 was 54581, checked in by vboxsync, 10 years ago

HostDrivers/Support: add and use supdrvOSAreCpusOfflinedOnSuspend(). FreeBSD, Darwin, Solaris need verification.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 KB
Line 
1/* $Id: SUPDrv-os2.cpp 54581 2015-03-02 14:56:02Z vboxsync $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - OS/2 specifics.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SUP_DRV
35#define __STDC_CONSTANT_MACROS
36#define __STDC_LIMIT_MACROS
37
38#include <os2ddk/bsekee.h>
39#undef RT_MAX
40
41#include "SUPDrvInternal.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 <VBox/log.h>
49#include <iprt/param.h>
50
51
52/*******************************************************************************
53* Global Variables *
54*******************************************************************************/
55/**
56 * Device extention & session data association structure.
57 */
58static SUPDRVDEVEXT g_DevExt;
59/** Spinlock protecting g_apSessionHashTab. */
60static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
61/** Hash table */
62static PSUPDRVSESSION g_apSessionHashTab[19];
63/** Calculates the index into g_apSessionHashTab.*/
64#define SESSION_HASH(sfn) ((sfn) % RT_ELEMENTS(g_apSessionHashTab))
65
66RT_C_DECLS_BEGIN
67/* Defined in SUPDrvA-os2.asm */
68extern uint16_t g_offLogHead;
69extern uint16_t volatile g_offLogTail;
70extern uint16_t const g_cchLogMax;
71extern char g_szLog[];
72/* (init only:) */
73extern char g_szInitText[];
74extern uint16_t g_cchInitText;
75extern uint16_t g_cchInitTextMax;
76RT_C_DECLS_END
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 */
91DECLASM(int) VBoxDrvInit(const char *pszArgs)
92{
93 /*
94 * Initialize the runtime.
95 */
96 int rc = RTR0Init(0);
97 if (RT_SUCCESS(rc))
98 {
99 Log(("VBoxDrvInit: pszArgs=%s\n", pszArgs));
100
101 /*
102 * Initialize the device extension.
103 */
104 rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
105 if (RT_SUCCESS(rc))
106 {
107 /*
108 * Initialize the session hash table.
109 */
110 rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDrvOS2");
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 Oracle Corporation\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=%Rrc\n", rc);
133 supdrvDeleteDevExt(&g_DevExt);
134 }
135 else
136 g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: supdrvInitDevExt failed, rc=%Rrc\n", rc);
137 RTR0Term();
138 }
139 else
140 g_cchInitText = RTStrPrintf(&g_szInitText[0], g_cchInitTextMax, "VBoxDrv.sys: RTR0Init failed, rc=%Rrc\n", rc);
141 return rc;
142}
143
144
145DECLASM(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, true /* fUser */, true /*fUnrestricted*/, &pSession);
154 if (RT_SUCCESS(rc))
155 {
156 pSession->sfn = sfn;
157
158 /*
159 * Insert it into the hash table.
160 */
161 unsigned iHash = SESSION_HASH(sfn);
162 RTSpinlockAcquire(g_Spinlock);
163 pSession->pNextHash = g_apSessionHashTab[iHash];
164 g_apSessionHashTab[iHash] = pSession;
165 RTSpinlockRelease(g_Spinlock);
166 }
167
168 Log(("VBoxDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
169 return rc;
170}
171
172
173DECLASM(int) VBoxDrvClose(uint16_t sfn)
174{
175 Log(("VBoxDrvClose: pid=%d sfn=%d\n", (int)RTProcSelf(), sfn));
176
177 /*
178 * Remove from the hash table.
179 */
180 PSUPDRVSESSION pSession;
181 const RTPROCESS Process = RTProcSelf();
182 const unsigned iHash = SESSION_HASH(sfn);
183 RTSpinlockAcquire(g_Spinlock);
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 RTSpinlockRelease(g_Spinlock);
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 supdrvSessionRelease(pSession);
225 return 0;
226}
227
228
229DECLASM(int) VBoxDrvIOCtlFast(uint16_t sfn, uint8_t iFunction)
230{
231 /*
232 * Find the session.
233 */
234 const RTPROCESS Process = RTProcSelf();
235 const unsigned iHash = SESSION_HASH(sfn);
236 PSUPDRVSESSION pSession;
237
238 RTSpinlockAcquire(g_Spinlock);
239 pSession = g_apSessionHashTab[iHash];
240 if (pSession && pSession->Process != Process)
241 {
242 do pSession = pSession->pNextHash;
243 while ( pSession
244 && ( pSession->sfn != sfn
245 || pSession->Process != Process));
246
247 if (RT_LIKELY(pSession))
248 supdrvSessionRetain(pSession);
249 }
250 RTSpinlockRelease(g_Spinlock);
251 if (RT_UNLIKELY(!pSession))
252 {
253 OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
254 return VERR_INVALID_PARAMETER;
255 }
256
257 /*
258 * Dispatch the fast IOCtl.
259 */
260 supdrvIOCtlFast(iFunction, 0, &g_DevExt, pSession);
261 supdrvSessionRelease(pSession);
262 return 0;
263}
264
265
266DECLASM(int) VBoxDrvIOCtl(uint16_t sfn, uint8_t iCat, uint8_t iFunction, void *pvParm, void *pvData, uint16_t *pcbParm, uint16_t *pcbData)
267{
268 /*
269 * Find the session.
270 */
271 const RTPROCESS Process = RTProcSelf();
272 const unsigned iHash = SESSION_HASH(sfn);
273 PSUPDRVSESSION pSession;
274
275 RTSpinlockAcquire(g_Spinlock);
276 pSession = g_apSessionHashTab[iHash];
277 if (pSession && pSession->Process != Process)
278 {
279 do pSession = pSession->pNextHash;
280 while ( pSession
281 && ( pSession->sfn != sfn
282 || pSession->Process != Process));
283
284 if (RT_LIKELY(pSession))
285 supdrvSessionRetain(pSession);
286 }
287 RTSpinlockRelease(g_Spinlock);
288 if (!pSession)
289 {
290 OSDBGPRINT(("VBoxDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
291 return VERR_INVALID_PARAMETER;
292 }
293
294 /*
295 * Verify the category and dispatch the IOCtl.
296 */
297 if (RT_LIKELY(iCat == SUP_CTL_CATEGORY))
298 {
299 Log(("VBoxDrvIOCtl: pSession=%p iFunction=%#x pvParm=%p pvData=%p *pcbParm=%d *pcbData=%d\n", pSession, iFunction, pvParm, pvData, *pcbParm, *pcbData));
300 Assert(pvParm);
301 Assert(!pvData);
302
303 /*
304 * Lock the header.
305 */
306 PSUPREQHDR pHdr = (PSUPREQHDR)pvParm;
307 AssertReturn(*pcbParm == sizeof(*pHdr), VERR_INVALID_PARAMETER);
308 KernVMLock_t Lock;
309 int rc = KernVMLock(VMDHL_WRITE, pHdr, *pcbParm, &Lock, (KernPageList_t *)-1, NULL);
310 AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, *pcbParm, &Lock, rc), VERR_LOCK_FAILED);
311
312 /*
313 * Validate the header.
314 */
315 if (RT_LIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) == SUPREQHDR_FLAGS_MAGIC))
316 {
317 uint32_t cbReq = RT_MAX(pHdr->cbIn, pHdr->cbOut);
318 if (RT_LIKELY( pHdr->cbIn >= sizeof(*pHdr)
319 && pHdr->cbOut >= sizeof(*pHdr)
320 && cbReq <= _1M*16))
321 {
322 /*
323 * Lock the rest of the buffer if necessary.
324 */
325 if (((uintptr_t)pHdr & PAGE_OFFSET_MASK) + cbReq > PAGE_SIZE)
326 {
327 rc = KernVMUnlock(&Lock);
328 AssertMsgReturn(!rc, ("KernVMUnlock(Lock) -> %#x\n", rc), VERR_LOCK_FAILED);
329
330 rc = KernVMLock(VMDHL_WRITE, pHdr, cbReq, &Lock, (KernPageList_t *)-1, NULL);
331 AssertMsgReturn(!rc, ("KernVMLock(VMDHL_WRITE, %p, %#x, &p, NULL, NULL) -> %d\n", pHdr, cbReq, &Lock, rc), VERR_LOCK_FAILED);
332 }
333
334 /*
335 * Process the IOCtl.
336 */
337 rc = supdrvIOCtl(iFunction, &g_DevExt, pSession, pHdr, cbReq);
338 }
339 else
340 {
341 OSDBGPRINT(("VBoxDrvIOCtl: max(%#x,%#x); iCmd=%#x\n", pHdr->cbIn, pHdr->cbOut, iFunction));
342 rc = VERR_INVALID_PARAMETER;
343 }
344 }
345 else
346 {
347 OSDBGPRINT(("VBoxDrvIOCtl: bad magic fFlags=%#x; iCmd=%#x\n", pHdr->fFlags, iFunction));
348 rc = VERR_INVALID_PARAMETER;
349 }
350
351 /*
352 * Unlock and return.
353 */
354 int rc2 = KernVMUnlock(&Lock);
355 AssertMsg(!rc2, ("rc2=%d\n", rc2)); NOREF(rc2);s
356 }
357 else
358 rc = VERR_NOT_SUPPORTED;
359
360 supdrvSessionRelease(pSession);
361 Log2(("VBoxDrvIOCtl: returns %d\n", rc));
362 return rc;
363}
364
365
366void VBOXCALL supdrvOSCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
367{
368 NOREF(pDevExt);
369 NOREF(pSession);
370}
371
372
373void VBOXCALL supdrvOSSessionHashTabInserted(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
374{
375 NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
376}
377
378
379void VBOXCALL supdrvOSSessionHashTabRemoved(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, void *pvUser)
380{
381 NOREF(pDevExt); NOREF(pSession); NOREF(pvUser);
382}
383
384
385void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
386{
387 NOREF(pObj);
388 NOREF(pSession);
389}
390
391
392bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
393{
394 NOREF(pObj);
395 NOREF(pSession);
396 NOREF(pszObjName);
397 NOREF(prc);
398 return false;
399}
400
401
402bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
403{
404 NOREF(pDevExt);
405 return false;
406}
407
408
409bool VBOXCALL supdrvOSAreCpusOfflinedOnSuspend(void)
410{
411 return false;
412}
413
414
415bool VBOXCALL supdrvOSAreTscDeltasInSync(void)
416{
417 NOREF(pDevExt);
418 return false;
419}
420
421
422int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
423{
424 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
425 return VERR_NOT_SUPPORTED;
426}
427
428
429void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
430{
431 NOREF(pDevExt); NOREF(pImage);
432}
433
434
435int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
436{
437 NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
438 return VERR_NOT_SUPPORTED;
439}
440
441
442int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
443{
444 NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
445 return VERR_NOT_SUPPORTED;
446}
447
448
449void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
450{
451 NOREF(pDevExt); NOREF(pImage);
452}
453
454
455#ifdef SUPDRV_WITH_MSR_PROBER
456
457int VBOXCALL supdrvOSMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue)
458{
459 NOREF(uMsr); NOREF(idCpu); NOREF(puValue);
460 return VERR_NOT_SUPPORTED;
461}
462
463
464int VBOXCALL supdrvOSMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue)
465{
466 NOREF(uMsr); NOREF(idCpu); NOREF(uValue);
467 return VERR_NOT_SUPPORTED;
468}
469
470
471int VBOXCALL supdrvOSMsrProberModify(RTCPUID idCpu, PSUPMSRPROBER pReq)
472{
473 NOREF(idCpu); NOREF(pReq);
474 return VERR_NOT_SUPPORTED;
475}
476
477#endif /* SUPDRV_WITH_MSR_PROBER */
478
479
480/**
481 * Callback for writing to the log buffer.
482 *
483 * @returns number of bytes written.
484 * @param pvArg Unused.
485 * @param pachChars Pointer to an array of utf-8 characters.
486 * @param cbChars Number of bytes in the character array pointed to by pachChars.
487 */
488static DECLCALLBACK(size_t) VBoxDrvLogOutput(void *pvArg, const char *pachChars, size_t cbChars)
489{
490 size_t cchWritten = 0;
491 while (cbChars-- > 0)
492 {
493 const uint16_t offLogHead = g_offLogHead;
494 const uint16_t offLogHeadNext = (offLogHead + 1) & (g_cchLogMax - 1);
495 if (offLogHeadNext == g_offLogTail)
496 break; /* no */
497 g_szLog[offLogHead] = *pachChars++;
498 g_offLogHead = offLogHeadNext;
499 cchWritten++;
500 }
501 return cchWritten;
502}
503
504
505SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
506{
507 va_list va;
508
509#if 0 //def DEBUG_bird
510 va_start(va, pszFormat);
511 RTLogComPrintfV(pszFormat, va);
512 va_end(va);
513#endif
514
515 va_start(va, pszFormat);
516 int cch = RTLogFormatV(VBoxDrvLogOutput, NULL, pszFormat, va);
517 va_end(va);
518
519 return cch;
520}
521
522
523/**
524 * Returns configuration flags of the host kernel.
525 */
526SUPR0DECL(uint32_t) SUPR0GetKernelFeatures(void)
527{
528 return 0;
529}
530
Note: See TracBrowser for help on using the repository browser.

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