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_linux.h"
  35 #include "memory/allocation.inline.hpp"
  36 #include "nativeInst_sparc.hpp"
  37 #include "os_share_linux.hpp"
  38 #include "prims/jniFastGetField.hpp"
  39 #include "prims/jvm.h"
  40 #include "prims/jvm_misc.hpp"
  41 #include "runtime/arguments.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 // Linux/Sparc has rather obscure naming of registers in sigcontext
  57 // different between 32 and 64 bits
  58 #ifdef _LP64
  59 #define SIG_PC(x) ((x)->sigc_regs.tpc)
  60 #define SIG_NPC(x) ((x)->sigc_regs.tnpc)
  61 #define SIG_REGS(x) ((x)->sigc_regs)
  62 #else
  63 #define SIG_PC(x) ((x)->si_regs.pc)
  64 #define SIG_NPC(x) ((x)->si_regs.npc)
  65 #define SIG_REGS(x) ((x)->si_regs)
  66 #endif
  67 
  68 // those are to reference registers in sigcontext
  69 enum {
  70   CON_G0 = 0,
  71   CON_G1,
  72   CON_G2,
  73   CON_G3,
  74   CON_G4,
  75   CON_G5,
  76   CON_G6,
  77   CON_G7,
  78   CON_O0,
  79   CON_O1,
  80   CON_O2,
  81   CON_O3,
  82   CON_O4,
  83   CON_O5,
  84   CON_O6,
  85   CON_O7,
  86 };
  87 
  88 // For Forte Analyzer AsyncGetCallTrace profiling support - thread is
  89 // currently interrupted by SIGPROF.
  90 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested
  91 // signal frames. Currently we don't do that on Linux, so it's the
  92 // same as os::fetch_frame_from_context().
  93 ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread,
  94                                                 const ucontext_t* uc,
  95                                                 intptr_t** ret_sp,
  96                                                 intptr_t** ret_fp) {
  97   assert(thread != NULL, "just checking");
  98   assert(ret_sp != NULL, "just checking");
  99   assert(ret_fp != NULL, "just checking");
 100 
 101   return os::fetch_frame_from_context(uc, ret_sp, ret_fp);
 102 }
 103 
 104 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 105                                         intptr_t** ret_sp,
 106                                         intptr_t** ret_fp) {
 107   const ucontext_t* uc = (const ucontext_t*) ucVoid;
 108   ExtendedPC  epc;
 109 
 110   if (uc != NULL) {
 111     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
 112     if (ret_sp) {
 113       *ret_sp = os::Linux::ucontext_get_sp(uc);
 114     }
 115     if (ret_fp) {
 116       *ret_fp = (intptr_t*)NULL;
 117     }
 118   } else {
 119     // construct empty ExtendedPC for return value checking
 120     epc = ExtendedPC(NULL);
 121     if (ret_sp) {
 122       *ret_sp = (intptr_t*) NULL;
 123     }
 124     if (ret_fp) {
 125       *ret_fp = (intptr_t*) NULL;
 126     }
 127   }
 128 
 129   return epc;
 130 }
 131 
 132 frame os::fetch_frame_from_context(const void* ucVoid) {
 133   intptr_t* sp;
 134   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, NULL);
 135   return frame(sp, frame::unpatchable, epc.pc());
 136 }
 137 
 138 frame os::get_sender_for_C_frame(frame* fr) {
 139   return frame(fr->sender_sp(), frame::unpatchable, fr->sender_pc());
 140 }
 141 
 142 frame os::current_frame() {
 143   intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
 144   frame myframe(sp, frame::unpatchable,
 145                 CAST_FROM_FN_PTR(address, os::current_frame));
 146   if (os::is_first_C_frame(&myframe)) {
 147     // stack is not walkable
 148     return frame(NULL, frame::unpatchable, NULL);
 149   } else {
 150     return os::get_sender_for_C_frame(&myframe);
 151   }
 152 }
 153 
 154 address os::current_stack_pointer() {
 155   register void *sp __asm__ ("sp");
 156   return (address)sp;
 157 }
 158 
 159 char* os::non_memory_address_word() {
 160   // Must never look like an address returned by reserve_memory,
 161   // even in its subfields (as defined by the CPU immediate fields,
 162   // if the CPU splits constants across multiple instructions).
 163   // On SPARC, 0 != %hi(any real address), because there is no
 164   // allocation in the first 1Kb of the virtual address space.
 165   return (char*) 0;
 166 }
 167 
 168 void os::initialize_thread(Thread* thr) {}
 169 
 170 void os::print_context(outputStream *st, const void *context) {
 171   if (context == NULL) return;
 172 
 173   const ucontext_t* uc = (const ucontext_t*)context;
 174   sigcontext* sc = (sigcontext*)context;
 175   st->print_cr("Registers:");
 176 
 177   st->print_cr(" G1=" INTPTR_FORMAT " G2=" INTPTR_FORMAT
 178                " G3=" INTPTR_FORMAT " G4=" INTPTR_FORMAT,
 179                SIG_REGS(sc).u_regs[CON_G1],
 180                SIG_REGS(sc).u_regs[CON_G2],
 181                SIG_REGS(sc).u_regs[CON_G3],
 182                SIG_REGS(sc).u_regs[CON_G4]);
 183   st->print_cr(" G5=" INTPTR_FORMAT " G6=" INTPTR_FORMAT
 184                " G7=" INTPTR_FORMAT " Y=0x%x",
 185                SIG_REGS(sc).u_regs[CON_G5],
 186                SIG_REGS(sc).u_regs[CON_G6],
 187                SIG_REGS(sc).u_regs[CON_G7],
 188                SIG_REGS(sc).y);
 189   st->print_cr(" O0=" INTPTR_FORMAT " O1=" INTPTR_FORMAT
 190                " O2=" INTPTR_FORMAT " O3=" INTPTR_FORMAT,
 191                SIG_REGS(sc).u_regs[CON_O0],
 192                SIG_REGS(sc).u_regs[CON_O1],
 193                SIG_REGS(sc).u_regs[CON_O2],
 194                SIG_REGS(sc).u_regs[CON_O3]);
 195   st->print_cr(" O4=" INTPTR_FORMAT " O5=" INTPTR_FORMAT
 196                " O6=" INTPTR_FORMAT " O7=" INTPTR_FORMAT,
 197                SIG_REGS(sc).u_regs[CON_O4],
 198                SIG_REGS(sc).u_regs[CON_O5],
 199                SIG_REGS(sc).u_regs[CON_O6],
 200                SIG_REGS(sc).u_regs[CON_O7]);
 201 
 202 
 203   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 204   st->print_cr(" L0=" INTPTR_FORMAT " L1=" INTPTR_FORMAT
 205                " L2=" INTPTR_FORMAT " L3=" INTPTR_FORMAT,
 206                sp[L0->sp_offset_in_saved_window()],
 207                sp[L1->sp_offset_in_saved_window()],
 208                sp[L2->sp_offset_in_saved_window()],
 209                sp[L3->sp_offset_in_saved_window()]);
 210   st->print_cr(" L4=" INTPTR_FORMAT " L5=" INTPTR_FORMAT
 211                " L6=" INTPTR_FORMAT " L7=" INTPTR_FORMAT,
 212                sp[L4->sp_offset_in_saved_window()],
 213                sp[L5->sp_offset_in_saved_window()],
 214                sp[L6->sp_offset_in_saved_window()],
 215                sp[L7->sp_offset_in_saved_window()]);
 216   st->print_cr(" I0=" INTPTR_FORMAT " I1=" INTPTR_FORMAT
 217                " I2=" INTPTR_FORMAT " I3=" INTPTR_FORMAT,
 218                sp[I0->sp_offset_in_saved_window()],
 219                sp[I1->sp_offset_in_saved_window()],
 220                sp[I2->sp_offset_in_saved_window()],
 221                sp[I3->sp_offset_in_saved_window()]);
 222   st->print_cr(" I4=" INTPTR_FORMAT " I5=" INTPTR_FORMAT
 223                " I6=" INTPTR_FORMAT " I7=" INTPTR_FORMAT,
 224                sp[I4->sp_offset_in_saved_window()],
 225                sp[I5->sp_offset_in_saved_window()],
 226                sp[I6->sp_offset_in_saved_window()],
 227                sp[I7->sp_offset_in_saved_window()]);
 228 
 229   st->print_cr(" PC=" INTPTR_FORMAT " nPC=" INTPTR_FORMAT,
 230                SIG_PC(sc),
 231                SIG_NPC(sc));
 232   st->cr();
 233   st->cr();
 234 
 235   st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", p2i(sp));
 236   print_hex_dump(st, (address)sp, (address)(sp + 32), sizeof(intptr_t));
 237   st->cr();
 238 
 239   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 240   // point to garbage if entry point in an nmethod is corrupted. Leave
 241   // this at the end, and hope for the best.
 242   address pc = os::Linux::ucontext_get_pc(uc);
 243   st->print_cr("Instructions: (pc=" INTPTR_FORMAT ")", p2i(pc));
 244   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 245 }
 246 
 247 
 248 void os::print_register_info(outputStream *st, const void *context) {
 249   if (context == NULL) return;
 250 
 251   const ucontext_t *uc = (const ucontext_t*)context;
 252   const sigcontext* sc = (const sigcontext*)context;
 253   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 254 
 255   st->print_cr("Register to memory mapping:");
 256   st->cr();
 257 
 258   // this is only for the "general purpose" registers
 259   st->print("G1="); print_location(st, SIG_REGS(sc).u_regs[CON_G1]);
 260   st->print("G2="); print_location(st, SIG_REGS(sc).u_regs[CON_G2]);
 261   st->print("G3="); print_location(st, SIG_REGS(sc).u_regs[CON_G3]);
 262   st->print("G4="); print_location(st, SIG_REGS(sc).u_regs[CON_G4]);
 263   st->print("G5="); print_location(st, SIG_REGS(sc).u_regs[CON_G5]);
 264   st->print("G6="); print_location(st, SIG_REGS(sc).u_regs[CON_G6]);
 265   st->print("G7="); print_location(st, SIG_REGS(sc).u_regs[CON_G7]);
 266   st->cr();
 267 
 268   st->print("O0="); print_location(st, SIG_REGS(sc).u_regs[CON_O0]);
 269   st->print("O1="); print_location(st, SIG_REGS(sc).u_regs[CON_O1]);
 270   st->print("O2="); print_location(st, SIG_REGS(sc).u_regs[CON_O2]);
 271   st->print("O3="); print_location(st, SIG_REGS(sc).u_regs[CON_O3]);
 272   st->print("O4="); print_location(st, SIG_REGS(sc).u_regs[CON_O4]);
 273   st->print("O5="); print_location(st, SIG_REGS(sc).u_regs[CON_O5]);
 274   st->print("O6="); print_location(st, SIG_REGS(sc).u_regs[CON_O6]);
 275   st->print("O7="); print_location(st, SIG_REGS(sc).u_regs[CON_O7]);
 276   st->cr();
 277 
 278   st->print("L0="); print_location(st, sp[L0->sp_offset_in_saved_window()]);
 279   st->print("L1="); print_location(st, sp[L1->sp_offset_in_saved_window()]);
 280   st->print("L2="); print_location(st, sp[L2->sp_offset_in_saved_window()]);
 281   st->print("L3="); print_location(st, sp[L3->sp_offset_in_saved_window()]);
 282   st->print("L4="); print_location(st, sp[L4->sp_offset_in_saved_window()]);
 283   st->print("L5="); print_location(st, sp[L5->sp_offset_in_saved_window()]);
 284   st->print("L6="); print_location(st, sp[L6->sp_offset_in_saved_window()]);
 285   st->print("L7="); print_location(st, sp[L7->sp_offset_in_saved_window()]);
 286   st->cr();
 287 
 288   st->print("I0="); print_location(st, sp[I0->sp_offset_in_saved_window()]);
 289   st->print("I1="); print_location(st, sp[I1->sp_offset_in_saved_window()]);
 290   st->print("I2="); print_location(st, sp[I2->sp_offset_in_saved_window()]);
 291   st->print("I3="); print_location(st, sp[I3->sp_offset_in_saved_window()]);
 292   st->print("I4="); print_location(st, sp[I4->sp_offset_in_saved_window()]);
 293   st->print("I5="); print_location(st, sp[I5->sp_offset_in_saved_window()]);
 294   st->print("I6="); print_location(st, sp[I6->sp_offset_in_saved_window()]);
 295   st->print("I7="); print_location(st, sp[I7->sp_offset_in_saved_window()]);
 296   st->cr();
 297 }
 298 
 299 
 300 address os::Linux::ucontext_get_pc(const ucontext_t* uc) {
 301   return (address) SIG_PC((sigcontext*)uc);
 302 }
 303 
 304 void os::Linux::ucontext_set_pc(ucontext_t* uc, address pc) {
 305   sigcontext* ctx = (sigcontext*) uc;
 306   SIG_PC(ctx)  = (intptr_t)pc;
 307   SIG_NPC(ctx) = (intptr_t)(pc+4);
 308 }
 309 
 310 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t *uc) {
 311   return (intptr_t*)
 312     ((intptr_t)SIG_REGS((sigcontext*)uc).u_regs[CON_O6] + STACK_BIAS);
 313 }
 314 
 315 // not used on Sparc
 316 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t *uc) {
 317   ShouldNotReachHere();
 318   return NULL;
 319 }
 320 
 321 // Utility functions
 322 
 323 inline static bool checkPrefetch(sigcontext* uc, address pc) {
 324   if (StubRoutines::is_safefetch_fault(pc)) {
 325     os::Linux::ucontext_set_pc((ucontext_t*)uc, StubRoutines::continuation_for_safefetch_fault(pc));
 326     return true;
 327   }
 328   return false;
 329 }
 330 
 331 inline static bool checkOverflow(sigcontext* uc,
 332                                  address pc,
 333                                  address addr,
 334                                  JavaThread* thread,
 335                                  address* stub) {
 336   // check if fault address is within thread stack
 337   if (thread->on_local_stack(addr)) {
 338     // stack overflow
 339     if (thread->in_stack_yellow_reserved_zone(addr)) {
 340       thread->disable_stack_yellow_reserved_zone();
 341       if (thread->thread_state() == _thread_in_Java) {
 342         // Throw a stack overflow exception.  Guard pages will be reenabled
 343         // while unwinding the stack.
 344         *stub =
 345           SharedRuntime::continuation_for_implicit_exception(thread,
 346                                                              pc,
 347                                                              SharedRuntime::STACK_OVERFLOW);
 348       } else {
 349         // Thread was in the vm or native code.  Return and try to finish.
 350         return true;
 351       }
 352     } else if (thread->in_stack_red_zone(addr)) {
 353       // Fatal red zone violation.  Disable the guard pages and fall through
 354       // to handle_unexpected_exception way down below.
 355       thread->disable_stack_red_zone();
 356       tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 357 
 358       // This is a likely cause, but hard to verify. Let's just print
 359       // it as a hint.
 360       tty->print_raw_cr("Please check if any of your loaded .so files has "
 361                         "enabled executable stack (see man page execstack(8))");
 362     } else {
 363       // Accessing stack address below sp may cause SEGV if current
 364       // thread has MAP_GROWSDOWN stack. This should only happen when
 365       // current thread was created by user code with MAP_GROWSDOWN flag
 366       // and then attached to VM. See notes in os_linux.cpp.
 367       if (thread->osthread()->expanding_stack() == 0) {
 368         thread->osthread()->set_expanding_stack();
 369         if (os::Linux::manually_expand_stack(thread, addr)) {
 370           thread->osthread()->clear_expanding_stack();
 371           return true;
 372         }
 373         thread->osthread()->clear_expanding_stack();
 374       } else {
 375         fatal("recursive segv. expanding stack.");
 376       }
 377     }
 378   }
 379   return false;
 380 }
 381 
 382 inline static bool checkPollingPage(address pc, address fault, address* stub) {
 383   if (fault == os::get_polling_page()) {
 384     *stub = SharedRuntime::get_poll_stub(pc);
 385     return true;
 386   }
 387   return false;
 388 }
 389 
 390 inline static bool checkByteBuffer(address pc, address npc, address* stub) {
 391   // BugId 4454115: A read from a MappedByteBuffer can fault
 392   // here if the underlying file has been truncated.
 393   // Do not crash the VM in such a case.
 394   CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 395   CompiledMethod* nm = cb->as_compiled_method_or_null();
 396   if (nm != NULL && nm->has_unsafe_access()) {
 397     *stub = SharedRuntime::handle_unsafe_access(thread, npc);
 398     return true;
 399   }
 400   return false;
 401 }
 402 
 403 inline static bool checkVerifyOops(address pc, address fault, address* stub) {
 404   if (pc >= MacroAssembler::_verify_oop_implicit_branch[0]
 405       && pc <  MacroAssembler::_verify_oop_implicit_branch[1] ) {
 406     *stub     =  MacroAssembler::_verify_oop_implicit_branch[2];
 407     warning("fixed up memory fault in +VerifyOops at address "
 408             INTPTR_FORMAT, p2i(fault));
 409     return true;
 410   }
 411   return false;
 412 }
 413 
 414 inline static bool checkFPFault(address pc, int code,
 415                                 JavaThread* thread, address* stub) {
 416   if (code == FPE_INTDIV || code == FPE_FLTDIV) {
 417     *stub =
 418       SharedRuntime::
 419       continuation_for_implicit_exception(thread,
 420                                           pc,
 421                                           SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 422     return true;
 423   }
 424   return false;
 425 }
 426 
 427 inline static bool checkNullPointer(address pc, intptr_t fault,
 428                                     JavaThread* thread, address* stub) {
 429   if (!MacroAssembler::needs_explicit_null_check(fault)) {
 430     // Determination of interpreter/vtable stub/compiled code null
 431     // exception
 432     *stub =
 433       SharedRuntime::
 434       continuation_for_implicit_exception(thread, pc,
 435                                           SharedRuntime::IMPLICIT_NULL);
 436     return true;
 437   }
 438   return false;
 439 }
 440 
 441 inline static bool checkFastJNIAccess(address pc, address* stub) {
 442   address addr = JNI_FastGetField::find_slowcase_pc(pc);
 443   if (addr != (address)-1) {
 444     *stub = addr;
 445     return true;
 446   }
 447   return false;
 448 }
 449 
 450 inline static bool checkSerializePage(JavaThread* thread, address addr) {
 451   return os::is_memory_serialize_page(thread, addr);
 452 }
 453 
 454 inline static bool checkZombie(sigcontext* uc, address* pc, address* stub) {
 455   if (nativeInstruction_at(*pc)->is_zombie()) {
 456     // zombie method (ld [%g0],%o7 instruction)
 457     *stub = SharedRuntime::get_handle_wrong_method_stub();
 458 
 459     // At the stub it needs to look like a call from the caller of this
 460     // method (not a call from the segv site).
 461     *pc = (address)SIG_REGS(uc).u_regs[CON_O7];
 462     return true;
 463   }
 464   return false;
 465 }
 466 
 467 inline static bool checkICMiss(sigcontext* uc, address* pc, address* stub) {
 468 #ifdef COMPILER2
 469   if (nativeInstruction_at(*pc)->is_ic_miss_trap()) {
 470 #ifdef ASSERT
 471 #ifdef TIERED
 472     CodeBlob* cb = CodeCache::find_blob_unsafe(*pc);
 473     assert(cb->is_compiled_by_c2(), "Wrong compiler");
 474 #endif // TIERED
 475 #endif // ASSERT
 476     // Inline cache missed and user trap "Tne G0+ST_RESERVED_FOR_USER_0+2" taken.
 477     *stub = SharedRuntime::get_ic_miss_stub();
 478     // At the stub it needs to look like a call from the caller of this
 479     // method (not a call from the segv site).
 480     *pc = (address)SIG_REGS(uc).u_regs[CON_O7];
 481     return true;
 482   }
 483 #endif  // COMPILER2
 484   return false;
 485 }
 486 
 487 extern "C" JNIEXPORT int
 488 JVM_handle_linux_signal(int sig,
 489                         siginfo_t* info,
 490                         void* ucVoid,
 491                         int abort_if_unrecognized) {
 492   // in fact this isn't ucontext_t* at all, but struct sigcontext*
 493   // but Linux porting layer uses ucontext_t, so to minimize code change
 494   // we cast as needed
 495   ucontext_t* ucFake = (ucontext_t*) ucVoid;
 496   sigcontext* uc = (sigcontext*)ucVoid;
 497 
 498   Thread* t = Thread::current_or_null_safe();
 499 
 500   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 501   // (no destructors can be run)
 502   os::WatcherThreadCrashProtection::check_crash_protection(sig, t);
 503 
 504   SignalHandlerMark shm(t);
 505 
 506   // Note: it's not uncommon that JNI code uses signal/sigset to install
 507   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 508   // or have a SIGILL handler when detecting CPU type). When that happens,
 509   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 510   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 511   // that do not require siginfo/ucontext first.
 512 
 513   if (sig == SIGPIPE || sig == SIGXFSZ) {
 514     // allow chained handler to go first
 515     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 516       return true;
 517     } else {
 518       // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219
 519       return true;
 520     }
 521   }
 522 
 523   JavaThread* thread = NULL;
 524   VMThread* vmthread = NULL;
 525   if (os::Linux::signal_handlers_are_installed) {
 526     if (t != NULL ){
 527       if(t->is_Java_thread()) {
 528         thread = (JavaThread*)t;
 529       }
 530       else if(t->is_VM_thread()){
 531         vmthread = (VMThread *)t;
 532       }
 533     }
 534   }
 535 
 536   // decide if this trap can be handled by a stub
 537   address stub = NULL;
 538   address pc = NULL;
 539   address npc = NULL;
 540 
 541   //%note os_trap_1
 542   if (info != NULL && uc != NULL && thread != NULL) {
 543     pc = address(SIG_PC(uc));
 544     npc = address(SIG_NPC(uc));
 545 
 546     // Check to see if we caught the safepoint code in the
 547     // process of write protecting the memory serialization page.
 548     // It write enables the page immediately after protecting it
 549     // so we can just return to retry the write.
 550     if ((sig == SIGSEGV) && checkSerializePage(thread, (address)info->si_addr)) {
 551       // Block current thread until the memory serialize page permission restored.
 552       os::block_on_serialize_page_trap();
 553       return 1;
 554     }
 555 
 556     if (checkPrefetch(uc, pc)) {
 557       return 1;
 558     }
 559 
 560     // Handle ALL stack overflow variations here
 561     if (sig == SIGSEGV) {
 562       if (checkOverflow(uc, pc, (address)info->si_addr, thread, &stub)) {
 563         return 1;
 564       }
 565     }
 566 
 567     if (sig == SIGBUS &&
 568         thread->thread_state() == _thread_in_vm &&
 569         thread->doing_unsafe_access()) {
 570       stub = SharedRuntime::handle_unsafe_access(thread, npc);
 571     }
 572 
 573     if (thread->thread_state() == _thread_in_Java) {
 574       do {
 575         // Java thread running in Java code => find exception handler if any
 576         // a fault inside compiled code, the interpreter, or a stub
 577 
 578         if ((sig == SIGSEGV) && checkPollingPage(pc, (address)info->si_addr, &stub)) {
 579           break;
 580         }
 581 
 582         if ((sig == SIGBUS) && checkByteBuffer(pc, npc, &stub)) {
 583           break;
 584         }
 585 
 586         if ((sig == SIGSEGV || sig == SIGBUS) &&
 587             checkVerifyOops(pc, (address)info->si_addr, &stub)) {
 588           break;
 589         }
 590 
 591         if ((sig == SIGSEGV) && checkZombie(uc, &pc, &stub)) {
 592           break;
 593         }
 594 
 595         if ((sig == SIGILL) && checkICMiss(uc, &pc, &stub)) {
 596           break;
 597         }
 598 
 599         if ((sig == SIGFPE) && checkFPFault(pc, info->si_code, thread, &stub)) {
 600           break;
 601         }
 602 
 603         if ((sig == SIGSEGV) &&
 604             checkNullPointer(pc, (intptr_t)info->si_addr, thread, &stub)) {
 605           break;
 606         }
 607       } while (0);
 608 
 609       // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 610       // and the heap gets shrunk before the field access.
 611       if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 612         checkFastJNIAccess(pc, &stub);
 613       }
 614     }
 615 
 616     if (stub != NULL) {
 617       // save all thread context in case we need to restore it
 618       thread->set_saved_exception_pc(pc);
 619       thread->set_saved_exception_npc(npc);
 620       os::Linux::ucontext_set_pc((ucontext_t*)uc, stub);
 621       return true;
 622     }
 623   }
 624 
 625   // signal-chaining
 626   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 627     return true;
 628   }
 629 
 630   if (!abort_if_unrecognized) {
 631     // caller wants another chance, so give it to him
 632     return false;
 633   }
 634 
 635   if (pc == NULL && uc != NULL) {
 636     pc = os::Linux::ucontext_get_pc((const ucontext_t*)uc);
 637   }
 638 
 639   // unmask current signal
 640   sigset_t newset;
 641   sigemptyset(&newset);
 642   sigaddset(&newset, sig);
 643   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 644 
 645   VMError::report_and_die(t, sig, pc, info, ucVoid);
 646 
 647   ShouldNotReachHere();
 648   return false;
 649 }
 650 
 651 void os::Linux::init_thread_fpu_state(void) {
 652   // Nothing to do
 653 }
 654 
 655 int os::Linux::get_fpu_control_word() {
 656   return 0;
 657 }
 658 
 659 void os::Linux::set_fpu_control_word(int fpu) {
 660   // nothing
 661 }
 662 
 663 bool os::is_allocatable(size_t bytes) {
 664 #ifdef _LP64
 665   return true;
 666 #else
 667   if (bytes < 2 * G) {
 668     return true;
 669   }
 670 
 671   char* addr = reserve_memory(bytes, NULL);
 672 
 673   if (addr != NULL) {
 674     release_memory(addr, bytes);
 675   }
 676 
 677   return addr != NULL;
 678 #endif // _LP64
 679 }
 680 
 681 ///////////////////////////////////////////////////////////////////////////////
 682 // thread stack
 683 
 684 size_t os::Posix::_compiler_thread_min_stack_allowed = 128 * K;
 685 size_t os::Posix::_java_thread_min_stack_allowed = 128 * K;
 686 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 128 * K;
 687 
 688 // return default stack size for thr_type
 689 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 690   // default stack size (compiler thread needs larger stack)
 691   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 692   return s;
 693 }
 694 
 695 #ifndef PRODUCT
 696 void os::verify_stack_alignment() {
 697 }
 698 #endif
 699 
 700 int os::extra_bang_size_in_bytes() {
 701   // SPARC does not require the additional stack bang.
 702   return 0;
 703 }