Skip to content

Commit

Permalink
Added jump table mask macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Dec 27, 2024
1 parent de0dd9c commit 775a22c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib86cpu/core/emitter/x64/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ void lc86_jit::gen_tc_linking_jmp(T target_pc)
Label ret = m_a.newLabel();
LEA(R8, MEMD64(RCX, CPU_CTX_JMP_TABLE));
MOV(EDX, EBX);
AND(EBX, (JMP_TABLE_NUM_ELEMENTS - 1)); // hash_idx = target virt_pc & 4095
AND(EBX, JMP_TABLE_MASK); // hash_idx = target virt_pc & JMP_TABLE_MASK
LEA(RBX, MEMS64(RBX, RBX, 2));
SHL(RBX, 2); // hash_idx * JMP_TABLE_NUM_ELEMENTS; element offset at hash_idx
CMP(MEMS32(R8, RBX, 0), EDX); // if zero, virt_pc matches
Expand Down
6 changes: 3 additions & 3 deletions lib86cpu/core/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ tc_unlink(cpu_t *cpu, addr_t virt_pc)
if (it_map->second.empty()) {
cpu->jmp_page_map.erase(it_map);
}
uint32_t idx = virt_pc & (JMP_TABLE_NUM_ELEMENTS - 1);
uint32_t idx = virt_pc & JMP_TABLE_MASK;
jmp_table_elem *jmp_elem_off = (jmp_table_elem *)&cpu->cpu_ctx.jmp_table[idx * JMP_TABLE_ELEMENT_SIZE];
jmp_elem_off->guest_flags = HFLG_INVALID;
}
Expand All @@ -559,7 +559,7 @@ tc_unlink_page(cpu_t *cpu, addr_t virt_pc)
{
if (auto it_map = cpu->jmp_page_map.find(virt_pc >> PAGE_SHIFT); it_map != cpu->jmp_page_map.end()) {
for (auto addr : it_map->second) {
uint32_t idx = addr & (JMP_TABLE_NUM_ELEMENTS - 1);
uint32_t idx = addr & JMP_TABLE_MASK;
jmp_table_elem *jmp_elem_off = (jmp_table_elem *)&cpu->cpu_ctx.jmp_table[idx * JMP_TABLE_ELEMENT_SIZE];
jmp_elem_off->guest_flags = HFLG_INVALID;
}
Expand Down Expand Up @@ -719,7 +719,7 @@ tc_cache_purge(cpu_t *cpu)
static void
tc_link_jmp(cpu_t *cpu, translated_code_t *ptr_tc)
{
uint32_t idx = ptr_tc->virt_pc & (JMP_TABLE_NUM_ELEMENTS - 1);
uint32_t idx = ptr_tc->virt_pc & JMP_TABLE_MASK;
jmp_table_elem *jmp_elem_off = (jmp_table_elem *)&cpu->cpu_ctx.jmp_table[idx * JMP_TABLE_ELEMENT_SIZE];

// If there is an existing entry in the table, we must flush it first before inserting the new entry
Expand Down
1 change: 1 addition & 0 deletions lib86cpu/lib86cpu_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// jmp_table: 4096 entries of 3 uint32_t + 1 pointer -> virt_pc, cs_base, hflags | eflags, tc->ptr_code
#define JMP_TABLE_NUM_ELEMENTS (1 << 12)
#define JMP_TABLE_ELEMENT_SIZE (12 + sizeof(entry_t))
#define JMP_TABLE_MASK (JMP_TABLE_NUM_ELEMENTS - 1)
#define JMP_TABLE_MAX_SIZE (JMP_TABLE_NUM_ELEMENTS * JMP_TABLE_ELEMENT_SIZE)
// itlb: 512 sets * 8 lines = 4096 entries -> offset 12 bits, index 9 bites, tag 11 bits
#define ITLB_NUM_SETS (1 << 9)
Expand Down

0 comments on commit 775a22c

Please sign in to comment.