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  * 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/macroAssembler.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 "code/nativeInst.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "memory/allocation.inline.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/sharedRuntime.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/thread.inline.hpp"
  52 #include "runtime/timer.hpp"
  53 #include "utilities/debug.hpp"
  54 #include "utilities/events.hpp"
  55 #include "utilities/vmError.hpp"
  56 #ifdef BUILTIN_SIM
  57 #include "../../../../../../simulator/simulator.hpp"
  58 #endif
  59 
  60 // put OS-includes here
  61 # include <sys/types.h>
  62 # include <sys/mman.h>
  63 # include <pthread.h>
  64 # include <signal.h>
  65 # include <errno.h>
  66 # include <dlfcn.h>
  67 # include <stdlib.h>
  68 # include <stdio.h>
  69 # include <unistd.h>
  70 # include <sys/resource.h>
  71 # include <pthread.h>
  72 # include <sys/stat.h>
  73 # include <sys/time.h>
  74 # include <sys/utsname.h>
  75 # include <sys/socket.h>
  76 # include <sys/wait.h>
  77 # include <pwd.h>
  78 # include <poll.h>
  79 # include <ucontext.h>
  80 # include <fpu_control.h>
  81 
  82 #ifdef BUILTIN_SIM
  83 #define REG_SP REG_RSP
  84 #define REG_PC REG_RIP
  85 #define REG_FP REG_RBP
  86 #else
  87 #define REG_FP 29
  88 #define REG_LR 30
  89 #endif
  90 
  91 NOINLINE address os::current_stack_pointer() {
  92   return (address)__builtin_frame_address(0);
  93 }
  94 
  95 char* os::non_memory_address_word() {
  96   // Must never look like an address returned by reserve_memory,
  97   // even in its subfields (as defined by the CPU immediate fields,
  98   // if the CPU splits constants across multiple instructions).
  99 
 100   return (char*) 0xffffffffffff;
 101 }
 102 
 103 address os::Linux::ucontext_get_pc(const ucontext_t * uc) {
 104 #ifdef BUILTIN_SIM
 105   return (address)uc->uc_mcontext.gregs[REG_PC];
 106 #else
 107   return (address)uc->uc_mcontext.pc;
 108 #endif
 109 }
 110 
 111 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) {
 112 #ifdef BUILTIN_SIM
 113   uc->uc_mcontext.gregs[REG_PC] = (intptr_t)pc;
 114 #else
 115   uc->uc_mcontext.pc = (intptr_t)pc;
 116 #endif
 117 }
 118 
 119 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) {
 120 #ifdef BUILTIN_SIM
 121   return (intptr_t*)uc->uc_mcontext.gregs[REG_SP];
 122 #else
 123   return (intptr_t*)uc->uc_mcontext.sp;
 124 #endif
 125 }
 126 
 127 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) {
 128 #ifdef BUILTIN_SIM
 129   return (intptr_t*)uc->uc_mcontext.gregs[REG_FP];
 130 #else
 131   return (intptr_t*)uc->uc_mcontext.regs[REG_FP];
 132 #endif
 133 }
 134 
 135 // For Forte Analyzer AsyncGetCallTrace profiling support - thread
 136 // is currently interrupted by SIGPROF.
 137 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal
 138 // frames. Currently we don't do that on Linux, so it's the same as
 139 // os::fetch_frame_from_context().
 140 ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread,
 141   const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) {
 142 
 143   assert(thread != NULL, "just checking");
 144   assert(ret_sp != NULL, "just checking");
 145   assert(ret_fp != NULL, "just checking");
 146 
 147   return os::fetch_frame_from_context(uc, ret_sp, ret_fp);
 148 }
 149 
 150 ExtendedPC os::fetch_frame_from_context(const void* ucVoid,
 151                     intptr_t** ret_sp, intptr_t** ret_fp) {
 152 
 153   ExtendedPC  epc;
 154   const ucontext_t* uc = (const ucontext_t*)ucVoid;
 155 
 156   if (uc != NULL) {
 157     epc = ExtendedPC(os::Linux::ucontext_get_pc(uc));
 158     if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc);
 159     if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc);
 160   } else {
 161     // construct empty ExtendedPC for return value checking
 162     epc = ExtendedPC(NULL);
 163     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 164     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 165   }
 166 
 167   return epc;
 168 }
 169 
 170 frame os::fetch_frame_from_context(const void* ucVoid) {
 171   intptr_t* sp;
 172   intptr_t* fp;
 173   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 174   return frame(sp, fp, epc.pc());
 175 }
 176 
 177 bool os::Linux::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) {
 178   address pc = (address) os::Linux::ucontext_get_pc(uc);
 179   if (Interpreter::contains(pc)) {
 180     // interpreter performs stack banging after the fixed frame header has
 181     // been generated while the compilers perform it before. To maintain
 182     // semantic consistency between interpreted and compiled frames, the
 183     // method returns the Java sender of the current frame.
 184     *fr = os::fetch_frame_from_context(uc);
 185     if (!fr->is_first_java_frame()) {
 186       assert(fr->safe_for_sender(thread), "Safety check");
 187       *fr = fr->java_sender();
 188     }
 189   } else {
 190     // more complex code with compiled code
 191     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
 192     CodeBlob* cb = CodeCache::find_blob(pc);
 193     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
 194       // Not sure where the pc points to, fallback to default
 195       // stack overflow handling
 196       return false;
 197     } else {
 198       // In compiled code, the stack banging is performed before LR
 199       // has been saved in the frame.  LR is live, and SP and FP
 200       // belong to the caller.
 201       intptr_t* fp = os::Linux::ucontext_get_fp(uc);
 202       intptr_t* sp = os::Linux::ucontext_get_sp(uc);
 203       address pc = (address)(uc->uc_mcontext.regs[REG_LR]
 204                          - NativeInstruction::instruction_size);
 205       *fr = frame(sp, fp, pc);
 206       if (!fr->is_java_frame()) {
 207         assert(fr->safe_for_sender(thread), "Safety check");
 208         assert(!fr->is_first_frame(), "Safety check");
 209         *fr = fr->java_sender();
 210       }
 211     }
 212   }
 213   assert(fr->is_java_frame(), "Safety check");
 214   return true;
 215 }
 216 
 217 // By default, gcc always saves frame pointer rfp on this stack. This
 218 // may get turned off by -fomit-frame-pointer.
 219 frame os::get_sender_for_C_frame(frame* fr) {
 220 #ifdef BUILTIN_SIM
 221   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
 222 #else
 223   return frame(fr->link(), fr->link(), fr->sender_pc());
 224 #endif
 225 }
 226 
 227 NOINLINE frame os::current_frame() {
 228   intptr_t *fp = *(intptr_t **)__builtin_frame_address(0);
 229   frame myframe((intptr_t*)os::current_stack_pointer(),
 230                 (intptr_t*)fp,
 231                 CAST_FROM_FN_PTR(address, os::current_frame));
 232   if (os::is_first_C_frame(&myframe)) {
 233     // stack is not walkable
 234     return frame();
 235   } else {
 236     return os::get_sender_for_C_frame(&myframe);
 237   }
 238 }
 239 
 240 // Utility functions
 241 #ifdef BUILTIN_SIM
 242 extern "C" void Fetch32PFI () ;
 243 extern "C" void Fetch32Resume () ;
 244 extern "C" void FetchNPFI () ;
 245 extern "C" void FetchNResume () ;
 246 #endif
 247 
 248 extern "C" JNIEXPORT int
 249 JVM_handle_linux_signal(int sig,
 250                         siginfo_t* info,
 251                         void* ucVoid,
 252                         int abort_if_unrecognized) {
 253   ucontext_t* uc = (ucontext_t*) ucVoid;
 254 
 255   Thread* t = Thread::current_or_null_safe();
 256 
 257   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 258   // (no destructors can be run)
 259   os::ThreadCrashProtection::check_crash_protection(sig, t);
 260 
 261   SignalHandlerMark shm(t);
 262 
 263   // Note: it's not uncommon that JNI code uses signal/sigset to install
 264   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 265   // or have a SIGILL handler when detecting CPU type). When that happens,
 266   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 267   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 268   // that do not require siginfo/ucontext first.
 269 
 270   if (sig == SIGPIPE || sig == SIGXFSZ) {
 271     // allow chained handler to go first
 272     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 273       return true;
 274     } else {
 275       // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219
 276       return true;
 277     }
 278   }
 279 
 280 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
 281   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
 282     handle_assert_poison_fault(ucVoid, info->si_addr);
 283     return 1;
 284   }
 285 #endif
 286 
 287   JavaThread* thread = NULL;
 288   VMThread* vmthread = NULL;
 289   if (os::Linux::signal_handlers_are_installed) {
 290     if (t != NULL ){
 291       if(t->is_Java_thread()) {
 292         thread = (JavaThread*)t;
 293       }
 294       else if(t->is_VM_thread()){
 295         vmthread = (VMThread *)t;
 296       }
 297     }
 298   }
 299 /*
 300   NOTE: does not seem to work on linux.
 301   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 302     // can't decode this kind of signal
 303     info = NULL;
 304   } else {
 305     assert(sig == info->si_signo, "bad siginfo");
 306   }
 307 */
 308   // decide if this trap can be handled by a stub
 309   address stub = NULL;
 310 
 311   address pc          = NULL;
 312 
 313   //%note os_trap_1
 314   if (info != NULL && uc != NULL && thread != NULL) {
 315     pc = (address) os::Linux::ucontext_get_pc(uc);
 316 
 317 #ifdef BUILTIN_SIM
 318     if (pc == (address) Fetch32PFI) {
 319        uc->uc_mcontext.gregs[REG_PC] = intptr_t(Fetch32Resume) ;
 320        return 1 ;
 321     }
 322     if (pc == (address) FetchNPFI) {
 323        uc->uc_mcontext.gregs[REG_PC] = intptr_t (FetchNResume) ;
 324        return 1 ;
 325     }
 326 #else
 327     if (StubRoutines::is_safefetch_fault(pc)) {
 328       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 329       return 1;
 330     }
 331 #endif
 332 
 333     address addr = (address) info->si_addr;
 334 
 335     // Make sure the high order byte is sign extended, as it may be masked away by the hardware.
 336     if ((uintptr_t(addr) & (uintptr_t(1) << 55)) != 0) {
 337       addr = address(uintptr_t(addr) | (uintptr_t(0xFF) << 56));
 338     }
 339 
 340     // Handle ALL stack overflow variations here
 341     if (sig == SIGSEGV) {
 342       // check if fault address is within thread stack
 343       if (thread->on_local_stack(addr)) {
 344         // stack overflow
 345         if (thread->in_stack_yellow_reserved_zone(addr)) {
 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             thread->disable_stack_yellow_reserved_zone();
 368             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 369           } else {
 370             // Thread was in the vm or native code.  Return and try to finish.
 371             thread->disable_stack_yellow_reserved_zone();
 372             return 1;
 373           }
 374         } else if (thread->in_stack_red_zone(addr)) {
 375           // Fatal red zone violation.  Disable the guard pages and fall through
 376           // to handle_unexpected_exception way down below.
 377           thread->disable_stack_red_zone();
 378           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 379 
 380           // This is a likely cause, but hard to verify. Let's just print
 381           // it as a hint.
 382           tty->print_raw_cr("Please check if any of your loaded .so files has "
 383                             "enabled executable stack (see man page execstack(8))");
 384         } else {
 385           // Accessing stack address below sp may cause SEGV if current
 386           // thread has MAP_GROWSDOWN stack. This should only happen when
 387           // current thread was created by user code with MAP_GROWSDOWN flag
 388           // and then attached to VM. See notes in os_linux.cpp.
 389           if (thread->osthread()->expanding_stack() == 0) {
 390              thread->osthread()->set_expanding_stack();
 391              if (os::Linux::manually_expand_stack(thread, addr)) {
 392                thread->osthread()->clear_expanding_stack();
 393                return 1;
 394              }
 395              thread->osthread()->clear_expanding_stack();
 396           } else {
 397              fatal("recursive segv. expanding stack.");
 398           }
 399         }
 400       }
 401     }
 402 
 403     if (thread->thread_state() == _thread_in_Java) {
 404       // Java thread running in Java code => find exception handler if any
 405       // a fault inside compiled code, the interpreter, or a stub
 406 
 407       // Handle signal from NativeJump::patch_verified_entry().
 408       if ((sig == SIGILL || sig == SIGTRAP)
 409           && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
 410         if (TraceTraps) {
 411           tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL");
 412         }
 413         stub = SharedRuntime::get_handle_wrong_method_stub();
 414       } else if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
 415         stub = SharedRuntime::get_poll_stub(pc);
 416       } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
 417         // BugId 4454115: A read from a MappedByteBuffer can fault
 418         // here if the underlying file has been truncated.
 419         // Do not crash the VM in such a case.
 420         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 421         CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
 422         if (nm != NULL && nm->has_unsafe_access()) {
 423           address next_pc = pc + NativeCall::instruction_size;
 424           stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
 425         }
 426       }
 427       else
 428 
 429       if (sig == SIGFPE  &&
 430           (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {
 431         stub =
 432           SharedRuntime::
 433           continuation_for_implicit_exception(thread,
 434                                               pc,
 435                                               SharedRuntime::
 436                                               IMPLICIT_DIVIDE_BY_ZERO);
 437       } else if (sig == SIGSEGV &&
 438                  MacroAssembler::uses_implicit_null_check((void*)addr)) {
 439           // Determination of interpreter/vtable stub/compiled code null exception
 440           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 441       }
 442     } else if (thread->thread_state() == _thread_in_vm &&
 443                sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
 444                thread->doing_unsafe_access()) {
 445       address next_pc = pc + NativeCall::instruction_size;
 446       stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
 447     }
 448 
 449     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 450     // and the heap gets shrunk before the field access.
 451     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 452       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 453       if (addr != (address)-1) {
 454         stub = addr;
 455       }
 456     }
 457   }
 458 
 459   if (stub != NULL) {
 460     // save all thread context in case we need to restore it
 461     if (thread != NULL) thread->set_saved_exception_pc(pc);
 462 
 463     os::Linux::ucontext_set_pc(uc, stub);
 464     return true;
 465   }
 466 
 467   // signal-chaining
 468   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 469      return true;
 470   }
 471 
 472   if (!abort_if_unrecognized) {
 473     // caller wants another chance, so give it to him
 474     return false;
 475   }
 476 
 477   if (pc == NULL && uc != NULL) {
 478     pc = os::Linux::ucontext_get_pc(uc);
 479   }
 480 
 481   // unmask current signal
 482   sigset_t newset;
 483   sigemptyset(&newset);
 484   sigaddset(&newset, sig);
 485   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 486 
 487   VMError::report_and_die(t, sig, pc, info, ucVoid);
 488 
 489   ShouldNotReachHere();
 490   return true; // Mute compiler
 491 }
 492 
 493 void os::Linux::init_thread_fpu_state(void) {
 494 }
 495 
 496 int os::Linux::get_fpu_control_word(void) {
 497   return 0;
 498 }
 499 
 500 void os::Linux::set_fpu_control_word(int fpu_control) {
 501 }
 502 
 503 // Check that the linux kernel version is 2.4 or higher since earlier
 504 // versions do not support SSE without patches.
 505 bool os::supports_sse() {
 506   return true;
 507 }
 508 
 509 bool os::is_allocatable(size_t bytes) {
 510   return true;
 511 }
 512 
 513 ////////////////////////////////////////////////////////////////////////////////
 514 // thread stack
 515 
 516 // Minimum usable stack sizes required to get to user code. Space for
 517 // HotSpot guard pages is added later.
 518 size_t os::Posix::_compiler_thread_min_stack_allowed = 72 * K;
 519 size_t os::Posix::_java_thread_min_stack_allowed = 72 * K;
 520 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 72 * K;
 521 
 522 // return default stack size for thr_type
 523 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 524   // default stack size (compiler thread needs larger stack)
 525   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 526   return s;
 527 }
 528 
 529 /////////////////////////////////////////////////////////////////////////////
 530 // helper functions for fatal error handler
 531 
 532 void os::print_context(outputStream *st, const void *context) {
 533   if (context == NULL) return;
 534 
 535   const ucontext_t *uc = (const ucontext_t*)context;
 536   st->print_cr("Registers:");
 537 #ifdef BUILTIN_SIM
 538   st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
 539   st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
 540   st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
 541   st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
 542   st->cr();
 543   st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
 544   st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
 545   st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
 546   st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
 547   st->cr();
 548   st->print(  "R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
 549   st->print(", R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
 550   st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
 551   st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
 552   st->cr();
 553   st->print(  "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
 554   st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
 555   st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
 556   st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
 557   st->cr();
 558   st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
 559   st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);
 560   st->print(", CSGSFS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_CSGSFS]);
 561   st->print(", ERR=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ERR]);
 562   st->cr();
 563   st->print("  TRAPNO=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_TRAPNO]);
 564   st->cr();
 565 #else
 566   for (int r = 0; r < 31; r++) {
 567     st->print("R%-2d=", r);
 568     print_location(st, uc->uc_mcontext.regs[r]);
 569   }
 570 #endif
 571   st->cr();
 572 
 573   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 574   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 575   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 576   st->cr();
 577 
 578   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 579   // point to garbage if entry point in an nmethod is corrupted. Leave
 580   // this at the end, and hope for the best.
 581   address pc = os::Linux::ucontext_get_pc(uc);
 582   print_instructions(st, pc, sizeof(char));
 583   st->cr();
 584 }
 585 
 586 void os::print_register_info(outputStream *st, const void *context) {
 587   if (context == NULL) return;
 588 
 589   const ucontext_t *uc = (const ucontext_t*)context;
 590 
 591   st->print_cr("Register to memory mapping:");
 592   st->cr();
 593 
 594   // this is horrendously verbose but the layout of the registers in the
 595   // context does not match how we defined our abstract Register set, so
 596   // we can't just iterate through the gregs area
 597 
 598   // this is only for the "general purpose" registers
 599 
 600 #ifdef BUILTIN_SIM
 601   st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
 602   st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
 603   st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
 604   st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
 605   st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
 606   st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
 607   st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
 608   st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
 609   st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
 610   st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
 611   st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
 612   st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
 613   st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
 614   st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
 615   st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
 616   st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
 617 #else
 618   for (int r = 0; r < 31; r++)
 619     st->print_cr(  "R%d=" INTPTR_FORMAT, r, (uintptr_t)uc->uc_mcontext.regs[r]);
 620 #endif
 621   st->cr();
 622 }
 623 
 624 void os::setup_fpu() {
 625 }
 626 
 627 #ifndef PRODUCT
 628 void os::verify_stack_alignment() {
 629   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 630 }
 631 #endif
 632 
 633 int os::extra_bang_size_in_bytes() {
 634   // AArch64 does not require the additional stack bang.
 635   return 0;
 636 }
 637 
 638 extern "C" {
 639   int SpinPause() {
 640     return 0;
 641   }
 642 
 643   void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) {
 644     if (from > to) {
 645       const jshort *end = from + count;
 646       while (from < end)
 647         *(to++) = *(from++);
 648     }
 649     else if (from < to) {
 650       const jshort *end = from;
 651       from += count - 1;
 652       to   += count - 1;
 653       while (from >= end)
 654         *(to--) = *(from--);
 655     }
 656   }
 657   void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) {
 658     if (from > to) {
 659       const jint *end = from + count;
 660       while (from < end)
 661         *(to++) = *(from++);
 662     }
 663     else if (from < to) {
 664       const jint *end = from;
 665       from += count - 1;
 666       to   += count - 1;
 667       while (from >= end)
 668         *(to--) = *(from--);
 669     }
 670   }
 671   void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) {
 672     if (from > to) {
 673       const jlong *end = from + count;
 674       while (from < end)
 675         os::atomic_copy64(from++, to++);
 676     }
 677     else if (from < to) {
 678       const jlong *end = from;
 679       from += count - 1;
 680       to   += count - 1;
 681       while (from >= end)
 682         os::atomic_copy64(from--, to--);
 683     }
 684   }
 685 
 686   void _Copy_arrayof_conjoint_bytes(const HeapWord* from,
 687                                     HeapWord* to,
 688                                     size_t    count) {
 689     memmove(to, from, count);
 690   }
 691   void _Copy_arrayof_conjoint_jshorts(const HeapWord* from,
 692                                       HeapWord* to,
 693                                       size_t    count) {
 694     memmove(to, from, count * 2);
 695   }
 696   void _Copy_arrayof_conjoint_jints(const HeapWord* from,
 697                                     HeapWord* to,
 698                                     size_t    count) {
 699     memmove(to, from, count * 4);
 700   }
 701   void _Copy_arrayof_conjoint_jlongs(const HeapWord* from,
 702                                      HeapWord* to,
 703                                      size_t    count) {
 704     memmove(to, from, count * 8);
 705   }
 706 };