@@ -596,8 +596,15 @@ class MacroAssembler: public Assembler {
596596 return ReservedCodeCacheSize > branch_range;
597597 }
598598
599- // Jumps that can reach anywhere in the code cache.
600- // Trashes tmp.
599+ // Emit a direct call/jump if the entry address will always be in range,
600+ // otherwise a far call/jump.
601+ // The address must be inside the code cache.
602+ // Supported entry.rspec():
603+ // - relocInfo::external_word_type
604+ // - relocInfo::runtime_call_type
605+ // - relocInfo::none
606+ // In the case of a far call/jump, the entry address is put in the tmp register.
607+ // The tmp register is invalidated.
601608 void far_call (Address entry, Register tmp = t0);
602609 void far_jump (Address entry, Register tmp = t0);
603610
@@ -635,6 +642,70 @@ class MacroAssembler: public Assembler {
635642 void get_polling_page (Register dest, relocInfo::relocType rtype);
636643 address read_polling_page (Register r, int32_t offset, relocInfo::relocType rtype);
637644
645+ // RISCV64 OpenJDK uses four different types of calls:
646+ // - direct call: jal pc_relative_offset
647+ // This is the shortest and the fastest, but the offset has the range: +/-1MB.
648+ //
649+ // - far call: auipc reg, pc_relative_offset; jalr ra, reg, offset
650+ // This is longer than a direct call. The offset has
651+ // the range [-(2G + 2K), 2G - 2K). Addresses out of the range in the code cache
652+ // requires indirect call.
653+ // If a jump is needed rather than a call, a far jump 'jalr x0, reg, offset' can
654+ // be used instead.
655+ // All instructions are embedded at a call site.
656+ //
657+ // - trampoline call:
658+ // This is only available in C1/C2-generated code (nmethod). It is a combination
659+ // of a direct call, which is used if the destination of a call is in range,
660+ // and a register-indirect call. It has the advantages of reaching anywhere in
661+ // the RISCV address space and being patchable at runtime when the generated
662+ // code is being executed by other threads.
663+ //
664+ // [Main code section]
665+ // jal trampoline
666+ // [Stub code section]
667+ // trampoline:
668+ // ld reg, pc + 8 (auipc + ld)
669+ // jr reg
670+ // <64-bit destination address>
671+ //
672+ // If the destination is in range when the generated code is moved to the code
673+ // cache, 'jal trampoline' is replaced with 'jal destination' and the trampoline
674+ // is not used.
675+ // The optimization does not remove the trampoline from the stub section.
676+
677+ // This is necessary because the trampoline may well be redirected later when
678+ // code is patched, and the new destination may not be reachable by a simple JAL
679+ // instruction.
680+ //
681+ // - indirect call: movptr_with_offset + jalr
682+ // This too can reach anywhere in the address space, but it cannot be
683+ // patched while code is running, so it must only be modified at a safepoint.
684+ // This form of call is most suitable for targets at fixed addresses, which
685+ // will never be patched.
686+ //
687+ //
688+ // To patch a trampoline call when the JAL can't reach, we first modify
689+ // the 64-bit destination address in the trampoline, then modify the
690+ // JAL to point to the trampoline, then flush the instruction cache to
691+ // broadcast the change to all executing threads. See
692+ // NativeCall::set_destination_mt_safe for the details.
693+ //
694+ // There is a benign race in that the other thread might observe the
695+ // modified JAL before it observes the modified 64-bit destination
696+ // address. That does not matter because the destination method has been
697+ // invalidated, so there will be a trap at its start.
698+ // For this to work, the destination address in the trampoline is
699+ // always updated, even if we're not using the trampoline.
700+
701+ // Emit a direct call if the entry address will always be in range,
702+ // otherwise a trampoline call.
703+ // Supported entry.rspec():
704+ // - relocInfo::runtime_call_type
705+ // - relocInfo::opt_virtual_call_type
706+ // - relocInfo::static_call_type
707+ // - relocInfo::virtual_call_type
708+ //
638709 // Return: the call PC or NULL if CodeCache is full.
639710 address trampoline_call (Address entry);
640711 address ic_call (address entry, jint method_index = 0 );
0 commit comments