-
Notifications
You must be signed in to change notification settings - Fork 16
/
jit_metadata.c
223 lines (184 loc) · 6.68 KB
/
jit_metadata.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
216
217
218
219
220
221
222
223
#include "jit_metadata.h"
#include "defs_6502.h"
#include "util.h"
#include "asm/asm_jit.h"
#include "asm/asm_jit_defs.h"
#include <assert.h>
struct jit_metadata {
void* p_jit_base;
void* p_jit_ptr_no_code;
void* p_jit_ptr_dynamic;
uint32_t* p_jit_ptrs;
int32_t code_blocks[k_6502_addr_space_size];
};
struct jit_metadata*
jit_metadata_create(void* p_jit_base,
void* p_jit_ptr_no_code,
void* p_jit_ptr_dynamic,
uint32_t* p_jit_ptrs) {
uint32_t i;
struct jit_metadata* p_metadata = util_mallocz(sizeof(struct jit_metadata));
p_metadata->p_jit_base = p_jit_base;
p_metadata->p_jit_ptr_no_code = p_jit_ptr_no_code;
p_metadata->p_jit_ptr_dynamic = p_jit_ptr_dynamic;
p_metadata->p_jit_ptrs = p_jit_ptrs;
for (i = 0; i < k_6502_addr_space_size; ++i) {
p_metadata->p_jit_ptrs[i] =
(uint32_t) (uintptr_t) p_metadata->p_jit_ptr_no_code;
p_metadata->code_blocks[i] = -1;
}
return p_metadata;
}
void
jit_metadata_destroy(struct jit_metadata* p_metadata) {
util_free(p_metadata);
}
void*
jit_metadata_get_host_block_address(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
void* p_jit_ptr = p_metadata->p_jit_base;
return ((uint8_t*) p_jit_ptr + (addr_6502 * K_JIT_BYTES_PER_BYTE));
}
void*
jit_metadata_get_host_jit_ptr(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
void* p_jit_ptr;
uintptr_t jit_ptr = (uintptr_t) p_metadata->p_jit_ptrs[addr_6502];
jit_ptr |= (uintptr_t) p_metadata->p_jit_base;
p_jit_ptr = (void*) jit_ptr;
return p_jit_ptr;
}
int
jit_metadata_is_pc_in_code_block(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
int ret = (p_metadata->code_blocks[addr_6502] != -1);
return ret;
}
int32_t
jit_metadata_get_code_block(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
return p_metadata->code_blocks[addr_6502];
}
int
jit_metadata_is_jit_ptr_no_code(struct jit_metadata* p_metadata,
void* p_jit_ptr) {
return (p_jit_ptr == p_metadata->p_jit_ptr_no_code);
}
int
jit_metadata_is_jit_ptr_dynamic(struct jit_metadata* p_metadata,
void* p_jit_ptr) {
return (p_jit_ptr == p_metadata->p_jit_ptr_dynamic);
}
int
jit_metadata_has_invalidated_code(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, addr_6502);
if (p_jit_ptr == p_metadata->p_jit_ptr_no_code) {
assert(p_metadata->code_blocks[addr_6502] == -1);
return 0;
}
assert(p_metadata->code_blocks[addr_6502] != -1);
/* Need to explicitly handle dynamic opcodes. The expectation is that they
* always show as self-modified. We can't rely on the JIT code bytes in memory
* on ARM64, because of the way the invalidation write works.
*/
if (p_jit_ptr == p_metadata->p_jit_ptr_dynamic) {
return 1;
}
return asm_jit_is_invalidated_code_at(p_jit_ptr);
}
uint16_t
jit_metadata_get_block_addr_from_host_pc(struct jit_metadata* p_metadata,
void* p_host_pc) {
size_t block_addr_6502;
void* p_jit_base = p_metadata->p_jit_base;
block_addr_6502 = ((uint8_t*) p_host_pc - (uint8_t*) p_jit_base);
block_addr_6502 /= K_JIT_BYTES_PER_BYTE;
assert(block_addr_6502 < k_6502_addr_space_size);
return (uint16_t) block_addr_6502;
}
uint16_t
jit_metadata_get_6502_pc_from_host_pc(struct jit_metadata* p_metadata,
void* p_host_pc) {
uint16_t addr_6502;
void* p_curr_jit_ptr = NULL;
uint16_t ret_addr_6502 = 0;
uint16_t host_block_6502 =
jit_metadata_get_block_addr_from_host_pc(p_metadata, p_host_pc);
int32_t code_block_6502 = jit_metadata_get_code_block(p_metadata,
host_block_6502);
if (((uintptr_t) p_host_pc & (K_JIT_BYTES_PER_BYTE - 1)) == 0) {
return host_block_6502;
}
assert(code_block_6502 != -1);
for (addr_6502 = code_block_6502;
(jit_metadata_get_code_block(p_metadata, addr_6502) ==
code_block_6502);
++addr_6502) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, addr_6502);
assert(!jit_metadata_is_jit_ptr_no_code(p_metadata, p_jit_ptr));
if (jit_metadata_is_jit_ptr_dynamic(p_metadata, p_jit_ptr)) {
continue;
}
if (p_jit_ptr > p_host_pc) {
break;
}
if (p_jit_ptr != p_curr_jit_ptr) {
p_curr_jit_ptr = p_jit_ptr;
ret_addr_6502 = addr_6502;
}
}
assert(p_curr_jit_ptr != NULL);
return ret_addr_6502;
}
void
jit_metadata_set_jit_ptr(struct jit_metadata* p_metadata,
uint16_t addr_6502,
uint32_t jit_ptr) {
p_metadata->p_jit_ptrs[addr_6502] = jit_ptr;
}
void
jit_metadata_make_jit_ptr_no_code(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
uint32_t jit_ptr = (uint32_t) (uintptr_t) p_metadata->p_jit_ptr_no_code;
p_metadata->p_jit_ptrs[addr_6502] = jit_ptr;
}
void
jit_metadata_make_jit_ptr_dynamic(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
uint32_t jit_ptr = (uint32_t) (uintptr_t) p_metadata->p_jit_ptr_dynamic;
p_metadata->p_jit_ptrs[addr_6502] = jit_ptr;
}
void
jit_metadata_set_code_block(struct jit_metadata* p_jit_metadata,
uint16_t addr_6502,
int32_t code_block) {
p_jit_metadata->code_blocks[addr_6502] = code_block;
}
void
jit_metadata_invalidate_jump_target(struct jit_metadata* p_metadata,
uint16_t addr_6502) {
void* p_jit_ptr = jit_metadata_get_host_block_address(p_metadata, addr_6502);
asm_jit_invalidate_code_at(p_jit_ptr);
}
void
jit_metadata_invalidate_code(struct jit_metadata* p_metadata, uint16_t addr) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, addr);
asm_jit_invalidate_code_at(p_jit_ptr);
}
void
jit_metadata_clear_block(struct jit_metadata* p_metadata, uint16_t addr_6502) {
uint16_t i_addr_6502;
int32_t next_code_block_addr_6502;
int32_t code_block_addr_6502 = jit_metadata_get_code_block(p_metadata,
addr_6502);
assert(code_block_addr_6502 != -1);
i_addr_6502 = addr_6502;
do {
jit_metadata_make_jit_ptr_no_code(p_metadata, i_addr_6502);
jit_metadata_set_code_block(p_metadata, i_addr_6502, -1);
i_addr_6502++;
next_code_block_addr_6502 = jit_metadata_get_code_block(p_metadata,
i_addr_6502);
} while (next_code_block_addr_6502 == code_block_addr_6502);
}