-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch68k.c
215 lines (185 loc) · 5 KB
/
patch68k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright (c) 2023 Elliot Nunn */
/* Licensed under the MIT license */
#include <Gestalt.h>
#include <Memory.h>
#include <Patches.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "log.h"
#include "printf.h"
#include "patch68k.h"
struct block {
void *original;
long vector;
unsigned char code[128];
};
static int hex(char c) {
if ('0'<=c && c<='9') return c - '0';
if ('a'<=c && c<='f') return c - 'a' + 10;
return -1;
}
static void *getvec(long vec) {
if (vec == 0) {
return NULL;
} if (vec & 0xffff0000) {
return NULL; // GetGestaltProcPtr is sometimes unavailable
} else if ((vec & 0xa800) == 0xa800) {
return GetToolTrapAddress(vec);
} else if ((vec & 0xa800) == 0xa000) {
return GetOSTrapAddress(vec);
} else {
return *(void **)vec;
}
}
static void setvec(long vec, void *addr) {
if (vec == 0) {
// no nothing
} if (vec & 0xffff0000) {
SelectorFunctionUPP old;
if (NewGestalt(vec, addr) != noErr)
ReplaceGestalt(vec, addr, &old);
} else if ((vec & 0xa800) == 0xa800) {
SetToolTrapAddress(addr, vec);
} else if ((vec & 0xa800) == 0xa000) {
SetOSTrapAddress(addr, vec);
} else {
*(void **)vec = addr;
}
}
void *Patch68k(unsigned long vector, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
// if this allocation fails then the system is crashed anyway
struct block *block = (struct block *)NewPtrSysClear(sizeof (struct block));
block->vector = vector;
block->original = getvec(vector);
unsigned char *code = block->code;
int midhex = 0;
int labels[26] = {};
int fixups[64];
int nfixups = 0;
for (const char *i=fmt; *i!=0;) {
if (*i == '%') {
i++;
if (*i == 'b') {
i++;
*code++ = va_arg(argp, int);
} else if (*i == 'w') {
i++;
short word = va_arg(argp, int);
*code++ = word >> 8;
*code++ = word;
} else if (*i == 'l') {
i++;
long lword = va_arg(argp, long);
*code++ = lword >> 24;
*code++ = lword >> 16;
*code++ = lword >> 8;
*code++ = lword;
} else if (*i == 'o') {
i++;
long lword = (long)block->original;
// Special case: never JMP or JSR to NULL or 0xffffffff
if (lword == -1 || lword == 0) {
if (code[-2] == 0x4e && code[-1] == 0xf9) { // JMP
code[-2] = 0x4e; // becomes RTS
code[-1] = 0x75;
} else if (code[-2] == 0x4e && code[-1] == 0xb9) { // JSR
code[-2] = 0x60; // becomes BRA.S .+6
code[-1] = 0x04;
}
}
*code++ = lword >> 24;
*code++ = lword >> 16;
*code++ = lword >> 8;
*code++ = lword;
} else if (*i >= 'A' && *i <= 'Z') {
fixups[nfixups++] = code - block->code;
*code++ = *i;
if ((code - block->code) & 1) code++;
i++;
while (*i >= 'A' && *i <= 'Z') i++; // ignore extra letters
}
} else if (*i >= 'A' && *i <= 'Z') {
labels[*i-'A'] = code - block->code;
i++;
while (*i >= 'A' && *i <= 'Z') i++; // ignore extra letters
} else if (hex(*i) != -1) {
if (midhex) {
*code++ |= hex(*i);
} else {
*code = hex(*i) << 4;
}
midhex = !midhex;
i++;
} else {
i++; // ignore other letters
}
}
va_end(argp);
for (int i=0; i<nfixups; i++) {
unsigned char *caller = block->code + fixups[i];
unsigned char *callee = block->code + labels[*caller-'A'];
if (fixups[i] % 2) {
// At odd offsets, the off-by-one offset byte in a bra.s
*caller = (signed char)(callee - caller - 1);
} else {
// At even offsets, the two-byte offset in lea, bra etc
*(short *)caller = (signed short)(callee - caller);
}
}
// Fallthrough code to remove the patch
*code++ = 0x48; // MOVEM.L d0-d2/a0-a2,-(sp)
*code++ = 0xe7;
*code++ = 0xe0;
*code++ = 0xe0;
*code++ = 0x41; // LEA copycode,a0
*code++ = 0xfa;
*code++ = 0x00;
*code++ = 0x10;
*code++ = 0x43; // LEA entry,a1
*code++ = 0xfa;
short delta = block->code - code;
*code++ = delta >> 8;
*code++ = delta;
*code++ = 0x70; // MOVEQ.L #12,d0
*code++ = 0x0c;
*code++ = 0xa0; // _BlockMove
*code++ = 0x2e;
*code++ = 0x4c; // MOVEM.L (sp)+,d0-d2/a0-a2
*code++ = 0xdf;
*code++ = 0x07;
*code++ = 0x07;
*code++ = 0x4e; // RTS
*code++ = 0x75;
// COPYCODE: (12 bytes to force BlockMove to clear the icache)
if (block->original) {
*code++ = 0x4e; // JMP
*code++ = 0xf9;
*code++ = (unsigned long)block->original >> 24;
*code++ = (unsigned long)block->original >> 16;
*code++ = (unsigned long)block->original >> 8;
*code++ = (unsigned long)block->original;
} else {
*code++ = 0x4e; // RTS
*code++ = 0x75;
}
code += 10; // at least 12 bytes for BlockMove to block-move
BlockMove(block, block, sizeof *block); // clear 68k emulator cache
SetPtrSize((Ptr)block, (char *)code - (char *)block); // shrink
setvec(vector, &block->code); // install
if (LogEnable) {
printf("vector=%X, oldcode=%p, newcode=%p\n",
vector, block->original, &block->code);
int cnt = code - block->code;
for (int i=0; i<cnt; i+=2) {
printf("%s%04x%c",
(i%16) ? "" : " ",
0xffff & *(uint16_t *)(block->code + i),
(i==cnt-2 || (i%16)==14) ? '\n' : ' ');
}
}
return &block->code;
}