aoqi@1: /* aoqi@1: * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@1: * Copyright (c) 2015, 2016, Loongson Technology. All rights reserved. aoqi@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@1: * aoqi@1: * This code is free software; you can redistribute it and/or modify it aoqi@1: * under the terms of the GNU General Public License version 2 only, as aoqi@1: * published by the Free Software Foundation. aoqi@1: * aoqi@1: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@1: * version 2 for more details (a copy is included in the LICENSE file that aoqi@1: * accompanied this code). aoqi@1: * aoqi@1: * You should have received a copy of the GNU General Public License version aoqi@1: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@1: * aoqi@1: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@1: * or visit www.oracle.com if you need additional information or have any aoqi@1: * questions. aoqi@1: * aoqi@1: */ aoqi@1: aoqi@1: // do not include precompiled header file aoqi@1: #include "asm/macroAssembler.hpp" aoqi@1: #include "classfile/classLoader.hpp" aoqi@1: #include "classfile/systemDictionary.hpp" aoqi@1: #include "classfile/vmSymbols.hpp" aoqi@1: #include "code/icBuffer.hpp" aoqi@1: #include "code/vtableStubs.hpp" aoqi@1: #include "interpreter/interpreter.hpp" aoqi@1: #include "jvm_linux.h" aoqi@1: #include "memory/allocation.inline.hpp" aoqi@1: #include "mutex_linux.inline.hpp" aoqi@1: #include "os_share_linux.hpp" aoqi@1: #include "prims/jniFastGetField.hpp" aoqi@1: #include "prims/jvm.h" aoqi@1: #include "prims/jvm_misc.hpp" aoqi@1: #include "runtime/arguments.hpp" aoqi@1: #include "runtime/extendedPC.hpp" aoqi@1: #include "runtime/frame.inline.hpp" aoqi@1: #include "runtime/interfaceSupport.hpp" aoqi@1: #include "runtime/java.hpp" aoqi@1: #include "runtime/javaCalls.hpp" aoqi@1: #include "runtime/mutexLocker.hpp" aoqi@1: #include "runtime/osThread.hpp" aoqi@1: #include "runtime/sharedRuntime.hpp" aoqi@1: #include "runtime/stubRoutines.hpp" aoqi@1: #include "runtime/thread.inline.hpp" aoqi@1: #include "runtime/timer.hpp" aoqi@1: #include "utilities/events.hpp" aoqi@1: #include "utilities/vmError.hpp" aoqi@1: #include "utilities/debug.hpp" aoqi@1: #include "compiler/disassembler.hpp" aoqi@1: // put OS-includes here aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: # include aoqi@1: aoqi@1: #define REG_SP 29 aoqi@1: #define REG_FP 30 aoqi@1: aoqi@1: address os::current_stack_pointer() { aoqi@1: register void *sp __asm__ ("$29"); aoqi@1: return (address) sp; aoqi@1: } aoqi@1: aoqi@1: char* os::non_memory_address_word() { aoqi@1: // Must never look like an address returned by reserve_memory, aoqi@1: // even in its subfields (as defined by the CPU immediate fields, aoqi@1: // if the CPU splits constants across multiple instructions). aoqi@1: aoqi@1: return (char*) -1; aoqi@1: } aoqi@1: aoqi@1: void os::initialize_thread(Thread* thr) { aoqi@1: // Nothing to do. aoqi@1: } aoqi@1: aoqi@1: //the next three method just exists in os::Linux, none in other os, by yjl 6/21/2005 aoqi@1: address os::Linux::ucontext_get_pc(ucontext_t * uc) { aoqi@1: //return (address)uc->uc_mcontext.gregs[REG_PC]; aoqi@1: return (address)uc->uc_mcontext.pc;//aoqi:what is gregs? aoqi@1: } aoqi@1: aoqi@1: intptr_t* os::Linux::ucontext_get_sp(ucontext_t * uc) { aoqi@1: return (intptr_t*)uc->uc_mcontext.gregs[REG_SP]; aoqi@1: } aoqi@1: aoqi@1: intptr_t* os::Linux::ucontext_get_fp(ucontext_t * uc) { aoqi@1: return (intptr_t*)uc->uc_mcontext.gregs[REG_FP]; aoqi@1: } aoqi@1: aoqi@1: // For Forte Analyzer AsyncGetCallTrace profiling support - thread aoqi@1: // is currently interrupted by SIGPROF. aoqi@1: // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal aoqi@1: // frames. Currently we don't do that on Linux, so it's the same as aoqi@1: // os::fetch_frame_from_context(). aoqi@1: ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread, aoqi@1: ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) { aoqi@1: aoqi@1: assert(thread != NULL, "just checking"); aoqi@1: assert(ret_sp != NULL, "just checking"); aoqi@1: assert(ret_fp != NULL, "just checking"); aoqi@1: aoqi@1: return os::fetch_frame_from_context(uc, ret_sp, ret_fp); aoqi@1: } aoqi@1: aoqi@1: ExtendedPC os::fetch_frame_from_context(void* ucVoid, aoqi@1: intptr_t** ret_sp, intptr_t** ret_fp) { aoqi@1: aoqi@1: ExtendedPC epc; aoqi@1: ucontext_t* uc = (ucontext_t*)ucVoid; aoqi@1: aoqi@1: address pc = (address)os::Linux::ucontext_get_pc(uc); aoqi@1: aoqi@1: /* Jin: to capture invalid 32-bit PC, for debbuging */ aoqi@1: if (((long)pc & 0xFFFFFFFF00000000UL) == 0) aoqi@1: { aoqi@1: pc = (address)((long)pc | 0x5500000000UL); aoqi@1: tty->print_cr(" 32-bit pc: %lx", pc); aoqi@1: } aoqi@1: aoqi@1: if (uc != NULL) { aoqi@1: epc = ExtendedPC(pc); aoqi@1: if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc); aoqi@1: if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc); aoqi@1: } else { aoqi@1: // construct empty ExtendedPC for return value checking aoqi@1: epc = ExtendedPC(NULL); aoqi@1: if (ret_sp) *ret_sp = (intptr_t *)NULL; aoqi@1: if (ret_fp) *ret_fp = (intptr_t *)NULL; aoqi@1: } aoqi@1: aoqi@1: return epc; aoqi@1: } aoqi@1: aoqi@1: frame os::fetch_frame_from_context(void* ucVoid) { aoqi@1: intptr_t* sp; aoqi@1: intptr_t* fp; aoqi@1: ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp); aoqi@1: return frame(sp, fp, epc.pc()); aoqi@1: } aoqi@1: aoqi@1: // By default, gcc always save frame pointer (%ebp/%rbp) on stack. It may get aoqi@1: // turned off by -fomit-frame-pointer, aoqi@1: frame os::get_sender_for_C_frame(frame* fr) { aoqi@1: //return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); aoqi@1: //tty->print("c frame sp = 0x%lx, fp=0x%lx, pc=0x%lx \n", (int)fr->sp(),(int)fr->fp(),(int)fr->pc()); aoqi@1: //tty->print("c frame send_sp =0x%lx, fp = 0x%lx, pc = 0x%lx \n", aoqi@1: // (int) fr->sender_sp(), (int) fr->link(), (int)fr->sender_pc()); aoqi@1: return frame(fr->sender_sp(), fr->link(), fr->sender_pc()); aoqi@1: } aoqi@1: aoqi@1: //intptr_t* _get_previous_fp() { aoqi@1: //see StubGenerator::generate_get_previous_fp in stubGenerator_gs2.cpp aoqi@1: jint* os::get_previous_fp() { aoqi@1: int *pc; aoqi@1: int sp; aoqi@1: int *pc_limit = (int*)(void*)&os::get_previous_fp; aoqi@1: int insn; aoqi@1: aoqi@1: { aoqi@1: l_pc:; aoqi@1: pc = (int*)&&l_pc; aoqi@1: __asm__ __volatile__ ("move %0, $sp" : "=r" (sp)); aoqi@1: } aoqi@1: aoqi@1: do { aoqi@1: --pc; aoqi@1: insn = *pc; aoqi@1: switch(bitfield(insn, 16, 16)) { aoqi@1: case 0x27bd: /* addiu $sp,$sp,-i */ aoqi@1: case 0x23bd: /* addi $sp,$sp,-i */ aoqi@1: case 0x67bd: /* daddiu $sp,$sp,-i */ aoqi@1: case 0x63bd: /* daddi $sp,$sp,-i */ aoqi@1: assert ((short)bitfield(insn, 0, 16)<0, "bad frame"); aoqi@1: sp -= (short)bitfield(insn, 0, 16); aoqi@1: return (jint*)sp; aoqi@1: } aoqi@1: } while (pc>pc_limit); aoqi@1: aoqi@1: ShouldNotReachHere(); aoqi@1: } aoqi@1: aoqi@1: frame os::current_frame() { aoqi@1: tty->print("@@@@@@@@@@@@@@@@@@@get_previous_fp = 0x%lx \n", (intptr_t)(get_previous_fp())); aoqi@1: frame myframe((intptr_t*)os::current_stack_pointer(), aoqi@1: (intptr_t*)get_previous_fp(), aoqi@1: CAST_FROM_FN_PTR(address, os::current_frame)); aoqi@1: if (os::is_first_C_frame(&myframe)) { aoqi@1: // stack is not walkable aoqi@1: return frame(NULL, NULL, NULL); aoqi@1: } else { aoqi@1: return os::get_sender_for_C_frame(&myframe); aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: //x86 add 2 new assemble function here! aoqi@1: extern "C" int aoqi@1: JVM_handle_linux_signal(int sig, aoqi@1: siginfo_t* info, aoqi@1: void* ucVoid, aoqi@1: int abort_if_unrecognized) { aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("Signal: signo=%d, sicode=%d, sierrno=%d, siaddr=%lx", aoqi@1: info->si_signo, aoqi@1: info->si_code, aoqi@1: info->si_errno, aoqi@1: info->si_addr); aoqi@1: #endif aoqi@1: aoqi@1: ucontext_t* uc = (ucontext_t*) ucVoid; aoqi@1: aoqi@1: Thread* t = ThreadLocalStorage::get_thread_slow(); aoqi@1: aoqi@1: SignalHandlerMark shm(t); aoqi@1: aoqi@1: // Note: it's not uncommon that JNI code uses signal/sigset to install aoqi@1: // then restore certain signal handler (e.g. to temporarily block SIGPIPE, aoqi@1: // or have a SIGILL handler when detecting CPU type). When that happens, aoqi@1: // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To aoqi@1: // avoid unnecessary crash when libjsig is not preloaded, try handle signals aoqi@1: // that do not require siginfo/ucontext first. aoqi@1: aoqi@1: //if (sig == SIGPIPE || sig == SIGXFSZ) { aoqi@1: if (sig == SIGPIPE) { aoqi@1: // allow chained handler to go first aoqi@1: if (os::Linux::chained_handler(sig, info, ucVoid)) { aoqi@1: return true; aoqi@1: } else { aoqi@1: if (PrintMiscellaneous && (WizardMode || Verbose)) { aoqi@1: warning("Ignoring SIGPIPE - see bug 4229104"); aoqi@1: } aoqi@1: return true; aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: JavaThread* thread = NULL; aoqi@1: VMThread* vmthread = NULL; aoqi@1: if (os::Linux::signal_handlers_are_installed) { aoqi@1: if (t != NULL ){ aoqi@1: if(t->is_Java_thread()) { aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print_cr("this thread is a java thread"); aoqi@1: #endif aoqi@1: thread = (JavaThread*)t; aoqi@1: } aoqi@1: else if(t->is_VM_thread()){ aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print_cr("this thread is a VM thread\n"); aoqi@1: #endif aoqi@1: vmthread = (VMThread *)t; aoqi@1: } aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: // decide if this trap can be handled by a stub aoqi@1: address stub = NULL; aoqi@1: address pc = NULL; aoqi@1: aoqi@1: pc = (address) os::Linux::ucontext_get_pc(uc); aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("pc=%lx", pc); aoqi@1: os::print_context(tty, uc); aoqi@1: #endif aoqi@1: //%note os_trap_1 aoqi@1: if (info != NULL && uc != NULL && thread != NULL) { aoqi@1: pc = (address) os::Linux::ucontext_get_pc(uc); aoqi@1: // Handle ALL stack overflow variations here aoqi@1: if (sig == SIGSEGV) { aoqi@1: address addr = (address) info->si_addr; aoqi@1: // check if fault address is within thread stack aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print("handle all stack overflow variations: "); aoqi@1: /*tty->print("addr = %lx, stack base = %lx, stack top = %lx\n", aoqi@1: addr, aoqi@1: thread->stack_base(), aoqi@1: thread->stack_base() - thread->stack_size()); aoqi@1: */ aoqi@1: #endif aoqi@1: aoqi@1: if (addr < thread->stack_base() && aoqi@1: addr >= thread->stack_base() - thread->stack_size()) { aoqi@1: // stack overflow aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("stack exception check \n"); aoqi@1: #endif aoqi@1: if (thread->in_stack_yellow_zone(addr)) { aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("exception addr is in yellow zone\n"); aoqi@1: #endif aoqi@1: thread->disable_stack_yellow_zone(); aoqi@1: if (thread->thread_state() == _thread_in_Java) { aoqi@1: // Throw a stack overflow exception. Guard pages will be reenabled aoqi@1: // while unwinding the stack. aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("this thread is in java\n"); aoqi@1: #endif aoqi@1: stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW); aoqi@1: } else { aoqi@1: // Thread was in the vm or native code. Return and try to finish. aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("this thread is in vm or native codes and return\n"); aoqi@1: #endif aoqi@1: return 1; aoqi@1: } aoqi@1: } else if (thread->in_stack_red_zone(addr)) { aoqi@1: // Fatal red zone violation. Disable the guard pages and fall through aoqi@1: // to handle_unexpected_exception way down below. aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("exception addr is in red zone\n"); aoqi@1: #endif aoqi@1: thread->disable_stack_red_zone(); aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_raw_cr("An irrecoverable stack overflow has occurred."); aoqi@1: #endif aoqi@1: } else { aoqi@1: // Accessing stack address below sp may cause SEGV if current aoqi@1: // thread has MAP_GROWSDOWN stack. This should only happen when aoqi@1: // current thread was created by user code with MAP_GROWSDOWN flag aoqi@1: // and then attached to VM. See notes in os_linux.cpp. aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("exception addr is neither in yellow zone nor in the red one\n"); aoqi@1: #endif aoqi@1: if (thread->osthread()->expanding_stack() == 0) { aoqi@1: thread->osthread()->set_expanding_stack(); aoqi@1: if (os::Linux::manually_expand_stack(thread, addr)) { aoqi@1: thread->osthread()->clear_expanding_stack(); aoqi@1: return 1; aoqi@1: } aoqi@1: thread->osthread()->clear_expanding_stack(); aoqi@1: } else { aoqi@1: fatal("recursive segv. expanding stack."); aoqi@1: } aoqi@1: } aoqi@1: } //addr < aoqi@1: } //sig == SIGSEGV aoqi@1: aoqi@1: if (thread->thread_state() == _thread_in_Java) { aoqi@1: // Java thread running in Java code => find exception handler if any aoqi@1: // a fault inside compiled code, the interpreter, or a stub aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("java thread running in java code\n"); aoqi@1: tty->print_cr("polling address = %lx, sig=%d", os::get_polling_page(), sig); aoqi@1: #endif aoqi@1: if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) { aoqi@1: aoqi@1: stub = SharedRuntime::get_poll_stub(pc); aoqi@1: } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) { aoqi@1: // BugId 4454115: A read from a MappedByteBuffer can fault aoqi@1: // here if the underlying file has been truncated. aoqi@1: // Do not crash the VM in such a case. aoqi@1: CodeBlob* cb = CodeCache::find_blob_unsafe(pc); aoqi@1: nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL; aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("cb = %lx, nm = %lx\n", cb, nm); aoqi@1: #endif aoqi@1: if (nm != NULL && nm->has_unsafe_access()) { aoqi@1: stub = StubRoutines::handler_for_unsafe_access(); aoqi@1: } aoqi@1: } else if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) { aoqi@1: // HACK: si_code does not work on linux 2.2.12-20!!! aoqi@1: int op = pc[0] & 0x3f; aoqi@1: int op1 = pc[3] & 0x3f; aoqi@1: //FIXME, Must port to mips code!! aoqi@1: switch (op) { aoqi@1: case 0x1e: //ddiv aoqi@1: case 0x1f: //ddivu aoqi@1: case 0x1a: //div aoqi@1: case 0x1b: //divu aoqi@1: case 0x34: //trap aoqi@1: /* In MIPS, div_by_zero exception can only be triggered by explicit 'trap'. aoqi@1: * Ref: [c1_LIRAssembler_mips.cpp] arithmetic_idiv() aoqi@1: */ aoqi@1: stub = SharedRuntime::continuation_for_implicit_exception(thread, aoqi@1: pc, aoqi@1: SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO); aoqi@1: break; aoqi@1: default: aoqi@1: // TODO: handle more cases if we are using other x86 instructions aoqi@1: // that can generate SIGFPE signal on linux. aoqi@1: tty->print_cr("unknown opcode 0x%X -0x%X with SIGFPE.", op, op1); aoqi@1: //fatal("please update this code."); aoqi@1: } aoqi@1: } aoqi@1: else if (sig == SIGSEGV && aoqi@1: !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) { aoqi@1: // Determination of interpreter/vtable stub/compiled code null exception aoqi@1: #ifndef PRODUCT aoqi@1: tty->print("continuation for implicit exception\n"); aoqi@1: #endif aoqi@1: stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL); aoqi@1: } aoqi@1: } else if (thread->thread_state() == _thread_in_vm && aoqi@1: sig == SIGBUS && /* info->si_code == BUS_OBJERR && */ aoqi@1: thread->doing_unsafe_access()) { aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("SIGBUS in vm thread \n"); aoqi@1: #endif aoqi@1: stub = StubRoutines::handler_for_unsafe_access(); aoqi@1: } aoqi@1: aoqi@1: // jni_fast_GetField can trap at certain pc's if a GC kicks in aoqi@1: // and the heap gets shrunk before the field access. aoqi@1: if ((sig == SIGSEGV) || (sig == SIGBUS)) { aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print("jni fast get trap: "); aoqi@1: #endif aoqi@1: address addr = JNI_FastGetField::find_slowcase_pc(pc); aoqi@1: if (addr != (address)-1) { aoqi@1: stub = addr; aoqi@1: } aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print_cr("addr = %d, stub = %lx", addr, stub); aoqi@1: #endif aoqi@1: } aoqi@1: aoqi@1: // Check to see if we caught the safepoint code in the aoqi@1: // process of write protecting the memory serialization page. aoqi@1: // It write enables the page immediately after protecting it aoqi@1: // so we can just return to retry the write. aoqi@1: if ((sig == SIGSEGV) && aoqi@1: os::is_memory_serialize_page(thread, (address) info->si_addr)) { aoqi@1: // Block current thread until the memory serialize page permission restored. aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print("write protecting the memory serialiazation page\n"); aoqi@1: #endif aoqi@1: os::block_on_serialize_page_trap(); aoqi@1: return true; aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: // Execution protection violation aoqi@1: // aoqi@1: // This should be kept as the last step in the triage. We don't aoqi@1: // have a dedicated trap number for a no-execute fault, so be aoqi@1: // conservative and allow other handlers the first shot. aoqi@1: // aoqi@1: // Note: We don't test that info->si_code == SEGV_ACCERR here. aoqi@1: // this si_code is so generic that it is almost meaningless; and aoqi@1: // the si_code for this condition may change in the future. aoqi@1: // Furthermore, a false-positive should be harmless. aoqi@1: if (UnguardOnExecutionViolation > 0 && aoqi@1: //(sig == SIGSEGV || sig == SIGBUS) && aoqi@1: //uc->uc_mcontext.gregs[REG_TRAPNO] == trap_page_fault) { aoqi@1: (sig == SIGSEGV || sig == SIGBUS aoqi@1: #ifdef OPT_RANGECHECK aoqi@1: || sig == SIGSYS aoqi@1: #endif aoqi@1: ) && aoqi@1: //(uc->uc_mcontext.cause == 2 || uc->uc_mcontext.cause == 3)) { aoqi@1: (uc->uc_mcontext.hi1 == 2 || uc->uc_mcontext.hi1 == 3)) { aoqi@1: //aoqi: copy from jdk1.5, dont understand the struct mcontext_t. aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("execution protection violation\n"); aoqi@1: #endif aoqi@1: aoqi@1: int page_size = os::vm_page_size(); aoqi@1: address addr = (address) info->si_addr; aoqi@1: address pc = os::Linux::ucontext_get_pc(uc); aoqi@1: // Make sure the pc and the faulting address are sane. aoqi@1: // aoqi@1: // If an instruction spans a page boundary, and the page containing aoqi@1: // the beginning of the instruction is executable but the following aoqi@1: // page is not, the pc and the faulting address might be slightly aoqi@1: // different - we still want to unguard the 2nd page in this case. aoqi@1: // aoqi@1: // 15 bytes seems to be a (very) safe value for max instruction size. aoqi@1: bool pc_is_near_addr = aoqi@1: (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15); aoqi@1: bool instr_spans_page_boundary = aoqi@1: (align_size_down((intptr_t) pc ^ (intptr_t) addr, aoqi@1: (intptr_t) page_size) > 0); aoqi@1: aoqi@1: if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) { aoqi@1: static volatile address last_addr = aoqi@1: (address) os::non_memory_address_word(); aoqi@1: aoqi@1: // In conservative mode, don't unguard unless the address is in the VM aoqi@1: if (addr != last_addr && aoqi@1: (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) { aoqi@1: aoqi@1: // Set memory to RWX and retry aoqi@1: address page_start = aoqi@1: (address) align_size_down((intptr_t) addr, (intptr_t) page_size); aoqi@1: //bool res = os::protect_memory((char*) page_start, page_size, aoqi@1: // os::MEM_PROT_RWX); aoqi@1: bool res = os::unguard_memory((char*) page_start, page_size);//aoqi:? aoqi@1: aoqi@1: if (PrintMiscellaneous && Verbose) { aoqi@1: char buf[256]; aoqi@1: jio_snprintf(buf, sizeof(buf), "Execution protection violation " aoqi@1: "at " INTPTR_FORMAT aoqi@1: ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr, aoqi@1: page_start, (res ? "success" : "failed"), errno); aoqi@1: tty->print_raw_cr(buf); aoqi@1: } aoqi@1: stub = pc; aoqi@1: aoqi@1: // Set last_addr so if we fault again at the same address, we don't end aoqi@1: // up in an endless loop. aoqi@1: // aoqi@1: // There are two potential complications here. Two threads trapping at aoqi@1: // the same address at the same time could cause one of the threads to aoqi@1: // think it already unguarded, and abort the VM. Likely very rare. aoqi@1: // aoqi@1: // The other race involves two threads alternately trapping at aoqi@1: // different addresses and failing to unguard the page, resulting in aoqi@1: // an endless loop. This condition is probably even more unlikely than aoqi@1: // the first. aoqi@1: // aoqi@1: // Although both cases could be avoided by using locks or thread local aoqi@1: // last_addr, these solutions are unnecessary complication: this aoqi@1: // handler is a best-effort safety net, not a complete solution. It is aoqi@1: // disabled by default and should only be used as a workaround in case aoqi@1: // we missed any no-execute-unsafe VM code. aoqi@1: aoqi@1: last_addr = addr; aoqi@1: } aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: if (stub != NULL) { aoqi@1: #ifndef PRODUCT aoqi@1: //tty->print_cr("resolved stub=%lx\n",stub); aoqi@1: #endif aoqi@1: // save all thread context in case we need to restore it aoqi@1: if (thread != NULL) thread->set_saved_exception_pc(pc); aoqi@1: aoqi@1: uc->uc_mcontext.pc = (greg_t)stub; aoqi@1: return true; aoqi@1: } aoqi@1: aoqi@1: // signal-chaining aoqi@1: if (os::Linux::chained_handler(sig, info, ucVoid)) { aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("signal chaining\n"); aoqi@1: #endif aoqi@1: return true; aoqi@1: } aoqi@1: aoqi@1: if (!abort_if_unrecognized) { aoqi@1: // caller wants another chance, so give it to him aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("abort becauce of unrecognized\n"); aoqi@1: #endif aoqi@1: return false; aoqi@1: } aoqi@1: aoqi@1: if (pc == NULL && uc != NULL) { aoqi@1: pc = os::Linux::ucontext_get_pc(uc); aoqi@1: } aoqi@1: aoqi@1: // unmask current signal aoqi@1: sigset_t newset; aoqi@1: sigemptyset(&newset); aoqi@1: sigaddset(&newset, sig); aoqi@1: sigprocmask(SIG_UNBLOCK, &newset, NULL); aoqi@1: #ifndef PRODUCT aoqi@1: tty->print_cr("VMError in signal handler\n"); aoqi@1: #endif aoqi@1: VMError err(t, sig, pc, info, ucVoid); aoqi@1: err.report_and_die(); aoqi@1: aoqi@1: ShouldNotReachHere(); aoqi@1: } aoqi@1: aoqi@1: void os::Linux::init_thread_fpu_state(void) { aoqi@1: // set fpu to 53 bit precision aoqi@1: //set_fpu_control_word(0x27f); aoqi@1: } aoqi@1: aoqi@1: int os::Linux::get_fpu_control_word(void) { aoqi@1: } aoqi@1: aoqi@1: void os::Linux::set_fpu_control_word(int fpu_control) { aoqi@1: } aoqi@1: aoqi@1: bool os::is_allocatable(size_t bytes) { aoqi@1: aoqi@1: if (bytes < 2 * G) { aoqi@1: return true; aoqi@1: } aoqi@1: aoqi@1: char* addr = reserve_memory(bytes, NULL); aoqi@1: aoqi@1: if (addr != NULL) { aoqi@1: release_memory(addr, bytes); aoqi@1: } aoqi@1: aoqi@1: return addr != NULL; aoqi@1: } aoqi@1: aoqi@1: //////////////////////////////////////////////////////////////////////////////// aoqi@1: // thread stack aoqi@1: aoqi@1: size_t os::Linux::min_stack_allowed = 96 * K; aoqi@1: aoqi@1: aoqi@1: // Test if pthread library can support variable thread stack size. LinuxThreads aoqi@1: // in fixed stack mode allocates 2M fixed slot for each thread. LinuxThreads aoqi@1: // in floating stack mode and NPTL support variable stack size. aoqi@1: bool os::Linux::supports_variable_stack_size() { aoqi@1: if (os::Linux::is_NPTL()) { aoqi@1: // NPTL, yes aoqi@1: return true; aoqi@1: aoqi@1: } else { aoqi@1: // Note: We can't control default stack size when creating a thread. aoqi@1: // If we use non-default stack size (pthread_attr_setstacksize), both aoqi@1: // floating stack and non-floating stack LinuxThreads will return the aoqi@1: // same value. This makes it impossible to implement this function by aoqi@1: // detecting thread stack size directly. aoqi@1: // aoqi@1: // An alternative approach is to check %gs. Fixed-stack LinuxThreads aoqi@1: // do not use %gs, so its value is 0. Floating-stack LinuxThreads use aoqi@1: // %gs (either as LDT selector or GDT selector, depending on kernel) aoqi@1: // to access thread specific data. aoqi@1: // aoqi@1: // Note that %gs is a reserved glibc register since early 2001, so aoqi@1: // applications are not allowed to change its value (Ulrich Drepper from aoqi@1: // Red Hat confirmed that all known offenders have been modified to use aoqi@1: // either %fs or TSD). In the worst case scenario, when VM is embedded in aoqi@1: // a native application that plays with %gs, we might see non-zero %gs aoqi@1: // even LinuxThreads is running in fixed stack mode. As the result, we'll aoqi@1: // return true and skip _thread_safety_check(), so we may not be able to aoqi@1: // detect stack-heap collisions. But otherwise it's harmless. aoqi@1: // aoqi@1: //FIXME we should do something here not just return false. by yjl 6/21/2005 aoqi@1: return false; aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: // return default stack size for thr_type aoqi@1: //maybe we need change this, FIXME by yjl 6/21/2005 aoqi@1: size_t os::Linux::default_stack_size(os::ThreadType thr_type) { aoqi@1: // default stack size (compiler thread needs larger stack) aoqi@1: size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K); aoqi@1: return s; aoqi@1: } aoqi@1: aoqi@1: size_t os::Linux::default_guard_size(os::ThreadType thr_type) { aoqi@1: // Creating guard page is very expensive. Java thread has HotSpot aoqi@1: // guard page, only enable glibc guard page for non-Java threads. aoqi@1: return (thr_type == java_thread ? 0 : page_size()); aoqi@1: } aoqi@1: aoqi@1: // Java thread: aoqi@1: // aoqi@1: // Low memory addresses aoqi@1: // +------------------------+ aoqi@1: // | |\ JavaThread created by VM does not have glibc aoqi@1: // | glibc guard page | - guard, attached Java thread usually has aoqi@1: // | |/ 1 page glibc guard. aoqi@1: // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() aoqi@1: // | |\ aoqi@1: // | HotSpot Guard Pages | - red and yellow pages aoqi@1: // | |/ aoqi@1: // +------------------------+ JavaThread::stack_yellow_zone_base() aoqi@1: // | |\ aoqi@1: // | Normal Stack | - aoqi@1: // | |/ aoqi@1: // P2 +------------------------+ Thread::stack_base() aoqi@1: // aoqi@1: // Non-Java thread: aoqi@1: // aoqi@1: // Low memory addresses aoqi@1: // +------------------------+ aoqi@1: // | |\ aoqi@1: // | glibc guard page | - usually 1 page aoqi@1: // | |/ aoqi@1: // P1 +------------------------+ Thread::stack_base() - Thread::stack_size() aoqi@1: // | |\ aoqi@1: // | Normal Stack | - aoqi@1: // | |/ aoqi@1: // P2 +------------------------+ Thread::stack_base() aoqi@1: // aoqi@1: // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from aoqi@1: // pthread_attr_getstack() aoqi@1: aoqi@1: static void current_stack_region(address * bottom, size_t * size) { aoqi@1: if (os::Linux::is_initial_thread()) { aoqi@1: // initial thread needs special handling because pthread_getattr_np() aoqi@1: // may return bogus value. aoqi@1: *bottom = os::Linux::initial_thread_stack_bottom(); aoqi@1: *size = os::Linux::initial_thread_stack_size(); aoqi@1: } else { aoqi@1: pthread_attr_t attr; aoqi@1: aoqi@1: int rslt = pthread_getattr_np(pthread_self(), &attr); aoqi@1: aoqi@1: // JVM needs to know exact stack location, abort if it fails aoqi@1: if (rslt != 0) { aoqi@1: if (rslt == ENOMEM) { aoqi@1: vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np"); aoqi@1: } else { aoqi@1: fatal(err_msg("pthread_getattr_np failed with errno = %d", rslt)); aoqi@1: } aoqi@1: } aoqi@1: aoqi@1: if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0 ) { aoqi@1: fatal("Can not locate current stack attributes!"); aoqi@1: } aoqi@1: /* aoqi@1: void * top; aoqi@1: if (pthread_attr_getstackaddr(&attr, &top) != 0 || aoqi@1: pthread_attr_getstacksize(&attr, size) != 0) { aoqi@1: fatal("Can not locate current stack attributes!"); aoqi@1: } aoqi@1: */ aoqi@1: pthread_attr_destroy(&attr); aoqi@1: aoqi@1: //*bottom = (address) align_size_up((uintptr_t)top - *size, os::Linux::page_size()); aoqi@1: //*size = (address)top - *bottom; aoqi@1: } aoqi@1: assert(os::current_stack_pointer() >= *bottom && aoqi@1: os::current_stack_pointer() < *bottom + *size, "just checking"); aoqi@1: } aoqi@1: aoqi@1: address os::current_stack_base() { aoqi@1: address bottom; aoqi@1: size_t size; aoqi@1: current_stack_region(&bottom, &size); aoqi@1: return (bottom + size); aoqi@1: } aoqi@1: aoqi@1: size_t os::current_stack_size() { aoqi@1: // stack size includes normal stack and HotSpot guard pages aoqi@1: address bottom; aoqi@1: size_t size; aoqi@1: current_stack_region(&bottom, &size); aoqi@1: return size; aoqi@1: } aoqi@1: aoqi@1: ///////////////////////////////////////////////////////////////////////////// aoqi@1: // helper functions for fatal error handler aoqi@1: void os::print_register_info(outputStream *st, void *context) { aoqi@1: if (context == NULL) return; aoqi@1: aoqi@1: ucontext_t *uc = (ucontext_t*)context; aoqi@1: aoqi@1: st->print_cr("Register to memory mapping:"); aoqi@1: st->cr(); aoqi@1: // this is horrendously verbose but the layout of the registers in the aoqi@1: // // context does not match how we defined our abstract Register set, so aoqi@1: // // we can't just iterate through the gregs area aoqi@1: // aoqi@1: // // this is only for the "general purpose" registers aoqi@1: st->print("R0=" ); print_location(st, uc->uc_mcontext.gregs[0]); aoqi@1: st->print("AT=" ); print_location(st, uc->uc_mcontext.gregs[1]); aoqi@1: st->print("V0=" ); print_location(st, uc->uc_mcontext.gregs[2]); aoqi@1: st->print("V1=" ); print_location(st, uc->uc_mcontext.gregs[3]); aoqi@1: st->cr(); aoqi@1: st->print("A0=" ); print_location(st, uc->uc_mcontext.gregs[4]); aoqi@1: st->print("A1=" ); print_location(st, uc->uc_mcontext.gregs[5]); aoqi@1: st->print("A2=" ); print_location(st, uc->uc_mcontext.gregs[6]); aoqi@1: st->print("A3=" ); print_location(st, uc->uc_mcontext.gregs[7]); aoqi@1: st->cr(); aoqi@1: st->print("A4=" ); print_location(st, uc->uc_mcontext.gregs[8]); aoqi@1: st->print("A5=" ); print_location(st, uc->uc_mcontext.gregs[9]); aoqi@1: st->print("A6=" ); print_location(st, uc->uc_mcontext.gregs[10]); aoqi@1: st->print("A7=" ); print_location(st, uc->uc_mcontext.gregs[11]); aoqi@1: st->cr(); aoqi@1: st->print("T0=" ); print_location(st, uc->uc_mcontext.gregs[12]); aoqi@1: st->print("T1=" ); print_location(st, uc->uc_mcontext.gregs[13]); aoqi@1: st->print("T2=" ); print_location(st, uc->uc_mcontext.gregs[14]); aoqi@1: st->print("T3=" ); print_location(st, uc->uc_mcontext.gregs[15]); aoqi@1: st->cr(); aoqi@1: st->print("S0=" ); print_location(st, uc->uc_mcontext.gregs[16]); aoqi@1: st->print("S1=" ); print_location(st, uc->uc_mcontext.gregs[17]); aoqi@1: st->print("S2=" ); print_location(st, uc->uc_mcontext.gregs[18]); aoqi@1: st->print("S3=" ); print_location(st, uc->uc_mcontext.gregs[19]); aoqi@1: st->cr(); aoqi@1: st->print("S4=" ); print_location(st, uc->uc_mcontext.gregs[20]); aoqi@1: st->print("S5=" ); print_location(st, uc->uc_mcontext.gregs[21]); aoqi@1: st->print("S6=" ); print_location(st, uc->uc_mcontext.gregs[22]); aoqi@1: st->print("S7=" ); print_location(st, uc->uc_mcontext.gregs[23]); aoqi@1: st->cr(); aoqi@1: st->print("T8=" ); print_location(st, uc->uc_mcontext.gregs[24]); aoqi@1: st->print("T9=" ); print_location(st, uc->uc_mcontext.gregs[25]); aoqi@1: st->print("K0=" ); print_location(st, uc->uc_mcontext.gregs[26]); aoqi@1: st->print("K1=" ); print_location(st, uc->uc_mcontext.gregs[27]); aoqi@1: st->cr(); aoqi@1: st->print("GP=" ); print_location(st, uc->uc_mcontext.gregs[28]); aoqi@1: st->print("SP=" ); print_location(st, uc->uc_mcontext.gregs[29]); aoqi@1: st->print("FP=" ); print_location(st, uc->uc_mcontext.gregs[30]); aoqi@1: st->print("RA=" ); print_location(st, uc->uc_mcontext.gregs[31]); aoqi@1: st->cr(); aoqi@1: aoqi@1: } aoqi@1: void os::print_context(outputStream *st, void *context) { aoqi@1: if (context == NULL) return; aoqi@1: aoqi@1: ucontext_t *uc = (ucontext_t*)context; aoqi@1: st->print_cr("Registers:"); aoqi@1: st->print( "R0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[0]); aoqi@1: st->print(", AT=" INTPTR_FORMAT, uc->uc_mcontext.gregs[1]); aoqi@1: st->print(", V0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[2]); aoqi@1: st->print(", V1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[3]); aoqi@1: st->cr(); aoqi@1: st->print( "A0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[4]); aoqi@1: st->print(", A1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[5]); aoqi@1: st->print(", A2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[6]); aoqi@1: st->print(", A3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[7]); aoqi@1: st->cr(); aoqi@1: st->print( "A4=" INTPTR_FORMAT, uc->uc_mcontext.gregs[8]); aoqi@1: st->print(", A5=" INTPTR_FORMAT, uc->uc_mcontext.gregs[9]); aoqi@1: st->print(", A6=" INTPTR_FORMAT, uc->uc_mcontext.gregs[10]); aoqi@1: st->print(", A7=" INTPTR_FORMAT, uc->uc_mcontext.gregs[11]); aoqi@1: st->cr(); aoqi@1: st->print( "T0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[12]); aoqi@1: st->print(", T1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[13]); aoqi@1: st->print(", T2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[14]); aoqi@1: st->print(", T3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[15]); aoqi@1: st->cr(); aoqi@1: st->print( "S0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[16]); aoqi@1: st->print(", S1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[17]); aoqi@1: st->print(", S2=" INTPTR_FORMAT, uc->uc_mcontext.gregs[18]); aoqi@1: st->print(", S3=" INTPTR_FORMAT, uc->uc_mcontext.gregs[19]); aoqi@1: st->cr(); aoqi@1: st->print( "S4=" INTPTR_FORMAT, uc->uc_mcontext.gregs[20]); aoqi@1: st->print(", S5=" INTPTR_FORMAT, uc->uc_mcontext.gregs[21]); aoqi@1: st->print(", S6=" INTPTR_FORMAT, uc->uc_mcontext.gregs[22]); aoqi@1: st->print(", S7=" INTPTR_FORMAT, uc->uc_mcontext.gregs[23]); aoqi@1: st->cr(); aoqi@1: st->print( "T8=" INTPTR_FORMAT, uc->uc_mcontext.gregs[24]); aoqi@1: st->print(", T9=" INTPTR_FORMAT, uc->uc_mcontext.gregs[25]); aoqi@1: st->print(", K0=" INTPTR_FORMAT, uc->uc_mcontext.gregs[26]); aoqi@1: st->print(", K1=" INTPTR_FORMAT, uc->uc_mcontext.gregs[27]); aoqi@1: st->cr(); aoqi@1: st->print( "GP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[28]); aoqi@1: st->print(", SP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[29]); aoqi@1: st->print(", FP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[30]); aoqi@1: st->print(", RA=" INTPTR_FORMAT, uc->uc_mcontext.gregs[31]); aoqi@1: st->cr(); aoqi@1: st->cr(); aoqi@1: aoqi@1: intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc); aoqi@1: st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp); aoqi@1: //print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t)); aoqi@1: print_hex_dump(st, (address)sp-32, (address)(sp + 32), sizeof(intptr_t)); aoqi@1: st->cr(); aoqi@1: aoqi@1: // Note: it may be unsafe to inspect memory near pc. For example, pc may aoqi@1: // point to garbage if entry point in an nmethod is corrupted. Leave aoqi@1: // this at the end, and hope for the best. aoqi@1: address pc = os::Linux::ucontext_get_pc(uc); aoqi@1: st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc); aoqi@1: print_hex_dump(st, pc - 64, pc + 64, sizeof(char)); aoqi@1: Disassembler::decode(pc - 80, pc + 80, st); aoqi@1: } aoqi@1: aoqi@1: #ifndef PRODUCT aoqi@1: void os::verify_stack_alignment() { aoqi@1: assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment"); aoqi@1: } aoqi@1: #endif aoqi@1: