2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* Copyright 2007, 2008, 2009, 2010 Red Hat, Inc.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
26
// no precompiled headers
27
#include "asm/assembler.inline.hpp"
28
#include "atomic_bsd_zero.hpp"
29
#include "classfile/vmSymbols.hpp"
30
#include "code/vtableStubs.hpp"
31
#include "interpreter/interpreter.hpp"
33
#include "memory/allocation.inline.hpp"
34
#include "nativeInst_zero.hpp"
36
#include "os_posix.hpp"
37
#include "prims/jniFastGetField.hpp"
38
#include "prims/jvm_misc.hpp"
39
#include "runtime/arguments.hpp"
40
#include "runtime/frame.inline.hpp"
41
#include "runtime/interfaceSupport.inline.hpp"
42
#include "runtime/java.hpp"
43
#include "runtime/javaCalls.hpp"
44
#include "runtime/javaThread.hpp"
45
#include "runtime/mutexLocker.hpp"
46
#include "runtime/osThread.hpp"
47
#include "runtime/sharedRuntime.hpp"
48
#include "runtime/stubRoutines.hpp"
49
#include "runtime/timer.hpp"
50
#include "signals_posix.hpp"
51
#include "utilities/events.hpp"
52
#include "utilities/vmError.hpp"
54
#if !defined(__APPLE__) && !defined(__NetBSD__)
56
# include <pthread_np.h> /* For pthread_attr_get_np */
59
address os::current_stack_pointer() {
60
address dummy = (address) &dummy;
64
frame os::get_sender_for_C_frame(frame* fr) {
69
frame os::current_frame() {
70
// The only thing that calls this is the stack printing code in
72
// - Step 110 (printing stack bounds) uses the sp in the frame
73
// to determine the amount of free space on the stack. We
74
// set the sp to a close approximation of the real value in
75
// order to allow this step to complete.
76
// - Step 120 (printing native stack) tries to walk the stack.
77
// The frame we create has a null pc, which is ignored as an
79
frame dummy = frame();
80
dummy.set_sp((intptr_t *) current_stack_pointer());
84
char* os::non_memory_address_word() {
85
// Must never look like an address returned by reserve_memory,
86
// even in its subfields (as defined by the CPU immediate fields,
87
// if the CPU splits constants across multiple instructions).
88
// This is the value for x86; works pretty well for PPC too.
92
address os::Posix::ucontext_get_pc(const ucontext_t* uc) {
97
void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {
101
address os::fetch_frame_from_context(const void* ucVoid,
108
frame os::fetch_frame_from_context(const void* ucVoid) {
113
bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
114
ucontext_t* uc, JavaThread* thread) {
116
if (info != nullptr && thread != nullptr) {
117
// Handle ALL stack overflow variations here
118
if (sig == SIGSEGV || sig == SIGBUS) {
119
address addr = (address) info->si_addr;
121
// check if fault address is within thread stack
122
if (thread->is_in_full_stack(addr)) {
123
StackOverflow* overflow_state = thread->stack_overflow_state();
125
if (overflow_state->in_stack_yellow_reserved_zone(addr)) {
126
overflow_state->disable_stack_yellow_reserved_zone();
129
else if (overflow_state->in_stack_red_zone(addr)) {
130
overflow_state->disable_stack_red_zone();
136
/*if (thread->thread_state() == _thread_in_Java) {
139
else*/ if ((thread->thread_state() == _thread_in_vm ||
140
thread->thread_state() == _thread_in_native) &&
141
sig == SIGBUS && thread->doing_unsafe_access()) {
145
// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC
146
// kicks in and the heap gets shrunk before the field access.
147
/*if (sig == SIGSEGV || sig == SIGBUS) {
148
address addr = JNI_FastGetField::find_slowcase_pc(pc);
149
if (addr != (address)-1) {
158
void os::Bsd::init_thread_fpu_state(void) {
162
///////////////////////////////////////////////////////////////////////////////
165
size_t os::_compiler_thread_min_stack_allowed = 64 * K;
166
size_t os::_java_thread_min_stack_allowed = 64 * K;
167
size_t os::_vm_internal_thread_min_stack_allowed = 64 * K;
169
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
171
size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
173
size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
178
void os::current_stack_base_and_size(address* base, size_t* size) {
182
pthread_t self = pthread_self();
183
*base = (address) pthread_get_stackaddr_np(self);
184
*size = pthread_get_stacksize_np(self);
185
bottom = *base - *size;
186
#elif defined(__OpenBSD__)
188
int rslt = pthread_stackseg_np(pthread_self(), &ss);
191
fatal("pthread_stackseg_np failed with error = %d", rslt);
193
*base = (address) ss.ss_sp;
195
bottom = *base - *size;
199
int rslt = pthread_attr_init(&attr);
201
// JVM needs to know exact stack location, abort if it fails
203
fatal("pthread_attr_init failed with error = %d", rslt);
205
rslt = pthread_attr_get_np(pthread_self(), &attr);
208
fatal("pthread_attr_get_np failed with error = %d", rslt);
210
if (pthread_attr_getstackaddr(&attr, (void **) &bottom) != 0 ||
211
pthread_attr_getstacksize(&attr, size) != 0) {
212
fatal("Can not locate current stack attributes!");
215
*base = bottom + *size;
217
pthread_attr_destroy(&attr);
220
assert(os::current_stack_pointer() >= bottom &&
221
os::current_stack_pointer() < *base, "just checking");
224
/////////////////////////////////////////////////////////////////////////////
225
// helper functions for fatal error handler
227
void os::print_context(outputStream* st, const void* context) {
231
void os::print_tos_pc(outputStream *st, const void *context) {
235
void os::print_register_info(outputStream *st, const void *context, int& continuation) {
239
/////////////////////////////////////////////////////////////////////////////
240
// Stubs for things that would be in bsd_zero.s if it existed.
241
// You probably want to disassemble these monkeys to check they're ok.
248
void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {
250
const jshort *end = from + count;
254
else if (from < to) {
255
const jshort *end = from;
262
void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {
264
const jint *end = from + count;
268
else if (from < to) {
269
const jint *end = from;
276
void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {
278
const jlong *end = from + count;
280
atomic_copy64(from++, to++);
282
else if (from < to) {
283
const jlong *end = from;
287
atomic_copy64(from--, to--);
291
void _Copy_arrayof_conjoint_bytes(const HeapWord* from,
294
memmove(to, from, count);
296
void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,
299
memmove(to, from, count * 2);
301
void _Copy_arrayof_conjoint_jints(const HeapWord* from,
304
memmove(to, from, count * 4);
306
void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,
309
memmove(to, from, count * 8);
314
void os::verify_stack_alignment() {
318
int os::extra_bang_size_in_bytes() {
319
// Zero does not require an additional stack bang.
323
#if defined(AARCH64) && defined(__APPLE__)
324
void os::current_thread_enable_wx(WXMode mode) {
325
pthread_jit_write_protect_np(mode == WXExec);
329
void os::setup_fpu() {}