Debian Codegraph @

unstable/mips64_jit.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * MIPS64 JIT compiler.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <assert.h>
17
18 #include "sbox.h"
19 #include "cpu.h"
20 #include "device.h"
21 #include "tcb.h"
22 #include "mips64.h"
23 #include "mips64_cp0.h"
24 #include "mips64_exec.h"
25 #include "mips64_jit.h"
26 #include "insn_lookup.h"
27 #include "memory.h"
28 #include "ptask.h"
29
30 #include MIPS64_ARCH_INC_FILE
31
32 #define DEBUG_JIT_SHARED 0
33
34 #if DEBUG_BLOCK_TIMESTAMP
35 static volatile m_uint64_t jit_jiffies = 0;
36 #endif
37
38 /* MIPS jump instructions for block scan */
39 struct mips64_insn_jump mips64_insn_jumps[] = {
40 { "b" , 0xffff0000, 0x10000000, 16, 1 },
41 { "bal" , 0xffff0000, 0x04110000, 16, 1 },
42 { "beq" , 0xfc000000, 0x10000000, 16, 1 },
43 { "beql" , 0xfc000000, 0x50000000, 16, 1 },
44 { "bgez" , 0xfc1f0000, 0x04010000, 16, 1 },
45 { "bgezl" , 0xfc1f0000, 0x04030000, 16, 1 },
46 { "bgezal" , 0xfc1f0000, 0x04110000, 16, 1 },
47 { "bgezall" , 0xfc1f0000, 0x04130000, 16, 1 },
48 { "bgtz" , 0xfc1f0000, 0x1c000000, 16, 1 },
49 { "bgtzl" , 0xfc1f0000, 0x5c000000, 16, 1 },
50 { "blez" , 0xfc1f0000, 0x18000000, 16, 1 },
51 { "blezl" , 0xfc1f0000, 0x58000000, 16, 1 },
52 { "bltz" , 0xfc1f0000, 0x04000000, 16, 1 },
53 { "bltzl" , 0xfc1f0000, 0x04020000, 16, 1 },
54 { "bltzal" , 0xfc1f0000, 0x04100000, 16, 1 },
55 { "bltzall" , 0xfc1f0000, 0x04120000, 16, 1 },
56 { "bne" , 0xfc000000, 0x14000000, 16, 1 },
57 { "bnel" , 0xfc000000, 0x54000000, 16, 1 },
58 { "j" , 0xfc000000, 0x08000000, 26, 0 },
59 { NULL , 0x00000000, 0x00000000, 0, 0 },
60 };
61
62 /* Instruction Lookup Table */
63 static insn_lookup_t *ilt = NULL;
64
65 static void *mips64_jit_get_insn(int index)
66 {
67 return(&mips64_insn_tags[index]);
68 }
69
70 static int mips64_jit_chk_lo(struct mips64_insn_tag *tag,int value)
71 {
72 return((value & tag->mask) == (tag->value & 0xFFFF));
73 }
74
75 static int mips64_jit_chk_hi(struct mips64_insn_tag *tag,int value)
76 {
77 return((value & (tag->mask >> 16)) == (tag->value >> 16));
78 }
79
80 /* Destroy instruction lookup table */
81 static void destroy_ilt(void)
82 {
83 assert(ilt);
84 ilt_destroy(ilt);
85 ilt = NULL;
86 }
87
88 /* Initialize instruction lookup table */
89 void mips64_jit_create_ilt(void)
90 {
91 int i,count;
92
93 for(i=0,count=0;mips64_insn_tags[i].emit;i++)
94 count++;
95
96 ilt = ilt_create("mips64j",count,
97 (ilt_get_insn_cbk_t)mips64_jit_get_insn,
98 (ilt_check_cbk_t)mips64_jit_chk_lo,
99 (ilt_check_cbk_t)mips64_jit_chk_hi);
100
101 atexit(destroy_ilt);
102 }
103
104 /* Initialize the JIT structure */
105 int mips64_jit_init(cpu_mips_t *cpu)
106 {
107 if (tsg_bind_cpu(cpu->gen) == -1)
108 return(-1);
109
110 return(cpu_jit_init(cpu->gen,
111 MIPS_JIT_VIRT_HASH_SIZE,
112 MIPS_JIT_PHYS_HASH_SIZE));
113 }
114
115 /* Flush the JIT */
116 u_int mips64_jit_flush(cpu_mips_t *cpu,u_int threshold)
117 {
118 /* TO FIX / ENHANCE */
119 return(tsg_remove_single_desc(cpu->gen));
120 }
121
122 /* Shutdown the JIT */
123 void mips64_jit_shutdown(cpu_mips_t *cpu)
124 {
125 cpu_jit_shutdown(cpu->gen);
126 }
127
128 /* Find the JIT code emitter for the specified MIPS instruction */
129 static struct mips64_insn_tag *insn_tag_find(mips_insn_t ins)
130 {
131 struct mips64_insn_tag *tag = NULL;
132 int index;
133
134 index = ilt_lookup(ilt,ins);
135 tag = mips64_jit_get_insn(index);
136 return tag;
137 }
138
139 /* Check if the specified MIPS instruction is a jump */
140 _Unused static struct mips64_insn_jump *insn_jump_find(mips_insn_t ins)
141 {
142 struct mips64_insn_jump *jump = NULL;
143 int i;
144
145 for(i=0;mips64_insn_jumps[i].name;i++)
146 if ((ins & mips64_insn_jumps[i].mask) == mips64_insn_jumps[i].value) {
147 jump = &mips64_insn_jumps[i];
148 break;
149 }
150
151 return(jump);
152 }
153
154 /* Fetch a MIPS instruction */
155 static forced_inline mips_insn_t insn_fetch(cpu_tc_t *tc)
156 {
157 return(vmtoh32(((mips_insn_t *)tc->target_code)[tc->trans_pos]));
158 }
159
160 /* Emit a breakpoint if necessary */
161 #if BREAKPOINT_ENABLE
162 static void insn_emit_breakpoint(cpu_mips_t *cpu,cpu_tc_t *tc)
163 {
164 m_uint64_t pc;
165 int i;
166
167 pc = tc->vaddr + ((tc->trans_pos-1) << 2);
168
169 for(i=0;i<MIPS64_MAX_BREAKPOINTS;i++)
170 if (pc == cpu->breakpoints[i]) {
171 mips64_emit_breakpoint(tc);
172 break;
173 }
174 }
175 #endif /* BREAKPOINT_ENABLE */
176
177 /* Check if an instruction is in a delay slot or not */
178 int mips64_jit_is_delay_slot(cpu_tc_t *tc,m_uint64_t pc)
179 {
180 struct mips64_insn_tag *tag;
181 m_uint32_t offset,insn;
182
183 offset = (pc - tc->vaddr) >> 2;
184
185 if (!offset)
186 return(FALSE);
187
188 /* Fetch the previous instruction to determine if it is a jump */
189 insn = vmtoh32(((m_uint32_t *)tc->target_code)[offset-1]);
190 tag = insn_tag_find(insn);
191 assert(tag != NULL);
192 return(!tag->delay_slot);
193 }
194
195 /* Fetch a MIPS instruction and emit corresponding translated code */
196 struct mips64_insn_tag *mips64_jit_fetch_and_emit(cpu_mips_t *cpu,
197 cpu_tc_t *tc,
198 int delay_slot)
199 {
200 struct mips64_insn_tag *tag;
201 mips_insn_t code;
202
203 code = insn_fetch(tc);
204 tag = insn_tag_find(code);
205 assert(tag);
206
207 /* Branch-delay slot is in another page: slow exec */
208 if ((tc->trans_pos == (MIPS_INSN_PER_PAGE-1)) && !tag->delay_slot) {
209 tc->jit_insn_ptr[tc->trans_pos] = tc->jit_ptr;
210
211 mips64_set_pc(tc,tc->vaddr + (tc->trans_pos << 2));
212 mips64_emit_single_step(tc,code);
213 mips64_jit_tcb_push_epilog(tc);
214 tc->trans_pos++;
215 return tag;
216 }
217
218 if (delay_slot && !tag->delay_slot) {
219 mips64_emit_invalid_delay_slot(tc);
220 return NULL;
221 }
222
223 if (!delay_slot)
224 tc->jit_insn_ptr[tc->trans_pos] = tc->jit_ptr;
225
226 if (delay_slot != 2)
227 tc->trans_pos++;
228
229 #if DEBUG_INSN_PERF_CNT
230 mips64_inc_perf_counter(tc);
231 #endif
232
233 if (!delay_slot) {
234 /* Check for IRQs + Increment count register before jumps */
235 if (!tag->delay_slot) {
236 mips64_inc_cp0_count_reg(tc);
237 mips64_check_pending_irq(tc);
238 }
239 }
240
241 #if BREAKPOINT_ENABLE
242 if (cpu->breakpoints_enabled)
243 insn_emit_breakpoint(cpu,tc);
244 #endif
245
246 tag->emit(cpu,tc,code);
247 return tag;
248 }
249
250 /* Add end of JIT block */
251 static void mips64_jit_tcb_add_end(cpu_tc_t *tc)
252 {
253 mips64_set_pc(tc,tc->vaddr+(tc->trans_pos<<2));
254 mips64_jit_tcb_push_epilog(tc);
255 }
256
257 /* Record a patch to apply in a compiled block */
258 int mips64_jit_tcb_record_patch(cpu_mips_t *cpu,cpu_tc_t *tc,
259 u_char *jit_ptr,m_uint64_t vaddr)
260 {
261 struct insn_patch *patch;
262
263 patch = tc_record_patch(cpu->gen,tc,jit_ptr,vaddr);
264 return((patch != NULL) ? 0 : -1);
265 }
266
267 /* Apply all patches */
268 static int mips64_jit_tcb_apply_patches(cpu_mips_t *cpu,cpu_tc_t *tc)
269 {
270 tc_apply_patches(tc,mips64_jit_tcb_set_patch);
271 return(0);
272 }
273
274 /* Adjust the JIT buffer if its size is not sufficient */
275 static int mips64_jit_tcb_adjust_buffer(cpu_mips_t *cpu,cpu_tc_t *tc)
276 {
277 return(tc_adjust_jit_buffer(cpu->gen,tc,mips64_jit_tcb_set_jump));
278 }
279
280 /* Produce translated code for a page. If this fails, use non-compiled mode */
281 static cpu_tc_t *mips64_jit_tcb_translate(cpu_mips_t *cpu,cpu_tb_t *tb)
282 {
283 struct mips64_insn_tag *tag;
284 cpu_tc_t *tc;
285
286 /* The page is not shared, we have to compile it */
287 tc = tc_alloc(cpu->gen,tb->vaddr,tb->exec_state);
288
289 if (tc == NULL)
290 return NULL;
291
292 tc->target_code = tb->target_code;
293 tc->trans_pos = 0;
294
295 /* Emit native code for each instruction */
296 while(tc->trans_pos < MIPS_INSN_PER_PAGE)
297 {
298 if (unlikely(!(tag = mips64_jit_fetch_and_emit(cpu,tc,0)))) {
299 cpu_log(cpu->gen,"JIT",
300 "unable to fetch instruction (VA=0x%8.8llx,exec_state=%u).\n",
301 tb->vaddr,tb->exec_state);
302 return NULL;
303 }
304
305 #if DEBUG_BLOCK_COMPILE
306 cpu_log(cpu->gen,"JIT","Page 0x%8.8llx: emitted tag 0x%8.8x/0x%8.8x\n",
307 tb->vaddr,tag->mask,tag->value);
308 #endif
309
310 if (mips64_jit_tcb_adjust_buffer(cpu,tc) == -1)
311 return NULL;
312 }
313
314 mips64_jit_tcb_add_end(tc);
315 mips64_jit_tcb_apply_patches(cpu,tc);
316 tc_free_patches(tc);
317 tc->target_code = NULL;
318 return tc;
319 }
320
321 /* Compile a MIPS instruction page */
322 static cpu_tb_t *
323 mips64_jit_tcb_compile(cpu_mips_t *cpu,m_uint64_t vaddr,m_uint32_t exec_state)
324 {
325 cpu_tb_t *tb;
326 cpu_tc_t *tc;
327 m_uint64_t page_addr;
328 mips_insn_t *mips_code;
329 m_uint32_t phys_page;
330
331 page_addr = vaddr & MIPS_MIN_PAGE_MASK;
332
333 /*
334 * Get the mips code address from the host point of view.
335 * If there is an error (TLB,...), we return directly to the main loop.
336 */
337 mips_code = cpu->mem_op_ifetch(cpu,page_addr);
338
339 if (unlikely(cpu->translate(cpu,page_addr,&phys_page)))
340 return NULL;
341
342 /* Create a new translation block */
343 if (!(tb = tb_alloc(cpu->gen,page_addr,exec_state)))
344 return NULL;
345
346 tb->vaddr = page_addr;
347 tb->exec_state = exec_state;
348 tb->phys_page = phys_page;
349 tb->phys_hash = mips64_jit_get_phys_hash(phys_page);
350 tb->virt_hash = mips64_jit_get_virt_hash(page_addr);
351 tb->target_code = mips_code;
352 tb->checksum = tsg_checksum_page(tb->target_code,VM_PAGE_SIZE);
353
354 /* Check if we can share this page with another virtual CPU */
355 if (tc_find_shared(cpu->gen,tb) == TSG_LOOKUP_SHARED) {
356 #if DEBUG_JIT_SHARED
357 cpu_log(cpu->gen,"JIT","Page 0x%8.8llx is shared (ref_count=%u)\n",
358 tb->vaddr,tb->tc->ref_count);
359 #endif
360 return tb;
361 }
362
363 /* The page is not shared, we have to compile it */
364 tc = mips64_jit_tcb_translate(cpu,tb);
365
366 if (tc != NULL) {
367 tc->target_code = tb->target_code;
368 tc->trans_pos = 0;
369
370 tb_enable(cpu->gen,tb);
371 tc_register(cpu->gen,tb,tc);
372 } else {
373 tb->flags |= TB_FLAG_NOJIT;
374 tb_enable(cpu->gen,tb);
375 }
376
377 return tb;
378 }
379
380 /* Run a compiled MIPS instruction block */
381 static forced_inline
382 void mips64_jit_tcb_run(cpu_mips_t *cpu,cpu_tb_t *tb)
383 {
384 #if DEBUG_SYM_TREE
385 struct symbol *sym = NULL;
386 int mark = FALSE;
387 #endif
388
389 if (unlikely(cpu->pc & 0x03)) {
390 fprintf(stderr,"mips64_jit_tcb_run: Invalid PC 0x%llx.\n",cpu->pc);
391 mips64_dump_regs(cpu->gen);
392 mips64_tlb_dump(cpu->gen);
393 cpu_stop(cpu->gen);
394 return;
395 }
396
397 #if DEBUG_SYM_TREE
398 if (cpu->sym_trace && cpu->sym_tree)
399 {
400 if ((sym = mips64_sym_lookup(cpu,cpu->pc)) != NULL) {
401 cpu_log(cpu,"mips64_jit_tcb_run(start)",
402 "%s (PC=0x%llx) RA = 0x%llx\na0=0x%llx, "
403 "a1=0x%llx, a2=0x%llx, a3=0x%llx\n",
404 sym->name, cpu->pc, cpu->gpr[MIPS_GPR_RA],
405 cpu->gpr[MIPS_GPR_A0], cpu->gpr[MIPS_GPR_A1],
406 cpu->gpr[MIPS_GPR_A2], cpu->gpr[MIPS_GPR_A3]);
407 mark = TRUE;
408 }
409 }
410 #endif
411
412 /* Execute JIT compiled code */
413 mips64_jit_tcb_exec(cpu,tb);
414
415 #if DEBUG_SYM_TREE
416 if (mark) {
417 cpu_log(cpu,"mips64_jit_tcb_run(end)","%s, v0 = 0x%llx\n",
418 sym->name,cpu->gpr[MIPS_GPR_V0]);
419 }
420 #endif
421 }
422
423 /* Execute compiled MIPS code */
424 void *mips64_jit_run_cpu(cpu_gen_t *gen)
425 {
426 cpu_mips_t *cpu = CPU_MIPS64(gen);
427 pthread_t timer_irq_thread;
428 cpu_tb_t *tb;
429 m_uint32_t hv,hp;
430 m_uint32_t phys_page;
431 int timer_irq_check = 0;
432
433 if (pthread_create(&timer_irq_thread,NULL,
434 (void *)mips64_timer_irq_run,cpu))
435 {
436 fprintf(stderr,
437 "VM '%s': unable to create Timer IRQ thread for CPU%u.\n",
438 cpu->vm->name,gen->id);
439 cpu_stop(cpu->gen);
440 return NULL;
441 }
442
443 gen->cpu_thread_running = TRUE;
444 cpu_exec_loop_set(gen);
445
446 start_cpu:
447 gen->idle_count = 0;
448
449 for(;;) {
450 if (unlikely(gen->state != CPU_STATE_RUNNING)) {
451 /*
452 * We are paused/halted, so free the TCB/TCD in order to allow
453 * reallocation of exec pages for other vCPUs.
454 */
455 cpu_jit_tcb_flush_all(cpu->gen);
456 break;
457 }
458
459 #if DEBUG_BLOCK_PERF_CNT
460 cpu->perf_counter++;
461 #endif
462 /* Handle virtual idle loop */
463 if (unlikely(cpu->pc == cpu->idle_pc)) {
464 if (++gen->idle_count == gen->idle_max) {
465 cpu_idle_loop(gen);
466 gen->idle_count = 0;
467 }
468 }
469
470 /* Handle the virtual CPU clock */
471 if (++timer_irq_check == cpu->timer_irq_check_itv) {
472 timer_irq_check = 0;
473
474 if (cpu->timer_irq_pending && !cpu->irq_disable) {
475 mips64_trigger_timer_irq(cpu);
476 mips64_trigger_irq(cpu);
477 cpu->timer_irq_pending--;
478 }
479 }
480
481 /* Get the JIT block corresponding to PC register */
482 hv = mips64_jit_get_virt_hash(cpu->pc);
483 tb = gen->tb_virt_hash[hv];
484
485 if (unlikely(!tb) || unlikely(!mips64_jit_tcb_match(cpu,tb)))
486 {
487 /* slow lookup: try to find the page by physical address */
488 cpu->translate(cpu,cpu->pc,&phys_page);
489 hp = mips64_jit_get_phys_hash(phys_page);
490
491 for(tb=gen->tb_phys_hash[hp];tb;tb=tb->phys_next)
492 if (mips64_jit_tcb_match(cpu,tb))
493 goto tb_found;
494
495 /* the TB doesn't exist, compile the page */
496 tb = mips64_jit_tcb_compile(cpu,cpu->pc,cpu->exec_state);
497
498 if (unlikely(!tb)) {
499 fprintf(stderr,
500 "VM '%s': unable to compile block for CPU%u PC=0x%llx\n",
501 cpu->vm->name,gen->id,cpu->pc);
502 cpu_stop(gen);
503 break;
504 }
505
506 tb_found:
507 /* update the virtual hash table */
508 gen->tb_virt_hash[hv] = tb;
509 }
510
511 #if DEBUG_BLOCK_TIMESTAMP
512 tb->tm_last_use = jit_jiffies++;
513 #endif
514 tb->acc_count++;
515
516 cpu->current_tb = tb;
517
518 if (unlikely(tb->flags & TB_FLAG_NOTRANS))
519 mips64_exec_page(cpu);
520 else
521 mips64_jit_tcb_run(cpu,tb);
522 }
523
524 /* Check regularly if the CPU has been restarted */
525 while(gen->cpu_thread_running) {
526 gen->seq_state++;
527
528 switch(gen->state) {
529 case CPU_STATE_RUNNING:
530 printf("VM %s: starting CPU!\n",cpu->vm->name);
531 goto start_cpu;
532
533 case CPU_STATE_HALTED:
534 gen->cpu_thread_running = FALSE;
535 pthread_join(timer_irq_thread,NULL);
536 return NULL;
537 }
538
539 /* CPU is paused */
540 usleep(200000);
541 }
542
543 return NULL;
544 }