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 "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 "os_share_solaris.hpp"
  37 #include "prims/jniFastGetField.hpp"
  38 #include "prims/jvm.h"
  39 #include "prims/jvm_misc.hpp"
  40 #include "runtime/arguments.hpp"
  41 #include "runtime/atomic.hpp"
  42 #include "runtime/extendedPC.hpp"
  43 #include "runtime/frame.inline.hpp"
  44 #include "runtime/interfaceSupport.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/javaCalls.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/sharedRuntime.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/thread.inline.hpp"
  52 #include "runtime/timer.hpp"
  53 #include "utilities/events.hpp"
  54 #include "utilities/vmError.hpp"
  55 
  56 // put OS-includes here
  57 # include <sys/types.h>
  58 # include <sys/mman.h>
  59 # include <pthread.h>
  60 # include <signal.h>
  61 # include <setjmp.h>
  62 # include <errno.h>
  63 # include <dlfcn.h>
  64 # include <stdio.h>
  65 # include <unistd.h>
  66 # include <sys/resource.h>
  67 # include <thread.h>
  68 # include <sys/stat.h>
  69 # include <sys/time.h>
  70 # include <sys/filio.h>
  71 # include <sys/utsname.h>
  72 # include <sys/systeminfo.h>
  73 # include <sys/socket.h>
  74 # include <sys/trap.h>
  75 # include <sys/lwp.h>
  76 # include <poll.h>
  77 # include <sys/lwp.h>
  78 # include <procfs.h>     //  see comment in <sys/procfs.h>
  79 
  80 #ifndef AMD64
  81 // QQQ seems useless at this point
  82 # define _STRUCTURED_PROC 1  //  this gets us the new structured proc interfaces of 5.6 & later
  83 #endif // AMD64
  84 # include <sys/procfs.h>     //  see comment in <sys/procfs.h>
  85 
  86 
  87 #define MAX_PATH (2 * K)
  88 
  89 // Minimum stack size for the VM.  It's easier to document a constant value
  90 // but it's different for x86 and sparc because the page sizes are different.
  91 #ifdef AMD64
  92 size_t os::Solaris::min_stack_allowed = 224*K;
  93 #define REG_SP REG_RSP
  94 #define REG_PC REG_RIP
  95 #define REG_FP REG_RBP
  96 #else
  97 size_t os::Solaris::min_stack_allowed = 64*K;
  98 #define REG_SP UESP
  99 #define REG_PC EIP
 100 #define REG_FP EBP
 101 // 4900493 counter to prevent runaway LDTR refresh attempt
 102 
 103 static volatile int ldtr_refresh = 0;
 104 // the libthread instruction that faults because of the stale LDTR
 105 
 106 static const unsigned char movlfs[] = { 0x8e, 0xe0    // movl %eax,%fs
 107                        };
 108 #endif // AMD64
 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   return (char*) -1;
 115 }
 116 
 117 //
 118 // Validate a ucontext retrieved from walking a uc_link of a ucontext.
 119 // There are issues with libthread giving out uc_links for different threads
 120 // on the same uc_link chain and bad or circular links.
 121 //
 122 bool os::Solaris::valid_ucontext(Thread* thread, const ucontext_t* valid, const ucontext_t* suspect) {
 123   if (valid >= suspect ||
 124       valid->uc_stack.ss_flags != suspect->uc_stack.ss_flags ||
 125       valid->uc_stack.ss_sp    != suspect->uc_stack.ss_sp    ||
 126       valid->uc_stack.ss_size  != suspect->uc_stack.ss_size) {
 127     DEBUG_ONLY(tty->print_cr("valid_ucontext: failed test 1");)
 128     return false;
 129   }
 130 
 131   if (thread->is_Java_thread()) {
 132     if (!valid_stack_address(thread, (address)suspect)) {
 133       DEBUG_ONLY(tty->print_cr("valid_ucontext: uc_link not in thread stack");)
 134       return false;
 135     }
 136     if (!valid_stack_address(thread,  (address) suspect->uc_mcontext.gregs[REG_SP])) {
 137       DEBUG_ONLY(tty->print_cr("valid_ucontext: stackpointer not in thread stack");)
 138       return false;
 139     }
 140   }
 141   return true;
 142 }
 143 
 144 // We will only follow one level of uc_link since there are libthread
 145 // issues with ucontext linking and it is better to be safe and just
 146 // let caller retry later.
 147 const ucontext_t* os::Solaris::get_valid_uc_in_signal_handler(Thread *thread,
 148   const ucontext_t *uc) {
 149 
 150   const ucontext_t *retuc = NULL;
 151 
 152   if (uc != NULL) {
 153     if (uc->uc_link == NULL) {
 154       // cannot validate without uc_link so accept current ucontext
 155       retuc = uc;
 156     } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 157       // first ucontext is valid so try the next one
 158       uc = uc->uc_link;
 159       if (uc->uc_link == NULL) {
 160         // cannot validate without uc_link so accept current ucontext
 161         retuc = uc;
 162       } else if (os::Solaris::valid_ucontext(thread, uc, uc->uc_link)) {
 163         // the ucontext one level down is also valid so return it
 164         retuc = uc;
 165       }
 166     }
 167   }
 168   return retuc;
 169 }
 170 
 171 // Assumes ucontext is valid
 172 ExtendedPC os::Solaris::ucontext_get_ExtendedPC(const ucontext_t *uc) {
 173   return ExtendedPC((address)uc->uc_mcontext.gregs[REG_PC]);
 174 }
 175 
 176 void os::Solaris::ucontext_set_pc(ucontext_t* uc, address pc) {
 177   uc->uc_mcontext.gregs [REG_PC]  = (greg_t) pc;
 178 }
 179 
 180 // Assumes ucontext is valid
 181 intptr_t* os::Solaris::ucontext_get_sp(const ucontext_t *uc) {
 182   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
 183 }
 184 
 185 // Assumes ucontext is valid
 186 intptr_t* os::Solaris::ucontext_get_fp(const ucontext_t *uc) {
 187   return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
 188 }
 189 
 190 address os::Solaris::ucontext_get_pc(const ucontext_t *uc) {
 191   return (address) uc->uc_mcontext.gregs[REG_PC];
 192 }
 193 
 194 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
 195 // is currently interrupted by SIGPROF.
 196 //
 197 // The difference between this and os::fetch_frame_from_context() is that
 198 // here we try to skip nested signal frames.
 199 // This method is also used for stack overflow signal handling.
 200 ExtendedPC os::Solaris::fetch_frame_from_ucontext(Thread* thread,
 201   const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
 202 
 203   assert(thread != NULL, "just checking");
 204   assert(ret_sp != NULL, "just checking");
 205   assert(ret_fp != NULL, "just checking");
 206 
 207   const ucontext_t *luc = os::Solaris::get_valid_uc_in_signal_handler(thread, uc);
 208   return os::fetch_frame_from_context(luc, ret_sp, ret_fp);
 209 }
 210 
 211 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 212                     intptr_t** ret_sp, intptr_t** ret_fp) {
 213 
 214   ExtendedPC  epc;
 215   const ucontext_t *uc = (const ucontext_t*)ucVoid;
 216 
 217   if (uc != NULL) {
 218     epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 219     if (ret_sp) *ret_sp = os::Solaris::ucontext_get_sp(uc);
 220     if (ret_fp) *ret_fp = os::Solaris::ucontext_get_fp(uc);
 221   } else {
 222     // construct empty ExtendedPC for return value checking
 223     epc = ExtendedPC(NULL);
 224     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 225     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 226   }
 227 
 228   return epc;
 229 }
 230 
 231 frame os::fetch_frame_from_context(const void* ucVoid) {
 232   intptr_t* sp;
 233   intptr_t* fp;
 234   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 235   return frame(sp, fp, epc.pc());
 236 }
 237 
 238 frame os::fetch_frame_from_ucontext(Thread* thread, void* ucVoid) {
 239   intptr_t* sp;
 240   intptr_t* fp;
 241   ExtendedPC epc = os::Solaris::fetch_frame_from_ucontext(thread, (ucontext_t*)ucVoid, &sp, &fp);
 242   return frame(sp, fp, epc.pc());
 243 }
 244 
 245 bool os::Solaris::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 246  address pc = (address) os::Solaris::ucontext_get_pc(uc);
 247   if (Interpreter::contains(pc)) {
 248     // interpreter performs stack banging after the fixed frame header has
 249     // been generated while the compilers perform it before. To maintain
 250     // semantic consistency between interpreted and compiled frames, the
 251     // method returns the Java sender of the current frame.
 252     *fr = os::fetch_frame_from_ucontext(thread, uc);
 253     if (!fr->is_first_java_frame()) {
 254       assert(fr->safe_for_sender(thread), "Safety check");
 255       *fr = fr->java_sender();
 256     }
 257   } else {
 258     // more complex code with compiled code
 259     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 260     CodeBlob* cb = CodeCache::find_blob(pc);
 261     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 262       // Not sure where the pc points to, fallback to default
 263       // stack overflow handling
 264       return false;
 265     } else {
 266       // in compiled code, the stack banging is performed just after the return pc
 267       // has been pushed on the stack
 268       intptr_t* fp = os::Solaris::ucontext_get_fp(uc);
 269       intptr_t* sp = os::Solaris::ucontext_get_sp(uc);
 270       *fr = frame(sp + 1, fp, (address)*sp);
 271       if (!fr->is_java_frame()) {
 272         assert(fr->safe_for_sender(thread), "Safety check");
 273         *fr = fr->java_sender();
 274       }
 275     }
 276   }
 277   assert(fr->is_java_frame(), "Safety check");
 278   return true;
 279 }
 280 
 281 frame os::get_sender_for_C_frame(frame* fr) {
 282   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
 283 }
 284 
 285 extern "C" intptr_t *_get_current_sp();  // in .il file
 286 
 287 address os::current_stack_pointer() {
 288   return (address)_get_current_sp();
 289 }
 290 
 291 extern "C" intptr_t *_get_current_fp();  // in .il file
 292 
 293 frame os::current_frame() {
 294   intptr_t* fp = _get_current_fp();  // it's inlined so want current fp
 295   // fp is for os::current_frame. We want the fp for our caller.
 296   frame myframe((intptr_t*)os::current_stack_pointer(),
 297                 (intptr_t*)fp,
 298                 CAST_FROM_FN_PTR(address, os::current_frame));
 299   frame caller_frame = os::get_sender_for_C_frame(&myframe);
 300 
 301   if (os::is_first_C_frame(&caller_frame)) {
 302     // stack is not walkable
 303     frame ret; // This will be a null useless frame
 304     return ret;
 305   } else {
 306     // return frame for our caller's caller
 307     return os::get_sender_for_C_frame(&caller_frame);
 308   }
 309 }
 310 
 311 #ifndef AMD64
 312 
 313 // Detecting SSE support by OS
 314 // From solaris_i486.s
 315 extern "C" bool sse_check();
 316 extern "C" bool sse_unavailable();
 317 
 318 enum { SSE_UNKNOWN, SSE_NOT_SUPPORTED, SSE_SUPPORTED};
 319 static int sse_status = SSE_UNKNOWN;
 320 
 321 
 322 static void  check_for_sse_support() {
 323   if (!VM_Version::supports_sse()) {
 324     sse_status = SSE_NOT_SUPPORTED;
 325     return;
 326   }
 327   // looking for _sse_hw in libc.so, if it does not exist or
 328   // the value (int) is 0, OS has no support for SSE
 329   int *sse_hwp;
 330   void *h;
 331 
 332   if ((h=dlopen("/usr/lib/libc.so", RTLD_LAZY)) == NULL) {
 333     //open failed, presume no support for SSE
 334     sse_status = SSE_NOT_SUPPORTED;
 335     return;
 336   }
 337   if ((sse_hwp = (int *)dlsym(h, "_sse_hw")) == NULL) {
 338     sse_status = SSE_NOT_SUPPORTED;
 339   } else if (*sse_hwp == 0) {
 340     sse_status = SSE_NOT_SUPPORTED;
 341   }
 342   dlclose(h);
 343 
 344   if (sse_status == SSE_UNKNOWN) {
 345     bool (*try_sse)() = (bool (*)())sse_check;
 346     sse_status = (*try_sse)() ? SSE_SUPPORTED : SSE_NOT_SUPPORTED;
 347   }
 348 
 349 }
 350 
 351 #endif // AMD64
 352 
 353 bool os::supports_sse() {
 354 #ifdef AMD64
 355   return true;
 356 #else
 357   if (sse_status == SSE_UNKNOWN)
 358     check_for_sse_support();
 359   return sse_status == SSE_SUPPORTED;
 360 #endif // AMD64
 361 }
 362 
 363 bool os::is_allocatable(size_t bytes) {
 364 #ifdef AMD64
 365   return true;
 366 #else
 367 
 368   if (bytes < 2 * G) {
 369     return true;
 370   }
 371 
 372   char* addr = reserve_memory(bytes, NULL);
 373 
 374   if (addr != NULL) {
 375     release_memory(addr, bytes);
 376   }
 377 
 378   return addr != NULL;
 379 #endif // AMD64
 380 
 381 }
 382 
 383 extern "C" JNIEXPORT int
 384 JVM_handle_solaris_signal(int sig, siginfo_t* info, void* ucVoid,
 385                           int abort_if_unrecognized) {
 386   ucontext_t* uc = (ucontext_t*) ucVoid;
 387 
 388 #ifndef AMD64
 389   if (sig == SIGILL && info->si_addr == (caddr_t)sse_check) {
 390     // the SSE instruction faulted. supports_sse() need return false.
 391     uc->uc_mcontext.gregs[EIP] = (greg_t)sse_unavailable;
 392     return true;
 393   }
 394 #endif // !AMD64
 395 
 396   Thread* t = Thread::current_or_null_safe();
 397 
 398   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 399   // (no destructors can be run)
 400   os::WatcherThreadCrashProtection::check_crash_protection(sig, t);
 401 
 402   SignalHandlerMark shm(t);
 403 
 404   if(sig == SIGPIPE || sig == SIGXFSZ) {
 405     if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 406       return true;
 407     } else {
 408       // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219
 409       return true;
 410     }
 411   }
 412 
 413   JavaThread* thread = NULL;
 414   VMThread* vmthread = NULL;
 415 
 416   if (os::Solaris::signal_handlers_are_installed) {
 417     if (t != NULL ){
 418       if(t->is_Java_thread()) {
 419         thread = (JavaThread*)t;
 420       }
 421       else if(t->is_VM_thread()){
 422         vmthread = (VMThread *)t;
 423       }
 424     }
 425   }
 426 
 427   if (sig == os::Solaris::SIGasync()) {
 428     if(thread || vmthread){
 429       OSThread::SR_handler(t, uc);
 430       return true;
 431     } else if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 432       return true;
 433     } else {
 434       // If os::Solaris::SIGasync not chained, and this is a non-vm and
 435       // non-java thread
 436       return true;
 437     }
 438   }
 439 
 440   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 441     // can't decode this kind of signal
 442     info = NULL;
 443   } else {
 444     assert(sig == info->si_signo, "bad siginfo");
 445   }
 446 
 447   // decide if this trap can be handled by a stub
 448   address stub = NULL;
 449 
 450   address pc          = NULL;
 451 
 452   //%note os_trap_1
 453   if (info != NULL && uc != NULL && thread != NULL) {
 454     // factor me: getPCfromContext
 455     pc = (address) uc->uc_mcontext.gregs[REG_PC];
 456 
 457     if (StubRoutines::is_safefetch_fault(pc)) {
 458       os::Solaris::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 459       return true;
 460     }
 461 
 462     // Handle ALL stack overflow variations here
 463     if (sig == SIGSEGV && info->si_code == SEGV_ACCERR) {
 464       address addr = (address) info->si_addr;
 465       if (thread->in_stack_yellow_reserved_zone(addr)) {
 466         if (thread->thread_state() == _thread_in_Java) {
 467           if (thread->in_stack_reserved_zone(addr)) {
 468             frame fr;
 469             if (os::Solaris::get_frame_at_stack_banging_point(thread, uc, &fr)) {
 470               assert(fr.is_java_frame(), "Must be Java frame");
 471               frame activation = SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
 472               if (activation.sp() != NULL) {
 473                 thread->disable_stack_reserved_zone();
 474                 if (activation.is_interpreted_frame()) {
 475                   thread->set_reserved_stack_activation((address)(
 476                     activation.fp() + frame::interpreter_frame_initial_sp_offset));
 477                 } else {
 478                   thread->set_reserved_stack_activation((address)activation.unextended_sp());
 479                 }
 480                 return true;
 481               }
 482             }
 483           }
 484           // Throw a stack overflow exception.  Guard pages will be reenabled
 485           // while unwinding the stack.
 486           thread->disable_stack_yellow_reserved_zone();
 487           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 488         } else {
 489           // Thread was in the vm or native code.  Return and try to finish.
 490           thread->disable_stack_yellow_reserved_zone();
 491           return true;
 492         }
 493       } else if (thread->in_stack_red_zone(addr)) {
 494         // Fatal red zone violation.  Disable the guard pages and fall through
 495         // to handle_unexpected_exception way down below.
 496         thread->disable_stack_red_zone();
 497         tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 498       }
 499     }
 500 
 501     if ((sig == SIGSEGV) && VM_Version::is_cpuinfo_segv_addr(pc)) {
 502       // Verify that OS save/restore AVX registers.
 503       stub = VM_Version::cpuinfo_cont_addr();
 504     }
 505 
 506     if (thread->thread_state() == _thread_in_vm) {
 507       if (sig == SIGBUS && info->si_code == BUS_OBJERR && thread->doing_unsafe_access()) {
 508         address next_pc = Assembler::locate_next_instruction(pc);
 509         stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
 510       }
 511     }
 512 
 513     if (thread->thread_state() == _thread_in_Java) {
 514       // Support Safepoint Polling
 515       if ( sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
 516         stub = SharedRuntime::get_poll_stub(pc);
 517       }
 518       else if (sig == SIGBUS && info->si_code == BUS_OBJERR) {
 519         // BugId 4454115: A read from a MappedByteBuffer can fault
 520         // here if the underlying file has been truncated.
 521         // Do not crash the VM in such a case.
 522         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 523         if (cb != NULL) {
 524           CompiledMethod* nm = cb->as_compiled_method_or_null();
 525           if (nm != NULL && nm->has_unsafe_access()) {
 526             address next_pc = Assembler::locate_next_instruction(pc);
 527             stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
 528           }
 529         }
 530       }
 531       else
 532       if (sig == SIGFPE && info->si_code == FPE_INTDIV) {
 533         // integer divide by zero
 534         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 535       }
 536 #ifndef AMD64
 537       else if (sig == SIGFPE && info->si_code == FPE_FLTDIV) {
 538         // floating-point divide by zero
 539         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 540       }
 541       else if (sig == SIGFPE && info->si_code == FPE_FLTINV) {
 542         // The encoding of D2I in i486.ad can cause an exception prior
 543         // to the fist instruction if there was an invalid operation
 544         // pending. We want to dismiss that exception. From the win_32
 545         // side it also seems that if it really was the fist causing
 546         // the exception that we do the d2i by hand with different
 547         // rounding. Seems kind of weird. QQQ TODO
 548         // Note that we take the exception at the NEXT floating point instruction.
 549         if (pc[0] == 0xDB) {
 550             assert(pc[0] == 0xDB, "not a FIST opcode");
 551             assert(pc[1] == 0x14, "not a FIST opcode");
 552             assert(pc[2] == 0x24, "not a FIST opcode");
 553             return true;
 554         } else {
 555             assert(pc[-3] == 0xDB, "not an flt invalid opcode");
 556             assert(pc[-2] == 0x14, "not an flt invalid opcode");
 557             assert(pc[-1] == 0x24, "not an flt invalid opcode");
 558         }
 559       }
 560       else if (sig == SIGFPE ) {
 561         tty->print_cr("caught SIGFPE, info 0x%x.", info->si_code);
 562       }
 563 #endif // !AMD64
 564 
 565         // QQQ It doesn't seem that we need to do this on x86 because we should be able
 566         // to return properly from the handler without this extra stuff on the back side.
 567 
 568       else if (sig == SIGSEGV && info->si_code > 0 && !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
 569         // Determination of interpreter/vtable stub/compiled code null exception
 570         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 571       }
 572     }
 573 
 574     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 575     // and the heap gets shrunk before the field access.
 576     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 577       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 578       if (addr != (address)-1) {
 579         stub = addr;
 580       }
 581     }
 582 
 583     // Check to see if we caught the safepoint code in the
 584     // process of write protecting the memory serialization page.
 585     // It write enables the page immediately after protecting it
 586     // so we can just return to retry the write.
 587     if ((sig == SIGSEGV) &&
 588         os::is_memory_serialize_page(thread, (address)info->si_addr)) {
 589       // Block current thread until the memory serialize page permission restored.
 590       os::block_on_serialize_page_trap();
 591       return true;
 592     }
 593   }
 594 
 595   // Execution protection violation
 596   //
 597   // Preventative code for future versions of Solaris which may
 598   // enable execution protection when running the 32-bit VM on AMD64.
 599   //
 600   // This should be kept as the last step in the triage.  We don't
 601   // have a dedicated trap number for a no-execute fault, so be
 602   // conservative and allow other handlers the first shot.
 603   //
 604   // Note: We don't test that info->si_code == SEGV_ACCERR here.
 605   // this si_code is so generic that it is almost meaningless; and
 606   // the si_code for this condition may change in the future.
 607   // Furthermore, a false-positive should be harmless.
 608   if (UnguardOnExecutionViolation > 0 &&
 609       (sig == SIGSEGV || sig == SIGBUS) &&
 610       uc->uc_mcontext.gregs[TRAPNO] == T_PGFLT) {  // page fault
 611     int page_size = os::vm_page_size();
 612     address addr = (address) info->si_addr;
 613     address pc = (address) uc->uc_mcontext.gregs[REG_PC];
 614     // Make sure the pc and the faulting address are sane.
 615     //
 616     // If an instruction spans a page boundary, and the page containing
 617     // the beginning of the instruction is executable but the following
 618     // page is not, the pc and the faulting address might be slightly
 619     // different - we still want to unguard the 2nd page in this case.
 620     //
 621     // 15 bytes seems to be a (very) safe value for max instruction size.
 622     bool pc_is_near_addr =
 623       (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
 624     bool instr_spans_page_boundary =
 625       (align_size_down((intptr_t) pc ^ (intptr_t) addr,
 626                        (intptr_t) page_size) > 0);
 627 
 628     if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
 629       static volatile address last_addr =
 630         (address) os::non_memory_address_word();
 631 
 632       // In conservative mode, don't unguard unless the address is in the VM
 633       if (addr != last_addr &&
 634           (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
 635 
 636         // Make memory rwx and retry
 637         address page_start =
 638           (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
 639         bool res = os::protect_memory((char*) page_start, page_size,
 640                                       os::MEM_PROT_RWX);
 641 
 642         log_debug(os)("Execution protection violation "
 643                       "at " INTPTR_FORMAT
 644                       ", unguarding " INTPTR_FORMAT ": %s, errno=%d", p2i(addr),
 645                       p2i(page_start), (res ? "success" : "failed"), errno);
 646         stub = pc;
 647 
 648         // Set last_addr so if we fault again at the same address, we don't end
 649         // up in an endless loop.
 650         //
 651         // There are two potential complications here.  Two threads trapping at
 652         // the same address at the same time could cause one of the threads to
 653         // think it already unguarded, and abort the VM.  Likely very rare.
 654         //
 655         // The other race involves two threads alternately trapping at
 656         // different addresses and failing to unguard the page, resulting in
 657         // an endless loop.  This condition is probably even more unlikely than
 658         // the first.
 659         //
 660         // Although both cases could be avoided by using locks or thread local
 661         // last_addr, these solutions are unnecessary complication: this
 662         // handler is a best-effort safety net, not a complete solution.  It is
 663         // disabled by default and should only be used as a workaround in case
 664         // we missed any no-execute-unsafe VM code.
 665 
 666         last_addr = addr;
 667       }
 668     }
 669   }
 670 
 671   if (stub != NULL) {
 672     // save all thread context in case we need to restore it
 673 
 674     if (thread != NULL) thread->set_saved_exception_pc(pc);
 675     // 12/02/99: On Sparc it appears that the full context is also saved
 676     // but as yet, no one looks at or restores that saved context
 677     os::Solaris::ucontext_set_pc(uc, stub);
 678     return true;
 679   }
 680 
 681   // signal-chaining
 682   if (os::Solaris::chained_handler(sig, info, ucVoid)) {
 683     return true;
 684   }
 685 
 686 #ifndef AMD64
 687   // Workaround (bug 4900493) for Solaris kernel bug 4966651.
 688   // Handle an undefined selector caused by an attempt to assign
 689   // fs in libthread getipriptr(). With the current libthread design every 512
 690   // thread creations the LDT for a private thread data structure is extended
 691   // and thre is a hazard that and another thread attempting a thread creation
 692   // will use a stale LDTR that doesn't reflect the structure's growth,
 693   // causing a GP fault.
 694   // Enforce the probable limit of passes through here to guard against an
 695   // infinite loop if some other move to fs caused the GP fault. Note that
 696   // this loop counter is ultimately a heuristic as it is possible for
 697   // more than one thread to generate this fault at a time in an MP system.
 698   // In the case of the loop count being exceeded or if the poll fails
 699   // just fall through to a fatal error.
 700   // If there is some other source of T_GPFLT traps and the text at EIP is
 701   // unreadable this code will loop infinitely until the stack is exausted.
 702   // The key to diagnosis in this case is to look for the bottom signal handler
 703   // frame.
 704 
 705   if(! IgnoreLibthreadGPFault) {
 706     if (sig == SIGSEGV && uc->uc_mcontext.gregs[TRAPNO] == T_GPFLT) {
 707       const unsigned char *p =
 708                         (unsigned const char *) uc->uc_mcontext.gregs[EIP];
 709 
 710       // Expected instruction?
 711 
 712       if(p[0] == movlfs[0] && p[1] == movlfs[1]) {
 713 
 714         Atomic::inc(&ldtr_refresh);
 715 
 716         // Infinite loop?
 717 
 718         if(ldtr_refresh < ((2 << 16) / PAGESIZE)) {
 719 
 720           // No, force scheduling to get a fresh view of the LDTR
 721 
 722           if(poll(NULL, 0, 10) == 0) {
 723 
 724             // Retry the move
 725 
 726             return false;
 727           }
 728         }
 729       }
 730     }
 731   }
 732 #endif // !AMD64
 733 
 734   if (!abort_if_unrecognized) {
 735     // caller wants another chance, so give it to him
 736     return false;
 737   }
 738 
 739   if (!os::Solaris::libjsig_is_loaded) {
 740     struct sigaction oldAct;
 741     sigaction(sig, (struct sigaction *)0, &oldAct);
 742     if (oldAct.sa_sigaction != signalHandler) {
 743       void* sighand = oldAct.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oldAct.sa_sigaction)
 744                                           : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
 745       warning("Unexpected Signal %d occurred under user-defined signal handler %#lx", sig, (long)sighand);
 746     }
 747   }
 748 
 749   if (pc == NULL && uc != NULL) {
 750     pc = (address) uc->uc_mcontext.gregs[REG_PC];
 751   }
 752 
 753   // unmask current signal
 754   sigset_t newset;
 755   sigemptyset(&newset);
 756   sigaddset(&newset, sig);
 757   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 758 
 759   // Determine which sort of error to throw.  Out of swap may signal
 760   // on the thread stack, which could get a mapping error when touched.
 761   address addr = (address) info->si_addr;
 762   if (sig == SIGBUS && info->si_code == BUS_OBJERR && info->si_errno == ENOMEM) {
 763     vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "Out of swap space to map in thread stack.");
 764   }
 765 
 766   VMError::report_and_die(t, sig, pc, info, ucVoid);
 767 
 768   ShouldNotReachHere();
 769   return false;
 770 }
 771 
 772 void os::print_context(outputStream *st, const void *context) {
 773   if (context == NULL) return;
 774 
 775   const ucontext_t *uc = (const ucontext_t*)context;
 776   st->print_cr("Registers:");
 777 #ifdef AMD64
 778   st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
 779   st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
 780   st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
 781   st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
 782   st->cr();
 783   st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
 784   st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
 785   st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
 786   st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
 787   st->cr();
 788   st->print(  "R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
 789   st->print(", R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
 790   st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
 791   st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
 792   st->cr();
 793   st->print(  "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
 794   st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
 795   st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
 796   st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
 797   st->cr();
 798   st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
 799   st->print(", RFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RFL]);
 800 #else
 801   st->print(  "EAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EAX]);
 802   st->print(", EBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBX]);
 803   st->print(", ECX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ECX]);
 804   st->print(", EDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDX]);
 805   st->cr();
 806   st->print(  "ESP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[UESP]);
 807   st->print(", EBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EBP]);
 808   st->print(", ESI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[ESI]);
 809   st->print(", EDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EDI]);
 810   st->cr();
 811   st->print(  "EIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EIP]);
 812   st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[EFL]);
 813 #endif // AMD64
 814   st->cr();
 815   st->cr();
 816 
 817   intptr_t *sp = (intptr_t *)os::Solaris::ucontext_get_sp(uc);
 818   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
 819   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 820   st->cr();
 821 
 822   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 823   // point to garbage if entry point in an nmethod is corrupted. Leave
 824   // this at the end, and hope for the best.
 825   ExtendedPC epc = os::Solaris::ucontext_get_ExtendedPC(uc);
 826   address pc = epc.pc();
 827   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", pc);
 828   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 829 }
 830 
 831 void os::print_register_info(outputStream *st, const void *context) {
 832   if (context == NULL) return;
 833 
 834   const ucontext_t *uc = (const ucontext_t*)context;
 835 
 836   st->print_cr("Register to memory mapping:");
 837   st->cr();
 838 
 839   // this is horrendously verbose but the layout of the registers in the
 840   // context does not match how we defined our abstract Register set, so
 841   // we can't just iterate through the gregs area
 842 
 843   // this is only for the "general purpose" registers
 844 
 845 #ifdef AMD64
 846   st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
 847   st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
 848   st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
 849   st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
 850   st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
 851   st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
 852   st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
 853   st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
 854   st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
 855   st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
 856   st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
 857   st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
 858   st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
 859   st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
 860   st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
 861   st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
 862 #else
 863   st->print("EAX="); print_location(st, uc->uc_mcontext.gregs[EAX]);
 864   st->print("EBX="); print_location(st, uc->uc_mcontext.gregs[EBX]);
 865   st->print("ECX="); print_location(st, uc->uc_mcontext.gregs[ECX]);
 866   st->print("EDX="); print_location(st, uc->uc_mcontext.gregs[EDX]);
 867   st->print("ESP="); print_location(st, uc->uc_mcontext.gregs[UESP]);
 868   st->print("EBP="); print_location(st, uc->uc_mcontext.gregs[EBP]);
 869   st->print("ESI="); print_location(st, uc->uc_mcontext.gregs[ESI]);
 870   st->print("EDI="); print_location(st, uc->uc_mcontext.gregs[EDI]);
 871 #endif
 872 
 873   st->cr();
 874 }
 875 
 876 
 877 #ifdef AMD64
 878 void os::Solaris::init_thread_fpu_state(void) {
 879   // Nothing to do
 880 }
 881 #else
 882 // From solaris_i486.s
 883 extern "C" void fixcw();
 884 
 885 void os::Solaris::init_thread_fpu_state(void) {
 886   // Set fpu to 53 bit precision. This happens too early to use a stub.
 887   fixcw();
 888 }
 889 
 890 // These routines are the initial value of atomic_xchg_entry(),
 891 // atomic_cmpxchg_entry(), atomic_inc_entry() and fence_entry()
 892 // until initialization is complete.
 893 // TODO - replace with .il implementation when compiler supports it.
 894 
 895 typedef jint  xchg_func_t        (jint,  volatile jint*);
 896 typedef jint  cmpxchg_func_t     (jint,  volatile jint*,  jint);
 897 typedef jlong cmpxchg_long_func_t(jlong, volatile jlong*, jlong);
 898 typedef jint  add_func_t         (jint,  volatile jint*);
 899 
 900 jint os::atomic_xchg_bootstrap(jint exchange_value, volatile jint* dest) {
 901   // try to use the stub:
 902   xchg_func_t* func = CAST_TO_FN_PTR(xchg_func_t*, StubRoutines::atomic_xchg_entry());
 903 
 904   if (func != NULL) {
 905     os::atomic_xchg_func = func;
 906     return (*func)(exchange_value, dest);
 907   }
 908   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 909 
 910   jint old_value = *dest;
 911   *dest = exchange_value;
 912   return old_value;
 913 }
 914 
 915 jint os::atomic_cmpxchg_bootstrap(jint exchange_value, volatile jint* dest, jint compare_value) {
 916   // try to use the stub:
 917   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
 918 
 919   if (func != NULL) {
 920     os::atomic_cmpxchg_func = func;
 921     return (*func)(exchange_value, dest, compare_value);
 922   }
 923   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 924 
 925   jint old_value = *dest;
 926   if (old_value == compare_value)
 927     *dest = exchange_value;
 928   return old_value;
 929 }
 930 
 931 jlong os::atomic_cmpxchg_long_bootstrap(jlong exchange_value, volatile jlong* dest, jlong compare_value) {
 932   // try to use the stub:
 933   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
 934 
 935   if (func != NULL) {
 936     os::atomic_cmpxchg_long_func = func;
 937     return (*func)(exchange_value, dest, compare_value);
 938   }
 939   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 940 
 941   jlong old_value = *dest;
 942   if (old_value == compare_value)
 943     *dest = exchange_value;
 944   return old_value;
 945 }
 946 
 947 jint os::atomic_add_bootstrap(jint add_value, volatile jint* dest) {
 948   // try to use the stub:
 949   add_func_t* func = CAST_TO_FN_PTR(add_func_t*, StubRoutines::atomic_add_entry());
 950 
 951   if (func != NULL) {
 952     os::atomic_add_func = func;
 953     return (*func)(add_value, dest);
 954   }
 955   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 956 
 957   return (*dest) += add_value;
 958 }
 959 
 960 xchg_func_t*         os::atomic_xchg_func         = os::atomic_xchg_bootstrap;
 961 cmpxchg_func_t*      os::atomic_cmpxchg_func      = os::atomic_cmpxchg_bootstrap;
 962 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
 963 add_func_t*          os::atomic_add_func          = os::atomic_add_bootstrap;
 964 
 965 extern "C" void _solaris_raw_setup_fpu(address ptr);
 966 void os::setup_fpu() {
 967   address fpu_cntrl = StubRoutines::addr_fpu_cntrl_wrd_std();
 968   _solaris_raw_setup_fpu(fpu_cntrl);
 969 }
 970 #endif // AMD64
 971 
 972 #ifndef PRODUCT
 973 void os::verify_stack_alignment() {
 974 #ifdef AMD64
 975   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 976 #endif
 977 }
 978 #endif
 979 
 980 int os::extra_bang_size_in_bytes() {
 981   // JDK-8050147 requires the full cache line bang for x86.
 982   return VM_Version::L1_line_size();
 983 }