1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2015 SAP AG. 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 "assembler_ppc.inline.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.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 "mutex_linux.inline.hpp"
  37 #include "nativeInst_ppc.hpp"
  38 #include "os_share_linux.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm.h"
  41 #include "prims/jvm_misc.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/extendedPC.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/interfaceSupport.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/mutexLocker.hpp"
  49 #include "runtime/osThread.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/events.hpp"
  55 #include "utilities/vmError.hpp"
  56 
  57 // put OS-includes here
  58 # include <sys/types.h>
  59 # include <sys/mman.h>
  60 # include <pthread.h>
  61 # include <signal.h>
  62 # include <errno.h>
  63 # include <dlfcn.h>
  64 # include <stdlib.h>
  65 # include <stdio.h>
  66 # include <unistd.h>
  67 # include <sys/resource.h>
  68 # include <pthread.h>
  69 # include <sys/stat.h>
  70 # include <sys/time.h>
  71 # include <sys/utsname.h>
  72 # include <sys/socket.h>
  73 # include <sys/wait.h>
  74 # include <pwd.h>
  75 # include <poll.h>
  76 # include <ucontext.h>
  77 
  78 
  79 address os::current_stack_pointer() {
  80   intptr_t* csp;
  81 
  82   // inline assembly `mr regno(csp), R1_SP':
  83   __asm__ __volatile__ ("mr %0, 1":"=r"(csp):);
  84 
  85   return (address) csp;
  86 }
  87 
  88 char* os::non_memory_address_word() {
  89   // Must never look like an address returned by reserve_memory,
  90   // even in its subfields (as defined by the CPU immediate fields,
  91   // if the CPU splits constants across multiple instructions).
  92 
  93   return (char*) -1;
  94 }
  95 
  96 void os::initialize_thread(Thread *thread) { }
  97 
  98 // Frame information (pc, sp, fp) retrieved via ucontext
  99 // always looks like a C-frame according to the frame
 100 // conventions in frame_ppc64.hpp.
 101 address os::Linux::ucontext_get_pc(ucontext_t * uc) {
 102   // On powerpc64, ucontext_t is not selfcontained but contains
 103   // a pointer to an optional substructure (mcontext_t.regs) containing the volatile
 104   // registers - NIP, among others.
 105   // This substructure may or may not be there depending where uc came from:
 106   // - if uc was handed over as the argument to a sigaction handler, a pointer to the
 107   //   substructure was provided by the kernel when calling the signal handler, and
 108   //   regs->nip can be accessed.
 109   // - if uc was filled by getcontext(), it is undefined - getcontext() does not fill
 110   //   it because the volatile registers are not needed to make setcontext() work.
 111   //   Hopefully it was zero'd out beforehand.
 112   guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_get_pc in sigaction context");
 113   return (address)uc->uc_mcontext.regs->nip;
 114 }
 115 
 116 // modify PC in ucontext.
 117 // Note: Only use this for an ucontext handed down to a signal handler. See comment
 118 // in ucontext_get_pc.
 119 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) {
 120   guarantee(uc->uc_mcontext.regs != NULL, "only use ucontext_set_pc in sigaction context");
 121   uc->uc_mcontext.regs->nip = (unsigned long)pc;
 122 }
 123 
 124 intptr_t* os::Linux::ucontext_get_sp(ucontext_t * uc) {
 125   return (intptr_t*)uc->uc_mcontext.regs->gpr[1/*REG_SP*/];
 126 }
 127 
 128 intptr_t* os::Linux::ucontext_get_fp(ucontext_t * uc) {
 129   return NULL;
 130 }
 131 
 132 ExtendedPC os::fetch_frame_from_context(void* ucVoid,
 133                     intptr_t** ret_sp, intptr_t** ret_fp) {
 134 
 135   ExtendedPC  epc;
 136   ucontext_t* uc = (ucontext_t*)ucVoid;
 137 
 138   if (uc != NULL) {
 139     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
 140     if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
 141     if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
 142   } else {
 143     // construct empty ExtendedPC for return value checking
 144     epc = ExtendedPC(NULL);
 145     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 146     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 147   }
 148 
 149   return epc;
 150 }
 151 
 152 frame os::fetch_frame_from_context(void* ucVoid) {
 153   intptr_t* sp;
 154   intptr_t* fp;
 155   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 156   return frame(sp, epc.pc());
 157 }
 158 
 159 frame os::get_sender_for_C_frame(frame* fr) {
 160   if (*fr->sp() == 0) {
 161     // fr is the last C frame
 162     return frame(NULL, NULL);
 163   }
 164   return frame(fr->sender_sp(), fr->sender_pc());
 165 }
 166 
 167 
 168 frame os::current_frame() {
 169   intptr_t* csp = (intptr_t*) *((intptr_t*) os::current_stack_pointer());
 170   // hack.
 171   frame topframe(csp, (address)0x8);
 172   // return sender of current topframe which hopefully has pc != NULL.
 173   return os::get_sender_for_C_frame(&topframe);
 174 }
 175 
 176 // Utility functions
 177 
 178 extern "C" JNIEXPORT int
 179 JVM_handle_linux_signal(int sig,
 180                         siginfo_t* info,
 181                         void* ucVoid,
 182                         int abort_if_unrecognized) {
 183   ucontext_t* uc = (ucontext_t*) ucVoid;
 184 
 185   Thread* t = Thread::current_or_null_safe();
 186 
 187   SignalHandlerMark shm(t);
 188 
 189   // Note: it's not uncommon that JNI code uses signal/sigset to install
 190   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 191   // or have a SIGILL handler when detecting CPU type). When that happens,
 192   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 193   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 194   // that do not require siginfo/ucontext first.
 195 
 196   if (sig == SIGPIPE) {
 197     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 198       return true;
 199     } else {
 200       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 201         warning("Ignoring SIGPIPE - see bug 4229104");
 202       }
 203       return true;
 204     }
 205   }
 206 
 207   JavaThread* thread = NULL;
 208   VMThread* vmthread = NULL;
 209   if (os::Linux::signal_handlers_are_installed) {
 210     if (t != NULL) {
 211       if(t->is_Java_thread()) {
 212         thread = (JavaThread*)t;
 213       } else if(t->is_VM_thread()) {
 214         vmthread = (VMThread *)t;
 215       }
 216     }
 217   }
 218 
 219   // Moved SafeFetch32 handling outside thread!=NULL conditional block to make
 220   // it work if no associated JavaThread object exists.
 221   if (uc) {
 222     address const pc = os::Linux::ucontext_get_pc(uc);
 223     if (pc && StubRoutines::is_safefetch_fault(pc)) {
 224       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 225       return true;
 226     }
 227   }
 228 
 229   // decide if this trap can be handled by a stub
 230   address stub = NULL;
 231   address pc   = NULL;
 232 
 233   //%note os_trap_1
 234   if (info != NULL && uc != NULL && thread != NULL) {
 235     pc = (address) os::Linux::ucontext_get_pc(uc);
 236 
 237     // Handle ALL stack overflow variations here
 238     if (sig == SIGSEGV) {
 239       // Si_addr may not be valid due to a bug in the linux-ppc64 kernel (see
 240       // comment below). Use get_stack_bang_address instead of si_addr.
 241       address addr = ((NativeInstruction*)pc)->get_stack_bang_address(uc);
 242 
 243       // Check if fault address is within thread stack.
 244       if (addr < thread->stack_base() &&
 245           addr >= thread->stack_base() - thread->stack_size()) {
 246         // stack overflow
 247         if (thread->in_stack_yellow_zone(addr)) {
 248           thread->disable_stack_yellow_zone();
 249           if (thread->thread_state() == _thread_in_Java) {
 250             // Throw a stack overflow exception.
 251             // Guard pages will be reenabled while unwinding the stack.
 252             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 253           } else {
 254             // Thread was in the vm or native code. Return and try to finish.
 255             return 1;
 256           }
 257         } else if (thread->in_stack_red_zone(addr)) {
 258           // Fatal red zone violation.  Disable the guard pages and fall through
 259           // to handle_unexpected_exception way down below.
 260           thread->disable_stack_red_zone();
 261           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 262 
 263           // This is a likely cause, but hard to verify. Let's just print
 264           // it as a hint.
 265           tty->print_raw_cr("Please check if any of your loaded .so files has "
 266                             "enabled executable stack (see man page execstack(8))");
 267         } else {
 268           // Accessing stack address below sp may cause SEGV if current
 269           // thread has MAP_GROWSDOWN stack. This should only happen when
 270           // current thread was created by user code with MAP_GROWSDOWN flag
 271           // and then attached to VM. See notes in os_linux.cpp.
 272           if (thread->osthread()->expanding_stack() == 0) {
 273              thread->osthread()->set_expanding_stack();
 274              if (os::Linux::manually_expand_stack(thread, addr)) {
 275                thread->osthread()->clear_expanding_stack();
 276                return 1;
 277              }
 278              thread->osthread()->clear_expanding_stack();
 279           } else {
 280              fatal("recursive segv. expanding stack.");
 281           }
 282         }
 283       }
 284     }
 285 
 286     if (thread->thread_state() == _thread_in_Java) {
 287       // Java thread running in Java code => find exception handler if any
 288       // a fault inside compiled code, the interpreter, or a stub
 289 
 290       // A VM-related SIGILL may only occur if we are not in the zero page.
 291       // On AIX, we get a SIGILL if we jump to 0x0 or to somewhere else
 292       // in the zero page, because it is filled with 0x0. We ignore
 293       // explicit SIGILLs in the zero page.
 294       if (sig == SIGILL && (pc < (address) 0x200)) {
 295         if (TraceTraps) {
 296           tty->print_raw_cr("SIGILL happened inside zero page.");
 297         }
 298         goto report_and_die;
 299       }
 300 
 301       CodeBlob *cb = NULL;
 302       // Handle signal from NativeJump::patch_verified_entry().
 303       if (( TrapBasedNotEntrantChecks && sig == SIGTRAP && nativeInstruction_at(pc)->is_sigtrap_zombie_not_entrant()) ||
 304           (!TrapBasedNotEntrantChecks && sig == SIGILL  && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant())) {
 305         if (TraceTraps) {
 306           tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL");
 307         }
 308         stub = SharedRuntime::get_handle_wrong_method_stub();
 309       }
 310 
 311       else if (sig == SIGSEGV &&
 312                // A linux-ppc64 kernel before 2.6.6 doesn't set si_addr on some segfaults
 313                // in 64bit mode (cf. http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.6),
 314                // especially when we try to read from the safepoint polling page. So the check
 315                //   (address)info->si_addr == os::get_standard_polling_page()
 316                // doesn't work for us. We use:
 317                ((NativeInstruction*)pc)->is_safepoint_poll() &&
 318                CodeCache::contains((void*) pc) &&
 319                ((cb = CodeCache::find_blob(pc)) != NULL) &&
 320                cb->is_nmethod()) {
 321         if (TraceTraps) {
 322           tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
 323         }
 324         stub = SharedRuntime::get_poll_stub(pc);
 325       }
 326 
 327       // SIGTRAP-based ic miss check in compiled code.
 328       else if (sig == SIGTRAP && TrapBasedICMissChecks &&
 329                nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {
 330         if (TraceTraps) {
 331           tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 332         }
 333         stub = SharedRuntime::get_ic_miss_stub();
 334       }
 335 
 336       // SIGTRAP-based implicit null check in compiled code.
 337       else if (sig == SIGTRAP && TrapBasedNullChecks &&
 338                nativeInstruction_at(pc)->is_sigtrap_null_check()) {
 339         if (TraceTraps) {
 340           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 341         }
 342         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 343       }
 344 
 345       // SIGSEGV-based implicit null check in compiled code.
 346       else if (sig == SIGSEGV && ImplicitNullChecks &&
 347                CodeCache::contains((void*) pc) &&
 348                !MacroAssembler::needs_explicit_null_check((intptr_t) info->si_addr)) {
 349         if (TraceTraps) {
 350           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", p2i(pc));
 351         }
 352         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 353       }
 354 
 355 #ifdef COMPILER2
 356       // SIGTRAP-based implicit range check in compiled code.
 357       else if (sig == SIGTRAP && TrapBasedRangeChecks &&
 358                nativeInstruction_at(pc)->is_sigtrap_range_check()) {
 359         if (TraceTraps) {
 360           tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", p2i(pc));
 361         }
 362         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 363       }
 364 #endif
 365       else if (sig == SIGBUS) {
 366         // BugId 4454115: A read from a MappedByteBuffer can fault here if the
 367         // underlying file has been truncated. Do not crash the VM in such a case.
 368         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 369         nmethod* nm = (cb != NULL && cb->is_nmethod()) ? (nmethod*)cb : NULL;
 370         if (nm != NULL && nm->has_unsafe_access()) {
 371           // We don't really need a stub here! Just set the pending exeption and
 372           // continue at the next instruction after the faulting read. Returning
 373           // garbage from this read is ok.
 374           thread->set_pending_unsafe_access_error();
 375           os::Linux::ucontext_set_pc(uc, pc + 4);
 376           return true;
 377         }
 378       }
 379     }
 380 
 381     else { // thread->thread_state() != _thread_in_Java
 382       if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
 383         // SIGILL must be caused by VM_Version::determine_features().
 384         *(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,
 385                         // flushing of icache is not necessary.
 386         stub = pc + 4;  // continue with next instruction.
 387       }
 388       else if (thread->thread_state() == _thread_in_vm &&
 389                sig == SIGBUS && thread->doing_unsafe_access()) {
 390         // We don't really need a stub here! Just set the pending exeption and
 391         // continue at the next instruction after the faulting read. Returning
 392         // garbage from this read is ok.
 393         thread->set_pending_unsafe_access_error();
 394         os::Linux::ucontext_set_pc(uc, pc + 4);
 395         return true;
 396       }
 397     }
 398 
 399     // Check to see if we caught the safepoint code in the
 400     // process of write protecting the memory serialization page.
 401     // It write enables the page immediately after protecting it
 402     // so we can just return to retry the write.
 403     if ((sig == SIGSEGV) &&
 404         // Si_addr may not be valid due to a bug in the linux-ppc64 kernel (see comment above).
 405         // Use is_memory_serialization instead of si_addr.
 406         ((NativeInstruction*)pc)->is_memory_serialization(thread, ucVoid)) {
 407       // Synchronization problem in the pseudo memory barrier code (bug id 6546278)
 408       // Block current thread until the memory serialize page permission restored.
 409       os::block_on_serialize_page_trap();
 410       return true;
 411     }
 412   }
 413 
 414   if (stub != NULL) {
 415     // Save all thread context in case we need to restore it.
 416     if (thread != NULL) thread->set_saved_exception_pc(pc);
 417     os::Linux::ucontext_set_pc(uc, stub);
 418     return true;
 419   }
 420 
 421   // signal-chaining
 422   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 423     return true;
 424   }
 425 
 426   if (!abort_if_unrecognized) {
 427     // caller wants another chance, so give it to him
 428     return false;
 429   }
 430 
 431   if (pc == NULL && uc != NULL) {
 432     pc = os::Linux::ucontext_get_pc(uc);
 433   }
 434 
 435 report_and_die:
 436   // unmask current signal
 437   sigset_t newset;
 438   sigemptyset(&newset);
 439   sigaddset(&newset, sig);
 440   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 441 
 442   VMError::report_and_die(t, sig, pc, info, ucVoid);
 443 
 444   ShouldNotReachHere();
 445   return false;
 446 }
 447 
 448 void os::Linux::init_thread_fpu_state(void) {
 449   // Disable FP exceptions.
 450   __asm__ __volatile__ ("mtfsfi 6,0");
 451 }
 452 
 453 int os::Linux::get_fpu_control_word(void) {
 454   // x86 has problems with FPU precision after pthread_cond_timedwait().
 455   // nothing to do on ppc64.
 456   return 0;
 457 }
 458 
 459 void os::Linux::set_fpu_control_word(int fpu_control) {
 460   // x86 has problems with FPU precision after pthread_cond_timedwait().
 461   // nothing to do on ppc64.
 462 }
 463 
 464 ////////////////////////////////////////////////////////////////////////////////
 465 // thread stack
 466 
 467 size_t os::Linux::min_stack_allowed = 128*K;
 468 
 469 // return default stack size for thr_type
 470 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 471   // default stack size (compiler thread needs larger stack)
 472   // Notice that the setting for compiler threads here have no impact
 473   // because of the strange 'fallback logic' in os::create_thread().
 474   // Better set CompilerThreadStackSize in globals_<os_cpu>.hpp if you want to
 475   // specify a different stack size for compiler threads!
 476   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1024 * K);
 477   return s;
 478 }
 479 
 480 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 481   return 2 * page_size();
 482 }
 483 
 484 // Java thread:
 485 //
 486 //   Low memory addresses
 487 //    +------------------------+
 488 //    |                        |\  JavaThread created by VM does not have glibc
 489 //    |    glibc guard page    | - guard, attached Java thread usually has
 490 //    |                        |/  1 page glibc guard.
 491 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 492 //    |                        |\
 493 //    |  HotSpot Guard Pages   | - red and yellow pages
 494 //    |                        |/
 495 //    +------------------------+ JavaThread::stack_yellow_zone_base()
 496 //    |                        |\
 497 //    |      Normal Stack      | -
 498 //    |                        |/
 499 // P2 +------------------------+ Thread::stack_base()
 500 //
 501 // Non-Java thread:
 502 //
 503 //   Low memory addresses
 504 //    +------------------------+
 505 //    |                        |\
 506 //    |  glibc guard page      | - usually 1 page
 507 //    |                        |/
 508 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 509 //    |                        |\
 510 //    |      Normal Stack      | -
 511 //    |                        |/
 512 // P2 +------------------------+ Thread::stack_base()
 513 //
 514 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
 515 //    pthread_attr_getstack()
 516 
 517 static void current_stack_region(address * bottom, size_t * size) {
 518   if (os::Linux::is_initial_thread()) {
 519      // initial thread needs special handling because pthread_getattr_np()
 520      // may return bogus value.
 521     *bottom = os::Linux::initial_thread_stack_bottom();
 522     *size   = os::Linux::initial_thread_stack_size();
 523   } else {
 524     pthread_attr_t attr;
 525 
 526     int rslt = pthread_getattr_np(pthread_self(), &attr);
 527 
 528     // JVM needs to know exact stack location, abort if it fails
 529     if (rslt != 0) {
 530       if (rslt == ENOMEM) {
 531         vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");
 532       } else {
 533         fatal("pthread_getattr_np failed with errno = %d", rslt);
 534       }
 535     }
 536 
 537     if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
 538       fatal("Can not locate current stack attributes!");
 539     }
 540 
 541     pthread_attr_destroy(&attr);
 542 
 543   }
 544   assert(os::current_stack_pointer() >= *bottom &&
 545          os::current_stack_pointer() < *bottom + *size, "just checking");
 546 }
 547 
 548 address os::current_stack_base() {
 549   address bottom;
 550   size_t size;
 551   current_stack_region(&bottom, &size);
 552   return (bottom + size);
 553 }
 554 
 555 size_t os::current_stack_size() {
 556   // stack size includes normal stack and HotSpot guard pages
 557   address bottom;
 558   size_t size;
 559   current_stack_region(&bottom, &size);
 560   return size;
 561 }
 562 
 563 /////////////////////////////////////////////////////////////////////////////
 564 // helper functions for fatal error handler
 565 
 566 void os::print_context(outputStream *st, void *context) {
 567   if (context == NULL) return;
 568 
 569   ucontext_t* uc = (ucontext_t*)context;
 570 
 571   st->print_cr("Registers:");
 572   st->print("pc =" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->nip);
 573   st->print("lr =" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->link);
 574   st->print("ctr=" INTPTR_FORMAT "  ", uc->uc_mcontext.regs->ctr);
 575   st->cr();
 576   for (int i = 0; i < 32; i++) {
 577     st->print("r%-2d=" INTPTR_FORMAT "  ", i, uc->uc_mcontext.regs->gpr[i]);
 578     if (i % 3 == 2) st->cr();
 579   }
 580   st->cr();
 581   st->cr();
 582 
 583   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 584   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 585   print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));
 586   st->cr();
 587 
 588   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 589   // point to garbage if entry point in an nmethod is corrupted. Leave
 590   // this at the end, and hope for the best.
 591   address pc = os::Linux::ucontext_get_pc(uc);
 592   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
 593   print_hex_dump(st, pc - 64, pc + 64, /*instrsize=*/4);
 594   st->cr();
 595 }
 596 
 597 void os::print_register_info(outputStream *st, void *context) {
 598   if (context == NULL) return;
 599 
 600   ucontext_t *uc = (ucontext_t*)context;
 601 
 602   st->print_cr("Register to memory mapping:");
 603   st->cr();
 604 
 605   // this is only for the "general purpose" registers
 606   for (int i = 0; i < 32; i++) {
 607     st->print("r%-2d=", i);
 608     print_location(st, uc->uc_mcontext.regs->gpr[i]);
 609   }
 610   st->cr();
 611 }
 612 
 613 extern "C" {
 614   int SpinPause() {
 615     return 0;
 616   }
 617 }
 618 
 619 #ifndef PRODUCT
 620 void os::verify_stack_alignment() {
 621   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 622 }
 623 #endif
 624 
 625 int os::extra_bang_size_in_bytes() {
 626   // PPC does not require the additional stack bang.
 627   return 0;
 628 }