Path: blob/master/src/hotspot/os_cpu/linux_zero/os_linux_zero.cpp
40931 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) {201pthread_attr_t attr;202int res = pthread_getattr_np(pthread_self(), &attr);203if (res != 0) {204if (res == ENOMEM) {205vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");206}207else {208fatal("pthread_getattr_np failed with error = %d", res);209}210}211212address stack_bottom;213size_t stack_bytes;214res = pthread_attr_getstack(&attr, (void **) &stack_bottom, &stack_bytes);215if (res != 0) {216fatal("pthread_attr_getstack failed with error = %d", res);217}218address stack_top = stack_bottom + stack_bytes;219220// The block of memory returned by pthread_attr_getstack() includes221// guard pages where present. We need to trim these off.222size_t page_bytes = os::Linux::page_size();223assert(((intptr_t) stack_bottom & (page_bytes - 1)) == 0, "unaligned stack");224225size_t guard_bytes;226res = pthread_attr_getguardsize(&attr, &guard_bytes);227if (res != 0) {228fatal("pthread_attr_getguardsize failed with errno = %d", res);229}230int guard_pages = align_up(guard_bytes, page_bytes) / page_bytes;231assert(guard_bytes == guard_pages * page_bytes, "unaligned guard");232233#ifdef IA64234// IA64 has two stacks sharing the same area of memory, a normal235// stack growing downwards and a register stack growing upwards.236// Guard pages, if present, are in the centre. This code splits237// the stack in two even without guard pages, though in theory238// there's nothing to stop us allocating more to the normal stack239// or more to the register stack if one or the other were found240// to grow faster.241int total_pages = align_down(stack_bytes, page_bytes) / page_bytes;242stack_bottom += (total_pages - guard_pages) / 2 * page_bytes;243#endif // IA64244245stack_bottom += guard_bytes;246247pthread_attr_destroy(&attr);248249// The initial thread has a growable stack, and the size reported250// by pthread_attr_getstack is the maximum size it could possibly251// be given what currently mapped. This can be huge, so we cap it.252if (os::is_primordial_thread()) {253stack_bytes = stack_top - stack_bottom;254255if (stack_bytes > JavaThread::stack_size_at_create())256stack_bytes = JavaThread::stack_size_at_create();257258stack_bottom = stack_top - stack_bytes;259}260261assert(os::current_stack_pointer() >= stack_bottom, "should do");262assert(os::current_stack_pointer() < stack_top, "should do");263264*bottom = stack_bottom;265*size = stack_top - stack_bottom;266}267268address os::current_stack_base() {269address bottom;270size_t size;271current_stack_region(&bottom, &size);272return bottom + size;273}274275size_t os::current_stack_size() {276// stack size includes normal stack and HotSpot guard pages277address bottom;278size_t size;279current_stack_region(&bottom, &size);280return size;281}282283/////////////////////////////////////////////////////////////////////////////284// helper functions for fatal error handler285286void os::print_context(outputStream* st, const void* context) {287ShouldNotCallThis();288}289290void os::print_register_info(outputStream *st, const void *context) {291ShouldNotCallThis();292}293294/////////////////////////////////////////////////////////////////////////////295// Stubs for things that would be in linux_zero.s if it existed.296// You probably want to disassemble these monkeys to check they're ok.297298extern "C" {299int SpinPause() {300return -1; // silence compile warnings301}302303304void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {305if (from > to) {306const jshort *end = from + count;307while (from < end)308*(to++) = *(from++);309}310else if (from < to) {311const jshort *end = from;312from += count - 1;313to += count - 1;314while (from >= end)315*(to--) = *(from--);316}317}318void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {319if (from > to) {320const jint *end = from + count;321while (from < end)322*(to++) = *(from++);323}324else if (from < to) {325const jint *end = from;326from += count - 1;327to += count - 1;328while (from >= end)329*(to--) = *(from--);330}331}332void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {333if (from > to) {334const jlong *end = from + count;335while (from < end)336os::atomic_copy64(from++, to++);337}338else if (from < to) {339const jlong *end = from;340from += count - 1;341to += count - 1;342while (from >= end)343os::atomic_copy64(from--, to--);344}345}346347void _Copy_arrayof_conjoint_bytes(const HeapWord* from,348HeapWord* to,349size_t count) {350memmove(to, from, count);351}352void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,353HeapWord* to,354size_t count) {355memmove(to, from, count * 2);356}357void _Copy_arrayof_conjoint_jints(const HeapWord* from,358HeapWord* to,359size_t count) {360memmove(to, from, count * 4);361}362void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,363HeapWord* to,364size_t count) {365memmove(to, from, count * 8);366}367};368369/////////////////////////////////////////////////////////////////////////////370// Implementations of atomic operations not supported by processors.371// -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html372373#ifndef _LP64374extern "C" {375long long unsigned int __sync_val_compare_and_swap_8(376volatile void *ptr,377long long unsigned int oldval,378long long unsigned int newval) {379ShouldNotCallThis();380return 0; // silence compiler warnings381}382};383#endif // !_LP64384385#ifndef PRODUCT386void os::verify_stack_alignment() {387}388#endif389390int os::extra_bang_size_in_bytes() {391// Zero does not require an additional stack banging.392return 0;393}394395396