1010N/A/*
2273N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
1176N/A * Copyright 2007, 2008, 2009, 2010 Red Hat, Inc.
1010N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1010N/A *
1010N/A * This code is free software; you can redistribute it and/or modify it
1010N/A * under the terms of the GNU General Public License version 2 only, as
1010N/A * published by the Free Software Foundation.
1010N/A *
1010N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1010N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1010N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1010N/A * version 2 for more details (a copy is included in the LICENSE file that
1010N/A * accompanied this code).
1010N/A *
1010N/A * You should have received a copy of the GNU General Public License version
1010N/A * 2 along with this work; if not, write to the Free Software Foundation,
1010N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1010N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
1010N/A *
1010N/A */
1010N/A
1879N/A// no precompiled headers
1879N/A#include "assembler_zero.inline.hpp"
1879N/A#include "classfile/classLoader.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "code/icBuffer.hpp"
1879N/A#include "code/vtableStubs.hpp"
1879N/A#include "interpreter/interpreter.hpp"
1879N/A#include "jvm_linux.h"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "mutex_linux.inline.hpp"
1879N/A#include "nativeInst_zero.hpp"
1879N/A#include "os_share_linux.hpp"
1879N/A#include "prims/jniFastGetField.hpp"
1879N/A#include "prims/jvm.h"
1879N/A#include "prims/jvm_misc.hpp"
1879N/A#include "runtime/arguments.hpp"
1879N/A#include "runtime/extendedPC.hpp"
1879N/A#include "runtime/frame.inline.hpp"
1879N/A#include "runtime/interfaceSupport.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/javaCalls.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/osThread.hpp"
1879N/A#include "runtime/sharedRuntime.hpp"
1879N/A#include "runtime/stubRoutines.hpp"
1879N/A#include "runtime/timer.hpp"
1879N/A#include "thread_linux.inline.hpp"
1879N/A#include "utilities/events.hpp"
1879N/A#include "utilities/vmError.hpp"
1010N/A
1010N/Aaddress os::current_stack_pointer() {
1010N/A address dummy = (address) &dummy;
1010N/A return dummy;
1010N/A}
1010N/A
1010N/Aframe os::get_sender_for_C_frame(frame* fr) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1010N/Aframe os::current_frame() {
1010N/A // The only thing that calls this is the stack printing code in
1010N/A // VMError::report:
1010N/A // - Step 110 (printing stack bounds) uses the sp in the frame
1010N/A // to determine the amount of free space on the stack. We
1010N/A // set the sp to a close approximation of the real value in
1010N/A // order to allow this step to complete.
1010N/A // - Step 120 (printing native stack) tries to walk the stack.
1010N/A // The frame we create has a NULL pc, which is ignored as an
1010N/A // invalid frame.
1010N/A frame dummy = frame();
1010N/A dummy.set_sp((intptr_t *) current_stack_pointer());
1010N/A return dummy;
1010N/A}
1010N/A
1010N/Achar* os::non_memory_address_word() {
1010N/A // Must never look like an address returned by reserve_memory,
1010N/A // even in its subfields (as defined by the CPU immediate fields,
1010N/A // if the CPU splits constants across multiple instructions).
1010N/A#ifdef SPARC
1010N/A // On SPARC, 0 != %hi(any real address), because there is no
1010N/A // allocation in the first 1Kb of the virtual address space.
1010N/A return (char *) 0;
1010N/A#else
1010N/A // This is the value for x86; works pretty well for PPC too.
1010N/A return (char *) -1;
1010N/A#endif // SPARC
1010N/A}
1010N/A
4060N/Avoid os::initialize_thread(Thread * thr){
1010N/A // Nothing to do.
1010N/A}
1010N/A
1010N/Aaddress os::Linux::ucontext_get_pc(ucontext_t* uc) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1010N/AExtendedPC os::fetch_frame_from_context(void* ucVoid,
1010N/A intptr_t** ret_sp,
1010N/A intptr_t** ret_fp) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1010N/Aframe os::fetch_frame_from_context(void* ucVoid) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
2072N/Aextern "C" JNIEXPORT int
1010N/AJVM_handle_linux_signal(int sig,
1010N/A siginfo_t* info,
1010N/A void* ucVoid,
1010N/A int abort_if_unrecognized) {
1010N/A ucontext_t* uc = (ucontext_t*) ucVoid;
1010N/A
1010N/A Thread* t = ThreadLocalStorage::get_thread_slow();
1010N/A
1010N/A SignalHandlerMark shm(t);
1010N/A
1010N/A // Note: it's not uncommon that JNI code uses signal/sigset to
1010N/A // install then restore certain signal handler (e.g. to temporarily
1010N/A // block SIGPIPE, or have a SIGILL handler when detecting CPU
1010N/A // type). When that happens, JVM_handle_linux_signal() might be
1010N/A // invoked with junk info/ucVoid. To avoid unnecessary crash when
1010N/A // libjsig is not preloaded, try handle signals that do not require
1010N/A // siginfo/ucontext first.
1010N/A
1010N/A if (sig == SIGPIPE || sig == SIGXFSZ) {
1010N/A // allow chained handler to go first
1010N/A if (os::Linux::chained_handler(sig, info, ucVoid)) {
1010N/A return true;
1010N/A } else {
1010N/A if (PrintMiscellaneous && (WizardMode || Verbose)) {
1010N/A char buf[64];
1010N/A warning("Ignoring %s - see bugs 4229104 or 646499219",
1010N/A os::exception_name(sig, buf, sizeof(buf)));
1010N/A }
1010N/A return true;
1010N/A }
1010N/A }
1010N/A
1010N/A JavaThread* thread = NULL;
1010N/A VMThread* vmthread = NULL;
1010N/A if (os::Linux::signal_handlers_are_installed) {
1010N/A if (t != NULL ){
1010N/A if(t->is_Java_thread()) {
1010N/A thread = (JavaThread*)t;
1010N/A }
1010N/A else if(t->is_VM_thread()){
1010N/A vmthread = (VMThread *)t;
1010N/A }
1010N/A }
1010N/A }
1010N/A
1010N/A if (info != NULL && thread != NULL) {
1010N/A // Handle ALL stack overflow variations here
1010N/A if (sig == SIGSEGV) {
1010N/A address addr = (address) info->si_addr;
1010N/A
1010N/A // check if fault address is within thread stack
1010N/A if (addr < thread->stack_base() &&
1010N/A addr >= thread->stack_base() - thread->stack_size()) {
1010N/A // stack overflow
1010N/A if (thread->in_stack_yellow_zone(addr)) {
1010N/A thread->disable_stack_yellow_zone();
1010N/A ShouldNotCallThis();
1010N/A }
1010N/A else if (thread->in_stack_red_zone(addr)) {
1010N/A thread->disable_stack_red_zone();
1010N/A ShouldNotCallThis();
1010N/A }
1010N/A else {
1010N/A // Accessing stack address below sp may cause SEGV if
1010N/A // current thread has MAP_GROWSDOWN stack. This should
1010N/A // only happen when current thread was created by user
1010N/A // code with MAP_GROWSDOWN flag and then attached to VM.
1010N/A // See notes in os_linux.cpp.
1010N/A if (thread->osthread()->expanding_stack() == 0) {
1010N/A thread->osthread()->set_expanding_stack();
1010N/A if (os::Linux::manually_expand_stack(thread, addr)) {
1010N/A thread->osthread()->clear_expanding_stack();
1010N/A return true;
1010N/A }
1010N/A thread->osthread()->clear_expanding_stack();
1010N/A }
1010N/A else {
1010N/A fatal("recursive segv. expanding stack.");
1010N/A }
1010N/A }
1010N/A }
1010N/A }
1010N/A
1010N/A /*if (thread->thread_state() == _thread_in_Java) {
1010N/A ShouldNotCallThis();
1010N/A }
1010N/A else*/ if (thread->thread_state() == _thread_in_vm &&
1010N/A sig == SIGBUS && thread->doing_unsafe_access()) {
1010N/A ShouldNotCallThis();
1010N/A }
1010N/A
1010N/A // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC
1010N/A // kicks in and the heap gets shrunk before the field access.
1010N/A /*if (sig == SIGSEGV || sig == SIGBUS) {
1010N/A address addr = JNI_FastGetField::find_slowcase_pc(pc);
1010N/A if (addr != (address)-1) {
1010N/A stub = addr;
1010N/A }
1010N/A }*/
1010N/A
1010N/A // Check to see if we caught the safepoint code in the process
1010N/A // of write protecting the memory serialization page. It write
1010N/A // enables the page immediately after protecting it so we can
1010N/A // just return to retry the write.
1010N/A if (sig == SIGSEGV &&
1010N/A os::is_memory_serialize_page(thread, (address) info->si_addr)) {
1010N/A // Block current thread until permission is restored.
1010N/A os::block_on_serialize_page_trap();
1010N/A return true;
1010N/A }
1010N/A }
1010N/A
1010N/A // signal-chaining
1010N/A if (os::Linux::chained_handler(sig, info, ucVoid)) {
1010N/A return true;
1010N/A }
1010N/A
1010N/A if (!abort_if_unrecognized) {
1010N/A // caller wants another chance, so give it to him
1010N/A return false;
1010N/A }
1010N/A
1010N/A#ifndef PRODUCT
1010N/A if (sig == SIGSEGV) {
1010N/A fatal("\n#"
1010N/A "\n# /--------------------\\"
1010N/A "\n# | segmentation fault |"
1010N/A "\n# \\---\\ /--------------/"
1010N/A "\n# /"
1010N/A "\n# [-] |\\_/| "
1010N/A "\n# (+)=C |o o|__ "
1010N/A "\n# | | =-*-=__\\ "
1010N/A "\n# OOO c_c_(___)");
1010N/A }
1010N/A#endif // !PRODUCT
1010N/A
1010N/A const char *fmt = "caught unhandled signal %d";
1010N/A char buf[64];
1010N/A
1010N/A sprintf(buf, fmt, sig);
1010N/A fatal(buf);
1010N/A}
1010N/A
1010N/Avoid os::Linux::init_thread_fpu_state(void) {
1010N/A // Nothing to do
1010N/A}
1010N/A
1010N/Aint os::Linux::get_fpu_control_word() {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1010N/Avoid os::Linux::set_fpu_control_word(int fpu) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1010N/Abool os::is_allocatable(size_t bytes) {
1176N/A#ifdef _LP64
1176N/A return true;
1176N/A#else
1176N/A if (bytes < 2 * G) {
1176N/A return true;
1176N/A }
1176N/A
1176N/A char* addr = reserve_memory(bytes, NULL);
1176N/A
1176N/A if (addr != NULL) {
1176N/A release_memory(addr, bytes);
1176N/A }
1176N/A
1176N/A return addr != NULL;
1176N/A#endif // _LP64
1010N/A}
1010N/A
1010N/A///////////////////////////////////////////////////////////////////////////////
1010N/A// thread stack
1010N/A
1010N/Asize_t os::Linux::min_stack_allowed = 64 * K;
1010N/A
1010N/Abool os::Linux::supports_variable_stack_size() {
1010N/A return true;
1010N/A}
1010N/A
1010N/Asize_t os::Linux::default_stack_size(os::ThreadType thr_type) {
1010N/A#ifdef _LP64
1010N/A size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
1010N/A#else
1010N/A size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
1010N/A#endif // _LP64
1010N/A return s;
1010N/A}
1010N/A
1010N/Asize_t os::Linux::default_guard_size(os::ThreadType thr_type) {
1010N/A // Only enable glibc guard pages for non-Java threads
1010N/A // (Java threads have HotSpot guard pages)
1010N/A return (thr_type == java_thread ? 0 : page_size());
1010N/A}
1010N/A
1010N/Astatic void current_stack_region(address *bottom, size_t *size) {
1010N/A pthread_attr_t attr;
1010N/A int res = pthread_getattr_np(pthread_self(), &attr);
1010N/A if (res != 0) {
1010N/A if (res == ENOMEM) {
1010N/A vm_exit_out_of_memory(0, "pthread_getattr_np");
1010N/A }
1010N/A else {
1442N/A fatal(err_msg("pthread_getattr_np failed with errno = %d", res));
1010N/A }
1010N/A }
1010N/A
1010N/A address stack_bottom;
1010N/A size_t stack_bytes;
1010N/A res = pthread_attr_getstack(&attr, (void **) &stack_bottom, &stack_bytes);
1010N/A if (res != 0) {
1442N/A fatal(err_msg("pthread_attr_getstack failed with errno = %d", res));
1010N/A }
1010N/A address stack_top = stack_bottom + stack_bytes;
1010N/A
1010N/A // The block of memory returned by pthread_attr_getstack() includes
1010N/A // guard pages where present. We need to trim these off.
1010N/A size_t page_bytes = os::Linux::page_size();
1010N/A assert(((intptr_t) stack_bottom & (page_bytes - 1)) == 0, "unaligned stack");
1010N/A
1010N/A size_t guard_bytes;
1010N/A res = pthread_attr_getguardsize(&attr, &guard_bytes);
1010N/A if (res != 0) {
1442N/A fatal(err_msg("pthread_attr_getguardsize failed with errno = %d", res));
1010N/A }
1010N/A int guard_pages = align_size_up(guard_bytes, page_bytes) / page_bytes;
1010N/A assert(guard_bytes == guard_pages * page_bytes, "unaligned guard");
1010N/A
1010N/A#ifdef IA64
1010N/A // IA64 has two stacks sharing the same area of memory, a normal
1010N/A // stack growing downwards and a register stack growing upwards.
1010N/A // Guard pages, if present, are in the centre. This code splits
1010N/A // the stack in two even without guard pages, though in theory
1010N/A // there's nothing to stop us allocating more to the normal stack
1010N/A // or more to the register stack if one or the other were found
1010N/A // to grow faster.
1010N/A int total_pages = align_size_down(stack_bytes, page_bytes) / page_bytes;
1010N/A stack_bottom += (total_pages - guard_pages) / 2 * page_bytes;
1010N/A#endif // IA64
1010N/A
1010N/A stack_bottom += guard_bytes;
1010N/A
1010N/A pthread_attr_destroy(&attr);
1010N/A
1010N/A // The initial thread has a growable stack, and the size reported
1010N/A // by pthread_attr_getstack is the maximum size it could possibly
1010N/A // be given what currently mapped. This can be huge, so we cap it.
1010N/A if (os::Linux::is_initial_thread()) {
1010N/A stack_bytes = stack_top - stack_bottom;
1010N/A
1010N/A if (stack_bytes > JavaThread::stack_size_at_create())
1010N/A stack_bytes = JavaThread::stack_size_at_create();
1010N/A
1010N/A stack_bottom = stack_top - stack_bytes;
1010N/A }
1010N/A
1010N/A assert(os::current_stack_pointer() >= stack_bottom, "should do");
1010N/A assert(os::current_stack_pointer() < stack_top, "should do");
1010N/A
1010N/A *bottom = stack_bottom;
1010N/A *size = stack_top - stack_bottom;
1010N/A}
1010N/A
1010N/Aaddress os::current_stack_base() {
1010N/A address bottom;
1010N/A size_t size;
1010N/A current_stack_region(&bottom, &size);
1010N/A return bottom + size;
1010N/A}
1010N/A
1010N/Asize_t os::current_stack_size() {
1010N/A // stack size includes normal stack and HotSpot guard pages
1010N/A address bottom;
1010N/A size_t size;
1010N/A current_stack_region(&bottom, &size);
1010N/A return size;
1010N/A}
1010N/A
1010N/A/////////////////////////////////////////////////////////////////////////////
1010N/A// helper functions for fatal error handler
1010N/A
1010N/Avoid os::print_context(outputStream* st, void* context) {
1010N/A ShouldNotCallThis();
1010N/A}
1010N/A
1877N/Avoid os::print_register_info(outputStream *st, void *context) {
1877N/A ShouldNotCallThis();
1877N/A}
1877N/A
1010N/A/////////////////////////////////////////////////////////////////////////////
1010N/A// Stubs for things that would be in linux_zero.s if it existed.
1010N/A// You probably want to disassemble these monkeys to check they're ok.
1010N/A
1010N/Aextern "C" {
1010N/A int SpinPause() {
1010N/A }
1010N/A
1010N/A int SafeFetch32(int *adr, int errValue) {
1010N/A int value = errValue;
1010N/A value = *adr;
1010N/A return value;
1010N/A }
1010N/A intptr_t SafeFetchN(intptr_t *adr, intptr_t errValue) {
1010N/A intptr_t value = errValue;
1010N/A value = *adr;
1010N/A return value;
1010N/A }
1010N/A
1010N/A void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
1010N/A if (from > to) {
1010N/A jshort *end = from + count;
1010N/A while (from < end)
1010N/A *(to++) = *(from++);
1010N/A }
1010N/A else if (from < to) {
1010N/A jshort *end = from;
1010N/A from += count - 1;
1010N/A to += count - 1;
1010N/A while (from >= end)
1010N/A *(to--) = *(from--);
1010N/A }
1010N/A }
1010N/A void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
1010N/A if (from > to) {
1010N/A jint *end = from + count;
1010N/A while (from < end)
1010N/A *(to++) = *(from++);
1010N/A }
1010N/A else if (from < to) {
1010N/A jint *end = from;
1010N/A from += count - 1;
1010N/A to += count - 1;
1010N/A while (from >= end)
1010N/A *(to--) = *(from--);
1010N/A }
1010N/A }
1010N/A void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
1010N/A if (from > to) {
1010N/A jlong *end = from + count;
1010N/A while (from < end)
1010N/A os::atomic_copy64(from++, to++);
1010N/A }
1010N/A else if (from < to) {
1010N/A jlong *end = from;
1010N/A from += count - 1;
1010N/A to += count - 1;
1010N/A while (from >= end)
1010N/A os::atomic_copy64(from--, to--);
1010N/A }
1010N/A }
1010N/A
1010N/A void _Copy_arrayof_conjoint_bytes(HeapWord* from,
1010N/A HeapWord* to,
1010N/A size_t count) {
1649N/A memmove(to, from, count);
1010N/A }
1010N/A void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
1010N/A HeapWord* to,
1010N/A size_t count) {
1649N/A memmove(to, from, count * 2);
1010N/A }
1010N/A void _Copy_arrayof_conjoint_jints(HeapWord* from,
1010N/A HeapWord* to,
1010N/A size_t count) {
1649N/A memmove(to, from, count * 4);
1010N/A }
1010N/A void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
1010N/A HeapWord* to,
1010N/A size_t count) {
1649N/A memmove(to, from, count * 8);
1010N/A }
1010N/A};
1010N/A
1010N/A/////////////////////////////////////////////////////////////////////////////
1010N/A// Implementations of atomic operations not supported by processors.
1010N/A// -- http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Atomic-Builtins.html
1010N/A
1010N/A#ifndef _LP64
1010N/Aextern "C" {
1010N/A long long unsigned int __sync_val_compare_and_swap_8(
1010N/A volatile void *ptr,
1010N/A long long unsigned int oldval,
1010N/A long long unsigned int newval) {
1010N/A ShouldNotCallThis();
1010N/A }
1010N/A};
1010N/A#endif // !_LP64
3574N/A
3574N/A#ifndef PRODUCT
3574N/Avoid os::verify_stack_alignment() {
3574N/A}
3574N/A#endif