1 /*
   2  * Copyright (c) 1999, 2011, 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  */
  24 
  25 // no precompiled headers
  26 #include "assembler_sparc.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_sparc.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/interfaceSupport.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/javaCalls.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/sharedRuntime.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/timer.hpp"
  52 #include "thread_solaris.inline.hpp"
  53 #include "utilities/events.hpp"
  54 #include "utilities/vmError.hpp"
  55 #ifdef COMPILER1
  56 #include "c1/c1_Runtime1.hpp"
  57 #endif
  58 #ifdef COMPILER2
  59 #include "opto/runtime.hpp"
  60 #endif
  61 
  62 
  63 # include <signal.h>        // needed first to avoid name collision for "std" with SC 5.0
  64 
  65 // put OS-includes here
  66 # include <sys/types.h>
  67 # include <sys/mman.h>
  68 # include <pthread.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/lwp.h>
  82 # include <pwd.h>
  83 # include <poll.h>
  84 # include <sys/lwp.h>
  85 
  86 # define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
  87 # include <sys/procfs.h>     //  see comment in <sys/procfs.h>
  88 
  89 #define MAX_PATH (2 * K)
  90 
  91 // Minimum stack size for the VM.  It's easier to document a constant
  92 // but it's different for x86 and sparc because the page sizes are different.
  93 #ifdef _LP64
  94 size_t os::Solaris::min_stack_allowed = 128*K;
  95 #else
  96 size_t os::Solaris::min_stack_allowed = 96*K;
  97 #endif
  98 
  99 int os::Solaris::max_register_window_saves_before_flushing() {
 100   // We should detect this at run time. For now, filling
 101   // in with a constant.
 102   return 8;
 103 }
 104 
 105 static void handle_unflushed_register_windows(gwindows_t *win) {
 106   int restore_count = win->wbcnt;
 107   int i;
 108 
 109   for(i=0; i<restore_count; i++) {
 110     address sp = ((address)win->spbuf[i]) + STACK_BIAS;
 111     address reg_win = (address)&win->wbuf[i];
 112     memcpy(sp,reg_win,sizeof(struct rwindow));
 113   }
 114 }
 115 
 116 char* os::non_memory_address_word() {
 117   // Must never look like an address returned by reserve_memory,
 118   // even in its subfields (as defined by the CPU immediate fields,
 119   // if the CPU splits constants across multiple instructions).
 120   // On SPARC, 0 != %hi(any real address), because there is no
 121   // allocation in the first 1Kb of the virtual address space.
 122   return (char*) 0;
 123 }
 124 
 125 // Validate a ucontext retrieved from walking a uc_link of a ucontext.
 126 // There are issues with libthread giving out uc_links for different threads
 127 // on the same uc_link chain and bad or circular links.
 128 //
 129 bool os::Solaris::valid_ucontext(Thread* thread, ucontext_t* valid, ucontext_t* suspect) {
 130   if (valid >= suspect ||
 131       valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags ||
 132       valid->uc_stack.ss_sp    != suspect->uc_stack.ss_sp    ||
 133       valid->uc_stack.ss_size  != suspect->uc_stack.ss_size) {
 134     DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");)
 135     return false;
 136   }
 137 
 138   if (thread->is_Java_thread()) {
 139     if (!valid_stack_address(thread, (address)suspect)) {
 140       DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");)
 141       return false;
 142     }
 143     address _sp   = (address)((intptr_t)suspect->uc_mcontext.gregs[REG_SP] + STACK_BIAS);
 144     if (!valid_stack_address(thread, _sp) ||
 145         !frame::is_valid_stack_pointer(((JavaThread*)thread)->base_of_stack_pointer(), (intptr_t*)_sp)) {
 146       DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");)
 147       return false;
 148     }
 149   }
 150   return true;
 151 }
 152 
 153 // We will only follow one level of uc_link since there are libthread
 154 // issues with ucontext linking and it is better to be safe and just
 155 // let caller retry later.
 156 ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread,
 157   ucontext_t *uc) {
 158 
 159   ucontext_t *retuc = NULL;
 160 
 161   // Sometimes the topmost register windows are not properly flushed.
 162   // i.e., if the kernel would have needed to take a page fault
 163   if (uc != NULL && uc->uc_mcontext.gwins != NULL) {
 164     ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 165   }
 166 
 167   if (uc != NULL) {
 168     if (uc->uc_link == NULL) {
 169       // cannot validate without uc_link so accept current ucontext
 170       retuc = uc;
 171     } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 172       // first ucontext is valid so try the next one
 173       uc = uc->uc_link;
 174       if (uc->uc_link == NULL) {
 175         // cannot validate without uc_link so accept current ucontext
 176         retuc = uc;
 177       } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 178         // the ucontext one level down is also valid so return it
 179         retuc = uc;
 180       }
 181     }
 182   }
 183   return retuc;
 184 }
 185 
 186 // Assumes ucontext is valid
 187 ExtendedPC os::Solaris::ucontext_get_ExtendedPC(ucontext_t *uc) {
 188   address pc = (address)uc->uc_mcontext.gregs[REG_PC];
 189   // set npc to zero to avoid using it for safepoint, good for profiling only
 190   return ExtendedPC(pc);
 191 }
 192 
 193 // Assumes ucontext is valid
 194 intptr_t* os::Solaris::ucontext_get_sp(ucontext_t *uc) {
 195   return (intptr_t*)((intptr_t)uc->uc_mcontext.gregs[REG_SP] + STACK_BIAS);
 196 }
 197 
 198 // Solaris X86 only
 199 intptr_t* os::Solaris::ucontext_get_fp(ucontext_t *uc) {
 200   ShouldNotReachHere();
 201   return NULL;
 202 }
 203 
 204 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
 205 // is currently interrupted by SIGPROF.
 206 //
 207 // ret_fp parameter is only used by Solaris X86.
 208 //
 209 // The difference between this and os::fetch_frame_from_context() is that
 210 // here we try to skip nested signal frames.
 211 ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread,
 212   ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
 213 
 214   assert(thread != NULL, "just checking");
 215   assert(ret_sp != NULL, "just checking");
 216   assert(ret_fp == NULL, "just checking");
 217 
 218   ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc);
 219 
 220   return os::fetch_frame_from_context(luc, ret_sp, ret_fp);
 221 }
 222 
 223 
 224 // ret_fp parameter is only used by Solaris X86.
 225 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
 226                     intptr_t** ret_sp, intptr_t** ret_fp) {
 227 
 228   ExtendedPC  epc;
 229   ucontext_t *uc = (ucontext_t*)ucVoid;
 230 
 231   if (uc != NULL) {
 232     epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 233     if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc);
 234   } else {
 235     // construct empty ExtendedPC for return value checking
 236     epc = ExtendedPC(NULL);
 237     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 238   }
 239 
 240   return epc;
 241 }
 242 
 243 frame os::fetch_frame_from_context(void* ucVoid) {
 244   intptr_t* sp;
 245   intptr_t* fp;
 246   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 247   return frame(sp, frame::unpatchable, epc.pc());
 248 }
 249 
 250 frame os::get_sender_for_C_frame(frame* fr) {
 251   return frame(fr->sender_sp(), frame::unpatchable, fr->sender_pc());
 252 }
 253 
 254 frame os::current_frame() {
 255   intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
 256   frame myframe(sp, frame::unpatchable,
 257                 CAST_FROM_FN_PTR(address, os::current_frame));
 258   if (os::is_first_C_frame(&myframe)) {
 259     // stack is not walkable
 260     return frame(NULL, NULL, NULL);
 261   } else {
 262     return os::get_sender_for_C_frame(&myframe);
 263   }
 264 }
 265 
 266 
 267 void GetThreadPC_Callback::execute(OSThread::InterruptArguments *args) {
 268   Thread*     thread = args->thread();
 269   ucontext_t* uc     = args->ucontext();
 270   intptr_t* sp;
 271 
 272   assert(ProfileVM && thread->is_VM_thread(), "just checking");
 273 
 274   // Skip the mcontext corruption verification. If if occasionally
 275   // things get corrupt, it is ok for profiling - we will just get an unresolved
 276   // function name
 277   ExtendedPC new_addr((address)uc->uc_mcontext.gregs[REG_PC]);
 278   _addr = new_addr;
 279 }
 280 
 281 
 282 static int threadgetstate(thread_t tid, int *flags, lwpid_t *lwp, stack_t *ss, gregset_t rs, lwpstatus_t *lwpstatus) {
 283   char lwpstatusfile[PROCFILE_LENGTH];
 284   int lwpfd, err;
 285 
 286   if (err = os::Solaris::thr_getstate(tid, flags, lwp, ss, rs))
 287     return (err);
 288   if (*flags == TRS_LWPID) {
 289     sprintf(lwpstatusfile, "/proc/%d/lwp/%d/lwpstatus", getpid(),
 290             *lwp);
 291     if ((lwpfd = ::open(lwpstatusfile, O_RDONLY)) < 0) {
 292       perror("thr_mutator_status: open lwpstatus");
 293       return (EINVAL);
 294     }
 295     if (pread(lwpfd, lwpstatus, sizeof (lwpstatus_t), (off_t)0) !=
 296         sizeof (lwpstatus_t)) {
 297       perror("thr_mutator_status: read lwpstatus");
 298       (void) ::close(lwpfd);
 299       return (EINVAL);
 300     }
 301     (void) ::close(lwpfd);
 302   }
 303   return (0);
 304 }
 305 
 306 
 307 bool os::is_allocatable(size_t bytes) {
 308 #ifdef _LP64
 309    return true;
 310 #else
 311    return (bytes <= (size_t)3835*M);
 312 #endif
 313 }
 314 
 315 extern "C" void Fetch32PFI () ;
 316 extern "C" void Fetch32Resume () ;
 317 extern "C" void FetchNPFI () ;
 318 extern "C" void FetchNResume () ;
 319 
 320 extern "C" JNIEXPORT int
 321 JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid,
 322                           int abort_if_unrecognized) {
 323   ucontext_t* uc = (ucontext_t*) ucVoid;
 324 
 325   Thread* t = ThreadLocalStorage::get_thread_slow();
 326 
 327   SignalHandlerMark shm(t);
 328 
 329   if(sig == SIGPIPE || sig == SIGXFSZ) {
 330     if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 331       return true;
 332     } else {
 333       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 334         char buf[64];
 335         warning("Ignoring %s - see 4229104 or 6499219",
 336                 os::exception_name(sig, buf, sizeof(buf)));
 337 
 338       }
 339       return true;
 340     }
 341   }
 342 
 343   JavaThread* thread = NULL;
 344   VMThread* vmthread = NULL;
 345   if (os::Solaris::signal_handlers_are_installed) {
 346     if (t != NULL ){
 347       if(t->is_Java_thread()) {
 348         thread = (JavaThread*)t;
 349       }
 350       else if(t->is_VM_thread()){
 351         vmthread = (VMThread *)t;
 352       }
 353     }
 354   }
 355 
 356   guarantee(sig != os::Solaris::SIGinterrupt(), "Can not chain VM interrupt signal, try -XX:+UseAltSigs");
 357 
 358   if (sig == os::Solaris::SIGasync()) {
 359     if (thread) {
 360       OSThread::InterruptArguments args(thread, uc);
 361       thread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
 362       return true;
 363     } else if (vmthread) {
 364       OSThread::InterruptArguments args(vmthread, uc);
 365       vmthread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
 366       return true;
 367     } else if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 368       return true;
 369     } else {
 370       // If os::Solaris::SIGasync not chained, and this is a non-vm and
 371       // non-java thread
 372       return true;
 373     }
 374   }
 375 
 376   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 377     // can't decode this kind of signal
 378     info = NULL;
 379   } else {
 380     assert(sig == info->si_signo, "bad siginfo");
 381   }
 382 
 383   // decide if this trap can be handled by a stub
 384   address stub = NULL;
 385 
 386   address pc          = NULL;
 387   address npc         = NULL;
 388 
 389   //%note os_trap_1
 390   if (info != NULL && uc != NULL && thread != NULL) {
 391     // factor me: getPCfromContext
 392     pc  = (address) uc->uc_mcontext.gregs[REG_PC];
 393     npc = (address) uc->uc_mcontext.gregs[REG_nPC];
 394 
 395     // SafeFetch() support
 396     // Implemented with either a fixed set of addresses such
 397     // as Fetch32*, or with Thread._OnTrap.
 398     if (uc->uc_mcontext.gregs[REG_PC] == intptr_t(Fetch32PFI)) {
 399       uc->uc_mcontext.gregs [REG_PC]  = intptr_t(Fetch32Resume) ;
 400       uc->uc_mcontext.gregs [REG_nPC] = intptr_t(Fetch32Resume) + 4 ;
 401       return true ;
 402     }
 403     if (uc->uc_mcontext.gregs[REG_PC] == intptr_t(FetchNPFI)) {
 404       uc->uc_mcontext.gregs [REG_PC]  = intptr_t(FetchNResume) ;
 405       uc->uc_mcontext.gregs [REG_nPC] = intptr_t(FetchNResume) + 4 ;
 406       return true ;
 407     }
 408 
 409     // Handle ALL stack overflow variations here
 410     if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
 411       address addr = (address) info->si_addr;
 412       if (thread->in_stack_yellow_zone(addr)) {
 413         thread->disable_stack_yellow_zone();
 414         // Sometimes the register windows are not properly flushed.
 415         if(uc->uc_mcontext.gwins != NULL) {
 416           ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 417         }
 418         if (thread->thread_state() == _thread_in_Java) {
 419           // Throw a stack overflow exception.  Guard pages will be reenabled
 420           // while unwinding the stack.
 421           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 422         } else {
 423           // Thread was in the vm or native code.  Return and try to finish.
 424           return true;
 425         }
 426       } else if (thread->in_stack_red_zone(addr)) {
 427         // Fatal red zone violation.  Disable the guard pages and fall through
 428         // to handle_unexpected_exception way down below.
 429         thread->disable_stack_red_zone();
 430         tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 431         // Sometimes the register windows are not properly flushed.
 432         if(uc->uc_mcontext.gwins != NULL) {
 433           ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 434         }
 435       }
 436     }
 437 
 438 
 439     if (thread->thread_state() == _thread_in_vm) {
 440       if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) {
 441         stub = StubRoutines::handler_for_unsafe_access();
 442       }
 443     }
 444 
 445     else if (thread->thread_state() == _thread_in_Java) {
 446       // Java thread running in Java code => find exception handler if any
 447       // a fault inside compiled code, the interpreter, or a stub
 448 
 449       // Support Safepoint Polling
 450       if ( sig == SIGSEGV && (address)info->si_addr == os::get_polling_page() ) {
 451         stub = SharedRuntime::get_poll_stub(pc);
 452       }
 453 
 454       // Not needed on x86 solaris because verify_oops doesn't generate
 455       // SEGV/BUS like sparc does.
 456       if ( (sig == SIGSEGV || sig == SIGBUS)
 457            && pc >= MacroAssembler::_verify_oop_implicit_branch[0]
 458            && pc <  MacroAssembler::_verify_oop_implicit_branch[1] ) {
 459         stub     =  MacroAssembler::_verify_oop_implicit_branch[2];
 460         warning("fixed up memory fault in +VerifyOops at address " INTPTR_FORMAT, info->si_addr);
 461       }
 462 
 463       // This is not factored because on x86 solaris the patching for
 464       // zombies does not generate a SEGV.
 465       else if (sig == SIGSEGV && nativeInstruction_at(pc)->is_zombie()) {
 466         // zombie method (ld [%g0],%o7 instruction)
 467         stub = SharedRuntime::get_handle_wrong_method_stub();
 468 
 469         // At the stub it needs to look like a call from the caller of this
 470         // method (not a call from the segv site).
 471         pc = (address)uc->uc_mcontext.gregs[REG_O7];
 472       }
 473       else if (sig == SIGBUS && info->si_code == BUS_OBJERR) {
 474         // BugId 4454115: A read from a MappedByteBuffer can fault
 475         // here if the underlying file has been truncated.
 476         // Do not crash the VM in such a case.
 477         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 478         nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
 479         if (nm != NULL && nm->has_unsafe_access()) {
 480           stub = StubRoutines::handler_for_unsafe_access();
 481         }
 482       }
 483 
 484       else if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
 485         // integer divide by zero
 486         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 487       }
 488       else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) {
 489         // floating-point divide by zero
 490         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 491       }
 492 #ifdef COMPILER2
 493       else if (sig == SIGILL && nativeInstruction_at(pc)->is_ic_miss_trap()) {
 494 #ifdef ASSERT
 495   #ifdef TIERED
 496         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 497         assert(cb->is_compiled_by_c2(), "Wrong compiler");
 498   #endif // TIERED
 499 #endif // ASSERT
 500         // Inline cache missed and user trap "Tne G0+ST_RESERVED_FOR_USER_0+2" taken.
 501         stub = SharedRuntime::get_ic_miss_stub();
 502         // At the stub it needs to look like a call from the caller of this
 503         // method (not a call from the segv site).
 504         pc = (address)uc->uc_mcontext.gregs[REG_O7];
 505       }
 506 #endif  // COMPILER2
 507 
 508       else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
 509         // Determination of interpreter/vtable stub/compiled code null exception
 510         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 511       }
 512     }
 513 
 514     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 515     // and the heap gets shrunk before the field access.
 516     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 517       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 518       if (addr != (address)-1) {
 519         stub = addr;
 520       }
 521     }
 522 
 523     // Check to see if we caught the safepoint code in the
 524     // process of write protecting the memory serialization page.
 525     // It write enables the page immediately after protecting it
 526     // so just return.
 527     if ((sig == SIGSEGV) &&
 528         os::is_memory_serialize_page(thread, (address)info->si_addr)) {
 529       // Block current thread until the memory serialize page permission restored.
 530       os::block_on_serialize_page_trap();
 531       return true;
 532     }
 533   }
 534 
 535   if (stub != NULL) {
 536     // save all thread context in case we need to restore it
 537 
 538     thread->set_saved_exception_pc(pc);
 539     thread->set_saved_exception_npc(npc);
 540 
 541     // simulate a branch to the stub (a "call" in the safepoint stub case)
 542     // factor me: setPC
 543     uc->uc_mcontext.gregs[REG_PC ] = (greg_t)stub;
 544     uc->uc_mcontext.gregs[REG_nPC] = (greg_t)(stub + 4);
 545 
 546 #ifndef PRODUCT
 547     if (TraceJumps) thread->record_jump(stub, NULL, __FILE__, __LINE__);
 548 #endif /* PRODUCT */
 549 
 550     return true;
 551   }
 552 
 553   // signal-chaining
 554   if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 555     return true;
 556   }
 557 
 558   if (!abort_if_unrecognized) {
 559     // caller wants another chance, so give it to him
 560     return false;
 561   }
 562 
 563   if (!os::Solaris::libjsig_is_loaded) {
 564     struct sigaction oldAct;
 565     sigaction(sig, (struct sigaction *)0, &oldAct);
 566     if (oldAct.sa_sigaction != signalHandler) {
 567       void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
 568                                           : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
 569       warning("Unexpected Signal %d occurred under user-defined signal handler " INTPTR_FORMAT, sig, (intptr_t)sighand);
 570     }
 571   }
 572 
 573   if (pc == NULL && uc != NULL) {
 574     pc = (address) uc->uc_mcontext.gregs[REG_PC];
 575   }
 576 
 577   // Sometimes the register windows are not properly flushed.
 578   if(uc->uc_mcontext.gwins != NULL) {
 579     ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 580   }
 581 
 582   // unmask current signal
 583   sigset_t newset;
 584   sigemptyset(&newset);
 585   sigaddset(&newset, sig);
 586   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 587 
 588   // Determine which sort of error to throw.  Out of swap may signal
 589   // on the thread stack, which could get a mapping error when touched.
 590   address addr = (address) info->si_addr;
 591   if (sig == SIGBUS && info->si_code == BUS_OBJERR && info->si_errno == ENOMEM) {
 592     vm_exit_out_of_memory(0, "Out of swap space to map in thread stack.");
 593   }
 594 
 595   VMError err(t, sig, pc, info, ucVoid);
 596   err.report_and_die();
 597 
 598   ShouldNotReachHere();
 599 }
 600 
 601 void os::print_context(outputStream *st, void *context) {
 602   if (context == NULL) return;
 603 
 604   ucontext_t *uc = (ucontext_t*)context;
 605   st->print_cr("Registers:");
 606 
 607   st->print_cr(" G1=" INTPTR_FORMAT " G2=" INTPTR_FORMAT
 608                " G3=" INTPTR_FORMAT " G4=" INTPTR_FORMAT,
 609             uc->uc_mcontext.gregs[REG_G1],
 610             uc->uc_mcontext.gregs[REG_G2],
 611             uc->uc_mcontext.gregs[REG_G3],
 612             uc->uc_mcontext.gregs[REG_G4]);
 613   st->print_cr(" G5=" INTPTR_FORMAT " G6=" INTPTR_FORMAT
 614                " G7=" INTPTR_FORMAT " Y=" INTPTR_FORMAT,
 615             uc->uc_mcontext.gregs[REG_G5],
 616             uc->uc_mcontext.gregs[REG_G6],
 617             uc->uc_mcontext.gregs[REG_G7],
 618             uc->uc_mcontext.gregs[REG_Y]);
 619   st->print_cr(" O0=" INTPTR_FORMAT " O1=" INTPTR_FORMAT
 620                " O2=" INTPTR_FORMAT " O3=" INTPTR_FORMAT,
 621                  uc->uc_mcontext.gregs[REG_O0],
 622                  uc->uc_mcontext.gregs[REG_O1],
 623                  uc->uc_mcontext.gregs[REG_O2],
 624                  uc->uc_mcontext.gregs[REG_O3]);
 625   st->print_cr(" O4=" INTPTR_FORMAT " O5=" INTPTR_FORMAT
 626                " O6=" INTPTR_FORMAT " O7=" INTPTR_FORMAT,
 627             uc->uc_mcontext.gregs[REG_O4],
 628             uc->uc_mcontext.gregs[REG_O5],
 629             uc->uc_mcontext.gregs[REG_O6],
 630             uc->uc_mcontext.gregs[REG_O7]);
 631 
 632 
 633   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 634   st->print_cr(" L0=" INTPTR_FORMAT " L1=" INTPTR_FORMAT
 635                " L2=" INTPTR_FORMAT " L3=" INTPTR_FORMAT,
 636                sp[L0->sp_offset_in_saved_window()],
 637                sp[L1->sp_offset_in_saved_window()],
 638                sp[L2->sp_offset_in_saved_window()],
 639                sp[L3->sp_offset_in_saved_window()]);
 640   st->print_cr(" L4=" INTPTR_FORMAT " L5=" INTPTR_FORMAT
 641                " L6=" INTPTR_FORMAT " L7=" INTPTR_FORMAT,
 642                sp[L4->sp_offset_in_saved_window()],
 643                sp[L5->sp_offset_in_saved_window()],
 644                sp[L6->sp_offset_in_saved_window()],
 645                sp[L7->sp_offset_in_saved_window()]);
 646   st->print_cr(" I0=" INTPTR_FORMAT " I1=" INTPTR_FORMAT
 647                " I2=" INTPTR_FORMAT " I3=" INTPTR_FORMAT,
 648                sp[I0->sp_offset_in_saved_window()],
 649                sp[I1->sp_offset_in_saved_window()],
 650                sp[I2->sp_offset_in_saved_window()],
 651                sp[I3->sp_offset_in_saved_window()]);
 652   st->print_cr(" I4=" INTPTR_FORMAT " I5=" INTPTR_FORMAT
 653                " I6=" INTPTR_FORMAT " I7=" INTPTR_FORMAT,
 654                sp[I4->sp_offset_in_saved_window()],
 655                sp[I5->sp_offset_in_saved_window()],
 656                sp[I6->sp_offset_in_saved_window()],
 657                sp[I7->sp_offset_in_saved_window()]);
 658 
 659   st->print_cr(" PC=" INTPTR_FORMAT " nPC=" INTPTR_FORMAT,
 660             uc->uc_mcontext.gregs[REG_PC],
 661             uc->uc_mcontext.gregs[REG_nPC]);
 662   st->cr();
 663   st->cr();
 664 
 665   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
 666   print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
 667   st->cr();
 668 
 669   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 670   // point to garbage if entry point in an nmethod is corrupted. Leave
 671   // this at the end, and hope for the best.
 672   ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 673   address pc = epc.pc();
 674   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
 675   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 676 }
 677 
 678 void os::print_register_info(outputStream *st, void *context) {
 679   if (context == NULL) return;
 680 
 681   ucontext_t *uc = (ucontext_t*)context;
 682   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 683 
 684   st->print_cr("Register to memory mapping:");
 685   st->cr();
 686 
 687   // this is only for the "general purpose" registers
 688   st->print("G1="); print_location(st, uc->uc_mcontext.gregs[REG_G1]);
 689   st->print("G2="); print_location(st, uc->uc_mcontext.gregs[REG_G2]);
 690   st->print("G3="); print_location(st, uc->uc_mcontext.gregs[REG_G3]);
 691   st->print("G4="); print_location(st, uc->uc_mcontext.gregs[REG_G4]);
 692   st->print("G5="); print_location(st, uc->uc_mcontext.gregs[REG_G5]);
 693   st->print("G6="); print_location(st, uc->uc_mcontext.gregs[REG_G6]);
 694   st->print("G7="); print_location(st, uc->uc_mcontext.gregs[REG_G7]);
 695   st->cr();
 696 
 697   st->print("O0="); print_location(st, uc->uc_mcontext.gregs[REG_O0]);
 698   st->print("O1="); print_location(st, uc->uc_mcontext.gregs[REG_O1]);
 699   st->print("O2="); print_location(st, uc->uc_mcontext.gregs[REG_O2]);
 700   st->print("O3="); print_location(st, uc->uc_mcontext.gregs[REG_O3]);
 701   st->print("O4="); print_location(st, uc->uc_mcontext.gregs[REG_O4]);
 702   st->print("O5="); print_location(st, uc->uc_mcontext.gregs[REG_O5]);
 703   st->print("O6="); print_location(st, uc->uc_mcontext.gregs[REG_O6]);
 704   st->print("O7="); print_location(st, uc->uc_mcontext.gregs[REG_O7]);
 705   st->cr();
 706 
 707   st->print("L0="); print_location(st, sp[L0->sp_offset_in_saved_window()]);
 708   st->print("L1="); print_location(st, sp[L1->sp_offset_in_saved_window()]);
 709   st->print("L2="); print_location(st, sp[L2->sp_offset_in_saved_window()]);
 710   st->print("L3="); print_location(st, sp[L3->sp_offset_in_saved_window()]);
 711   st->print("L4="); print_location(st, sp[L4->sp_offset_in_saved_window()]);
 712   st->print("L5="); print_location(st, sp[L5->sp_offset_in_saved_window()]);
 713   st->print("L6="); print_location(st, sp[L6->sp_offset_in_saved_window()]);
 714   st->print("L7="); print_location(st, sp[L7->sp_offset_in_saved_window()]);
 715   st->cr();
 716 
 717   st->print("I0="); print_location(st, sp[I0->sp_offset_in_saved_window()]);
 718   st->print("I1="); print_location(st, sp[I1->sp_offset_in_saved_window()]);
 719   st->print("I2="); print_location(st, sp[I2->sp_offset_in_saved_window()]);
 720   st->print("I3="); print_location(st, sp[I3->sp_offset_in_saved_window()]);
 721   st->print("I4="); print_location(st, sp[I4->sp_offset_in_saved_window()]);
 722   st->print("I5="); print_location(st, sp[I5->sp_offset_in_saved_window()]);
 723   st->print("I6="); print_location(st, sp[I6->sp_offset_in_saved_window()]);
 724   st->print("I7="); print_location(st, sp[I7->sp_offset_in_saved_window()]);
 725   st->cr();
 726 }
 727 
 728 void os::Solaris::init_thread_fpu_state(void) {
 729     // Nothing needed on Sparc.
 730 }
 731 
 732 #if !defined(COMPILER2) && !defined(_LP64)
 733 
 734 // These routines are the initial value of atomic_xchg_entry(),
 735 // atomic_cmpxchg_entry(), atomic_add_entry() and fence_entry()
 736 // until initialization is complete.
 737 // TODO - remove when the VM drops support for V8.
 738 
 739 typedef jint  xchg_func_t        (jint,  volatile jint*);
 740 typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
 741 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
 742 typedef jint  add_func_t         (jint,  volatile jint*);
 743 
 744 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
 745   // try to use the stub:
 746   xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
 747 
 748   if (func != NULL) {
 749     os::atomic_xchg_func = func;
 750     return (*func)(exchange_value, dest);
 751   }
 752   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 753 
 754   jint old_value = *dest;
 755   *dest = exchange_value;
 756   return old_value;
 757 }
 758 
 759 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
 760   // try to use the stub:
 761   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
 762 
 763   if (func != NULL) {
 764     os::atomic_cmpxchg_func = func;
 765     return (*func)(exchange_value, dest, compare_value);
 766   }
 767   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 768 
 769   jint old_value = *dest;
 770   if (old_value == compare_value)
 771     *dest = exchange_value;
 772   return old_value;
 773 }
 774 
 775 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
 776   // try to use the stub:
 777   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
 778 
 779   if (func != NULL) {
 780     os::atomic_cmpxchg_long_func = func;
 781     return (*func)(exchange_value, dest, compare_value);
 782   }
 783   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 784 
 785   jlong old_value = *dest;
 786   if (old_value == compare_value)
 787     *dest = exchange_value;
 788   return old_value;
 789 }
 790 
 791 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
 792   // try to use the stub:
 793   add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
 794 
 795   if (func != NULL) {
 796     os::atomic_add_func = func;
 797     return (*func)(add_value, dest);
 798   }
 799   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 800 
 801   return (*dest) += add_value;
 802 }
 803 
 804 xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
 805 cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
 806 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
 807 add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
 808 
 809 #endif // !_LP64 && !COMPILER2
 810 
 811 #if defined(__sparc) && defined(COMPILER2) && defined(_GNU_SOURCE)
 812  // See file build/solaris/makefiles/$compiler.make
 813  // For compiler1 the architecture is v8 and frps isn't present in v8
 814  extern "C"  void _mark_fpu_nosave() {
 815    __asm__ __volatile__ ("wr %%g0, 0, %%fprs \n\t" : : :);
 816   }
 817 #endif //defined(__sparc) && defined(COMPILER2)