Path: blob/master/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp
40930 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"5657// put OS-includes here58# include <sys/types.h>59# include <sys/mman.h>60# include <pthread.h>61# include <signal.h>62# include <errno.h>63# include <dlfcn.h>64# include <stdlib.h>65# include <stdio.h>66# include <unistd.h>67# include <sys/resource.h>68# include <sys/stat.h>69# include <sys/time.h>70# include <sys/utsname.h>71# include <sys/socket.h>72# include <sys/wait.h>73# include <pwd.h>74# include <poll.h>75#ifndef __OpenBSD__76# include <ucontext.h>77#endif7879#if !defined(__APPLE__) && !defined(__NetBSD__)80# include <pthread_np.h>81#endif8283#define SPELL_REG_SP "sp"84#define SPELL_REG_FP "fp"8586#ifdef __APPLE__87// see darwin-xnu/osfmk/mach/arm/_structs.h8889// 10.5 UNIX03 member name prefixes90#define DU3_PREFIX(s, m) __ ## s.__ ## m91#endif9293#define context_x uc_mcontext->DU3_PREFIX(ss,x)94#define context_fp uc_mcontext->DU3_PREFIX(ss,fp)95#define context_lr uc_mcontext->DU3_PREFIX(ss,lr)96#define context_sp uc_mcontext->DU3_PREFIX(ss,sp)97#define context_pc uc_mcontext->DU3_PREFIX(ss,pc)98#define context_cpsr uc_mcontext->DU3_PREFIX(ss,cpsr)99#define context_esr uc_mcontext->DU3_PREFIX(es,esr)100101address os::current_stack_pointer() {102#if defined(__clang__) || defined(__llvm__)103void *sp;104__asm__("mov %0, " SPELL_REG_SP : "=r"(sp));105return (address) sp;106#else107register void *sp __asm__ (SPELL_REG_SP);108return (address) sp;109#endif110}111112char* os::non_memory_address_word() {113// Must never look like an address returned by reserve_memory,114// even in its subfields (as defined by the CPU immediate fields,115// if the CPU splits constants across multiple instructions).116117// the return value used in computation of Universe::non_oop_word(), which118// is loaded by cpu/aarch64 by MacroAssembler::movptr(Register, uintptr_t)119return (char*) 0xffffffffffff;120}121122address os::Posix::ucontext_get_pc(const ucontext_t * uc) {123return (address)uc->context_pc;124}125126void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {127uc->context_pc = (intptr_t)pc ;128}129130intptr_t* os::Bsd::ucontext_get_sp(const ucontext_t * uc) {131return (intptr_t*)uc->context_sp;132}133134intptr_t* os::Bsd::ucontext_get_fp(const ucontext_t * uc) {135return (intptr_t*)uc->context_fp;136}137138address os::fetch_frame_from_context(const void* ucVoid,139intptr_t** ret_sp, intptr_t** ret_fp) {140141address epc;142const ucontext_t* uc = (const ucontext_t*)ucVoid;143144if (uc != NULL) {145epc = os::Posix::ucontext_get_pc(uc);146if (ret_sp) *ret_sp = os::Bsd::ucontext_get_sp(uc);147if (ret_fp) *ret_fp = os::Bsd::ucontext_get_fp(uc);148} else {149epc = NULL;150if (ret_sp) *ret_sp = (intptr_t *)NULL;151if (ret_fp) *ret_fp = (intptr_t *)NULL;152}153154return epc;155}156157frame os::fetch_frame_from_context(const void* ucVoid) {158intptr_t* sp;159intptr_t* fp;160address epc = fetch_frame_from_context(ucVoid, &sp, &fp);161return frame(sp, fp, epc);162}163164frame os::fetch_compiled_frame_from_context(const void* ucVoid) {165const ucontext_t* uc = (const ucontext_t*)ucVoid;166// In compiled code, the stack banging is performed before LR167// has been saved in the frame. LR is live, and SP and FP168// belong to the caller.169intptr_t* fp = os::Bsd::ucontext_get_fp(uc);170intptr_t* sp = os::Bsd::ucontext_get_sp(uc);171address pc = (address)(uc->context_lr172- NativeInstruction::instruction_size);173return frame(sp, fp, pc);174}175176// JVM compiled with -fno-omit-frame-pointer, so RFP is saved on the stack.177frame os::get_sender_for_C_frame(frame* fr) {178return frame(fr->link(), fr->link(), fr->sender_pc());179}180181NOINLINE frame os::current_frame() {182intptr_t *fp = *(intptr_t **)__builtin_frame_address(0);183frame myframe((intptr_t*)os::current_stack_pointer(),184(intptr_t*)fp,185CAST_FROM_FN_PTR(address, os::current_frame));186if (os::is_first_C_frame(&myframe)) {187// stack is not walkable188return frame();189} else {190return os::get_sender_for_C_frame(&myframe);191}192}193194ATTRIBUTE_PRINTF(6, 7)195static void report_and_die(Thread* thread, void* context, const char* filename, int lineno, const char* message,196const char* detail_fmt, ...) {197va_list va;198va_start(va, detail_fmt);199VMError::report_and_die(thread, context, filename, lineno, message, detail_fmt, va);200va_end(va);201}202203bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,204ucontext_t* uc, JavaThread* thread) {205// Enable WXWrite: this function is called by the signal handler at arbitrary206// point of execution.207ThreadWXEnable wx(WXWrite, thread);208209// decide if this trap can be handled by a stub210address stub = NULL;211212address pc = NULL;213214//%note os_trap_1215if (info != NULL && uc != NULL && thread != NULL) {216pc = (address) os::Posix::ucontext_get_pc(uc);217218// Handle ALL stack overflow variations here219if (sig == SIGSEGV || sig == SIGBUS) {220address addr = (address) info->si_addr;221222// Make sure the high order byte is sign extended, as it may be masked away by the hardware.223if ((uintptr_t(addr) & (uintptr_t(1) << 55)) != 0) {224addr = address(uintptr_t(addr) | (uintptr_t(0xFF) << 56));225}226227// check if fault address is within thread stack228if (thread->is_in_full_stack(addr)) {229// stack overflow230if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {231return true; // continue232}233}234}235236// We test if stub is already set (by the stack overflow code237// above) so it is not overwritten by the code that follows. This238// check is not required on other platforms, because on other239// platforms we check for SIGSEGV only or SIGBUS only, where here240// we have to check for both SIGSEGV and SIGBUS.241if (thread->thread_state() == _thread_in_Java && stub == NULL) {242// Java thread running in Java code => find exception handler if any243// a fault inside compiled code, the interpreter, or a stub244245// Handle signal from NativeJump::patch_verified_entry().246if ((sig == SIGILL)247&& nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {248if (TraceTraps) {249tty->print_cr("trap: zombie_not_entrant");250}251stub = SharedRuntime::get_handle_wrong_method_stub();252} else if ((sig == SIGSEGV || sig == SIGBUS) && SafepointMechanism::is_poll_address((address)info->si_addr)) {253stub = SharedRuntime::get_poll_stub(pc);254#if defined(__APPLE__)255// 32-bit Darwin reports a SIGBUS for nearly all memory access exceptions.256// 64-bit Darwin may also use a SIGBUS (seen with compressed oops).257// Catching SIGBUS here prevents the implicit SIGBUS NULL check below from258// being called, so only do so if the implicit NULL check is not necessary.259} else if (sig == SIGBUS && !MacroAssembler::uses_implicit_null_check(info->si_addr)) {260#else261} else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {262#endif263// BugId 4454115: A read from a MappedByteBuffer can fault264// here if the underlying file has been truncated.265// Do not crash the VM in such a case.266CodeBlob* cb = CodeCache::find_blob_unsafe(pc);267CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;268bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc));269if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {270address next_pc = pc + NativeCall::instruction_size;271if (is_unsafe_arraycopy) {272next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);273}274stub = SharedRuntime::handle_unsafe_access(thread, next_pc);275}276} else if (sig == SIGILL && nativeInstruction_at(pc)->is_stop()) {277// Pull a pointer to the error message out of the instruction278// stream.279const uint64_t *detail_msg_ptr280= (uint64_t*)(pc + NativeInstruction::instruction_size);281const char *detail_msg = (const char *)*detail_msg_ptr;282const char *msg = "stop";283if (TraceTraps) {284tty->print_cr("trap: %s: (SIGILL)", msg);285}286287// End life with a fatal error, message and detail message and the context.288// Note: no need to do any post-processing here (e.g. signal chaining)289report_and_die(thread, uc, NULL, 0, msg, "%s", detail_msg);290ShouldNotReachHere();291292} else if (sig == SIGFPE &&293(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {294stub =295SharedRuntime::296continuation_for_implicit_exception(thread,297pc,298SharedRuntime::299IMPLICIT_DIVIDE_BY_ZERO);300} else if ((sig == SIGSEGV || sig == SIGBUS) &&301MacroAssembler::uses_implicit_null_check(info->si_addr)) {302// Determination of interpreter/vtable stub/compiled code null exception303stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);304}305} else if ((thread->thread_state() == _thread_in_vm ||306thread->thread_state() == _thread_in_native) &&307sig == SIGBUS && /* info->si_code == BUS_OBJERR && */308thread->doing_unsafe_access()) {309address next_pc = pc + NativeCall::instruction_size;310if (UnsafeCopyMemory::contains_pc(pc)) {311next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);312}313stub = SharedRuntime::handle_unsafe_access(thread, next_pc);314}315316// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in317// and the heap gets shrunk before the field access.318if ((sig == SIGSEGV) || (sig == SIGBUS)) {319address addr = JNI_FastGetField::find_slowcase_pc(pc);320if (addr != (address)-1) {321stub = addr;322}323}324}325326if (stub != NULL) {327// save all thread context in case we need to restore it328if (thread != NULL) thread->set_saved_exception_pc(pc);329330os::Posix::ucontext_set_pc(uc, stub);331return true;332}333334return false; // Mute compiler335}336337void os::Bsd::init_thread_fpu_state(void) {338}339340bool os::is_allocatable(size_t bytes) {341return true;342}343344////////////////////////////////////////////////////////////////////////////////345// thread stack346347// Minimum usable stack sizes required to get to user code. Space for348// HotSpot guard pages is added later.349size_t os::Posix::_compiler_thread_min_stack_allowed = 72 * K;350size_t os::Posix::_java_thread_min_stack_allowed = 72 * K;351size_t os::Posix::_vm_internal_thread_min_stack_allowed = 72 * K;352353// return default stack size for thr_type354size_t os::Posix::default_stack_size(os::ThreadType thr_type) {355// default stack size (compiler thread needs larger stack)356size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);357return s;358}359360static void current_stack_region(address * bottom, size_t * size) {361#ifdef __APPLE__362pthread_t self = pthread_self();363void *stacktop = pthread_get_stackaddr_np(self);364*size = pthread_get_stacksize_np(self);365*bottom = (address) stacktop - *size;366#elif defined(__OpenBSD__)367stack_t ss;368int rslt = pthread_stackseg_np(pthread_self(), &ss);369370if (rslt != 0)371fatal("pthread_stackseg_np failed with error = %d", rslt);372373*bottom = (address)((char *)ss.ss_sp - ss.ss_size);374*size = ss.ss_size;375#else376pthread_attr_t attr;377378int rslt = pthread_attr_init(&attr);379380// JVM needs to know exact stack location, abort if it fails381if (rslt != 0)382fatal("pthread_attr_init failed with error = %d", rslt);383384rslt = pthread_attr_get_np(pthread_self(), &attr);385386if (rslt != 0)387fatal("pthread_attr_get_np failed with error = %d", rslt);388389if (pthread_attr_getstackaddr(&attr, (void **)bottom) != 0 ||390pthread_attr_getstacksize(&attr, size) != 0) {391fatal("Can not locate current stack attributes!");392}393394pthread_attr_destroy(&attr);395#endif396assert(os::current_stack_pointer() >= *bottom &&397os::current_stack_pointer() < *bottom + *size, "just checking");398}399400address os::current_stack_base() {401address bottom;402size_t size;403current_stack_region(&bottom, &size);404return (bottom + size);405}406407size_t os::current_stack_size() {408// stack size includes normal stack and HotSpot guard pages409address bottom;410size_t size;411current_stack_region(&bottom, &size);412return size;413}414415/////////////////////////////////////////////////////////////////////////////416// helper functions for fatal error handler417418void os::print_context(outputStream *st, const void *context) {419if (context == NULL) return;420421const ucontext_t *uc = (const ucontext_t*)context;422st->print_cr("Registers:");423st->print( " x0=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 0]);424st->print(" x1=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 1]);425st->print(" x2=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 2]);426st->print(" x3=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 3]);427st->cr();428st->print( " x4=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 4]);429st->print(" x5=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 5]);430st->print(" x6=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 6]);431st->print(" x7=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 7]);432st->cr();433st->print( " x8=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 8]);434st->print(" x9=" INTPTR_FORMAT, (intptr_t)uc->context_x[ 9]);435st->print(" x10=" INTPTR_FORMAT, (intptr_t)uc->context_x[10]);436st->print(" x11=" INTPTR_FORMAT, (intptr_t)uc->context_x[11]);437st->cr();438st->print( "x12=" INTPTR_FORMAT, (intptr_t)uc->context_x[12]);439st->print(" x13=" INTPTR_FORMAT, (intptr_t)uc->context_x[13]);440st->print(" x14=" INTPTR_FORMAT, (intptr_t)uc->context_x[14]);441st->print(" x15=" INTPTR_FORMAT, (intptr_t)uc->context_x[15]);442st->cr();443st->print( "x16=" INTPTR_FORMAT, (intptr_t)uc->context_x[16]);444st->print(" x17=" INTPTR_FORMAT, (intptr_t)uc->context_x[17]);445st->print(" x18=" INTPTR_FORMAT, (intptr_t)uc->context_x[18]);446st->print(" x19=" INTPTR_FORMAT, (intptr_t)uc->context_x[19]);447st->cr();448st->print( "x20=" INTPTR_FORMAT, (intptr_t)uc->context_x[20]);449st->print(" x21=" INTPTR_FORMAT, (intptr_t)uc->context_x[21]);450st->print(" x22=" INTPTR_FORMAT, (intptr_t)uc->context_x[22]);451st->print(" x23=" INTPTR_FORMAT, (intptr_t)uc->context_x[23]);452st->cr();453st->print( "x24=" INTPTR_FORMAT, (intptr_t)uc->context_x[24]);454st->print(" x25=" INTPTR_FORMAT, (intptr_t)uc->context_x[25]);455st->print(" x26=" INTPTR_FORMAT, (intptr_t)uc->context_x[26]);456st->print(" x27=" INTPTR_FORMAT, (intptr_t)uc->context_x[27]);457st->cr();458st->print( "x28=" INTPTR_FORMAT, (intptr_t)uc->context_x[28]);459st->print(" fp=" INTPTR_FORMAT, (intptr_t)uc->context_fp);460st->print(" lr=" INTPTR_FORMAT, (intptr_t)uc->context_lr);461st->print(" sp=" INTPTR_FORMAT, (intptr_t)uc->context_sp);462st->cr();463st->print( "pc=" INTPTR_FORMAT, (intptr_t)uc->context_pc);464st->print(" cpsr=" INTPTR_FORMAT, (intptr_t)uc->context_cpsr);465st->cr();466467intptr_t *sp = (intptr_t *)os::Bsd::ucontext_get_sp(uc);468st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", (intptr_t)sp);469print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));470st->cr();471472// Note: it may be unsafe to inspect memory near pc. For example, pc may473// point to garbage if entry point in an nmethod is corrupted. Leave474// this at the end, and hope for the best.475address pc = os::Posix::ucontext_get_pc(uc);476print_instructions(st, pc, 4/*native instruction size*/);477st->cr();478}479480void os::print_register_info(outputStream *st, const void *context) {481if (context == NULL) return;482483const ucontext_t *uc = (const ucontext_t*)context;484485st->print_cr("Register to memory mapping:");486st->cr();487488// this is horrendously verbose but the layout of the registers in the489// context does not match how we defined our abstract Register set, so490// we can't just iterate through the gregs area491492// this is only for the "general purpose" registers493494st->print(" x0="); print_location(st, uc->context_x[ 0]);495st->print(" x1="); print_location(st, uc->context_x[ 1]);496st->print(" x2="); print_location(st, uc->context_x[ 2]);497st->print(" x3="); print_location(st, uc->context_x[ 3]);498st->print(" x4="); print_location(st, uc->context_x[ 4]);499st->print(" x5="); print_location(st, uc->context_x[ 5]);500st->print(" x6="); print_location(st, uc->context_x[ 6]);501st->print(" x7="); print_location(st, uc->context_x[ 7]);502st->print(" x8="); print_location(st, uc->context_x[ 8]);503st->print(" x9="); print_location(st, uc->context_x[ 9]);504st->print("x10="); print_location(st, uc->context_x[10]);505st->print("x11="); print_location(st, uc->context_x[11]);506st->print("x12="); print_location(st, uc->context_x[12]);507st->print("x13="); print_location(st, uc->context_x[13]);508st->print("x14="); print_location(st, uc->context_x[14]);509st->print("x15="); print_location(st, uc->context_x[15]);510st->print("x16="); print_location(st, uc->context_x[16]);511st->print("x17="); print_location(st, uc->context_x[17]);512st->print("x18="); print_location(st, uc->context_x[18]);513st->print("x19="); print_location(st, uc->context_x[19]);514st->print("x20="); print_location(st, uc->context_x[20]);515st->print("x21="); print_location(st, uc->context_x[21]);516st->print("x22="); print_location(st, uc->context_x[22]);517st->print("x23="); print_location(st, uc->context_x[23]);518st->print("x24="); print_location(st, uc->context_x[24]);519st->print("x25="); print_location(st, uc->context_x[25]);520st->print("x26="); print_location(st, uc->context_x[26]);521st->print("x27="); print_location(st, uc->context_x[27]);522st->print("x28="); print_location(st, uc->context_x[28]);523524st->cr();525}526527void os::setup_fpu() {528}529530#ifndef PRODUCT531void os::verify_stack_alignment() {532assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");533}534#endif535536int os::extra_bang_size_in_bytes() {537// AArch64 does not require the additional stack bang.538return 0;539}540541void os::current_thread_enable_wx(WXMode mode) {542pthread_jit_write_protect_np(mode == WXExec);543}544545extern "C" {546int SpinPause() {547return 0;548}549550void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {551if (from > to) {552const jshort *end = from + count;553while (from < end)554*(to++) = *(from++);555}556else if (from < to) {557const jshort *end = from;558from += count - 1;559to += count - 1;560while (from >= end)561*(to--) = *(from--);562}563}564void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {565if (from > to) {566const jint *end = from + count;567while (from < end)568*(to++) = *(from++);569}570else if (from < to) {571const jint *end = from;572from += count - 1;573to += count - 1;574while (from >= end)575*(to--) = *(from--);576}577}578void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {579if (from > to) {580const jlong *end = from + count;581while (from < end)582os::atomic_copy64(from++, to++);583}584else if (from < to) {585const jlong *end = from;586from += count - 1;587to += count - 1;588while (from >= end)589os::atomic_copy64(from--, to--);590}591}592593void _Copy_arrayof_conjoint_bytes(const HeapWord* from,594HeapWord* to,595size_t count) {596memmove(to, from, count);597}598void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,599HeapWord* to,600size_t count) {601memmove(to, from, count * 2);602}603void _Copy_arrayof_conjoint_jints(const HeapWord* from,604HeapWord* to,605size_t count) {606memmove(to, from, count * 4);607}608void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,609HeapWord* to,610size_t count) {611memmove(to, from, count * 8);612}613};614615616