Skip to content

Commit

Permalink
Offsets pc_section to get to text_section instead of adding a 64 bit …
Browse files Browse the repository at this point in the history
…absolute address.
  • Loading branch information
Lichtso committed Dec 31, 2024
1 parent 3adf13d commit e50c2ce
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,14 +1565,10 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x2b, REGISTER_INSTRUCTION_METER, RSP, Some(X86IndirectAccess::OffsetIndexShift(-8, RSP, 0)))); // instruction_meter -= guest_current_pc;
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S64, 0x81, 5, REGISTER_INSTRUCTION_METER, 1, None)); // instruction_meter -= 1;
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x01, REGISTER_SCRATCH, REGISTER_INSTRUCTION_METER, None)); // instruction_meter += guest_target_pc;
// Load host target_address offset from self.result.pc_section
// Load host target_address: self.result.text_section.add(self.result.pc_section[guest_target_pc])
self.emit_ins(X86Instruction::load_immediate(REGISTER_MAP[0], self.result.pc_section.as_ptr() as i64)); // host_target_address = self.result.pc_section;
self.emit_ins(X86Instruction::load(OperandSize::S64, REGISTER_MAP[0], REGISTER_MAP[0], X86IndirectAccess::OffsetIndexShift(0, REGISTER_SCRATCH, shift_amount as u8))); // host_target_address = self.result.pc_section[guest_target_pc];
// Offset host target_address by self.result.text_section
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S32, 0x81, 0, REGISTER_MAP[0], self.result.text_section.as_ptr() as u32 as i64, None)); // wrapping_add(self.result.text_section as u32)
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S64, 0xc1, 1, REGISTER_MAP[0], 32, None)); // rotate_right(32)
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S64, 0x81, 0, REGISTER_MAP[0], (self.result.text_section.as_ptr() as u64 >> 32) as u32 as i64, None)); // wrapping_add(self.result.text_section >> 32)
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S64, 0xc1, 1, REGISTER_MAP[0], 32, None)); // rotate_right(32)
self.emit_ins(X86Instruction::alu(OperandSize::S64, 0x03, REGISTER_MAP[0], REGISTER_MAP[0], Some(X86IndirectAccess::OffsetIndexShift(0, REGISTER_SCRATCH, shift_amount as u8)))); // host_target_address = self.result.pc_section + self.result.pc_section[guest_target_pc];
self.emit_ins(X86Instruction::alu_immediate(OperandSize::S64, 0x81, 0, REGISTER_MAP[0], unsafe { self.result.text_section.as_ptr().offset_from(self.result.pc_section.as_ptr() as *const u8) } as i64, None)); // host_target_address += self.result.text_section - self.result.pc_section;
// Restore the clobbered REGISTER_MAP[0]
self.emit_ins(X86Instruction::xchg(OperandSize::S64, REGISTER_MAP[0], RSP, Some(X86IndirectAccess::OffsetIndexShift(0, RSP, 0)))); // Swap REGISTER_MAP[0] and host_target_address
self.emit_ins(X86Instruction::return_near()); // Tail call to host_target_address
Expand Down

0 comments on commit e50c2ce

Please sign in to comment.