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