src/os_cpu/solaris_x86/vm/os_solaris_x86.cpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2262
1e9a9d2e6509
child 2322
828eafbd85cc
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

     1 /*
     2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. 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 it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 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 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 // no precompiled headers
    26 #include "assembler_x86.inline.hpp"
    27 #include "classfile/classLoader.hpp"
    28 #include "classfile/systemDictionary.hpp"
    29 #include "classfile/vmSymbols.hpp"
    30 #include "code/icBuffer.hpp"
    31 #include "code/vtableStubs.hpp"
    32 #include "interpreter/interpreter.hpp"
    33 #include "jvm_solaris.h"
    34 #include "memory/allocation.inline.hpp"
    35 #include "mutex_solaris.inline.hpp"
    36 #include "nativeInst_x86.hpp"
    37 #include "os_share_solaris.hpp"
    38 #include "prims/jniFastGetField.hpp"
    39 #include "prims/jvm.h"
    40 #include "prims/jvm_misc.hpp"
    41 #include "runtime/arguments.hpp"
    42 #include "runtime/extendedPC.hpp"
    43 #include "runtime/frame.inline.hpp"
    44 #include "runtime/hpi.hpp"
    45 #include "runtime/interfaceSupport.hpp"
    46 #include "runtime/java.hpp"
    47 #include "runtime/javaCalls.hpp"
    48 #include "runtime/mutexLocker.hpp"
    49 #include "runtime/osThread.hpp"
    50 #include "runtime/sharedRuntime.hpp"
    51 #include "runtime/stubRoutines.hpp"
    52 #include "runtime/timer.hpp"
    53 #include "thread_solaris.inline.hpp"
    54 #include "utilities/events.hpp"
    55 #include "utilities/vmError.hpp"
    56 #ifdef COMPILER1
    57 #include "c1/c1_Runtime1.hpp"
    58 #endif
    59 #ifdef COMPILER2
    60 #include "opto/runtime.hpp"
    61 #endif
    63 // put OS-includes here
    64 # include <sys/types.h>
    65 # include <sys/mman.h>
    66 # include <pthread.h>
    67 # include <signal.h>
    68 # include <setjmp.h>
    69 # include <errno.h>
    70 # include <dlfcn.h>
    71 # include <stdio.h>
    72 # include <unistd.h>
    73 # include <sys/resource.h>
    74 # include <thread.h>
    75 # include <sys/stat.h>
    76 # include <sys/time.h>
    77 # include <sys/filio.h>
    78 # include <sys/utsname.h>
    79 # include <sys/systeminfo.h>
    80 # include <sys/socket.h>
    81 # include <sys/trap.h>
    82 # include <sys/lwp.h>
    83 # include <pwd.h>
    84 # include <poll.h>
    85 # include <sys/lwp.h>
    86 # include <procfs.h>     //  see comment in <sys/procfs.h>
    88 #ifndef AMD64
    89 // QQQ seems useless at this point
    90 # define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
    91 #endif // AMD64
    92 # include <sys/procfs.h>     //  see comment in <sys/procfs.h>
    95 #define MAX_PATH (2 * K)
    97 // Minimum stack size for the VM.  It's easier to document a constant value
    98 // but it's different for x86 and sparc because the page sizes are different.
    99 #ifdef AMD64
   100 size_t os::Solaris::min_stack_allowed = 224*K;
   101 #define REG_SP REG_RSP
   102 #define REG_PC REG_RIP
   103 #define REG_FP REG_RBP
   104 #else
   105 size_t os::Solaris::min_stack_allowed = 64*K;
   106 #define REG_SP UESP
   107 #define REG_PC EIP
   108 #define REG_FP EBP
   109 // 4900493 counter to prevent runaway LDTR refresh attempt
   111 static volatile int ldtr_refresh = 0;
   112 // the libthread instruction that faults because of the stale LDTR
   114 static const unsigned char movlfs[] = { 0x8e, 0xe0    // movl %eax,%fs
   115                        };
   116 #endif // AMD64
   118 char* os::non_memory_address_word() {
   119   // Must never look like an address returned by reserve_memory,
   120   // even in its subfields (as defined by the CPU immediate fields,
   121   // if the CPU splits constants across multiple instructions).
   122   return (char*) -1;
   123 }
   125 //
   126 // Validate a ucontext retrieved from walking a uc_link of a ucontext.
   127 // There are issues with libthread giving out uc_links for different threads
   128 // on the same uc_link chain and bad or circular links.
   129 //
   130 bool os::Solaris::valid_ucontext(Thread* thread, ucontext_t* valid, ucontext_t* suspect) {
   131   if (valid >= suspect ||
   132       valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags ||
   133       valid->uc_stack.ss_sp    != suspect->uc_stack.ss_sp    ||
   134       valid->uc_stack.ss_size  != suspect->uc_stack.ss_size) {
   135     DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");)
   136     return false;
   137   }
   139   if (thread->is_Java_thread()) {
   140     if (!valid_stack_address(thread, (address)suspect)) {
   141       DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");)
   142       return false;
   143     }
   144     if (!valid_stack_address(thread,  (address) suspect->uc_mcontext.gregs[REG_SP])) {
   145       DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");)
   146       return false;
   147     }
   148   }
   149   return true;
   150 }
   152 // We will only follow one level of uc_link since there are libthread
   153 // issues with ucontext linking and it is better to be safe and just
   154 // let caller retry later.
   155 ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread,
   156   ucontext_t *uc) {
   158   ucontext_t *retuc = NULL;
   160   if (uc != NULL) {
   161     if (uc->uc_link == NULL) {
   162       // cannot validate without uc_link so accept current ucontext
   163       retuc = uc;
   164     } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
   165       // first ucontext is valid so try the next one
   166       uc = uc->uc_link;
   167       if (uc->uc_link == NULL) {
   168         // cannot validate without uc_link so accept current ucontext
   169         retuc = uc;
   170       } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
   171         // the ucontext one level down is also valid so return it
   172         retuc = uc;
   173       }
   174     }
   175   }
   176   return retuc;
   177 }
   179 // Assumes ucontext is valid
   180 ExtendedPC os::Solaris::ucontext_get_ExtendedPC(ucontext_t *uc) {
   181   return ExtendedPC((address)uc->uc_mcontext.gregs[REG_PC]);
   182 }
   184 // Assumes ucontext is valid
   185 intptr_t* os::Solaris::ucontext_get_sp(ucontext_t *uc) {
   186   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
   187 }
   189 // Assumes ucontext is valid
   190 intptr_t* os::Solaris::ucontext_get_fp(ucontext_t *uc) {
   191   return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
   192 }
   194 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
   195 // is currently interrupted by SIGPROF.
   196 //
   197 // The difference between this and os::fetch_frame_from_context() is that
   198 // here we try to skip nested signal frames.
   199 ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread,
   200   ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
   202   assert(thread != NULL, "just checking");
   203   assert(ret_sp != NULL, "just checking");
   204   assert(ret_fp != NULL, "just checking");
   206   ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc);
   207   return os::fetch_frame_from_context(luc, ret_sp, ret_fp);
   208 }
   210 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
   211                     intptr_t** ret_sp, intptr_t** ret_fp) {
   213   ExtendedPC  epc;
   214   ucontext_t *uc = (ucontext_t*)ucVoid;
   216   if (uc != NULL) {
   217     epc = os::Solaris::ucontext_get_ExtendedPC(uc);
   218     if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc);
   219     if (ret_fp) *ret_fp = os::Solaris::ucontext_get_fp(uc);
   220   } else {
   221     // construct empty ExtendedPC for return value checking
   222     epc = ExtendedPC(NULL);
   223     if (ret_sp) *ret_sp = (intptr_t *)NULL;
   224     if (ret_fp) *ret_fp = (intptr_t *)NULL;
   225   }
   227   return epc;
   228 }
   230 frame os::fetch_frame_from_context(void* ucVoid) {
   231   intptr_t* sp;
   232   intptr_t* fp;
   233   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
   234   return frame(sp, fp, epc.pc());
   235 }
   237 frame os::get_sender_for_C_frame(frame* fr) {
   238   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
   239 }
   241 extern "C" intptr_t *_get_current_fp();  // in .il file
   243 frame os::current_frame() {
   244   intptr_t* fp = _get_current_fp();  // it's inlined so want current fp
   245   frame myframe((intptr_t*)os::current_stack_pointer(),
   246                 (intptr_t*)fp,
   247                 CAST_FROM_FN_PTR(address, os::current_frame));
   248   if (os::is_first_C_frame(&myframe)) {
   249     // stack is not walkable
   250     frame ret; // This will be a null useless frame
   251     return ret;
   252   } else {
   253     return os::get_sender_for_C_frame(&myframe);
   254   }
   255 }
   257 // This is a simple callback that just fetches a PC for an interrupted thread.
   258 // The thread need not be suspended and the fetched PC is just a hint.
   259 // This one is currently used for profiling the VMThread ONLY!
   261 // Must be synchronous
   262 void GetThreadPC_Callback::execute(OSThread::InterruptArguments *args) {
   263   Thread*     thread = args->thread();
   264   ucontext_t* uc     = args->ucontext();
   265   intptr_t* sp;
   267   assert(ProfileVM && thread->is_VM_thread(), "just checking");
   269   ExtendedPC new_addr((address)uc->uc_mcontext.gregs[REG_PC]);
   270   _addr = new_addr;
   271 }
   273 static int threadgetstate(thread_t tid, int *flags, lwpid_t *lwp, stack_t *ss, gregset_t rs, lwpstatus_t *lwpstatus) {
   274   char lwpstatusfile[PROCFILE_LENGTH];
   275   int lwpfd, err;
   277   if (err = os::Solaris::thr_getstate(tid, flags, lwp, ss, rs))
   278     return (err);
   279   if (*flags == TRS_LWPID) {
   280     sprintf(lwpstatusfile, "/proc/%d/lwp/%d/lwpstatus", getpid(),
   281             *lwp);
   282     if ((lwpfd = open(lwpstatusfile, O_RDONLY)) < 0) {
   283       perror("thr_mutator_status: open lwpstatus");
   284       return (EINVAL);
   285     }
   286     if (pread(lwpfd, lwpstatus, sizeof (lwpstatus_t), (off_t)0) !=
   287         sizeof (lwpstatus_t)) {
   288       perror("thr_mutator_status: read lwpstatus");
   289       (void) close(lwpfd);
   290       return (EINVAL);
   291     }
   292     (void) close(lwpfd);
   293   }
   294   return (0);
   295 }
   297 #ifndef AMD64
   299 // Detecting SSE support by OS
   300 // From solaris_i486.s
   301 extern "C" bool sse_check();
   302 extern "C" bool sse_unavailable();
   304 enum { SSE_UNKNOWN, SSE_NOT_SUPPORTED, SSE_SUPPORTED};
   305 static int sse_status = SSE_UNKNOWN;
   308 static void  check_for_sse_support() {
   309   if (!VM_Version::supports_sse()) {
   310     sse_status = SSE_NOT_SUPPORTED;
   311     return;
   312   }
   313   // looking for _sse_hw in libc.so, if it does not exist or
   314   // the value (int) is 0, OS has no support for SSE
   315   int *sse_hwp;
   316   void *h;
   318   if ((h=dlopen("/usr/lib/libc.so", RTLD_LAZY)) == NULL) {
   319     //open failed, presume no support for SSE
   320     sse_status = SSE_NOT_SUPPORTED;
   321     return;
   322   }
   323   if ((sse_hwp = (int *)dlsym(h, "_sse_hw")) == NULL) {
   324     sse_status = SSE_NOT_SUPPORTED;
   325   } else if (*sse_hwp == 0) {
   326     sse_status = SSE_NOT_SUPPORTED;
   327   }
   328   dlclose(h);
   330   if (sse_status == SSE_UNKNOWN) {
   331     bool (*try_sse)() = (bool (*)())sse_check;
   332     sse_status = (*try_sse)() ? SSE_SUPPORTED : SSE_NOT_SUPPORTED;
   333   }
   335 }
   337 #endif // AMD64
   339 bool os::supports_sse() {
   340 #ifdef AMD64
   341   return true;
   342 #else
   343   if (sse_status == SSE_UNKNOWN)
   344     check_for_sse_support();
   345   return sse_status == SSE_SUPPORTED;
   346 #endif // AMD64
   347 }
   349 bool os::is_allocatable(size_t bytes) {
   350 #ifdef AMD64
   351   return true;
   352 #else
   354   if (bytes < 2 * G) {
   355     return true;
   356   }
   358   char* addr = reserve_memory(bytes, NULL);
   360   if (addr != NULL) {
   361     release_memory(addr, bytes);
   362   }
   364   return addr != NULL;
   365 #endif // AMD64
   367 }
   369 extern "C" int JVM_handle_solaris_signal(int signo, siginfo_t* siginfo, void* ucontext, int abort_if_unrecognized);
   371 extern "C" void Fetch32PFI () ;
   372 extern "C" void Fetch32Resume () ;
   373 #ifdef AMD64
   374 extern "C" void FetchNPFI () ;
   375 extern "C" void FetchNResume () ;
   376 #endif // AMD64
   378 int JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid, int abort_if_unrecognized) {
   379   ucontext_t* uc = (ucontext_t*) ucVoid;
   381 #ifndef AMD64
   382   if (sig == SIGILL && info->si_addr == (caddr_t)sse_check) {
   383     // the SSE instruction faulted. supports_sse() need return false.
   384     uc->uc_mcontext.gregs[EIP] = (greg_t)sse_unavailable;
   385     return true;
   386   }
   387 #endif // !AMD64
   389   Thread* t = ThreadLocalStorage::get_thread_slow();  // slow & steady
   391   SignalHandlerMark shm(t);
   393   if(sig == SIGPIPE || sig == SIGXFSZ) {
   394     if (os::Solaris::chained_handler(sig, info, ucVoid)) {
   395       return true;
   396     } else {
   397       if (PrintMiscellaneous && (WizardMode || Verbose)) {
   398         char buf[64];
   399         warning("Ignoring %s - see 4229104 or 6499219",
   400                 os::exception_name(sig, buf, sizeof(buf)));
   402       }
   403       return true;
   404     }
   405   }
   407   JavaThread* thread = NULL;
   408   VMThread* vmthread = NULL;
   410   if (os::Solaris::signal_handlers_are_installed) {
   411     if (t != NULL ){
   412       if(t->is_Java_thread()) {
   413         thread = (JavaThread*)t;
   414       }
   415       else if(t->is_VM_thread()){
   416         vmthread = (VMThread *)t;
   417       }
   418     }
   419   }
   421   guarantee(sig != os::Solaris::SIGinterrupt(), "Can not chain VM interrupt signal, try -XX:+UseAltSigs");
   423   if (sig == os::Solaris::SIGasync()) {
   424     if(thread){
   425       OSThread::InterruptArguments args(thread, uc);
   426       thread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
   427       return true;
   428     }
   429     else if(vmthread){
   430       OSThread::InterruptArguments args(vmthread, uc);
   431       vmthread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
   432       return true;
   433     } else if (os::Solaris::chained_handler(sig, info, ucVoid)) {
   434       return true;
   435     } else {
   436       // If os::Solaris::SIGasync not chained, and this is a non-vm and
   437       // non-java thread
   438       return true;
   439     }
   440   }
   442   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
   443     // can't decode this kind of signal
   444     info = NULL;
   445   } else {
   446     assert(sig == info->si_signo, "bad siginfo");
   447   }
   449   // decide if this trap can be handled by a stub
   450   address stub = NULL;
   452   address pc          = NULL;
   454   //%note os_trap_1
   455   if (info != NULL && uc != NULL && thread != NULL) {
   456     // factor me: getPCfromContext
   457     pc = (address) uc->uc_mcontext.gregs[REG_PC];
   459     // SafeFetch32() support
   460     if (pc == (address) Fetch32PFI) {
   461       uc->uc_mcontext.gregs[REG_PC] = intptr_t(Fetch32Resume) ;
   462       return true ;
   463     }
   464 #ifdef AMD64
   465     if (pc == (address) FetchNPFI) {
   466        uc->uc_mcontext.gregs [REG_PC] = intptr_t(FetchNResume) ;
   467        return true ;
   468     }
   469 #endif // AMD64
   471     // Handle ALL stack overflow variations here
   472     if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
   473       address addr = (address) info->si_addr;
   474       if (thread->in_stack_yellow_zone(addr)) {
   475         thread->disable_stack_yellow_zone();
   476         if (thread->thread_state() == _thread_in_Java) {
   477           // Throw a stack overflow exception.  Guard pages will be reenabled
   478           // while unwinding the stack.
   479           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
   480         } else {
   481           // Thread was in the vm or native code.  Return and try to finish.
   482           return true;
   483         }
   484       } else if (thread->in_stack_red_zone(addr)) {
   485         // Fatal red zone violation.  Disable the guard pages and fall through
   486         // to handle_unexpected_exception way down below.
   487         thread->disable_stack_red_zone();
   488         tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
   489       }
   490     }
   492     if (thread->thread_state() == _thread_in_vm) {
   493       if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) {
   494         stub = StubRoutines::handler_for_unsafe_access();
   495       }
   496     }
   498     if (thread->thread_state() == _thread_in_Java) {
   499       // Support Safepoint Polling
   500       if ( sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
   501         stub = SharedRuntime::get_poll_stub(pc);
   502       }
   503       else if (sig == SIGBUS && info->si_code == BUS_OBJERR) {
   504         // BugId 4454115: A read from a MappedByteBuffer can fault
   505         // here if the underlying file has been truncated.
   506         // Do not crash the VM in such a case.
   507         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
   508         nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
   509         if (nm != NULL && nm->has_unsafe_access()) {
   510           stub = StubRoutines::handler_for_unsafe_access();
   511         }
   512       }
   513       else
   514       if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
   515         // integer divide by zero
   516         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
   517       }
   518 #ifndef AMD64
   519       else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) {
   520         // floating-point divide by zero
   521         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
   522       }
   523       else if (sig == SIGFPE && info->si_code == FPE_FLTINV) {
   524         // The encoding of D2I in i486.ad can cause an exception prior
   525         // to the fist instruction if there was an invalid operation
   526         // pending. We want to dismiss that exception. From the win_32
   527         // side it also seems that if it really was the fist causing
   528         // the exception that we do the d2i by hand with different
   529         // rounding. Seems kind of weird. QQQ TODO
   530         // Note that we take the exception at the NEXT floating point instruction.
   531         if (pc[0] == 0xDB) {
   532             assert(pc[0] == 0xDB, "not a FIST opcode");
   533             assert(pc[1] == 0x14, "not a FIST opcode");
   534             assert(pc[2] == 0x24, "not a FIST opcode");
   535             return true;
   536         } else {
   537             assert(pc[-3] == 0xDB, "not an flt invalid opcode");
   538             assert(pc[-2] == 0x14, "not an flt invalid opcode");
   539             assert(pc[-1] == 0x24, "not an flt invalid opcode");
   540         }
   541       }
   542       else if (sig == SIGFPE ) {
   543         tty->print_cr("caught SIGFPE, info 0x%x.", info->si_code);
   544       }
   545 #endif // !AMD64
   547         // QQQ It doesn't seem that we need to do this on x86 because we should be able
   548         // to return properly from the handler without this extra stuff on the back side.
   550       else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
   551         // Determination of interpreter/vtable stub/compiled code null exception
   552         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
   553       }
   554     }
   556     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
   557     // and the heap gets shrunk before the field access.
   558     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
   559       address addr = JNI_FastGetField::find_slowcase_pc(pc);
   560       if (addr != (address)-1) {
   561         stub = addr;
   562       }
   563     }
   565     // Check to see if we caught the safepoint code in the
   566     // process of write protecting the memory serialization page.
   567     // It write enables the page immediately after protecting it
   568     // so we can just return to retry the write.
   569     if ((sig == SIGSEGV) &&
   570         os::is_memory_serialize_page(thread, (address)info->si_addr)) {
   571       // Block current thread until the memory serialize page permission restored.
   572       os::block_on_serialize_page_trap();
   573       return true;
   574     }
   575   }
   577   // Execution protection violation
   578   //
   579   // Preventative code for future versions of Solaris which may
   580   // enable execution protection when running the 32-bit VM on AMD64.
   581   //
   582   // This should be kept as the last step in the triage.  We don't
   583   // have a dedicated trap number for a no-execute fault, so be
   584   // conservative and allow other handlers the first shot.
   585   //
   586   // Note: We don't test that info->si_code == SEGV_ACCERR here.
   587   // this si_code is so generic that it is almost meaningless; and
   588   // the si_code for this condition may change in the future.
   589   // Furthermore, a false-positive should be harmless.
   590   if (UnguardOnExecutionViolation > 0 &&
   591       (sig == SIGSEGV || sig == SIGBUS) &&
   592       uc->uc_mcontext.gregs[TRAPNO] == T_PGFLT) {  // page fault
   593     int page_size = os::vm_page_size();
   594     address addr = (address) info->si_addr;
   595     address pc = (address) uc->uc_mcontext.gregs[REG_PC];
   596     // Make sure the pc and the faulting address are sane.
   597     //
   598     // If an instruction spans a page boundary, and the page containing
   599     // the beginning of the instruction is executable but the following
   600     // page is not, the pc and the faulting address might be slightly
   601     // different - we still want to unguard the 2nd page in this case.
   602     //
   603     // 15 bytes seems to be a (very) safe value for max instruction size.
   604     bool pc_is_near_addr =
   605       (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
   606     bool instr_spans_page_boundary =
   607       (align_size_down((intptr_t) pc ^ (intptr_t) addr,
   608                        (intptr_t) page_size) > 0);
   610     if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
   611       static volatile address last_addr =
   612         (address) os::non_memory_address_word();
   614       // In conservative mode, don't unguard unless the address is in the VM
   615       if (addr != last_addr &&
   616           (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
   618         // Make memory rwx and retry
   619         address page_start =
   620           (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
   621         bool res = os::protect_memory((char*) page_start, page_size,
   622                                       os::MEM_PROT_RWX);
   624         if (PrintMiscellaneous && Verbose) {
   625           char buf[256];
   626           jio_snprintf(buf, sizeof(buf), "Execution protection violation "
   627                        "at " INTPTR_FORMAT
   628                        ", unguarding " INTPTR_FORMAT ": %s, errno=%d", addr,
   629                        page_start, (res ? "success" : "failed"), errno);
   630           tty->print_raw_cr(buf);
   631         }
   632         stub = pc;
   634         // Set last_addr so if we fault again at the same address, we don't end
   635         // up in an endless loop.
   636         //
   637         // There are two potential complications here.  Two threads trapping at
   638         // the same address at the same time could cause one of the threads to
   639         // think it already unguarded, and abort the VM.  Likely very rare.
   640         //
   641         // The other race involves two threads alternately trapping at
   642         // different addresses and failing to unguard the page, resulting in
   643         // an endless loop.  This condition is probably even more unlikely than
   644         // the first.
   645         //
   646         // Although both cases could be avoided by using locks or thread local
   647         // last_addr, these solutions are unnecessary complication: this
   648         // handler is a best-effort safety net, not a complete solution.  It is
   649         // disabled by default and should only be used as a workaround in case
   650         // we missed any no-execute-unsafe VM code.
   652         last_addr = addr;
   653       }
   654     }
   655   }
   657   if (stub != NULL) {
   658     // save all thread context in case we need to restore it
   660     if (thread != NULL) thread->set_saved_exception_pc(pc);
   661     // 12/02/99: On Sparc it appears that the full context is also saved
   662     // but as yet, no one looks at or restores that saved context
   663     // factor me: setPC
   664     uc->uc_mcontext.gregs[REG_PC] = (greg_t)stub;
   665     return true;
   666   }
   668   // signal-chaining
   669   if (os::Solaris::chained_handler(sig, info, ucVoid)) {
   670     return true;
   671   }
   673 #ifndef AMD64
   674   // Workaround (bug 4900493) for Solaris kernel bug 4966651.
   675   // Handle an undefined selector caused by an attempt to assign
   676   // fs in libthread getipriptr(). With the current libthread design every 512
   677   // thread creations the LDT for a private thread data structure is extended
   678   // and thre is a hazard that and another thread attempting a thread creation
   679   // will use a stale LDTR that doesn't reflect the structure's growth,
   680   // causing a GP fault.
   681   // Enforce the probable limit of passes through here to guard against an
   682   // infinite loop if some other move to fs caused the GP fault. Note that
   683   // this loop counter is ultimately a heuristic as it is possible for
   684   // more than one thread to generate this fault at a time in an MP system.
   685   // In the case of the loop count being exceeded or if the poll fails
   686   // just fall through to a fatal error.
   687   // If there is some other source of T_GPFLT traps and the text at EIP is
   688   // unreadable this code will loop infinitely until the stack is exausted.
   689   // The key to diagnosis in this case is to look for the bottom signal handler
   690   // frame.
   692   if(! IgnoreLibthreadGPFault) {
   693     if (sig == SIGSEGV && uc->uc_mcontext.gregs[TRAPNO] == T_GPFLT) {
   694       const unsigned char *p =
   695                         (unsigned const char *) uc->uc_mcontext.gregs[EIP];
   697       // Expected instruction?
   699       if(p[0] == movlfs[0] && p[1] == movlfs[1]) {
   701         Atomic::inc(&ldtr_refresh);
   703         // Infinite loop?
   705         if(ldtr_refresh < ((2 << 16) / PAGESIZE)) {
   707           // No, force scheduling to get a fresh view of the LDTR
   709           if(poll(NULL, 0, 10) == 0) {
   711             // Retry the move
   713             return false;
   714           }
   715         }
   716       }
   717     }
   718   }
   719 #endif // !AMD64
   721   if (!abort_if_unrecognized) {
   722     // caller wants another chance, so give it to him
   723     return false;
   724   }
   726   if (!os::Solaris::libjsig_is_loaded) {
   727     struct sigaction oldAct;
   728     sigaction(sig, (struct sigaction *)0, &oldAct);
   729     if (oldAct.sa_sigaction != signalHandler) {
   730       void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oldAct.sa_sigaction)
   731                                           : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
   732       warning("Unexpected Signal %d occurred under user-defined signal handler %#lx", sig, (long)sighand);
   733     }
   734   }
   736   if (pc == NULL && uc != NULL) {
   737     pc = (address) uc->uc_mcontext.gregs[REG_PC];
   738   }
   740   // unmask current signal
   741   sigset_t newset;
   742   sigemptyset(&newset);
   743   sigaddset(&newset, sig);
   744   sigprocmask(SIG_UNBLOCK, &newset, NULL);
   746   VMError err(t, sig, pc, info, ucVoid);
   747   err.report_and_die();
   749   ShouldNotReachHere();
   750 }
   752 void os::print_context(outputStream *st, void *context) {
   753   if (context == NULL) return;
   755   ucontext_t *uc = (ucontext_t*)context;
   756   st->print_cr("Registers:");
   757 #ifdef AMD64
   758   st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
   759   st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
   760   st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
   761   st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
   762   st->cr();
   763   st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
   764   st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
   765   st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
   766   st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
   767   st->cr();
   768   st->print(  "R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
   769   st->print(", R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
   770   st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
   771   st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
   772   st->cr();
   773   st->print(  "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
   774   st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
   775   st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
   776   st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
   777   st->cr();
   778   st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
   779   st->print(", RFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RFL]);
   780 #else
   781   st->print(  "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EAX]);
   782   st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBX]);
   783   st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ECX]);
   784   st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDX]);
   785   st->cr();
   786   st->print(  "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[UESP]);
   787   st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBP]);
   788   st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ESI]);
   789   st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDI]);
   790   st->cr();
   791   st->print(  "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EIP]);
   792   st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EFL]);
   793 #endif // AMD64
   794   st->cr();
   795   st->cr();
   797   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
   798   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
   799   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
   800   st->cr();
   802   // Note: it may be unsafe to inspect memory near pc. For example, pc may
   803   // point to garbage if entry point in an nmethod is corrupted. Leave
   804   // this at the end, and hope for the best.
   805   ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
   806   address pc = epc.pc();
   807   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
   808   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
   809 }
   811 void os::print_register_info(outputStream *st, void *context) {
   812   if (context == NULL) return;
   814   ucontext_t *uc = (ucontext_t*)context;
   816   st->print_cr("Register to memory mapping:");
   817   st->cr();
   819   // this is horrendously verbose but the layout of the registers in the
   820   // context does not match how we defined our abstract Register set, so
   821   // we can't just iterate through the gregs area
   823   // this is only for the "general purpose" registers
   825 #ifdef AMD64
   826   st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
   827   st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
   828   st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
   829   st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
   830   st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
   831   st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
   832   st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
   833   st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
   834   st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
   835   st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
   836   st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
   837   st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
   838   st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
   839   st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
   840   st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
   841   st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
   842 #else
   843   st->print("EAX="); print_location(st, uc->uc_mcontext.gregs[EAX]);
   844   st->print("EBX="); print_location(st, uc->uc_mcontext.gregs[EBX]);
   845   st->print("ECX="); print_location(st, uc->uc_mcontext.gregs[ECX]);
   846   st->print("EDX="); print_location(st, uc->uc_mcontext.gregs[EDX]);
   847   st->print("ESP="); print_location(st, uc->uc_mcontext.gregs[UESP]);
   848   st->print("EBP="); print_location(st, uc->uc_mcontext.gregs[EBP]);
   849   st->print("ESI="); print_location(st, uc->uc_mcontext.gregs[ESI]);
   850   st->print("EDI="); print_location(st, uc->uc_mcontext.gregs[EDI]);
   851 #endif
   853   st->cr();
   854 }
   857 #ifdef AMD64
   858 void os::Solaris::init_thread_fpu_state(void) {
   859   // Nothing to do
   860 }
   861 #else
   862 // From solaris_i486.s
   863 extern "C" void fixcw();
   865 void os::Solaris::init_thread_fpu_state(void) {
   866   // Set fpu to 53 bit precision. This happens too early to use a stub.
   867   fixcw();
   868 }
   870 // These routines are the initial value of atomic_xchg_entry(),
   871 // atomic_cmpxchg_entry(), atomic_inc_entry() and fence_entry()
   872 // until initialization is complete.
   873 // TODO - replace with .il implementation when compiler supports it.
   875 typedef jint  xchg_func_t        (jint,  volatile jint*);
   876 typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
   877 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
   878 typedef jint  add_func_t         (jint,  volatile jint*);
   880 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
   881   // try to use the stub:
   882   xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
   884   if (func != NULL) {
   885     os::atomic_xchg_func = func;
   886     return (*func)(exchange_value, dest);
   887   }
   888   assert(Threads::number_of_threads() == 0, "for bootstrap only");
   890   jint old_value = *dest;
   891   *dest = exchange_value;
   892   return old_value;
   893 }
   895 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
   896   // try to use the stub:
   897   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
   899   if (func != NULL) {
   900     os::atomic_cmpxchg_func = func;
   901     return (*func)(exchange_value, dest, compare_value);
   902   }
   903   assert(Threads::number_of_threads() == 0, "for bootstrap only");
   905   jint old_value = *dest;
   906   if (old_value == compare_value)
   907     *dest = exchange_value;
   908   return old_value;
   909 }
   911 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
   912   // try to use the stub:
   913   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
   915   if (func != NULL) {
   916     os::atomic_cmpxchg_long_func = func;
   917     return (*func)(exchange_value, dest, compare_value);
   918   }
   919   assert(Threads::number_of_threads() == 0, "for bootstrap only");
   921   jlong old_value = *dest;
   922   if (old_value == compare_value)
   923     *dest = exchange_value;
   924   return old_value;
   925 }
   927 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
   928   // try to use the stub:
   929   add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
   931   if (func != NULL) {
   932     os::atomic_add_func = func;
   933     return (*func)(add_value, dest);
   934   }
   935   assert(Threads::number_of_threads() == 0, "for bootstrap only");
   937   return (*dest) += add_value;
   938 }
   940 xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
   941 cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
   942 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
   943 add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
   945 extern "C" void _solaris_raw_setup_fpu(address ptr);
   946 void os::setup_fpu() {
   947   address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();
   948   _solaris_raw_setup_fpu(fpu_cntrl);
   949 }
   950 #endif // AMD64

mercurial