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