riscv-collab / riscv-openjdk

GNU General Public License v2.0
23 stars 11 forks source link

[C-Ext] Fix patchable `jal`s spanning cache lines #24

Open zhengxiaolinX opened 3 years ago

zhengxiaolinX commented 3 years ago

Hi team,

This patch includes several fixes for the C extension support. It contains three small patches to fix different problems or potential issues.

  1. As @yadongw has suggested, MacroAssembler::far_call() has a reserved place needing a fix. Though hardly reachable, for its correctness we also need to fix it.
  2. After supporting the C extension, instructions become 2-byte aligned - also for trampolines. In order to make the emit_int64((intptr_t)dest) 8-byte aligned, some code needs a revise to adjust the new change. We have checked places where unalignment-related issues could occur and found this one, and now the 64-bit addresses in trampolines are properly aligned.
  3. The third issue is a crash which is a bit hard to address, only happening by using the C extension. Please see a hs_err file attached. In short, patchable instructions' patching bits must not span cache lines. Concisely, the problem is that, after the C extension instructions get 2-byte aligned. Patchable jals are remaining their 4-byte form, but now they might stay in a 2-byte alignment. An example:
    0x0000003fe11b01fe:   jal   ra,0x0000003fe1126d80 (patchable)
    0x0000003fe11b0202:   ...

Code is running at full speed on every hart. If this instruction is patched with unaligned access, the patching operation is not atomic anymore. In this respect, concurrency issues may occur, which are hard to address as pcs could fly to any place and the generated code seems to have no error. We solve this issue by using ins_alignment(4) to force the patchable jals to stay in a 4-byte aligned address.

0x0000003fe11b0200:   jal   ra,0x0000003fe1126d80 (patchable)
0x0000003fe11b0204:   ...

Jtreg tests

com/sun/jdi/EnumTest.java
com/sun/jdi/RedefineCrossStart.java
java/util/stream/test/org/openjdk/tests/java/util/stream/SliceOpTest.java
java/util/stream/test/org/openjdk/tests/java/util/stream/mapMultiOpTest.java
java/util/ResourceBundle/Control/StressTest.java
java/nio/charset/coders/BashCache.java
java/nio/channels/AsynchronousSocketChannel/StressLoopback.java
java/net/HugeDataTransferTest.java
java/net/httpclient/LargeResponseTest.java

could reproduce this issue in small chances.

I have run these tests over nights to ensure the issue is solved. Also, other jtreg tests are passed.

Thanks, Xiaolin