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