ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/thunks.cpp
Revision: 1.7
Committed: 2004-04-18T23:03:50Z (20 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.6: +14 -0 lines
Log Message:
Start Native QuickDraw acceleration

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     +------------+--------------------------+--+----------+------------+
41     | 6 | |FN| OP | 2 |
42     +------------+--------------------------+--+----------+------------+
43     0 5 |6 19 20 21 25 26 31
44     */
45    
46     #define POWERPC_NATIVE_OP(LR, OP) \
47     (POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2)
48    
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     case NATIVE_DISABLE_INTERRUPT:
59     case NATIVE_ENABLE_INTERRUPT:
60 gbeauche 1.6 case NATIVE_CHECK_LOAD_INVOC:
61 gbeauche 1.1 opcode = POWERPC_NATIVE_OP(0, selector);
62     break;
63     case NATIVE_PATCH_NAME_REGISTRY:
64     case NATIVE_VIDEO_INSTALL_ACCEL:
65     case NATIVE_VIDEO_VBL:
66     case NATIVE_VIDEO_DO_DRIVER_IO:
67     case NATIVE_ETHER_IRQ:
68     case NATIVE_ETHER_INIT:
69     case NATIVE_ETHER_TERM:
70     case NATIVE_ETHER_OPEN:
71     case NATIVE_ETHER_CLOSE:
72     case NATIVE_ETHER_WPUT:
73     case NATIVE_ETHER_RSRV:
74     case NATIVE_SERIAL_NOTHING:
75     case NATIVE_SERIAL_OPEN:
76     case NATIVE_SERIAL_PRIME_IN:
77     case NATIVE_SERIAL_PRIME_OUT:
78     case NATIVE_SERIAL_CONTROL:
79     case NATIVE_SERIAL_STATUS:
80     case NATIVE_SERIAL_CLOSE:
81     case NATIVE_GET_RESOURCE:
82     case NATIVE_GET_1_RESOURCE:
83     case NATIVE_GET_IND_RESOURCE:
84     case NATIVE_GET_1_IND_RESOURCE:
85     case NATIVE_R_GET_RESOURCE:
86     case NATIVE_MAKE_EXECUTABLE:
87 gbeauche 1.7 case NATIVE_SYNC_HOOK:
88     case NATIVE_BITBLT_HOOK:
89     case NATIVE_FILLRECT_HOOK:
90     case NATIVE_BITBLT:
91     case NATIVE_INVRECT:
92     case NATIVE_FILLRECT_8:
93     case NATIVE_FILLRECT_32:
94 gbeauche 1.1 opcode = POWERPC_NATIVE_OP(1, selector);
95     break;
96     default:
97     abort();
98     }
99     return opcode;
100     }
101     #endif
102    
103 gbeauche 1.2
104     /*
105 gbeauche 1.6 * Generate PowerPC thunks for GetResource() replacements
106     */
107    
108     #if EMULATED_PPC
109     static uint32 get_resource_func;
110     static uint32 get_1_resource_func;
111     static uint32 get_ind_resource_func;
112     static uint32 get_1_ind_resource_func;
113     static uint32 r_get_resource_func;
114    
115     static void generate_powerpc_thunks(void)
116     {
117     static uint32 get_resource_template[] = {
118     PL(0x7c0802a6), // mflr r0
119     PL(0x90010008), // stw r0,8(r1)
120     PL(0x9421ffbc), // stwu r1,-68(r1)
121     PL(0x90610038), // stw r3,56(r1)
122     PL(0x9081003c), // stw r4,60(r1)
123     PL(0x00000000), // lwz r0,XLM_GET_RESOURCE(r0)
124     PL(0x80402834), // lwz r2,XLM_RES_LIB_TOC(r0)
125     PL(0x7c0903a6), // mtctr r0
126     PL(0x4e800421), // bctrl
127     PL(0x90610040), // stw r3,64(r1)
128     PL(0x80610038), // lwz r3,56(r1)
129     PL(0xa881003e), // lha r4,62(r1)
130     PL(0x80a10040), // lwz r5,64(r1)
131     PL(0x00000001), // <check_load_invoc>
132     PL(0x80610040), // lwz r3,64(r1)
133     PL(0x8001004c), // lwz r0,76(r1)
134     PL(0x7c0803a6), // mtlr r0
135     PL(0x38210044), // addi r1,r1,68
136     PL(0x4e800020) // blr
137     };
138     const uint32 get_resource_template_size = sizeof(get_resource_template);
139    
140     int xlm_index = -1, check_load_invoc_index = -1;
141     for (int i = 0; i < get_resource_template_size/4; i++) {
142     uint32 opcode = ntohl(get_resource_template[i]);
143     switch (opcode) {
144     case 0x00000000:
145     xlm_index = i;
146     break;
147     case 0x00000001:
148     check_load_invoc_index = i;
149     break;
150     }
151     }
152     assert(xlm_index != -1 && check_load_invoc_index != -1);
153    
154     uint32 check_load_invoc_opcode = NativeOpcode(NATIVE_CHECK_LOAD_INVOC);
155     uintptr base;
156    
157     // GetResource()
158     get_resource_func = base = SheepMem::Reserve(get_resource_template_size);
159     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
160     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_RESOURCE);
161     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
162    
163     // Get1Resource()
164     get_1_resource_func = base = SheepMem::Reserve(get_resource_template_size);
165     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
166     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_1_RESOURCE);
167     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
168    
169     // GetIndResource()
170     get_ind_resource_func = base = SheepMem::Reserve(get_resource_template_size);
171     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
172     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_IND_RESOURCE);
173     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
174    
175     // Get1IndResource()
176     get_1_ind_resource_func = base = SheepMem::Reserve(get_resource_template_size);
177     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
178     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_GET_1_IND_RESOURCE);
179     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
180    
181     // RGetResource()
182     r_get_resource_func = base = SheepMem::Reserve(get_resource_template_size);
183     Host2Mac_memcpy(base, get_resource_template, get_resource_template_size);
184     WriteMacInt32(base + xlm_index * 4, 0x80000000 | XLM_R_GET_RESOURCE);
185     WriteMacInt32(base + check_load_invoc_index * 4, check_load_invoc_opcode);
186     }
187     #endif
188    
189    
190     /*
191 gbeauche 1.2 * Initialize the thunks system
192     */
193    
194 gbeauche 1.1 struct native_op_t {
195     uint32 tvect;
196     uint32 func;
197 gbeauche 1.3 SheepRoutineDescriptor *desc;
198 gbeauche 1.1 };
199     static native_op_t native_op[NATIVE_OP_MAX];
200    
201     bool ThunksInit(void)
202     {
203     #if EMULATED_PPC
204     for (int i = 0; i < NATIVE_OP_MAX; i++) {
205     uintptr base = SheepMem::Reserve(12);
206     WriteMacInt32(base + 0, base + 8);
207     WriteMacInt32(base + 4, 0); // Fake TVECT
208     WriteMacInt32(base + 8, NativeOpcode(i));
209     native_op[i].tvect = base;
210     native_op[i].func = base + 8;
211     }
212 gbeauche 1.6 #if POWERPC_GET_RESOURCE_THUNKS
213     generate_powerpc_thunks();
214     native_op[NATIVE_GET_RESOURCE].func = get_resource_func;
215     native_op[NATIVE_GET_1_RESOURCE].func = get_1_resource_func;
216     native_op[NATIVE_GET_IND_RESOURCE].func = get_ind_resource_func;
217     native_op[NATIVE_GET_1_IND_RESOURCE].func = get_1_ind_resource_func;
218     native_op[NATIVE_R_GET_RESOURCE].func = r_get_resource_func;
219     #endif
220 gbeauche 1.1 #else
221 gbeauche 1.5 #if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__))
222 gbeauche 1.1 #define DEFINE_NATIVE_OP(ID, FUNC) do { \
223     uintptr base = SheepMem::Reserve(8); \
224     WriteMacInt32(base + 0, (uint32)FUNC); \
225     WriteMacInt32(base + 4, 0); /*Fake TVECT*/ \
226     native_op[ID].tvect = base; \
227     native_op[ID].func = (uint32)FUNC; \
228     } while (0)
229     #elif defined(__BEOS__)
230     #define DEFINE_NATIVE_OP(ID, FUNC) do { \
231     native_op[ID].tvect = FUNC; \
232     native_op[ID].func = ((uint32 *)FUNC)[0]; \
233     } while (0)
234     #else
235     #error "FIXME: define NativeOp for your platform"
236     #endif
237 gbeauche 1.4 // FIXME: add GetResource() and friends for completeness
238 gbeauche 1.2 DEFINE_NATIVE_OP(NATIVE_PATCH_NAME_REGISTRY, DoPatchNameRegistry);
239     DEFINE_NATIVE_OP(NATIVE_VIDEO_INSTALL_ACCEL, VideoInstallAccel);
240     DEFINE_NATIVE_OP(NATIVE_VIDEO_VBL, VideoVBL);
241     DEFINE_NATIVE_OP(NATIVE_VIDEO_DO_DRIVER_IO, VideoDoDriverIO);
242     DEFINE_NATIVE_OP(NATIVE_ETHER_IRQ, EtherIRQ);
243     DEFINE_NATIVE_OP(NATIVE_ETHER_INIT, InitStreamModule);
244     DEFINE_NATIVE_OP(NATIVE_ETHER_TERM, TerminateStreamModule);
245     DEFINE_NATIVE_OP(NATIVE_ETHER_OPEN, ether_open);
246     DEFINE_NATIVE_OP(NATIVE_ETHER_CLOSE, ether_close);
247     DEFINE_NATIVE_OP(NATIVE_ETHER_WPUT, ether_wput);
248     DEFINE_NATIVE_OP(NATIVE_ETHER_RSRV, ether_rsrv);
249 gbeauche 1.1 DEFINE_NATIVE_OP(NATIVE_SERIAL_NOTHING, SerialNothing);
250     DEFINE_NATIVE_OP(NATIVE_SERIAL_OPEN, SerialOpen);
251     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_IN, SerialPrimeIn);
252     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_OUT, SerialPrimeOut);
253     DEFINE_NATIVE_OP(NATIVE_SERIAL_CONTROL, SerialControl);
254     DEFINE_NATIVE_OP(NATIVE_SERIAL_STATUS, SerialStatus);
255     DEFINE_NATIVE_OP(NATIVE_SERIAL_CLOSE, SerialClose);
256     DEFINE_NATIVE_OP(NATIVE_MAKE_EXECUTABLE, MakeExecutable);
257 gbeauche 1.7 DEFINE_NATIVE_OP(NATIVE_SYNC_HOOK, NQD_sync_hook);
258     DEFINE_NATIVE_OP(NATIVE_BITBLT_HOOK, NQD_bitblt_hook);
259     // DEFINE_NATIVE_OP(NATIVE_FILLRECT_HOOK, NQD_fillrect_hook);
260     DEFINE_NATIVE_OP(NATIVE_BITBLT, NQD_bitblt);
261     // DEFINE_NATIVE_OP(NATIVE_INVRECT, NQD_invrect);
262     // DEFINE_NATIVE_OP(NATIVE_FILLRECT_8, NQD_fillrect8);
263     // DEFINE_NATIVE_OP(NATIVE_FILLRECT_32, NQD_fillrect32);
264 gbeauche 1.1 #undef DEFINE_NATIVE_OP
265     #endif
266 gbeauche 1.3
267 gbeauche 1.4 // Initialize routine descriptors (if TVECT exists)
268     for (int i = 0; i < NATIVE_OP_MAX; i++) {
269     uint32 tvect = native_op[i].tvect;
270     if (tvect)
271     native_op[i].desc = new SheepRoutineDescriptor(0, tvect);
272     }
273 gbeauche 1.3
274 gbeauche 1.1 return true;
275     }
276    
277 gbeauche 1.2
278     /*
279 gbeauche 1.3 * Delete generated thunks
280     */
281    
282     void ThunksExit(void)
283     {
284     for (int i = 0; i < NATIVE_OP_MAX; i++) {
285     SheepRoutineDescriptor *desc = native_op[i].desc;
286     if (desc)
287     delete desc;
288     }
289     }
290    
291    
292     /*
293 gbeauche 1.2 * Return the native function descriptor (TVECT)
294     */
295    
296 gbeauche 1.1 uint32 NativeTVECT(int selector)
297     {
298     assert(selector < NATIVE_OP_MAX);
299     const uint32 tvect = native_op[selector].tvect;
300     assert(tvect != 0);
301 gbeauche 1.2 return tvect;
302 gbeauche 1.1 }
303    
304 gbeauche 1.2
305     /*
306     * Return the native function address
307     */
308    
309 gbeauche 1.1 uint32 NativeFunction(int selector)
310     {
311     assert(selector < NATIVE_OP_MAX);
312     const uint32 func = native_op[selector].func;
313     assert(func != 0);
314 gbeauche 1.2 return func;
315     }
316    
317    
318     /*
319 gbeauche 1.3 * Return the routine descriptor address of the native function
320     */
321    
322     uint32 NativeRoutineDescriptor(int selector)
323     {
324     assert(selector < NATIVE_OP_MAX);
325     SheepRoutineDescriptor * const desc = native_op[selector].desc;
326     assert(desc != 0);
327     return desc->addr();
328     }
329    
330    
331     /*
332 gbeauche 1.2 * Execute native code from EMUL_OP routine (real mode switch)
333     */
334    
335     void ExecuteNative(int selector)
336     {
337     M68kRegisters r;
338 gbeauche 1.3 Execute68k(NativeRoutineDescriptor(selector), &r);
339 gbeauche 1.1 }