Skip to content

Commit

Permalink
Windows/Zero Port
Browse files Browse the repository at this point in the history
Signed-off-by: TheShermanTanker <[email protected]>
  • Loading branch information
TheShermanTanker committed Nov 14, 2024
1 parent 63367ec commit 4e6470a
Show file tree
Hide file tree
Showing 12 changed files with 781 additions and 146 deletions.
3 changes: 2 additions & 1 deletion make/modules/java.base/Lib.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ ifeq ($(ENABLE_FALLBACK_LINKER), true)
CFLAGS := $(LIBFFI_CFLAGS), \
DISABLED_WARNINGS_gcc := implicit-function-declaration unused-variable, \
LIBS := $(LIBFFI_LIBS), \
LIBS_windows := ws2_32.lib, \
LIBS_microsoft_windows := ws2_32.lib, \
LIBS_gcc_windows := -lws2_32, \
))

TARGETS += $(BUILD_LIBFALLBACKLINKER)
Expand Down
12 changes: 11 additions & 1 deletion src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,7 @@ const char* os::exception_name(int exception_code, char *buf, size_t size) {
return nullptr;
}

#ifndef ZERO
//-----------------------------------------------------------------------------
LONG Handle_IDiv_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
// handle exception caused by idiv; should only happen for -MinInt/-1
Expand Down Expand Up @@ -2555,6 +2556,7 @@ LONG Handle_IDiv_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
#endif
return EXCEPTION_CONTINUE_EXECUTION;
}
#endif

#if defined(_M_AMD64)
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -2611,7 +2613,7 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
#endif
Thread* t = Thread::current_or_null_safe();

#if defined(_M_AMD64)
#if defined(_M_AMD64) && !defined(ZERO)
if ((exception_code == EXCEPTION_ACCESS_VIOLATION) &&
VM_Version::is_cpuinfo_segv_addr(pc)) {
// Verify that OS save/restore AVX registers.
Expand All @@ -2638,13 +2640,15 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
if (exception_code == EXCEPTION_STACK_OVERFLOW) {
StackOverflow* overflow_state = thread->stack_overflow_state();
if (overflow_state->stack_guards_enabled()) {
#ifndef ZERO
if (in_java) {
frame fr;
if (os::win32::get_frame_at_stack_banging_point(thread, exceptionInfo, pc, &fr)) {
assert(fr.is_java_frame(), "Must be a Java frame");
SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
}
}
#endif
// Yellow zone violation. The o/s has unprotected the first yellow
// zone page for us. Note: must call disable_stack_yellow_zone to
// update the enabled status, even if the zone contains only one page.
Expand Down Expand Up @@ -2694,11 +2698,13 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
!ExecMem);
return EXCEPTION_CONTINUE_EXECUTION;
}
#ifndef ZERO
// Null pointer exception.
if (MacroAssembler::uses_implicit_null_check((void*)addr)) {
address stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
if (stub != nullptr) return Handle_Exception(exceptionInfo, stub);
}
#endif
report_error(t, exception_code, pc, exception_record,
exceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_SEARCH;
Expand Down Expand Up @@ -2728,6 +2734,7 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
nm = (cb != nullptr) ? cb->as_nmethod_or_null() : nullptr;
}

#ifndef ZERO
bool is_unsafe_memory_access = (in_native || in_java) && UnsafeMemoryAccess::contains_pc(pc);
if (((in_vm || in_native || is_unsafe_memory_access) && thread->doing_unsafe_access()) ||
(nm != nullptr && nm->has_unsafe_access())) {
Expand All @@ -2737,6 +2744,7 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
}
return Handle_Exception(exceptionInfo, SharedRuntime::handle_unsafe_access(thread, next_pc));
}
#endif
}

#ifdef _M_ARM64
Expand All @@ -2757,8 +2765,10 @@ LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO));

#ifndef ZERO
case EXCEPTION_INT_OVERFLOW:
return Handle_IDiv_Exception(exceptionInfo);
#endif

} // switch
}
Expand Down
103 changes: 103 additions & 0 deletions src/hotspot/os_cpu/windows_zero/atomic_windows_zero.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef OS_CPU_WINDOWS_ZERO_ATOMIC_WINDOWS_ZERO_HPP
#define OS_CPU_WINDOWS_ZERO_ATOMIC_WINDOWS_ZERO_HPP

#include <intrin.h>
#include "runtime/os.hpp"

template<size_t byte_size>
struct Atomic::PlatformAdd {
template<typename D, typename I>
D add_then_fetch(D volatile* dest, I add_value, atomic_memory_order order) const;

template<typename D, typename I>
D fetch_then_add(D volatile* dest, I add_value, atomic_memory_order order) const {
return add_then_fetch(dest, add_value, order) - add_value;
}
};

// The Interlocked* APIs only take long and will not accept __int32. That is
// acceptable on Windows, since long is a 32-bits integer type.

#define DEFINE_INTRINSIC_ADD(IntrinsicName, IntrinsicType) \
template<> \
template<typename D, typename I> \
inline D Atomic::PlatformAdd<sizeof(IntrinsicType)>::add_then_fetch(D volatile* dest, \
I add_value, \
atomic_memory_order order) const { \
STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(D)); \
return PrimitiveConversions::cast<D>( \
IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
PrimitiveConversions::cast<IntrinsicType>(add_value))); \
}

DEFINE_INTRINSIC_ADD(InterlockedAdd, long)
DEFINE_INTRINSIC_ADD(InterlockedAdd64, long long)

#undef DEFINE_INTRINSIC_ADD

#define DEFINE_INTRINSIC_XCHG(IntrinsicName, IntrinsicType) \
template<> \
template<typename T> \
inline T Atomic::PlatformXchg<sizeof(IntrinsicType)>::operator()(T volatile* dest, \
T exchange_value, \
atomic_memory_order order) const { \
STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(T)); \
return PrimitiveConversions::cast<T>( \
IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
PrimitiveConversions::cast<IntrinsicType>(exchange_value))); \
}

DEFINE_INTRINSIC_XCHG(InterlockedExchange, long)
DEFINE_INTRINSIC_XCHG(InterlockedExchange64, long long)

#undef DEFINE_INTRINSIC_XCHG

// Note: the order of the parameters is different between
// Atomic::PlatformCmpxchg<*>::operator() and the
// InterlockedCompareExchange* API.

#define DEFINE_INTRINSIC_CMPXCHG(IntrinsicName, IntrinsicType) \
template<> \
template<typename T> \
inline T Atomic::PlatformCmpxchg<sizeof(IntrinsicType)>::operator()(T volatile* dest, \
T compare_value, \
T exchange_value, \
atomic_memory_order order) const { \
STATIC_ASSERT(sizeof(IntrinsicType) == sizeof(T)); \
return PrimitiveConversions::cast<T>( \
IntrinsicName(reinterpret_cast<IntrinsicType volatile *>(dest), \
PrimitiveConversions::cast<IntrinsicType>(exchange_value), \
PrimitiveConversions::cast<IntrinsicType>(compare_value))); \
}

DEFINE_INTRINSIC_CMPXCHG(_InterlockedCompareExchange8, char) // Use the intrinsic as InterlockedCompareExchange8 does not exist
DEFINE_INTRINSIC_CMPXCHG(InterlockedCompareExchange, long)
DEFINE_INTRINSIC_CMPXCHG(InterlockedCompareExchange64, long long)

#undef DEFINE_INTRINSIC_CMPXCHG

#endif // OS_CPU_WINDOWS_ZERO_ATOMIC_WINDOWS_ZERO_HPP
47 changes: 47 additions & 0 deletions src/hotspot/os_cpu/windows_zero/globals_windows_zero.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef OS_CPU_WINDOWS_ZERO_GLOBALS_WINDOWS_ZERO_HPP
#define OS_CPU_WINDOWS_ZERO_GLOBALS_WINDOWS_ZERO_HPP

// Sets the default values for platform dependent flags used by the runtime system.
// (see globals.hpp)

define_pd_global(bool, DontYieldALot, false);

// Default stack size on Windows is determined by the executable (java.exe
// has a default value of 320K/1MB [32bit/64bit]). Depending on Windows version, changing
// ThreadStackSize to non-zero may have significant impact on memory usage.
// See comments in os_windows.cpp.
define_pd_global(intx, ThreadStackSize, 0); // 0 => use system default
define_pd_global(intx, VMThreadStackSize, 0); // 0 => use system default

define_pd_global(intx, CompilerThreadStackSize, 0);

define_pd_global(size_t, JVMInvokeMethodSlack, 8192);

// Used on 64 bit platforms for UseCompressedOops base address
define_pd_global(size_t, HeapBaseMinAddress, 2*G);

#endif // OS_CPU_WINDOWS_ZERO_GLOBALS_WINDOWS_ZERO_HPP
66 changes: 66 additions & 0 deletions src/hotspot/os_cpu/windows_zero/javaThread_windows_zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/javaThread.hpp"

frame JavaThread::pd_last_frame() {
assert(has_last_Java_frame(), "must have last_Java_sp() when suspended");
return frame(last_Java_fp(), last_Java_sp());
}

bool JavaThread::pd_get_top_frame_for_signal_handler(frame* fr_addr,
void* ucontext,
bool isInJava) {
if (has_last_Java_frame()) {
*fr_addr = pd_last_frame();
return true;
}

if (isInJava) {
// We know we are in Java, but there is no frame?
// Try to find the top-most Java frame on Zero stack then.
intptr_t* sp = stack.sp();
ZeroFrame* zf = top;
while (zf != nullptr) {
if (zf->is_interpreter_frame()) {
interpreterState istate = zf->as_interpreter_frame()->interpreter_state();
if (istate->self_link() == istate) {
// Valid interpreter state found, this is our frame.
*fr_addr = frame(zf, sp);
return true;
}
}
sp = reinterpret_cast<intptr_t *>(zf) + 1;
zf = zf->next();
}
}

// No dice.
return false;
}

void JavaThread::cache_global_variables() {
}
79 changes: 79 additions & 0 deletions src/hotspot/os_cpu/windows_zero/javaThread_windows_zero.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef OS_CPU_WINDOWS_ZERO_JAVATHREAD_WINDOWS_ZERO_HPP
#define OS_CPU_WINDOWS_ZERO_JAVATHREAD_WINDOWS_ZERO_HPP

ZeroStack* zero_stack() noexcept {
return &stack;
}

ZeroFrame* top_zero_frame() const noexcept {
return top;
}

void push_zero_frame(ZeroFrame* frame) noexcept {
*reinterpret_cast<ZeroFrame**>(frame) = top;
top = frame;
}

void pop_zero_frame() {
stack.set_sp(reinterpret_cast<intptr_t*>(top) + 1);
top = *reinterpret_cast<ZeroFrame**>(top);
}

void set_last_Java_frame() {
set_last_Java_frame(top, stack.sp());
}

void reset_last_Java_frame() {
frame_anchor()->zap();
}
void set_last_Java_frame(ZeroFrame* fp, intptr_t* sp) {
frame_anchor()->set(sp, nullptr, fp);
}

ZeroFrame* last_Java_fp() {
return frame_anchor()->last_Java_fp();
}

bool has_special_condition_for_native_trans() const noexcept {
return _suspend_flags != 0;
}

bool pd_get_top_frame_for_signal_handler(frame* fr_addr,
void* ucontext,
bool isInJava);

private:
void pd_initialize() noexcept {
top = nullptr;
}

frame pd_last_frame();

ZeroStack stack;
ZeroFrame* top;

#endif // OS_CPU_WINDOWS_ZERO_JAVATHREAD_WINDOWS_ZERO_HPP
Loading

0 comments on commit 4e6470a

Please sign in to comment.