1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  *
  26  */
  27 
  28 // No precompiled headers
  29 #include "jvm.h"
  30 #include "asm/macroAssembler.hpp"
  31 #include "classfile/classLoader.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/icBuffer.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "memory/allocation.inline.hpp"
  38 #include "nativeInst_aarch32.hpp"
  39 #include "os_share_linux.hpp"
  40 #include "prims/jniFastGetField.hpp"
  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.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/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 # include <fpu_control.h>
  79 
  80 #define SPELL_REG_SP "sp"
  81 #define SPELL_REG_FP "fp"
  82 
  83 extern "C" {
  84   void *linux_aarch32_current_frame_pointer();
  85   void *linux_aarch32_previous_frame_pointer();
  86 }
  87 
  88 address os::current_stack_pointer() {
  89   return (address) linux_aarch32_current_frame_pointer();
  90 }
  91 
  92 char* os::non_memory_address_word() {
  93   // Must never look like an address returned by reserve_memory,
  94   // even in its subfields (as defined by the CPU immediate fields,
  95   // if the CPU splits constants across multiple instructions).
  96 
  97   return (char*) 0xfffffffful;
  98 }
  99 
 100 void os::initialize_thread(Thread *thr) {
 101 }
 102 
 103 address os::Linux::ucontext_get_pc(const ucontext_t * uc) {
 104   return (address)uc->uc_mcontext.arm_pc;
 105 }
 106 
 107 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) {
 108   uc->uc_mcontext.arm_pc = (intptr_t)pc;
 109 }
 110 
 111 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
 112   return (intptr_t*)uc->uc_mcontext.arm_sp;
 113 }
 114 
 115 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
 116   return (intptr_t*)uc->uc_mcontext.arm_fp;
 117 }
 118 
 119 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
 120 // is currently interrupted by SIGPROF.
 121 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal
 122 // frames. Currently we don't do that on Linux, so it's the same as
 123 // os::fetch_frame_from_context().
 124 ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread,
 125   const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
 126 
 127   assert(thread != NULL, "just checking");
 128   assert(ret_sp != NULL, "just checking");
 129   assert(ret_fp != NULL, "just checking");
 130 
 131   return os::fetch_frame_from_context(uc, ret_sp, ret_fp);
 132 }
 133 
 134 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 135                     intptr_t** ret_sp, intptr_t** ret_fp) {
 136 
 137   ExtendedPC  epc;
 138   ucontext_t* uc = (ucontext_t*)ucVoid;
 139 
 140   if (uc != NULL) {
 141     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
 142     if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
 143     if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
 144   } else {
 145     // construct empty ExtendedPC for return value checking
 146     epc = ExtendedPC(NULL);
 147     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 148     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 149   }
 150 
 151   return epc;
 152 }
 153 
 154 frame os::fetch_frame_from_context(const void* ucVoid) {
 155   intptr_t* sp;
 156   intptr_t* fp;
 157   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 158   return frame(sp, fp, epc.pc());
 159 }
 160 
 161 bool os::Linux::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 162   address pc = (address) os::Linux::ucontext_get_pc(uc);
 163   if (Interpreter::contains(pc)) {
 164     // interpreter performs stack banging after the fixed frame header has
 165     // been generated while the compilers perform it before. To maintain
 166     // semantic consistency between interpreted and compiled frames, the
 167     // method returns the Java sender of the current frame.
 168     *fr = os::fetch_frame_from_context(uc);
 169     if (!fr->is_first_java_frame()) {
 170       assert(fr->safe_for_sender(thread), "Safety check");
 171       *fr = fr->java_sender();
 172     }
 173   } else {
 174     // more complex code with compiled code
 175     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 176     CodeBlob* cb = CodeCache::find_blob(pc);
 177     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 178       // Not sure where the pc points to, fallback to default
 179       // stack overflow handling
 180       return false;
 181     } else {
 182       // In compiled code, the stack banging is performed before LR
 183       // has been saved in the frame.  LR is live, and SP and FP
 184       // belong to the caller.
 185       intptr_t* fp = os::Linux::ucontext_get_fp(uc);
 186       intptr_t* sp = os::Linux::ucontext_get_sp(uc);
 187       //TODO: XXX: Merge
 188       // could be pc = os::Linux::ucontext_get_pc(uc) ?
 189       address pc = (address)(uc->uc_mcontext.arm_lr
 190                          - NativeInstruction::arm_insn_sz);
 191       *fr = frame(sp, fp, pc);
 192       if (!fr->is_java_frame()) {
 193         assert(fr->safe_for_sender(thread), "Safety check");
 194         assert(!fr->is_first_frame(), "Safety check");
 195         *fr = fr->java_sender();
 196       }
 197     }
 198   }
 199   assert(fr->is_java_frame(), "Safety check");
 200   return true;
 201 }
 202 
 203 // By default, gcc always saves frame pointer rfp on this stack. This
 204 // may get turned off by -fomit-frame-pointer.
 205 frame os::get_sender_for_C_frame(frame* fr) {
 206 #ifdef __thumb__
 207   return frame();
 208 #else
 209   address sender_pc = *(address*) fr->addr_at(fr->get_return_addr_offset(JNIFrameAPCS));
 210   intptr_t* link = *(intptr_t**) fr->addr_at(fr->get_link_offset(JNIFrameAPCS));
 211   return frame(fr->sender_sp(), link, sender_pc);
 212 #endif
 213 }
 214 
 215 frame os::current_frame() {
 216 #ifdef __thumb__
 217   return frame();
 218 #else
 219   intptr_t* fp = (intptr_t*)linux_aarch32_previous_frame_pointer();
 220   frame myframe((intptr_t*)os::current_stack_pointer(),
 221                 (intptr_t*)fp,
 222                 CAST_FROM_FN_PTR(address, os::current_frame));
 223   // check for C2 frame first, those to not have valid FP
 224   if (!CodeCache::find_blob(
 225           *(address*)myframe.addr_at(myframe.get_return_addr_offset(JNIFrameAPCS))) &&
 226           os::is_first_C_frame(&myframe)) {
 227     // stack is not walkable
 228     return frame();
 229   } else {
 230     return os::get_sender_for_C_frame(&myframe);
 231   }
 232 #endif
 233 }
 234 
 235 // Utility functions
 236 
 237 // From IA32 System Programming Guide
 238 enum {
 239   trap_page_fault = 0xE
 240 };
 241 
 242 // An operation in Unsafe has faulted.  We're going to return to the
 243 // instruction after the faulting load or store.  We also set
 244 // pending_unsafe_access_error so that at some point in the future our
 245 // user will get a helpful message.
 246 static address handle_unsafe_access(JavaThread* thread, address pc) {
 247   // pc is the instruction which we must emulate
 248   // doing a no-op is fine:  return garbage from the load
 249   // therefore, compute npc
 250   address npc = pc + NativeInstruction::arm_insn_sz;
 251 
 252   // request an async exception
 253   thread->set_pending_unsafe_access_error();
 254 
 255   // return address of next instruction to execute
 256   return npc;
 257 }
 258 
 259 extern "C" JNIEXPORT int
 260 JVM_handle_linux_signal(int sig,
 261                         siginfo_t* info,
 262                         void* ucVoid,
 263                         int abort_if_unrecognized) {
 264   ucontext_t* uc = (ucontext_t*) ucVoid;
 265 
 266   Thread* t = Thread::current_or_null_safe();
 267 
 268   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 269   // (no destructors can be run)
 270   os::ThreadCrashProtection::check_crash_protection(sig, t);
 271 
 272   SignalHandlerMark shm(t);
 273 
 274   // Note: it's not uncommon that JNI code uses signal/sigset to install
 275   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 276   // or have a SIGILL handler when detecting CPU type). When that happens,
 277   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 278   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 279   // that do not require siginfo/ucontext first.
 280 
 281   if (sig == SIGPIPE || sig == SIGXFSZ) {
 282     // allow chained handler to go first
 283     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 284       return true;
 285     } else {
 286       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 287         char buf[64];
 288         warning("Ignoring %s - see bugs 4229104 or 646499219",
 289                 os::exception_name(sig, buf, sizeof(buf)));
 290       }
 291       return true;
 292     }
 293   }
 294 
 295 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
 296   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
 297     handle_assert_poison_fault(ucVoid, info->si_addr);
 298     return 1;
 299   }
 300 #endif
 301 
 302   JavaThread* thread = NULL;
 303   VMThread* vmthread = NULL;
 304   if (os::Linux::signal_handlers_are_installed) {
 305     if (t != NULL ){
 306       if(t->is_Java_thread()) {
 307         thread = (JavaThread*)t;
 308       }
 309       else if(t->is_VM_thread()){
 310         vmthread = (VMThread *)t;
 311       }
 312     }
 313   }
 314 /*
 315   NOTE: does not seem to work on linux.
 316   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 317     // can't decode this kind of signal
 318     info = NULL;
 319   } else {
 320     assert(sig == info->si_signo, "bad siginfo");
 321   }
 322 */
 323   // decide if this trap can be handled by a stub
 324   address stub = NULL;
 325 
 326   address pc          = NULL;
 327 
 328   //%note os_trap_1
 329   if (info != NULL && uc != NULL && thread != NULL) {
 330     pc = (address) os::Linux::ucontext_get_pc(uc);
 331 
 332     if (StubRoutines::is_safefetch_fault(pc)) {
 333       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 334       return 1;
 335     }
 336 
 337     // Handle ALL stack overflow variations here
 338     if (sig == SIGSEGV) {
 339       address addr = (address) info->si_addr;
 340 
 341       // check if fault address is within thread stack
 342       if (thread->on_local_stack(addr)) {
 343         // stack overflow
 344         if (thread->in_stack_yellow_reserved_zone(addr)) {
 345           thread->disable_stack_yellow_reserved_zone();
 346           if (thread->thread_state() == _thread_in_Java) {
 347             if (thread->in_stack_reserved_zone(addr)) {
 348               frame fr;
 349               if (os::Linux::get_frame_at_stack_banging_point(thread, uc, &fr)) {
 350                 assert(fr.is_java_frame(), "Must be a Java frame");
 351                 frame activation =
 352                   SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
 353                 if (activation.sp() != NULL) {
 354                   thread->disable_stack_reserved_zone();
 355                   if (activation.is_interpreted_frame()) {
 356                     thread->set_reserved_stack_activation((address)(
 357                       activation.fp() + frame::interpreter_frame_initial_sp_offset));
 358                   } else {
 359                     thread->set_reserved_stack_activation((address)activation.unextended_sp());
 360                   }
 361                   return 1;
 362                 }
 363               }
 364             }
 365             // Throw a stack overflow exception.  Guard pages will be reenabled
 366             // while unwinding the stack.
 367             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 368           } else {
 369             // Thread was in the vm or native code.  Return and try to finish.
 370             return 1;
 371           }
 372         } else if (thread->in_stack_red_zone(addr)) {
 373           // Fatal red zone violation.  Disable the guard pages and fall through
 374           // to handle_unexpected_exception way down below.
 375           thread->disable_stack_red_zone();
 376           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 377 
 378           // This is a likely cause, but hard to verify. Let's just print
 379           // it as a hint.
 380           tty->print_raw_cr("Please check if any of your loaded .so files has "
 381                             "enabled executable stack (see man page execstack(8))");
 382         } else {
 383           // Accessing stack address below sp may cause SEGV if current
 384           // thread has MAP_GROWSDOWN stack. This should only happen when
 385           // current thread was created by user code with MAP_GROWSDOWN flag
 386           // and then attached to VM. See notes in os_linux.cpp.
 387           if (thread->osthread()->expanding_stack() == 0) {
 388              thread->osthread()->set_expanding_stack();
 389              if (os::Linux::manually_expand_stack(thread, addr)) {
 390                thread->osthread()->clear_expanding_stack();
 391                return 1;
 392              }
 393              thread->osthread()->clear_expanding_stack();
 394           } else {
 395              fatal("recursive segv. expanding stack.");
 396           }
 397         }
 398       }
 399     }
 400 
 401     if (thread->thread_state() == _thread_in_Java) {
 402       // Java thread running in Java code => find exception handler if any
 403       // a fault inside compiled code, the interpreter, or a stub
 404 
 405       // Handle signal from NativeJump::patch_verified_entry().
 406       if ((sig == SIGILL || sig == SIGTRAP)
 407           && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
 408         if (TraceTraps) {
 409           tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL");
 410         }
 411         stub = SharedRuntime::get_handle_wrong_method_stub();
 412       } else if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
 413         stub = SharedRuntime::get_poll_stub(pc);
 414       } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
 415         // BugId 4454115: A read from a MappedByteBuffer can fault
 416         // here if the underlying file has been truncated.
 417         // Do not crash the VM in such a case.
 418         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 419         CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
 420         if (nm != NULL && nm->has_unsafe_access()) {
 421           stub = handle_unsafe_access(thread, pc);
 422         }
 423       }
 424       else
 425 
 426       if (sig == SIGFPE  &&
 427           (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {
 428         stub =
 429           SharedRuntime::
 430           continuation_for_implicit_exception(thread,
 431                                               pc,
 432                                               SharedRuntime::
 433                                               IMPLICIT_DIVIDE_BY_ZERO);
 434       } else if (sig == SIGSEGV &&
 435                !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
 436           // Determination of interpreter/vtable stub/compiled code null exception
 437           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 438       }
 439     } else if (sig == SIGILL && VM_Version::is_determine_features_test_running()) {
 440         // SIGILL must be caused by VM_Version::get_processor_features().
 441         *(int *)pc = Assembler::nop_insn; // patch instruction to NOP to indicate that it causes a SIGILL,
 442                         // flushing of icache is not necessary.
 443         stub = pc + 4;  // continue with next instruction.
 444     } else if (thread->thread_state() == _thread_in_vm &&
 445                sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
 446                thread->doing_unsafe_access()) {
 447         stub = handle_unsafe_access(thread, pc);
 448     }
 449 
 450     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 451     // and the heap gets shrunk before the field access.
 452     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 453       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 454       if (addr != (address)-1) {
 455         stub = addr;
 456       }
 457     }
 458 
 459     // Check to see if we caught the safepoint code in the
 460     // process of write protecting the memory serialization page.
 461     // It write enables the page immediately after protecting it
 462     // so we can just return to retry the write.
 463     if ((sig == SIGSEGV) &&
 464         os::is_memory_serialize_page(thread, (address) info->si_addr)) {
 465       // Block current thread until the memory serialize page permission restored.
 466       os::block_on_serialize_page_trap();
 467       return true;
 468     }
 469   }
 470 
 471   if (stub != NULL) {
 472     // save all thread context in case we need to restore it
 473     if (thread != NULL) thread->set_saved_exception_pc(pc);
 474 
 475     os::Linux::ucontext_set_pc(uc, stub);
 476     return true;
 477   }
 478 
 479   // signal-chaining
 480   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 481      return true;
 482   }
 483 
 484   if (!abort_if_unrecognized) {
 485     // caller wants another chance, so give it to him
 486     return false;
 487   }
 488 
 489   if (pc == NULL && uc != NULL) {
 490     pc = os::Linux::ucontext_get_pc(uc);
 491   }
 492 
 493   // unmask current signal
 494   sigset_t newset;
 495   sigemptyset(&newset);
 496   sigaddset(&newset, sig);
 497   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 498 
 499   VMError::report_and_die(t, sig, pc, info, ucVoid);
 500 
 501   ShouldNotReachHere();
 502   return true; // Mute compiler
 503 }
 504 
 505 void os::Linux::init_thread_fpu_state(void) {
 506 }
 507 
 508 int os::Linux::get_fpu_control_word(void) {
 509   return 0;
 510 }
 511 
 512 void os::Linux::set_fpu_control_word(int fpu_control) {
 513 }
 514 
 515 // Check that the linux kernel version is 2.4 or higher since earlier
 516 // versions do not support SSE without patches.
 517 bool os::supports_sse() {
 518   return true;
 519 }
 520 
 521 bool os::is_allocatable(size_t bytes) {
 522   return true;
 523 }
 524 
 525 ////////////////////////////////////////////////////////////////////////////////
 526 // thread stack
 527 
 528 // Minimum usable stack sizes required to get to user code. Space for
 529 // HotSpot guard pages is added later.
 530 size_t os::Posix::_compiler_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;
 531 size_t os::Posix::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;
 532 size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;
 533 
 534 // return default stack size for thr_type
 535 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 536   // default stack size (compiler thread needs larger stack)
 537   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
 538   return s;
 539 }
 540 
 541 // Java thread:
 542 //
 543 //   Low memory addresses
 544 //    +------------------------+
 545 //    |                        |\  JavaThread created by VM does not have glibc
 546 //    |    glibc guard page    | - guard, attached Java thread usually has
 547 //    |                        |/  1 page glibc guard.
 548 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 549 //    |                        |\
 550 //    |  HotSpot Guard Pages   | - red and yellow pages
 551 //    |                        |/
 552 //    +------------------------+ JavaThread::stack_yellow_zone_base()
 553 //    |                        |\
 554 //    |      Normal Stack      | -
 555 //    |                        |/
 556 // P2 +------------------------+ Thread::stack_base()
 557 //
 558 // Non-Java thread:
 559 //
 560 //   Low memory addresses
 561 //    +------------------------+
 562 //    |                        |\
 563 //    |  glibc guard page      | - usually 1 page
 564 //    |                        |/
 565 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 566 //    |                        |\
 567 //    |      Normal Stack      | -
 568 //    |                        |/
 569 // P2 +------------------------+ Thread::stack_base()
 570 //
 571 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
 572 //    pthread_attr_getstack()
 573 
 574 /////////////////////////////////////////////////////////////////////////////
 575 // helper functions for fatal error handler
 576 
 577 
 578 void os::print_context(outputStream *st, const void *context) {
 579   if (context == NULL) return;
 580 
 581   ucontext_t *uc = (ucontext_t*)context;
 582   st->print_cr("Registers:");
 583 
 584   for (int r = 0; r < 16; r++)
 585     st->print_cr(  "R%d=" INTPTR_FORMAT, r,  *((unsigned int*)&uc->uc_mcontext.arm_r0 + r) );
 586 
 587   st->cr();
 588 
 589   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 590   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 591   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 592   st->cr();
 593 
 594   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 595   // point to garbage if entry point in an nmethod is corrupted. Leave
 596   // this at the end, and hope for the best.
 597   address pc = os::Linux::ucontext_get_pc(uc);
 598   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
 599   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 600 }
 601 
 602 void os::print_register_info(outputStream *st, const void *context) {
 603   if (context == NULL) return;
 604 
 605   ucontext_t *uc = (ucontext_t*)context;
 606 
 607   st->print_cr("Register to memory mapping:");
 608   st->cr();
 609 
 610   for (int r = 0; r < 16; r++) {
 611     st->print(  "R%d=", r); print_location(st, *((unsigned int*)&uc->uc_mcontext.arm_r0 + r));
 612   }
 613   st->cr();
 614 }
 615 
 616 void os::setup_fpu() {
 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   // AArch64 does not require the additional stack bang.
 627   // does AArch32?
 628   return 0;
 629 }
 630 
 631 extern "C" {
 632   int SpinPause() {
 633     return 0;
 634   }
 635 
 636   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
 637     if (from > to) {
 638       jshort *end = from + count;
 639       while (from < end)
 640         *(to++) = *(from++);
 641     }
 642     else if (from < to) {
 643       jshort *end = from;
 644       from += count - 1;
 645       to   += count - 1;
 646       while (from >= end)
 647         *(to--) = *(from--);
 648     }
 649   }
 650   void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
 651     if (from > to) {
 652       jint *end = from + count;
 653       while (from < end)
 654         *(to++) = *(from++);
 655     }
 656     else if (from < to) {
 657       jint *end = from;
 658       from += count - 1;
 659       to   += count - 1;
 660       while (from >= end)
 661         *(to--) = *(from--);
 662     }
 663   }
 664   void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
 665     if (from > to) {
 666       jlong *end = from + count;
 667       while (from < end)
 668         os::atomic_copy64(from++, to++);
 669     }
 670     else if (from < to) {
 671       jlong *end = from;
 672       from += count - 1;
 673       to   += count - 1;
 674       while (from >= end)
 675         os::atomic_copy64(from--, to--);
 676     }
 677   }
 678 
 679   void _Copy_arrayof_conjoint_bytes(HeapWord* from,
 680                                     HeapWord* to,
 681                                     size_t    count) {
 682     memmove(to, from, count);
 683   }
 684   void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
 685                                       HeapWord* to,
 686                                       size_t    count) {
 687     memmove(to, from, count * 2);
 688   }
 689   void _Copy_arrayof_conjoint_jints(HeapWord* from,
 690                                     HeapWord* to,
 691                                     size_t    count) {
 692     memmove(to, from, count * 4);
 693   }
 694   void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
 695                                      HeapWord* to,
 696                                      size_t    count) {
 697     memmove(to, from, count * 8);
 698   }
 699 };