ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/thunks.cpp
Revision: 1.1
Committed: 2003-12-04T17:26:35Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Log Message:
Add new thunking system for 64-bit fixes.

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    
26     // Native function declarations
27     #include "main.h"
28     #include "video.h"
29     #include "name_registry.h"
30     #include "serial.h"
31     #include "ether.h"
32    
33    
34     /* NativeOp instruction format:
35     +------------+--------------------------+--+----------+------------+
36     | 6 | |FN| OP | 2 |
37     +------------+--------------------------+--+----------+------------+
38     0 5 |6 19 20 21 25 26 31
39     */
40    
41     #define POWERPC_NATIVE_OP(LR, OP) \
42     (POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2)
43    
44     // Return the fake PowerPC opcode to handle specified native code
45     #if EMULATED_PPC
46     uint32 NativeOpcode(int selector)
47     {
48     uint32 opcode;
49     switch (selector) {
50     case NATIVE_DISABLE_INTERRUPT:
51     case NATIVE_ENABLE_INTERRUPT:
52     opcode = POWERPC_NATIVE_OP(0, selector);
53     break;
54     case NATIVE_PATCH_NAME_REGISTRY:
55     case NATIVE_VIDEO_INSTALL_ACCEL:
56     case NATIVE_VIDEO_VBL:
57     case NATIVE_VIDEO_DO_DRIVER_IO:
58     case NATIVE_ETHER_IRQ:
59     case NATIVE_ETHER_INIT:
60     case NATIVE_ETHER_TERM:
61     case NATIVE_ETHER_OPEN:
62     case NATIVE_ETHER_CLOSE:
63     case NATIVE_ETHER_WPUT:
64     case NATIVE_ETHER_RSRV:
65     case NATIVE_SERIAL_NOTHING:
66     case NATIVE_SERIAL_OPEN:
67     case NATIVE_SERIAL_PRIME_IN:
68     case NATIVE_SERIAL_PRIME_OUT:
69     case NATIVE_SERIAL_CONTROL:
70     case NATIVE_SERIAL_STATUS:
71     case NATIVE_SERIAL_CLOSE:
72     case NATIVE_GET_RESOURCE:
73     case NATIVE_GET_1_RESOURCE:
74     case NATIVE_GET_IND_RESOURCE:
75     case NATIVE_GET_1_IND_RESOURCE:
76     case NATIVE_R_GET_RESOURCE:
77     case NATIVE_MAKE_EXECUTABLE:
78     opcode = POWERPC_NATIVE_OP(1, selector);
79     break;
80     default:
81     abort();
82     }
83     return opcode;
84     }
85     #endif
86    
87     // NativeOp -> { TVECT, function base } mappings
88     struct native_op_t {
89     uint32 tvect;
90     uint32 func;
91     };
92     static native_op_t native_op[NATIVE_OP_MAX];
93    
94     // Initialize the thunks system
95     bool ThunksInit(void)
96     {
97     #if EMULATED_PPC
98     for (int i = 0; i < NATIVE_OP_MAX; i++) {
99     uintptr base = SheepMem::Reserve(12);
100     WriteMacInt32(base + 0, base + 8);
101     WriteMacInt32(base + 4, 0); // Fake TVECT
102     WriteMacInt32(base + 8, NativeOpcode(i));
103     native_op[i].tvect = base;
104     native_op[i].func = base + 8;
105     }
106     #else
107     #if defined(__linux__)
108     #define DEFINE_NATIVE_OP(ID, FUNC) do { \
109     uintptr base = SheepMem::Reserve(8); \
110     WriteMacInt32(base + 0, (uint32)FUNC); \
111     WriteMacInt32(base + 4, 0); /*Fake TVECT*/ \
112     native_op[ID].tvect = base; \
113     native_op[ID].func = (uint32)FUNC; \
114     } while (0)
115     #elif defined(__BEOS__)
116     #define DEFINE_NATIVE_OP(ID, FUNC) do { \
117     native_op[ID].tvect = FUNC; \
118     native_op[ID].func = ((uint32 *)FUNC)[0]; \
119     } while (0)
120     #else
121     #error "FIXME: define NativeOp for your platform"
122     #endif
123     DEFINE_NATIVE_OP(NATIVE_SERIAL_NOTHING, SerialNothing);
124     DEFINE_NATIVE_OP(NATIVE_SERIAL_OPEN, SerialOpen);
125     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_IN, SerialPrimeIn);
126     DEFINE_NATIVE_OP(NATIVE_SERIAL_PRIME_OUT, SerialPrimeOut);
127     DEFINE_NATIVE_OP(NATIVE_SERIAL_CONTROL, SerialControl);
128     DEFINE_NATIVE_OP(NATIVE_SERIAL_STATUS, SerialStatus);
129     DEFINE_NATIVE_OP(NATIVE_SERIAL_CLOSE, SerialClose);
130     DEFINE_NATIVE_OP(NATIVE_MAKE_EXECUTABLE, MakeExecutable);
131     #undef DEFINE_NATIVE_OP
132     #endif
133     return true;
134     }
135    
136     // Return the native function descriptor (TVECT)
137     uint32 NativeTVECT(int selector)
138     {
139     assert(selector < NATIVE_OP_MAX);
140     const uint32 tvect = native_op[selector].tvect;
141     assert(tvect != 0);
142     return native_op[selector].tvect;
143     }
144    
145     // Return the native function address
146     uint32 NativeFunction(int selector)
147     {
148     assert(selector < NATIVE_OP_MAX);
149     const uint32 func = native_op[selector].func;
150     assert(func != 0);
151     return native_op[selector].func;
152     }