ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/thunks.cpp
Revision: 1.16
Committed: 2006-05-03T21:45:14Z (18 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.15: +63 -3 lines
Log Message:
Add patches for native GetNamedResource() and Get1NamedResource(). This will
be useful to fix a bug in the AppleShare extension (see DRVR .AFPTranslator
in Basilisk II)

Unrelated improvement: call sheepshaver_cpu::get_resource() directly, don't
get it through another global function.

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * thunks.cpp - Thunks to share data and code with MacOS
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22     #include "thunks.h"
23     #include "emul_op.h"
24     #include "cpu_emulation.h"
25 gbeauche 1.6 #include "xlowmem.h"
26 gbeauche 1.1
27     // Native function declarations
28     #include "main.h"
29     #include "video.h"
30     #include "name_registry.h"
31     #include "serial.h"
32     #include "ether.h"
33 gbeauche 1.2 #include "macos_util.h"
34 gbeauche 1.1
35 gbeauche 1.6 // Generate PowerPC thunks for GetResource() replacements?
36     #define POWERPC_GET_RESOURCE_THUNKS 1
37    
38 gbeauche 1.1
39     /* NativeOp instruction format:
40 gbeauche 1.9 +------------+-------------------------+--+-----------+------------+
41     | 6 | |FN| OP | 2 |
42     +------------+-------------------------+--+-----------+------------+
43     0 5 |6 18 19 20 25 26 31
44 gbeauche 1.1 */
45    
46 gbeauche 1.9 #define POWERPC_NATIVE_OP(FN, OP) \
47     (POWERPC_EMUL_OP | ((FN) << 12) | (((uint32)OP) << 6) | 2)
48 gbeauche 1.1
49 gbeauche 1.2 /*
50     * Return the fake PowerPC opcode to handle specified native code
51     */
52    
53 gbeauche 1.1 #if EMULATED_PPC
54     uint32 NativeOpcode(int selector)
55     {
56     uint32 opcode;
57     switch (selector) {
58 gbeauche 1.6 case NATIVE_CHECK_LOAD_INVOC:
59 gbeauche 1.16 case NATIVE_NAMED_CHECK_LOAD_INVOC:
60 gbeauche 1.1 opcode = POWERPC_NATIVE_OP(0, selector);
61     break;
62     case NATIVE_PATCH_NAME_REGISTRY:
63     case NATIVE_VIDEO_INSTALL_ACCEL:
64     case NATIVE_VIDEO_VBL:
65     case NATIVE_VIDEO_DO_DRIVER_IO:
66 gbeauche 1.14 case NATIVE_ETHER_AO_GET_HWADDR:
67     case NATIVE_ETHER_AO_ADD_MULTI:
68     case NATIVE_ETHER_AO_DEL_MULTI:
69     case NATIVE_ETHER_AO_SEND_PACKET:
70 gbeauche 1.1 case NATIVE_ETHER_IRQ:
71     case NATIVE_ETHER_INIT:
72     case NATIVE_ETHER_TERM:
73     case NATIVE_ETHER_OPEN:
74     case NATIVE_ETHER_CLOSE:
75     case NATIVE_ETHER_WPUT:
76     case NATIVE_ETHER_RSRV:
77     case NATIVE_SERIAL_NOTHING:
78     case NATIVE_SERIAL_OPEN:
79     case NATIVE_SERIAL_PRIME_IN:
80     case NATIVE_SERIAL_PRIME_OUT:
81     case NATIVE_SERIAL_CONTROL:
82     case NATIVE_SERIAL_STATUS:
83     case NATIVE_SERIAL_CLOSE:
84     case NATIVE_GET_RESOURCE:
85     case NATIVE_GET_1_RESOURCE:
86     case NATIVE_GET_IND_RESOURCE:
87     case NATIVE_GET_1_IND_RESOURCE:
88     case NATIVE_R_GET_RESOURCE:
89 gbeauche 1.16 case NATIVE_GET_NAMED_RESOURCE:
90     case NATIVE_GET_1_NAMED_RESOURCE:
91 gbeauche 1.1 case NATIVE_MAKE_EXECUTABLE:
92 gbeauche 1.7 case NATIVE_SYNC_HOOK:
93     case NATIVE_BITBLT_HOOK:
94     case NATIVE_FILLRECT_HOOK:
95     case NATIVE_BITBLT:
96     case NATIVE_INVRECT:
97 gbeauche 1.8 case NATIVE_FILLRECT:
98 gbeauche 1.1 opcode = POWERPC_NATIVE_OP(1, selector);
99     break;
100     default:
101     abort();
102     }
103     return opcode;
104     }
105     #endif
106    
107 gbeauche 1.2
108     /*
109 gbeauche 1.6 * Generate PowerPC thunks for GetResource() replacements
110     */
111    
112     #if EMULATED_PPC
113     static uint32 get_resource_func;
114     static uint32 get_1_resource_func;
115     static uint32 get_ind_resource_func;
116     static uint32 get_1_ind_resource_func;
117     static uint32 r_get_resource_func;
118 gbeauche 1.16 static uint32 get_named_resource_func;
119     static uint32 get_1_named_resource_func;
120 gbeauche 1.6
121     static void generate_powerpc_thunks(void)
122     {
123 gbeauche 1.16 // check_load_invoc() thunk
124     uint32 check_load_invoc_opcode = NativeOpcode(NATIVE_CHECK_LOAD_INVOC);
125     uint32 base;
126    
127 gbeauche 1.6 static uint32 get_resource_template[] = {
128     PL(0x7c0802a6), // mflr r0
129     PL(0x90010008), // stw r0,8(r1)
130     PL(0x9421ffbc), // stwu r1,-68(r1)
131     PL(0x90610038), // stw r3,56(r1)
132     PL(0x9081003c), // stw r4,60(r1)
133     PL(0x00000000), // lwz r0,XLM_GET_RESOURCE(r0)
134     PL(0x80402834), // lwz r2,XLM_RES_LIB_TOC(r0)
135     PL(0x7c0903a6), // mtctr r0
136     PL(0x4e800421), // bctrl
137     PL(0x90610040), // stw r3,64(r1)
138     PL(0x80610038), // lwz r3,56(r1)
139     PL(0xa881003e), // lha r4,62(r1)
140     PL(0x80a10040), // lwz r5,64(r1)
141     PL(0x00000001), // <check_load_invoc>
142     PL(0x80610040), // lwz r3,64(r1)
143     PL(0x8001004c), // lwz r0,76(r1)
144     PL(0x7c0803a6), // mtlr r0
145     PL(0x38210044), // addi r1,r1,68
146     PL(0x4e800020) // blr
147     };
148     const uint32 get_resource_template_size = sizeof(get_resource_template);
149    
150     int xlm_index = -1, check_load_invoc_index = -1;
151     for (int i = 0; i < get_resource_template_size/4; i++) {
152     uint32 opcode = ntohl(get_resource_template[i]);
153     switch (opcode) {
154     case 0x00000000:
155     xlm_index = i;
156     break;
157     case 0x00000001:
158     check_load_invoc_index = i;
159     break;
160     }
161     }
162     assert(xlm_index != -1 && check_load_invoc_index != -1);
163    
164     // GetResource()
165     get_resource_func = base = SheepMem::Reserve(get_resource_template_size);
166     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
167     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_RESOURCE);
168     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
169    
170     // Get1Resource()
171     get_1_resource_func = base = SheepMem::Reserve(get_resource_template_size);
172     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
173     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_1_RESOURCE);
174     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
175    
176     // GetIndResource()
177     get_ind_resource_func = base = SheepMem::Reserve(get_resource_template_size);
178     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
179     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_IND_RESOURCE);
180     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
181    
182     // Get1IndResource()
183     get_1_ind_resource_func = base = SheepMem::Reserve(get_resource_template_size);
184     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
185     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_1_IND_RESOURCE);
186     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
187    
188     // RGetResource()
189     r_get_resource_func = base = SheepMem::Reserve(get_resource_template_size);
190     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
191     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_R_GET_RESOURCE);
192     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
193 gbeauche 1.16
194     // named_check_load_invoc() thunk
195     check_load_invoc_opcode = NativeOpcode(NATIVE_NAMED_CHECK_LOAD_INVOC);
196    
197     static uint32 get_named_resource_template[] = {
198     PL(0x7c0802a6), // mflr r0
199     PL(0x90010008), // stw r0,8(r1)
200     PL(0x9421ffbc), // stwu r1,-68(r1)
201     PL(0x90610038), // stw r3,56(r1)
202     PL(0x9081003c), // stw r4,60(r1)
203     PL(0x00000000), // lwz r0,XLM_GET_NAMED_RESOURCE(r0)
204     PL(0x80402834), // lwz r2,XLM_RES_LIB_TOC(r0)
205     PL(0x7c0903a6), // mtctr r0
206     PL(0x4e800421), // bctrl
207     PL(0x90610040), // stw r3,64(r1)
208     PL(0x80610038), // lwz r3,56(r1)
209     PL(0x8081003c), // lwz r4,60(r1)
210     PL(0x80a10040), // lwz r5,64(r1)
211     PL(0x00000001), // <named_check_load_invoc>
212     PL(0x80610040), // lwz r3,64(r1)
213     PL(0x8001004c), // lwz r0,76(r1)
214     PL(0x7c0803a6), // mtlr r0
215     PL(0x38210044), // addi r1,r1,68
216     PL(0x4e800020) // blr
217     };
218     const uint32 get_named_resource_template_size = sizeof(get_named_resource_template);
219    
220     xlm_index = -1, check_load_invoc_index = -1;
221     for (int i = 0; i < get_resource_template_size/4; i++) {
222     uint32 opcode = ntohl(get_resource_template[i]);
223     switch (opcode) {
224     case 0x00000000:
225     xlm_index = i;
226     break;
227     case 0x00000001:
228     check_load_invoc_index = i;
229     break;
230     }
231     }
232     assert(xlm_index != -1 && check_load_invoc_index != -1);
233    
234     // GetNamedResource()
235     get_named_resource_func = base = SheepMem::Reserve(get_named_resource_template_size);
236     Host2Mac_memcpy(base, get_named_resource_template, get_named_resource_template_size);
237     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_NAMED_RESOURCE);
238     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
239    
240     // Get1NamedResource()
241     get_1_named_resource_func = base = SheepMem::Reserve(get_named_resource_template_size);
242     Host2Mac_memcpy(base, get_named_resource_template, get_named_resource_template_size);
243     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_1_NAMED_RESOURCE);
244     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
245 gbeauche 1.6 }
246     #endif
247    
248    
249     /*
250 gbeauche 1.2 * Initialize the thunks system
251     */
252    
253 gbeauche 1.1 struct native_op_t {
254     uint32 tvect;
255     uint32 func;
256 gbeauche 1.3 SheepRoutineDescriptor *desc;
257 gbeauche 1.1 };
258     static native_op_t native_op[NATIVE_OP_MAX];
259    
260     bool ThunksInit(void)
261     {
262     #if EMULATED_PPC
263     for (int i = 0; i < NATIVE_OP_MAX; i++) {
264     uintptr base = SheepMem::Reserve(12);
265     WriteMacInt32(base + 0, base + 8);
266     WriteMacInt32(base + 4, 0); // Fake TVECT
267     WriteMacInt32(base + 8, NativeOpcode(i));
268     native_op[i].tvect = base;
269     native_op[i].func = base + 8;
270     }
271 gbeauche 1.6 #if POWERPC_GET_RESOURCE_THUNKS
272     generate_powerpc_thunks();
273     native_op[NATIVE_GET_RESOURCE].func = get_resource_func;
274     native_op[NATIVE_GET_1_RESOURCE].func = get_1_resource_func;
275     native_op[NATIVE_GET_IND_RESOURCE].func = get_ind_resource_func;
276     native_op[NATIVE_GET_1_IND_RESOURCE].func = get_1_ind_resource_func;
277     native_op[NATIVE_R_GET_RESOURCE].func = r_get_resource_func;
278 gbeauche 1.16 native_op[NATIVE_GET_NAMED_RESOURCE].func = get_named_resource_func;
279     native_op[NATIVE_GET_1_NAMED_RESOURCE].func = get_1_named_resource_func;
280 gbeauche 1.6 #endif
281 gbeauche 1.1 #else
282 gbeauche 1.12 #if defined(__linux__) || defined(__NetBSD__) || (defined(__APPLE__) && defined(__MACH__))
283 gbeauche 1.1 #define DEFINE_NATIVE_OP(ID, FUNC) do { \
284     uintptr base = SheepMem::Reserve(8); \
285     WriteMacInt32(base + 0, (uint32)FUNC); \
286 gbeauche 1.13 WriteMacInt32(base + 4, (uint32)TOC); \
287 gbeauche 1.1 native_op[ID].tvect = base; \
288     native_op[ID].func = (uint32)FUNC; \
289     } while (0)
290     #elif defined(__BEOS__)
291     #define DEFINE_NATIVE_OP(ID, FUNC) do { \
292     native_op[ID].tvect = FUNC; \
293     native_op[ID].func = ((uint32 *)FUNC)[0]; \
294     } while (0)
295     #else
296     #error "FIXME: define NativeOp for your platform"
297     #endif
298 gbeauche 1.4 // FIXME: add GetResource() and friends for completeness
299 gbeauche 1.2 DEFINE_NATIVE_OP(NATIVE_PATCH_NAME_REGISTRY, DoPatchNameRegistry);
300     DEFINE_NATIVE_OP(NATIVE_VIDEO_INSTALL_ACCEL, VideoInstallAccel);
301     DEFINE_NATIVE_OP(NATIVE_VIDEO_VBL, VideoVBL);
302     DEFINE_NATIVE_OP(NATIVE_VIDEO_DO_DRIVER_IO, VideoDoDriverIO);
303 gbeauche 1.14 DEFINE_NATIVE_OP(NATIVE_ETHER_AO_GET_HWADDR, AO_get_ethernet_address);
304     DEFINE_NATIVE_OP(NATIVE_ETHER_AO_ADD_MULTI, AO_enable_multicast);
305     DEFINE_NATIVE_OP(NATIVE_ETHER_AO_DEL_MULTI, AO_disable_multicast);
306 gbeauche 1.15 DEFINE_NATIVE_OP(NATIVE_ETHER_AO_SEND_PACKET, AO_transmit_packet);
307 gbeauche 1.2 DEFINE_NATIVE_OP(NATIVE_ETHER_IRQ, EtherIRQ);
308     DEFINE_NATIVE_OP(NATIVE_ETHER_INIT, InitStreamModule);
309     DEFINE_NATIVE_OP(NATIVE_ETHER_TERM, TerminateStreamModule);
310     DEFINE_NATIVE_OP(NATIVE_ETHER_OPEN, ether_open);
311     DEFINE_NATIVE_OP(NATIVE_ETHER_CLOSE, ether_close);
312     DEFINE_NATIVE_OP(NATIVE_ETHER_WPUT, ether_wput);
313     DEFINE_NATIVE_OP(NATIVE_ETHER_RSRV, ether_rsrv);
314 gbeauche 1.1 DEFINE_NATIVE_OP(NATIVE_SERIAL_NOTHING, SerialNothing);
315     DEFINE_NATIVE_OP(NATIVE_SERIAL_OPEN, SerialOpen);
316     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_IN, SerialPrimeIn);
317     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_OUT, SerialPrimeOut);
318     DEFINE_NATIVE_OP(NATIVE_SERIAL_CONTROL, SerialControl);
319     DEFINE_NATIVE_OP(NATIVE_SERIAL_STATUS, SerialStatus);
320     DEFINE_NATIVE_OP(NATIVE_SERIAL_CLOSE, SerialClose);
321     DEFINE_NATIVE_OP(NATIVE_MAKE_EXECUTABLE, MakeExecutable);
322 gbeauche 1.7 DEFINE_NATIVE_OP(NATIVE_SYNC_HOOK, NQD_sync_hook);
323     DEFINE_NATIVE_OP(NATIVE_BITBLT_HOOK, NQD_bitblt_hook);
324 gbeauche 1.8 DEFINE_NATIVE_OP(NATIVE_FILLRECT_HOOK, NQD_fillrect_hook);
325 gbeauche 1.7 DEFINE_NATIVE_OP(NATIVE_BITBLT, NQD_bitblt);
326 gbeauche 1.8 DEFINE_NATIVE_OP(NATIVE_INVRECT, NQD_invrect);
327     DEFINE_NATIVE_OP(NATIVE_FILLRECT, NQD_fillrect);
328 gbeauche 1.1 #undef DEFINE_NATIVE_OP
329     #endif
330 gbeauche 1.3
331 gbeauche 1.4 // Initialize routine descriptors (if TVECT exists)
332     for (int i = 0; i < NATIVE_OP_MAX; i++) {
333     uint32 tvect = native_op[i].tvect;
334     if (tvect)
335     native_op[i].desc = new SheepRoutineDescriptor(0, tvect);
336     }
337 gbeauche 1.3
338 gbeauche 1.1 return true;
339     }
340    
341 gbeauche 1.2
342     /*
343 gbeauche 1.3 * Delete generated thunks
344     */
345    
346     void ThunksExit(void)
347     {
348     for (int i = 0; i < NATIVE_OP_MAX; i++) {
349     SheepRoutineDescriptor *desc = native_op[i].desc;
350     if (desc)
351     delete desc;
352     }
353     }
354    
355    
356     /*
357 gbeauche 1.2 * Return the native function descriptor (TVECT)
358     */
359    
360 gbeauche 1.1 uint32 NativeTVECT(int selector)
361     {
362     assert(selector < NATIVE_OP_MAX);
363     const uint32 tvect = native_op[selector].tvect;
364     assert(tvect != 0);
365 gbeauche 1.2 return tvect;
366 gbeauche 1.1 }
367    
368 gbeauche 1.2
369     /*
370     * Return the native function address
371     */
372    
373 gbeauche 1.1 uint32 NativeFunction(int selector)
374     {
375     assert(selector < NATIVE_OP_MAX);
376     const uint32 func = native_op[selector].func;
377     assert(func != 0);
378 gbeauche 1.2 return func;
379     }
380    
381    
382     /*
383 gbeauche 1.3 * Return the routine descriptor address of the native function
384     */
385    
386     uint32 NativeRoutineDescriptor(int selector)
387     {
388     assert(selector < NATIVE_OP_MAX);
389     SheepRoutineDescriptor * const desc = native_op[selector].desc;
390     assert(desc != 0);
391     return desc->addr();
392     }
393    
394    
395     /*
396 gbeauche 1.2 * Execute native code from EMUL_OP routine (real mode switch)
397     */
398    
399     void ExecuteNative(int selector)
400     {
401     M68kRegisters r;
402 gbeauche 1.3 Execute68k(NativeRoutineDescriptor(selector), &r);
403 gbeauche 1.1 }