1 /*
   2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2018 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_aix.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm_misc.hpp"
  41 #include "porting_aix.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/extendedPC.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/interfaceSupport.inline.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/mutexLocker.hpp"
  49 #include "runtime/osThread.hpp"
  50 #include "runtime/safepointMechanism.hpp"
  51 #include "runtime/sharedRuntime.hpp"
  52 #include "runtime/stubRoutines.hpp"
  53 #include "runtime/thread.inline.hpp"
  54 #include "runtime/timer.hpp"
  55 #include "utilities/events.hpp"
  56 #include "utilities/vmError.hpp"
  57 #ifdef COMPILER1
  58 #include "c1/c1_Runtime1.hpp"
  59 #endif
  60 #ifdef COMPILER2
  61 #include "opto/runtime.hpp"
  62 #endif
  63 
  64 // put OS-includes here
  65 # include <ucontext.h>
  66 
  67 address os::current_stack_pointer() {
  68   address csp;
  69 
  70 #if !defined(USE_XLC_BUILTINS)
  71   // inline assembly for `mr regno(csp), R1_SP':
  72   __asm__ __volatile__ ("mr %0, 1":"=r"(csp):);
  73 #else
  74   csp = (address) __builtin_frame_address(0);
  75 #endif
  76 
  77   return csp;
  78 }
  79 
  80 char* os::non_memory_address_word() {
  81   // Must never look like an address returned by reserve_memory,
  82   // even in its subfields (as defined by the CPU immediate fields,
  83   // if the CPU splits constants across multiple instructions).
  84 
  85   return (char*) -1;
  86 }
  87 
  88 // Frame information (pc, sp, fp) retrieved via ucontext
  89 // always looks like a C-frame according to the frame
  90 // conventions in frame_ppc.hpp.
  91 
  92 address os::Aix::ucontext_get_pc(const ucontext_t * uc) {
  93   return (address)uc->uc_mcontext.jmp_context.iar;
  94 }
  95 
  96 intptr_t* os::Aix::ucontext_get_sp(const ucontext_t * uc) {
  97   // gpr1 holds the stack pointer on aix
  98   return (intptr_t*)uc->uc_mcontext.jmp_context.gpr[1/*REG_SP*/];
  99 }
 100 
 101 intptr_t* os::Aix::ucontext_get_fp(const ucontext_t * uc) {
 102   return NULL;
 103 }
 104 
 105 void os::Aix::ucontext_set_pc(ucontext_t* uc, address new_pc) {
 106   uc->uc_mcontext.jmp_context.iar = (uint64_t) new_pc;
 107 }
 108 
 109 static address ucontext_get_lr(const ucontext_t * uc) {
 110   return (address)uc->uc_mcontext.jmp_context.lr;
 111 }
 112 
 113 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 114                                         intptr_t** ret_sp, intptr_t** ret_fp) {
 115 
 116   ExtendedPC  epc;
 117   const ucontext_t* uc = (const ucontext_t*)ucVoid;
 118 
 119   if (uc != NULL) {
 120     epc = ExtendedPC(os::Aix::ucontext_get_pc(uc));
 121     if (ret_sp) *ret_sp = os::Aix::ucontext_get_sp(uc);
 122     if (ret_fp) *ret_fp = os::Aix::ucontext_get_fp(uc);
 123   } else {
 124     // construct empty ExtendedPC for return value checking
 125     epc = ExtendedPC(NULL);
 126     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 127     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 128   }
 129 
 130   return epc;
 131 }
 132 
 133 frame os::fetch_frame_from_context(const void* ucVoid) {
 134   intptr_t* sp;
 135   intptr_t* fp;
 136   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 137   // Avoid crash during crash if pc broken.
 138   if (epc.pc()) {
 139     frame fr(sp, epc.pc());
 140     return fr;
 141   }
 142   frame fr(sp);
 143   return fr;
 144 }
 145 
 146 bool os::Aix::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 147   address pc = (address) os::Aix::ucontext_get_pc(uc);
 148   if (Interpreter::contains(pc)) {
 149     // Interpreter performs stack banging after the fixed frame header has
 150     // been generated while the compilers perform it before. To maintain
 151     // semantic consistency between interpreted and compiled frames, the
 152     // method returns the Java sender of the current frame.
 153     *fr = os::fetch_frame_from_context(uc);
 154     if (!fr->is_first_java_frame()) {
 155       assert(fr->safe_for_sender(thread), "Safety check");
 156       *fr = fr->java_sender();
 157     }
 158   } else {
 159     // More complex code with compiled code.
 160     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 161     CodeBlob* cb = CodeCache::find_blob(pc);
 162     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 163       // Not sure where the pc points to, fallback to default
 164       // stack overflow handling. In compiled code, we bang before
 165       // the frame is complete.
 166       return false;
 167     } else {
 168       intptr_t* sp = os::Aix::ucontext_get_sp(uc);
 169       address lr = ucontext_get_lr(uc);
 170       *fr = frame(sp, lr);
 171       if (!fr->is_java_frame()) {
 172         assert(fr->safe_for_sender(thread), "Safety check");
 173         assert(!fr->is_first_frame(), "Safety check");
 174         *fr = fr->java_sender();
 175       }
 176     }
 177   }
 178   assert(fr->is_java_frame(), "Safety check");
 179   return true;
 180 }
 181 
 182 frame os::get_sender_for_C_frame(frame* fr) {
 183   if (*fr->sp() == NULL) {
 184     // fr is the last C frame
 185     return frame(NULL, NULL);
 186   }
 187   return frame(fr->sender_sp(), fr->sender_pc());
 188 }
 189 
 190 
 191 frame os::current_frame() {
 192   intptr_t* csp = (intptr_t*) *((intptr_t*) os::current_stack_pointer());
 193   // hack.
 194   frame topframe(csp, (address)0x8);
 195   // Return sender of sender of current topframe which hopefully
 196   // both have pc != NULL.
 197   frame tmp = os::get_sender_for_C_frame(&topframe);
 198   return os::get_sender_for_C_frame(&tmp);
 199 }
 200 
 201 // Utility functions
 202 
 203 extern "C" JNIEXPORT int
 204 JVM_handle_aix_signal(int sig, siginfo_t* info, void* ucVoid, int abort_if_unrecognized) {
 205 
 206   ucontext_t* uc = (ucontext_t*) ucVoid;
 207 
 208   Thread* t = Thread::current_or_null_safe();
 209 
 210   SignalHandlerMark shm(t);
 211 
 212   // Note: it's not uncommon that JNI code uses signal/sigset to install
 213   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 214   // or have a SIGILL handler when detecting CPU type). When that happens,
 215   // JVM_handle_aix_signal() might be invoked with junk info/ucVoid. To
 216   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 217   // that do not require siginfo/ucontext first.
 218 
 219   if (sig == SIGPIPE) {
 220     if (os::Aix::chained_handler(sig, info, ucVoid)) {
 221       return 1;
 222     } else {
 223       // Ignoring SIGPIPE - see bugs 4229104
 224       return 1;
 225     }
 226   }
 227 
 228   JavaThread* thread = NULL;
 229   VMThread* vmthread = NULL;
 230   if (os::Aix::signal_handlers_are_installed) {
 231     if (t != NULL) {
 232       if(t->is_Java_thread()) {
 233         thread = (JavaThread*)t;
 234       }
 235       else if(t->is_VM_thread()) {
 236         vmthread = (VMThread *)t;
 237       }
 238     }
 239   }
 240 
 241   // Decide if this trap can be handled by a stub.
 242   address stub = NULL;
 243 
 244   // retrieve program counter
 245   address const pc = uc ? os::Aix::ucontext_get_pc(uc) : NULL;
 246 
 247   // retrieve crash address
 248   address const addr = info ? (const address) info->si_addr : NULL;
 249 
 250   // SafeFetch 32 handling:
 251   // - make it work if _thread is null
 252   // - make it use the standard os::...::ucontext_get/set_pc APIs
 253   if (uc) {
 254     address const pc = os::Aix::ucontext_get_pc(uc);
 255     if (pc && StubRoutines::is_safefetch_fault(pc)) {
 256       os::Aix::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 257       return true;
 258     }
 259   }
 260 
 261   if (info == NULL || uc == NULL || thread == NULL && vmthread == NULL) {
 262     goto run_chained_handler;
 263   }
 264 
 265   // If we are a java thread...
 266   if (thread != NULL) {
 267 
 268     // Handle ALL stack overflow variations here
 269     if (sig == SIGSEGV && thread->is_in_full_stack(addr)) {
 270       // stack overflow
 271       //
 272       // If we are in a yellow zone and we are inside java, we disable the yellow zone and
 273       // throw a stack overflow exception.
 274       // If we are in native code or VM C code, we report-and-die. The original coding tried
 275       // to continue with yellow zone disabled, but that doesn't buy us much and prevents
 276       // hs_err_pid files.
 277       if (thread->in_stack_yellow_reserved_zone(addr)) {
 278         if (thread->thread_state() == _thread_in_Java) {
 279             if (thread->in_stack_reserved_zone(addr)) {
 280               frame fr;
 281               if (os::Aix::get_frame_at_stack_banging_point(thread, uc, &fr)) {
 282                 assert(fr.is_java_frame(), "Must be a Javac frame");
 283                 frame activation =
 284                   SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
 285                 if (activation.sp() != NULL) {
 286                   thread->disable_stack_reserved_zone();
 287                   if (activation.is_interpreted_frame()) {
 288                     thread->set_reserved_stack_activation((address)activation.fp());
 289                   } else {
 290                     thread->set_reserved_stack_activation((address)activation.unextended_sp());
 291                   }
 292                   return 1;
 293                 }
 294               }
 295             }
 296           // Throw a stack overflow exception.
 297           // Guard pages will be reenabled while unwinding the stack.
 298           thread->disable_stack_yellow_reserved_zone();
 299           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 300           goto run_stub;
 301         } else {
 302           // Thread was in the vm or native code. Return and try to finish.
 303           thread->disable_stack_yellow_reserved_zone();
 304           return 1;
 305         }
 306       } else if (thread->in_stack_red_zone(addr)) {
 307         // Fatal red zone violation. Disable the guard pages and fall through
 308         // to handle_unexpected_exception way down below.
 309         thread->disable_stack_red_zone();
 310         tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 311         goto report_and_die;
 312       } else {
 313         // This means a segv happened inside our stack, but not in
 314         // the guarded zone. I'd like to know when this happens,
 315         tty->print_raw_cr("SIGSEGV happened inside stack but outside yellow and red zone.");
 316         goto report_and_die;
 317       }
 318 
 319     } // end handle SIGSEGV inside stack boundaries
 320 
 321     if (thread->thread_state() == _thread_in_Java) {
 322       // Java thread running in Java code
 323 
 324       // The following signals are used for communicating VM events:
 325       //
 326       // SIGILL: the compiler generates illegal opcodes
 327       //   at places where it wishes to interrupt the VM:
 328       //   Safepoints, Unreachable Code, Entry points of Zombie methods,
 329       //    This results in a SIGILL with (*pc) == inserted illegal instruction.
 330       //
 331       //   (so, SIGILLs with a pc inside the zero page are real errors)
 332       //
 333       // SIGTRAP:
 334       //   The ppc trap instruction raises a SIGTRAP and is very efficient if it
 335       //   does not trap. It is used for conditional branches that are expected
 336       //   to be never taken. These are:
 337       //     - zombie methods
 338       //     - IC (inline cache) misses.
 339       //     - null checks leading to UncommonTraps.
 340       //     - range checks leading to Uncommon Traps.
 341       //   On Aix, these are especially null checks, as the ImplicitNullCheck
 342       //   optimization works only in rare cases, as the page at address 0 is only
 343       //   write protected.      //
 344       //   Note: !UseSIGTRAP is used to prevent SIGTRAPS altogether, to facilitate debugging.
 345       //
 346       // SIGSEGV:
 347       //   used for safe point polling:
 348       //     To notify all threads that they have to reach a safe point, safe point polling is used:
 349       //     All threads poll a certain mapped memory page. Normally, this page has read access.
 350       //     If the VM wants to inform the threads about impending safe points, it puts this
 351       //     page to read only ("poisens" the page), and the threads then reach a safe point.
 352       //   used for null checks:
 353       //     If the compiler finds a store it uses it for a null check. Unfortunately this
 354       //     happens rarely.  In heap based and disjoint base compressd oop modes also loads
 355       //     are used for null checks.
 356 
 357       // A VM-related SIGILL may only occur if we are not in the zero page.
 358       // On AIX, we get a SIGILL if we jump to 0x0 or to somewhere else
 359       // in the zero page, because it is filled with 0x0. We ignore
 360       // explicit SIGILLs in the zero page.
 361       if (sig == SIGILL && (pc < (address) 0x200)) {
 362         if (TraceTraps) {
 363           tty->print_raw_cr("SIGILL happened inside zero page.");
 364         }
 365         goto report_and_die;
 366       }
 367 
 368       int stop_type = -1;
 369       // Handle signal from NativeJump::patch_verified_entry().
 370       if (sig == SIGILL && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
 371         if (TraceTraps) {
 372           tty->print_cr("trap: zombie_not_entrant");
 373         }
 374         stub = SharedRuntime::get_handle_wrong_method_stub();
 375         goto run_stub;
 376       }
 377 
 378       else if (USE_POLL_BIT_ONLY
 379                ? (sig == SIGTRAP && ((NativeInstruction*)pc)->is_safepoint_poll())
 380                : (sig == SIGSEGV && SafepointMechanism::is_poll_address(addr))) {
 381         if (TraceTraps) {
 382           tty->print_cr("trap: safepoint_poll at " INTPTR_FORMAT " (%s)", p2i(pc),
 383                         USE_POLL_BIT_ONLY ? "SIGTRAP" : "SIGSEGV");
 384         }
 385         stub = SharedRuntime::get_poll_stub(pc);
 386         goto run_stub;
 387       }
 388 
 389       // SIGTRAP-based ic miss check in compiled code.
 390       else if (sig == SIGTRAP && TrapBasedICMissChecks &&
 391                nativeInstruction_at(pc)->is_sigtrap_ic_miss_check()) {
 392         if (TraceTraps) {
 393           tty->print_cr("trap: ic_miss_check at " INTPTR_FORMAT " (SIGTRAP)", pc);
 394         }
 395         stub = SharedRuntime::get_ic_miss_stub();
 396         goto run_stub;
 397       }
 398 
 399       // SIGTRAP-based implicit null check in compiled code.
 400       else if (sig == SIGTRAP && TrapBasedNullChecks &&
 401                nativeInstruction_at(pc)->is_sigtrap_null_check()) {
 402         if (TraceTraps) {
 403           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGTRAP)", pc);
 404         }
 405         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 406         goto run_stub;
 407       }
 408 
 409       // SIGSEGV-based implicit null check in compiled code.
 410       else if (sig == SIGSEGV && ImplicitNullChecks &&
 411                CodeCache::contains((void*) pc) &&
 412                MacroAssembler::uses_implicit_null_check(info->si_addr)) {
 413         if (TraceTraps) {
 414           tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", pc);
 415         }
 416         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 417       }
 418 
 419 #ifdef COMPILER2
 420       // SIGTRAP-based implicit range check in compiled code.
 421       else if (sig == SIGTRAP && TrapBasedRangeChecks &&
 422                nativeInstruction_at(pc)->is_sigtrap_range_check()) {
 423         if (TraceTraps) {
 424           tty->print_cr("trap: range_check at " INTPTR_FORMAT " (SIGTRAP)", pc);
 425         }
 426         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 427         goto run_stub;
 428       }
 429 #endif
 430 
 431       else if (sig == SIGFPE /* && info->si_code == FPE_INTDIV */) {
 432         if (TraceTraps) {
 433           tty->print_raw_cr("Fix SIGFPE handler, trying divide by zero handler.");
 434         }
 435         stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO);
 436         goto run_stub;
 437       }
 438 
 439       // stop on request
 440       else if (sig == SIGTRAP && (stop_type = nativeInstruction_at(pc)->get_stop_type()) != -1) {
 441         const char *msg = NULL,
 442                    *detail_msg = (const char*)(uc->uc_mcontext.jmp_context.gpr[0]);
 443         switch (stop_type) {
 444           case MacroAssembler::stop_stop              :  msg = "stop"; break;
 445           case MacroAssembler::stop_untested          :  msg = "untested"; break;
 446           case MacroAssembler::stop_unimplemented     :  msg = "unimplemented"; break;
 447           case MacroAssembler::stop_shouldnotreachhere:  msg = "shouldnotreachhere"; detail_msg = NULL; break;
 448           default: msg = "unknown"; break;
 449         }
 450         if (detail_msg == NULL) {
 451           detail_msg = "no details provided";
 452         }
 453 
 454         if (TraceTraps) {
 455           tty->print_cr("trap: %s: %s (SIGTRAP, stop type %d)", msg, detail_msg, stop_type);
 456         }
 457 
 458         va_list detail_args;
 459         VMError::report_and_die(t, ucVoid, NULL, 0, msg, detail_msg, detail_args);
 460         va_end(detail_args);
 461       }
 462 
 463       else if (sig == SIGBUS) {
 464         // BugId 4454115: A read from a MappedByteBuffer can fault here if the
 465         // underlying file has been truncated. Do not crash the VM in such a case.
 466         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 467         CompiledMethod* nm = cb->as_compiled_method_or_null();
 468         bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc));
 469         if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) {
 470           address next_pc = pc + 4;
 471           if (is_unsafe_arraycopy) {
 472             next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
 473           }
 474           next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
 475           os::Aix::ucontext_set_pc(uc, next_pc);
 476           return 1;
 477         }
 478       }
 479     }
 480 
 481     else { // thread->thread_state() != _thread_in_Java
 482       // Detect CPU features. This is only done at the very start of the VM. Later, the
 483       // VM_Version::is_determine_features_test_running() flag should be false.
 484 
 485       if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
 486         // SIGILL must be caused by VM_Version::determine_features().
 487         *(int *)pc = 0; // patch instruction to 0 to indicate that it causes a SIGILL,
 488                         // flushing of icache is not necessary.
 489         stub = pc + 4;  // continue with next instruction.
 490         goto run_stub;
 491       }
 492       else if ((thread->thread_state() == _thread_in_vm ||
 493                 thread->thread_state() == _thread_in_native) &&
 494                sig == SIGBUS && thread->doing_unsafe_access()) {
 495         address next_pc = pc + 4;
 496         if (UnsafeCopyMemory::contains_pc(pc)) {
 497           next_pc = UnsafeCopyMemory::page_error_continue_pc(pc);
 498         }
 499         next_pc = SharedRuntime::handle_unsafe_access(thread, next_pc);
 500         os::Aix::ucontext_set_pc(uc, next_pc);
 501         return 1;
 502       }
 503     }
 504 
 505     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 506     // and the heap gets shrunk before the field access.
 507     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 508       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 509       if (addr != (address)-1) {
 510         stub = addr;
 511       }
 512     }
 513   }
 514 
 515 run_stub:
 516 
 517   // One of the above code blocks ininitalized the stub, so we want to
 518   // delegate control to that stub.
 519   if (stub != NULL) {
 520     // Save all thread context in case we need to restore it.
 521     if (thread != NULL) thread->set_saved_exception_pc(pc);
 522     os::Aix::ucontext_set_pc(uc, stub);
 523     return 1;
 524   }
 525 
 526 run_chained_handler:
 527 
 528   // signal-chaining
 529   if (os::Aix::chained_handler(sig, info, ucVoid)) {
 530     return 1;
 531   }
 532   if (!abort_if_unrecognized) {
 533     // caller wants another chance, so give it to him
 534     return 0;
 535   }
 536 
 537 report_and_die:
 538 
 539   // Use sigthreadmask instead of sigprocmask on AIX and unmask current signal.
 540   sigset_t newset;
 541   sigemptyset(&newset);
 542   sigaddset(&newset, sig);
 543   sigthreadmask(SIG_UNBLOCK, &newset, NULL);
 544 
 545   VMError::report_and_die(t, sig, pc, info, ucVoid);
 546 
 547   ShouldNotReachHere();
 548   return 0;
 549 }
 550 
 551 void os::Aix::init_thread_fpu_state(void) {
 552 #if !defined(USE_XLC_BUILTINS)
 553   // Disable FP exceptions.
 554   __asm__ __volatile__ ("mtfsfi 6,0");
 555 #else
 556   __mtfsfi(6, 0);
 557 #endif
 558 }
 559 
 560 ////////////////////////////////////////////////////////////////////////////////
 561 // thread stack
 562 
 563 // Minimum usable stack sizes required to get to user code. Space for
 564 // HotSpot guard pages is added later.
 565 size_t os::Posix::_compiler_thread_min_stack_allowed = 192 * K;
 566 size_t os::Posix::_java_thread_min_stack_allowed = 64 * K;
 567 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 64 * K;
 568 
 569 // Return default stack size for thr_type.
 570 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 571   // Default stack size (compiler thread needs larger stack).
 572   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 573   return s;
 574 }
 575 
 576 /////////////////////////////////////////////////////////////////////////////
 577 // helper functions for fatal error handler
 578 
 579 void os::print_context(outputStream *st, const void *context) {
 580   if (context == NULL) return;
 581 
 582   const ucontext_t* uc = (const ucontext_t*)context;
 583 
 584   st->print_cr("Registers:");
 585   st->print("pc =" INTPTR_FORMAT "  ", uc->uc_mcontext.jmp_context.iar);
 586   st->print("lr =" INTPTR_FORMAT "  ", uc->uc_mcontext.jmp_context.lr);
 587   st->print("ctr=" INTPTR_FORMAT "  ", uc->uc_mcontext.jmp_context.ctr);
 588   st->cr();
 589   for (int i = 0; i < 32; i++) {
 590     st->print("r%-2d=" INTPTR_FORMAT "  ", i, uc->uc_mcontext.jmp_context.gpr[i]);
 591     if (i % 3 == 2) st->cr();
 592   }
 593   st->cr();
 594   st->cr();
 595 
 596   intptr_t *sp = (intptr_t *)os::Aix::ucontext_get_sp(uc);
 597   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", sp);
 598   print_hex_dump(st, (address)sp, (address)(sp + 128), sizeof(intptr_t));
 599   st->cr();
 600 
 601   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 602   // point to garbage if entry point in an nmethod is corrupted. Leave
 603   // this at the end, and hope for the best.
 604   address pc = os::Aix::ucontext_get_pc(uc);
 605   print_instructions(st, pc, /*instrsize=*/4);
 606   st->cr();
 607 
 608   // Try to decode the instructions.
 609   st->print_cr("Decoded instructions: (pc=" PTR_FORMAT ")", pc);
 610   st->print("<TODO: PPC port - print_context>");
 611   // TODO: PPC port Disassembler::decode(pc, 16, 16, st);
 612   st->cr();
 613 }
 614 
 615 void os::print_register_info(outputStream *st, const void *context) {
 616   if (context == NULL) return;
 617 
 618   ucontext_t *uc = (ucontext_t*)context;
 619 
 620   st->print_cr("Register to memory mapping:");
 621   st->cr();
 622 
 623   st->print("pc ="); print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.iar);
 624   st->print("lr ="); print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.lr);
 625   st->print("sp ="); print_location(st, (intptr_t)os::Aix::ucontext_get_sp(uc));
 626   for (int i = 0; i < 32; i++) {
 627     st->print("r%-2d=", i);
 628     print_location(st, (intptr_t)uc->uc_mcontext.jmp_context.gpr[i]);
 629   }
 630 
 631   st->cr();
 632 }
 633 
 634 extern "C" {
 635   int SpinPause() {
 636     return 0;
 637   }
 638 }
 639 
 640 #ifndef PRODUCT
 641 void os::verify_stack_alignment() {
 642   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 643 }
 644 #endif
 645 
 646 int os::extra_bang_size_in_bytes() {
 647   // PPC does not require the additional stack bang.
 648   return 0;
 649 }
 650 
 651 bool os::platform_print_native_stack(outputStream* st, void* context, char *buf, int buf_size) {
 652   AixNativeCallstack::print_callstack_for_context(st, (const ucontext_t*)context, true, buf, (size_t) buf_size);
 653   return true;
 654 }