Debian Codegraph @

stable/ppc32_jit.h

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