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