Panama-FFI API Porting for RISCV64

何伟凯 weikai at isrc.iscas.ac.cn
Wed Oct 26 06:15:51 UTC 2022


## Summary

In recent, Panama FFI-API has been a preview feature. In many scenarios, the Panama FFI API can replace the JNI to implement native function access. The FFI API provides more secure and convenient access to native functions. The specific implementation of FFI API is related to ARCH and OS. In order to enable RISC V64 to use FFI API, porting is required. 

## Notable Things

Because there are different return value passing convention for special structures like `struct {int, float}` on the RISCV64[1]. 

When making an upcall, that's calling a Java method from a native function, return value of the Java method will be saved in a segment of stack memory called return buffer, and then riscv backend will transfer the data in memory to `a0` and `fa0`. 

The instructions used for data transfer are closely related to correctness. If the field containing in the special structure menteioned above is float, must use 'flw', otherwise, 'fld'. This requires a means to pass the width information of fields to the riscv backend.

Unfortunately, according to my understanding, the current interface does not provide a direct means to pass the width information of struct fields to the riscv backend, so the width information is encoded in other ways. Although this makes the riscv porting slightly different from other arch, it does not make the code more difficult to understand and maintain. For details, see comments in the code[2].

## Testing

All the tests in jtreg have been passed. Some tests under the user mode of the QEMU may fail, however they can pass on the development board.

## Reference

[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc

[2] https://github.com/feilongjiang/jdk/tree/riscv-foreign-api

[3] https://github.com/openjdk/jdk/pull/8842/files


More information about the riscv-port-dev mailing list