Path: blob/master/src/hotspot/os_cpu/linux_ppc/os_linux_ppc.cpp
40951 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2012, 2021 SAP SE. All rights reserved.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 "assembler_ppc.hpp"28#include "asm/assembler.inline.hpp"29#include "classfile/vmSymbols.hpp"30#include "code/codeCache.hpp"31#include "code/icBuffer.hpp"32#include "code/vtableStubs.hpp"33#include "interpreter/interpreter.hpp"34#include "memory/allocation.inline.hpp"35#include "nativeInst_ppc.hpp"36#include "os_share_linux.hpp"37#include "prims/jniFastGetField.hpp"38#include "prims/jvm_misc.hpp"39#include "runtime/arguments.hpp"40#include "runtime/frame.inline.hpp"41#include "runtime/interfaceSupport.inline.hpp"42#include "runtime/java.hpp"43#include "runtime/javaCalls.hpp"44#include "runtime/mutexLocker.hpp"45#include "runtime/osThread.hpp"46#include "runtime/safepointMechanism.hpp"47#include "runtime/sharedRuntime.hpp"48#include "runtime/stubRoutines.hpp"49#include "runtime/thread.inline.hpp"50#include "runtime/timer.hpp"51#include "runtime/vm_version.hpp"52#include "signals_posix.hpp"53#include "utilities/debug.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 <pthread.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# include <ucontext.h>777879address os::current_stack_pointer() {80return (address)__builtin_frame_address(0);81}8283char* os::non_memory_address_word() {84// Must never look like an address returned by reserve_memory,85// even in its subfields (as defined by the CPU immediate fields,86// if the CPU splits constants across multiple instructions).8788return (char*) -1;89}9091// Frame information (pc, sp, fp) retrieved via ucontext92// always looks like a C-frame according to the frame93// conventions in frame_ppc64.hpp.94address os::Posix::ucontext_get_pc(const ucontext_t * uc) {95// On powerpc64, ucontext_t is not selfcontained but contains96// a pointer to an optional substructure (mcontext_t.regs) containing the volatile97// registers - NIP, among others.98// This substructure may or may not be there depending where uc came from:99// - if uc was handed over as the argument to a sigaction handler, a pointer to the100// substructure was provided by the kernel when calling the signal handler, and101// regs->nip can be accessed.102// - if uc was filled by getcontext(), it is undefined - getcontext() does not fill103// it because the volatile registers are not needed to make setcontext() work.104// Hopefully it was zero'd out beforehand.105guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_get_pc in sigaction context");106return (address)uc->uc_mcontext.regs->nip;107}108109// modify PC in ucontext.110// Note: Only use this for an ucontext handed down to a signal handler. See comment111// in ucontext_get_pc.112void os::Posix::ucontext_set_pc(ucontext_t * uc, address pc) {113guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_set_pc in sigaction context");114uc->uc_mcontext.regs->nip = (unsigned long)pc;115}116117static address ucontext_get_lr(const ucontext_t * uc) {118return (address)uc->uc_mcontext.regs->link;119}120121intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {122return (intptr_t*)uc->uc_mcontext.regs->gpr[1/*REG_SP*/];123}124125intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {126return NULL;127}128129static unsigned long ucontext_get_trap(const ucontext_t * uc) {130return uc->uc_mcontext.regs->trap;131}132133address os::fetch_frame_from_context(const void* ucVoid,134intptr_t** ret_sp, intptr_t** ret_fp) {135136address epc;137const ucontext_t* uc = (const ucontext_t*)ucVoid;138139if (uc != NULL) {140epc = os::Posix::ucontext_get_pc(uc);141if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);142if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);143} else {144epc = NULL;145if (ret_sp) *ret_sp = (intptr_t *)NULL;146if (ret_fp) *ret_fp = (intptr_t *)NULL;147}148149return epc;150}151152frame os::fetch_frame_from_context(const void* ucVoid) {153intptr_t* sp;154intptr_t* fp;155address epc = fetch_frame_from_context(ucVoid, &sp, &fp);156return frame(sp, epc);157}158159frame os::fetch_compiled_frame_from_context(const void* ucVoid) {160const ucontext_t* uc = (const ucontext_t*)ucVoid;161intptr_t* sp = os::Linux::ucontext_get_sp(uc);162address lr = ucontext_get_lr(uc);163return frame(sp, lr);164}165166frame os::get_sender_for_C_frame(frame* fr) {167if (*fr->sp() == 0) {168// fr is the last C frame169return frame(NULL, NULL);170}171return frame(fr->sender_sp(), fr->sender_pc());172}173174175frame os::current_frame() {176intptr_t* csp = *(intptr_t**) __builtin_frame_address(0);177frame topframe(csp, CAST_FROM_FN_PTR(address, os::current_frame));178return os::get_sender_for_C_frame(&topframe);179}180181bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,182ucontext_t* uc, JavaThread* thread) {183184// Make the signal handler transaction-aware by checking the existence of a185// second (transactional) context with MSR TS bits active. If the signal is186// caught during a transaction, then just return to the HTM abort handler.187// Please refer to Linux kernel document powerpc/transactional_memory.txt,188// section "Signals".189if (uc && uc->uc_link) {190ucontext_t* second_uc = uc->uc_link;191192// MSR TS bits are 29 and 30 (Power ISA, v2.07B, Book III-S, pp. 857-858,193// 3.2.1 "Machine State Register"), however note that ISA notation for bit194// numbering is MSB 0, so for normal bit numbering (LSB 0) they come to be195// bits 33 and 34. It's not related to endianness, just a notation matter.196if (second_uc->uc_mcontext.regs->msr & 0x600000000) {197if (TraceTraps) {198tty->print_cr("caught signal in transaction, "199"ignoring to jump to abort handler");200}201// Return control to the HTM abort handler.202return true;203}204}205206// decide if this trap can be handled by a stub207address stub = NULL;208address pc = NULL;209210if (info != NULL && uc != NULL && thread != NULL) {211pc = (address) os::Posix::ucontext_get_pc(uc);212213// Handle ALL stack overflow variations here214if (sig == SIGSEGV) {215// si_addr may not be valid due to a bug in the linux-ppc64 kernel (see216// comment below). Use get_stack_bang_address instead of si_addr.217// If SIGSEGV is caused due to a branch to an invalid address an218// "Instruction Storage Interrupt" is generated and 'pc' (NIP) already219// contains the invalid address. Otherwise, the SIGSEGV is caused due to220// load/store instruction trying to load/store from/to an invalid address221// and causing a "Data Storage Interrupt", so we inspect the intruction222// in order to extract the faulty data addresss.223address addr;224if ((ucontext_get_trap(uc) & 0x0F00 /* no IRQ reply bits */) == 0x0400) {225// Instruction Storage Interrupt (ISI)226addr = pc;227} else {228// Data Storage Interrupt (DSI), i.e. 0x0300: extract faulty data address229addr = ((NativeInstruction*)pc)->get_stack_bang_address(uc);230}231232// Check if fault address is within thread stack.233if (thread->is_in_full_stack(addr)) {234// stack overflow235if (os::Posix::handle_stack_overflow(thread, addr, pc, uc, &stub)) {236return true; // continue237}238}239}240241if (thread->thread_state() == _thread_in_Java) {242// Java thread running in Java code => find exception handler if any243// a fault inside compiled code, the interpreter, or a stub244245CodeBlob *cb = NULL;246int stop_type = -1;247// Handle signal from NativeJump::patch_verified_entry().248if (sig == SIGILL && 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}254255else if ((sig == USE_POLL_BIT_ONLY ? SIGTRAP : SIGSEGV) &&256// A linux-ppc64 kernel before 2.6.6 doesn't set si_addr on some segfaults257// in 64bit mode (cf. http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.6),258// especially when we try to read from the safepoint polling page. So the check259// (address)info->si_addr == os::get_standard_polling_page()260// doesn't work for us. We use:261((NativeInstruction*)pc)->is_safepoint_poll() &&262CodeCache::contains((void*) pc) &&263((cb = CodeCache::find_blob(pc)) != NULL) &&264cb->is_compiled()) {265if (TraceTraps) {266tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (%s)", p2i(pc),267USE_POLL_BIT_ONLY ? "SIGTRAP" : "SIGSEGV");268}269stub = SharedRuntime::get_poll_stub(pc);270}271272else if (UseSIGTRAP && sig == SIGTRAP &&273((NativeInstruction*)pc)->is_safepoint_poll_return() &&274CodeCache::contains((void*) pc) &&275((cb = CodeCache::find_blob(pc)) != NULL) &&276cb->is_compiled()) {277if (TraceTraps) {278tty->print_cr("trap: safepoint_poll at return at " INTPTR_FORMAT " (nmethod)", p2i(pc));279}280stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();281}282283// SIGTRAP-based ic miss check in compiled code.284else if (sig == SIGTRAP && TrapBasedICMissChecks &&285nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {286if (TraceTraps) {287tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));288}289stub = SharedRuntime::get_ic_miss_stub();290}291292// SIGTRAP-based implicit null check in compiled code.293else if (sig == SIGTRAP && TrapBasedNullChecks &&294nativeInstruction_at(pc)->is_sigtrap_null_check()) {295if (TraceTraps) {296tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));297}298stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);299}300301// SIGSEGV-based implicit null check in compiled code.302else if (sig == SIGSEGV && ImplicitNullChecks &&303CodeCache::contains((void*) pc) &&304MacroAssembler::uses_implicit_null_check(info->si_addr)) {305if (TraceTraps) {306tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));307}308stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);309}310311#ifdef COMPILER2312// SIGTRAP-based implicit range check in compiled code.313else if (sig == SIGTRAP && TrapBasedRangeChecks &&314nativeInstruction_at(pc)->is_sigtrap_range_check()) {315if (TraceTraps) {316tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));317}318stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);319}320#endif321322// stop on request323else if (sig == SIGTRAP && (stop_type = nativeInstruction_at(pc)->get_stop_type()) != -1) {324bool msg_present = (stop_type & MacroAssembler::stop_msg_present);325stop_type = (stop_type &~ MacroAssembler::stop_msg_present);326327const char *msg = NULL;328switch (stop_type) {329case MacroAssembler::stop_stop : msg = "stop"; break;330case MacroAssembler::stop_untested : msg = "untested"; break;331case MacroAssembler::stop_unimplemented : msg = "unimplemented"; break;332case MacroAssembler::stop_shouldnotreachhere: msg = "shouldnotreachhere"; break;333default: msg = "unknown"; break;334}335336const char **detail_msg_ptr = (const char**)(pc + 4);337const char *detail_msg = msg_present ? *detail_msg_ptr : "no details provided";338339if (TraceTraps) {340tty->print_cr("trap: %s: %s (SIGTRAP, stop type %d)", msg, detail_msg, stop_type);341}342343// End life with a fatal error, message and detail message and the context.344// Note: no need to do any post-processing here (e.g. signal chaining)345va_list va_dummy;346VMError::report_and_die(thread, uc, NULL, 0, msg, detail_msg, va_dummy);347va_end(va_dummy);348349ShouldNotReachHere();350351}352353else if (sig == SIGBUS) {354// BugId 4454115: A read from a MappedByteBuffer can fault here if the355// underlying file has been truncated. Do not crash the VM in such a case.356CodeBlob* cb = CodeCache::find_blob_unsafe(pc);357CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;358bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc));359if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {360address next_pc = pc + 4;361if (is_unsafe_arraycopy) {362next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);363}364next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);365os::Posix::ucontext_set_pc(uc, next_pc);366return true;367}368}369}370371else { // thread->thread_state() != _thread_in_Java372if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {373// SIGILL must be caused by VM_Version::determine_features().374*(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,375// flushing of icache is not necessary.376stub = pc + 4; // continue with next instruction.377}378else if ((thread->thread_state() == _thread_in_vm ||379thread->thread_state() == _thread_in_native) &&380sig == SIGBUS && thread->doing_unsafe_access()) {381address next_pc = pc + 4;382if (UnsafeCopyMemory::contains_pc(pc)) {383next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);384}385next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);386os::Posix::ucontext_set_pc(uc, next_pc);387return true;388}389}390391// jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in392// and the heap gets shrunk before the field access.393if ((sig == SIGSEGV) || (sig == SIGBUS)) {394address addr = JNI_FastGetField::find_slowcase_pc(pc);395if (addr != (address)-1) {396stub = addr;397}398}399}400401if (stub != NULL) {402// Save all thread context in case we need to restore it.403if (thread != NULL) thread->set_saved_exception_pc(pc);404os::Posix::ucontext_set_pc(uc, stub);405return true;406}407408return false;409}410411void os::Linux::init_thread_fpu_state(void) {412// Disable FP exceptions.413__asm__ __volatile__ ("mtfsfi 6,0");414}415416int os::Linux::get_fpu_control_word(void) {417// x86 has problems with FPU precision after pthread_cond_timedwait().418// nothing to do on ppc64.419return 0;420}421422void os::Linux::set_fpu_control_word(int fpu_control) {423// x86 has problems with FPU precision after pthread_cond_timedwait().424// nothing to do on ppc64.425}426427////////////////////////////////////////////////////////////////////////////////428// thread stack429430// Minimum usable stack sizes required to get to user code. Space for431// HotSpot guard pages is added later.432size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K;433size_t os::Posix::_java_thread_min_stack_allowed = 64 * K;434size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;435436// Return default stack size for thr_type.437size_t os::Posix::default_stack_size(os::ThreadType thr_type) {438// Default stack size (compiler thread needs larger stack).439size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);440return s;441}442443/////////////////////////////////////////////////////////////////////////////444// helper functions for fatal error handler445446void os::print_context(outputStream *st, const void *context) {447if (context == NULL) return;448449const ucontext_t* uc = (const ucontext_t*)context;450451st->print_cr("Registers:");452st->print("pc =" INTPTR_FORMAT " ", uc->uc_mcontext.regs->nip);453st->print("lr =" INTPTR_FORMAT " ", uc->uc_mcontext.regs->link);454st->print("ctr=" INTPTR_FORMAT " ", uc->uc_mcontext.regs->ctr);455st->cr();456for (int i = 0; i < 32; i++) {457st->print("r%-2d=" INTPTR_FORMAT " ", i, uc->uc_mcontext.regs->gpr[i]);458if (i % 3 == 2) st->cr();459}460st->cr();461st->cr();462463intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);464st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));465print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));466st->cr();467468// Note: it may be unsafe to inspect memory near pc. For example, pc may469// point to garbage if entry point in an nmethod is corrupted. Leave470// this at the end, and hope for the best.471address pc = os::Posix::ucontext_get_pc(uc);472print_instructions(st, pc, /*instrsize=*/4);473st->cr();474}475476void os::print_register_info(outputStream *st, const void *context) {477if (context == NULL) return;478479const ucontext_t *uc = (const ucontext_t*)context;480481st->print_cr("Register to memory mapping:");482st->cr();483484st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->nip);485st->print("lr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->link);486st->print("ctr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->ctr);487for (int i = 0; i < 32; i++) {488st->print("r%-2d=", i);489print_location(st, uc->uc_mcontext.regs->gpr[i]);490}491st->cr();492}493494extern "C" {495int SpinPause() {496return 0;497}498}499500#ifndef PRODUCT501void os::verify_stack_alignment() {502assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");503}504#endif505506int os::extra_bang_size_in_bytes() {507// PPC does not require the additional stack bang.508return 0;509}510511#ifdef HAVE_FUNCTION_DESCRIPTORS512void* os::resolve_function_descriptor(void* p) {513return ((const FunctionDescriptor*)p)->entry();514}515#endif516517518