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