From c3c0a676e53dbafd82e8614a20f6c47df7fc2108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20=C3=96sterlund?= Date: Tue, 28 Oct 2025 13:41:38 +0000 Subject: [PATCH] 8370500: Change windows x64 implementation of os::current_stack_pointer() Reviewed-by: aboldtch, dholmes, kvn, adinn --- src/hotspot/cpu/x86/stubDeclarations_x86.hpp | 4 ---- src/hotspot/cpu/x86/stubGenerator_x86_64.cpp | 18 ----------------- src/hotspot/cpu/x86/stubGenerator_x86_64.hpp | 6 ------ .../os_cpu/windows_x86/os_windows_x86.cpp | 20 ++++++++++--------- 4 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/hotspot/cpu/x86/stubDeclarations_x86.hpp b/src/hotspot/cpu/x86/stubDeclarations_x86.hpp index 30a93fa4917..3e0e1d5bf07 100644 --- a/src/hotspot/cpu/x86/stubDeclarations_x86.hpp +++ b/src/hotspot/cpu/x86/stubDeclarations_x86.hpp @@ -41,10 +41,6 @@ do_stub(initial, verify_mxcsr) \ do_arch_entry(x86, initial, verify_mxcsr, verify_mxcsr_entry, \ verify_mxcsr_entry) \ - do_stub(initial, get_previous_sp) \ - do_arch_entry(x86, initial, get_previous_sp, \ - get_previous_sp_entry, \ - get_previous_sp_entry) \ do_stub(initial, f2i_fixup) \ do_arch_entry(x86, initial, f2i_fixup, f2i_fixup, f2i_fixup) \ do_stub(initial, f2l_fixup) \ diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp index 6eb641daaf9..efb0411aa39 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.cpp @@ -541,22 +541,6 @@ address StubGenerator::generate_orderaccess_fence() { } -// Support for intptr_t get_previous_sp() -// -// This routine is used to find the previous stack pointer for the -// caller. -address StubGenerator::generate_get_previous_sp() { - StubId stub_id = StubId::stubgen_get_previous_sp_id; - StubCodeMark mark(this, stub_id); - address start = __ pc(); - - __ movptr(rax, rsp); - __ addptr(rax, 8); // return address is at the top of the stack. - __ ret(0); - - return start; -} - //---------------------------------------------------------------------------------------------------- // Support for void verify_mxcsr() // @@ -4083,8 +4067,6 @@ void StubGenerator::generate_initial_stubs() { StubRoutines::_catch_exception_entry = generate_catch_exception(); // platform dependent - StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp(); - StubRoutines::x86::_verify_mxcsr_entry = generate_verify_mxcsr(); StubRoutines::x86::_f2i_fixup = generate_f2i_fixup(); diff --git a/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp b/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp index 0887225d3e8..36315535d16 100644 --- a/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp +++ b/src/hotspot/cpu/x86/stubGenerator_x86_64.hpp @@ -68,12 +68,6 @@ class StubGenerator: public StubCodeGenerator { // Support for intptr_t OrderAccess::fence() address generate_orderaccess_fence(); - // Support for intptr_t get_previous_sp() - // - // This routine is used to find the previous stack pointer for the - // caller. - address generate_get_previous_sp(); - //---------------------------------------------------------------------------------------------------- // Support for void verify_mxcsr() // diff --git a/src/hotspot/os_cpu/windows_x86/os_windows_x86.cpp b/src/hotspot/os_cpu/windows_x86/os_windows_x86.cpp index 53f96479832..c688848c790 100644 --- a/src/hotspot/os_cpu/windows_x86/os_windows_x86.cpp +++ b/src/hotspot/os_cpu/windows_x86/os_windows_x86.cpp @@ -51,6 +51,8 @@ #include "utilities/vmError.hpp" #include "windbghelp.hpp" +#include + #undef REG_SP #undef REG_FP @@ -247,11 +249,15 @@ intptr_t* os::fetch_bcp_from_context(const void* ucVoid) { // Returns the current stack pointer. Accurate value needed for // os::verify_stack_alignment(). +// The function is intentionally not inlined. This way, the transfer of control +// into this method must be made with a call instruction. The MSVC +// _AddressOfReturnAddress() intrinsic returns the address of the return PC +// saved by that call instruction. Therefore, the stack pointer of the caller +// just before the call instruction, is acquired by skipping over the return PC +// slot in the stack. +__declspec(noinline) address os::current_stack_pointer() { - typedef address get_sp_func(); - get_sp_func* func = CAST_TO_FN_PTR(get_sp_func*, - StubRoutines::x86::get_previous_sp_entry()); - return (*func)(); + return ((address)_AddressOfReturnAddress()) + sizeof(void*); } bool os::win32::get_frame_at_stack_banging_point(JavaThread* thread, @@ -408,11 +414,7 @@ void os::setup_fpu() { #ifndef PRODUCT void os::verify_stack_alignment() { - // The current_stack_pointer() calls generated get_previous_sp stub routine. - // Only enable the assert after the routine becomes available. - if (StubRoutines::initial_stubs_code() != nullptr) { - assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment"); - } + assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment"); } #endif