| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2008 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Translation Sharing Groups. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __TSG_H__ |
| 9 |
#define __TSG_H__ |
| 10 |
|
| 11 |
#include "utils.h" |
| 12 |
#include "vm.h" |
| 13 |
|
| 14 |
/* Checksum type */ |
| 15 |
typedef m_uint64_t tsg_checksum_t; |
| 16 |
|
| 17 |
/* Instruction jump patch */ |
| 18 |
struct insn_patch { |
| 19 |
struct insn_patch *next; |
| 20 |
u_char *jit_insn; |
| 21 |
m_uint64_t vaddr; |
| 22 |
}; |
| 23 |
|
| 24 |
/* Instruction patch table */ |
| 25 |
#define INSN_PATCH_TABLE_SIZE 32 |
| 26 |
|
| 27 |
struct insn_patch_table { |
| 28 |
struct insn_patch_table *next; |
| 29 |
struct insn_patch patches[INSN_PATCH_TABLE_SIZE]; |
| 30 |
u_int cur_patch; |
| 31 |
}; |
| 32 |
|
| 33 |
/* Flags for CPU Tranlation Blocks (TB) */ |
| 34 |
#define TB_FLAG_SMC 0x01 /* Self-modifying code */ |
| 35 |
#define TB_FLAG_RECOMP 0x02 /* Page being recompiled */ |
| 36 |
#define TB_FLAG_NOJIT 0x04 /* Page not supported for JIT */ |
| 37 |
#define TB_FLAG_VALID 0x08 |
| 38 |
|
| 39 |
/* Don't use translated code to execute the page */ |
| 40 |
#define TB_FLAG_NOTRANS (TB_FLAG_SMC|TB_FLAG_RECOMP|TB_FLAG_NOJIT) |
| 41 |
|
| 42 |
/* CPU Translation Block */ |
| 43 |
struct cpu_tb { |
| 44 |
u_int flags; |
| 45 |
m_uint64_t vaddr; |
| 46 |
m_uint32_t exec_state; |
| 47 |
tsg_checksum_t checksum; |
| 48 |
|
| 49 |
m_uint64_t acc_count; |
| 50 |
void *target_code; |
| 51 |
|
| 52 |
cpu_tb_t **tb_pprev,*tb_next; |
| 53 |
|
| 54 |
/* Translated Code (can be shared among multiple CPUs) */ |
| 55 |
cpu_tc_t *tc; |
| 56 |
cpu_tb_t **tb_dl_pprev,*tb_dl_next; |
| 57 |
|
| 58 |
/* Virtual page hash */ |
| 59 |
m_uint32_t virt_hash; |
| 60 |
|
| 61 |
/* Physical page information */ |
| 62 |
m_uint32_t phys_page; |
| 63 |
m_uint32_t phys_hash; |
| 64 |
cpu_tb_t **phys_pprev,*phys_next; |
| 65 |
|
| 66 |
#if DEBUG_BLOCK_TIMESTAMP |
| 67 |
m_uint64_t tm_first_use,tm_last_use; |
| 68 |
#endif |
| 69 |
}; |
| 70 |
|
| 71 |
/* Maximum exec pages per TC descriptor */ |
| 72 |
#define TC_MAX_CHUNKS 32 |
| 73 |
|
| 74 |
/* TC descriptor flags */ |
| 75 |
#define TC_FLAG_REMOVAL 0x01 /* Descriptor marked for removal */ |
| 76 |
#define TC_FLAG_VALID 0x02 |
| 77 |
|
| 78 |
/* CPU Translated Code */ |
| 79 |
struct cpu_tc { |
| 80 |
tsg_checksum_t checksum; |
| 81 |
m_uint64_t vaddr; |
| 82 |
m_uint32_t exec_state; |
| 83 |
u_int flags; |
| 84 |
|
| 85 |
/* Temporarily used during the translation */ |
| 86 |
void *target_code; |
| 87 |
|
| 88 |
u_char **jit_insn_ptr; |
| 89 |
u_int jit_chunk_pos; |
| 90 |
insn_exec_page_t *jit_chunks[TC_MAX_CHUNKS]; |
| 91 |
|
| 92 |
/* Current JIT buffer */ |
| 93 |
insn_exec_page_t *jit_buffer; |
| 94 |
u_char *jit_ptr; |
| 95 |
|
| 96 |
/* Patch table */ |
| 97 |
struct insn_patch_table *patch_table; |
| 98 |
|
| 99 |
/* Translation position in target code */ |
| 100 |
u_int trans_pos; |
| 101 |
|
| 102 |
/* 1024 instructions per page, one bit per instruction */ |
| 103 |
m_uint32_t target_bitmap[32]; |
| 104 |
m_uint32_t target_undef_cnt; |
| 105 |
|
| 106 |
/* Reference count */ |
| 107 |
int ref_count; |
| 108 |
|
| 109 |
/* TB list referring to this translated code / exec pages */ |
| 110 |
cpu_tb_t *tb_list; |
| 111 |
|
| 112 |
/* Linked list for hash table referencing */ |
| 113 |
cpu_tc_t **hash_pprev,*hash_next; |
| 114 |
|
| 115 |
/* Linked list for single-CPU referencement (ref_count=1) */ |
| 116 |
cpu_tc_t **sc_pprev,*sc_next; |
| 117 |
}; |
| 118 |
|
| 119 |
#define TC_TARGET_BITMAP_INDEX(x) (((x) >> 7) & 0x1F) |
| 120 |
#define TC_TARGET_BITMAP_POS(x) (((x) >> 2) & 0x1F) |
| 121 |
|
| 122 |
/* Mark the specified vaddr as a target for further recompiling */ |
| 123 |
static inline void tc_set_target_bit(cpu_tc_t *tc,m_uint32_t vaddr) |
| 124 |
{ |
| 125 |
int index,pos; |
| 126 |
|
| 127 |
index = TC_TARGET_BITMAP_INDEX(vaddr); |
| 128 |
pos = TC_TARGET_BITMAP_POS(vaddr); |
| 129 |
|
| 130 |
tc->target_bitmap[index] |= 1 << pos; |
| 131 |
} |
| 132 |
|
| 133 |
/* Returns TRUE if the specified vaddr is in the target bitmap */ |
| 134 |
static inline int tc_get_target_bit(cpu_tc_t *tc,m_uint32_t vaddr) |
| 135 |
{ |
| 136 |
int index,pos; |
| 137 |
|
| 138 |
index = TC_TARGET_BITMAP_INDEX(vaddr); |
| 139 |
pos = TC_TARGET_BITMAP_POS(vaddr); |
| 140 |
|
| 141 |
return(tc->target_bitmap[index] & (1 << pos)); |
| 142 |
} |
| 143 |
|
| 144 |
/* Get the JIT instruction pointer in a translated code */ |
| 145 |
static forced_inline u_char *tc_get_host_ptr(cpu_tc_t *tc,m_uint64_t vaddr) |
| 146 |
{ |
| 147 |
m_uint32_t offset; |
| 148 |
|
| 149 |
offset = (vaddr & VM_PAGE_IMASK) >> 2; |
| 150 |
return(tc->jit_insn_ptr[offset]); |
| 151 |
} |
| 152 |
|
| 153 |
/* Get the JIT instruction pointer in a translated block */ |
| 154 |
static forced_inline u_char *tb_get_host_ptr(cpu_tb_t *tb,m_uint64_t vaddr) |
| 155 |
{ |
| 156 |
return(tc_get_host_ptr(tb->tc,vaddr)); |
| 157 |
} |
| 158 |
|
| 159 |
/* Lookup return codes */ |
| 160 |
#define TSG_LOOKUP_NEW 0 |
| 161 |
#define TSG_LOOKUP_SHARED 1 |
| 162 |
|
| 163 |
/* Translation sharing group statistics */ |
| 164 |
struct tsg_stats { |
| 165 |
u_int total_tc; |
| 166 |
u_int shared_tc; |
| 167 |
u_int shared_pages; |
| 168 |
}; |
| 169 |
|
| 170 |
enum { |
| 171 |
CPU_JIT_DISABLE_CPU = 0, |
| 172 |
CPU_JIT_ENABLE_CPU, |
| 173 |
}; |
| 174 |
|
| 175 |
/* Allocate a new TCB descriptor */ |
| 176 |
cpu_tc_t *tc_alloc(cpu_gen_t *cpu,m_uint64_t vaddr,m_uint32_t exec_state); |
| 177 |
|
| 178 |
/* Bind a CPU to a TSG - If the group isn't specified, create one */ |
| 179 |
int tsg_bind_cpu(cpu_gen_t *cpu); |
| 180 |
|
| 181 |
/* Unbind a CPU from a TSG - release all resources used */ |
| 182 |
int tsg_unbind_cpu(cpu_gen_t *cpu); |
| 183 |
|
| 184 |
/* Create a JIT chunk */ |
| 185 |
int tc_alloc_jit_chunk(cpu_gen_t *cpu,cpu_tc_t *tc); |
| 186 |
|
| 187 |
/* Allocate a new TC descriptor */ |
| 188 |
cpu_tc_t *tc_alloc(cpu_gen_t *cpu,m_uint64_t vaddr,m_uint32_t exec_state); |
| 189 |
|
| 190 |
/* Compute a checksum on a page */ |
| 191 |
tsg_checksum_t tsg_checksum_page(void *page,ssize_t size); |
| 192 |
|
| 193 |
/* Try to find a TC descriptor to share generated code */ |
| 194 |
int tc_find_shared(cpu_gen_t *cpu,cpu_tb_t *tb); |
| 195 |
|
| 196 |
/* Register a newly compiled TCB descriptor */ |
| 197 |
void tc_register(cpu_gen_t *cpu,cpu_tb_t *tb,cpu_tc_t *tc); |
| 198 |
|
| 199 |
/* Remove all TC descriptors belonging to a single CPU (ie not shared) */ |
| 200 |
int tsg_remove_single_desc(cpu_gen_t *cpu); |
| 201 |
|
| 202 |
/* Dump a TCB */ |
| 203 |
void tb_dump(cpu_tb_t *tb); |
| 204 |
|
| 205 |
/* Dump a TCB descriptor */ |
| 206 |
void tc_dump(cpu_tc_t *tc); |
| 207 |
|
| 208 |
/* Show statistics about all translation groups */ |
| 209 |
void tsg_show_stats(void); |
| 210 |
|
| 211 |
/* Adjust the JIT buffer if its size is not sufficient */ |
| 212 |
int tc_adjust_jit_buffer(cpu_gen_t *cpu,cpu_tc_t *tc, |
| 213 |
void (*set_jump)(u_char **insn,u_char *dst)); |
| 214 |
|
| 215 |
/* Record a patch to apply in a compiled block */ |
| 216 |
struct insn_patch *tc_record_patch(cpu_gen_t *cpu,cpu_tc_t *tc, |
| 217 |
u_char *jit_ptr,m_uint64_t vaddr); |
| 218 |
|
| 219 |
/* Apply all patches */ |
| 220 |
int tc_apply_patches(cpu_tc_t *tc,void (*set_patch)(u_char *insn,u_char *dst)); |
| 221 |
|
| 222 |
/* Free the patch table */ |
| 223 |
void tc_free_patches(cpu_tc_t *tc); |
| 224 |
|
| 225 |
/* Initialize the JIT structures of a CPU */ |
| 226 |
int cpu_jit_init(cpu_gen_t *cpu,size_t virt_hash_size,size_t phys_hash_size); |
| 227 |
|
| 228 |
/* Shutdown the JIT structures of a CPU */ |
| 229 |
void cpu_jit_shutdown(cpu_gen_t *cpu); |
| 230 |
|
| 231 |
/* Allocate a new Translation Block */ |
| 232 |
cpu_tb_t *tb_alloc(cpu_gen_t *cpu,m_uint64_t vaddr,u_int exec_state); |
| 233 |
|
| 234 |
/* Free a Translation Block */ |
| 235 |
void tb_free(cpu_gen_t *cpu,cpu_tb_t *tb); |
| 236 |
|
| 237 |
/* Enable a Tranlsation Block */ |
| 238 |
void tb_enable(cpu_gen_t *cpu,cpu_tb_t *tb); |
| 239 |
|
| 240 |
/* Flush all TCB of a virtual CPU */ |
| 241 |
void cpu_jit_tcb_flush_all(cpu_gen_t *cpu); |
| 242 |
|
| 243 |
/* Mark a TB as containing self-modifying code */ |
| 244 |
void tb_mark_smc(cpu_gen_t *cpu,cpu_tb_t *tb); |
| 245 |
|
| 246 |
/* Handle write access on an executable page */ |
| 247 |
void cpu_jit_write_on_exec_page(cpu_gen_t *cpu, |
| 248 |
m_uint32_t wr_phys_page, |
| 249 |
m_uint32_t wr_hp, |
| 250 |
m_uint32_t ip_phys_page); |
| 251 |
|
| 252 |
#endif |