Commit | Line | Data |
---|---|---|
98fa08a5 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d16005f8 | 2 | /* |
98fa08a5 | 3 | * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net> |
d16005f8 PC |
4 | */ |
5 | ||
6 | #ifndef __LIGHTREC_PRIVATE_H__ | |
7 | #define __LIGHTREC_PRIVATE_H__ | |
8 | ||
02487de7 | 9 | #include "lightning-wrapper.h" |
98fa08a5 | 10 | #include "lightrec-config.h" |
d16005f8 PC |
11 | #include "disassembler.h" |
12 | #include "lightrec.h" | |
ba3814c1 | 13 | #include "regcache.h" |
d16005f8 PC |
14 | |
15 | #if ENABLE_THREADED_COMPILER | |
16 | #include <stdatomic.h> | |
17 | #endif | |
18 | ||
13b02197 PC |
19 | #ifdef _MSC_BUILD |
20 | #include <immintrin.h> | |
21 | #endif | |
22 | ||
f5ee77ca PC |
23 | #include <inttypes.h> |
24 | #include <stdint.h> | |
25 | ||
8afce295 PC |
26 | #define X32_FMT "0x%08"PRIx32 |
27 | #define PC_FMT "PC "X32_FMT | |
f5ee77ca | 28 | |
d16005f8 | 29 | #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0) |
d16005f8 | 30 | |
13b02197 PC |
31 | #define GENMASK(h, l) \ |
32 | (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h)))) | |
33 | ||
d16005f8 PC |
34 | #ifdef __GNUC__ |
35 | # define likely(x) __builtin_expect(!!(x),1) | |
36 | # define unlikely(x) __builtin_expect(!!(x),0) | |
37 | #else | |
38 | # define likely(x) (x) | |
39 | # define unlikely(x) (x) | |
40 | #endif | |
41 | ||
42 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
43 | # define LE32TOH(x) __builtin_bswap32(x) | |
44 | # define HTOLE32(x) __builtin_bswap32(x) | |
45 | # define LE16TOH(x) __builtin_bswap16(x) | |
46 | # define HTOLE16(x) __builtin_bswap16(x) | |
47 | #else | |
48 | # define LE32TOH(x) (x) | |
49 | # define HTOLE32(x) (x) | |
50 | # define LE16TOH(x) (x) | |
51 | # define HTOLE16(x) (x) | |
52 | #endif | |
53 | ||
98fa08a5 PC |
54 | #if HAS_DEFAULT_ELM |
55 | #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value | |
56 | #else | |
57 | #define SET_DEFAULT_ELM(table, value) [0] = NULL | |
58 | #endif | |
59 | ||
684432ad PC |
60 | #if __has_attribute(__fallthrough__) |
61 | # define fallthrough __attribute__((__fallthrough__)) | |
62 | #else | |
63 | # define fallthrough do {} while (0) /* fallthrough */ | |
64 | #endif | |
d8b04acd | 65 | |
ba3814c1 PC |
66 | #define container_of(ptr, type, member) \ |
67 | ((type *)((void *)(ptr) - offsetof(type, member))) | |
68 | ||
69 | #ifdef _MSC_BUILD | |
70 | # define popcount32(x) __popcnt(x) | |
13b02197 PC |
71 | # define clz32(x) _lzcnt_u32(x) |
72 | # define ctz32(x) _tzcnt_u32(x) | |
ba3814c1 PC |
73 | #else |
74 | # define popcount32(x) __builtin_popcount(x) | |
13b02197 PC |
75 | # define clz32(x) __builtin_clz(x) |
76 | # define ctz32(x) __builtin_ctz(x) | |
ba3814c1 PC |
77 | #endif |
78 | ||
d16005f8 PC |
79 | /* Flags for (struct block *)->flags */ |
80 | #define BLOCK_NEVER_COMPILE BIT(0) | |
81 | #define BLOCK_SHOULD_RECOMPILE BIT(1) | |
82 | #define BLOCK_FULLY_TAGGED BIT(2) | |
a59e5536 | 83 | #define BLOCK_IS_DEAD BIT(3) |
98fa08a5 | 84 | #define BLOCK_IS_MEMSET BIT(4) |
ba3814c1 | 85 | #define BLOCK_NO_OPCODE_LIST BIT(5) |
684432ad | 86 | #define BLOCK_PRELOAD_PC BIT(6) |
d16005f8 PC |
87 | |
88 | #define RAM_SIZE 0x200000 | |
89 | #define BIOS_SIZE 0x80000 | |
90 | ||
91 | #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2) | |
92 | ||
98fa08a5 PC |
93 | #define REG_LO 32 |
94 | #define REG_HI 33 | |
cb72ea13 | 95 | #define REG_TEMP (offsetof(struct lightrec_state, temp_reg) / sizeof(u32)) |
98fa08a5 | 96 | |
d16005f8 PC |
97 | /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */ |
98 | struct jit_node; | |
99 | struct jit_state; | |
100 | typedef struct jit_state jit_state_t; | |
101 | ||
102 | struct blockcache; | |
103 | struct recompiler; | |
104 | struct regcache; | |
105 | struct opcode; | |
a59e5536 | 106 | struct reaper; |
d16005f8 | 107 | |
11357fef PC |
108 | struct u16x2 { |
109 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
110 | u16 h, l; | |
111 | #else | |
112 | u16 l, h; | |
113 | #endif | |
114 | }; | |
115 | ||
d16005f8 PC |
116 | struct block { |
117 | jit_state_t *_jit; | |
d16005f8 PC |
118 | struct opcode *opcode_list; |
119 | void (*function)(void); | |
98fa08a5 PC |
120 | const u32 *code; |
121 | struct block *next; | |
d16005f8 PC |
122 | u32 pc; |
123 | u32 hash; | |
d8b04acd | 124 | u32 precompile_date; |
98fa08a5 PC |
125 | unsigned int code_size; |
126 | u16 nb_ops; | |
d16005f8 | 127 | #if ENABLE_THREADED_COMPILER |
ba3814c1 PC |
128 | _Atomic u8 flags; |
129 | #else | |
130 | u8 flags; | |
d16005f8 | 131 | #endif |
d16005f8 PC |
132 | }; |
133 | ||
134 | struct lightrec_branch { | |
135 | struct jit_node *branch; | |
136 | u32 target; | |
137 | }; | |
138 | ||
139 | struct lightrec_branch_target { | |
140 | struct jit_node *label; | |
141 | u32 offset; | |
142 | }; | |
143 | ||
98fa08a5 PC |
144 | enum c_wrappers { |
145 | C_WRAPPER_RW, | |
146 | C_WRAPPER_RW_GENERIC, | |
fdf33147 | 147 | C_WRAPPER_MFC, |
98fa08a5 PC |
148 | C_WRAPPER_MTC, |
149 | C_WRAPPER_CP, | |
98fa08a5 PC |
150 | C_WRAPPERS_COUNT, |
151 | }; | |
152 | ||
153 | struct lightrec_cstate { | |
154 | struct lightrec_state *state; | |
155 | ||
d16005f8 PC |
156 | struct lightrec_branch local_branches[512]; |
157 | struct lightrec_branch_target targets[512]; | |
684432ad | 158 | u16 movi_temp[32]; |
d16005f8 PC |
159 | unsigned int nb_local_branches; |
160 | unsigned int nb_targets; | |
98fa08a5 PC |
161 | unsigned int cycles; |
162 | ||
163 | struct regcache *reg_cache; | |
cb72ea13 PC |
164 | |
165 | _Bool no_load_delay; | |
98fa08a5 PC |
166 | }; |
167 | ||
168 | struct lightrec_state { | |
169 | struct lightrec_registers regs; | |
cb72ea13 | 170 | u32 temp_reg; |
0e720fb1 | 171 | u32 curr_pc; |
98fa08a5 | 172 | u32 next_pc; |
9259d748 | 173 | uintptr_t wrapper_regs[NUM_TEMPS]; |
cb72ea13 | 174 | u8 in_delay_slot_n; |
98fa08a5 PC |
175 | u32 current_cycle; |
176 | u32 target_cycle; | |
177 | u32 exit_flags; | |
178 | u32 old_cycle_counter; | |
684432ad | 179 | u32 cycles_per_op; |
8afce295 | 180 | void *c_wrapper; |
98fa08a5 | 181 | struct block *dispatcher, *c_wrapper_block; |
fd58fa32 | 182 | void *c_wrappers[C_WRAPPERS_COUNT]; |
d16005f8 | 183 | struct blockcache *block_cache; |
d16005f8 | 184 | struct recompiler *rec; |
98fa08a5 | 185 | struct lightrec_cstate *cstate; |
a59e5536 | 186 | struct reaper *reaper; |
02487de7 | 187 | void *tlsf; |
d16005f8 | 188 | void (*eob_wrapper_func)(void); |
cb72ea13 PC |
189 | void (*interpreter_func)(void); |
190 | void (*ds_check_func)(void); | |
98fa08a5 | 191 | void (*memset_func)(void); |
d16005f8 PC |
192 | void (*get_next_block)(void); |
193 | struct lightrec_ops ops; | |
a59e5536 | 194 | unsigned int nb_precompile; |
cb72ea13 | 195 | unsigned int nb_compile; |
d16005f8 PC |
196 | unsigned int nb_maps; |
197 | const struct lightrec_mem_map *maps; | |
ba3814c1 | 198 | uintptr_t offset_ram, offset_bios, offset_scratch, offset_io; |
684432ad | 199 | u32 opt_flags; |
02487de7 | 200 | _Bool with_32bit_lut; |
d16005f8 | 201 | _Bool mirrors_mapped; |
d16005f8 PC |
202 | void *code_lut[]; |
203 | }; | |
204 | ||
8afce295 PC |
205 | #define lightrec_offset(ptr) \ |
206 | offsetof(struct lightrec_state, ptr) | |
207 | ||
cb72ea13 PC |
208 | u32 lightrec_rw(struct lightrec_state *state, union code op, u32 addr, |
209 | u32 data, u32 *flags, struct block *block, u16 offset); | |
d16005f8 | 210 | |
98fa08a5 | 211 | void lightrec_free_block(struct lightrec_state *state, struct block *block); |
d16005f8 PC |
212 | |
213 | void remove_from_code_lut(struct blockcache *cache, struct block *block); | |
214 | ||
98fa08a5 PC |
215 | const struct lightrec_mem_map * |
216 | lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr); | |
217 | ||
d16005f8 PC |
218 | static inline u32 kunseg(u32 addr) |
219 | { | |
220 | if (unlikely(addr >= 0xa0000000)) | |
221 | return addr - 0xa0000000; | |
222 | else | |
223 | return addr &~ 0x80000000; | |
224 | } | |
225 | ||
226 | static inline u32 lut_offset(u32 pc) | |
227 | { | |
228 | if (pc & BIT(28)) | |
229 | return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS | |
230 | else | |
231 | return (pc & (RAM_SIZE - 1)) >> 2; // RAM | |
232 | } | |
233 | ||
02487de7 PC |
234 | static inline _Bool is_big_endian(void) |
235 | { | |
236 | return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; | |
237 | } | |
238 | ||
239 | static inline _Bool lut_is_32bit(const struct lightrec_state *state) | |
240 | { | |
241 | return __WORDSIZE == 32 || | |
242 | (ENABLE_CODE_BUFFER && state->with_32bit_lut); | |
243 | } | |
244 | ||
245 | static inline size_t lut_elm_size(const struct lightrec_state *state) | |
246 | { | |
247 | return lut_is_32bit(state) ? 4 : sizeof(void *); | |
248 | } | |
249 | ||
250 | static inline void ** lut_address(struct lightrec_state *state, u32 offset) | |
251 | { | |
252 | if (lut_is_32bit(state)) | |
253 | return (void **) ((uintptr_t) state->code_lut + offset * 4); | |
254 | else | |
255 | return &state->code_lut[offset]; | |
256 | } | |
257 | ||
258 | static inline void * lut_read(struct lightrec_state *state, u32 offset) | |
259 | { | |
d8b04acd | 260 | void **lut_entry = lut_address(state, offset); |
02487de7 PC |
261 | |
262 | if (lut_is_32bit(state)) | |
263 | return (void *)(uintptr_t) *(u32 *) lut_entry; | |
264 | else | |
265 | return *lut_entry; | |
266 | } | |
267 | ||
268 | static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr) | |
269 | { | |
270 | void **lut_entry = lut_address(state, offset); | |
271 | ||
272 | if (lut_is_32bit(state)) | |
273 | *(u32 *) lut_entry = (u32)(uintptr_t) ptr; | |
274 | else | |
275 | *lut_entry = ptr; | |
276 | } | |
277 | ||
98fa08a5 PC |
278 | static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm) |
279 | { | |
280 | u16 flags = block->opcode_list[offset].flags; | |
281 | ||
03535202 | 282 | offset += op_flag_no_ds(flags); |
98fa08a5 | 283 | |
684432ad | 284 | return block->pc + ((offset + imm) << 2); |
98fa08a5 PC |
285 | } |
286 | ||
287 | static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm) | |
288 | { | |
289 | u16 flags = block->opcode_list[offset].flags; | |
290 | ||
03535202 | 291 | offset -= op_flag_no_ds(flags); |
98fa08a5 | 292 | |
684432ad | 293 | return block->pc + ((offset + imm) << 2); |
98fa08a5 PC |
294 | } |
295 | ||
9259d748 | 296 | void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data); |
d16005f8 | 297 | u32 lightrec_mfc(struct lightrec_state *state, union code op); |
98fa08a5 PC |
298 | void lightrec_rfe(struct lightrec_state *state); |
299 | void lightrec_cp(struct lightrec_state *state, union code op); | |
300 | ||
301 | struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state); | |
302 | void lightrec_free_cstate(struct lightrec_cstate *cstate); | |
d16005f8 PC |
303 | |
304 | union code lightrec_read_opcode(struct lightrec_state *state, u32 pc); | |
305 | ||
98fa08a5 | 306 | int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block); |
ba3814c1 PC |
307 | void lightrec_free_opcode_list(struct lightrec_state *state, |
308 | struct opcode *list); | |
98fa08a5 | 309 | |
684432ad PC |
310 | unsigned int lightrec_cycles_of_opcode(const struct lightrec_state *state, |
311 | union code code); | |
98fa08a5 PC |
312 | |
313 | static inline u8 get_mult_div_lo(union code c) | |
314 | { | |
315 | return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO; | |
316 | } | |
317 | ||
318 | static inline u8 get_mult_div_hi(union code c) | |
319 | { | |
320 | return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI; | |
321 | } | |
d16005f8 | 322 | |
03535202 PC |
323 | static inline s16 s16_max(s16 a, s16 b) |
324 | { | |
325 | return a > b ? a : b; | |
326 | } | |
327 | ||
ba3814c1 PC |
328 | static inline _Bool block_has_flag(struct block *block, u8 flag) |
329 | { | |
330 | #if ENABLE_THREADED_COMPILER | |
331 | return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag; | |
332 | #else | |
333 | return block->flags & flag; | |
334 | #endif | |
335 | } | |
336 | ||
337 | static inline u8 block_set_flags(struct block *block, u8 mask) | |
338 | { | |
339 | #if ENABLE_THREADED_COMPILER | |
340 | return atomic_fetch_or_explicit(&block->flags, mask, | |
341 | memory_order_relaxed); | |
342 | #else | |
343 | u8 flags = block->flags; | |
344 | ||
345 | block->flags |= mask; | |
346 | ||
347 | return flags; | |
348 | #endif | |
349 | } | |
350 | ||
351 | static inline u8 block_clear_flags(struct block *block, u8 mask) | |
352 | { | |
353 | #if ENABLE_THREADED_COMPILER | |
354 | return atomic_fetch_and_explicit(&block->flags, ~mask, | |
355 | memory_order_relaxed); | |
356 | #else | |
357 | u8 flags = block->flags; | |
358 | ||
359 | block->flags &= ~mask; | |
360 | ||
361 | return flags; | |
362 | #endif | |
363 | } | |
364 | ||
9259d748 PC |
365 | static inline _Bool can_sign_extend(s32 value, u8 order) |
366 | { | |
684432ad | 367 | return ((u32)(value >> (order - 1)) + 1) < 2; |
9259d748 PC |
368 | } |
369 | ||
370 | static inline _Bool can_zero_extend(u32 value, u8 order) | |
371 | { | |
372 | return (value >> order) == 0; | |
373 | } | |
374 | ||
cb72ea13 PC |
375 | static inline const struct opcode * |
376 | get_delay_slot(const struct opcode *list, u16 i) | |
377 | { | |
378 | return op_flag_no_ds(list[i].flags) ? &list[i - 1] : &list[i + 1]; | |
379 | } | |
380 | ||
0e720fb1 PC |
381 | static inline _Bool lightrec_store_next_pc(void) |
382 | { | |
383 | return NUM_REGS + NUM_TEMPS <= 4; | |
384 | } | |
385 | ||
d16005f8 | 386 | #endif /* __LIGHTREC_PRIVATE_H__ */ |