1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net>
6 #ifndef __LIGHTREC_PRIVATE_H__
7 #define __LIGHTREC_PRIVATE_H__
9 #include "lightning-wrapper.h"
10 #include "lightrec-config.h"
11 #include "disassembler.h"
15 #if ENABLE_THREADED_COMPILER
16 #include <stdatomic.h>
19 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
22 # define likely(x) __builtin_expect(!!(x),1)
23 # define unlikely(x) __builtin_expect(!!(x),0)
25 # define likely(x) (x)
26 # define unlikely(x) (x)
29 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
30 # define LE32TOH(x) __builtin_bswap32(x)
31 # define HTOLE32(x) __builtin_bswap32(x)
32 # define LE16TOH(x) __builtin_bswap16(x)
33 # define HTOLE16(x) __builtin_bswap16(x)
35 # define LE32TOH(x) (x)
36 # define HTOLE32(x) (x)
37 # define LE16TOH(x) (x)
38 # define HTOLE16(x) (x)
42 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
44 #define SET_DEFAULT_ELM(table, value) [0] = NULL
47 #define fallthrough do {} while (0) /* fall-through */
49 #define container_of(ptr, type, member) \
50 ((type *)((void *)(ptr) - offsetof(type, member)))
53 # define popcount32(x) __popcnt(x)
54 # define ffs32(x) (31 - __lzcnt(x))
56 # define popcount32(x) __builtin_popcount(x)
57 # define ffs32(x) (__builtin_ffs(x) - 1)
60 /* Flags for (struct block *)->flags */
61 #define BLOCK_NEVER_COMPILE BIT(0)
62 #define BLOCK_SHOULD_RECOMPILE BIT(1)
63 #define BLOCK_FULLY_TAGGED BIT(2)
64 #define BLOCK_IS_DEAD BIT(3)
65 #define BLOCK_IS_MEMSET BIT(4)
66 #define BLOCK_NO_OPCODE_LIST BIT(5)
68 #define RAM_SIZE 0x200000
69 #define BIOS_SIZE 0x80000
71 #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
76 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
79 typedef struct jit_state jit_state_t;
88 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
97 struct opcode *opcode_list;
98 void (*function)(void);
104 unsigned int code_size;
106 #if ENABLE_THREADED_COMPILER
113 struct lightrec_branch {
114 struct jit_node *branch;
118 struct lightrec_branch_target {
119 struct jit_node *label;
125 C_WRAPPER_RW_GENERIC,
132 struct lightrec_cstate {
133 struct lightrec_state *state;
135 struct lightrec_branch local_branches[512];
136 struct lightrec_branch_target targets[512];
137 unsigned int nb_local_branches;
138 unsigned int nb_targets;
141 struct regcache *reg_cache;
144 struct lightrec_state {
145 struct lightrec_registers regs;
146 uintptr_t wrapper_regs[NUM_TEMPS];
151 u32 old_cycle_counter;
152 struct block *dispatcher, *c_wrapper_block;
153 void *c_wrappers[C_WRAPPERS_COUNT];
154 void *wrappers_eps[C_WRAPPERS_COUNT];
155 struct blockcache *block_cache;
156 struct recompiler *rec;
157 struct lightrec_cstate *cstate;
158 struct reaper *reaper;
160 void (*eob_wrapper_func)(void);
161 void (*memset_func)(void);
162 void (*get_next_block)(void);
163 struct lightrec_ops ops;
164 unsigned int nb_precompile;
165 unsigned int nb_maps;
166 const struct lightrec_mem_map *maps;
167 uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
168 _Bool with_32bit_lut;
169 _Bool mirrors_mapped;
170 _Bool invalidate_from_dma_only;
174 u32 lightrec_rw(struct lightrec_state *state, union code op,
175 u32 addr, u32 data, u32 *flags,
176 struct block *block);
178 void lightrec_free_block(struct lightrec_state *state, struct block *block);
180 void remove_from_code_lut(struct blockcache *cache, struct block *block);
183 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
185 const struct lightrec_mem_map *
186 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
188 static inline u32 kunseg(u32 addr)
190 if (unlikely(addr >= 0xa0000000))
191 return addr - 0xa0000000;
193 return addr &~ 0x80000000;
196 static inline u32 lut_offset(u32 pc)
199 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
201 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
204 static inline _Bool is_big_endian(void)
206 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
209 static inline _Bool lut_is_32bit(const struct lightrec_state *state)
211 return __WORDSIZE == 32 ||
212 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
215 static inline size_t lut_elm_size(const struct lightrec_state *state)
217 return lut_is_32bit(state) ? 4 : sizeof(void *);
220 static inline void ** lut_address(struct lightrec_state *state, u32 offset)
222 if (lut_is_32bit(state))
223 return (void **) ((uintptr_t) state->code_lut + offset * 4);
225 return &state->code_lut[offset];
228 static inline void * lut_read(struct lightrec_state *state, u32 offset)
230 void **lut_entry = lut_address(state, offset);
232 if (lut_is_32bit(state))
233 return (void *)(uintptr_t) *(u32 *) lut_entry;
238 static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
240 void **lut_entry = lut_address(state, offset);
242 if (lut_is_32bit(state))
243 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
248 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
250 u16 flags = block->opcode_list[offset].flags;
252 offset += op_flag_no_ds(flags);
254 return block->pc + (offset + imm << 2);
257 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
259 u16 flags = block->opcode_list[offset].flags;
261 offset -= op_flag_no_ds(flags);
263 return block->pc + (offset + imm << 2);
266 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
267 u32 lightrec_mfc(struct lightrec_state *state, union code op);
268 void lightrec_rfe(struct lightrec_state *state);
269 void lightrec_cp(struct lightrec_state *state, union code op);
271 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
272 void lightrec_free_cstate(struct lightrec_cstate *cstate);
274 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
276 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
277 void lightrec_free_opcode_list(struct lightrec_state *state,
278 struct opcode *list);
280 unsigned int lightrec_cycles_of_opcode(union code code);
282 static inline u8 get_mult_div_lo(union code c)
284 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
287 static inline u8 get_mult_div_hi(union code c)
289 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
292 static inline s16 s16_max(s16 a, s16 b)
294 return a > b ? a : b;
297 static inline _Bool block_has_flag(struct block *block, u8 flag)
299 #if ENABLE_THREADED_COMPILER
300 return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
302 return block->flags & flag;
306 static inline u8 block_set_flags(struct block *block, u8 mask)
308 #if ENABLE_THREADED_COMPILER
309 return atomic_fetch_or_explicit(&block->flags, mask,
310 memory_order_relaxed);
312 u8 flags = block->flags;
314 block->flags |= mask;
320 static inline u8 block_clear_flags(struct block *block, u8 mask)
322 #if ENABLE_THREADED_COMPILER
323 return atomic_fetch_and_explicit(&block->flags, ~mask,
324 memory_order_relaxed);
326 u8 flags = block->flags;
328 block->flags &= ~mask;
334 #endif /* __LIGHTREC_PRIVATE_H__ */