ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/thunks.cpp
(Generate patch)

Comparing SheepShaver/src/thunks.cpp (file contents):
Revision 1.1 by gbeauche, 2003-12-04T17:26:35Z vs.
Revision 1.7 by gbeauche, 2004-04-18T23:03:50Z

# Line 22 | Line 22
22   #include "thunks.h"
23   #include "emul_op.h"
24   #include "cpu_emulation.h"
25 + #include "xlowmem.h"
26  
27   // Native function declarations
28   #include "main.h"
# Line 29 | Line 30
30   #include "name_registry.h"
31   #include "serial.h"
32   #include "ether.h"
33 + #include "macos_util.h"
34 +
35 + // Generate PowerPC thunks for GetResource() replacements?
36 + #define POWERPC_GET_RESOURCE_THUNKS 1
37  
38  
39   /*              NativeOp instruction format:
# Line 41 | Line 46
46   #define POWERPC_NATIVE_OP(LR, OP) \
47                  (POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2)
48  
49 < // Return the fake PowerPC opcode to handle specified native code
49 > /*
50 > *  Return the fake PowerPC opcode to handle specified native code
51 > */
52 >
53   #if EMULATED_PPC
54   uint32 NativeOpcode(int selector)
55   {
# Line 49 | Line 57 | uint32 NativeOpcode(int selector)
57          switch (selector) {
58          case NATIVE_DISABLE_INTERRUPT:
59          case NATIVE_ENABLE_INTERRUPT:
60 +        case NATIVE_CHECK_LOAD_INVOC:
61                  opcode = POWERPC_NATIVE_OP(0, selector);
62                  break;
63          case NATIVE_PATCH_NAME_REGISTRY:
# Line 75 | Line 84 | uint32 NativeOpcode(int selector)
84          case NATIVE_GET_1_IND_RESOURCE:
85          case NATIVE_R_GET_RESOURCE:
86          case NATIVE_MAKE_EXECUTABLE:
87 +        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                  opcode = POWERPC_NATIVE_OP(1, selector);
95                  break;
96          default:
# Line 84 | Line 100 | uint32 NativeOpcode(int selector)
100   }
101   #endif
102  
103 < // NativeOp -> { TVECT, function base } mappings
103 >
104 > /*
105 > *  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 > *  Initialize the thunks system
192 > */
193 >
194   struct native_op_t {
195          uint32 tvect;
196          uint32 func;
197 +        SheepRoutineDescriptor *desc;
198   };
199   static native_op_t native_op[NATIVE_OP_MAX];
200  
94 // Initialize the thunks system
201   bool ThunksInit(void)
202   {
203   #if EMULATED_PPC
# Line 103 | Line 209 | bool ThunksInit(void)
209                  native_op[i].tvect = base;
210                  native_op[i].func  = base + 8;
211          }
212 + #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   #else
221 < #if defined(__linux__)
221 > #if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__))
222   #define DEFINE_NATIVE_OP(ID, FUNC) do {                         \
223                  uintptr base = SheepMem::Reserve(8);            \
224                  WriteMacInt32(base + 0, (uint32)FUNC);          \
# Line 120 | Line 234 | bool ThunksInit(void)
234   #else
235   #error "FIXME: define NativeOp for your platform"
236   #endif
237 +        // FIXME: add GetResource() and friends for completeness
238 +        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          DEFINE_NATIVE_OP(NATIVE_SERIAL_NOTHING, SerialNothing);
250          DEFINE_NATIVE_OP(NATIVE_SERIAL_OPEN, SerialOpen);
251          DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_IN, SerialPrimeIn);
# Line 128 | Line 254 | bool ThunksInit(void)
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 +        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   #undef DEFINE_NATIVE_OP
265   #endif
266 +
267 +        // 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 +
274          return true;
275   }
276  
277 < // Return the native function descriptor (TVECT)
277 >
278 > /*
279 > *  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 > *  Return the native function descriptor (TVECT)
294 > */
295 >
296   uint32 NativeTVECT(int selector)
297   {
298          assert(selector < NATIVE_OP_MAX);
299          const uint32 tvect = native_op[selector].tvect;
300          assert(tvect != 0);
301 <        return native_op[selector].tvect;
301 >        return tvect;
302   }
303  
304 < // Return the native function address
304 >
305 > /*
306 > *  Return the native function address
307 > */
308 >
309   uint32 NativeFunction(int selector)
310   {
311          assert(selector < NATIVE_OP_MAX);
312          const uint32 func = native_op[selector].func;
313          assert(func != 0);
314 <        return native_op[selector].func;
314 >        return func;
315 > }
316 >
317 >
318 > /*
319 > *  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 > *  Execute native code from EMUL_OP routine (real mode switch)
333 > */
334 >
335 > void ExecuteNative(int selector)
336 > {
337 >        M68kRegisters r;
338 >        Execute68k(NativeRoutineDescriptor(selector), &r);
339   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines