1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2019 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 // no precompiled headers
  27 #include "jvm.h"
  28 #include "asm/assembler.inline.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "code/icBuffer.hpp"
  34 #include "code/vtableStubs.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "nativeInst_ppc.hpp"
  38 #include "os_share_linux.hpp"
  39 #include "prims/jniFastGetField.hpp"
  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.inline.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/javaCalls.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/safepointMechanism.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/thread.inline.hpp"
  53 #include "runtime/timer.hpp"
  54 #include "utilities/debug.hpp"
  55 #include "utilities/events.hpp"
  56 #include "utilities/vmError.hpp"
  57 
  58 // put OS-includes here
  59 # include <sys/types.h>
  60 # include <sys/mman.h>
  61 # include <pthread.h>
  62 # include <signal.h>
  63 # include <errno.h>
  64 # include <dlfcn.h>
  65 # include <stdlib.h>
  66 # include <stdio.h>
  67 # include <unistd.h>
  68 # include <sys/resource.h>
  69 # include <pthread.h>
  70 # include <sys/stat.h>
  71 # include <sys/time.h>
  72 # include <sys/utsname.h>
  73 # include <sys/socket.h>
  74 # include <sys/wait.h>
  75 # include <pwd.h>
  76 # include <poll.h>
  77 # include <ucontext.h>
  78 
  79 
  80 address os::current_stack_pointer() {
  81   intptr_t* csp;
  82 
  83   // inline assembly `mr regno(csp), R1_SP':
  84   __asm__ __volatile__ ("mr %0, 1":"=r"(csp):);
  85 
  86   return (address) csp;
  87 }
  88 
  89 char* os::non_memory_address_word() {
  90   // Must never look like an address returned by reserve_memory,
  91   // even in its subfields (as defined by the CPU immediate fields,
  92   // if the CPU splits constants across multiple instructions).
  93 
  94   return (char*) -1;
  95 }
  96 
  97 // Frame information (pc, sp, fp) retrieved via ucontext
  98 // always looks like a C-frame according to the frame
  99 // conventions in frame_ppc64.hpp.
 100 address os::Linux::ucontext_get_pc(const ucontext_t * uc) {
 101   // On powerpc64, ucontext_t is not selfcontained but contains
 102   // a pointer to an optional substructure (mcontext_t.regs) containing the volatile
 103   // registers - NIP, among others.
 104   // This substructure may or may not be there depending where uc came from:
 105   // - if uc was handed over as the argument to a sigaction handler, a pointer to the
 106   //   substructure was provided by the kernel when calling the signal handler, and
 107   //   regs->nip can be accessed.
 108   // - if uc was filled by getcontext(), it is undefined - getcontext() does not fill
 109   //   it because the volatile registers are not needed to make setcontext() work.
 110   //   Hopefully it was zero'd out beforehand.
 111   guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_get_pc in sigaction context");
 112   return (address)uc->uc_mcontext.regs->nip;
 113 }
 114 
 115 // modify PC in ucontext.
 116 // Note: Only use this for an ucontext handed down to a signal handler. See comment
 117 // in ucontext_get_pc.
 118 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) {
 119   guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_set_pc in sigaction context");
 120   uc->uc_mcontext.regs->nip = (unsigned long)pc;
 121 }
 122 
 123 static address ucontext_get_lr(const ucontext_t * uc) {
 124   return (address)uc->uc_mcontext.regs->link;
 125 }
 126 
 127 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
 128   return (intptr_t*)uc->uc_mcontext.regs->gpr[1/*REG_SP*/];
 129 }
 130 
 131 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
 132   return NULL;
 133 }
 134 
 135 static unsigned long ucontext_get_trap(const ucontext_t * uc) {
 136   return uc->uc_mcontext.regs->trap;
 137 }
 138 
 139 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 140                     intptr_t** ret_sp, intptr_t** ret_fp) {
 141 
 142   ExtendedPC  epc;
 143   const ucontext_t* uc = (const ucontext_t*)ucVoid;
 144 
 145   if (uc != NULL) {
 146     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
 147     if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
 148     if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
 149   } else {
 150     // construct empty ExtendedPC for return value checking
 151     epc = ExtendedPC(NULL);
 152     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 153     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 154   }
 155 
 156   return epc;
 157 }
 158 
 159 frame os::fetch_frame_from_context(const void* ucVoid) {
 160   intptr_t* sp;
 161   intptr_t* fp;
 162   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 163   return frame(sp, epc.pc());
 164 }
 165 
 166 bool os::Linux::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 167   address pc = (address) os::Linux::ucontext_get_pc(uc);
 168   if (Interpreter::contains(pc)) {
 169     // Interpreter performs stack banging after the fixed frame header has
 170     // been generated while the compilers perform it before. To maintain
 171     // semantic consistency between interpreted and compiled frames, the
 172     // method returns the Java sender of the current frame.
 173     *fr = os::fetch_frame_from_context(uc);
 174     if (!fr->is_first_java_frame()) {
 175       assert(fr->safe_for_sender(thread), "Safety check");
 176       *fr = fr->java_sender();
 177     }
 178   } else {
 179     // More complex code with compiled code.
 180     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 181     CodeBlob* cb = CodeCache::find_blob(pc);
 182     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 183       // Not sure where the pc points to, fallback to default
 184       // stack overflow handling. In compiled code, we bang before
 185       // the frame is complete.
 186       return false;
 187     } else {
 188       intptr_t* sp = os::Linux::ucontext_get_sp(uc);
 189       address lr = ucontext_get_lr(uc);
 190       *fr = frame(sp, lr);
 191       if (!fr->is_java_frame()) {
 192         assert(fr->safe_for_sender(thread), "Safety check");
 193         assert(!fr->is_first_frame(), "Safety check");
 194         *fr = fr->java_sender();
 195       }
 196     }
 197   }
 198   assert(fr->is_java_frame(), "Safety check");
 199   return true;
 200 }
 201 
 202 frame os::get_sender_for_C_frame(frame* fr) {
 203   if (*fr->sp() == 0) {
 204     // fr is the last C frame
 205     return frame(NULL, NULL);
 206   }
 207   return frame(fr->sender_sp(), fr->sender_pc());
 208 }
 209 
 210 
 211 frame os::current_frame() {
 212   intptr_t* csp = (intptr_t*) *((intptr_t*) os::current_stack_pointer());
 213   // hack.
 214   frame topframe(csp, (address)0x8);
 215   // Return sender of sender of current topframe which hopefully
 216   // both have pc != NULL.
 217   frame tmp = os::get_sender_for_C_frame(&topframe);
 218   return os::get_sender_for_C_frame(&tmp);
 219 }
 220 
 221 // Utility functions
 222 
 223 extern "C" JNIEXPORT int
 224 JVM_handle_linux_signal(int sig,
 225                         siginfo_t* info,
 226                         void* ucVoid,
 227                         int abort_if_unrecognized) {
 228   ucontext_t* uc = (ucontext_t*) ucVoid;
 229 
 230   Thread* t = Thread::current_or_null_safe();
 231 
 232   SignalHandlerMark shm(t);
 233 
 234   // Note: it's not uncommon that JNI code uses signal/sigset to install
 235   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 236   // or have a SIGILL handler when detecting CPU type). When that happens,
 237   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 238   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 239   // that do not require siginfo/ucontext first.
 240 
 241   if (sig == SIGPIPE) {
 242     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 243       return true;
 244     } else {
 245       // Ignoring SIGPIPE - see bugs 4229104
 246       return true;
 247     }
 248   }
 249 
 250   // Make the signal handler transaction-aware by checking the existence of a
 251   // second (transactional) context with MSR TS bits active. If the signal is
 252   // caught during a transaction, then just return to the HTM abort handler.
 253   // Please refer to Linux kernel document powerpc/transactional_memory.txt,
 254   // section "Signals".
 255   if (uc && uc->uc_link) {
 256     ucontext_t* second_uc = uc->uc_link;
 257 
 258     // MSR TS bits are 29 and 30 (Power ISA, v2.07B, Book III-S, pp. 857-858,
 259     // 3.2.1 "Machine State Register"), however note that ISA notation for bit
 260     // numbering is MSB 0, so for normal bit numbering (LSB 0) they come to be
 261     // bits 33 and 34. It's not related to endianness, just a notation matter.
 262     if (second_uc->uc_mcontext.regs->msr & 0x600000000) {
 263       if (TraceTraps) {
 264         tty->print_cr("caught signal in transaction, "
 265                         "ignoring to jump to abort handler");
 266       }
 267       // Return control to the HTM abort handler.
 268       return true;
 269     }
 270   }
 271 
 272 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
 273   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
 274     handle_assert_poison_fault(ucVoid, info->si_addr);
 275     return 1;
 276   }
 277 #endif
 278 
 279   JavaThread* thread = NULL;
 280   VMThread* vmthread = NULL;
 281   if (os::Linux::signal_handlers_are_installed) {
 282     if (t != NULL) {
 283       if(t->is_Java_thread()) {
 284         thread = (JavaThread*)t;
 285       } else if(t->is_VM_thread()) {
 286         vmthread = (VMThread *)t;
 287       }
 288     }
 289   }
 290 
 291   // Moved SafeFetch32 handling outside thread!=NULL conditional block to make
 292   // it work if no associated JavaThread object exists.
 293   if (uc) {
 294     address const pc = os::Linux::ucontext_get_pc(uc);
 295     if (pc && StubRoutines::is_safefetch_fault(pc)) {
 296       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 297       return true;
 298     }
 299   }
 300 
 301   // decide if this trap can be handled by a stub
 302   address stub = NULL;
 303   address pc   = NULL;
 304 
 305   if (info != NULL && uc != NULL && thread != NULL) {
 306     pc = (address) os::Linux::ucontext_get_pc(uc);
 307 
 308     // Handle ALL stack overflow variations here
 309     if (sig == SIGSEGV) {
 310       // si_addr may not be valid due to a bug in the linux-ppc64 kernel (see
 311       // comment below). Use get_stack_bang_address instead of si_addr.
 312       // If SIGSEGV is caused due to a branch to an invalid address an
 313       // "Instruction Storage Interrupt" is generated and 'pc' (NIP) already
 314       // contains the invalid address. Otherwise, the SIGSEGV is caused due to
 315       // load/store instruction trying to load/store from/to an invalid address
 316       // and causing a "Data Storage Interrupt", so we inspect the intruction
 317       // in order to extract the faulty data addresss.
 318       address addr;
 319       if ((ucontext_get_trap(uc) & 0x0F00 /* no IRQ reply bits */) == 0x0400) {
 320         // Instruction Storage Interrupt (ISI)
 321         addr = pc;
 322       } else {
 323         // Data Storage Interrupt (DSI), i.e. 0x0300: extract faulty data address
 324         addr = ((NativeInstruction*)pc)->get_stack_bang_address(uc);
 325       }
 326 
 327       // Check if fault address is within thread stack.
 328       if (thread->on_local_stack(addr)) {
 329         // stack overflow
 330         if (thread->in_stack_yellow_reserved_zone(addr)) {
 331           if (thread->thread_state() == _thread_in_Java) {
 332             if (thread->in_stack_reserved_zone(addr)) {
 333               frame fr;
 334               if (os::Linux::get_frame_at_stack_banging_point(thread, uc, &fr)) {
 335                 assert(fr.is_java_frame(), "Must be a Javac frame");
 336                 frame activation =
 337                   SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
 338                 if (activation.sp() != NULL) {
 339                   thread->disable_stack_reserved_zone();
 340                   if (activation.is_interpreted_frame()) {
 341                     thread->set_reserved_stack_activation((address)activation.fp());
 342                   } else {
 343                     thread->set_reserved_stack_activation((address)activation.unextended_sp());
 344                   }
 345                   return 1;
 346                 }
 347               }
 348             }
 349             // Throw a stack overflow exception.
 350             // Guard pages will be reenabled while unwinding the stack.
 351             thread->disable_stack_yellow_reserved_zone();
 352             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 353           } else {
 354             // Thread was in the vm or native code. Return and try to finish.
 355             thread->disable_stack_yellow_reserved_zone();
 356             return 1;
 357           }
 358         } else if (thread->in_stack_red_zone(addr)) {
 359           // Fatal red zone violation.  Disable the guard pages and fall through
 360           // to handle_unexpected_exception way down below.
 361           thread->disable_stack_red_zone();
 362           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 363 
 364           // This is a likely cause, but hard to verify. Let's just print
 365           // it as a hint.
 366           tty->print_raw_cr("Please check if any of your loaded .so files has "
 367                             "enabled executable stack (see man page execstack(8))");
 368         } else {
 369           // Accessing stack address below sp may cause SEGV if current
 370           // thread has MAP_GROWSDOWN stack. This should only happen when
 371           // current thread was created by user code with MAP_GROWSDOWN flag
 372           // and then attached to VM. See notes in os_linux.cpp.
 373           if (thread->osthread()->expanding_stack() == 0) {
 374              thread->osthread()->set_expanding_stack();
 375              if (os::Linux::manually_expand_stack(thread, addr)) {
 376                thread->osthread()->clear_expanding_stack();
 377                return 1;
 378              }
 379              thread->osthread()->clear_expanding_stack();
 380           } else {
 381              fatal("recursive segv. expanding stack.");
 382           }
 383         }
 384       }
 385     }
 386 
 387     if (thread->thread_state() == _thread_in_Java) {
 388       // Java thread running in Java code => find exception handler if any
 389       // a fault inside compiled code, the interpreter, or a stub
 390 
 391       // A VM-related SIGILL may only occur if we are not in the zero page.
 392       // On AIX, we get a SIGILL if we jump to 0x0 or to somewhere else
 393       // in the zero page, because it is filled with 0x0. We ignore
 394       // explicit SIGILLs in the zero page.
 395       if (sig == SIGILL && (pc < (address) 0x200)) {
 396         if (TraceTraps) {
 397           tty->print_raw_cr("SIGILL happened inside zero page.");
 398         }
 399         goto report_and_die;
 400       }
 401 
 402       CodeBlob *cb = NULL;
 403       // Handle signal from NativeJump::patch_verified_entry().
 404       if (( TrapBasedNotEntrantChecks && sig == SIGTRAP && nativeInstruction_at(pc)->is_sigtrap_zombie_not_entrant()) ||
 405           (!TrapBasedNotEntrantChecks && sig == SIGILL  && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant())) {
 406         if (TraceTraps) {
 407           tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL");
 408         }
 409         stub = SharedRuntime::get_handle_wrong_method_stub();
 410       }
 411 
 412       else if (sig == ((SafepointMechanism::uses_thread_local_poll() && USE_POLL_BIT_ONLY) ? SIGTRAP : SIGSEGV) &&
 413                // A linux-ppc64 kernel before 2.6.6 doesn't set si_addr on some segfaults
 414                // in 64bit mode (cf. http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.6),
 415                // especially when we try to read from the safepoint polling page. So the check
 416                //   (address)info->si_addr == os::get_standard_polling_page()
 417                // doesn't work for us. We use:
 418                ((NativeInstruction*)pc)->is_safepoint_poll() &&
 419                CodeCache::contains((void*) pc) &&
 420                ((cb = CodeCache::find_blob(pc)) != NULL) &&
 421                cb->is_compiled()) {
 422         if (TraceTraps) {
 423           tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (%s)", p2i(pc),
 424                         (SafepointMechanism::uses_thread_local_poll() && USE_POLL_BIT_ONLY) ? "SIGTRAP" : "SIGSEGV");
 425         }
 426         stub = SharedRuntime::get_poll_stub(pc);
 427       }
 428 
 429       // SIGTRAP-based ic miss check in compiled code.
 430       else if (sig == SIGTRAP && TrapBasedICMissChecks &&
 431                nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {
 432         if (TraceTraps) {
 433           tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 434         }
 435         stub = SharedRuntime::get_ic_miss_stub();
 436       }
 437 
 438       // SIGTRAP-based implicit null check in compiled code.
 439       else if (sig == SIGTRAP && TrapBasedNullChecks &&
 440                nativeInstruction_at(pc)->is_sigtrap_null_check()) {
 441         if (TraceTraps) {
 442           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 443         }
 444         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 445       }
 446 
 447       // SIGSEGV-based implicit null check in compiled code.
 448       else if (sig == SIGSEGV && ImplicitNullChecks &&
 449                CodeCache::contains((void*) pc) &&
 450                MacroAssembler::uses_implicit_null_check(info->si_addr)) {
 451         if (TraceTraps) {
 452           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
 453         }
 454         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 455       }
 456 
 457 #ifdef COMPILER2
 458       // SIGTRAP-based implicit range check in compiled code.
 459       else if (sig == SIGTRAP && TrapBasedRangeChecks &&
 460                nativeInstruction_at(pc)->is_sigtrap_range_check()) {
 461         if (TraceTraps) {
 462           tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 463         }
 464         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 465       }
 466 #endif
 467       else if (sig == SIGBUS) {
 468         // BugId 4454115: A read from a MappedByteBuffer can fault here if the
 469         // underlying file has been truncated. Do not crash the VM in such a case.
 470         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 471         CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
 472         if (nm != NULL && nm->has_unsafe_access()) {
 473           address next_pc = pc + 4;
 474           next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
 475           os::Linux::ucontext_set_pc(uc, next_pc);
 476           return true;
 477         }
 478       }
 479     }
 480 
 481     else { // thread->thread_state() != _thread_in_Java
 482       if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
 483         // SIGILL must be caused by VM_Version::determine_features().
 484         *(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,
 485                         // flushing of icache is not necessary.
 486         stub = pc + 4;  // continue with next instruction.
 487       }
 488       else if (thread->thread_state() == _thread_in_vm &&
 489                sig == SIGBUS && thread->doing_unsafe_access()) {
 490         address next_pc = pc + 4;
 491         next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
 492         os::Linux::ucontext_set_pc(uc, pc + 4);
 493         return true;
 494       }
 495     }
 496   }
 497 
 498   if (stub != NULL) {
 499     // Save all thread context in case we need to restore it.
 500     if (thread != NULL) thread->set_saved_exception_pc(pc);
 501     os::Linux::ucontext_set_pc(uc, stub);
 502     return true;
 503   }
 504 
 505   // signal-chaining
 506   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 507     return true;
 508   }
 509 
 510   if (!abort_if_unrecognized) {
 511     // caller wants another chance, so give it to him
 512     return false;
 513   }
 514 
 515   if (pc == NULL && uc != NULL) {
 516     pc = os::Linux::ucontext_get_pc(uc);
 517   }
 518 
 519 report_and_die:
 520   // unmask current signal
 521   sigset_t newset;
 522   sigemptyset(&newset);
 523   sigaddset(&newset, sig);
 524   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 525 
 526   VMError::report_and_die(t, sig, pc, info, ucVoid);
 527 
 528   ShouldNotReachHere();
 529   return false;
 530 }
 531 
 532 void os::Linux::init_thread_fpu_state(void) {
 533   // Disable FP exceptions.
 534   __asm__ __volatile__ ("mtfsfi 6,0");
 535 }
 536 
 537 int os::Linux::get_fpu_control_word(void) {
 538   // x86 has problems with FPU precision after pthread_cond_timedwait().
 539   // nothing to do on ppc64.
 540   return 0;
 541 }
 542 
 543 void os::Linux::set_fpu_control_word(int fpu_control) {
 544   // x86 has problems with FPU precision after pthread_cond_timedwait().
 545   // nothing to do on ppc64.
 546 }
 547 
 548 ////////////////////////////////////////////////////////////////////////////////
 549 // thread stack
 550 
 551 // Minimum usable stack sizes required to get to user code. Space for
 552 // HotSpot guard pages is added later.
 553 size_t os::Posix::_compiler_thread_min_stack_allowed = 64 * K;
 554 size_t os::Posix::_java_thread_min_stack_allowed = 64 * K;
 555 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;
 556 
 557 // Return default stack size for thr_type.
 558 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 559   // Default stack size (compiler thread needs larger stack).
 560   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);
 561   return s;
 562 }
 563 
 564 /////////////////////////////////////////////////////////////////////////////
 565 // helper functions for fatal error handler
 566 
 567 void os::print_context(outputStream *st, const void *context) {
 568   if (context == NULL) return;
 569 
 570   const ucontext_t* uc = (const ucontext_t*)context;
 571 
 572   st->print_cr("Registers:");
 573   st->print("pc =" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->nip);
 574   st->print("lr =" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->link);
 575   st->print("ctr=" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->ctr);
 576   st->cr();
 577   for (int i = 0; i < 32; i++) {
 578     st->print("r%-2d=" INTPTR_FORMAT "  ", i, uc->uc_mcontext.regs->gpr[i]);
 579     if (i % 3 == 2) st->cr();
 580   }
 581   st->cr();
 582   st->cr();
 583 
 584   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 585   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 586   print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));
 587   st->cr();
 588 
 589   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 590   // point to garbage if entry point in an nmethod is corrupted. Leave
 591   // this at the end, and hope for the best.
 592   address pc = os::Linux::ucontext_get_pc(uc);
 593   print_instructions(st, pc, /*instrsize=*/4);
 594   st->cr();
 595 }
 596 
 597 void os::print_register_info(outputStream *st, const void *context) {
 598   if (context == NULL) return;
 599 
 600   const ucontext_t *uc = (const ucontext_t*)context;
 601 
 602   st->print_cr("Register to memory mapping:");
 603   st->cr();
 604 
 605   st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->nip);
 606   st->print("lr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->link);
 607   st->print("ctr ="); print_location(st, (intptr_t)uc->uc_mcontext.regs->ctr);
 608   for (int i = 0; i < 32; i++) {
 609     st->print("r%-2d=", i);
 610     print_location(st, uc->uc_mcontext.regs->gpr[i]);
 611   }
 612   st->cr();
 613 }
 614 
 615 extern "C" {
 616   int SpinPause() {
 617     return 0;
 618   }
 619 }
 620 
 621 #ifndef PRODUCT
 622 void os::verify_stack_alignment() {
 623   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 624 }
 625 #endif
 626 
 627 int os::extra_bang_size_in_bytes() {
 628   // PPC does not require the additional stack bang.
 629   return 0;
 630 }