ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/include/thunks.h
Revision: 1.14
Committed: 2006-05-03T21:45:14Z (18 years, 7 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.13: +3 -0 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

# Content
1 /*
2 * thunks.h - 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 #ifndef THUNKS_H
22 #define THUNKS_H
23
24 #include "cpu_emulation.h"
25
26 /*
27 * Native function invocation
28 */
29
30 enum {
31 NATIVE_PATCH_NAME_REGISTRY,
32 NATIVE_VIDEO_INSTALL_ACCEL,
33 NATIVE_VIDEO_VBL,
34 NATIVE_VIDEO_DO_DRIVER_IO,
35 NATIVE_ETHER_AO_GET_HWADDR,
36 NATIVE_ETHER_AO_ADD_MULTI,
37 NATIVE_ETHER_AO_DEL_MULTI,
38 NATIVE_ETHER_AO_SEND_PACKET,
39 NATIVE_ETHER_IRQ,
40 NATIVE_ETHER_INIT,
41 NATIVE_ETHER_TERM,
42 NATIVE_ETHER_OPEN,
43 NATIVE_ETHER_CLOSE,
44 NATIVE_ETHER_WPUT,
45 NATIVE_ETHER_RSRV,
46 NATIVE_SERIAL_NOTHING,
47 NATIVE_SERIAL_OPEN,
48 NATIVE_SERIAL_PRIME_IN,
49 NATIVE_SERIAL_PRIME_OUT,
50 NATIVE_SERIAL_CONTROL,
51 NATIVE_SERIAL_STATUS,
52 NATIVE_SERIAL_CLOSE,
53 NATIVE_GET_RESOURCE,
54 NATIVE_GET_1_RESOURCE,
55 NATIVE_GET_IND_RESOURCE,
56 NATIVE_GET_1_IND_RESOURCE,
57 NATIVE_R_GET_RESOURCE,
58 NATIVE_MAKE_EXECUTABLE,
59 NATIVE_CHECK_LOAD_INVOC,
60 NATIVE_SYNC_HOOK,
61 NATIVE_BITBLT_HOOK,
62 NATIVE_FILLRECT_HOOK,
63 NATIVE_BITBLT,
64 NATIVE_INVRECT,
65 NATIVE_FILLRECT,
66 NATIVE_NAMED_CHECK_LOAD_INVOC,
67 NATIVE_GET_NAMED_RESOURCE,
68 NATIVE_GET_1_NAMED_RESOURCE,
69 NATIVE_OP_MAX
70 };
71
72 // Initialize the thunks system
73 extern bool ThunksInit(void);
74
75 // Exit the thunks system
76 extern void ThunksExit(void);
77
78 // Return the fake PowerPC opcode to handle specified native code
79 #if EMULATED_PPC
80 extern uint32 NativeOpcode(int selector);
81 #endif
82
83 // Return the native function descriptor (TVECT)
84 extern uint32 NativeTVECT(int selector);
85
86 // Return the native function address
87 extern uint32 NativeFunction(int selector);
88
89 // Return the routine descriptor address of the native function
90 extern uint32 NativeRoutineDescriptor(int selector);
91
92
93 /*
94 * Helpers to share 32-bit addressable data with MacOS
95 *
96 * There are two distinct allocatable regions:
97 *
98 * - The Data region is used to share data between MacOS and
99 * SheepShaver. This is stack-like allocation since it is
100 * meant to only hold temporary data which dies at the end
101 * of the current function scope.
102 *
103 * - The Procedure region is used to hold permanent M68K or
104 * PowerPC code to assist native routine implementations.
105 *
106 * - The Procedure region grows up whereas the Data region
107 * grows down. They may intersect into the ZeroPage, which
108 * is a read-only page with all bits set to zero. In practise,
109 * the intersection is unlikely since the Procedure region is
110 * static and the Data region is meant to be small (< 256 KB).
111 */
112
113 class SheepMem {
114 static uint32 align(uint32 size);
115 protected:
116 static uint32 page_size;
117 static uintptr zero_page;
118 static uintptr base;
119 static uintptr data;
120 static uintptr proc;
121 static const uint32 size = 0x80000; // 512 KB
122 public:
123 static bool Init(void);
124 static void Exit(void);
125 static uint32 PageSize();
126 static uint32 ZeroPage();
127 static uint32 Reserve(uint32 size);
128 static void Release(uint32 size);
129 static uint32 ReserveProc(uint32 size);
130 friend class SheepVar;
131 };
132
133 inline uint32 SheepMem::align(uint32 size)
134 {
135 // Align on 4 bytes boundaries
136 return (size + 3) & -4;
137 }
138
139 inline uint32 SheepMem::PageSize()
140 {
141 return page_size;
142 }
143
144 inline uint32 SheepMem::ZeroPage()
145 {
146 return zero_page;
147 }
148
149 inline uint32 SheepMem::Reserve(uint32 size)
150 {
151 data -= align(size);
152 assert(data >= proc);
153 return data;
154 }
155
156 inline void SheepMem::Release(uint32 size)
157 {
158 data += align(size);
159 }
160
161 inline uint32 SheepMem::ReserveProc(uint32 size)
162 {
163 uint32 mproc = proc;
164 proc += align(size);
165 assert(proc < data);
166 return mproc;
167 }
168
169 static inline uint32 SheepProc(const uint8 *proc, uint32 proc_size)
170 {
171 uint32 mac_proc = SheepMem::ReserveProc(proc_size);
172 Host2Mac_memcpy(mac_proc, proc, proc_size);
173 return mac_proc;
174 }
175
176 #define BUILD_SHEEPSHAVER_PROCEDURE(PROC) \
177 static uint32 PROC = 0; \
178 if (PROC == 0) \
179 PROC = SheepProc(PROC##_template, sizeof(PROC##_template))
180
181 class SheepVar
182 {
183 uint32 m_base;
184 uint32 m_size;
185 public:
186 SheepVar(uint32 requested_size);
187 ~SheepVar() { SheepMem::Release(m_size); }
188 uint32 addr() const { return m_base; }
189 };
190
191 inline SheepVar::SheepVar(uint32 requested_size)
192 {
193 m_size = SheepMem::align(requested_size);
194 m_base = SheepMem::Reserve(m_size);
195 }
196
197 // TODO: optimize for 32-bit platforms
198
199 template< int requested_size >
200 struct SheepArray : public SheepVar
201 {
202 SheepArray() : SheepVar(requested_size) { }
203 };
204
205 struct SheepVar32 : public SheepVar
206 {
207 SheepVar32() : SheepVar(4) { }
208 SheepVar32(uint32 value) : SheepVar(4) { set_value(value); }
209 uint32 value() const { return ReadMacInt32(addr()); }
210 void set_value(uint32 v) { WriteMacInt32(addr(), v); }
211 };
212
213 struct SheepString : public SheepVar
214 {
215 SheepString(const char *str) : SheepVar(strlen(str) + 1)
216 { if (str) strcpy(value(), str); else WriteMacInt8(addr(), 0); }
217 char *value() const
218 { return (char *)Mac2HostAddr(addr()); }
219 };
220
221 #endif