VBoxGuestR3Lib.cpp revision 4cf22962b49e1056d0424cbe6736fee615e296ef
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync/* $Id$ */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/** @file
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * available from http://www.virtualbox.org. This file is free software;
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * you can redistribute it and/or modify it under the terms of the GNU
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * General Public License (GPL) as published by the Free Software
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*******************************************************************************
85c594c1140f082dd862abde9dc7825137a3d51avboxsync* Header Files *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync*******************************************************************************/
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# include <Windows.h>
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# define INCL_BASE
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# define INCL_ERRORS
85c594c1140f082dd862abde9dc7825137a3d51avboxsync# include <os2.h>
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
b29e03c9044019d9c77222336e8f8616052824f5vboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <sys/types.h>
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <sys/stat.h>
11a862be79fe123488bccca60c06e92cdbfec6e8vboxsync# include <errno.h>
87b2fb3f8283472ba7010aedbf2b4dc12302155cvboxsync# include <unistd.h>
e38719852d98638514dba23fbacf53ad11361d6avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#include <iprt/time.h>
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#include <iprt/asm.h>
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#include <iprt/string.h>
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#include <iprt/file.h>
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#include <iprt/assert.h>
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#include <iprt/thread.h>
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#include <VBox/VBoxGuest.h>
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync#include <VBox/log.h>
083dd76e9fd7a829b1ed67ffc9003276643e7db1vboxsync#include "VBGLR3Internal.h"
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#ifdef VBOX_VBGLR3_XFREE86
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync/* Rather than try to resolve all the header file conflicts, I will just
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync prototype what we need here. */
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync# define XF86_O_RDWR 0x0002
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsynctypedef void *pointer;
8ab60c04baaf509b2aaae2a260adaf1281aaac03vboxsyncextern "C" int xf86open(const char*, int,...);
8ab60c04baaf509b2aaae2a260adaf1281aaac03vboxsyncextern "C" int xf86close(int);
8ab60c04baaf509b2aaae2a260adaf1281aaac03vboxsyncextern "C" int xf86ioctl(int, unsigned long, pointer);
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync#endif
6edb4183bc898fddcd0987b6c5c3903b8246fe45vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/*******************************************************************************
85c594c1140f082dd862abde9dc7825137a3d51avboxsync* Global Variables *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync*******************************************************************************/
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/** The VBoxGuest device handle. */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#ifdef VBOX_VBGLR3_XFREE86
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsyncstatic int g_File = -1;
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#elif defined(RT_OS_WINDOWS)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsyncstatic HANDLE g_hFile = INVALID_HANDLE_VALUE;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#else
85c594c1140f082dd862abde9dc7825137a3d51avboxsyncstatic RTFILE g_File = NIL_RTFILE;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync#endif
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** User counter.
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync * A counter of the number of times the library has been initialised, for use with
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync * X.org drivers, where the library may be shared by multiple independant modules
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync * inside a single process space.
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsyncstatic uint32_t volatile g_cInits = 0;
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
4ad933b349952e43d0b6ce21b3f5e2833ca04e85vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsyncVBGLR3DECL(int) VbglR3Init(void)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync uint32_t cInits = ASMAtomicIncU32(&g_cInits);
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#ifndef VBOX_VBGLR3_XFREE86
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync Assert(cInits > 0);
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#endif
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync if (cInits > 1)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync {
98da2c2cca8786e117ad93a31c6b2c6c1c3cdc78vboxsync /*
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync * This will fail if two (or more) threads race each other calling VbglR3Init.
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync * However it will work fine for single threaded or otherwise serialized
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync * processed calling us more than once.
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#ifdef RT_OS_WINDOWS
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_hFile == INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif !defined (VBOX_VBGLR3_XFREE86)
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync if (g_File == NIL_RTFILE)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#else
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File == -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return VINF_SUCCESS;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync }
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync if (g_hFile != INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif !defined(VBOX_VBGLR3_XFREE86)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File != NIL_RTFILE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#else
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (g_File != -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync#endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_INTERNAL_ERROR;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync /*
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync * and possible some other bits not availble thru iprt/file.h.
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync HANDLE hFile = CreateFile(VBOXGUEST_DEVICE_NAME,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync GENERIC_READ | GENERIC_WRITE,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync FILE_SHARE_READ | FILE_SHARE_WRITE,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync NULL,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync OPEN_EXISTING,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync NULL);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (hFile == INVALID_HANDLE_VALUE)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VERR_OPEN_FAILED;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_hFile = hFile;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync /*
e38719852d98638514dba23fbacf53ad11361d6avboxsync * We might wish to compile this with Watcom, so stick to
e38719852d98638514dba23fbacf53ad11361d6avboxsync * the OS/2 APIs all the way. And in any case we have to use
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * DosDevIOCtl for the requests, why not use Dos* for everything.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync HFILE hf = NULLHANDLE;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG ulAction = 0;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync APIRET rc = DosOpen((PCSZ)VBOXGUEST_DEVICE_NAME, &hf, &ulAction, 0, FILE_NORMAL,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync OPEN_ACTION_OPEN_IF_EXISTS,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync NULL);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return RTErrConvertFromOS2(rc);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (hf < 16)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync HFILE ahfs[16];
85c594c1140f082dd862abde9dc7825137a3d51avboxsync unsigned i;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync for (i = 0; i < RT_ELEMENTS(ahfs); i++)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ahfs[i] = 0xffffffff;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosDupHandle(hf, &ahfs[i]);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync break;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (i-- > 1)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG fulState = 0;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosQueryFHState(ahfs[i], &fulState);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (!rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync fulState |= OPEN_FLAGS_NOINHERIT;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosSetFHState(ahfs[i], fulState);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (!rc)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync {
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = DosClose(hf);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertMsg(!rc, ("%ld\n", rc));
85c594c1140f082dd862abde9dc7825137a3d51avboxsync hf = ahfs[i];
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync else
85c594c1140f082dd862abde9dc7825137a3d51avboxsync i++;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync while (i-- > 0)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync DosClose(ahfs[i]);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync }
85c594c1140f082dd862abde9dc7825137a3d51avboxsync g_File = hf;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync#elif defined(RT_OS_FREEBSD)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync /*
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync * Try open the BSD device. The device cloning makes this a bit of work.
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync */
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# if defined(VBOX_VBGLR3_XFREE86)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync int File = 0;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# else
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync RTFILE File = 0;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# endif
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync int rc;
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync char szDevice[sizeof(VBOXGUEST_DEVICE_NAME) + 16];
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync {
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync RTStrPrintf(szDevice, sizeof(szDevice), VBOXGUEST_DEVICE_NAME "%d", iUnit);
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# if defined(VBOX_VBGLR3_XFREE86)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync File = xf86open(szDevice, XF86_O_RDWR);
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync if (File >= 0)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync break;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# else
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync rc = RTFileOpen(&File, szDevice, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync if (RT_SUCCESS(rc))
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync break;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# endif
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync }
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# if defined(VBOX_VBGLR3_XFREE86)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync if (File == -1)
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync return VERR_OPEN_FAILED;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# else
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync if (RT_FAILURE(rc))
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync return rc;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync# endif
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync
999b9efb1c9a95fee642550c525ca0cf7c6f07b5vboxsync g_File = File;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(VBOX_VBGLR3_XFREE86) && !defined(RT_OS_FREEBSD)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync int File = xf86open(VBOXGUEST_DEVICE_NAME, XF86_O_RDWR);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (File == -1)
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync return VERR_OPEN_FAILED;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = File;
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
6edb4183bc898fddcd0987b6c5c3903b8246fe45vboxsync#else
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync /* The default implemenation. (linux, solaris) */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync RTFILE File;
6edb4183bc898fddcd0987b6c5c3903b8246fe45vboxsync int rc = RTFileOpen(&File, VBOXGUEST_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_FAILURE(rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return rc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync g_File = File;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
e38719852d98638514dba23fbacf53ad11361d6avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync /*
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync * Create release logger
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync */
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync PRTLOGGER pReleaseLogger;
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync int rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync RT_ELEMENTS(s_apszGroups), &s_apszGroups[0],
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync RTLOGDEST_USER, NULL);
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync /* This may legitimately fail if we are using the mini-runtime. */
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync if (RT_SUCCESS(rc2))
eb5d68001efb9346c3341aa46f2871b973b47107vboxsync RTLogRelSetDefaultInstance(pReleaseLogger);
3598f07e0e71a448a04d478320a9ca6314160ff6vboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return VINF_SUCCESS;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsyncVBGLR3DECL(void) VbglR3Term(void)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync /*
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync * Decrement the reference count and see if we're the last one out.
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync uint32_t cInits = ASMAtomicDecU32(&g_cInits);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync if (cInits > 0)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if !defined(VBOX_VBGLR3_XFREE86)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(!cInits);
fde449f361029c75f9bf28f145bd1ba7b36a9c77vboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync HANDLE hFile = g_hFile;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_hFile = INVALID_HANDLE_VALUE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync BOOL fRc = CloseHandle(hFile);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync Assert(fRc); NOREF(fRc);
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# elif defined(RT_OS_OS2)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync RTFILE File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = NIL_RTFILE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(File != NIL_RTFILE);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync APIRET rc = DosClose(File);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertMsg(!rc, ("%ld\n", rc));
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# else /* The IPRT case. */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync RTFILE File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = NIL_RTFILE;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync AssertReturnVoid(File != NIL_RTFILE);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc = RTFileClose(File);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync AssertRC(rc);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync# endif
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#else /* VBOX_VBGLR3_XFREE86 */
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync int File = g_File;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync g_File = -1;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync if (File == -1)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync xf86close(File);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#endif /* VBOX_VBGLR3_XFREE86 */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
85c594c1140f082dd862abde9dc7825137a3d51avboxsync/**
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * Internal wrapper around various OS specific ioctl implemenations.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * an failure returned by the OS specific ioctl APIs.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param iFunction The requested function.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param pvData The input and output data buffer.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @param cbData The size of the buffer.
e38719852d98638514dba23fbacf53ad11361d6avboxsync *
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * @remark Exactly how the VBoxGuestCommonIOCtl is ferried back
e38719852d98638514dba23fbacf53ad11361d6avboxsync * here is OS specific. On BSD and Darwin we can use errno,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync * while on OS/2 we use the 2nd buffer of the IOCtl.
85c594c1140f082dd862abde9dc7825137a3d51avboxsync */
0c1bdc5adae416967cb64e09f8ec81a5b77fe31dvboxsyncint vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync{
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#if defined(RT_OS_WINDOWS)
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync DWORD cbReturned = 0;
16a9c73495bd644adca2ffe9567c3b2a72d7b94dvboxsync if (!DeviceIoControl(g_hFile, iFunction, pvData, cbData, pvData, cbData, &cbReturned, NULL))
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync {
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync/** @todo The passing of error codes needs to be tested and fixed (as does *all* the other hosts except for
4cf22962b49e1056d0424cbe6736fee615e296efvboxsync * OS/2). The idea is that the VBox status codes in ring-0 should be transfered without loss down to
4cf22962b49e1056d0424cbe6736fee615e296efvboxsync * ring-3. However, it's not vitally important right now (obviously, since the other guys has been
4cf22962b49e1056d0424cbe6736fee615e296efvboxsync * ignoring it for 1+ years now). On Linux and Solaris the transfer is done, but it is currently not
4cf22962b49e1056d0424cbe6736fee615e296efvboxsync * lossless, so still needs fixing. */
16a9c73495bd644adca2ffe9567c3b2a72d7b94dvboxsync DWORD LastErr = GetLastError();
16a9c73495bd644adca2ffe9567c3b2a72d7b94dvboxsync return RTErrConvertFromWin32(LastErr);
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync }
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync return VINF_SUCCESS;
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync
f6b7aa2d424718f1bf8ca45a37b219b986f1024bvboxsync#elif defined(RT_OS_OS2)
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG cbOS2Parm = cbData;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int32_t vrc = VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync ULONG cbOS2Data = sizeof(vrc);
e38719852d98638514dba23fbacf53ad11361d6avboxsync APIRET rc = DosDevIOCtl(g_File, VBOXGUEST_IOCTL_CATEGORY, iFunction,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync pvData, cbData, &cbOS2Parm,
85c594c1140f082dd862abde9dc7825137a3d51avboxsync &vrc, sizeof(vrc), &cbOS2Data);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_LIKELY(!rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return vrc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return RTErrConvertFromOS2(rc);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync
b29e03c9044019d9c77222336e8f8616052824f5vboxsync#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync VBGLBIGREQ Hdr;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync Hdr.u32Magic = VBGLBIGREQ_MAGIC;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync Hdr.cbData = cbData;
11a862be79fe123488bccca60c06e92cdbfec6e8vboxsync Hdr.pvDataR3 = pvData;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** @todo test status code passing! */
11a862be79fe123488bccca60c06e92cdbfec6e8vboxsync int rc = ioctl((int)g_File, iFunction, &Hdr);
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync if (rc == -1)
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync {
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync rc = errno;
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return RTErrConvertFromErrno(rc);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync }
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return VINF_SUCCESS;
1c8130d3c423590046a8ed4f3059de38ac5dcc14vboxsync
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#elif defined(VBOX_VBGLR3_XFREE86)
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync /* PORTME - This is preferred over the RTFileIOCtl variant below, just be careful with the (int). */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** @todo test status code passing! */
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync int rc = xf86ioctl(g_File, iFunction, pvData);
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync if (rc == -1)
463d00559e51c6e08ccc9f5a77d2ee6d122b6e8cvboxsync return VERR_FILE_IO_ERROR; /* This is purely legacy stuff, it has to work and no more. */
34822e8b7d00c04a0bc98c0d1a565a00d9bb1fd9vboxsync return VINF_SUCCESS;
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync#else
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync /* Default implementation - PORTME: Do not use this without testings that passing errors works! */
507ebf9b3b77c84000a55645867db6617b5324bcvboxsync/** @todo test status code passing! */
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc2 = VERR_INTERNAL_ERROR;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync int rc = RTFileIoCtl(g_File, (int)iFunction, pvData, cbData, &rc2);
85c594c1140f082dd862abde9dc7825137a3d51avboxsync if (RT_SUCCESS(rc))
85c594c1140f082dd862abde9dc7825137a3d51avboxsync rc = rc2;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync return rc;
85c594c1140f082dd862abde9dc7825137a3d51avboxsync#endif
85c594c1140f082dd862abde9dc7825137a3d51avboxsync}
85c594c1140f082dd862abde9dc7825137a3d51avboxsync