Path: blob/master/src/hotspot/os_cpu/linux_arm/os_linux_arm.cpp
64440 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# include <fpu_control.h>73# include <asm/ptrace.h>7475#define SPELL_REG_SP "sp"7677// Don't #define SPELL_REG_FP for thumb because it is not safe to use, so this makes sure we never fetch it.78#ifndef __thumb__79#define SPELL_REG_FP "fp"80#endif8182address os::current_stack_pointer() {83register address sp __asm__ (SPELL_REG_SP);84return sp;85}8687char* os::non_memory_address_word() {88// Must never look like an address returned by reserve_memory89return (char*) -1;90}919293#if NGREG == 1694// These definitions are based on the observation that until95// the certain version of GCC mcontext_t was defined as96// a structure containing gregs[NGREG] array with 16 elements.97// In later GCC versions mcontext_t was redefined as struct sigcontext,98// along with NGREG constant changed to 18.99#define arm_pc gregs[15]100#define arm_sp gregs[13]101#define arm_fp gregs[11]102#define arm_r0 gregs[0]103#endif104105#define ARM_REGS_IN_CONTEXT 16106107108address os::Posix::ucontext_get_pc(const ucontext_t* uc) {109return (address)uc->uc_mcontext.arm_pc;110}111112void os::Posix::ucontext_set_pc(ucontext_t* uc, address pc) {113uc->uc_mcontext.arm_pc = (uintx)pc;114}115116intptr_t* os::Linux::ucontext_get_sp(const ucontext_t* uc) {117return (intptr_t*)uc->uc_mcontext.arm_sp;118}119120intptr_t* os::Linux::ucontext_get_fp(const ucontext_t* uc) {121return (intptr_t*)uc->uc_mcontext.arm_fp;122}123124bool is_safe_for_fp(address pc) {125#ifdef __thumb__126if (CodeCache::find_blob(pc) != NULL) {127return true;128}129// For thumb C frames, given an fp we have no idea how to access the frame contents.130return false;131#else132// Calling os::address_is_in_vm() here leads to a dladdr call. Calling any libc133// function during os::get_native_stack() can result in a deadlock if JFR is134// enabled. For now, be more lenient and allow all pc's. There are other135// frame sanity checks in shared code, and to date they have been sufficient136// for other platforms.137//return os::address_is_in_vm(pc);138return true;139#endif140}141142address os::fetch_frame_from_context(const void* ucVoid,143intptr_t** ret_sp, intptr_t** ret_fp) {144145address epc;146const ucontext_t* uc = (const ucontext_t*)ucVoid;147148if (uc != NULL) {149epc = os::Posix::ucontext_get_pc(uc);150if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);151if (ret_fp) {152intptr_t* fp = os::Linux::ucontext_get_fp(uc);153#ifndef __thumb__154if (CodeCache::find_blob(epc) == NULL) {155// It's a C frame. We need to adjust the fp.156fp += os::C_frame_offset;157}158#endif159// Clear FP when stack walking is dangerous so that160// the frame created will not be walked.161// However, ensure FP is set correctly when reliable and162// potentially necessary.163if (!is_safe_for_fp(epc)) {164// FP unreliable165fp = (intptr_t *)NULL;166}167*ret_fp = fp;168}169} else {170epc = NULL;171if (ret_sp) *ret_sp = (intptr_t *)NULL;172if (ret_fp) *ret_fp = (intptr_t *)NULL;173}174175return epc;176}177178frame os::fetch_frame_from_context(const void* ucVoid) {179intptr_t* sp;180intptr_t* fp;181address epc = fetch_frame_from_context(ucVoid, &sp, &fp);182return frame(sp, fp, epc);183}184185frame os::get_sender_for_C_frame(frame* fr) {186#ifdef __thumb__187// We can't reliably get anything from a thumb C frame.188return frame();189#else190address pc = fr->sender_pc();191if (! is_safe_for_fp(pc)) {192return frame(fr->sender_sp(), (intptr_t *)NULL, pc);193} else {194return frame(fr->sender_sp(), fr->link() + os::C_frame_offset, pc);195}196#endif197}198199//200// This actually returns two frames up. It does not return os::current_frame(),201// which is the actual current frame. Nor does it return os::get_native_stack(),202// which is the caller. It returns whoever called os::get_native_stack(). Not203// very intuitive, but consistent with how this API is implemented on other204// platforms.205//206frame os::current_frame() {207#ifdef __thumb__208// We can't reliably get anything from a thumb C frame.209return frame();210#else211register intptr_t* fp __asm__ (SPELL_REG_FP);212// fp is for os::current_frame. We want the fp for our caller.213frame myframe((intptr_t*)os::current_stack_pointer(), fp + os::C_frame_offset,214CAST_FROM_FN_PTR(address, os::current_frame));215frame caller_frame = os::get_sender_for_C_frame(&myframe);216217if (os::is_first_C_frame(&caller_frame)) {218// stack is not walkable219// Assert below was added because it does not seem like this can ever happen.220// How can this frame ever be the first C frame since it is called from C code?221// If it does ever happen, undo the assert and comment here on when/why it happens.222assert(false, "this should never happen");223return frame();224}225226// return frame for our caller's caller227return os::get_sender_for_C_frame(&caller_frame);228#endif229}230231extern "C" address check_vfp_fault_instr;232extern "C" address check_vfp3_32_fault_instr;233extern "C" address check_simd_fault_instr;234extern "C" address check_mp_ext_fault_instr;235236address check_vfp_fault_instr = NULL;237address check_vfp3_32_fault_instr = NULL;238address check_simd_fault_instr = NULL;239address check_mp_ext_fault_instr = NULL;240241242bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,243ucontext_t* uc, JavaThread* thread) {244245if (sig == SIGILL &&246((info->si_addr == (caddr_t)check_simd_fault_instr)247|| info->si_addr == (caddr_t)check_vfp_fault_instr248|| info->si_addr == (caddr_t)check_vfp3_32_fault_instr249|| info->si_addr == (caddr_t)check_mp_ext_fault_instr)) {250// skip faulty instruction + instruction that sets return value to251// success and set return value to failure.252os::Posix::ucontext_set_pc(uc, (address)info->si_addr + 8);253uc->uc_mcontext.arm_r0 = 0;254return true;255}256257address stub = NULL;258address pc = NULL;259bool unsafe_access = false;260261if (info != NULL && uc != NULL && thread != NULL) {262pc = (address) os::Posix::ucontext_get_pc(uc);263264// Handle ALL stack overflow variations here265if (sig == SIGSEGV) {266address addr = (address) info->si_addr;267268// check if fault address is within thread stack269if (thread->is_in_full_stack(addr)) {270// stack overflow271StackOverflow* overflow_state = thread->stack_overflow_state();272if (overflow_state->in_stack_yellow_reserved_zone(addr)) {273overflow_state->disable_stack_yellow_reserved_zone();274if (thread->thread_state() == _thread_in_Java) {275// Throw a stack overflow exception. Guard pages will be reenabled276// while unwinding the stack.277stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);278} else {279// Thread was in the vm or native code. Return and try to finish.280return true;281}282} else if (overflow_state->in_stack_red_zone(addr)) {283// Fatal red zone violation. Disable the guard pages and fall through284// to handle_unexpected_exception way down below.285overflow_state->disable_stack_red_zone();286tty->print_raw_cr("An irrecoverable stack overflow has occurred.");287} else {288// Accessing stack address below sp may cause SEGV if current289// thread has MAP_GROWSDOWN stack. This should only happen when290// current thread was created by user code with MAP_GROWSDOWN flag291// and then attached to VM. See notes in os_linux.cpp.292if (thread->osthread()->expanding_stack() == 0) {293thread->osthread()->set_expanding_stack();294if (os::Linux::manually_expand_stack(thread, addr)) {295thread->osthread()->clear_expanding_stack();296return true;297}298thread->osthread()->clear_expanding_stack();299} else {300fatal("recursive segv. expanding stack.");301}302}303}304}305306if (thread->thread_state() == _thread_in_Java) {307// Java thread running in Java code => find exception handler if any308// a fault inside compiled code, the interpreter, or a stub309310if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) {311stub = SharedRuntime::get_poll_stub(pc);312} else if (sig == SIGBUS) {313// BugId 4454115: A read from a MappedByteBuffer can fault314// here if the underlying file has been truncated.315// Do not crash the VM in such a case.316CodeBlob* cb = CodeCache::find_blob_unsafe(pc);317CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;318if ((nm != NULL && nm->has_unsafe_access()) || (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc))) {319unsafe_access = true;320}321} else if (sig == SIGSEGV &&322MacroAssembler::uses_implicit_null_check(info->si_addr)) {323// Determination of interpreter/vtable stub/compiled code null exception324CodeBlob* cb = CodeCache::find_blob_unsafe(pc);325if (cb != NULL) {326stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);327}328} else if (sig == SIGILL && *(int *)pc == NativeInstruction::zombie_illegal_instruction) {329// Zombie330stub = SharedRuntime::get_handle_wrong_method_stub();331}332} else if ((thread->thread_state() == _thread_in_vm ||333thread->thread_state() == _thread_in_native) &&334sig == SIGBUS && thread->doing_unsafe_access()) {335unsafe_access = true;336}337338// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in339// and the heap gets shrunk before the field access.340if (sig == SIGSEGV || sig == SIGBUS) {341address addr = JNI_FastGetField::find_slowcase_pc(pc);342if (addr != (address)-1) {343stub = addr;344}345}346}347348if (unsafe_access && stub == NULL) {349// it can be an unsafe access and we haven't found350// any other suitable exception reason,351// so assume it is an unsafe access.352address next_pc = pc + Assembler::InstructionSize;353if (UnsafeCopyMemory::contains_pc(pc)) {354next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);355}356#ifdef __thumb__357if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {358next_pc = (address)((intptr_t)next_pc | 0x1);359}360#endif361362stub = SharedRuntime::handle_unsafe_access(thread, next_pc);363}364365if (stub != NULL) {366#ifdef __thumb__367if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {368intptr_t p = (intptr_t)pc | 0x1;369pc = (address)p;370371// Clear Thumb mode bit if we're redirected into the ARM ISA based code372if (((intptr_t)stub & 0x1) == 0) {373uc->uc_mcontext.arm_cpsr &= ~PSR_T_BIT;374}375} else {376// No Thumb2 compiled stubs are triggered from ARM ISA compiled JIT'd code today.377// The support needs to be added if that changes378assert((((intptr_t)stub & 0x1) == 0), "can't return to Thumb code");379}380#endif381382// save all thread context in case we need to restore it383if (thread != NULL) thread->set_saved_exception_pc(pc);384385os::Posix::ucontext_set_pc(uc, stub);386return true;387}388389return false;390}391392void os::Linux::init_thread_fpu_state(void) {393os::setup_fpu();394}395396int os::Linux::get_fpu_control_word(void) {397return 0;398}399400void os::Linux::set_fpu_control_word(int fpu_control) {401// Nothing to do402}403404void os::setup_fpu() {405#if !defined(__SOFTFP__) && defined(__VFP_FP__)406// Turn on IEEE-754 compliant VFP mode407__asm__ volatile (408"mov %%r0, #0;"409"fmxr fpscr, %%r0"410: /* no output */ : /* no input */ : "r0"411);412#endif413}414415////////////////////////////////////////////////////////////////////////////////416// thread stack417418// Minimum usable stack sizes required to get to user code. Space for419// HotSpot guard pages is added later.420size_t os::Posix::_compiler_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;421size_t os::Posix::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;422size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;423424// return default stack size for thr_type425size_t os::Posix::default_stack_size(os::ThreadType thr_type) {426// default stack size (compiler thread needs larger stack)427size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);428return s;429}430431/////////////////////////////////////////////////////////////////////////////432// helper functions for fatal error handler433434void os::print_context(outputStream *st, const void *context) {435if (context == NULL) return;436const ucontext_t *uc = (const ucontext_t*)context;437438st->print_cr("Registers:");439intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;440for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {441st->print_cr(" %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);442}443#define U64_FORMAT "0x%016llx"444// now print flag register445st->print_cr(" %-4s = 0x%08lx", "cpsr",uc->uc_mcontext.arm_cpsr);446st->cr();447448intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);449st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", p2i(sp));450print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));451st->cr();452453// Note: it may be unsafe to inspect memory near pc. For example, pc may454// point to garbage if entry point in an nmethod is corrupted. Leave455// this at the end, and hope for the best.456address pc = os::Posix::ucontext_get_pc(uc);457print_instructions(st, pc, Assembler::InstructionSize);458st->cr();459}460461void os::print_register_info(outputStream *st, const void *context) {462if (context == NULL) return;463464const ucontext_t *uc = (const ucontext_t*)context;465intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;466467st->print_cr("Register to memory mapping:");468st->cr();469for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {470st->print_cr(" %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);471print_location(st, reg_area[r]);472st->cr();473}474st->cr();475}476477478479typedef int64_t cmpxchg_long_func_t(int64_t, int64_t, volatile int64_t*);480481cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;482483int64_t os::atomic_cmpxchg_long_bootstrap(int64_t compare_value, int64_t exchange_value, volatile int64_t* dest) {484// try to use the stub:485cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());486487if (func != NULL) {488os::atomic_cmpxchg_long_func = func;489return (*func)(compare_value, exchange_value, dest);490}491assert(Threads::number_of_threads() == 0, "for bootstrap only");492493int64_t old_value = *dest;494if (old_value == compare_value)495*dest = exchange_value;496return old_value;497}498typedef int64_t load_long_func_t(const volatile int64_t*);499500load_long_func_t* os::atomic_load_long_func = os::atomic_load_long_bootstrap;501502int64_t os::atomic_load_long_bootstrap(const volatile int64_t* src) {503// try to use the stub:504load_long_func_t* func = CAST_TO_FN_PTR(load_long_func_t*, StubRoutines::atomic_load_long_entry());505506if (func != NULL) {507os::atomic_load_long_func = func;508return (*func)(src);509}510assert(Threads::number_of_threads() == 0, "for bootstrap only");511512int64_t old_value = *src;513return old_value;514}515516typedef void store_long_func_t(int64_t, volatile int64_t*);517518store_long_func_t* os::atomic_store_long_func = os::atomic_store_long_bootstrap;519520void os::atomic_store_long_bootstrap(int64_t val, volatile int64_t* dest) {521// try to use the stub:522store_long_func_t* func = CAST_TO_FN_PTR(store_long_func_t*, StubRoutines::atomic_store_long_entry());523524if (func != NULL) {525os::atomic_store_long_func = func;526return (*func)(val, dest);527}528assert(Threads::number_of_threads() == 0, "for bootstrap only");529530*dest = val;531}532533typedef int32_t atomic_add_func_t(int32_t add_value, volatile int32_t *dest);534535atomic_add_func_t * os::atomic_add_func = os::atomic_add_bootstrap;536537int32_t os::atomic_add_bootstrap(int32_t add_value, volatile int32_t *dest) {538atomic_add_func_t * func = CAST_TO_FN_PTR(atomic_add_func_t*,539StubRoutines::atomic_add_entry());540if (func != NULL) {541os::atomic_add_func = func;542return (*func)(add_value, dest);543}544545int32_t old_value = *dest;546*dest = old_value + add_value;547return (old_value + add_value);548}549550typedef int32_t atomic_xchg_func_t(int32_t exchange_value, volatile int32_t *dest);551552atomic_xchg_func_t * os::atomic_xchg_func = os::atomic_xchg_bootstrap;553554int32_t os::atomic_xchg_bootstrap(int32_t exchange_value, volatile int32_t *dest) {555atomic_xchg_func_t * func = CAST_TO_FN_PTR(atomic_xchg_func_t*,556StubRoutines::atomic_xchg_entry());557if (func != NULL) {558os::atomic_xchg_func = func;559return (*func)(exchange_value, dest);560}561562int32_t old_value = *dest;563*dest = exchange_value;564return (old_value);565}566567typedef int32_t cmpxchg_func_t(int32_t, int32_t, volatile int32_t*);568569cmpxchg_func_t* os::atomic_cmpxchg_func = os::atomic_cmpxchg_bootstrap;570571int32_t os::atomic_cmpxchg_bootstrap(int32_t compare_value, int32_t exchange_value, volatile int32_t* dest) {572// try to use the stub:573cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());574575if (func != NULL) {576os::atomic_cmpxchg_func = func;577return (*func)(compare_value, exchange_value, dest);578}579assert(Threads::number_of_threads() == 0, "for bootstrap only");580581int32_t old_value = *dest;582if (old_value == compare_value)583*dest = exchange_value;584return old_value;585}586587588#ifndef PRODUCT589void os::verify_stack_alignment() {590}591#endif592593int os::extra_bang_size_in_bytes() {594// ARM does not require an additional stack bang.595return 0;596}597598599