1 /*
   2  * Copyright (c) 1999, 2016, 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 "asm/macroAssembler.hpp"
  27 #include "macroAssembler_sparc.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "jvm_solaris.h"
  36 #include "memory/allocation.inline.hpp"
  37 #include "nativeInst_sparc.hpp"
  38 #include "os_share_solaris.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm.h"
  41 #include "prims/jvm_misc.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/extendedPC.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/interfaceSupport.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/mutexLocker.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "runtime/timer.hpp"
  54 #include "utilities/events.hpp"
  55 #include "utilities/vmError.hpp"
  56 
  57 # include <signal.h>        // needed first to avoid name collision for "std" with SC 5.0
  58 
  59 // put OS-includes here
  60 # include <sys/types.h>
  61 # include <sys/mman.h>
  62 # include <pthread.h>
  63 # include <errno.h>
  64 # include <dlfcn.h>
  65 # include <stdio.h>
  66 # include <unistd.h>
  67 # include <sys/resource.h>
  68 # include <thread.h>
  69 # include <sys/stat.h>
  70 # include <sys/time.h>
  71 # include <sys/filio.h>
  72 # include <sys/utsname.h>
  73 # include <sys/systeminfo.h>
  74 # include <sys/socket.h>
  75 # include <sys/lwp.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 usable stack sizes required to get to user code. Space for
  85 // HotSpot guard pages is added later.
  86 size_t os::Posix::_compiler_thread_min_stack_allowed = 104 * K;
  87 size_t os::Posix::_java_thread_min_stack_allowed = 86 * K;
  88 #ifdef _LP64
  89 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 128 * K;
  90 #else
  91 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 96 * K;
  92 #endif
  93 
  94 int os::Solaris::max_register_window_saves_before_flushing() {
  95   // We should detect this at run time. For now, filling
  96   // in with a constant.
  97   return 8;
  98 }
  99 
 100 static void handle_unflushed_register_windows(gwindows_t *win) {
 101   int restore_count = win->wbcnt;
 102   int i;
 103 
 104   for(i=0; i<restore_count; i++) {
 105     address sp = ((address)win->spbuf[i]) + STACK_BIAS;
 106     address reg_win = (address)&win->wbuf[i];
 107     memcpy(sp,reg_win,sizeof(struct rwindow));
 108   }
 109 }
 110 
 111 char* os::non_memory_address_word() {
 112   // Must never look like an address returned by reserve_memory,
 113   // even in its subfields (as defined by the CPU immediate fields,
 114   // if the CPU splits constants across multiple instructions).
 115   // On SPARC, 0 != %hi(any real address), because there is no
 116   // allocation in the first 1Kb of the virtual address space.
 117   return (char*) 0;
 118 }
 119 
 120 // Validate a ucontext retrieved from walking a uc_link of a ucontext.
 121 // There are issues with libthread giving out uc_links for different threads
 122 // on the same uc_link chain and bad or circular links.
 123 //
 124 bool os::Solaris::valid_ucontext(Thread* thread, const ucontext_t* valid, const ucontext_t* suspect) {
 125   if (valid >= suspect ||
 126       valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags ||
 127       valid->uc_stack.ss_sp    != suspect->uc_stack.ss_sp    ||
 128       valid->uc_stack.ss_size  != suspect->uc_stack.ss_size) {
 129     DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");)
 130     return false;
 131   }
 132 
 133   if (thread->is_Java_thread()) {
 134     if (!valid_stack_address(thread, (address)suspect)) {
 135       DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");)
 136       return false;
 137     }
 138     address _sp   = (address)((intptr_t)suspect->uc_mcontext.gregs[REG_SP] + STACK_BIAS);
 139     if (!valid_stack_address(thread, _sp) ||
 140         !frame::is_valid_stack_pointer(((JavaThread*)thread)->base_of_stack_pointer(), (intptr_t*)_sp)) {
 141       DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");)
 142       return false;
 143     }
 144   }
 145   return true;
 146 }
 147 
 148 // We will only follow one level of uc_link since there are libthread
 149 // issues with ucontext linking and it is better to be safe and just
 150 // let caller retry later.
 151 const ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread,
 152   const ucontext_t *uc) {
 153 
 154   const ucontext_t *retuc = NULL;
 155 
 156   // Sometimes the topmost register windows are not properly flushed.
 157   // i.e., if the kernel would have needed to take a page fault
 158   if (uc != NULL && uc->uc_mcontext.gwins != NULL) {
 159     ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 160   }
 161 
 162   if (uc != NULL) {
 163     if (uc->uc_link == NULL) {
 164       // cannot validate without uc_link so accept current ucontext
 165       retuc = uc;
 166     } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 167       // first ucontext is valid so try the next one
 168       uc = uc->uc_link;
 169       if (uc->uc_link == NULL) {
 170         // cannot validate without uc_link so accept current ucontext
 171         retuc = uc;
 172       } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 173         // the ucontext one level down is also valid so return it
 174         retuc = uc;
 175       }
 176     }
 177   }
 178   return retuc;
 179 }
 180 
 181 // Assumes ucontext is valid
 182 ExtendedPC os::Solaris::ucontext_get_ExtendedPC(const ucontext_t *uc) {
 183   address pc = (address)uc->uc_mcontext.gregs[REG_PC];
 184   // set npc to zero to avoid using it for safepoint, good for profiling only
 185   return ExtendedPC(pc);
 186 }
 187 
 188 void os::Solaris::ucontext_set_pc(ucontext_t* uc, address pc) {
 189   uc->uc_mcontext.gregs [REG_PC]  = (greg_t) pc;
 190   uc->uc_mcontext.gregs [REG_nPC] = (greg_t) (pc + 4);
 191 }
 192 
 193 // Assumes ucontext is valid
 194 intptr_t* os::Solaris::ucontext_get_sp(const 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(const ucontext_t *uc) {
 200   ShouldNotReachHere();
 201   return NULL;
 202 }
 203 
 204 address os::Solaris::ucontext_get_pc(const ucontext_t *uc) {
 205   return (address) uc->uc_mcontext.gregs[REG_PC];
 206 }
 207 
 208 
 209 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
 210 // is currently interrupted by SIGPROF.
 211 //
 212 // ret_fp parameter is only used by Solaris X86.
 213 //
 214 // The difference between this and os::fetch_frame_from_context() is that
 215 // here we try to skip nested signal frames.
 216 // This method is also used for stack overflow signal handling.
 217 ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread,
 218   const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
 219 
 220   assert(thread != NULL, "just checking");
 221   assert(ret_sp != NULL, "just checking");
 222   assert(ret_fp == NULL, "just checking");
 223 
 224   const ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc);
 225 
 226   return os::fetch_frame_from_context(luc, ret_sp, ret_fp);
 227 }
 228 
 229 
 230 // ret_fp parameter is only used by Solaris X86.
 231 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 232                     intptr_t** ret_sp, intptr_t** ret_fp) {
 233 
 234   ExtendedPC  epc;
 235   const ucontext_t *uc = (const ucontext_t*)ucVoid;
 236 
 237   if (uc != NULL) {
 238     epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 239     if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc);
 240   } else {
 241     // construct empty ExtendedPC for return value checking
 242     epc = ExtendedPC(NULL);
 243     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 244   }
 245 
 246   return epc;
 247 }
 248 
 249 frame os::fetch_frame_from_context(const void* ucVoid) {
 250   intptr_t* sp;
 251   intptr_t* fp;
 252   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 253   return frame(sp, frame::unpatchable, epc.pc());
 254 }
 255 
 256 frame os::fetch_frame_from_ucontext(Thread* thread, void* ucVoid) {
 257   intptr_t* sp;
 258   ExtendedPC epc = os::Solaris::fetch_frame_from_ucontext(thread, (ucontext_t*)ucVoid, &sp, NULL);
 259   return frame(sp, frame::unpatchable, epc.pc());
 260 }
 261 
 262 bool os::Solaris::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 263   address pc = (address) os::Solaris::ucontext_get_pc(uc);
 264   if (Interpreter::contains(pc)) {
 265     *fr = os::fetch_frame_from_ucontext(thread, uc);
 266     if (!fr->is_first_java_frame()) {
 267       assert(fr->safe_for_sender(thread), "Safety check");
 268       *fr = fr->java_sender();
 269     }
 270   } else {
 271     // more complex code with compiled code
 272     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 273     CodeBlob* cb = CodeCache::find_blob(pc);
 274     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 275       // Not sure where the pc points to, fallback to default
 276       // stack overflow handling
 277       return false;
 278     } else {
 279       // Returned frame will be the caller of the method that faults on the stack bang.
 280       // Register window not yet rotated (happens at SAVE after stack bang), so there is no new
 281       // frame to go with the faulting PC. Using caller SP that is still in SP, and caller PC
 282       // that was written to O7 at call.
 283       intptr_t* sp = os::Solaris::ucontext_get_sp(uc);
 284       address pc = (address)uc->uc_mcontext.gregs[REG_O7];
 285       *fr = frame(sp, frame::unpatchable, pc);
 286 
 287       if (!fr->is_java_frame()) {
 288         assert(fr->safe_for_sender(thread), "Safety check");
 289         *fr = fr->java_sender();
 290       }
 291     }
 292   }
 293   assert(fr->is_java_frame(), "Safety check");
 294   return true;
 295 }
 296 
 297 frame os::get_sender_for_C_frame(frame* fr) {
 298   return frame(fr->sender_sp(), frame::unpatchable, fr->sender_pc());
 299 }
 300 
 301 // Returns an estimate of the current stack pointer. Result must be guaranteed to
 302 // point into the calling threads stack, and be no lower than the current stack
 303 // pointer.
 304 address os::current_stack_pointer() {
 305   volatile int dummy;
 306   address sp = (address)&dummy + 8;     // %%%% need to confirm if this is right
 307   return sp;
 308 }
 309 
 310 frame os::current_frame() {
 311   intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
 312   frame myframe(sp, frame::unpatchable,
 313                 CAST_FROM_FN_PTR(address, os::current_frame));
 314   if (os::is_first_C_frame(&myframe)) {
 315     // stack is not walkable
 316     return frame(NULL, NULL, false);
 317   } else {
 318     return os::get_sender_for_C_frame(&myframe);
 319   }
 320 }
 321 
 322 bool os::is_allocatable(size_t bytes) {
 323 #ifdef _LP64
 324    return true;
 325 #else
 326    return (bytes <= (size_t)3835*M);
 327 #endif
 328 }
 329 
 330 extern "C" JNIEXPORT int
 331 JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid,
 332                           int abort_if_unrecognized) {
 333   ucontext_t* uc = (ucontext_t*) ucVoid;
 334 
 335   Thread* t = Thread::current_or_null_safe();
 336 
 337   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 338   // (no destructors can be run)
 339   os::WatcherThreadCrashProtection::check_crash_protection(sig, t);
 340 
 341   SignalHandlerMark shm(t);
 342 
 343   if(sig == SIGPIPE || sig == SIGXFSZ) {
 344     if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 345       return true;
 346     } else {
 347       // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219
 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   if (sig == os::Solaris::SIGasync()) {
 366     if (thread || vmthread) {
 367       OSThread::SR_handler(t, uc);
 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     if (StubRoutines::is_safefetch_fault(pc)) {
 399       os::Solaris::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 400       return 1;
 401     }
 402 
 403     // Handle ALL stack overflow variations here
 404     if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
 405       address addr = (address) info->si_addr;
 406       if (thread->in_stack_yellow_reserved_zone(addr)) {
 407         // Sometimes the register windows are not properly flushed.
 408         if(uc->uc_mcontext.gwins != NULL) {
 409           ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 410         }
 411         if (thread->thread_state() == _thread_in_Java) {
 412           if (thread->in_stack_reserved_zone(addr)) {
 413             frame fr;
 414             if (os::Solaris::get_frame_at_stack_banging_point(thread, uc, &fr)) {
 415               assert(fr.is_java_frame(), "Must be a Java frame");
 416               frame activation = SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
 417               if (activation.sp() != NULL) {
 418                 thread->disable_stack_reserved_zone();
 419                 RegisterMap map(thread);
 420                 int frame_size = activation.frame_size(&map);
 421                 thread->set_reserved_stack_activation((address)(((address)activation.sp()) - STACK_BIAS));
 422                 return true;
 423               }
 424             }
 425           }
 426           // Throw a stack overflow exception.  Guard pages will be reenabled
 427           // while unwinding the stack.
 428           thread->disable_stack_yellow_reserved_zone();
 429           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 430         } else {
 431           // Thread was in the vm or native code.  Return and try to finish.
 432           thread->disable_stack_yellow_reserved_zone();
 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 && thread->doing_unsafe_access()) {
 450         stub = SharedRuntime::handle_unsafe_access(thread, npc);
 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         CompiledMethod* nm = cb->as_compiled_method_or_null();
 488         if (nm != NULL && nm->has_unsafe_access()) {
 489           stub = SharedRuntime::handle_unsafe_access(thread, npc);
 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     os::Solaris::ucontext_set_pc(uc, stub);
 553 
 554     return true;
 555   }
 556 
 557   // signal-chaining
 558   if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 559     return true;
 560   }
 561 
 562   if (!abort_if_unrecognized) {
 563     // caller wants another chance, so give it to him
 564     return false;
 565   }
 566 
 567   if (!os::Solaris::libjsig_is_loaded) {
 568     struct sigaction oldAct;
 569     sigaction(sig, (struct sigaction *)0, &oldAct);
 570     if (oldAct.sa_sigaction != signalHandler) {
 571       void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
 572                                           : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
 573       warning("Unexpected Signal %d occurred under user-defined signal handler " INTPTR_FORMAT, sig, (intptr_t)sighand);
 574     }
 575   }
 576 
 577   if (pc == NULL && uc != NULL) {
 578     pc = (address) uc->uc_mcontext.gregs[REG_PC];
 579   }
 580 
 581   // Sometimes the register windows are not properly flushed.
 582   if(uc->uc_mcontext.gwins != NULL) {
 583     ::handle_unflushed_register_windows(uc->uc_mcontext.gwins);
 584   }
 585 
 586   // unmask current signal
 587   sigset_t newset;
 588   sigemptyset(&newset);
 589   sigaddset(&newset, sig);
 590   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 591 
 592   // Determine which sort of error to throw.  Out of swap may signal
 593   // on the thread stack, which could get a mapping error when touched.
 594   address addr = (address) info->si_addr;
 595   if (sig == SIGBUS && info->si_code == BUS_OBJERR && info->si_errno == ENOMEM) {
 596     vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "Out of swap space to map in thread stack.");
 597   }
 598 
 599   VMError::report_and_die(t, sig, pc, info, ucVoid);
 600 
 601   ShouldNotReachHere();
 602   return false;
 603 }
 604 
 605 void os::print_context(outputStream *st, const void *context) {
 606   if (context == NULL) return;
 607 
 608   const ucontext_t *uc = (const ucontext_t*)context;
 609   st->print_cr("Registers:");
 610 
 611   st->print_cr(" G1=" INTPTR_FORMAT " G2=" INTPTR_FORMAT
 612                " G3=" INTPTR_FORMAT " G4=" INTPTR_FORMAT,
 613             uc->uc_mcontext.gregs[REG_G1],
 614             uc->uc_mcontext.gregs[REG_G2],
 615             uc->uc_mcontext.gregs[REG_G3],
 616             uc->uc_mcontext.gregs[REG_G4]);
 617   st->print_cr(" G5=" INTPTR_FORMAT " G6=" INTPTR_FORMAT
 618                " G7=" INTPTR_FORMAT " Y=" INTPTR_FORMAT,
 619             uc->uc_mcontext.gregs[REG_G5],
 620             uc->uc_mcontext.gregs[REG_G6],
 621             uc->uc_mcontext.gregs[REG_G7],
 622             uc->uc_mcontext.gregs[REG_Y]);
 623   st->print_cr(" O0=" INTPTR_FORMAT " O1=" INTPTR_FORMAT
 624                " O2=" INTPTR_FORMAT " O3=" INTPTR_FORMAT,
 625                  uc->uc_mcontext.gregs[REG_O0],
 626                  uc->uc_mcontext.gregs[REG_O1],
 627                  uc->uc_mcontext.gregs[REG_O2],
 628                  uc->uc_mcontext.gregs[REG_O3]);
 629   st->print_cr(" O4=" INTPTR_FORMAT " O5=" INTPTR_FORMAT
 630                " O6=" INTPTR_FORMAT " O7=" INTPTR_FORMAT,
 631             uc->uc_mcontext.gregs[REG_O4],
 632             uc->uc_mcontext.gregs[REG_O5],
 633             uc->uc_mcontext.gregs[REG_O6],
 634             uc->uc_mcontext.gregs[REG_O7]);
 635 
 636 
 637   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 638   st->print_cr(" L0=" INTPTR_FORMAT " L1=" INTPTR_FORMAT
 639                " L2=" INTPTR_FORMAT " L3=" INTPTR_FORMAT,
 640                sp[L0->sp_offset_in_saved_window()],
 641                sp[L1->sp_offset_in_saved_window()],
 642                sp[L2->sp_offset_in_saved_window()],
 643                sp[L3->sp_offset_in_saved_window()]);
 644   st->print_cr(" L4=" INTPTR_FORMAT " L5=" INTPTR_FORMAT
 645                " L6=" INTPTR_FORMAT " L7=" INTPTR_FORMAT,
 646                sp[L4->sp_offset_in_saved_window()],
 647                sp[L5->sp_offset_in_saved_window()],
 648                sp[L6->sp_offset_in_saved_window()],
 649                sp[L7->sp_offset_in_saved_window()]);
 650   st->print_cr(" I0=" INTPTR_FORMAT " I1=" INTPTR_FORMAT
 651                " I2=" INTPTR_FORMAT " I3=" INTPTR_FORMAT,
 652                sp[I0->sp_offset_in_saved_window()],
 653                sp[I1->sp_offset_in_saved_window()],
 654                sp[I2->sp_offset_in_saved_window()],
 655                sp[I3->sp_offset_in_saved_window()]);
 656   st->print_cr(" I4=" INTPTR_FORMAT " I5=" INTPTR_FORMAT
 657                " I6=" INTPTR_FORMAT " I7=" INTPTR_FORMAT,
 658                sp[I4->sp_offset_in_saved_window()],
 659                sp[I5->sp_offset_in_saved_window()],
 660                sp[I6->sp_offset_in_saved_window()],
 661                sp[I7->sp_offset_in_saved_window()]);
 662 
 663   st->print_cr(" PC=" INTPTR_FORMAT " nPC=" INTPTR_FORMAT,
 664             uc->uc_mcontext.gregs[REG_PC],
 665             uc->uc_mcontext.gregs[REG_nPC]);
 666   st->cr();
 667   st->cr();
 668 
 669   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
 670   print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
 671   st->cr();
 672 
 673   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 674   // point to garbage if entry point in an nmethod is corrupted. Leave
 675   // this at the end, and hope for the best.
 676   ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 677   address pc = epc.pc();
 678   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
 679   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 680 }
 681 
 682 void os::print_register_info(outputStream *st, const void *context) {
 683   if (context == NULL) return;
 684 
 685   const ucontext_t *uc = (const ucontext_t*)context;
 686   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 687 
 688   st->print_cr("Register to memory mapping:");
 689   st->cr();
 690 
 691   // this is only for the "general purpose" registers
 692   st->print("G1="); print_location(st, uc->uc_mcontext.gregs[REG_G1]);
 693   st->print("G2="); print_location(st, uc->uc_mcontext.gregs[REG_G2]);
 694   st->print("G3="); print_location(st, uc->uc_mcontext.gregs[REG_G3]);
 695   st->print("G4="); print_location(st, uc->uc_mcontext.gregs[REG_G4]);
 696   st->print("G5="); print_location(st, uc->uc_mcontext.gregs[REG_G5]);
 697   st->print("G6="); print_location(st, uc->uc_mcontext.gregs[REG_G6]);
 698   st->print("G7="); print_location(st, uc->uc_mcontext.gregs[REG_G7]);
 699   st->cr();
 700 
 701   st->print("O0="); print_location(st, uc->uc_mcontext.gregs[REG_O0]);
 702   st->print("O1="); print_location(st, uc->uc_mcontext.gregs[REG_O1]);
 703   st->print("O2="); print_location(st, uc->uc_mcontext.gregs[REG_O2]);
 704   st->print("O3="); print_location(st, uc->uc_mcontext.gregs[REG_O3]);
 705   st->print("O4="); print_location(st, uc->uc_mcontext.gregs[REG_O4]);
 706   st->print("O5="); print_location(st, uc->uc_mcontext.gregs[REG_O5]);
 707   st->print("O6="); print_location(st, uc->uc_mcontext.gregs[REG_O6]);
 708   st->print("O7="); print_location(st, uc->uc_mcontext.gregs[REG_O7]);
 709   st->cr();
 710 
 711   st->print("L0="); print_location(st, sp[L0->sp_offset_in_saved_window()]);
 712   st->print("L1="); print_location(st, sp[L1->sp_offset_in_saved_window()]);
 713   st->print("L2="); print_location(st, sp[L2->sp_offset_in_saved_window()]);
 714   st->print("L3="); print_location(st, sp[L3->sp_offset_in_saved_window()]);
 715   st->print("L4="); print_location(st, sp[L4->sp_offset_in_saved_window()]);
 716   st->print("L5="); print_location(st, sp[L5->sp_offset_in_saved_window()]);
 717   st->print("L6="); print_location(st, sp[L6->sp_offset_in_saved_window()]);
 718   st->print("L7="); print_location(st, sp[L7->sp_offset_in_saved_window()]);
 719   st->cr();
 720 
 721   st->print("I0="); print_location(st, sp[I0->sp_offset_in_saved_window()]);
 722   st->print("I1="); print_location(st, sp[I1->sp_offset_in_saved_window()]);
 723   st->print("I2="); print_location(st, sp[I2->sp_offset_in_saved_window()]);
 724   st->print("I3="); print_location(st, sp[I3->sp_offset_in_saved_window()]);
 725   st->print("I4="); print_location(st, sp[I4->sp_offset_in_saved_window()]);
 726   st->print("I5="); print_location(st, sp[I5->sp_offset_in_saved_window()]);
 727   st->print("I6="); print_location(st, sp[I6->sp_offset_in_saved_window()]);
 728   st->print("I7="); print_location(st, sp[I7->sp_offset_in_saved_window()]);
 729   st->cr();
 730 }
 731 
 732 void os::Solaris::init_thread_fpu_state(void) {
 733     // Nothing needed on Sparc.
 734 }
 735 
 736 #if !defined(COMPILER2) && !defined(_LP64)
 737 
 738 // These routines are the initial value of atomic_xchg_entry(),
 739 // atomic_cmpxchg_entry(), atomic_add_entry() and fence_entry()
 740 // until initialization is complete.
 741 // TODO - remove when the VM drops support for V8.
 742 
 743 typedef jint  xchg_func_t        (jint,  volatile jint*);
 744 typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
 745 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
 746 typedef jint  add_func_t         (jint,  volatile jint*);
 747 
 748 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
 749   // try to use the stub:
 750   xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
 751 
 752   if (func != NULL) {
 753     os::atomic_xchg_func = func;
 754     return (*func)(exchange_value, dest);
 755   }
 756   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 757 
 758   jint old_value = *dest;
 759   *dest = exchange_value;
 760   return old_value;
 761 }
 762 
 763 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
 764   // try to use the stub:
 765   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
 766 
 767   if (func != NULL) {
 768     os::atomic_cmpxchg_func = func;
 769     return (*func)(exchange_value, dest, compare_value);
 770   }
 771   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 772 
 773   jint old_value = *dest;
 774   if (old_value == compare_value)
 775     *dest = exchange_value;
 776   return old_value;
 777 }
 778 
 779 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
 780   // try to use the stub:
 781   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
 782 
 783   if (func != NULL) {
 784     os::atomic_cmpxchg_long_func = func;
 785     return (*func)(exchange_value, dest, compare_value);
 786   }
 787   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 788 
 789   jlong old_value = *dest;
 790   if (old_value == compare_value)
 791     *dest = exchange_value;
 792   return old_value;
 793 }
 794 
 795 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
 796   // try to use the stub:
 797   add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
 798 
 799   if (func != NULL) {
 800     os::atomic_add_func = func;
 801     return (*func)(add_value, dest);
 802   }
 803   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 804 
 805   return (*dest) += add_value;
 806 }
 807 
 808 xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
 809 cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
 810 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
 811 add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
 812 
 813 #endif // !_LP64 && !COMPILER2
 814 
 815 #if defined(__sparc) && defined(COMPILER2) && defined(_GNU_SOURCE)
 816  // See file build/solaris/makefiles/$compiler.make
 817  // For compiler1 the architecture is v8 and frps isn't present in v8
 818  extern "C"  void _mark_fpu_nosave() {
 819    __asm__ __volatile__ ("wr %%g0, 0, %%fprs \n\t" : : :);
 820   }
 821 #endif //defined(__sparc) && defined(COMPILER2)
 822 
 823 #ifndef PRODUCT
 824 void os::verify_stack_alignment() {
 825 }
 826 #endif
 827 
 828 int os::extra_bang_size_in_bytes() {
 829   // SPARC does not require an additional stack bang.
 830   return 0;
 831 }