Path: blob/master/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp
64440 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2014, Red Hat Inc. All rights reserved.3* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.4* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.5*6* This code is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License version 2 only, as8* published by the Free Software Foundation.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 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.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*24*/2526// no precompiled headers27#include "jvm.h"28#include "asm/macroAssembler.hpp"29#include "classfile/classLoader.hpp"30#include "classfile/vmSymbols.hpp"31#include "code/codeCache.hpp"32#include "code/icBuffer.hpp"33#include "code/vtableStubs.hpp"34#include "interpreter/interpreter.hpp"35#include "logging/log.hpp"36#include "memory/allocation.inline.hpp"37#include "os_share_bsd.hpp"38#include "prims/jniFastGetField.hpp"39#include "prims/jvm_misc.hpp"40#include "runtime/arguments.hpp"41#include "runtime/frame.inline.hpp"42#include "runtime/interfaceSupport.inline.hpp"43#include "runtime/java.hpp"44#include "runtime/javaCalls.hpp"45#include "runtime/mutexLocker.hpp"46#include "runtime/osThread.hpp"47#include "runtime/safepointMechanism.hpp"48#include "runtime/sharedRuntime.hpp"49#include "runtime/stubRoutines.hpp"50#include "runtime/thread.inline.hpp"51#include "runtime/timer.hpp"52#include "signals_posix.hpp"53#include "utilities/align.hpp"54#include "utilities/events.hpp"55#include "utilities/vmError.hpp"56#include "tcg-apple-jit.h"5758// put OS-includes here59# include <sys/types.h>60# include <sys/mman.h>61# include <pthread.h>62# include <signal.h>63# include <errno.h>64# include <dlfcn.h>65# include <stdlib.h>66# include <stdio.h>67# include <unistd.h>68# include <sys/resource.h>69# include <sys/stat.h>70# include <sys/time.h>71# include <sys/utsname.h>72# include <sys/socket.h>73# include <sys/wait.h>74# include <pwd.h>75# include <poll.h>76#ifndef __OpenBSD__77# include <ucontext.h>78#endif7980#if !defined(__APPLE__) && !defined(__NetBSD__)81# include <pthread_np.h>82#endif8384#define SPELL_REG_SP "sp"85#define SPELL_REG_FP "fp"8687#ifdef __APPLE__88// see darwin-xnu/osfmk/mach/arm/_structs.h8990// 10.5 UNIX03 member name prefixes91#define DU3_PREFIX(s, m) __ ## s.__ ## m92#endif9394#define context_x uc_mcontext->DU3_PREFIX(ss,x)95#define context_fp uc_mcontext->DU3_PREFIX(ss,fp)96#define context_lr uc_mcontext->DU3_PREFIX(ss,lr)97#define context_sp uc_mcontext->DU3_PREFIX(ss,sp)98#define context_pc uc_mcontext->DU3_PREFIX(ss,pc)99#define context_cpsr uc_mcontext->DU3_PREFIX(ss,cpsr)100#define context_esr uc_mcontext->DU3_PREFIX(es,esr)101102address os::current_stack_pointer() {103#if defined(__clang__) || defined(__llvm__)104void *sp;105__asm__("mov %0, " SPELL_REG_SP : "=r"(sp));106return (address) sp;107#else108register void *sp __asm__ (SPELL_REG_SP);109return (address) sp;110#endif111}112113char* os::non_memory_address_word() {114// Must never look like an address returned by reserve_memory,115// even in its subfields (as defined by the CPU immediate fields,116// if the CPU splits constants across multiple instructions).117118// the return value used in computation of Universe::non_oop_word(), which119// is loaded by cpu/aarch64 by MacroAssembler::movptr(Register, uintptr_t)120return (char*) 0xffffffffffff;121}122123address os::Posix::ucontext_get_pc(const ucontext_t * uc) {124return (address)uc->context_pc;125}126127void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {128uc->context_pc = (intptr_t)pc ;129}130131intptr_t* os::Bsd::ucontext_get_sp(const ucontext_t * uc) {132return (intptr_t*)uc->context_sp;133}134135intptr_t* os::Bsd::ucontext_get_fp(const ucontext_t * uc) {136return (intptr_t*)uc->context_fp;137}138139address os::fetch_frame_from_context(const void* ucVoid,140intptr_t** ret_sp, intptr_t** ret_fp) {141142address epc;143const ucontext_t* uc = (const ucontext_t*)ucVoid;144145if (uc != NULL) {146epc = os::Posix::ucontext_get_pc(uc);147if (ret_sp) *ret_sp = os::Bsd::ucontext_get_sp(uc);148if (ret_fp) *ret_fp = os::Bsd::ucontext_get_fp(uc);149} else {150epc = NULL;151if (ret_sp) *ret_sp = (intptr_t *)NULL;152if (ret_fp) *ret_fp = (intptr_t *)NULL;153}154155return epc;156}157158frame os::fetch_frame_from_context(const void* ucVoid) {159intptr_t* sp;160intptr_t* fp;161address epc = fetch_frame_from_context(ucVoid, &sp, &fp);162return frame(sp, fp, epc);163}164165frame os::fetch_compiled_frame_from_context(const void* ucVoid) {166const ucontext_t* uc = (const ucontext_t*)ucVoid;167// In compiled code, the stack banging is performed before LR168// has been saved in the frame. LR is live, and SP and FP169// belong to the caller.170intptr_t* fp = os::Bsd::ucontext_get_fp(uc);171intptr_t* sp = os::Bsd::ucontext_get_sp(uc);172address pc = (address)(uc->context_lr173- NativeInstruction::instruction_size);174return frame(sp, fp, pc);175}176177// JVM compiled with -fno-omit-frame-pointer, so RFP is saved on the stack.178frame os::get_sender_for_C_frame(frame* fr) {179return frame(fr->link(), fr->link(), fr->sender_pc());180}181182NOINLINE frame os::current_frame() {183intptr_t *fp = *(intptr_t **)__builtin_frame_address(0);184frame myframe((intptr_t*)os::current_stack_pointer(),185(intptr_t*)fp,186CAST_FROM_FN_PTR(address, os::current_frame));187if (os::is_first_C_frame(&myframe)) {188// stack is not walkable189return frame();190} else {191return os::get_sender_for_C_frame(&myframe);192}193}194195ATTRIBUTE_PRINTF(6, 7)196static void report_and_die(Thread* thread, void* context, const char* filename, int lineno, const char* message,197const char* detail_fmt, ...) {198va_list va;199va_start(va, detail_fmt);200VMError::report_and_die(thread, context, filename, lineno, message, detail_fmt, va);201va_end(va);202}203204bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,205ucontext_t* uc, JavaThread* thread) {206// Enable WXWrite: this function is called by the signal handler at arbitrary207// point of execution.208ThreadWXEnable wx(WXWrite, thread);209210// decide if this trap can be handled by a stub211address stub = NULL;212213address pc = NULL;214215//%note os_trap_1216if (info != NULL && uc != NULL && thread != NULL) {217pc = (address) os::Posix::ucontext_get_pc(uc);218219// Handle ALL stack overflow variations here220if (sig == SIGSEGV || sig == SIGBUS) {221address addr = (address) info->si_addr;222223// Make sure the high order byte is sign extended, as it may be masked away by the hardware.224if ((uintptr_t(addr) & (uintptr_t(1) << 55)) != 0) {225addr = address(uintptr_t(addr) | (uintptr_t(0xFF) << 56));226}227228// check if fault address is within thread stack229if (thread->is_in_full_stack(addr)) {230// stack overflow231if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {232return true; // continue233}234}235}236237// We test if stub is already set (by the stack overflow code238// above) so it is not overwritten by the code that follows. This239// check is not required on other platforms, because on other240// platforms we check for SIGSEGV only or SIGBUS only, where here241// we have to check for both SIGSEGV and SIGBUS.242if (thread->thread_state() == _thread_in_Java && stub == NULL) {243// Java thread running in Java code => find exception handler if any244// a fault inside compiled code, the interpreter, or a stub245246// Handle signal from NativeJump::patch_verified_entry().247if ((sig == SIGILL)248&& nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {249if (TraceTraps) {250tty->print_cr("trap: zombie_not_entrant");251}252stub = SharedRuntime::get_handle_wrong_method_stub();253} else if ((sig == SIGSEGV || sig == SIGBUS) && SafepointMechanism::is_poll_address((address)info->si_addr)) {254stub = SharedRuntime::get_poll_stub(pc);255#if defined(__APPLE__)256// 32-bit Darwin reports a SIGBUS for nearly all memory access exceptions.257// 64-bit Darwin may also use a SIGBUS (seen with compressed oops).258// Catching SIGBUS here prevents the implicit SIGBUS NULL check below from259// being called, so only do so if the implicit NULL check is not necessary.260} else if (sig == SIGBUS && !MacroAssembler::uses_implicit_null_check(info->si_addr)) {261#else262} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {263#endif264// BugId 4454115: A read from a MappedByteBuffer can fault265// here if the underlying file has been truncated.266// Do not crash the VM in such a case.267CodeBlob* cb = CodeCache::find_blob_unsafe(pc);268CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;269bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc));270if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {271address next_pc = pc + NativeCall::instruction_size;272if (is_unsafe_arraycopy) {273next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);274}275stub = SharedRuntime::handle_unsafe_access(thread, next_pc);276}277} else if (sig == SIGILL && nativeInstruction_at(pc)->is_stop()) {278// Pull a pointer to the error message out of the instruction279// stream.280const uint64_t *detail_msg_ptr281= (uint64_t*)(pc + NativeInstruction::instruction_size);282const char *detail_msg = (const char *)*detail_msg_ptr;283const char *msg = "stop";284if (TraceTraps) {285tty->print_cr("trap: %s: (SIGILL)", msg);286}287288// End life with a fatal error, message and detail message and the context.289// Note: no need to do any post-processing here (e.g. signal chaining)290report_and_die(thread, uc, NULL, 0, msg, "%s", detail_msg);291ShouldNotReachHere();292293} else if (sig == SIGFPE &&294(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {295stub =296SharedRuntime::297continuation_for_implicit_exception(thread,298pc,299SharedRuntime::300IMPLICIT_DIVIDE_BY_ZERO);301} else if ((sig == SIGSEGV || sig == SIGBUS) &&302MacroAssembler::uses_implicit_null_check(info->si_addr)) {303// Determination of interpreter/vtable stub/compiled code null exception304stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);305}306} else if ((thread->thread_state() == _thread_in_vm ||307thread->thread_state() == _thread_in_native) &&308sig == SIGBUS && /* info->si_code == BUS_OBJERR && */309thread->doing_unsafe_access()) {310address next_pc = pc + NativeCall::instruction_size;311if (UnsafeCopyMemory::contains_pc(pc)) {312next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);313}314stub = SharedRuntime::handle_unsafe_access(thread, next_pc);315}316317// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in318// and the heap gets shrunk before the field access.319if ((sig == SIGSEGV) || (sig == SIGBUS)) {320address addr = JNI_FastGetField::find_slowcase_pc(pc);321if (addr != (address)-1) {322stub = addr;323}324}325}326327if (stub != NULL) {328// save all thread context in case we need to restore it329if (thread != NULL) thread->set_saved_exception_pc(pc);330331os::Posix::ucontext_set_pc(uc, stub);332return true;333}334335return false; // Mute compiler336}337338void os::Bsd::init_thread_fpu_state(void) {339}340341bool os::is_allocatable(size_t bytes) {342return true;343}344345////////////////////////////////////////////////////////////////////////////////346// thread stack347348// Minimum usable stack sizes required to get to user code. Space for349// HotSpot guard pages is added later.350size_t os::Posix::_compiler_thread_min_stack_allowed = 72 * K;351size_t os::Posix::_java_thread_min_stack_allowed = 72 * K;352size_t os::Posix::_vm_internal_thread_min_stack_allowed = 72 * K;353354// return default stack size for thr_type355size_t os::Posix::default_stack_size(os::ThreadType thr_type) {356// default stack size (compiler thread needs larger stack)357size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);358return s;359}360361static void current_stack_region(address * bottom, size_t * size) {362#ifdef __APPLE__363pthread_t self = pthread_self();364void *stacktop = pthread_get_stackaddr_np(self);365*size = pthread_get_stacksize_np(self);366*bottom = (address) stacktop - *size;367#elif defined(__OpenBSD__)368stack_t ss;369int rslt = pthread_stackseg_np(pthread_self(), &ss);370371if (rslt != 0)372fatal("pthread_stackseg_np failed with error = %d", rslt);373374*bottom = (address)((char *)ss.ss_sp - ss.ss_size);375*size = ss.ss_size;376#else377pthread_attr_t attr;378379int rslt = pthread_attr_init(&attr);380381// JVM needs to know exact stack location, abort if it fails382if (rslt != 0)383fatal("pthread_attr_init failed with error = %d", rslt);384385rslt = pthread_attr_get_np(pthread_self(), &attr);386387if (rslt != 0)388fatal("pthread_attr_get_np failed with error = %d", rslt);389390if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 ||391pthread_attr_getstacksize(&attr, size) != 0) {392fatal("Can not locate current stack attributes!");393}394395pthread_attr_destroy(&attr);396#endif397assert(os::current_stack_pointer() >= *bottom &&398os::current_stack_pointer() < *bottom + *size, "just checking");399}400401address os::current_stack_base() {402address bottom;403size_t size;404current_stack_region(&bottom, &size);405return (bottom + size);406}407408size_t os::current_stack_size() {409// stack size includes normal stack and HotSpot guard pages410address bottom;411size_t size;412current_stack_region(&bottom, &size);413return size;414}415416/////////////////////////////////////////////////////////////////////////////417// helper functions for fatal error handler418419void os::print_context(outputStream *st, const void *context) {420if (context == NULL) return;421422const ucontext_t *uc = (const ucontext_t*)context;423st->print_cr("Registers:");424st->print( " x0=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 0]);425st->print(" x1=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 1]);426st->print(" x2=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 2]);427st->print(" x3=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 3]);428st->cr();429st->print( " x4=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 4]);430st->print(" x5=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 5]);431st->print(" x6=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 6]);432st->print(" x7=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 7]);433st->cr();434st->print( " x8=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 8]);435st->print(" x9=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 9]);436st->print(" x10=" INTPTR_FORMAT, (intptr_t)uc->context_x[10]);437st->print(" x11=" INTPTR_FORMAT, (intptr_t)uc->context_x[11]);438st->cr();439st->print( "x12=" INTPTR_FORMAT, (intptr_t)uc->context_x[12]);440st->print(" x13=" INTPTR_FORMAT, (intptr_t)uc->context_x[13]);441st->print(" x14=" INTPTR_FORMAT, (intptr_t)uc->context_x[14]);442st->print(" x15=" INTPTR_FORMAT, (intptr_t)uc->context_x[15]);443st->cr();444st->print( "x16=" INTPTR_FORMAT, (intptr_t)uc->context_x[16]);445st->print(" x17=" INTPTR_FORMAT, (intptr_t)uc->context_x[17]);446st->print(" x18=" INTPTR_FORMAT, (intptr_t)uc->context_x[18]);447st->print(" x19=" INTPTR_FORMAT, (intptr_t)uc->context_x[19]);448st->cr();449st->print( "x20=" INTPTR_FORMAT, (intptr_t)uc->context_x[20]);450st->print(" x21=" INTPTR_FORMAT, (intptr_t)uc->context_x[21]);451st->print(" x22=" INTPTR_FORMAT, (intptr_t)uc->context_x[22]);452st->print(" x23=" INTPTR_FORMAT, (intptr_t)uc->context_x[23]);453st->cr();454st->print( "x24=" INTPTR_FORMAT, (intptr_t)uc->context_x[24]);455st->print(" x25=" INTPTR_FORMAT, (intptr_t)uc->context_x[25]);456st->print(" x26=" INTPTR_FORMAT, (intptr_t)uc->context_x[26]);457st->print(" x27=" INTPTR_FORMAT, (intptr_t)uc->context_x[27]);458st->cr();459st->print( "x28=" INTPTR_FORMAT, (intptr_t)uc->context_x[28]);460st->print(" fp=" INTPTR_FORMAT, (intptr_t)uc->context_fp);461st->print(" lr=" INTPTR_FORMAT, (intptr_t)uc->context_lr);462st->print(" sp=" INTPTR_FORMAT, (intptr_t)uc->context_sp);463st->cr();464st->print( "pc=" INTPTR_FORMAT, (intptr_t)uc->context_pc);465st->print(" cpsr=" INTPTR_FORMAT, (intptr_t)uc->context_cpsr);466st->cr();467468intptr_t *sp = (intptr_t *)os::Bsd::ucontext_get_sp(uc);469st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", (intptr_t)sp);470print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));471st->cr();472473// Note: it may be unsafe to inspect memory near pc. For example, pc may474// point to garbage if entry point in an nmethod is corrupted. Leave475// this at the end, and hope for the best.476address pc = os::Posix::ucontext_get_pc(uc);477print_instructions(st, pc, 4/*native instruction size*/);478st->cr();479}480481void os::print_register_info(outputStream *st, const void *context) {482if (context == NULL) return;483484const ucontext_t *uc = (const ucontext_t*)context;485486st->print_cr("Register to memory mapping:");487st->cr();488489// this is horrendously verbose but the layout of the registers in the490// context does not match how we defined our abstract Register set, so491// we can't just iterate through the gregs area492493// this is only for the "general purpose" registers494495st->print(" x0="); print_location(st, uc->context_x[ 0]);496st->print(" x1="); print_location(st, uc->context_x[ 1]);497st->print(" x2="); print_location(st, uc->context_x[ 2]);498st->print(" x3="); print_location(st, uc->context_x[ 3]);499st->print(" x4="); print_location(st, uc->context_x[ 4]);500st->print(" x5="); print_location(st, uc->context_x[ 5]);501st->print(" x6="); print_location(st, uc->context_x[ 6]);502st->print(" x7="); print_location(st, uc->context_x[ 7]);503st->print(" x8="); print_location(st, uc->context_x[ 8]);504st->print(" x9="); print_location(st, uc->context_x[ 9]);505st->print("x10="); print_location(st, uc->context_x[10]);506st->print("x11="); print_location(st, uc->context_x[11]);507st->print("x12="); print_location(st, uc->context_x[12]);508st->print("x13="); print_location(st, uc->context_x[13]);509st->print("x14="); print_location(st, uc->context_x[14]);510st->print("x15="); print_location(st, uc->context_x[15]);511st->print("x16="); print_location(st, uc->context_x[16]);512st->print("x17="); print_location(st, uc->context_x[17]);513st->print("x18="); print_location(st, uc->context_x[18]);514st->print("x19="); print_location(st, uc->context_x[19]);515st->print("x20="); print_location(st, uc->context_x[20]);516st->print("x21="); print_location(st, uc->context_x[21]);517st->print("x22="); print_location(st, uc->context_x[22]);518st->print("x23="); print_location(st, uc->context_x[23]);519st->print("x24="); print_location(st, uc->context_x[24]);520st->print("x25="); print_location(st, uc->context_x[25]);521st->print("x26="); print_location(st, uc->context_x[26]);522st->print("x27="); print_location(st, uc->context_x[27]);523st->print("x28="); print_location(st, uc->context_x[28]);524525st->cr();526}527528void os::setup_fpu() {529}530531#ifndef PRODUCT532void os::verify_stack_alignment() {533assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");534}535#endif536537int os::extra_bang_size_in_bytes() {538// AArch64 does not require the additional stack bang.539return 0;540}541542void os::current_thread_enable_wx(WXMode mode) {543if (os::Bsd::isRWXJITAvailable()) {544jit_write_protect(mode == WXExec);545}546}547548extern "C" {549int SpinPause() {550return 0;551}552553void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {554if (from > to) {555const jshort *end = from + count;556while (from < end)557*(to++) = *(from++);558}559else if (from < to) {560const jshort *end = from;561from += count - 1;562to += count - 1;563while (from >= end)564*(to--) = *(from--);565}566}567void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {568if (from > to) {569const jint *end = from + count;570while (from < end)571*(to++) = *(from++);572}573else if (from < to) {574const jint *end = from;575from += count - 1;576to += count - 1;577while (from >= end)578*(to--) = *(from--);579}580}581void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {582if (from > to) {583const jlong *end = from + count;584while (from < end)585os::atomic_copy64(from++, to++);586}587else if (from < to) {588const jlong *end = from;589from += count - 1;590to += count - 1;591while (from >= end)592os::atomic_copy64(from--, to--);593}594}595596void _Copy_arrayof_conjoint_bytes(const HeapWord* from,597HeapWord* to,598size_t count) {599memmove(to, from, count);600}601void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,602HeapWord* to,603size_t count) {604memmove(to, from, count * 2);605}606void _Copy_arrayof_conjoint_jints(const HeapWord* from,607HeapWord* to,608size_t count) {609memmove(to, from, count * 4);610}611void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,612HeapWord* to,613size_t count) {614memmove(to, from, count * 8);615}616};617618619