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