Path: blob/master/src/hotspot/os_cpu/linux_arm/os_linux_arm.cpp
40931 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// no precompiled headers25#include "jvm.h"26#include "asm/assembler.inline.hpp"27#include "classfile/vmSymbols.hpp"28#include "code/icBuffer.hpp"29#include "code/vtableStubs.hpp"30#include "interpreter/interpreter.hpp"31#include "memory/allocation.inline.hpp"32#include "nativeInst_arm.hpp"33#include "os_share_linux.hpp"34#include "prims/jniFastGetField.hpp"35#include "prims/jvm_misc.hpp"36#include "runtime/arguments.hpp"37#include "runtime/frame.inline.hpp"38#include "runtime/interfaceSupport.inline.hpp"39#include "runtime/java.hpp"40#include "runtime/javaCalls.hpp"41#include "runtime/mutexLocker.hpp"42#include "runtime/osThread.hpp"43#include "runtime/safepointMechanism.hpp"44#include "runtime/sharedRuntime.hpp"45#include "runtime/stubRoutines.hpp"46#include "runtime/timer.hpp"47#include "signals_posix.hpp"48#include "utilities/debug.hpp"49#include "utilities/events.hpp"50#include "utilities/vmError.hpp"5152// put OS-includes here53# include <sys/types.h>54# include <sys/mman.h>55# include <pthread.h>56# include <signal.h>57# include <errno.h>58# include <dlfcn.h>59# include <stdlib.h>60# include <stdio.h>61# include <unistd.h>62# include <sys/resource.h>63# include <pthread.h>64# include <sys/stat.h>65# include <sys/time.h>66# include <sys/utsname.h>67# include <sys/socket.h>68# include <sys/wait.h>69# include <pwd.h>70# include <poll.h>71# include <ucontext.h>72#ifndef __ANDROID__73# include <fpu_control.h>74#else75# include "fpu_control.h" //include the local header76#endif77# include <asm/ptrace.h>7879#define SPELL_REG_SP "sp"8081// Don't #define SPELL_REG_FP for thumb because it is not safe to use, so this makes sure we never fetch it.82#ifndef __thumb__83#define SPELL_REG_FP "fp"84#endif8586address os::current_stack_pointer() {87#if defined(__clang__) || defined(__llvm__)88void *sp;89__asm__("mov %0, " SPELL_REG_SP : "=r"(sp));90return (address) sp;91#else92register address sp __asm__ (SPELL_REG_SP);93return sp;94#endif95}9697char* os::non_memory_address_word() {98// Must never look like an address returned by reserve_memory99return (char*) -1;100}101102103#if NGREG == 16104// These definitions are based on the observation that until105// the certain version of GCC mcontext_t was defined as106// a structure containing gregs[NGREG] array with 16 elements.107// In later GCC versions mcontext_t was redefined as struct sigcontext,108// along with NGREG constant changed to 18.109#define arm_pc gregs[15]110#define arm_sp gregs[13]111#define arm_fp gregs[11]112#define arm_r0 gregs[0]113#endif114115#define ARM_REGS_IN_CONTEXT 16116117118address os::Posix::ucontext_get_pc(const ucontext_t* uc) {119return (address)uc->uc_mcontext.arm_pc;120}121122void os::Posix::ucontext_set_pc(ucontext_t* uc, address pc) {123uc->uc_mcontext.arm_pc = (uintx)pc;124}125126intptr_t* os::Linux::ucontext_get_sp(const ucontext_t* uc) {127return (intptr_t*)uc->uc_mcontext.arm_sp;128}129130intptr_t* os::Linux::ucontext_get_fp(const ucontext_t* uc) {131return (intptr_t*)uc->uc_mcontext.arm_fp;132}133134bool is_safe_for_fp(address pc) {135#ifdef __thumb__136if (CodeCache::find_blob(pc) != NULL) {137return true;138}139// For thumb C frames, given an fp we have no idea how to access the frame contents.140return false;141#else142// Calling os::address_is_in_vm() here leads to a dladdr call. Calling any libc143// function during os::get_native_stack() can result in a deadlock if JFR is144// enabled. For now, be more lenient and allow all pc's. There are other145// frame sanity checks in shared code, and to date they have been sufficient146// for other platforms.147//return os::address_is_in_vm(pc);148return true;149#endif150}151152address os::fetch_frame_from_context(const void* ucVoid,153intptr_t** ret_sp, intptr_t** ret_fp) {154155address epc;156const ucontext_t* uc = (const ucontext_t*)ucVoid;157158if (uc != NULL) {159epc = os::Posix::ucontext_get_pc(uc);160if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);161if (ret_fp) {162intptr_t* fp = os::Linux::ucontext_get_fp(uc);163#ifndef __thumb__164if (CodeCache::find_blob(epc) == NULL) {165// It's a C frame. We need to adjust the fp.166fp += os::C_frame_offset;167}168#endif169// Clear FP when stack walking is dangerous so that170// the frame created will not be walked.171// However, ensure FP is set correctly when reliable and172// potentially necessary.173if (!is_safe_for_fp(epc)) {174// FP unreliable175fp = (intptr_t *)NULL;176}177*ret_fp = fp;178}179} else {180epc = NULL;181if (ret_sp) *ret_sp = (intptr_t *)NULL;182if (ret_fp) *ret_fp = (intptr_t *)NULL;183}184185return epc;186}187188frame os::fetch_frame_from_context(const void* ucVoid) {189intptr_t* sp;190intptr_t* fp;191address epc = fetch_frame_from_context(ucVoid, &sp, &fp);192return frame(sp, fp, epc);193}194195frame os::get_sender_for_C_frame(frame* fr) {196#ifdef __thumb__197// We can't reliably get anything from a thumb C frame.198return frame();199#else200address pc = fr->sender_pc();201if (! is_safe_for_fp(pc)) {202return frame(fr->sender_sp(), (intptr_t *)NULL, pc);203} else {204return frame(fr->sender_sp(), fr->link() + os::C_frame_offset, pc);205}206#endif207}208209//210// This actually returns two frames up. It does not return os::current_frame(),211// which is the actual current frame. Nor does it return os::get_native_stack(),212// which is the caller. It returns whoever called os::get_native_stack(). Not213// very intuitive, but consistent with how this API is implemented on other214// platforms.215//216frame os::current_frame() {217#ifdef __thumb__218// We can't reliably get anything from a thumb C frame.219return frame();220#else221register intptr_t* fp __asm__ (SPELL_REG_FP);222// fp is for os::current_frame. We want the fp for our caller.223frame myframe((intptr_t*)os::current_stack_pointer(), fp + os::C_frame_offset,224CAST_FROM_FN_PTR(address, os::current_frame));225frame caller_frame = os::get_sender_for_C_frame(&myframe);226227if (os::is_first_C_frame(&caller_frame)) {228// stack is not walkable229// Assert below was added because it does not seem like this can ever happen.230// How can this frame ever be the first C frame since it is called from C code?231// If it does ever happen, undo the assert and comment here on when/why it happens.232assert(false, "this should never happen");233return frame();234}235236// return frame for our caller's caller237return os::get_sender_for_C_frame(&caller_frame);238#endif239}240241extern "C" address check_vfp_fault_instr;242extern "C" address check_vfp3_32_fault_instr;243extern "C" address check_simd_fault_instr;244extern "C" address check_mp_ext_fault_instr;245246address check_vfp_fault_instr = NULL;247address check_vfp3_32_fault_instr = NULL;248address check_simd_fault_instr = NULL;249address check_mp_ext_fault_instr = NULL;250251252bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,253ucontext_t* uc, JavaThread* thread) {254255if (sig == SIGILL &&256((info->si_addr == (caddr_t)check_simd_fault_instr)257|| info->si_addr == (caddr_t)check_vfp_fault_instr258|| info->si_addr == (caddr_t)check_vfp3_32_fault_instr259|| info->si_addr == (caddr_t)check_mp_ext_fault_instr)) {260// skip faulty instruction + instruction that sets return value to261// success and set return value to failure.262os::Posix::ucontext_set_pc(uc, (address)info->si_addr + 8);263uc->uc_mcontext.arm_r0 = 0;264return true;265}266267address stub = NULL;268address pc = NULL;269bool unsafe_access = false;270271if (info != NULL && uc != NULL && thread != NULL) {272pc = (address) os::Posix::ucontext_get_pc(uc);273274// Handle ALL stack overflow variations here275if (sig == SIGSEGV) {276address addr = (address) info->si_addr;277278// check if fault address is within thread stack279if (thread->is_in_full_stack(addr)) {280// stack overflow281StackOverflow* overflow_state = thread->stack_overflow_state();282if (overflow_state->in_stack_yellow_reserved_zone(addr)) {283overflow_state->disable_stack_yellow_reserved_zone();284if (thread->thread_state() == _thread_in_Java) {285// Throw a stack overflow exception. Guard pages will be reenabled286// while unwinding the stack.287stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);288} else {289// Thread was in the vm or native code. Return and try to finish.290return true;291}292} else if (overflow_state->in_stack_red_zone(addr)) {293// Fatal red zone violation. Disable the guard pages and fall through294// to handle_unexpected_exception way down below.295overflow_state->disable_stack_red_zone();296tty->print_raw_cr("An irrecoverable stack overflow has occurred.");297} else {298// Accessing stack address below sp may cause SEGV if current299// thread has MAP_GROWSDOWN stack. This should only happen when300// current thread was created by user code with MAP_GROWSDOWN flag301// and then attached to VM. See notes in os_linux.cpp.302if (thread->osthread()->expanding_stack() == 0) {303thread->osthread()->set_expanding_stack();304if (os::Linux::manually_expand_stack(thread, addr)) {305thread->osthread()->clear_expanding_stack();306return true;307}308thread->osthread()->clear_expanding_stack();309} else {310fatal("recursive segv. expanding stack.");311}312}313}314}315316if (thread->thread_state() == _thread_in_Java) {317// Java thread running in Java code => find exception handler if any318// a fault inside compiled code, the interpreter, or a stub319320if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {321stub = SharedRuntime::get_poll_stub(pc);322} else if (sig == SIGBUS) {323// BugId 4454115: A read from a MappedByteBuffer can fault324// here if the underlying file has been truncated.325// Do not crash the VM in such a case.326CodeBlob* cb = CodeCache::find_blob_unsafe(pc);327CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;328if ((nm != NULL && nm->has_unsafe_access()) || (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc))) {329unsafe_access = true;330}331} else if (sig == SIGSEGV &&332MacroAssembler::uses_implicit_null_check(info->si_addr)) {333// Determination of interpreter/vtable stub/compiled code null exception334CodeBlob* cb = CodeCache::find_blob_unsafe(pc);335if (cb != NULL) {336stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);337}338} else if (sig == SIGILL && *(int *)pc == NativeInstruction::zombie_illegal_instruction) {339// Zombie340stub = SharedRuntime::get_handle_wrong_method_stub();341}342} else if ((thread->thread_state() == _thread_in_vm ||343thread->thread_state() == _thread_in_native) &&344sig == SIGBUS && thread->doing_unsafe_access()) {345unsafe_access = true;346}347348// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in349// and the heap gets shrunk before the field access.350if (sig == SIGSEGV || sig == SIGBUS) {351address addr = JNI_FastGetField::find_slowcase_pc(pc);352if (addr != (address)-1) {353stub = addr;354}355}356}357358if (unsafe_access && stub == NULL) {359// it can be an unsafe access and we haven't found360// any other suitable exception reason,361// so assume it is an unsafe access.362address next_pc = pc + Assembler::InstructionSize;363if (UnsafeCopyMemory::contains_pc(pc)) {364next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);365}366#ifdef __thumb__367if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {368next_pc = (address)((intptr_t)next_pc | 0x1);369}370#endif371372stub = SharedRuntime::handle_unsafe_access(thread, next_pc);373}374375if (stub != NULL) {376#ifdef __thumb__377if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {378intptr_t p = (intptr_t)pc | 0x1;379pc = (address)p;380381// Clear Thumb mode bit if we're redirected into the ARM ISA based code382if (((intptr_t)stub & 0x1) == 0) {383uc->uc_mcontext.arm_cpsr &= ~PSR_T_BIT;384}385} else {386// No Thumb2 compiled stubs are triggered from ARM ISA compiled JIT'd code today.387// The support needs to be added if that changes388assert((((intptr_t)stub & 0x1) == 0), "can't return to Thumb code");389}390#endif391392// save all thread context in case we need to restore it393if (thread != NULL) thread->set_saved_exception_pc(pc);394395os::Posix::ucontext_set_pc(uc, stub);396return true;397}398399return false;400}401402void os::Linux::init_thread_fpu_state(void) {403os::setup_fpu();404}405406int os::Linux::get_fpu_control_word(void) {407return 0;408}409410void os::Linux::set_fpu_control_word(int fpu_control) {411// Nothing to do412}413414void os::setup_fpu() {415#if !defined(__SOFTFP__) && defined(__VFP_FP__)416// Turn on IEEE-754 compliant VFP mode417__asm__ volatile (418"mov r0, #0;"419"fmxr fpscr, r0"420: /* no output */ : /* no input */ : "r0"421);422#endif423}424425////////////////////////////////////////////////////////////////////////////////426// thread stack427428// Minimum usable stack sizes required to get to user code. Space for429// HotSpot guard pages is added later.430size_t os::Posix::_compiler_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;431size_t os::Posix::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;432size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;433434// return default stack size for thr_type435size_t os::Posix::default_stack_size(os::ThreadType thr_type) {436// default stack size (compiler thread needs larger stack)437size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);438return s;439}440441/////////////////////////////////////////////////////////////////////////////442// helper functions for fatal error handler443444void os::print_context(outputStream *st, const void *context) {445if (context == NULL) return;446const ucontext_t *uc = (const ucontext_t*)context;447448st->print_cr("Registers:");449intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;450for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {451st->print_cr(" %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);452}453#define U64_FORMAT "0x%016llx"454// now print flag register455st->print_cr(" %-4s = 0x%08lx", "cpsr",uc->uc_mcontext.arm_cpsr);456st->cr();457458intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);459st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", p2i(sp));460print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));461st->cr();462463// Note: it may be unsafe to inspect memory near pc. For example, pc may464// point to garbage if entry point in an nmethod is corrupted. Leave465// this at the end, and hope for the best.466address pc = os::Posix::ucontext_get_pc(uc);467print_instructions(st, pc, Assembler::InstructionSize);468st->cr();469}470471void os::print_register_info(outputStream *st, const void *context) {472if (context == NULL) return;473474const ucontext_t *uc = (const ucontext_t*)context;475intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;476477st->print_cr("Register to memory mapping:");478st->cr();479for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {480st->print_cr(" %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);481print_location(st, reg_area[r]);482st->cr();483}484st->cr();485}486487488489typedef int64_t cmpxchg_long_func_t(int64_t, int64_t, volatile int64_t*);490491cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;492493int64_t os::atomic_cmpxchg_long_bootstrap(int64_t compare_value, int64_t exchange_value, volatile int64_t* dest) {494// try to use the stub:495cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());496497if (func != NULL) {498os::atomic_cmpxchg_long_func = func;499return (*func)(compare_value, exchange_value, dest);500}501assert(Threads::number_of_threads() == 0, "for bootstrap only");502503int64_t old_value = *dest;504if (old_value == compare_value)505*dest = exchange_value;506return old_value;507}508typedef int64_t load_long_func_t(const volatile int64_t*);509510load_long_func_t* os::atomic_load_long_func = os::atomic_load_long_bootstrap;511512int64_t os::atomic_load_long_bootstrap(const volatile int64_t* src) {513// try to use the stub:514load_long_func_t* func = CAST_TO_FN_PTR(load_long_func_t*, StubRoutines::atomic_load_long_entry());515516if (func != NULL) {517os::atomic_load_long_func = func;518return (*func)(src);519}520assert(Threads::number_of_threads() == 0, "for bootstrap only");521522int64_t old_value = *src;523return old_value;524}525526typedef void store_long_func_t(int64_t, volatile int64_t*);527528store_long_func_t* os::atomic_store_long_func = os::atomic_store_long_bootstrap;529530void os::atomic_store_long_bootstrap(int64_t val, volatile int64_t* dest) {531// try to use the stub:532store_long_func_t* func = CAST_TO_FN_PTR(store_long_func_t*, StubRoutines::atomic_store_long_entry());533534if (func != NULL) {535os::atomic_store_long_func = func;536return (*func)(val, dest);537}538assert(Threads::number_of_threads() == 0, "for bootstrap only");539540*dest = val;541}542543typedef int32_t atomic_add_func_t(int32_t add_value, volatile int32_t *dest);544545atomic_add_func_t * os::atomic_add_func = os::atomic_add_bootstrap;546547int32_t os::atomic_add_bootstrap(int32_t add_value, volatile int32_t *dest) {548atomic_add_func_t * func = CAST_TO_FN_PTR(atomic_add_func_t*,549StubRoutines::atomic_add_entry());550if (func != NULL) {551os::atomic_add_func = func;552return (*func)(add_value, dest);553}554555int32_t old_value = *dest;556*dest = old_value + add_value;557return (old_value + add_value);558}559560typedef int32_t atomic_xchg_func_t(int32_t exchange_value, volatile int32_t *dest);561562atomic_xchg_func_t * os::atomic_xchg_func = os::atomic_xchg_bootstrap;563564int32_t os::atomic_xchg_bootstrap(int32_t exchange_value, volatile int32_t *dest) {565atomic_xchg_func_t * func = CAST_TO_FN_PTR(atomic_xchg_func_t*,566StubRoutines::atomic_xchg_entry());567if (func != NULL) {568os::atomic_xchg_func = func;569return (*func)(exchange_value, dest);570}571572int32_t old_value = *dest;573*dest = exchange_value;574return (old_value);575}576577typedef int32_t cmpxchg_func_t(int32_t, int32_t, volatile int32_t*);578579cmpxchg_func_t* os::atomic_cmpxchg_func = os::atomic_cmpxchg_bootstrap;580581int32_t os::atomic_cmpxchg_bootstrap(int32_t compare_value, int32_t exchange_value, volatile int32_t* dest) {582// try to use the stub:583cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());584585if (func != NULL) {586os::atomic_cmpxchg_func = func;587return (*func)(compare_value, exchange_value, dest);588}589assert(Threads::number_of_threads() == 0, "for bootstrap only");590591int32_t old_value = *dest;592if (old_value == compare_value)593*dest = exchange_value;594return old_value;595}596597598#ifndef PRODUCT599void os::verify_stack_alignment() {600}601#endif602603int os::extra_bang_size_in_bytes() {604// ARM does not require an additional stack bang.605return 0;606}607608609