9 |
#define __PPC32_JIT_H__ |
#define __PPC32_JIT_H__ |
10 |
|
|
11 |
#include "utils.h" |
#include "utils.h" |
12 |
|
#include "sbox.h" |
13 |
|
|
14 |
/* Size of executable page area (in Mb) */ |
/* Size of executable page area (in Mb) */ |
15 |
#ifndef __CYGWIN__ |
#ifndef __CYGWIN__ |
24 |
/* Maximum number of X86 chunks */ |
/* Maximum number of X86 chunks */ |
25 |
#define PPC_JIT_MAX_CHUNKS 32 |
#define PPC_JIT_MAX_CHUNKS 32 |
26 |
|
|
27 |
|
/* Size of hash for IA lookup */ |
28 |
|
#define PPC_JIT_IA_HASH_BITS 17 |
29 |
|
#define PPC_JIT_IA_HASH_MASK ((1 << PPC_JIT_IA_HASH_BITS) - 1) |
30 |
|
#define PPC_JIT_IA_HASH_SIZE (1 << PPC_JIT_IA_HASH_BITS) |
31 |
|
|
32 |
|
/* Size of hash for physical lookup */ |
33 |
|
#define PPC_JIT_PHYS_HASH_BITS 16 |
34 |
|
#define PPC_JIT_PHYS_HASH_MASK ((1 << PPC_JIT_PHYS_HASH_BITS) - 1) |
35 |
|
#define PPC_JIT_PHYS_HASH_SIZE (1 << PPC_JIT_PHYS_HASH_BITS) |
36 |
|
|
37 |
/* Instruction jump patch */ |
/* Instruction jump patch */ |
38 |
struct ppc32_insn_patch { |
struct ppc32_insn_patch { |
39 |
u_char *jit_insn; |
u_char *jit_insn; |
54 |
m_uint32_t start_ia; |
m_uint32_t start_ia; |
55 |
u_char **jit_insn_ptr; |
u_char **jit_insn_ptr; |
56 |
m_uint64_t acc_count; |
m_uint64_t acc_count; |
|
m_uint32_t phys_page; |
|
57 |
ppc_insn_t *ppc_code; |
ppc_insn_t *ppc_code; |
58 |
u_int ppc_trans_pos; |
u_int ppc_trans_pos; |
59 |
u_int jit_chunk_pos; |
u_int jit_chunk_pos; |
62 |
insn_exec_page_t *jit_chunks[PPC_JIT_MAX_CHUNKS]; |
insn_exec_page_t *jit_chunks[PPC_JIT_MAX_CHUNKS]; |
63 |
struct ppc32_jit_patch_table *patch_table; |
struct ppc32_jit_patch_table *patch_table; |
64 |
ppc32_jit_tcb_t *prev,*next; |
ppc32_jit_tcb_t *prev,*next; |
65 |
|
|
66 |
|
m_uint32_t phys_page; |
67 |
|
m_uint32_t phys_hash; |
68 |
|
ppc32_jit_tcb_t **phys_pprev,*phys_next; |
69 |
|
|
70 |
#if DEBUG_BLOCK_TIMESTAMP |
#if DEBUG_BLOCK_TIMESTAMP |
71 |
m_uint64_t tm_first_use,tm_last_use; |
m_uint64_t tm_first_use,tm_last_use; |
72 |
#endif |
#endif |
84 |
{ |
{ |
85 |
m_uint32_t offset; |
m_uint32_t offset; |
86 |
|
|
87 |
offset = (vaddr - b->start_ia) >> 2; |
offset = (vaddr & PPC32_MIN_PAGE_IMASK) >> 2; |
88 |
return(b->jit_insn_ptr[offset]); |
return(b->jit_insn_ptr[offset]); |
89 |
} |
} |
90 |
|
|
91 |
|
/* Check if the specified address belongs to the specified block */ |
92 |
|
static forced_inline |
93 |
|
int ppc32_jit_tcb_local_addr(ppc32_jit_tcb_t *block,m_uint32_t vaddr, |
94 |
|
u_char **jit_addr) |
95 |
|
{ |
96 |
|
if ((vaddr & PPC32_MIN_PAGE_MASK) == block->start_ia) { |
97 |
|
*jit_addr = ppc32_jit_tcb_get_host_ptr(block,vaddr); |
98 |
|
return(1); |
99 |
|
} |
100 |
|
|
101 |
|
return(0); |
102 |
|
} |
103 |
|
|
104 |
|
/* Check if PC register matches the compiled block virtual address */ |
105 |
|
static forced_inline |
106 |
|
int ppc32_jit_tcb_match(cpu_ppc_t *cpu,ppc32_jit_tcb_t *block) |
107 |
|
{ |
108 |
|
m_uint32_t vpage; |
109 |
|
|
110 |
|
vpage = cpu->ia & ~PPC32_MIN_PAGE_IMASK; |
111 |
|
return(block->start_ia == vpage); |
112 |
|
} |
113 |
|
|
114 |
|
/* Compute the hash index for the specified IA value */ |
115 |
|
static forced_inline m_uint32_t ppc32_jit_get_ia_hash(m_uint32_t ia) |
116 |
|
{ |
117 |
|
m_uint32_t page_hash; |
118 |
|
|
119 |
|
page_hash = sbox_u32(ia >> PPC32_MIN_PAGE_SHIFT); |
120 |
|
return((page_hash ^ (page_hash >> 14)) & PPC_JIT_IA_HASH_MASK); |
121 |
|
} |
122 |
|
|
123 |
|
/* Compute the hash index for the specified physical page */ |
124 |
|
static forced_inline m_uint32_t ppc32_jit_get_phys_hash(m_uint32_t phys_page) |
125 |
|
{ |
126 |
|
m_uint32_t page_hash; |
127 |
|
|
128 |
|
page_hash = sbox_u32(phys_page); |
129 |
|
return((page_hash ^ (page_hash >> 12)) & PPC_JIT_PHYS_HASH_MASK); |
130 |
|
} |
131 |
|
|
132 |
|
/* Find the JIT block matching a physical page */ |
133 |
|
static inline ppc32_jit_tcb_t * |
134 |
|
ppc32_jit_find_by_phys_page(cpu_ppc_t *cpu,m_uint32_t phys_page) |
135 |
|
{ |
136 |
|
m_uint32_t page_hash = ppc32_jit_get_phys_hash(phys_page); |
137 |
|
ppc32_jit_tcb_t *block; |
138 |
|
|
139 |
|
for(block=cpu->exec_phys_map[page_hash];block;block=block->phys_next) |
140 |
|
if (block->phys_page == phys_page) |
141 |
|
return block; |
142 |
|
|
143 |
|
return NULL; |
144 |
|
} |
145 |
|
|
146 |
|
/* Virtual Breakpoint */ |
147 |
|
void ppc32_emit_breakpoint(ppc32_jit_tcb_t *b); |
148 |
|
|
149 |
/* Initialize instruction lookup table */ |
/* Initialize instruction lookup table */ |
150 |
void ppc32_jit_create_ilt(void); |
void ppc32_jit_create_ilt(void); |
151 |
|
|