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