1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_mem.h"
|
---|
8 | #include "cr_string.h"
|
---|
9 | #include "cr_dll.h"
|
---|
10 | #include "cr_error.h"
|
---|
11 | #include "cr_spu.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | #include <iprt/param.h>
|
---|
15 | #include <iprt/string.h>
|
---|
16 | #include <iprt/path.h>
|
---|
17 |
|
---|
18 | #include <stdio.h>
|
---|
19 |
|
---|
20 | #ifdef WINDOWS
|
---|
21 | #ifdef VBOX_WDDM_WOW64
|
---|
22 | #define DLL_SUFFIX "-x86.dll"
|
---|
23 | #else
|
---|
24 | #define DLL_SUFFIX ".dll"
|
---|
25 | #endif
|
---|
26 | #define DLL_PREFIX "VBoxOGL"
|
---|
27 | #define snprintf _snprintf
|
---|
28 | #elif defined(DARWIN)
|
---|
29 | #define DLL_SUFFIX ".dylib"
|
---|
30 | #define DLL_PREFIX "VBoxOGL"
|
---|
31 | /*
|
---|
32 | #define DLL_SUFFIX ".bundle"
|
---|
33 | #define DLL_PREFIX ""
|
---|
34 | */
|
---|
35 | #else
|
---|
36 | #ifdef AIX
|
---|
37 | #define DLL_SUFFIX ".o"
|
---|
38 | #define DLL_PREFIX "VBoxOGL"
|
---|
39 | #else
|
---|
40 | #define DLL_SUFFIX ".so"
|
---|
41 | #define DLL_PREFIX "VBoxOGL"
|
---|
42 | #endif
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | extern void __buildDispatch( SPU *spu );
|
---|
46 |
|
---|
47 | static char *__findDLL( char *name, char *dir )
|
---|
48 | {
|
---|
49 | static char path[8092];
|
---|
50 |
|
---|
51 | if (!dir)
|
---|
52 | {
|
---|
53 | #if defined(DARWIN)
|
---|
54 | char szSharedLibPath[8092];
|
---|
55 | int rc = RTPathAppPrivateArch (szSharedLibPath, sizeof(szSharedLibPath));
|
---|
56 | if (RT_SUCCESS(rc))
|
---|
57 | sprintf ( path, "%s/%s%sspu%s", szSharedLibPath, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
58 | else
|
---|
59 | #endif /* DARWIN */
|
---|
60 | #ifdef VBOX
|
---|
61 | snprintf ( path, sizeof(path), "%s%sspu%s", DLL_PREFIX, name, DLL_SUFFIX );
|
---|
62 | #else
|
---|
63 | sprintf ( path, "%s%sspu%s", DLL_PREFIX, name, DLL_SUFFIX );
|
---|
64 | #endif
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | #ifdef VBOX
|
---|
69 | snprintf ( path, sizeof(path), "%s/%s%sspu%s", dir, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
70 | #else
|
---|
71 | sprintf ( path, "%s/%s%sspu%s", dir, DLL_PREFIX, name, DLL_SUFFIX );
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 | return path;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Load a single SPU from disk and initialize it. Is there any reason
|
---|
79 | * to export this from the SPU loader library? */
|
---|
80 |
|
---|
81 | SPU * crSPULoad( SPU *child, int id, char *name, char *dir, void *server )
|
---|
82 | {
|
---|
83 | SPU *the_spu;
|
---|
84 | char *path;
|
---|
85 | bool fNeedSuperSPU = false;
|
---|
86 |
|
---|
87 | CRASSERT( name != NULL );
|
---|
88 |
|
---|
89 | the_spu = (SPU*)crAlloc( sizeof( *the_spu ) );
|
---|
90 | /* ensure all fields are initially zero,
|
---|
91 | * NOTE: what actually MUST be zero at this point is the_spu->superSPU, otherwise
|
---|
92 | * crSPUUnloadChain in the failure branches below will misbehave */
|
---|
93 | crMemset(the_spu, 0, sizeof (*the_spu));
|
---|
94 | the_spu->id = id;
|
---|
95 | the_spu->privatePtr = NULL;
|
---|
96 | path = __findDLL( name, dir );
|
---|
97 | the_spu->dll = crDLLOpen( path, 0/*resolveGlobal*/ );
|
---|
98 | if (the_spu->dll == NULL)
|
---|
99 | {
|
---|
100 | crError("Couldn't load the DLL \"%s\"!\n", path);
|
---|
101 | crFree(the_spu);
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 | #if defined(DEBUG_misha) && defined(RT_OS_WINDOWS)
|
---|
105 | crDbgCmdSymLoadPrint(path, the_spu->dll->hinstLib);
|
---|
106 | #endif
|
---|
107 | the_spu->entry_point =
|
---|
108 | (SPULoadFunction) crDLLGetNoError( the_spu->dll, SPU_ENTRY_POINT_NAME );
|
---|
109 | if (!the_spu->entry_point)
|
---|
110 | {
|
---|
111 | crError( "Couldn't load the SPU entry point \"%s\" from SPU \"%s\"!",
|
---|
112 | SPU_ENTRY_POINT_NAME, name );
|
---|
113 | crSPUUnloadChain(the_spu);
|
---|
114 | return NULL;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* This basically calls the SPU's SPULoad() function */
|
---|
118 | if (!the_spu->entry_point( &(the_spu->name), &(the_spu->super_name),
|
---|
119 | &(the_spu->init), &(the_spu->self),
|
---|
120 | &(the_spu->cleanup),
|
---|
121 | &(the_spu->spu_flags)) )
|
---|
122 | {
|
---|
123 | crError( "I found the SPU \"%s\", but loading it failed!", name );
|
---|
124 | crSPUUnloadChain(the_spu);
|
---|
125 | return NULL;
|
---|
126 | }
|
---|
127 | #ifdef IN_GUEST
|
---|
128 | if (crStrcmp(the_spu->name,"error"))
|
---|
129 | {
|
---|
130 | /* the default super/base class for an SPU is the error SPU */
|
---|
131 | if (the_spu->super_name == NULL)
|
---|
132 | {
|
---|
133 | the_spu->super_name = "error";
|
---|
134 | }
|
---|
135 | the_spu->superSPU = crSPULoad( child, id, the_spu->super_name, dir, server );
|
---|
136 | fNeedSuperSPU = true;
|
---|
137 | }
|
---|
138 | #else
|
---|
139 | if (crStrcmp(the_spu->name,"hosterror"))
|
---|
140 | {
|
---|
141 | /* the default super/base class for an SPU is the error SPU */
|
---|
142 | if (the_spu->super_name == NULL)
|
---|
143 | {
|
---|
144 | the_spu->super_name = "hosterror";
|
---|
145 | }
|
---|
146 | the_spu->superSPU = crSPULoad( child, id, the_spu->super_name, dir, server );
|
---|
147 | fNeedSuperSPU = true;
|
---|
148 | }
|
---|
149 | #endif
|
---|
150 | else
|
---|
151 | {
|
---|
152 | the_spu->superSPU = NULL;
|
---|
153 | }
|
---|
154 | if (fNeedSuperSPU && !the_spu->superSPU)
|
---|
155 | {
|
---|
156 | crError( "Unable to load super SPU \"%s\" of \"%s\"!", the_spu->super_name, name );
|
---|
157 | crSPUUnloadChain(the_spu);
|
---|
158 | return NULL;
|
---|
159 | }
|
---|
160 | crDebug("Initializing %s SPU", name);
|
---|
161 | the_spu->function_table = the_spu->init( id, child, the_spu, 0, 1 );
|
---|
162 | if (!the_spu->function_table) {
|
---|
163 | crDebug("Failed to init %s SPU", name);
|
---|
164 | crSPUUnloadChain(the_spu);
|
---|
165 | return NULL;
|
---|
166 | }
|
---|
167 | __buildDispatch( the_spu );
|
---|
168 | /*crDebug( "initializing dispatch table %p (for SPU %s)", (void*)&(the_spu->dispatch_table), name );*/
|
---|
169 | crSPUInitDispatchTable( &(the_spu->dispatch_table) );
|
---|
170 | /*crDebug( "Done initializing the dispatch table for SPU %s, calling the self function", name );*/
|
---|
171 |
|
---|
172 | the_spu->dispatch_table.server = server;
|
---|
173 | the_spu->self( &(the_spu->dispatch_table) );
|
---|
174 | /*crDebug( "Done with the self function" );*/
|
---|
175 |
|
---|
176 | return the_spu;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Load the entire chain of SPUs and initialize all of them.
|
---|
181 | * This function returns the first one in the chain.
|
---|
182 | */
|
---|
183 | SPU *
|
---|
184 | crSPULoadChain( int count, int *ids, char **names, char *dir, void *server )
|
---|
185 | {
|
---|
186 | int i;
|
---|
187 | SPU *child_spu = NULL;
|
---|
188 | CRASSERT( count > 0 );
|
---|
189 |
|
---|
190 | for (i = count-1 ; i >= 0 ; i--)
|
---|
191 | {
|
---|
192 | int spu_id = ids[i];
|
---|
193 | char *spu_name = names[i];
|
---|
194 | SPU *the_spu, *temp;
|
---|
195 |
|
---|
196 | /* This call passes the previous version of spu, which is the SPU's
|
---|
197 | * "child" in this chain. */
|
---|
198 |
|
---|
199 | the_spu = crSPULoad( child_spu, spu_id, spu_name, dir, server );
|
---|
200 | if (!the_spu) {
|
---|
201 | return NULL;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (child_spu != NULL)
|
---|
205 | {
|
---|
206 | /* keep track of this so that people can pass functions through but
|
---|
207 | * still get updated when API's change on the fly. */
|
---|
208 | for (temp = the_spu ; temp ; temp = temp->superSPU )
|
---|
209 | {
|
---|
210 | struct _copy_list_node *node = (struct _copy_list_node *) crAlloc( sizeof( *node ) );
|
---|
211 | node->copy = &(temp->dispatch_table);
|
---|
212 | node->next = child_spu->dispatch_table.copyList;
|
---|
213 | child_spu->dispatch_table.copyList = node;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | child_spu = the_spu;
|
---|
217 | }
|
---|
218 | return child_spu;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | #if 00
|
---|
223 | /* XXXX experimental code - not used at this time */
|
---|
224 | /**
|
---|
225 | * Like crSPUChangeInterface(), but don't loop over all functions in
|
---|
226 | * the table to search for 'old_func'.
|
---|
227 | */
|
---|
228 | void
|
---|
229 | crSPUChangeFunction(SPUDispatchTable *table, unsigned int funcOffset,
|
---|
230 | void *newFunc)
|
---|
231 | {
|
---|
232 | SPUGenericFunction *f = (SPUGenericFunction *) table + funcOffset;
|
---|
233 | struct _copy_list_node *temp;
|
---|
234 |
|
---|
235 | CRASSERT(funcOffset < sizeof(*table) / sizeof(SPUGenericFunction));
|
---|
236 |
|
---|
237 | printf("%s\n", __FUNCTION__);
|
---|
238 | if (table->mark == 1)
|
---|
239 | return;
|
---|
240 | table->mark = 1;
|
---|
241 | *f = newFunc;
|
---|
242 |
|
---|
243 | /* update all copies of this table */
|
---|
244 | #if 1
|
---|
245 | for (temp = table->copyList ; temp ; temp = temp->next)
|
---|
246 | {
|
---|
247 | crSPUChangeFunction( temp->copy, funcOffset, newFunc );
|
---|
248 | }
|
---|
249 | #endif
|
---|
250 | if (table->copy_of != NULL)
|
---|
251 | {
|
---|
252 | crSPUChangeFunction( table->copy_of, funcOffset, newFunc );
|
---|
253 | }
|
---|
254 | #if 0
|
---|
255 | for (temp = table->copyList ; temp ; temp = temp->next)
|
---|
256 | {
|
---|
257 | crSPUChangeFunction( temp->copy, funcOffset, newFunc );
|
---|
258 | }
|
---|
259 | #endif
|
---|
260 | table->mark = 0;
|
---|
261 | }
|
---|
262 | #endif
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Call the cleanup() function for each SPU in a chain, close the SPU
|
---|
268 | * DLLs and free the SPU objects.
|
---|
269 | * \param headSPU pointer to the first SPU in the chain
|
---|
270 | */
|
---|
271 | void
|
---|
272 | crSPUUnloadChain(SPU *headSPU)
|
---|
273 | {
|
---|
274 | SPU *the_spu = headSPU, *next_spu;
|
---|
275 |
|
---|
276 | while (the_spu)
|
---|
277 | {
|
---|
278 | crDebug("Cleaning up SPU %s", the_spu->name);
|
---|
279 |
|
---|
280 | if (the_spu->cleanup)
|
---|
281 | the_spu->cleanup();
|
---|
282 |
|
---|
283 | next_spu = the_spu->superSPU;
|
---|
284 | crDLLClose(the_spu->dll);
|
---|
285 | crFree(the_spu);
|
---|
286 | the_spu = next_spu;
|
---|
287 | }
|
---|
288 | }
|
---|