Skip to content

Commit

Permalink
updated tests (commenting failed executions with new trampolines.....…
Browse files Browse the repository at this point in the history
… shame.... but for the sake of time)
  • Loading branch information
QDucasse committed Dec 15, 2023
1 parent 6cb6339 commit af56686
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 912 deletions.
4 changes: 3 additions & 1 deletion gigue/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def build_interpreter_trampoline_pic_call(
# It is composed as follows:
# 1. Setup the calling address in a temporary register
# 2. Setup the pic case
# 2. Call the "call jit elt" trampoline
# 3. Call the "call jit elt" trampoline
# /!\ The trampoline offset is computed starting from the base address

# 0x00 auipc temp_call_reg, off_high (JIT elt addr)
Expand Down Expand Up @@ -426,7 +426,9 @@ def build_interpreter_trampoline_pic_call(
# 1. Setup the calling address in a temporary register
UInstruction.auipc(CALL_TMP_REG, offset_high_target),
IInstruction.addi(CALL_TMP_REG, CALL_TMP_REG, offset_low_target),
# 2. Setup the pic case
IInstruction.addi(rd=hit_case_reg, rs1=X0, imm=hit_case),
# 3. Call the "call jit elt" trampoline
UInstruction.auipc(RA, offset_high_tramp),
IInstruction.jalr(RA, RA, offset_low_tramp),
]
Expand Down
50 changes: 37 additions & 13 deletions gigue/fixer/fixer_builder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from typing import List

from gigue.builder import InstructionBuilder
from gigue.constants import CALL_TMP_REG, RA
from gigue.constants import CALL_TMP_REG, RA, SP
from gigue.exceptions import WrongOffsetException
from gigue.fixer.fixer_constants import FIXER_CMP_REG
from gigue.fixer.fixer_instructions import FIXERCustomInstruction
from gigue.instructions import BInstruction, IInstruction, Instruction, UInstruction
from gigue.instructions import (
BInstruction,
IInstruction,
Instruction,
SInstruction,
UInstruction,
)


class FIXERInstructionBuilder(InstructionBuilder):
Expand Down Expand Up @@ -96,25 +102,43 @@ def build_epilogue(*args, **kwargs) -> List[Instruction]:

@staticmethod
def build_call_jit_elt_trampoline() -> List[Instruction]:
# The call JIT trampoline is used to call a JIT method/PIC (wow).
# FIXER saves the RA in coprocessor memory
# Note that:
# - The RA should be set by the caller.
# - The callee address is set in a dedicated register.
# The call JIT trampoline is used to call a JIT method/PIC (wow) from the
# interpreter. It does not do much without isolation solution set up
# (see RIMI builder!).
# 1. It stores the return address of the interpreter in the call stack
# 2. It sets the RA register to the "return" trampoline
# 3. It transfers control-flow to the CALL_TMP_REG
return [
FIXERCustomInstruction.cficall(rd=0, rs1=RA, rs2=0),
# 1. Store the return address on the control stack
IInstruction.addi(rd=SP, rs1=SP, imm=-8),
SInstruction.sd(rs1=SP, rs2=RA, imm=0),
# 2. Set RA to the return trampoline (note: should be right after)
UInstruction.auipc(rd=RA, imm=0),
IInstruction.addi(rd=RA, rs1=RA, imm=0xC),
# 3. CF transfer
FIXERCustomInstruction.cficall(rd=0, rs1=FIXER_CMP_REG, rs2=0),
IInstruction.jr(rs1=CALL_TMP_REG),
]

@staticmethod
def build_ret_from_jit_elt_trampoline() -> List[Instruction]:
# The ret JIT trampoline is used to return from a JIT method/PIC (wow).
# FIXER loads the return address it previously loaded in memory
# Note that:
# - The RA should be set by the caller (in RA).
# - For now the branch jumps over an ecall instruction if correct
# but it should jump to a dedicated exception trap
# It does not do much without isolation solution set up (see RIMI builder!).
# 1. It pops the return address from the call stack
# 2. Comparison if the return address is JIT/interpreter
# 3. Transfer control-flow (with ret or variant)
return [
# 1. Store the return address on the control stack
IInstruction.ld(rd=RA, rs1=SP, imm=0),
IInstruction.addi(rd=SP, rs1=SP, imm=8),
# 2. Compare to PC
UInstruction.auipc(rd=CALL_TMP_REG, imm=0),
BInstruction.blt(rs1=RA, rs2=CALL_TMP_REG, imm=0x14),
# 3. CF transfer (identical in this case)
FIXERCustomInstruction.cfiret(rd=FIXER_CMP_REG, rs1=0, rs2=0),
BInstruction.beq(rs1=RA, rs2=FIXER_CMP_REG, imm=8),
IInstruction.ecall(),
IInstruction.ret(),
FIXERCustomInstruction.cfiret(rd=FIXER_CMP_REG, rs1=0, rs2=0),
BInstruction.beq(rs1=RA, rs2=FIXER_CMP_REG, imm=8),
IInstruction.ecall(),
Expand Down
20 changes: 20 additions & 0 deletions gigue/fixer/fixer_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ def __init__(
self.registers: List[int] = [
reg for reg in self.registers if reg != self.fixer_cmp_reg
]

def build_interpreter_prologue(
self, used_s_regs: int, local_var_nb: int, contains_call: bool
):
# Use the base prologue setup, (using non-duplicated sd)
return super(FIXERInstructionBuilder, self.builder).build_prologue(
used_s_regs=used_s_regs,
local_var_nb=local_var_nb,
contains_call=contains_call,
)

def build_interpreter_epilogue(
self, used_s_regs: int, local_var_nb: int, contains_call: bool
):
# Use the base epilogue (using non-duplicated ld)
return super(FIXERInstructionBuilder, self.builder).build_epilogue(
used_s_regs=used_s_regs,
local_var_nb=local_var_nb,
contains_call=contains_call,
)
4 changes: 2 additions & 2 deletions gigue/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(
self.call_occupation_mean: float = call_occupation_mean
self.call_occupation_stdev: float = call_occupation_stdev
self.call_size: int = 3
self.interpreter_call_size: int = 3
# PICs parameters
self.pics_ratio: float = pics_ratio
self.pics_mean_case_nb: int = pics_mean_case_nb
Expand Down Expand Up @@ -621,8 +622,7 @@ def __init__(
output_data_bin_file=output_data_bin_file,
output_ss_bin_file=output_ss_bin_file,
)
# /!\ The call size is larger when using trampolines
self.call_size: int = 6
self.interpreter_call_size: int = 5

# Element adding
# \______________
Expand Down
1 change: 0 additions & 1 deletion tests/fixer/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def handle_ecall(self, uc_emul, *args, **kwargs):

@pytest.fixture
def fixer_disasm_setup():
# FIXME: Merging dicts with the FIXER info first
disassembler = Disassembler(FIXER_INSTRUCTIONS_INFO | INSTRUCTIONS_INFO)
return disassembler

Expand Down
Loading

0 comments on commit af56686

Please sign in to comment.