Skip to content

Commit

Permalink
8320892: AArch64: Restore FPU control state after JNI
Browse files Browse the repository at this point in the history
Backport-of: 50f31240555888018f0f496ab29c8a5932dce459
  • Loading branch information
Boris Ulasevich authored and Paul Hohensee committed Oct 17, 2024
1 parent fe4b0fa commit 27d2abe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/hotspot/cpu/aarch64/downcallLinker_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ void DowncallStubGenerator::generate() {
Label L_reguard;
Label L_after_reguard;
if (_needs_transition) {
// Restore cpu control state after JNI call
__ restore_cpu_control_state_after_jni(rscratch1, tmp1);

__ mov(tmp1, _thread_in_native_trans);
__ strw(tmp1, Address(rthread, JavaThread::thread_state_offset()));

Expand Down
17 changes: 17 additions & 0 deletions src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4435,6 +4435,23 @@ void MacroAssembler::load_klass(Register dst, Register src) {
}
}

void MacroAssembler::restore_cpu_control_state_after_jni(Register tmp1, Register tmp2) {
if (RestoreMXCSROnJNICalls) {
Label OK;
get_fpcr(tmp1);
mov(tmp2, tmp1);
// Set FPCR to the state we need. We do want Round to Nearest. We
// don't want non-IEEE rounding modes or floating-point traps.
bfi(tmp1, zr, 22, 4); // Clear DN, FZ, and Rmode
bfi(tmp1, zr, 8, 5); // Clear exception-control bits (8-12)
bfi(tmp1, zr, 0, 2); // Clear AH:FIZ
eor(tmp2, tmp1, tmp2);
cbz(tmp2, OK); // Only reset FPCR if it's wrong
set_fpcr(tmp1);
bind(OK);
}
}

// ((OopHandle)result).resolve();
void MacroAssembler::resolve_oop_handle(Register result, Register tmp1, Register tmp2) {
// OopHandle::resolve is an indirection.
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/cpu/aarch64/macroAssembler_aarch64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,8 @@ class MacroAssembler: public Assembler {
#define verify_method_ptr(reg) _verify_method_ptr(reg, "broken method " #reg, __FILE__, __LINE__)
#define verify_klass_ptr(reg) _verify_klass_ptr(reg, "broken klass " #reg, __FILE__, __LINE__)

// only if +VerifyFPU
void verify_FPU(int stack_depth, const char* s = "illegal FPU state");
// Restore cpu control state after JNI call
void restore_cpu_control_state_after_jni(Register tmp1, Register tmp2);

// prints msg, dumps registers and stops execution
void stop(const char* msg);
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,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(rscratch1, rscratch2);

// Unpack native results.
switch (ret_type) {
case T_BOOLEAN: __ c2bool(r0); break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,9 @@ address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
__ get_method(rmethod);
// result potentially in r0 or v0

// Restore cpu control state after JNI call
__ restore_cpu_control_state_after_jni(rscratch1, rscratch2);

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

1 comment on commit 27d2abe

@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.