Skip to content

Commit

Permalink
8330266: RISC-V: Restore frm to RoundingMode::rne after JNI
Browse files Browse the repository at this point in the history
Reviewed-by: fyang, rehn
  • Loading branch information
Hamlin Li committed Apr 19, 2024
1 parent 9f2a4fa commit 85261bc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/hotspot/cpu/riscv/downcallLinker_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ void DowncallLinker::StubGenerator::generate() {
Label L_reguard;
Label L_after_reguard;
if (_needs_transition) {
// Restore cpu control state after JNI call
__ restore_cpu_control_state_after_jni(t0);

__ block_comment("{ thread native2java");
__ mv(t0, _thread_in_native_trans);
__ sw(t0, Address(xthread, JavaThread::thread_state_offset()));
Expand Down
13 changes: 13 additions & 0 deletions src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,19 @@ void MacroAssembler::fsflagsi(Register Rd, unsigned imm) {

#undef INSN

void MacroAssembler::restore_cpu_control_state_after_jni(Register tmp) {
if (RestoreMXCSROnJNICalls) {
Label skip_fsrmi;
frrm(tmp);
// Set FRM to the state we need. We do want Round to Nearest.
// We don't want non-IEEE rounding modes.
guarantee(RoundingMode::rne == 0, "must be");
beqz(tmp, skip_fsrmi); // Only reset FRM if it's wrong
fsrmi(RoundingMode::rne);
bind(skip_fsrmi);
}
}

void MacroAssembler::push_reg(Register Rs)
{
addi(esp, esp, 0 - wordSize);
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/cpu/riscv/macroAssembler_riscv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ class MacroAssembler: public Assembler {
void fsflagsi(Register Rd, unsigned imm);
void fsflagsi(unsigned imm);

// Restore cpu control state after JNI call
void restore_cpu_control_state_after_jni(Register tmp);

// Control transfer pseudo instructions
void beqz(Register Rs, const address dest);
void bnez(Register Rs, const address dest);
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/cpu/riscv/sharedRuntime_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,9 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
intptr_t return_pc = (intptr_t) __ pc();
oop_maps->add_gc_map(return_pc - start, map);

// Verify or restore cpu control state after JNI call
__ restore_cpu_control_state_after_jni(t0);

// Unpack native results.
if (ret_type != T_OBJECT && ret_type != T_ARRAY) {
__ cast_primitive_type(ret_type, x10);
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
__ get_method(xmethod);
// result potentially in x10 or f10

// Restore cpu control state after JNI call
__ restore_cpu_control_state_after_jni(t0);

// make room for the pushes we're about to do
__ sub(t0, esp, 4 * wordSize);
__ andi(sp, t0, -16);
Expand Down

1 comment on commit 85261bc

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.