| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* PPC32 JIT compiler. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __PPC32_JIT_H__ |
| 9 |
#define __PPC32_JIT_H__ |
| 10 |
|
| 11 |
#include "utils.h" |
| 12 |
#include "sbox.h" |
| 13 |
|
| 14 |
/* Size of executable page area (in Mb) */ |
| 15 |
#ifndef __CYGWIN__ |
| 16 |
#define PPC_EXEC_AREA_SIZE 64 |
| 17 |
#else |
| 18 |
#define PPC_EXEC_AREA_SIZE 16 |
| 19 |
#endif |
| 20 |
|
| 21 |
/* Buffer size for JIT code generation */ |
| 22 |
#define PPC_JIT_BUFSIZE 32768 |
| 23 |
|
| 24 |
/* Maximum number of X86 chunks */ |
| 25 |
#define PPC_JIT_MAX_CHUNKS 64 |
| 26 |
|
| 27 |
/* Size of hash for virtual address lookup */ |
| 28 |
#define PPC_JIT_VIRT_HASH_BITS 17 |
| 29 |
#define PPC_JIT_VIRT_HASH_MASK ((1 << PPC_JIT_VIRT_HASH_BITS) - 1) |
| 30 |
#define PPC_JIT_VIRT_HASH_SIZE (1 << PPC_JIT_VIRT_HASH_BITS) |
| 31 |
|
| 32 |
/* Size of hash for physical lookup */ |
| 33 |
#define PPC_JIT_PHYS_HASH_BITS 16 |
| 34 |
#define PPC_JIT_PHYS_HASH_MASK ((1 << PPC_JIT_PHYS_HASH_BITS) - 1) |
| 35 |
#define PPC_JIT_PHYS_HASH_SIZE (1 << PPC_JIT_PHYS_HASH_BITS) |
| 36 |
|
| 37 |
#define PPC_JIT_TARGET_BITMAP_INDEX(x) (((x) >> 7) & 0x1F) |
| 38 |
#define PPC_JIT_TARGET_BITMAP_POS(x) (((x) >> 2) & 0x1F) |
| 39 |
|
| 40 |
/* Instruction jump patch */ |
| 41 |
struct ppc32_insn_patch { |
| 42 |
struct ppc32_insn_patch *next; |
| 43 |
u_char *jit_insn; |
| 44 |
m_uint32_t ppc_ia; |
| 45 |
}; |
| 46 |
|
| 47 |
/* Instruction patch table */ |
| 48 |
#define PPC32_INSN_PATCH_TABLE_SIZE 32 |
| 49 |
|
| 50 |
struct ppc32_jit_patch_table { |
| 51 |
struct ppc32_jit_patch_table *next; |
| 52 |
struct ppc32_insn_patch patches[PPC32_INSN_PATCH_TABLE_SIZE]; |
| 53 |
u_int cur_patch; |
| 54 |
}; |
| 55 |
|
| 56 |
#define PPC32_JIT_TCB_FLAG_SMC 0x1 /* Self-modifying code */ |
| 57 |
#define PPC32_JIT_TCB_FLAG_NO_FLUSH 0x2 /* No flushing */ |
| 58 |
|
| 59 |
/* PPC32 translated code block */ |
| 60 |
struct ppc32_jit_tcb { |
| 61 |
u_int flags; |
| 62 |
m_uint32_t start_ia; |
| 63 |
m_uint32_t exec_state; |
| 64 |
|
| 65 |
u_char **jit_insn_ptr; |
| 66 |
m_uint64_t acc_count; |
| 67 |
ppc_insn_t *ppc_code; |
| 68 |
u_int ppc_trans_pos; |
| 69 |
u_int jit_chunk_pos; |
| 70 |
u_char *jit_ptr; |
| 71 |
insn_exec_page_t *jit_buffer; |
| 72 |
insn_exec_page_t *jit_chunks[PPC_JIT_MAX_CHUNKS]; |
| 73 |
struct ppc32_jit_patch_table *patch_table; |
| 74 |
ppc32_jit_tcb_t *prev,*next; |
| 75 |
|
| 76 |
/* Physical page information */ |
| 77 |
m_uint32_t phys_page; |
| 78 |
m_uint32_t phys_hash; |
| 79 |
ppc32_jit_tcb_t **phys_pprev,*phys_next; |
| 80 |
|
| 81 |
/* 1024 instructions per page, one bit per instruction */ |
| 82 |
m_uint32_t target_bitmap[32]; |
| 83 |
m_uint32_t target_undef_cnt; |
| 84 |
|
| 85 |
#if DEBUG_BLOCK_TIMESTAMP |
| 86 |
m_uint64_t tm_first_use,tm_last_use; |
| 87 |
#endif |
| 88 |
}; |
| 89 |
|
| 90 |
/* PPC instruction recognition */ |
| 91 |
struct ppc32_insn_tag { |
| 92 |
int (*emit)(cpu_ppc_t *cpu,ppc32_jit_tcb_t *,ppc_insn_t); |
| 93 |
m_uint32_t mask,value; |
| 94 |
}; |
| 95 |
|
| 96 |
/* Mark the specified IA as a target for further recompiling */ |
| 97 |
static inline void |
| 98 |
ppc32_jit_tcb_set_target_bit(ppc32_jit_tcb_t *b,m_uint32_t ia) |
| 99 |
{ |
| 100 |
int index,pos; |
| 101 |
|
| 102 |
index = PPC_JIT_TARGET_BITMAP_INDEX(ia); |
| 103 |
pos = PPC_JIT_TARGET_BITMAP_POS(ia); |
| 104 |
|
| 105 |
b->target_bitmap[index] |= 1 << pos; |
| 106 |
} |
| 107 |
|
| 108 |
/* Returns TRUE if the specified IA is in the target bitmap */ |
| 109 |
static inline int |
| 110 |
ppc32_jit_tcb_get_target_bit(ppc32_jit_tcb_t *b,m_uint32_t ia) |
| 111 |
{ |
| 112 |
int index,pos; |
| 113 |
|
| 114 |
index = PPC_JIT_TARGET_BITMAP_INDEX(ia); |
| 115 |
pos = PPC_JIT_TARGET_BITMAP_POS(ia); |
| 116 |
|
| 117 |
return(b->target_bitmap[index] & (1 << pos)); |
| 118 |
} |
| 119 |
|
| 120 |
/* Get the JIT instruction pointer in a translated block */ |
| 121 |
static forced_inline |
| 122 |
u_char *ppc32_jit_tcb_get_host_ptr(ppc32_jit_tcb_t *tcb,m_uint32_t vaddr) |
| 123 |
{ |
| 124 |
m_uint32_t offset; |
| 125 |
|
| 126 |
offset = (vaddr & PPC32_MIN_PAGE_IMASK) >> 2; |
| 127 |
return(tcb->jit_insn_ptr[offset]); |
| 128 |
} |
| 129 |
|
| 130 |
/* Check if the specified address belongs to the specified block */ |
| 131 |
static forced_inline |
| 132 |
int ppc32_jit_tcb_local_addr(ppc32_jit_tcb_t *tcb,m_uint32_t vaddr, |
| 133 |
u_char **jit_addr) |
| 134 |
{ |
| 135 |
if ((vaddr & PPC32_MIN_PAGE_MASK) == tcb->start_ia) { |
| 136 |
*jit_addr = ppc32_jit_tcb_get_host_ptr(tcb,vaddr); |
| 137 |
return(1); |
| 138 |
} |
| 139 |
|
| 140 |
return(0); |
| 141 |
} |
| 142 |
|
| 143 |
/* Check if PC register matches the compiled block virtual address */ |
| 144 |
static forced_inline |
| 145 |
int ppc32_jit_tcb_match(cpu_ppc_t *cpu,ppc32_jit_tcb_t *tcb) |
| 146 |
{ |
| 147 |
m_uint32_t vpage; |
| 148 |
|
| 149 |
vpage = cpu->ia & PPC32_MIN_PAGE_MASK; |
| 150 |
return((tcb->start_ia == vpage) && (tcb->exec_state == cpu->exec_state)); |
| 151 |
} |
| 152 |
|
| 153 |
/* Compute the hash index for the specified virtual address */ |
| 154 |
static forced_inline m_uint32_t ppc32_jit_get_virt_hash(m_uint32_t vaddr) |
| 155 |
{ |
| 156 |
m_uint32_t page_hash; |
| 157 |
|
| 158 |
page_hash = sbox_u32(vaddr >> PPC32_MIN_PAGE_SHIFT); |
| 159 |
return((page_hash ^ (page_hash >> 14)) & PPC_JIT_VIRT_HASH_MASK); |
| 160 |
} |
| 161 |
|
| 162 |
/* Compute the hash index for the specified physical page */ |
| 163 |
static forced_inline m_uint32_t ppc32_jit_get_phys_hash(m_uint32_t phys_page) |
| 164 |
{ |
| 165 |
m_uint32_t page_hash; |
| 166 |
|
| 167 |
page_hash = sbox_u32(phys_page); |
| 168 |
return((page_hash ^ (page_hash >> 12)) & PPC_JIT_PHYS_HASH_MASK); |
| 169 |
} |
| 170 |
|
| 171 |
/* Find a JIT block matching a physical page */ |
| 172 |
static inline ppc32_jit_tcb_t * |
| 173 |
ppc32_jit_find_by_phys_page(cpu_ppc_t *cpu,m_uint32_t phys_page) |
| 174 |
{ |
| 175 |
m_uint32_t page_hash = ppc32_jit_get_phys_hash(phys_page); |
| 176 |
ppc32_jit_tcb_t *tcb; |
| 177 |
|
| 178 |
for(tcb=cpu->tcb_phys_hash[page_hash];tcb;tcb=tcb->phys_next) |
| 179 |
if (tcb->phys_page == phys_page) |
| 180 |
return tcb; |
| 181 |
|
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
/* ======================================================================== */ |
| 186 |
/* JIT emit operations (generic). */ |
| 187 |
/* ======================================================================== */ |
| 188 |
|
| 189 |
/* Indicate registers modified by ppc32_update_cr() functions */ |
| 190 |
extern void ppc32_update_cr_set_altered_hreg(cpu_ppc_t *cpu); |
| 191 |
|
| 192 |
/* Set opcode */ |
| 193 |
static inline void ppc32_op_set(cpu_ppc_t *cpu,jit_op_t *op) |
| 194 |
{ |
| 195 |
cpu_gen_t *c = cpu->gen; |
| 196 |
*c->jit_op_current = op; |
| 197 |
c->jit_op_current = &op->next; |
| 198 |
} |
| 199 |
|
| 200 |
/* EMIT_BASIC_OPCODE */ |
| 201 |
static inline void ppc32_op_emit_basic_opcode(cpu_ppc_t *cpu,u_int opcode) |
| 202 |
{ |
| 203 |
jit_op_t *op = jit_op_get(cpu->gen,0,opcode); |
| 204 |
ppc32_op_set(cpu,op); |
| 205 |
} |
| 206 |
|
| 207 |
/* Trash the specified host register */ |
| 208 |
static inline void ppc32_op_emit_alter_host_reg(cpu_ppc_t *cpu,int host_reg) |
| 209 |
{ |
| 210 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_ALTER_HOST_REG); |
| 211 |
op->param[0] = host_reg; |
| 212 |
ppc32_op_set(cpu,op); |
| 213 |
} |
| 214 |
|
| 215 |
/* EMIT_INSN_OUTPUT */ |
| 216 |
static inline jit_op_t * |
| 217 |
ppc32_op_emit_insn_output(cpu_ppc_t *cpu,u_int size_index,char *insn_name) |
| 218 |
{ |
| 219 |
jit_op_t *op = jit_op_get(cpu->gen,size_index,JIT_OP_INSN_OUTPUT); |
| 220 |
op->arg_ptr = NULL; |
| 221 |
op->insn_name = insn_name; |
| 222 |
ppc32_op_set(cpu,op); |
| 223 |
return op; |
| 224 |
} |
| 225 |
|
| 226 |
/* EMIT_LOAD_GPR */ |
| 227 |
static inline |
| 228 |
void ppc32_op_emit_load_gpr(cpu_ppc_t *cpu,int host_reg,int ppc_reg) |
| 229 |
{ |
| 230 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_LOAD_GPR); |
| 231 |
op->param[0] = host_reg; |
| 232 |
op->param[1] = ppc_reg; |
| 233 |
op->param[2] = host_reg; |
| 234 |
ppc32_op_set(cpu,op); |
| 235 |
} |
| 236 |
|
| 237 |
/* EMIT_STORE_GPR */ |
| 238 |
static inline |
| 239 |
void ppc32_op_emit_store_gpr(cpu_ppc_t *cpu,int ppc_reg,int host_reg) |
| 240 |
{ |
| 241 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_STORE_GPR); |
| 242 |
op->param[0] = host_reg; |
| 243 |
op->param[1] = ppc_reg; |
| 244 |
op->param[2] = host_reg; |
| 245 |
ppc32_op_set(cpu,op); |
| 246 |
} |
| 247 |
|
| 248 |
/* EMIT_UPDATE_FLAGS */ |
| 249 |
static inline |
| 250 |
void ppc32_op_emit_update_flags(cpu_ppc_t *cpu,int field,int is_signed) |
| 251 |
{ |
| 252 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_UPDATE_FLAGS); |
| 253 |
|
| 254 |
op->param[0] = field; |
| 255 |
op->param[1] = is_signed; |
| 256 |
|
| 257 |
ppc32_op_set(cpu,op); |
| 258 |
ppc32_update_cr_set_altered_hreg(cpu); |
| 259 |
} |
| 260 |
|
| 261 |
/* EMIT_REQUIRE_FLAGS */ |
| 262 |
static inline void ppc32_op_emit_require_flags(cpu_ppc_t *cpu,int field) |
| 263 |
{ |
| 264 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_REQUIRE_FLAGS); |
| 265 |
op->param[0] = field; |
| 266 |
ppc32_op_set(cpu,op); |
| 267 |
} |
| 268 |
|
| 269 |
/* EMIT_BRANCH_TARGET */ |
| 270 |
static inline void ppc32_op_emit_branch_target(cpu_ppc_t *cpu, |
| 271 |
ppc32_jit_tcb_t *b, |
| 272 |
m_uint32_t ia) |
| 273 |
{ |
| 274 |
if ((ia & PPC32_MIN_PAGE_MASK) == b->start_ia) { |
| 275 |
cpu_gen_t *c = cpu->gen; |
| 276 |
jit_op_t *op = jit_op_get(c,0,JIT_OP_BRANCH_TARGET); |
| 277 |
u_int pos = (ia & PPC32_MIN_PAGE_IMASK) >> 2; |
| 278 |
|
| 279 |
/* Insert in head */ |
| 280 |
op->next = c->jit_op_array[pos]; |
| 281 |
c->jit_op_array[pos] = op; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/* EMIT_SET_HOST_REG_IMM32 */ |
| 286 |
static inline void |
| 287 |
ppc32_op_emit_set_host_reg_imm32(cpu_ppc_t *cpu,int reg,m_uint32_t val) |
| 288 |
{ |
| 289 |
jit_op_t *op = jit_op_get(cpu->gen,0,JIT_OP_SET_HOST_REG_IMM32); |
| 290 |
op->param[0] = reg; |
| 291 |
op->param[1] = val; |
| 292 |
ppc32_op_set(cpu,op); |
| 293 |
} |
| 294 |
|
| 295 |
/* ======================================================================== */ |
| 296 |
/* JIT operations with implementations specific to target CPU */ |
| 297 |
void ppc32_op_insn_output(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 298 |
void ppc32_op_load_gpr(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 299 |
void ppc32_op_store_gpr(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 300 |
void ppc32_op_update_flags(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 301 |
void ppc32_op_move_host_reg(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 302 |
void ppc32_op_set_host_reg_imm32(ppc32_jit_tcb_t *b,jit_op_t *op); |
| 303 |
|
| 304 |
/* Set the Instruction Address (IA) register */ |
| 305 |
void ppc32_set_ia(u_char **ptr,m_uint32_t new_ia); |
| 306 |
|
| 307 |
/* Jump to the next page */ |
| 308 |
void ppc32_set_page_jump(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b); |
| 309 |
|
| 310 |
/* Increment the number of executed instructions (performance debugging) */ |
| 311 |
void ppc32_inc_perf_counter(cpu_ppc_t *cpu); |
| 312 |
|
| 313 |
/* ======================================================================== */ |
| 314 |
|
| 315 |
/* Virtual Breakpoint */ |
| 316 |
void ppc32_emit_breakpoint(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b); |
| 317 |
|
| 318 |
/* Initialize instruction lookup table */ |
| 319 |
void ppc32_jit_create_ilt(void); |
| 320 |
|
| 321 |
/* Initialize the JIT structure */ |
| 322 |
int ppc32_jit_init(cpu_ppc_t *cpu); |
| 323 |
|
| 324 |
/* Flush the JIT */ |
| 325 |
u_int ppc32_jit_flush(cpu_ppc_t *cpu,u_int threshold); |
| 326 |
|
| 327 |
/* Shutdown the JIT */ |
| 328 |
void ppc32_jit_shutdown(cpu_ppc_t *cpu); |
| 329 |
|
| 330 |
/* Fetch a PowerPC instruction and emit corresponding translated code */ |
| 331 |
struct ppc32_insn_tag *ppc32_jit_fetch_and_emit(cpu_ppc_t *cpu, |
| 332 |
ppc32_jit_tcb_t *block); |
| 333 |
|
| 334 |
/* Record a patch to apply in a compiled block */ |
| 335 |
int ppc32_jit_tcb_record_patch(ppc32_jit_tcb_t *block,jit_op_t *iop, |
| 336 |
u_char *jit_ptr,m_uint32_t vaddr); |
| 337 |
|
| 338 |
/* Mark a block as containing self-modifying code */ |
| 339 |
void ppc32_jit_mark_smc(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block); |
| 340 |
|
| 341 |
/* Free an instruction block */ |
| 342 |
void ppc32_jit_tcb_free(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block, |
| 343 |
int list_removal); |
| 344 |
|
| 345 |
/* Check if the specified address belongs to the specified block */ |
| 346 |
int ppc32_jit_tcb_local_addr(ppc32_jit_tcb_t *block,m_uint32_t vaddr, |
| 347 |
u_char **jit_addr); |
| 348 |
|
| 349 |
/* Recompile a page */ |
| 350 |
int ppc32_jit_tcb_recompile(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block); |
| 351 |
|
| 352 |
/* Execute compiled PowerPC code */ |
| 353 |
void *ppc32_jit_run_cpu(cpu_gen_t *gen); |
| 354 |
|
| 355 |
/* Start register allocation sequence */ |
| 356 |
void ppc32_jit_start_hreg_seq(cpu_ppc_t *cpu,char *insn); |
| 357 |
|
| 358 |
/* Close register allocation sequence */ |
| 359 |
void ppc32_jit_close_hreg_seq(cpu_ppc_t *cpu); |
| 360 |
|
| 361 |
/* Insert a reg map as head of list (as MRU element) */ |
| 362 |
void ppc32_jit_insert_hreg_mru(cpu_ppc_t *cpu,struct hreg_map *map); |
| 363 |
|
| 364 |
/* Allocate an host register */ |
| 365 |
int ppc32_jit_alloc_hreg(cpu_ppc_t *cpu,int ppc_reg); |
| 366 |
|
| 367 |
/* Force allocation of an host register */ |
| 368 |
int ppc32_jit_alloc_hreg_forced(cpu_ppc_t *cpu,int hreg); |
| 369 |
|
| 370 |
/* Initialize register mapping */ |
| 371 |
void ppc32_jit_init_hreg_mapping(cpu_ppc_t *cpu); |
| 372 |
|
| 373 |
#endif |