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