Mimick icon indicating copy to clipboard operation
Mimick copied to clipboard

Add support for RISC-V

Open Arun-42 opened this issue 2 years ago • 2 comments

I am trying to cross-compile ROS2 to RISCV64, and Mimick is a dependency. Could you please add support for RISCV64? Or if you could provide pointers as how to do it, I could work on it.

Arun-42 avatar Nov 04 '22 06:11 Arun-42

You would need to add a new trampoline for the architecture here: https://github.com/Snaipe/Mimick/tree/master/src/asm

The trampoline must be written with position-independent instructions, as it gets copied to multiple locations. It also must not clobber any of the ABI registers and stack, as its main role is to prepare some state before long-jumping to the stub function. This is why you'll see most trampolines backing up the state of some registers before restoring said state later on.

The general memory layout of a trampoline is as follows:

--------------------
 struct mmk_stub *    context pointer of stub  (8 bytes)
--------------------
      plt_fn *        function pointer of stub (8 bytes)
--------------------
   mmk_trampoline     trampoline opcodes       (N bytes)
        ...
 mmk_trampoline_end
--------------------

You can get inspiration from other architectures, but the general gist is that the higher-level behavior of the trampoline is:

  • Load mmk_stub pointer
  • Call mmk_stub->ctx_set
  • Call mmk_stub->ctx_asked
  • If ctx_asked returned 0, this is a stub trampoline jump, perform a long jump to the stub function pointer
  • Otherwise, this is mimick trying to retrieve the trampoline context, call mmk_stub->ctx_get and return its result.

When this is done, add the CMake config to handle the architecture here: https://github.com/Snaipe/Mimick/blob/master/CMakeLists.txt#L50

Snaipe avatar Nov 04 '22 07:11 Snaipe

Thanks for Snaipe's guide, I have ported Mimick to RISCV64. https://github.com/Snaipe/Mimick/pull/34

ziyao233 avatar Mar 26 '23 04:03 ziyao233