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