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