| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* MIPS64 JIT compiler. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __MIPS64_JIT_H__ |
| 9 |
#define __MIPS64_JIT_H__ |
| 10 |
|
| 11 |
#include "utils.h" |
| 12 |
#include "sbox.h" |
| 13 |
#include "tcb.h" |
| 14 |
|
| 15 |
/* Size of hash for virtual address lookup */ |
| 16 |
#define MIPS_JIT_VIRT_HASH_BITS 16 |
| 17 |
#define MIPS_JIT_VIRT_HASH_MASK ((1 << MIPS_JIT_VIRT_HASH_BITS) - 1) |
| 18 |
#define MIPS_JIT_VIRT_HASH_SIZE (1 << MIPS_JIT_VIRT_HASH_BITS) |
| 19 |
|
| 20 |
/* Size of hash for physical lookup */ |
| 21 |
#define MIPS_JIT_PHYS_HASH_BITS 16 |
| 22 |
#define MIPS_JIT_PHYS_HASH_MASK ((1 << MIPS_JIT_PHYS_HASH_BITS) - 1) |
| 23 |
#define MIPS_JIT_PHYS_HASH_SIZE (1 << MIPS_JIT_PHYS_HASH_BITS) |
| 24 |
|
| 25 |
/* MIPS instruction recognition */ |
| 26 |
struct mips64_insn_tag { |
| 27 |
int (*emit)(cpu_mips_t *cpu,cpu_tc_t *,mips_insn_t); |
| 28 |
m_uint32_t mask,value; |
| 29 |
int delay_slot; |
| 30 |
}; |
| 31 |
|
| 32 |
/* MIPS jump instruction (for block scan) */ |
| 33 |
struct mips64_insn_jump { |
| 34 |
char *name; |
| 35 |
m_uint32_t mask,value; |
| 36 |
int offset_bits; |
| 37 |
int relative; |
| 38 |
}; |
| 39 |
|
| 40 |
/* Get the JIT instruction pointer in a translated block */ |
| 41 |
static forced_inline |
| 42 |
u_char *mips64_jit_tc_get_host_ptr(cpu_tc_t *tc,m_uint64_t vaddr) |
| 43 |
{ |
| 44 |
m_uint32_t offset; |
| 45 |
|
| 46 |
offset = ((m_uint32_t)vaddr & MIPS_MIN_PAGE_IMASK) >> 2; |
| 47 |
return(tc->jit_insn_ptr[offset]); |
| 48 |
} |
| 49 |
|
| 50 |
/* Check if the specified address belongs to the specified block */ |
| 51 |
static forced_inline |
| 52 |
int mips64_jit_tcb_local_addr(cpu_tc_t *tc,m_uint64_t vaddr, |
| 53 |
u_char **jit_addr) |
| 54 |
{ |
| 55 |
if ((vaddr & MIPS_MIN_PAGE_MASK) == tc->vaddr) { |
| 56 |
*jit_addr = mips64_jit_tc_get_host_ptr(tc,vaddr); |
| 57 |
return(1); |
| 58 |
} |
| 59 |
|
| 60 |
return(0); |
| 61 |
} |
| 62 |
|
| 63 |
/* Check if PC register matches the compiled block virtual address */ |
| 64 |
static forced_inline int mips64_jit_tcb_match(cpu_mips_t *cpu,cpu_tb_t *tb) |
| 65 |
{ |
| 66 |
m_uint64_t vpage; |
| 67 |
|
| 68 |
vpage = cpu->pc & MIPS_MIN_PAGE_MASK; |
| 69 |
return((tb->vaddr == vpage) && (tb->exec_state == cpu->exec_state)); |
| 70 |
} |
| 71 |
|
| 72 |
/* Compute the hash index for the specified virtual address */ |
| 73 |
static forced_inline m_uint32_t mips64_jit_get_virt_hash(m_uint64_t vaddr) |
| 74 |
{ |
| 75 |
m_uint32_t page_hash; |
| 76 |
|
| 77 |
page_hash = sbox_u32(vaddr >> MIPS_MIN_PAGE_SHIFT); |
| 78 |
return((page_hash ^ (page_hash >> 12)) & MIPS_JIT_VIRT_HASH_MASK); |
| 79 |
} |
| 80 |
|
| 81 |
/* Compute the hash index for the specified physical page */ |
| 82 |
static forced_inline m_uint32_t mips64_jit_get_phys_hash(m_uint32_t phys_page) |
| 83 |
{ |
| 84 |
m_uint32_t page_hash; |
| 85 |
|
| 86 |
page_hash = sbox_u32(phys_page); |
| 87 |
return((page_hash ^ (page_hash >> 12)) & MIPS_JIT_PHYS_HASH_MASK); |
| 88 |
} |
| 89 |
|
| 90 |
/* Find a JIT block matching a physical page */ |
| 91 |
static inline cpu_tb_t * |
| 92 |
mips64_jit_find_by_phys_page(cpu_mips_t *cpu,m_uint32_t phys_page) |
| 93 |
{ |
| 94 |
m_uint32_t page_hash = mips64_jit_get_phys_hash(phys_page); |
| 95 |
cpu_tb_t *tb; |
| 96 |
|
| 97 |
for(tb=cpu->gen->tb_phys_hash[page_hash];tb;tb=tb->phys_next) |
| 98 |
if (tb->phys_page == phys_page) |
| 99 |
return tb; |
| 100 |
|
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
/* Check if there are pending IRQ */ |
| 105 |
extern void mips64_check_pending_irq(cpu_tc_t *tc); |
| 106 |
|
| 107 |
/* Initialize instruction lookup table */ |
| 108 |
void mips64_jit_create_ilt(void); |
| 109 |
|
| 110 |
/* Initialize the JIT structure */ |
| 111 |
int mips64_jit_init(cpu_mips_t *cpu); |
| 112 |
|
| 113 |
/* Flush the JIT */ |
| 114 |
u_int mips64_jit_flush(cpu_mips_t *cpu,u_int threshold); |
| 115 |
|
| 116 |
/* Shutdown the JIT */ |
| 117 |
void mips64_jit_shutdown(cpu_mips_t *cpu); |
| 118 |
|
| 119 |
/* Check if an instruction is in a delay slot or not */ |
| 120 |
int mips64_jit_is_delay_slot(cpu_tc_t *tc,m_uint64_t pc); |
| 121 |
|
| 122 |
/* Fetch a MIPS instruction and emit corresponding translated code */ |
| 123 |
struct mips64_insn_tag *mips64_jit_fetch_and_emit(cpu_mips_t *cpu, |
| 124 |
cpu_tc_t *tc, |
| 125 |
int delay_slot); |
| 126 |
|
| 127 |
/* Record a patch to apply in a compiled block */ |
| 128 |
int mips64_jit_tcb_record_patch(cpu_mips_t *cpu,cpu_tc_t *tc, |
| 129 |
u_char *jit_ptr,m_uint64_t vaddr); |
| 130 |
|
| 131 |
/* Mark a block as containing self-modifying code */ |
| 132 |
void mips64_jit_mark_smc(cpu_mips_t *cpu,cpu_tb_t *tb); |
| 133 |
|
| 134 |
/* Free an instruction block */ |
| 135 |
void mips64_jit_tcb_free(cpu_mips_t *cpu,cpu_tb_t *tb,int list_removal); |
| 136 |
|
| 137 |
/* Execute compiled MIPS code */ |
| 138 |
void *mips64_jit_run_cpu(cpu_gen_t *cpu); |
| 139 |
|
| 140 |
/* Set the Pointer Counter (PC) register */ |
| 141 |
void mips64_set_pc(cpu_tc_t *tc,m_uint64_t new_pc); |
| 142 |
|
| 143 |
/* Set the Return Address (RA) register */ |
| 144 |
void mips64_set_ra(cpu_tc_t *tc,m_uint64_t ret_pc); |
| 145 |
|
| 146 |
/* Single-step operation */ |
| 147 |
void mips64_emit_single_step(cpu_tc_t *tc,mips_insn_t insn); |
| 148 |
|
| 149 |
/* Virtual Breakpoint */ |
| 150 |
void mips64_emit_breakpoint(cpu_tc_t *tc); |
| 151 |
|
| 152 |
/* Emit unhandled instruction code */ |
| 153 |
int mips64_emit_invalid_delay_slot(cpu_tc_t *tc); |
| 154 |
|
| 155 |
/* |
| 156 |
* Increment count register and trigger the timer IRQ if value in compare |
| 157 |
* register is the same. |
| 158 |
*/ |
| 159 |
void mips64_inc_cp0_count_reg(cpu_tc_t *tc); |
| 160 |
|
| 161 |
/* Increment the number of executed instructions (performance debugging) */ |
| 162 |
void mips64_inc_perf_counter(cpu_tc_t *tc); |
| 163 |
|
| 164 |
#endif |