1 | /** @file
|
---|
2 | *
|
---|
3 | * VBoxGuestLib - A support library for VirtualBox guest additions:
|
---|
4 | * Library initialization
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | #include <VBox/VBoxGuestLib.h>
|
---|
25 |
|
---|
26 | #define VBGL_DECL_DATA
|
---|
27 | #include "VBGLInternal.h"
|
---|
28 |
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 |
|
---|
32 | VBGLDATA g_vbgldata;
|
---|
33 |
|
---|
34 | #ifndef VBGL_VBOXGUEST
|
---|
35 | /* The guest library uses lazy initialization for VMMDev port and memory,
|
---|
36 | * because these values are provided by the VBoxGuest driver and it might
|
---|
37 | * be loaded later than other drivers.
|
---|
38 | * The VbglEnter checks the current library status, tries to retrive these
|
---|
39 | * values and fails if they are unavailable.
|
---|
40 | */
|
---|
41 | static void vbglQueryVMMDevPort (void)
|
---|
42 | {
|
---|
43 | int rc = VINF_SUCCESS;
|
---|
44 |
|
---|
45 | VBGLDRIVER driver;
|
---|
46 |
|
---|
47 | rc = vbglDriverOpen (&driver);
|
---|
48 |
|
---|
49 | if (RT_SUCCESS(rc))
|
---|
50 | {
|
---|
51 | VBoxGuestPortInfo port;
|
---|
52 |
|
---|
53 | rc = vbglDriverIOCtl (&driver, VBOXGUEST_IOCTL_GETVMMDEVPORT, &port, sizeof (port));
|
---|
54 |
|
---|
55 | if (RT_SUCCESS (rc))
|
---|
56 | {
|
---|
57 | dprintf (("port = 0x%04X, mem = %p\n", port.portAddress, port.pVMMDevMemory));
|
---|
58 |
|
---|
59 | g_vbgldata.portVMMDev = port.portAddress;
|
---|
60 | g_vbgldata.pVMMDevMemory = port.pVMMDevMemory;
|
---|
61 |
|
---|
62 | g_vbgldata.status = VbglStatusReady;
|
---|
63 | }
|
---|
64 |
|
---|
65 | vbglDriverClose (&driver);
|
---|
66 | }
|
---|
67 |
|
---|
68 | dprintf (("vbglQueryVMMDevPort rc = %d\n", rc));
|
---|
69 | }
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | int VbglEnter (void)
|
---|
73 | {
|
---|
74 | int rc;
|
---|
75 |
|
---|
76 | #ifndef VBGL_VBOXGUEST
|
---|
77 | if (g_vbgldata.status == VbglStatusInitializing)
|
---|
78 | {
|
---|
79 | vbglQueryVMMDevPort ();
|
---|
80 | }
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | rc = g_vbgldata.status == VbglStatusReady? VINF_SUCCESS: VERR_VBGL_NOT_INITIALIZED;
|
---|
84 |
|
---|
85 | // dprintf(("VbglEnter: rc = %d\n", rc));
|
---|
86 |
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | int vbglInitCommon (void)
|
---|
91 | {
|
---|
92 | int rc = VINF_SUCCESS;
|
---|
93 |
|
---|
94 | memset(&g_vbgldata, 0, sizeof (VBGLDATA));
|
---|
95 |
|
---|
96 | g_vbgldata.status = VbglStatusInitializing;
|
---|
97 |
|
---|
98 | rc = VbglPhysHeapInit ();
|
---|
99 |
|
---|
100 | if (RT_SUCCESS(rc))
|
---|
101 | {
|
---|
102 | /* other subsystems, none yet */
|
---|
103 | ;
|
---|
104 | }
|
---|
105 |
|
---|
106 | dprintf(("vbglInitCommon: rc = %d\n", rc));
|
---|
107 |
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | DECLVBGL(void) vbglTerminateCommon (void)
|
---|
112 | {
|
---|
113 | VbglPhysHeapTerminate ();
|
---|
114 |
|
---|
115 | memset(&g_vbgldata, 0, sizeof (VBGLDATA));
|
---|
116 |
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | #ifdef VBGL_VBOXGUEST
|
---|
121 |
|
---|
122 | DECLVBGL(int) VbglInit (VBGLIOPORT portVMMDev, VMMDevMemory *pVMMDevMemory)
|
---|
123 | {
|
---|
124 | int rc = VINF_SUCCESS;
|
---|
125 |
|
---|
126 | dprintf(("vbglInit: starts g_vbgldata.status %d\n", g_vbgldata.status));
|
---|
127 |
|
---|
128 | if (g_vbgldata.status == VbglStatusInitializing
|
---|
129 | || g_vbgldata.status == VbglStatusReady)
|
---|
130 | {
|
---|
131 | /* Initialization is already in process. */
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | rc = vbglInitCommon ();
|
---|
136 |
|
---|
137 | if (RT_SUCCESS(rc))
|
---|
138 | {
|
---|
139 | g_vbgldata.portVMMDev = portVMMDev;
|
---|
140 | g_vbgldata.pVMMDevMemory = pVMMDevMemory;
|
---|
141 |
|
---|
142 | g_vbgldata.status = VbglStatusReady;
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | g_vbgldata.status = VbglStatusNotInitialized;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | DECLVBGL(void) VbglTerminate (void)
|
---|
153 | {
|
---|
154 | vbglTerminateCommon ();
|
---|
155 |
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | #else /* !VBGL_VBOXGUEST */
|
---|
161 |
|
---|
162 | DECLVBGL(int) VbglInit (void)
|
---|
163 | {
|
---|
164 | int rc = VINF_SUCCESS;
|
---|
165 |
|
---|
166 | if (g_vbgldata.status == VbglStatusInitializing
|
---|
167 | || g_vbgldata.status == VbglStatusReady)
|
---|
168 | {
|
---|
169 | /* Initialization is already in process. */
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | rc = vbglInitCommon ();
|
---|
174 |
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | /* Try to obtain VMMDev port via IOCTL to VBoxGuest main driver. */
|
---|
178 | vbglQueryVMMDevPort ();
|
---|
179 |
|
---|
180 | #ifdef VBOX_WITH_HGCM
|
---|
181 | rc = vbglHGCMInit ();
|
---|
182 | #endif /* VBOX_WITH_HGCM */
|
---|
183 |
|
---|
184 | if (RT_FAILURE(rc))
|
---|
185 | {
|
---|
186 | vbglTerminateCommon ();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | DECLVBGL(void) VbglTerminate (void)
|
---|
194 | {
|
---|
195 | vbglTerminateCommon ();
|
---|
196 |
|
---|
197 | #ifdef VBOX_WITH_HGCM
|
---|
198 | vbglHGCMTerminate ();
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | #endif /* !VBGL_VBOXGUEST */
|
---|
205 |
|
---|