Debian Codegraph @

stable/ppc32_jit.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * PPC32 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 "cpu.h"
19 #include "device.h"
20 #include "ppc32.h"
21 #include "ppc32_exec.h"
22 #include "ppc32_jit.h"
23 #include "insn_lookup.h"
24 #include "memory.h"
25 #include "ptask.h"
26
27 #include PPC32_ARCH_INC_FILE
28
29 /* Instruction Lookup Table */
30 static insn_lookup_t *ilt = NULL;
31
32 static void *ppc32_jit_get_insn(int index)
33 {
34 return(&ppc32_insn_tags[index]);
35 }
36
37 static int ppc32_jit_chk_lo(struct ppc32_insn_tag *tag,int value)
38 {
39 return((value & tag->mask) == (tag->value & 0xFFFF));
40 }
41
42 static int ppc32_jit_chk_hi(struct ppc32_insn_tag *tag,int value)
43 {
44 return((value & (tag->mask >> 16)) == (tag->value >> 16));
45 }
46
47 /* Destroy instruction lookup table */
48 static void destroy_ilt(void)
49 {
50 assert(ilt);
51 ilt_destroy(ilt);
52 ilt = NULL;
53 }
54
55 /* Initialize instruction lookup table */
56 void ppc32_jit_create_ilt(void)
57 {
58 int i,count;
59
60 for(i=0,count=0;ppc32_insn_tags[i].emit;i++)
61 count++;
62
63 ilt = ilt_create("ppc32j",count,
64 (ilt_get_insn_cbk_t)ppc32_jit_get_insn,
65 (ilt_check_cbk_t)ppc32_jit_chk_lo,
66 (ilt_check_cbk_t)ppc32_jit_chk_hi);
67
68 atexit(destroy_ilt);
69 }
70
71 /* Initialize the JIT structure */
72 int ppc32_jit_init(cpu_ppc_t *cpu)
73 {
74 insn_exec_page_t *cp;
75 u_char *cp_addr;
76 u_int area_size;
77 size_t len;
78 int i;
79
80 /* JIT mapping for executable pages */
81 len = PPC_JIT_IA_HASH_SIZE * sizeof(void *);
82 cpu->exec_blk_map = m_memalign(4096,len);
83 memset(cpu->exec_blk_map,0,len);
84
85 /* Physical mapping for executable pages */
86 len = PPC_JIT_PHYS_HASH_SIZE * sizeof(void *);
87 cpu->exec_phys_map = m_memalign(4096,len);
88 memset(cpu->exec_phys_map,0,len);
89
90 /* Get area size */
91 if (!(area_size = cpu->vm->exec_area_size))
92 area_size = PPC_EXEC_AREA_SIZE;
93
94 /* Create executable page area */
95 cpu->exec_page_area_size = area_size * 1048576;
96 cpu->exec_page_area = memzone_map_exec_area(cpu->exec_page_area_size);
97
98 if (!cpu->exec_page_area) {
99 fprintf(stderr,
100 "ppc32_jit_init: unable to create exec area (size %lu)\n",
101 (u_long)cpu->exec_page_area_size);
102 return(-1);
103 }
104
105 /* Carve the executable page area */
106 cpu->exec_page_count = cpu->exec_page_area_size / PPC_JIT_BUFSIZE;
107
108 cpu->exec_page_array = calloc(cpu->exec_page_count,
109 sizeof(insn_exec_page_t));
110
111 if (!cpu->exec_page_array) {
112 fprintf(stderr,"ppc32_jit_init: unable to create exec page array\n");
113 return(-1);
114 }
115
116 for(i=0,cp_addr=cpu->exec_page_area;i<cpu->exec_page_count;i++) {
117 cp = &cpu->exec_page_array[i];
118
119 cp->ptr = cp_addr;
120 cp_addr += PPC_JIT_BUFSIZE;
121
122 cp->next = cpu->exec_page_free_list;
123 cpu->exec_page_free_list = cp;
124 }
125
126 printf("CPU%u: carved JIT exec zone of %lu Mb into %lu pages of %u Kb.\n",
127 cpu->gen->id,
128 (u_long)(cpu->exec_page_area_size / 1048576),
129 (u_long)cpu->exec_page_count,PPC_JIT_BUFSIZE / 1024);
130 return(0);
131 }
132
133 /* Flush the JIT */
134 u_int ppc32_jit_flush(cpu_ppc_t *cpu,u_int threshold)
135 {
136 ppc32_jit_tcb_t *p,*next;
137 m_uint32_t ia_hash;
138 u_int count = 0;
139
140 if (!threshold)
141 threshold = (u_int)(-1); /* UINT_MAX not defined everywhere */
142
143 for(p=cpu->tcb_list;p;p=next) {
144 next = p->next;
145
146 /* flushing not allowed */
147 if (p->flags & PPC32_JIT_TCB_FLAG_NO_FLUSH)
148 continue;
149
150 if (p->acc_count <= threshold) {
151 ia_hash = ppc32_jit_get_ia_hash(p->start_ia);
152 ppc32_jit_tcb_free(cpu,p,TRUE);
153
154 if (cpu->exec_blk_map[ia_hash] == p)
155 cpu->exec_blk_map[ia_hash] = NULL;
156 count++;
157 }
158 }
159
160 cpu->compiled_pages -= count;
161 return(count);
162 }
163
164 /* Shutdown the JIT */
165 void ppc32_jit_shutdown(cpu_ppc_t *cpu)
166 {
167 ppc32_jit_tcb_t *p,*next;
168
169 /* Flush the JIT */
170 ppc32_jit_flush(cpu,0);
171
172 /* Free the instruction blocks */
173 for(p=cpu->tcb_free_list;p;p=next) {
174 next = p->next;
175 free(p);
176 }
177
178 /* Unmap the executable page area */
179 if (cpu->exec_page_area)
180 memzone_unmap(cpu->exec_page_area,cpu->exec_page_area_size);
181
182 /* Free the exec page array */
183 free(cpu->exec_page_array);
184
185 /* Free JIT block mapping */
186 free(cpu->exec_blk_map);
187
188 /* Free physical mapping for executable pages */
189 free(cpu->exec_phys_map);
190 }
191
192 /* Allocate an exec page */
193 static inline insn_exec_page_t *exec_page_alloc(cpu_ppc_t *cpu)
194 {
195 insn_exec_page_t *p;
196 u_int count;
197
198 /* If the free list is empty, flush JIT */
199 if (unlikely(!cpu->exec_page_free_list))
200 {
201 if (cpu->jit_flush_method) {
202 cpu_log(cpu->gen,
203 "JIT","flushing data structures (compiled pages=%u)\n",
204 cpu->compiled_pages);
205 ppc32_jit_flush(cpu,0);
206 } else {
207 count = ppc32_jit_flush(cpu,100);
208 cpu_log(cpu->gen,"JIT","partial JIT flush (count=%u)\n",count);
209
210 if (!cpu->exec_page_free_list)
211 ppc32_jit_flush(cpu,0);
212 }
213
214 /* Use both methods alternatively */
215 cpu->jit_flush_method = 1 - cpu->jit_flush_method;
216 }
217
218 if (unlikely(!(p = cpu->exec_page_free_list)))
219 return NULL;
220
221 cpu->exec_page_free_list = p->next;
222 cpu->exec_page_alloc++;
223 return p;
224 }
225
226 /* Free an exec page and returns it to the pool */
227 static inline void exec_page_free(cpu_ppc_t *cpu,insn_exec_page_t *p)
228 {
229 if (p) {
230 p->next = cpu->exec_page_free_list;
231 cpu->exec_page_free_list = p;
232 cpu->exec_page_alloc--;
233 }
234 }
235
236 /* Find the JIT code emitter for the specified PowerPC instruction */
237 static struct ppc32_insn_tag *insn_tag_find(ppc_insn_t ins)
238 {
239 struct ppc32_insn_tag *tag = NULL;
240 int index;
241
242 index = ilt_lookup(ilt,ins);
243 tag = ppc32_jit_get_insn(index);
244 return tag;
245 }
246
247 /* Fetch a PowerPC instruction */
248 static forced_inline ppc_insn_t insn_fetch(ppc32_jit_tcb_t *b)
249 {
250 return(vmtoh32(b->ppc_code[b->ppc_trans_pos]));
251 }
252
253 #define DEBUG_HREG 0
254
255 /* Show register allocation status */
256 _Unused static void ppc32_jit_show_hreg_status(cpu_ppc_t *cpu)
257 {
258 struct hreg_map *map;
259
260 printf("PPC32-JIT: reg status for insn '%s'\n",cpu->jit_hreg_seq_name);
261
262 for(map=cpu->hreg_map_list;map;map=map->next) {
263 switch(map->flags) {
264 case 0:
265 printf(" hreg %d is free, mapped to vreg %d\n",
266 map->hreg,map->vreg);
267 break;
268 case HREG_FLAG_ALLOC_LOCKED:
269 printf(" hreg %d is locked, mapped to vreg %d\n",
270 map->hreg,map->vreg);
271 break;
272 case HREG_FLAG_ALLOC_FORCED:
273 printf(" hreg %d is in forced alloc\n",map->hreg);
274 break;
275 }
276 }
277 }
278
279 /* Extract an host reg mapping from the register list */
280 static void ppc32_jit_extract_hreg(cpu_ppc_t *cpu,struct hreg_map *map)
281 {
282 if (map->prev != NULL)
283 map->prev->next = map->next;
284 else
285 cpu->hreg_map_list = map->next;
286
287 if (map->next != NULL)
288 map->next->prev = map->prev;
289 else
290 cpu->hreg_lru = map->prev;
291 }
292
293 /* Insert a reg map as head of list (as MRU element) */
294 void ppc32_jit_insert_hreg_mru(cpu_ppc_t *cpu,struct hreg_map *map)
295 {
296 map->next = cpu->hreg_map_list;
297 map->prev = NULL;
298
299 if (map->next == NULL) {
300 cpu->hreg_lru = map;
301 } else {
302 map->next->prev = map;
303 }
304
305 cpu->hreg_map_list = map;
306 }
307
308 /* Start register allocation sequence */
309 void ppc32_jit_start_hreg_seq(cpu_ppc_t *cpu,char *insn)
310 {
311 struct hreg_map *map;
312
313 #if DEBUG_HREG
314 printf("Starting hreg_seq insn='%s'\n",insn);
315 #endif
316
317 /* Reset the allocation state of all host registers */
318 for(map=cpu->hreg_map_list;map;map=map->next)
319 map->flags = 0;
320
321 /* Save the instruction name for debugging/error analysis */
322 cpu->jit_hreg_seq_name = insn;
323 }
324
325 /* Close register allocation sequence */
326 void ppc32_jit_close_hreg_seq(cpu_ppc_t *cpu)
327 {
328 #if DEBUG_HREG
329 ppc32_show_hreg_status(cpu);
330 #endif
331 }
332
333 /* Find a free host register to use */
334 static struct hreg_map *ppc32_jit_get_free_hreg(cpu_ppc_t *cpu)
335 {
336 struct hreg_map *map,*oldest_free = NULL;
337
338 for(map=cpu->hreg_lru;map;map=map->prev) {
339 if ((map->vreg == -1) && (map->flags == 0))
340 return map;
341
342 if ((map->flags == 0) && !oldest_free)
343 oldest_free = map;
344 }
345
346 if (!oldest_free) {
347 fprintf(stderr,
348 "ppc32_get_free_hreg: unable to find free reg for insn %s\n",
349 cpu->jit_hreg_seq_name);
350 }
351
352 return oldest_free;
353 }
354
355 /* Allocate an host register */
356 int ppc32_jit_alloc_hreg(cpu_ppc_t *cpu,int ppc_reg)
357 {
358 struct hreg_map *map;
359 int hreg;
360
361 /*
362 * If PPC reg is invalid, the caller requested for a temporary register.
363 */
364 if (ppc_reg == -1) {
365 if ((map = ppc32_jit_get_free_hreg(cpu)) == NULL)
366 return(-1);
367
368 /* Allocate the register and invalidate its PPC mapping if present */
369 map->flags = HREG_FLAG_ALLOC_LOCKED;
370
371 if (map->vreg != -1) {
372 cpu->ppc_reg_map[map->vreg] = -1;
373 map->vreg = -1;
374 }
375
376 return(map->hreg);
377 }
378
379 hreg = cpu->ppc_reg_map[ppc_reg];
380
381 /*
382 * If the PPC register is already mapped to an host register, re-use this
383 * mapping and put this as MRU mapping.
384 */
385 if (hreg != -1) {
386 map = &cpu->hreg_map[hreg];
387 } else {
388 /*
389 * This PPC register has no mapping to host register. Find a free
390 * register.
391 */
392 if ((map = ppc32_jit_get_free_hreg(cpu)) == NULL)
393 return(-1);
394
395 /* Remove the old PPC mapping if present */
396 if (map->vreg != -1)
397 cpu->ppc_reg_map[map->vreg] = -1;
398
399 /* Establish the new mapping */
400 cpu->ppc_reg_map[ppc_reg] = map->hreg;
401 map->vreg = ppc_reg;
402 }
403
404 /* Prevent this register from further allocation in this instruction */
405 map->flags = HREG_FLAG_ALLOC_LOCKED;
406 ppc32_jit_extract_hreg(cpu,map);
407 ppc32_jit_insert_hreg_mru(cpu,map);
408 return(map->hreg);
409 }
410
411 /* Force allocation of an host register */
412 int ppc32_jit_alloc_hreg_forced(cpu_ppc_t *cpu,int hreg)
413 {
414 int ppc_reg;
415
416 ppc_reg = cpu->hreg_map[hreg].vreg;
417
418 /* Check that this register is not already allocated */
419 if (cpu->hreg_map[hreg].flags != 0) {
420 fprintf(stderr,"ppc32_alloc_hreg_forced: trying to force allocation "
421 "of hreg %d (insn %s)\n",
422 hreg,cpu->jit_hreg_seq_name);
423 return(-1);
424 }
425
426 cpu->hreg_map[hreg].flags = HREG_FLAG_ALLOC_FORCED;
427 cpu->hreg_map[hreg].vreg = -1;
428
429 if (ppc_reg != -1)
430 cpu->ppc_reg_map[ppc_reg] = -1;
431
432 return(0);
433 }
434
435 /* Emit a breakpoint if necessary */
436 #if BREAKPOINT_ENABLE
437 static void insn_emit_breakpoint(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
438 {
439 m_uint32_t ia;
440 int i;
441
442 ia = b->start_ia+((b->ppc_trans_pos-1)<<2);
443
444 for(i=0;i<PPC32_MAX_BREAKPOINTS;i++)
445 if (ia == cpu->breakpoints[i]) {
446 ppc32_emit_breakpoint(cpu,b);
447 break;
448 }
449 }
450 #endif /* BREAKPOINT_ENABLE */
451
452 /* Fetch a PowerPC instruction and emit corresponding translated code */
453 struct ppc32_insn_tag *ppc32_jit_fetch_and_emit(cpu_ppc_t *cpu,
454 ppc32_jit_tcb_t *block)
455 {
456 struct ppc32_insn_tag *tag;
457 ppc_insn_t code;
458
459 code = insn_fetch(block);
460 tag = insn_tag_find(code);
461 assert(tag);
462
463 tag->emit(cpu,block,code);
464 return tag;
465 }
466
467 /* Add end of JIT block */
468 _Unused static void ppc32_jit_tcb_add_end(ppc32_jit_tcb_t *b)
469 {
470 ppc32_set_ia(&b->jit_ptr,b->start_ia+(b->ppc_trans_pos<<2));
471 ppc32_jit_tcb_push_epilog(&b->jit_ptr);
472 }
473
474 /* Record a patch to apply in a compiled block */
475 int ppc32_jit_tcb_record_patch(ppc32_jit_tcb_t *block,jit_op_t *iop,
476 u_char *jit_ptr,m_uint32_t vaddr)
477 {
478 struct ppc32_jit_patch_table *ipt = block->patch_table;
479 struct ppc32_insn_patch *patch;
480
481 /* pc must be 32-bit aligned */
482 if (vaddr & 0x03) {
483 fprintf(stderr,
484 "Block 0x%8.8x: trying to record an invalid IA (0x%8.8x)\n",
485 block->start_ia,vaddr);
486 return(-1);
487 }
488
489 if (!ipt || (ipt->cur_patch >= PPC32_INSN_PATCH_TABLE_SIZE))
490 {
491 /* full table or no table, create a new one */
492 ipt = malloc(sizeof(*ipt));
493 if (!ipt) {
494 fprintf(stderr,"Block 0x%8.8x: unable to create patch table.\n",
495 block->start_ia);
496 return(-1);
497 }
498
499 memset(ipt,0,sizeof(*ipt));
500 ipt->next = block->patch_table;
501 block->patch_table = ipt;
502 }
503
504 #if DEBUG_BLOCK_PATCH
505 printf("Block 0x%8.8x: recording patch [JIT:%p->ppc:0x%8.8x], "
506 "MTP=%d\n",block->start_ia,jit_ptr,vaddr,block->ppc_trans_pos);
507 #endif
508
509 patch = &ipt->patches[ipt->cur_patch];
510 patch->jit_insn = jit_ptr;
511 patch->ppc_ia = vaddr;
512 ipt->cur_patch++;
513
514 patch->next = iop->arg_ptr;
515 iop->arg_ptr = patch;
516 return(0);
517 }
518
519 /* Apply patches for a JIT instruction block */
520 static int ppc32_jit_tcb_apply_patches(cpu_ppc_t *cpu,
521 ppc32_jit_tcb_t *block,
522 jit_op_t *iop)
523 {
524 struct ppc32_insn_patch *patch;
525 u_char *jit_ptr,*jit_dst;
526 u_int pos;
527
528 for(patch=iop->arg_ptr;patch;patch=patch->next) {
529 jit_ptr = (patch->jit_insn - iop->ob_data) + iop->ob_final;
530
531 pos = (patch->ppc_ia & PPC32_MIN_PAGE_IMASK) >> 2;
532 jit_dst = block->jit_insn_ptr[pos];
533
534 if (jit_dst) {
535 #if DEBUG_BLOCK_PATCH
536 printf("Block 0x%8.8x: applying patch "
537 "[JIT:%p->ppc:0x%8.8x=JIT:%p, ]\n",
538 block->start_ia,patch->jit_insn,patch->ppc_ia,jit_dst);
539 #endif
540 ppc32_jit_tcb_set_patch(jit_ptr,jit_dst);
541 } else {
542 printf("Block 0x%8.8x: null dst for patch!\n",block->start_ia);
543 }
544 }
545
546 return(0);
547 }
548
549 /* Free the patch table */
550 static void ppc32_jit_tcb_free_patches(ppc32_jit_tcb_t *block)
551 {
552 struct ppc32_jit_patch_table *p,*next;
553
554 for(p=block->patch_table;p;p=next) {
555 next = p->next;
556 free(p);
557 }
558
559 block->patch_table = NULL;
560 }
561
562 /* Adjust the JIT buffer if its size is not sufficient */
563 static int ppc32_jit_tcb_adjust_buffer(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block)
564 {
565 insn_exec_page_t *new_buffer;
566
567 if ((block->jit_ptr - block->jit_buffer->ptr) <= (PPC_JIT_BUFSIZE - 512))
568 return(0);
569
570 #if DEBUG_BLOCK_CHUNK
571 printf("Block 0x%8.8x: adjusting JIT buffer...\n",block->start_ia);
572 #endif
573
574 if (block->jit_chunk_pos >= PPC_JIT_MAX_CHUNKS) {
575 fprintf(stderr,"Block 0x%8.8x: too many JIT chunks.\n",block->start_ia);
576 return(-1);
577 }
578
579 if (!(new_buffer = exec_page_alloc(cpu)))
580 return(-1);
581
582 /* record the new exec page */
583 block->jit_chunks[block->jit_chunk_pos++] = block->jit_buffer;
584 block->jit_buffer = new_buffer;
585
586 /* jump to the new exec page (link) */
587 ppc32_jit_tcb_set_jump(block->jit_ptr,new_buffer->ptr);
588 block->jit_ptr = new_buffer->ptr;
589 return(0);
590 }
591
592 /* Allocate an instruction block */
593 static inline ppc32_jit_tcb_t *ppc32_jit_tcb_alloc(cpu_ppc_t *cpu)
594 {
595 ppc32_jit_tcb_t *p;
596
597 if (cpu->tcb_free_list) {
598 p = cpu->tcb_free_list;
599 cpu->tcb_free_list = p->next;
600 } else {
601 if (!(p = malloc(sizeof(*p))))
602 return NULL;
603 }
604
605 memset(p,0,sizeof(*p));
606 return p;
607 }
608
609 /* Free the code chunks */
610 static void
611 ppc32_jit_tcb_free_code_chunks(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block)
612 {
613 int i;
614
615 /* Free code pages */
616 for(i=0;i<PPC_JIT_MAX_CHUNKS;i++) {
617 exec_page_free(cpu,block->jit_chunks[i]);
618 block->jit_chunks[i] = NULL;
619 }
620
621 /* Free the current JIT buffer */
622 exec_page_free(cpu,block->jit_buffer);
623 block->jit_buffer = NULL;
624 }
625
626 /* Free an instruction block */
627 void ppc32_jit_tcb_free(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block,
628 int list_removal)
629 {
630 if (block) {
631 if (list_removal) {
632 /* Remove the block from the linked list */
633 if (block->next)
634 block->next->prev = block->prev;
635 else
636 cpu->tcb_last = block->prev;
637
638 if (block->prev)
639 block->prev->next = block->next;
640 else
641 cpu->tcb_list = block->next;
642
643 /* Remove the block from the physical mapping hash table */
644 if (block->phys_pprev) {
645 if (block->phys_next)
646 block->phys_next->phys_pprev = block->phys_pprev;
647
648 *(block->phys_pprev) = block->phys_next;
649
650 block->phys_pprev = NULL;
651 block->phys_next = NULL;
652 }
653 }
654
655 /* Free the patch tables */
656 ppc32_jit_tcb_free_patches(block);
657
658 /* Free code pages */
659 ppc32_jit_tcb_free_code_chunks(cpu,block);
660
661 /* Free the PowerPC-to-native code mapping */
662 free(block->jit_insn_ptr);
663
664 block->next = cpu->tcb_free_list;
665 cpu->tcb_free_list = block;
666 }
667 }
668
669 /* Create an instruction block */
670 static ppc32_jit_tcb_t *ppc32_jit_tcb_create(cpu_ppc_t *cpu,m_uint32_t vaddr)
671 {
672 ppc32_jit_tcb_t *block = NULL;
673 m_uint32_t phys_page;
674
675 if (unlikely(cpu->translate(cpu,cpu->ia,PPC32_MTS_ICACHE,&phys_page)))
676 return NULL;
677
678 if (!(block = ppc32_jit_tcb_alloc(cpu)))
679 goto err_block_alloc;
680
681 block->start_ia = vaddr;
682 block->phys_page = phys_page;
683 block->phys_hash = ppc32_jit_get_phys_hash(phys_page);
684
685 /* Allocate the first JIT buffer */
686 if (!(block->jit_buffer = exec_page_alloc(cpu)))
687 goto err_jit_alloc;
688
689 block->jit_ptr = block->jit_buffer->ptr;
690 block->ppc_code = cpu->mem_op_lookup(cpu,block->start_ia,PPC32_MTS_ICACHE);
691
692 if (!block->ppc_code) {
693 fprintf(stderr,"%% No memory map for code execution at 0x%8.8x\n",
694 block->start_ia);
695 goto err_lookup;
696 }
697
698 #if DEBUG_BLOCK_TIMESTAMP
699 block->tm_first_use = block->tm_last_use = jit_jiffies;
700 #endif
701 return block;
702
703 err_lookup:
704 err_jit_alloc:
705 ppc32_jit_tcb_free(cpu,block,FALSE);
706 err_block_alloc:
707 fprintf(stderr,"%% Unable to create instruction block for vaddr=0x%8.8x\n",
708 vaddr);
709 return NULL;
710 }
711
712 /* ======================================================================== */
713
714 /* Dump a JIT opcode */
715 static void ppc32_op_dump_opcode(jit_op_t *op)
716 {
717 switch(op->opcode) {
718 case JIT_OP_BRANCH_TARGET:
719 printf("branch_target");
720 break;
721 case JIT_OP_BRANCH_JUMP:
722 printf("branch_jump");
723 break;
724 case JIT_OP_EOB:
725 printf("eob");
726 break;
727 case JIT_OP_LOAD_GPR:
728 printf("load_gpr(%d,$%d,r:%d)",
729 op->param[0],op->param[1],op->param[2]);
730 break;
731 case JIT_OP_STORE_GPR:
732 printf("store_gpr(%d,$%d,r:%d)",
733 op->param[0],op->param[1],op->param[2]);
734 break;
735 case JIT_OP_ALTER_HOST_REG:
736 printf("alter_host_reg(%d)",op->param[0]);
737 break;
738 case JIT_OP_UPDATE_FLAGS:
739 printf("update_flags(%d,%s)",
740 op->param[0],(op->param[1] ? "signed" : "unsigned"));
741 break;
742 case JIT_OP_REQUIRE_FLAGS:
743 printf("require_flags(%d)",op->param[0]);
744 break;
745 case JIT_OP_TRASH_FLAGS:
746 printf("trash_flags(%d)",op->param[0]);
747 break;
748 case JIT_OP_INSN_OUTPUT:
749 printf("insn_out(\"%s\")",op->insn_name);
750 break;
751 case JIT_OP_SET_HOST_REG_IMM32:
752 printf("set_host_reg_imm32(%d,0x%8.8x)",op->param[0],op->param[1]);
753 break;
754 default:
755 printf("op(%u)",op->opcode);
756 }
757 }
758
759 /* Dump JIT operations (debugging) */
760 _Unused static void ppc32_op_dump(cpu_gen_t *cpu,ppc32_jit_tcb_t *b)
761 {
762 m_uint32_t ia = b->start_ia;
763 jit_op_t *op;
764 int i;
765
766 printf("PPC32-JIT: dump of page 0x%8.8x\n",ia);
767
768 for(i=0;i<PPC32_INSN_PER_PAGE;i++,ia+=sizeof(ppc_insn_t)) {
769 printf(" 0x%8.8x: ", ia);
770
771 for(op=cpu->jit_op_array[i];op;op=op->next) {
772 ppc32_op_dump_opcode(op);
773 printf(" ");
774 }
775
776 printf("\n");
777 }
778
779 printf("\n");
780 }
781
782 /* PPC register mapping */
783 typedef struct {
784 int host_reg;
785 jit_op_t *last_store;
786 m_uint32_t last_store_ia;
787 }ppc_reg_map_t;
788
789 /* Clear register mapping (with PPC register) */
790 static void ppc32_clear_ppc_reg_map(ppc_reg_map_t *ppc_map,int *host_map,
791 int reg)
792 {
793 int i,hreg;
794
795 if (reg == JIT_OP_ALL_REGS) {
796 for(i=0;i<PPC32_GPR_NR;i++) {
797 ppc_map[i].host_reg = JIT_OP_INV_REG;
798 ppc_map[i].last_store = NULL;
799 }
800
801 for(i=0;i<JIT_HOST_NREG;i++)
802 host_map[i] = JIT_OP_INV_REG;
803 } else {
804 hreg = ppc_map[reg].host_reg;
805
806 if (hreg != JIT_OP_INV_REG)
807 host_map[hreg] = JIT_OP_INV_REG;
808
809 ppc_map[reg].host_reg = JIT_OP_INV_REG;
810 ppc_map[reg].last_store = NULL;
811 }
812 }
813
814 /* Clear register mapping (with host register) */
815 static void ppc32_clear_host_reg_map(ppc_reg_map_t *ppc_map,int *host_map,
816 int reg)
817 {
818 int ppc_reg;
819
820 if (host_map[reg] != JIT_OP_INV_REG) {
821 ppc_reg = host_map[reg];
822
823 ppc_map[ppc_reg].host_reg = JIT_OP_INV_REG;
824 ppc_map[ppc_reg].last_store = NULL;
825 host_map[reg] = JIT_OP_INV_REG;
826 }
827 }
828
829 /* Dump register mapping */
830 static void ppc32_dump_reg_map(ppc_reg_map_t *map_array,int *host_map)
831 {
832 int i;
833
834 printf("PPC32-JIT: current register mapping:\n");
835
836 for(i=0;i<PPC32_GPR_NR;i++)
837 printf(" ppc reg %2.2d: %d\n",i,map_array[i].host_reg);
838
839 printf("\n");
840
841 for(i=0;i<JIT_HOST_NREG;i++)
842 printf(" hreg %d: %d\n",i,host_map[i]);
843
844 printf("\n");
845 }
846
847 /* Check register mapping consistency */
848 _maybe_used static int ppc32_check_reg_map(ppc_reg_map_t *map_array,int *host_map)
849 {
850 ppc_reg_map_t *map;
851 int i;
852
853 for(i=0;i<PPC32_GPR_NR;i++) {
854 map = &map_array[i];
855
856 if ((map->host_reg != JIT_OP_INV_REG) && (host_map[map->host_reg] != i))
857 goto error;
858 }
859
860 for(i=0;i<JIT_HOST_NREG;i++) {
861 if ((host_map[i] != JIT_OP_INV_REG) &&
862 (map_array[host_map[i]].host_reg != i))
863 goto error;
864 }
865
866 return(0);
867
868 error:
869 printf("PPC32_JIT: inconsistency in register mapping.\n");
870 ppc32_dump_reg_map(map_array,host_map);
871 exit(1);
872 }
873
874 /* Optimize JIT operations */
875 static void ppc32_op_optimize(cpu_gen_t *cpu,ppc32_jit_tcb_t *b)
876 {
877 ppc_reg_map_t ppc_map[PPC32_GPR_NR],*map;
878 int reg,host_map[JIT_HOST_NREG];
879 jit_op_t *op,*opx,*last_cr_update[8];
880 m_uint32_t cur_ia;
881 int i,j;
882
883 ppc32_clear_ppc_reg_map(ppc_map,host_map,JIT_OP_ALL_REGS);
884
885 for(i=0;i<8;i++)
886 last_cr_update[i] = NULL;
887
888 for(i=0;i<PPC32_INSN_PER_PAGE;i++) {
889 for(op=cpu->jit_op_array[i];op;op=op->next)
890 {
891 //ppc32_check_reg_map(ppc_map,host_map);
892 cur_ia = b->start_ia + (i << 2);
893
894 switch(op->opcode) {
895 /* Clear mapping if end of block or branch target */
896 case JIT_OP_BRANCH_TARGET:
897 case JIT_OP_EOB:
898 ppc32_clear_ppc_reg_map(ppc_map,host_map,JIT_OP_ALL_REGS);
899
900 for(j=0;j<8;j++)
901 last_cr_update[j] = NULL;
902 break;
903
904 /* Branch jump: clear "store" operation status */
905 case JIT_OP_BRANCH_JUMP:
906 for(j=0;j<PPC32_GPR_NR;j++)
907 ppc_map[j].last_store = NULL;
908
909 for(j=0;j<8;j++)
910 last_cr_update[j] = NULL;
911 break;
912
913 /* Alteration of a specific host register */
914 case JIT_OP_ALTER_HOST_REG:
915 reg = op->param[0];
916
917 if (reg != JIT_OP_ALL_REGS) {
918 if (host_map[reg] != JIT_OP_INV_REG)
919 ppc32_clear_ppc_reg_map(ppc_map,host_map,host_map[reg]);
920 } else {
921 ppc32_clear_ppc_reg_map(ppc_map,host_map,JIT_OP_ALL_REGS);
922 }
923 break;
924
925 /* Save reg mapping and last operation */
926 case JIT_OP_STORE_GPR:
927 reg = op->param[0];
928 map = &ppc_map[op->param[1]];
929
930 /* clear old mapping */
931 if (reg != map->host_reg) {
932 ppc32_clear_host_reg_map(ppc_map,host_map,reg);
933 ppc32_clear_ppc_reg_map(ppc_map,host_map,op->param[1]);
934 }
935
936 /* cancel previous store op for this PPC register */
937 if (map->last_store) {
938 map->last_store->param[0] = JIT_OP_INV_REG;
939 map->last_store = NULL;
940 }
941
942 map->host_reg = reg;
943 map->last_store = op;
944 map->last_store_ia = cur_ia;
945 host_map[reg] = op->param[1];
946 break;
947
948 /* Load reg: check if can avoid it */
949 case JIT_OP_LOAD_GPR:
950 reg = op->param[0];
951 map = &ppc_map[op->param[1]];
952
953 if (reg == map->host_reg) {
954 /* Cancel this load */
955 op->param[0] = JIT_OP_INV_REG;
956 } else {
957 /* clear old mapping */
958 ppc32_clear_host_reg_map(ppc_map,host_map,reg);
959 ppc32_clear_ppc_reg_map(ppc_map,host_map,op->param[1]);
960
961 /* Save this reg mapping */
962 map->host_reg = op->param[0];
963 map->last_store = NULL;
964 host_map[op->param[0]] = op->param[1];
965 }
966 break;
967
968 /* Trash flags */
969 case JIT_OP_TRASH_FLAGS:
970 for(j=0;j<8;j++)
971 last_cr_update[j] = NULL;
972 break;
973
974 /* Flags required */
975 case JIT_OP_REQUIRE_FLAGS:
976 if (op->param[0] != JIT_OP_PPC_ALL_FLAGS) {
977 last_cr_update[op->param[0]] = NULL;
978 } else {
979 for(j=0;j<8;j++)
980 last_cr_update[j] = NULL;
981 }
982 break;
983
984 /* Update flags */
985 case JIT_OP_UPDATE_FLAGS:
986 opx = last_cr_update[op->param[0]];
987
988 if (opx != NULL)
989 opx->param[0] = JIT_OP_INV_REG;
990
991 last_cr_update[op->param[0]] = op;
992 break;
993 }
994 }
995 }
996 }
997
998 /* Generate the JIT code for the specified JIT op list */
999 static void ppc32_op_gen_list(ppc32_jit_tcb_t *b,int ipos,jit_op_t *op_list,
1000 u_char *jit_start)
1001 {
1002 jit_op_t *op;
1003
1004 for(op=op_list;op;op=op->next) {
1005 switch(op->opcode) {
1006 case JIT_OP_INSN_OUTPUT:
1007 ppc32_op_insn_output(b,op);
1008 break;
1009 case JIT_OP_LOAD_GPR:
1010 ppc32_op_load_gpr(b,op);
1011 break;
1012 case JIT_OP_STORE_GPR:
1013 ppc32_op_store_gpr(b,op);
1014 break;
1015 case JIT_OP_UPDATE_FLAGS:
1016 ppc32_op_update_flags(b,op);
1017 break;
1018 case JIT_OP_BRANCH_TARGET:
1019 b->jit_insn_ptr[ipos] = jit_start;
1020 break;
1021 case JIT_OP_MOVE_HOST_REG:
1022 ppc32_op_move_host_reg(b,op);
1023 break;
1024 case JIT_OP_SET_HOST_REG_IMM32:
1025 ppc32_op_set_host_reg_imm32(b,op);
1026 break;
1027 }
1028 }
1029 }
1030
1031 /* Opcode emit start */
1032 static inline void ppc32_op_emit_start(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
1033 {
1034 cpu_gen_t *c = cpu->gen;
1035 jit_op_t *op;
1036
1037 if (c->jit_op_array[b->ppc_trans_pos] == NULL)
1038 c->jit_op_current = &c->jit_op_array[b->ppc_trans_pos];
1039 else {
1040 for(op=c->jit_op_array[b->ppc_trans_pos];op;op=op->next)
1041 c->jit_op_current = &op->next;
1042 }
1043 }
1044
1045 /* Generate the JIT code for the current page, given an op list */
1046 static int ppc32_op_gen_page(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
1047 {
1048 struct ppc32_insn_tag *tag;
1049 cpu_gen_t *gcpu = cpu->gen;
1050 jit_op_t *iop;
1051 m_uint32_t cur_ia;
1052 u_char *jit_ptr;
1053 int i;
1054
1055 /* Generate JIT opcodes */
1056 for(b->ppc_trans_pos=0;
1057 b->ppc_trans_pos<PPC32_INSN_PER_PAGE;
1058 b->ppc_trans_pos++)
1059 {
1060 ppc32_op_emit_start(cpu,b);
1061
1062 cur_ia = b->start_ia + (b->ppc_trans_pos << 2);
1063
1064 if (ppc32_jit_tcb_get_target_bit(b,cur_ia))
1065 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_TARGET);
1066
1067 #if DEBUG_INSN_PERF_CNT
1068 ppc32_inc_perf_counter(cpu);
1069 #endif
1070 #if BREAKPOINT_ENABLE
1071 if (cpu->breakpoints_enabled)
1072 insn_emit_breakpoint(cpu,b);
1073 #endif
1074
1075 if (unlikely(!(tag = ppc32_jit_fetch_and_emit(cpu,b)))) {
1076 fprintf(stderr,"ppc32_op_gen_page: unable to fetch instruction.\n");
1077 return(-1);
1078 }
1079 }
1080
1081 /*
1082 * Mark the first instruction as a potential target, as well as the
1083 * current IA value.
1084 */
1085 ppc32_op_emit_branch_target(cpu,b,b->start_ia);
1086 ppc32_op_emit_branch_target(cpu,b,cpu->ia);
1087
1088 /* Optimize condition register and general registers */
1089 ppc32_op_optimize(gcpu,b);
1090
1091 /* Generate JIT code for each instruction in page */
1092 for(i=0;i<PPC32_INSN_PER_PAGE;i++)
1093 {
1094 jit_ptr = b->jit_ptr;
1095
1096 /* Generate output code */
1097 ppc32_op_gen_list(b,i,gcpu->jit_op_array[i],jit_ptr);
1098
1099 /* Adjust the JIT buffer if its size is not sufficient */
1100 ppc32_jit_tcb_adjust_buffer(cpu,b);
1101 }
1102
1103 /* Apply patches and free opcodes */
1104 for(i=0;i<PPC32_INSN_PER_PAGE;i++) {
1105 for(iop=gcpu->jit_op_array[i];iop;iop=iop->next)
1106 if (iop->opcode == JIT_OP_INSN_OUTPUT)
1107 ppc32_jit_tcb_apply_patches(cpu,b,iop);
1108
1109 jit_op_free_list(gcpu,gcpu->jit_op_array[i]);
1110 gcpu->jit_op_array[i] = NULL;
1111 }
1112
1113 /* Add end of page (returns to caller) */
1114 ppc32_set_page_jump(cpu,b);
1115
1116 /* Free patch tables */
1117 ppc32_jit_tcb_free_patches(b);
1118 return(0);
1119 }
1120
1121 /* ======================================================================== */
1122
1123 /* Compile a PowerPC instruction page */
1124 static inline
1125 ppc32_jit_tcb_t *ppc32_jit_tcb_compile(cpu_ppc_t *cpu,m_uint32_t vaddr)
1126 {
1127 ppc32_jit_tcb_t *block;
1128 m_uint32_t page_addr;
1129
1130 page_addr = vaddr & ~PPC32_MIN_PAGE_IMASK;
1131
1132 if (unlikely(!(block = ppc32_jit_tcb_create(cpu,page_addr)))) {
1133 fprintf(stderr,"insn_page_compile: unable to create JIT block.\n");
1134 return NULL;
1135 }
1136
1137 /* Allocate the array used to convert PPC code ptr to native code ptr */
1138 if (!(block->jit_insn_ptr = calloc(PPC32_INSN_PER_PAGE,sizeof(u_char *)))) {
1139 fprintf(stderr,"insn_page_compile: unable to create JIT mappings.\n");
1140 goto error;
1141 }
1142
1143 /* Compile the page */
1144 if (ppc32_op_gen_page(cpu,block) == -1) {
1145 fprintf(stderr,"insn_page_compile: unable to compile page.\n");
1146 goto error;
1147 }
1148
1149 /* Add the block to the linked list */
1150 block->next = cpu->tcb_list;
1151 block->prev = NULL;
1152
1153 if (cpu->tcb_list)
1154 cpu->tcb_list->prev = block;
1155 else
1156 cpu->tcb_last = block;
1157
1158 cpu->tcb_list = block;
1159
1160 /* Add the block to the physical mapping hash table */
1161 block->phys_next = cpu->exec_phys_map[block->phys_hash];
1162 block->phys_pprev = &cpu->exec_phys_map[block->phys_hash];
1163
1164 if (cpu->exec_phys_map[block->phys_hash] != NULL)
1165 cpu->exec_phys_map[block->phys_hash]->phys_pprev = &block->phys_next;
1166
1167 cpu->exec_phys_map[block->phys_hash] = block;
1168
1169 cpu->compiled_pages++;
1170 return block;
1171
1172 error:
1173 ppc32_jit_tcb_free(cpu,block,FALSE);
1174 return NULL;
1175 }
1176
1177 /* Recompile a page */
1178 int ppc32_jit_tcb_recompile(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block)
1179 {
1180 #if 0
1181 printf("PPC32-JIT: recompiling page 0x%8.8x\n",block->start_ia);
1182 #endif
1183
1184 /* Free old code chunks */
1185 ppc32_jit_tcb_free_code_chunks(cpu,block);
1186
1187 /* Reset code ptr array */
1188 memset(block->jit_insn_ptr,0,PPC32_INSN_PER_PAGE * sizeof(u_char *));
1189
1190 /* Allocate the first JIT buffer */
1191 if (!(block->jit_buffer = exec_page_alloc(cpu)))
1192 return(-1);
1193
1194 /* Disable flushing to avoid dangling pointers */
1195 block->flags |= PPC32_JIT_TCB_FLAG_NO_FLUSH;
1196
1197 /* Recompile the page */
1198 if (ppc32_op_gen_page(cpu,block) == -1) {
1199 fprintf(stderr,"insn_page_compile: unable to recompile page.\n");
1200 return(-1);
1201 }
1202
1203 block->flags &= ~PPC32_JIT_TCB_FLAG_NO_FLUSH;
1204 block->target_undef_cnt = 0;
1205 return(0);
1206 }
1207
1208 /* Run a compiled PowerPC instruction block */
1209 static forced_inline
1210 void ppc32_jit_tcb_run(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block)
1211 {
1212 if (unlikely(cpu->ia & 0x03)) {
1213 fprintf(stderr,"ppc32_jit_tcb_run: Invalid IA 0x%8.8x.\n",cpu->ia);
1214 ppc32_dump_regs(cpu->gen);
1215 ppc32_dump_mmu(cpu->gen);
1216 cpu_stop(cpu->gen);
1217 return;
1218 }
1219
1220 /* Execute JIT compiled code */
1221 ppc32_jit_tcb_exec(cpu,block);
1222 }
1223
1224 /* Execute compiled PowerPC code */
1225 void *ppc32_jit_run_cpu(cpu_gen_t *gen)
1226 {
1227 cpu_ppc_t *cpu = CPU_PPC32(gen);
1228 pthread_t timer_irq_thread;
1229 ppc32_jit_tcb_t *block;
1230 m_uint32_t ia_hash;
1231 int timer_irq_check = 0;
1232
1233 ppc32_jit_init_hreg_mapping(cpu);
1234
1235 if (pthread_create(&timer_irq_thread,NULL,(void *)ppc32_timer_irq_run,cpu))
1236 {
1237 fprintf(stderr,
1238 "VM '%s': unable to create Timer IRQ thread for CPU%u.\n",
1239 cpu->vm->name,gen->id);
1240 cpu_stop(cpu->gen);
1241 return NULL;
1242 }
1243
1244 gen->cpu_thread_running = TRUE;
1245 cpu_exec_loop_set(gen);
1246
1247 start_cpu:
1248 gen->idle_count = 0;
1249
1250 for(;;) {
1251 if (unlikely(gen->state != CPU_STATE_RUNNING))
1252 break;
1253
1254 #if DEBUG_BLOCK_PERF_CNT
1255 cpu->perf_counter++;
1256 #endif
1257 /* Handle virtual idle loop */
1258 if (unlikely(cpu->ia == cpu->idle_pc)) {
1259 if (++gen->idle_count == gen->idle_max) {
1260 cpu_idle_loop(gen);
1261 gen->idle_count = 0;
1262 }
1263 }
1264
1265 /* Handle the virtual CPU clock */
1266 if (++timer_irq_check == cpu->timer_irq_check_itv) {
1267 timer_irq_check = 0;
1268
1269 if (cpu->timer_irq_pending && !cpu->irq_disable &&
1270 (cpu->msr & PPC32_MSR_EE))
1271 {
1272 cpu->timer_irq_armed = 0;
1273 cpu->timer_irq_pending--;
1274
1275 vm_set_irq(cpu->vm,0);
1276 }
1277 }
1278
1279 /* Check IRQs */
1280 if (unlikely(cpu->irq_check))
1281 ppc32_trigger_irq(cpu);
1282
1283 /* Get the JIT block corresponding to IA register */
1284 ia_hash = ppc32_jit_get_ia_hash(cpu->ia);
1285 block = cpu->exec_blk_map[ia_hash];
1286
1287 /* No block found, compile the page */
1288 if (unlikely(!block) || unlikely(!ppc32_jit_tcb_match(cpu,block)))
1289 {
1290 if (block != NULL) {
1291 ppc32_jit_tcb_free(cpu,block,TRUE);
1292 cpu->exec_blk_map[ia_hash] = NULL;
1293 }
1294
1295 block = ppc32_jit_tcb_compile(cpu,cpu->ia);
1296
1297 if (unlikely(!block)) {
1298 fprintf(stderr,
1299 "VM '%s': unable to compile block for CPU%u IA=0x%8.8x\n",
1300 cpu->vm->name,gen->id,cpu->ia);
1301 cpu_stop(gen);
1302 break;
1303 }
1304
1305 cpu->exec_blk_map[ia_hash] = block;
1306 }
1307
1308 #if DEBUG_BLOCK_TIMESTAMP
1309 block->tm_last_use = jit_jiffies++;
1310 #endif
1311 block->acc_count++;
1312 ppc32_jit_tcb_run(cpu,block);
1313 }
1314
1315 if (!cpu->ia) {
1316 cpu_stop(gen);
1317 cpu_log(gen,"JIT","IA=0, halting CPU.\n");
1318 }
1319
1320 /* Check regularly if the CPU has been restarted */
1321 while(gen->cpu_thread_running) {
1322 gen->seq_state++;
1323
1324 switch(gen->state) {
1325 case CPU_STATE_RUNNING:
1326 gen->state = CPU_STATE_RUNNING;
1327 goto start_cpu;
1328
1329 case CPU_STATE_HALTED:
1330 gen->cpu_thread_running = FALSE;
1331 pthread_join(timer_irq_thread,NULL);
1332 break;
1333 }
1334
1335 /* CPU is paused */
1336 usleep(200000);
1337 }
1338
1339 return NULL;
1340 }