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