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 // Returns an estimate of the current stack pointer. Result must be guaranteed to
 255 // point into the calling threads stack, and be no lower than the current stack
 256 // pointer.
 257 address os::current_stack_pointer() {
 258   volatile int dummy;
 259   address sp = (address)&dummy + 8;     // %%%% need to confirm if this is right
 260   return sp;
 261 }
 262 
 263 frame os::current_frame() {
 264   intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
 265   frame myframe(sp, frame::unpatchable,
 266                 CAST_FROM_FN_PTR(address, os::current_frame));
 267   if (os::is_first_C_frame(&myframe)) {
 268     // stack is not walkable
 269     return frame(NULL, NULL, NULL);
 270   } else {
 271     return os::get_sender_for_C_frame(&myframe);
 272   }
 273 }
 274 
 275 
 276 void GetThreadPC_Callback::execute(OSThread::InterruptArguments *args) {
 277   Thread*     thread = args->thread();
 278   ucontext_t* uc     = args->ucontext();
 279   intptr_t* sp;
 280 
 281   assert(ProfileVM && thread->is_VM_thread(), "just checking");
 282 
 283   // Skip the mcontext corruption verification. If if occasionally
 284   // things get corrupt, it is ok for profiling - we will just get an unresolved
 285   // function name
 286   ExtendedPC new_addr((address)uc->uc_mcontext.gregs[REG_PC]);
 287   _addr = new_addr;
 288 }
 289 
 290 
 291 static int threadgetstate(thread_t tid, int *flags, lwpid_t *lwp, stack_t *ss, gregset_t rs, lwpstatus_t *lwpstatus) {
 292   char lwpstatusfile[PROCFILE_LENGTH];
 293   int lwpfd, err;
 294 
 295   if (err = os::Solaris::thr_getstate(tid, flags, lwp, ss, rs))
 296     return (err);
 297   if (*flags == TRS_LWPID) {
 298     sprintf(lwpstatusfile, "/proc/%d/lwp/%d/lwpstatus", getpid(),
 299             *lwp);
 300     if ((lwpfd = ::open(lwpstatusfile, O_RDONLY)) < 0) {
 301       perror("thr_mutator_status: open lwpstatus");
 302       return (EINVAL);
 303     }
 304     if (pread(lwpfd, lwpstatus, sizeof (lwpstatus_t), (off_t)0) !=
 305         sizeof (lwpstatus_t)) {
 306       perror("thr_mutator_status: read lwpstatus");
 307       (void) ::close(lwpfd);
 308       return (EINVAL);
 309     }
 310     (void) ::close(lwpfd);
 311   }
 312   return (0);
 313 }
 314 
 315 
 316 bool os::is_allocatable(size_t bytes) {
 317 #ifdef _LP64
 318    return true;
 319 #else
 320    return (bytes <= (size_t)3835*M);
 321 #endif
 322 }
 323 
 324 extern "C" void Fetch32PFI () ;
 325 extern "C" void Fetch32Resume () ;
 326 extern "C" void FetchNPFI () ;
 327 extern "C" void FetchNResume () ;
 328 
 329 extern "C" JNIEXPORT int
 330 JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid,
 331                           int abort_if_unrecognized) {
 332   ucontext_t* uc = (ucontext_t*) ucVoid;
 333 
 334   Thread* t = ThreadLocalStorage::get_thread_slow();
 335 
 336   SignalHandlerMark shm(t);
 337 
 338   if(sig == SIGPIPE || sig == SIGXFSZ) {
 339     if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 340       return true;
 341     } else {
 342       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 343         char buf[64];
 344         warning("Ignoring %s - see 4229104 or 6499219",
 345                 os::exception_name(sig, buf, sizeof(buf)));
 346 
 347       }
 348       return true;
 349     }
 350   }
 351 
 352   JavaThread* thread = NULL;
 353   VMThread* vmthread = NULL;
 354   if (os::Solaris::signal_handlers_are_installed) {
 355     if (t != NULL ){
 356       if(t->is_Java_thread()) {
 357         thread = (JavaThread*)t;
 358       }
 359       else if(t->is_VM_thread()){
 360         vmthread = (VMThread *)t;
 361       }
 362     }
 363   }
 364 
 365   guarantee(sig != os::Solaris::SIGinterrupt(), "Can not chain VM interrupt signal, try -XX:+UseAltSigs");
 366 
 367   if (sig == os::Solaris::SIGasync()) {
 368     if (thread) {
 369       OSThread::InterruptArguments args(thread, uc);
 370       thread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
 371       return true;
 372     } else if (vmthread) {
 373       OSThread::InterruptArguments args(vmthread, uc);
 374       vmthread->osthread()->do_interrupt_callbacks_at_interrupt(&args);
 375       return true;
 376     } else if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 377       return true;
 378     } else {
 379       // If os::Solaris::SIGasync not chained, and this is a non-vm and
 380       // non-java thread
 381       return true;
 382     }
 383   }
 384 
 385   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 386     // can't decode this kind of signal
 387     info = NULL;
 388   } else {
 389     assert(sig == info->si_signo, "bad siginfo");
 390   }
 391 
 392   // decide if this trap can be handled by a stub
 393   address stub = NULL;
 394 
 395   address pc          = NULL;
 396   address npc         = NULL;
 397 
 398   //%note os_trap_1
 399   if (info != NULL && uc != NULL && thread != NULL) {
 400     // factor me: getPCfromContext
 401     pc  = (address) uc->uc_mcontext.gregs[REG_PC];
 402     npc = (address) uc->uc_mcontext.gregs[REG_nPC];
 403 
 404     // SafeFetch() support
 405     // Implemented with either a fixed set of addresses such
 406     // as Fetch32*, or with Thread._OnTrap.
 407     if (uc->uc_mcontext.gregs[REG_PC] == intptr_t(Fetch32PFI)) {
 408       uc->uc_mcontext.gregs [REG_PC]  = intptr_t(Fetch32Resume) ;
 409       uc->uc_mcontext.gregs [REG_nPC] = intptr_t(Fetch32Resume) + 4 ;
 410       return true ;
 411     }
 412     if (uc->uc_mcontext.gregs[REG_PC] == intptr_t(FetchNPFI)) {
 413       uc->uc_mcontext.gregs [REG_PC]  = intptr_t(FetchNResume) ;
 414       uc->uc_mcontext.gregs [REG_nPC] = intptr_t(FetchNResume) + 4 ;
 415       return true ;
 416     }
 417 
 418     // Handle ALL stack overflow variations here
 419     if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
 420       address addr = (address) info->si_addr;
 421       if (thread->in_stack_yellow_zone(addr)) {
 422         thread->disable_stack_yellow_zone();
 423         // Sometimes the register windows are not properly flushed.
 424         if(uc->uc_mcontext.gwins != NULL) {
 425           ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 426         }
 427         if (thread->thread_state() == _thread_in_Java) {
 428           // Throw a stack overflow exception.  Guard pages will be reenabled
 429           // while unwinding the stack.
 430           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 431         } else {
 432           // Thread was in the vm or native code.  Return and try to finish.
 433           return true;
 434         }
 435       } else if (thread->in_stack_red_zone(addr)) {
 436         // Fatal red zone violation.  Disable the guard pages and fall through
 437         // to handle_unexpected_exception way down below.
 438         thread->disable_stack_red_zone();
 439         tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 440         // Sometimes the register windows are not properly flushed.
 441         if(uc->uc_mcontext.gwins != NULL) {
 442           ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 443         }
 444       }
 445     }
 446 
 447 
 448     if (thread->thread_state() == _thread_in_vm) {
 449       if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) {
 450         stub = StubRoutines::handler_for_unsafe_access();
 451       }
 452     }
 453 
 454     else if (thread->thread_state() == _thread_in_Java) {
 455       // Java thread running in Java code => find exception handler if any
 456       // a fault inside compiled code, the interpreter, or a stub
 457 
 458       // Support Safepoint Polling
 459       if ( sig == SIGSEGV && (address)info->si_addr == os::get_polling_page() ) {
 460         stub = SharedRuntime::get_poll_stub(pc);
 461       }
 462 
 463       // Not needed on x86 solaris because verify_oops doesn't generate
 464       // SEGV/BUS like sparc does.
 465       if ( (sig == SIGSEGV || sig == SIGBUS)
 466            && pc >= MacroAssembler::_verify_oop_implicit_branch[0]
 467            && pc <  MacroAssembler::_verify_oop_implicit_branch[1] ) {
 468         stub     =  MacroAssembler::_verify_oop_implicit_branch[2];
 469         warning("fixed up memory fault in +VerifyOops at address " INTPTR_FORMAT, info->si_addr);
 470       }
 471 
 472       // This is not factored because on x86 solaris the patching for
 473       // zombies does not generate a SEGV.
 474       else if (sig == SIGSEGV && nativeInstruction_at(pc)->is_zombie()) {
 475         // zombie method (ld [%g0],%o7 instruction)
 476         stub = SharedRuntime::get_handle_wrong_method_stub();
 477 
 478         // At the stub it needs to look like a call from the caller of this
 479         // method (not a call from the segv site).
 480         pc = (address)uc->uc_mcontext.gregs[REG_O7];
 481       }
 482       else if (sig == SIGBUS && info->si_code == BUS_OBJERR) {
 483         // BugId 4454115: A read from a MappedByteBuffer can fault
 484         // here if the underlying file has been truncated.
 485         // Do not crash the VM in such a case.
 486         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 487         nmethod* nm = cb->is_nmethod() ? (nmethod*)cb : NULL;
 488         if (nm != NULL && nm->has_unsafe_access()) {
 489           stub = StubRoutines::handler_for_unsafe_access();
 490         }
 491       }
 492 
 493       else if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
 494         // integer divide by zero
 495         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 496       }
 497       else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) {
 498         // floating-point divide by zero
 499         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 500       }
 501 #ifdef COMPILER2
 502       else if (sig == SIGILL && nativeInstruction_at(pc)->is_ic_miss_trap()) {
 503 #ifdef ASSERT
 504   #ifdef TIERED
 505         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 506         assert(cb->is_compiled_by_c2(), "Wrong compiler");
 507   #endif // TIERED
 508 #endif // ASSERT
 509         // Inline cache missed and user trap "Tne G0+ST_RESERVED_FOR_USER_0+2" taken.
 510         stub = SharedRuntime::get_ic_miss_stub();
 511         // At the stub it needs to look like a call from the caller of this
 512         // method (not a call from the segv site).
 513         pc = (address)uc->uc_mcontext.gregs[REG_O7];
 514       }
 515 #endif  // COMPILER2
 516 
 517       else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
 518         // Determination of interpreter/vtable stub/compiled code null exception
 519         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 520       }
 521     }
 522 
 523     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 524     // and the heap gets shrunk before the field access.
 525     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 526       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 527       if (addr != (address)-1) {
 528         stub = addr;
 529       }
 530     }
 531 
 532     // Check to see if we caught the safepoint code in the
 533     // process of write protecting the memory serialization page.
 534     // It write enables the page immediately after protecting it
 535     // so just return.
 536     if ((sig == SIGSEGV) &&
 537         os::is_memory_serialize_page(thread, (address)info->si_addr)) {
 538       // Block current thread until the memory serialize page permission restored.
 539       os::block_on_serialize_page_trap();
 540       return true;
 541     }
 542   }
 543 
 544   if (stub != NULL) {
 545     // save all thread context in case we need to restore it
 546 
 547     thread->set_saved_exception_pc(pc);
 548     thread->set_saved_exception_npc(npc);
 549 
 550     // simulate a branch to the stub (a "call" in the safepoint stub case)
 551     // factor me: setPC
 552     uc->uc_mcontext.gregs[REG_PC ] = (greg_t)stub;
 553     uc->uc_mcontext.gregs[REG_nPC] = (greg_t)(stub + 4);
 554 
 555 #ifndef PRODUCT
 556     if (TraceJumps) thread->record_jump(stub, NULL, __FILE__, __LINE__);
 557 #endif /* PRODUCT */
 558 
 559     return true;
 560   }
 561 
 562   // signal-chaining
 563   if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 564     return true;
 565   }
 566 
 567   if (!abort_if_unrecognized) {
 568     // caller wants another chance, so give it to him
 569     return false;
 570   }
 571 
 572   if (!os::Solaris::libjsig_is_loaded) {
 573     struct sigaction oldAct;
 574     sigaction(sig, (struct sigaction *)0, &oldAct);
 575     if (oldAct.sa_sigaction != signalHandler) {
 576       void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
 577                                           : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
 578       warning("Unexpected Signal %d occurred under user-defined signal handler " INTPTR_FORMAT, sig, (intptr_t)sighand);
 579     }
 580   }
 581 
 582   if (pc == NULL && uc != NULL) {
 583     pc = (address) uc->uc_mcontext.gregs[REG_PC];
 584   }
 585 
 586   // Sometimes the register windows are not properly flushed.
 587   if(uc->uc_mcontext.gwins != NULL) {
 588     ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 589   }
 590 
 591   // unmask current signal
 592   sigset_t newset;
 593   sigemptyset(&newset);
 594   sigaddset(&newset, sig);
 595   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 596 
 597   // Determine which sort of error to throw.  Out of swap may signal
 598   // on the thread stack, which could get a mapping error when touched.
 599   address addr = (address) info->si_addr;
 600   if (sig == SIGBUS && info->si_code == BUS_OBJERR && info->si_errno == ENOMEM) {
 601     vm_exit_out_of_memory(0, "Out of swap space to map in thread stack.");
 602   }
 603 
 604   VMError err(t, sig, pc, info, ucVoid);
 605   err.report_and_die();
 606 
 607   ShouldNotReachHere();
 608 }
 609 
 610 void os::print_context(outputStream *st, void *context) {
 611   if (context == NULL) return;
 612 
 613   ucontext_t *uc = (ucontext_t*)context;
 614   st->print_cr("Registers:");
 615 
 616   st->print_cr(" G1=" INTPTR_FORMAT " G2=" INTPTR_FORMAT
 617                " G3=" INTPTR_FORMAT " G4=" INTPTR_FORMAT,
 618             uc->uc_mcontext.gregs[REG_G1],
 619             uc->uc_mcontext.gregs[REG_G2],
 620             uc->uc_mcontext.gregs[REG_G3],
 621             uc->uc_mcontext.gregs[REG_G4]);
 622   st->print_cr(" G5=" INTPTR_FORMAT " G6=" INTPTR_FORMAT
 623                " G7=" INTPTR_FORMAT " Y=" INTPTR_FORMAT,
 624             uc->uc_mcontext.gregs[REG_G5],
 625             uc->uc_mcontext.gregs[REG_G6],
 626             uc->uc_mcontext.gregs[REG_G7],
 627             uc->uc_mcontext.gregs[REG_Y]);
 628   st->print_cr(" O0=" INTPTR_FORMAT " O1=" INTPTR_FORMAT
 629                " O2=" INTPTR_FORMAT " O3=" INTPTR_FORMAT,
 630                  uc->uc_mcontext.gregs[REG_O0],
 631                  uc->uc_mcontext.gregs[REG_O1],
 632                  uc->uc_mcontext.gregs[REG_O2],
 633                  uc->uc_mcontext.gregs[REG_O3]);
 634   st->print_cr(" O4=" INTPTR_FORMAT " O5=" INTPTR_FORMAT
 635                " O6=" INTPTR_FORMAT " O7=" INTPTR_FORMAT,
 636             uc->uc_mcontext.gregs[REG_O4],
 637             uc->uc_mcontext.gregs[REG_O5],
 638             uc->uc_mcontext.gregs[REG_O6],
 639             uc->uc_mcontext.gregs[REG_O7]);
 640 
 641 
 642   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 643   st->print_cr(" L0=" INTPTR_FORMAT " L1=" INTPTR_FORMAT
 644                " L2=" INTPTR_FORMAT " L3=" INTPTR_FORMAT,
 645                sp[L0->sp_offset_in_saved_window()],
 646                sp[L1->sp_offset_in_saved_window()],
 647                sp[L2->sp_offset_in_saved_window()],
 648                sp[L3->sp_offset_in_saved_window()]);
 649   st->print_cr(" L4=" INTPTR_FORMAT " L5=" INTPTR_FORMAT
 650                " L6=" INTPTR_FORMAT " L7=" INTPTR_FORMAT,
 651                sp[L4->sp_offset_in_saved_window()],
 652                sp[L5->sp_offset_in_saved_window()],
 653                sp[L6->sp_offset_in_saved_window()],
 654                sp[L7->sp_offset_in_saved_window()]);
 655   st->print_cr(" I0=" INTPTR_FORMAT " I1=" INTPTR_FORMAT
 656                " I2=" INTPTR_FORMAT " I3=" INTPTR_FORMAT,
 657                sp[I0->sp_offset_in_saved_window()],
 658                sp[I1->sp_offset_in_saved_window()],
 659                sp[I2->sp_offset_in_saved_window()],
 660                sp[I3->sp_offset_in_saved_window()]);
 661   st->print_cr(" I4=" INTPTR_FORMAT " I5=" INTPTR_FORMAT
 662                " I6=" INTPTR_FORMAT " I7=" INTPTR_FORMAT,
 663                sp[I4->sp_offset_in_saved_window()],
 664                sp[I5->sp_offset_in_saved_window()],
 665                sp[I6->sp_offset_in_saved_window()],
 666                sp[I7->sp_offset_in_saved_window()]);
 667 
 668   st->print_cr(" PC=" INTPTR_FORMAT " nPC=" INTPTR_FORMAT,
 669             uc->uc_mcontext.gregs[REG_PC],
 670             uc->uc_mcontext.gregs[REG_nPC]);
 671   st->cr();
 672   st->cr();
 673 
 674   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
 675   print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
 676   st->cr();
 677 
 678   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 679   // point to garbage if entry point in an nmethod is corrupted. Leave
 680   // this at the end, and hope for the best.
 681   ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 682   address pc = epc.pc();
 683   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
 684   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 685 }
 686 
 687 void os::print_register_info(outputStream *st, void *context) {
 688   if (context == NULL) return;
 689 
 690   ucontext_t *uc = (ucontext_t*)context;
 691   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 692 
 693   st->print_cr("Register to memory mapping:");
 694   st->cr();
 695 
 696   // this is only for the "general purpose" registers
 697   st->print("G1="); print_location(st, uc->uc_mcontext.gregs[REG_G1]);
 698   st->print("G2="); print_location(st, uc->uc_mcontext.gregs[REG_G2]);
 699   st->print("G3="); print_location(st, uc->uc_mcontext.gregs[REG_G3]);
 700   st->print("G4="); print_location(st, uc->uc_mcontext.gregs[REG_G4]);
 701   st->print("G5="); print_location(st, uc->uc_mcontext.gregs[REG_G5]);
 702   st->print("G6="); print_location(st, uc->uc_mcontext.gregs[REG_G6]);
 703   st->print("G7="); print_location(st, uc->uc_mcontext.gregs[REG_G7]);
 704   st->cr();
 705 
 706   st->print("O0="); print_location(st, uc->uc_mcontext.gregs[REG_O0]);
 707   st->print("O1="); print_location(st, uc->uc_mcontext.gregs[REG_O1]);
 708   st->print("O2="); print_location(st, uc->uc_mcontext.gregs[REG_O2]);
 709   st->print("O3="); print_location(st, uc->uc_mcontext.gregs[REG_O3]);
 710   st->print("O4="); print_location(st, uc->uc_mcontext.gregs[REG_O4]);
 711   st->print("O5="); print_location(st, uc->uc_mcontext.gregs[REG_O5]);
 712   st->print("O6="); print_location(st, uc->uc_mcontext.gregs[REG_O6]);
 713   st->print("O7="); print_location(st, uc->uc_mcontext.gregs[REG_O7]);
 714   st->cr();
 715 
 716   st->print("L0="); print_location(st, sp[L0->sp_offset_in_saved_window()]);
 717   st->print("L1="); print_location(st, sp[L1->sp_offset_in_saved_window()]);
 718   st->print("L2="); print_location(st, sp[L2->sp_offset_in_saved_window()]);
 719   st->print("L3="); print_location(st, sp[L3->sp_offset_in_saved_window()]);
 720   st->print("L4="); print_location(st, sp[L4->sp_offset_in_saved_window()]);
 721   st->print("L5="); print_location(st, sp[L5->sp_offset_in_saved_window()]);
 722   st->print("L6="); print_location(st, sp[L6->sp_offset_in_saved_window()]);
 723   st->print("L7="); print_location(st, sp[L7->sp_offset_in_saved_window()]);
 724   st->cr();
 725 
 726   st->print("I0="); print_location(st, sp[I0->sp_offset_in_saved_window()]);
 727   st->print("I1="); print_location(st, sp[I1->sp_offset_in_saved_window()]);
 728   st->print("I2="); print_location(st, sp[I2->sp_offset_in_saved_window()]);
 729   st->print("I3="); print_location(st, sp[I3->sp_offset_in_saved_window()]);
 730   st->print("I4="); print_location(st, sp[I4->sp_offset_in_saved_window()]);
 731   st->print("I5="); print_location(st, sp[I5->sp_offset_in_saved_window()]);
 732   st->print("I6="); print_location(st, sp[I6->sp_offset_in_saved_window()]);
 733   st->print("I7="); print_location(st, sp[I7->sp_offset_in_saved_window()]);
 734   st->cr();
 735 }
 736 
 737 void os::Solaris::init_thread_fpu_state(void) {
 738     // Nothing needed on Sparc.
 739 }
 740 
 741 #if !defined(COMPILER2) && !defined(_LP64)
 742 
 743 // These routines are the initial value of atomic_xchg_entry(),
 744 // atomic_cmpxchg_entry(), atomic_add_entry() and fence_entry()
 745 // until initialization is complete.
 746 // TODO - remove when the VM drops support for V8.
 747 
 748 typedef jint  xchg_func_t        (jint,  volatile jint*);
 749 typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
 750 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
 751 typedef jint  add_func_t         (jint,  volatile jint*);
 752 
 753 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
 754   // try to use the stub:
 755   xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
 756 
 757   if (func != NULL) {
 758     os::atomic_xchg_func = func;
 759     return (*func)(exchange_value, dest);
 760   }
 761   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 762 
 763   jint old_value = *dest;
 764   *dest = exchange_value;
 765   return old_value;
 766 }
 767 
 768 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
 769   // try to use the stub:
 770   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
 771 
 772   if (func != NULL) {
 773     os::atomic_cmpxchg_func = func;
 774     return (*func)(exchange_value, dest, compare_value);
 775   }
 776   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 777 
 778   jint old_value = *dest;
 779   if (old_value == compare_value)
 780     *dest = exchange_value;
 781   return old_value;
 782 }
 783 
 784 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
 785   // try to use the stub:
 786   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
 787 
 788   if (func != NULL) {
 789     os::atomic_cmpxchg_long_func = func;
 790     return (*func)(exchange_value, dest, compare_value);
 791   }
 792   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 793 
 794   jlong old_value = *dest;
 795   if (old_value == compare_value)
 796     *dest = exchange_value;
 797   return old_value;
 798 }
 799 
 800 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
 801   // try to use the stub:
 802   add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
 803 
 804   if (func != NULL) {
 805     os::atomic_add_func = func;
 806     return (*func)(add_value, dest);
 807   }
 808   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 809 
 810   return (*dest) += add_value;
 811 }
 812 
 813 xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
 814 cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
 815 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
 816 add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
 817 
 818 #endif // !_LP64 && !COMPILER2
 819 
 820 #if defined(__sparc) && defined(COMPILER2) && defined(_GNU_SOURCE)
 821  // See file build/solaris/makefiles/$compiler.make
 822  // For compiler1 the architecture is v8 and frps isn't present in v8
 823  extern "C"  void _mark_fpu_nosave() {
 824    __asm__ __volatile__ ("wr %%g0, 0, %%fprs \n\t" : : :);
 825   }
 826 #endif //defined(__sparc) && defined(COMPILER2)
 827 
 828 #ifndef PRODUCT
 829 void os::verify_stack_alignment() {
 830 }
 831 #endif