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