Path: blob/master/src/hotspot/os_cpu/linux_zero/os_linux_zero.cpp
64440 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright 2007, 2008, 2009, 2010 Red Hat, Inc.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425// no precompiled headers26#include "jvm.h"27#include "asm/assembler.inline.hpp"28#include "classfile/vmSymbols.hpp"29#include "code/icBuffer.hpp"30#include "code/vtableStubs.hpp"31#include "interpreter/interpreter.hpp"32#include "memory/allocation.inline.hpp"33#include "nativeInst_zero.hpp"34#include "os_share_linux.hpp"35#include "prims/jniFastGetField.hpp"36#include "prims/jvm_misc.hpp"37#include "runtime/arguments.hpp"38#include "runtime/frame.inline.hpp"39#include "runtime/interfaceSupport.inline.hpp"40#include "runtime/java.hpp"41#include "runtime/javaCalls.hpp"42#include "runtime/mutexLocker.hpp"43#include "runtime/osThread.hpp"44#include "runtime/sharedRuntime.hpp"45#include "runtime/stubRoutines.hpp"46#include "runtime/thread.inline.hpp"47#include "runtime/timer.hpp"48#include "signals_posix.hpp"49#include "utilities/align.hpp"50#include "utilities/events.hpp"51#include "utilities/vmError.hpp"5253address os::current_stack_pointer() {54// return the address of the current function55return (address)__builtin_frame_address(0);56}5758frame os::get_sender_for_C_frame(frame* fr) {59ShouldNotCallThis();60return frame(NULL, NULL); // silence compile warning.61}6263frame os::current_frame() {64// The only thing that calls this is the stack printing code in65// VMError::report:66// - Step 110 (printing stack bounds) uses the sp in the frame67// to determine the amount of free space on the stack. We68// set the sp to a close approximation of the real value in69// order to allow this step to complete.70// - Step 120 (printing native stack) tries to walk the stack.71// The frame we create has a NULL pc, which is ignored as an72// invalid frame.73frame dummy = frame();74dummy.set_sp((intptr_t *) current_stack_pointer());75return dummy;76}7778char* os::non_memory_address_word() {79// Must never look like an address returned by reserve_memory,80// even in its subfields (as defined by the CPU immediate fields,81// if the CPU splits constants across multiple instructions).82// This is the value for x86; works pretty well for PPC too.83return (char *) -1;84}8586address os::Posix::ucontext_get_pc(const ucontext_t* uc) {87ShouldNotCallThis();88return NULL; // silence compile warnings89}9091void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {92ShouldNotCallThis();93}9495address os::fetch_frame_from_context(const void* ucVoid,96intptr_t** ret_sp,97intptr_t** ret_fp) {98ShouldNotCallThis();99return NULL; // silence compile warnings100}101102frame os::fetch_frame_from_context(const void* ucVoid) {103ShouldNotCallThis();104return frame(NULL, NULL); // silence compile warnings105}106107bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,108ucontext_t* uc, JavaThread* thread) {109110if (info != NULL && thread != NULL) {111// Handle ALL stack overflow variations here112if (sig == SIGSEGV) {113address addr = (address) info->si_addr;114115// check if fault address is within thread stack116if (thread->is_in_full_stack(addr)) {117StackOverflow* overflow_state = thread->stack_overflow_state();118// stack overflow119if (overflow_state->in_stack_yellow_reserved_zone(addr)) {120overflow_state->disable_stack_yellow_reserved_zone();121ShouldNotCallThis();122}123else if (overflow_state->in_stack_red_zone(addr)) {124overflow_state->disable_stack_red_zone();125ShouldNotCallThis();126}127else {128// Accessing stack address below sp may cause SEGV if129// current thread has MAP_GROWSDOWN stack. This should130// only happen when current thread was created by user131// code with MAP_GROWSDOWN flag and then attached to VM.132// See notes in os_linux.cpp.133if (thread->osthread()->expanding_stack() == 0) {134thread->osthread()->set_expanding_stack();135if (os::Linux::manually_expand_stack(thread, addr)) {136thread->osthread()->clear_expanding_stack();137return true;138}139thread->osthread()->clear_expanding_stack();140}141else {142fatal("recursive segv. expanding stack.");143}144}145}146}147148/*if (thread->thread_state() == _thread_in_Java) {149ShouldNotCallThis();150}151else*/ if ((thread->thread_state() == _thread_in_vm ||152thread->thread_state() == _thread_in_native) &&153sig == SIGBUS && thread->doing_unsafe_access()) {154ShouldNotCallThis();155}156157// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC158// kicks in and the heap gets shrunk before the field access.159/*if (sig == SIGSEGV || sig == SIGBUS) {160address addr = JNI_FastGetField::find_slowcase_pc(pc);161if (addr != (address)-1) {162stub = addr;163}164}*/165}166167return false; // Fatal error168169}170171void os::Linux::init_thread_fpu_state(void) {172// Nothing to do173}174175int os::Linux::get_fpu_control_word() {176ShouldNotCallThis();177return -1; // silence compile warnings178}179180void os::Linux::set_fpu_control_word(int fpu) {181ShouldNotCallThis();182}183184///////////////////////////////////////////////////////////////////////////////185// thread stack186187size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K;188size_t os::Posix::_java_thread_min_stack_allowed = 64 * K;189size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;190191size_t os::Posix::default_stack_size(os::ThreadType thr_type) {192#ifdef _LP64193size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);194#else195size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);196#endif // _LP64197return s;198}199200static void current_stack_region(address *bottom, size_t *size) {201if (os::is_primordial_thread()) {202// primordial thread needs special handling because pthread_getattr_np()203// may return bogus value.204address stack_bottom = os::Linux::initial_thread_stack_bottom();205size_t stack_bytes = os::Linux::initial_thread_stack_size();206207assert(os::current_stack_pointer() >= stack_bottom, "should do");208assert(os::current_stack_pointer() < stack_bottom + stack_bytes, "should do");209210*bottom = stack_bottom;211*size = stack_bytes;212return;213}214215pthread_attr_t attr;216int res = pthread_getattr_np(pthread_self(), &attr);217if (res != 0) {218if (res == ENOMEM) {219vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");220}221else {222fatal("pthread_getattr_np failed with error = %d", res);223}224}225226address stack_bottom;227size_t stack_bytes;228res = pthread_attr_getstack(&attr, (void **) &stack_bottom, &stack_bytes);229if (res != 0) {230fatal("pthread_attr_getstack failed with error = %d", res);231}232address stack_top = stack_bottom + stack_bytes;233234// The block of memory returned by pthread_attr_getstack() includes235// guard pages where present. We need to trim these off.236size_t page_bytes = os::Linux::page_size();237assert(((intptr_t) stack_bottom & (page_bytes - 1)) == 0, "unaligned stack");238239size_t guard_bytes;240res = pthread_attr_getguardsize(&attr, &guard_bytes);241if (res != 0) {242fatal("pthread_attr_getguardsize failed with errno = %d", res);243}244int guard_pages = align_up(guard_bytes, page_bytes) / page_bytes;245assert(guard_bytes == guard_pages * page_bytes, "unaligned guard");246247#ifdef IA64248// IA64 has two stacks sharing the same area of memory, a normal249// stack growing downwards and a register stack growing upwards.250// Guard pages, if present, are in the centre. This code splits251// the stack in two even without guard pages, though in theory252// there's nothing to stop us allocating more to the normal stack253// or more to the register stack if one or the other were found254// to grow faster.255int total_pages = align_down(stack_bytes, page_bytes) / page_bytes;256stack_bottom += (total_pages - guard_pages) / 2 * page_bytes;257#endif // IA64258259stack_bottom += guard_bytes;260261pthread_attr_destroy(&attr);262263assert(os::current_stack_pointer() >= stack_bottom, "should do");264assert(os::current_stack_pointer() < stack_top, "should do");265266*bottom = stack_bottom;267*size = stack_top - stack_bottom;268}269270address os::current_stack_base() {271address bottom;272size_t size;273current_stack_region(&bottom, &size);274return bottom + size;275}276277size_t os::current_stack_size() {278// stack size includes normal stack and HotSpot guard pages279address bottom;280size_t size;281current_stack_region(&bottom, &size);282return size;283}284285/////////////////////////////////////////////////////////////////////////////286// helper functions for fatal error handler287288void os::print_context(outputStream* st, const void* context) {289ShouldNotCallThis();290}291292void os::print_register_info(outputStream *st, const void *context) {293ShouldNotCallThis();294}295296/////////////////////////////////////////////////////////////////////////////297// Stubs for things that would be in linux_zero.s if it existed.298// You probably want to disassemble these monkeys to check they're ok.299300extern "C" {301int SpinPause() {302return -1; // silence compile warnings303}304305306void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {307if (from > to) {308const jshort *end = from + count;309while (from < end)310*(to++) = *(from++);311}312else if (from < to) {313const jshort *end = from;314from += count - 1;315to += count - 1;316while (from >= end)317*(to--) = *(from--);318}319}320void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {321if (from > to) {322const jint *end = from + count;323while (from < end)324*(to++) = *(from++);325}326else if (from < to) {327const jint *end = from;328from += count - 1;329to += count - 1;330while (from >= end)331*(to--) = *(from--);332}333}334void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {335if (from > to) {336const jlong *end = from + count;337while (from < end)338os::atomic_copy64(from++, to++);339}340else if (from < to) {341const jlong *end = from;342from += count - 1;343to += count - 1;344while (from >= end)345os::atomic_copy64(from--, to--);346}347}348349void _Copy_arrayof_conjoint_bytes(const HeapWord* from,350HeapWord* to,351size_t count) {352memmove(to, from, count);353}354void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,355HeapWord* to,356size_t count) {357memmove(to, from, count * 2);358}359void _Copy_arrayof_conjoint_jints(const HeapWord* from,360HeapWord* to,361size_t count) {362memmove(to, from, count * 4);363}364void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,365HeapWord* to,366size_t count) {367memmove(to, from, count * 8);368}369};370371/////////////////////////////////////////////////////////////////////////////372// Implementations of atomic operations not supported by processors.373// -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html374375#ifndef _LP64376extern "C" {377long long unsigned int __sync_val_compare_and_swap_8(378volatile void *ptr,379long long unsigned int oldval,380long long unsigned int newval) {381ShouldNotCallThis();382return 0; // silence compiler warnings383}384};385#endif // !_LP64386387#ifndef PRODUCT388void os::verify_stack_alignment() {389}390#endif391392int os::extra_bang_size_in_bytes() {393// Zero does not require an additional stack banging.394return 0;395}396397398