Skip to content

Commit

Permalink
sync JDK-8296477
Browse files Browse the repository at this point in the history
  • Loading branch information
feilongjiang committed Dec 16, 2022
1 parent b888951 commit 614882d
Show file tree
Hide file tree
Showing 13 changed files with 451 additions and 338 deletions.
138 changes: 84 additions & 54 deletions src/hotspot/cpu/riscv/downcallLinker_riscv.cpp
Expand Up @@ -41,24 +41,26 @@ class DowncallStubGenerator : public StubCodeGenerator {
BasicType _ret_bt;

const ABIDescriptor& _abi;
const GrowableArray<VMReg>& _input_registers;
const GrowableArray<VMStorage>& _input_registers;
// output_registers is which carry return value from native function.
const GrowableArray<VMReg>& _output_registers;
const GrowableArray<VMStorage>& _output_registers;

bool _needs_return_buffer;
int _captured_state_mask;

int _frame_complete;
int _framesize;
int _frame_size_slots;
OopMapSet* _oop_maps;
public:
DowncallStubGenerator(CodeBuffer* buffer,
BasicType* signature,
int num_args,
BasicType ret_bt,
const ABIDescriptor& abi,
const GrowableArray<VMReg>& input_registers,
const GrowableArray<VMReg>& output_registers,
bool needs_return_buffer)
const GrowableArray<VMStorage>& input_registers,
const GrowableArray<VMStorage>& output_registers,
bool needs_return_buffer,
int captured_state_mask)
: StubCodeGenerator(buffer, PrintMethodHandleStubs),
_signature(signature),
_num_args(num_args),
Expand All @@ -67,8 +69,9 @@ class DowncallStubGenerator : public StubCodeGenerator {
_input_registers(input_registers),
_output_registers(output_registers),
_needs_return_buffer(needs_return_buffer),
_captured_state_mask(captured_state_mask),
_frame_complete(0),
_framesize(0),
_frame_size_slots(0),
_oop_maps(NULL) {
}

Expand All @@ -79,7 +82,7 @@ class DowncallStubGenerator : public StubCodeGenerator {
}

int framesize() const {
return (_framesize >> (LogBytesPerWord - LogBytesPerInt));
return (_frame_size_slots >> (LogBytesPerWord - LogBytesPerInt));
}

OopMapSet* oop_maps() const {
Expand All @@ -93,13 +96,14 @@ RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
int num_args,
BasicType ret_bt,
const ABIDescriptor& abi,
const GrowableArray<VMReg>& input_registers,
const GrowableArray<VMReg>& output_registers,
bool needs_return_buffer) {
const GrowableArray<VMStorage>& input_registers,
const GrowableArray<VMStorage>& output_registers,
bool needs_return_buffer,
int captured_state_mask) {
int locs_size = 64;
CodeBuffer code("nep_invoker_blob", native_invoker_code_size, locs_size);
DowncallStubGenerator g(&code, signature, num_args, ret_bt, abi, input_registers, output_registers,
needs_return_buffer);
needs_return_buffer, captured_state_mask);
g.generate();
code.log_section_sizes("nep_invoker_blob");

Expand All @@ -126,22 +130,21 @@ void DowncallStubGenerator::generate() {
// ra, fp
// unit = 32bits word
enum layout {
ra_off,
ra_off2,
fp_off,
fp_off2,
framesize_base // inclusive of return address
ra_off,
ra_off2,
framesize // inclusive of return address
// The following are also computed dynamically:
// shadow space
// spill area
// out arg area (e.g. for stack args)
};

Register shufffle_reg = t2;
VMStorage shufffle_reg = as_VMStorage(t2);
JavaCallingConvention in_conv;
NativeCallingConvention out_conv(_input_registers);
ArgumentShuffle arg_shuffle(_signature, _num_args, _signature, _num_args, &in_conv, &out_conv,
shufffle_reg->as_VMReg());
ArgumentShuffle arg_shuffle(_signature, _num_args, _signature, _num_args, &in_conv, &out_conv, shufffle_reg);

#ifndef PRODUCT
LogTarget(Trace, foreign, downcall) lt;
Expand All @@ -153,36 +156,41 @@ void DowncallStubGenerator::generate() {
#endif

int allocated_frame_size = 0;
if (_needs_return_buffer) {
// when perform downcall, only pointer to return buffer be saved in the stack.
// return buffer size will be determined by total size of vmstorages.
allocated_frame_size += 8; // store address
}
allocated_frame_size += arg_shuffle.out_arg_stack_slots() << LogBytesPerInt;
allocated_frame_size += _abi._shadow_space_bytes;
assert(_abi._shadow_space_bytes == 0, "not expecting shadow space on RISCV64");
allocated_frame_size += arg_shuffle.out_arg_bytes();

int ret_buf_addr_sp_offset = -1;
if (_needs_return_buffer) {
// the above
ret_buf_addr_sp_offset = allocated_frame_size - 8;
}
bool should_save_return_value = !_needs_return_buffer;

// when we don't use a return buffer we need to spill the return value around our slowpath calls
// when we use a return buffer case this SHOULD be unused.
RegSpiller out_reg_spiller(_output_registers);
int spill_offset = -1;

if (!_needs_return_buffer) {
if (should_save_return_value) {
spill_offset = 0;
// spill area can be shared with the above, so we take the max of the 2
// spill area can be shared with shadow space and out args,
// since they are only used before the call,
// and spill area is only used after.
allocated_frame_size = out_reg_spiller.spill_size_bytes() > allocated_frame_size
? out_reg_spiller.spill_size_bytes()
: allocated_frame_size;
}

StubLocations locs;
locs.set(StubLocations::TARGET_ADDRESS, _abi._scratch1);
if (_needs_return_buffer) {
locs.set_frame_data(StubLocations::RETURN_BUFFER, allocated_frame_size);
allocated_frame_size += BytesPerWord; // for address spill
}
if (_captured_state_mask != 0) {
locs.set_frame_data(StubLocations::CAPTURED_STATE_BUFFER, allocated_frame_size);
allocated_frame_size += BytesPerWord;
}

allocated_frame_size = align_up(allocated_frame_size, 16);
// _framesize is in 32-bit stack slots:
_framesize += framesize_base + (allocated_frame_size >> LogBytesPerInt);
assert(is_even(_framesize / 2), "sp not 16-byte aligned");
// _frame_size_slots is in 32-bit stack slots:
_frame_size_slots += framesize + (allocated_frame_size >> LogBytesPerInt);
assert(is_even(_frame_size_slots / 2), "sp not 16-byte aligned");

_oop_maps = new OopMapSet();
address start = __ pc();
Expand All @@ -197,7 +205,7 @@ void DowncallStubGenerator::generate() {
__ block_comment("{ thread java2native");
address the_pc = __ pc();
__ set_last_Java_frame(sp, fp, the_pc, t0);
OopMap* map = new OopMap(_framesize, 0);
OopMap* map = new OopMap(_frame_size_slots, 0);
_oop_maps->add_gc_map(the_pc - start, map);

// State transition
Expand All @@ -207,37 +215,53 @@ void DowncallStubGenerator::generate() {
__ block_comment("} thread java2native");

__ block_comment("{ argument shuffle");
arg_shuffle.generate(_masm, shufffle_reg->as_VMReg(), 0, _abi._shadow_space_bytes);
if (_needs_return_buffer) {
// spill our return buffer address
assert(ret_buf_addr_sp_offset != -1, "no return buffer addr spill");
__ sd(_abi._ret_buf_addr_reg, Address(sp, ret_buf_addr_sp_offset));
}
arg_shuffle.generate(_masm, shufffle_reg, 0, _abi._shadow_space_bytes, locs);
__ block_comment("} argument shuffle");

// jump to foreign funtion.
__ jalr(_abi._target_addr_reg);
__ jalr(as_Register(locs.get(StubLocations::TARGET_ADDRESS)));

if (_needs_return_buffer) {
// when use return buffer, copy content of return registers to return buffer,
// then operations created in BoxBindingCalculator will be operated.
assert(ret_buf_addr_sp_offset != -1, "no return buffer addr spill");
__ ld(t0, Address(sp, ret_buf_addr_sp_offset));
__ ld(t0, Address(sp, locs.data_offset(StubLocations::RETURN_BUFFER)));
int offset = 0;
for (int i = 0; i < _output_registers.length(); i++) {
VMReg reg = _output_registers.at(i);
if (reg->is_Register()) {
__ sd(reg->as_Register(), Address(t0, offset));
VMStorage reg = _output_registers.at(i);
if (reg.type() == StorageType::INTEGER) {
__ sd(as_Register(reg), Address(t0, offset));
offset += 8;
} else if (reg->is_FloatRegister()) {
__ fsd(reg->as_FloatRegister(), Address(t0, offset));
} else if (reg.type() == StorageType::FLOAT) {
__ fsd(as_FloatRegister(reg), Address(t0, offset));
offset += 8;
} else {
ShouldNotReachHere();
}
}
}

//////////////////////////////////////////////////////////////////////////////

if (_captured_state_mask != 0) {
__ block_comment("{ save thread local");

if (should_save_return_value) {
out_reg_spiller.generate_spill(_masm, spill_offset);
}

__ ld(c_rarg0, Address(sp, locs.data_offset(StubLocations::CAPTURED_STATE_BUFFER)));
__ mv(c_rarg1, _captured_state_mask);
__ rt_call(CAST_FROM_FN_PTR(address, DowncallLinker::capture_state));

if (should_save_return_value) {
out_reg_spiller.generate_fill(_masm, spill_offset);
}

__ block_comment("} save thread local");
}

//////////////////////////////////////////////////////////////////////////////

__ block_comment("{ thread native2java");
__ mv(t0, _thread_in_native_trans);
__ sw(t0, Address(xthread, JavaThread::thread_state_offset()));
Expand Down Expand Up @@ -271,9 +295,12 @@ void DowncallStubGenerator::generate() {
__ leave();
__ ret();

//////////////////////////////////////////////////////////////////////////////

__ block_comment("{ L_safepoint_poll_slow_path");
__ bind(L_safepoint_poll_slow_path);
if (!_needs_return_buffer) {

if (should_save_return_value) {
// Need to save the native result registers around any runtime calls.
out_reg_spiller.generate_spill(_masm, spill_offset);
}
Expand All @@ -282,22 +309,25 @@ void DowncallStubGenerator::generate() {
assert(frame::arg_reg_save_area_bytes == 0, "not expecting frame reg save area");
__ rt_call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));

if (!_needs_return_buffer) {
if (should_save_return_value) {
out_reg_spiller.generate_fill(_masm, spill_offset);
}
__ j(L_after_safepoint_poll);
__ block_comment("} L_safepoint_poll_slow_path");

//////////////////////////////////////////////////////////////////////////////

__ block_comment("{ L_reguard");
__ bind(L_reguard);
if (!_needs_return_buffer) {

if (should_save_return_value) {
// Need to save the native result registers around any runtime calls.
out_reg_spiller.generate_spill(_masm, spill_offset);
}

__ rt_call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));

if (!_needs_return_buffer) {
if (should_save_return_value) {
out_reg_spiller.generate_fill(_masm, spill_offset);
}

Expand Down

0 comments on commit 614882d

Please sign in to comment.