ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/thunks.cpp
Revision: 1.6
Committed: 2004-01-24T11:28:04Z (20 years, 3 months ago) by gbeauche
Branch: MAIN
Changes since 1.5: +99 -0 lines
Log Message:
Generate PowerPC code wrapping GetResource() replacements. That way, it's
a normal PPC function invocation that can be JIT compiled to native code
instead of nesting execute() calls which may lead to use the interpreter
(this took around 11% of total execution time on boot, downto 3%).

Also, optimize some SheepShaver EmulOps and actually report non-CTI.

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