1 /*
   2  * Copyright (c) 1999, 2015, 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 "asm/macroAssembler.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/icBuffer.hpp"
  32 #include "code/vtableStubs.hpp"
  33 #include "code/nativeInst.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "jvm_linux.h"
  36 #include "memory/allocation.inline.hpp"
  37 #include "mutex_linux.inline.hpp"
  38 #include "os_share_linux.hpp"
  39 #include "prims/jniFastGetField.hpp"
  40 #include "prims/jvm.h"
  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.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/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 
  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(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(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(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   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(void* ucVoid,
 159                     intptr_t** ret_sp, intptr_t** ret_fp) {
 160 
 161   ExtendedPC  epc;
 162   ucontext_t* uc = (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(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 // By default, gcc always saves frame pointer rfp on this stack. This
 186 // may get turned off by -fomit-frame-pointer.
 187 frame os::get_sender_for_C_frame(frame* fr) {
 188 #ifdef BUILTIN_SIM
 189   return frame(fr->sender_sp(), fr->link(), fr->sender_pc());
 190 #else
 191   return frame(fr->link(), fr->link(), fr->sender_pc());
 192 #endif
 193 }
 194 
 195 intptr_t* _get_previous_fp() {
 196   register intptr_t **ebp __asm__ (SPELL_REG_FP);
 197   return (intptr_t*) *ebp;   // we want what it points to.
 198 }
 199 
 200 
 201 frame os::current_frame() {
 202   intptr_t* fp = _get_previous_fp();
 203   frame myframe((intptr_t*)os::current_stack_pointer(),
 204                 (intptr_t*)fp,
 205                 CAST_FROM_FN_PTR(address, os::current_frame));
 206   if (os::is_first_C_frame(&myframe)) {
 207     // stack is not walkable
 208     return frame();
 209   } else {
 210     return os::get_sender_for_C_frame(&myframe);
 211   }
 212 }
 213 
 214 // Utility functions
 215 
 216 // From IA32 System Programming Guide
 217 enum {
 218   trap_page_fault = 0xE
 219 };
 220 
 221 #ifdef BUILTIN_SIM
 222 extern "C" void Fetch32PFI () ;
 223 extern "C" void Fetch32Resume () ;
 224 extern "C" void FetchNPFI () ;
 225 extern "C" void FetchNResume () ;
 226 #endif
 227 
 228 // An operation in Unsafe has faulted.  We're going to return to the
 229 // instruction after the faulting load or store.  We also set
 230 // pending_unsafe_access_error so that at some point in the future our
 231 // user will get a helpful message.
 232 static address handle_unsafe_access(JavaThread* thread, address pc) {
 233   // pc is the instruction which we must emulate
 234   // doing a no-op is fine:  return garbage from the load
 235   // therefore, compute npc
 236   address npc = pc + NativeCall::instruction_size;
 237 
 238   // request an async exception
 239   thread->set_pending_unsafe_access_error();
 240 
 241   // return address of next instruction to execute
 242   return npc;
 243 }
 244 
 245 extern "C" JNIEXPORT int
 246 JVM_handle_linux_signal(int sig,
 247                         siginfo_t* info,
 248                         void* ucVoid,
 249                         int abort_if_unrecognized) {
 250   ucontext_t* uc = (ucontext_t*) ucVoid;
 251 
 252   Thread* t = ThreadLocalStorage::get_thread_slow();
 253 
 254   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 255   // (no destructors can be run)
 256   os::WatcherThreadCrashProtection::check_crash_protection(sig, t);
 257 
 258   SignalHandlerMark shm(t);
 259 
 260   // Note: it's not uncommon that JNI code uses signal/sigset to install
 261   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 262   // or have a SIGILL handler when detecting CPU type). When that happens,
 263   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 264   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 265   // that do not require siginfo/ucontext first.
 266 
 267   if (sig == SIGPIPE || sig == SIGXFSZ) {
 268     // allow chained handler to go first
 269     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 270       return true;
 271     } else {
 272       if (PrintMiscellaneous && (WizardMode || Verbose)) {
 273         char buf[64];
 274         warning("Ignoring %s - see bugs 4229104 or 646499219",
 275                 os::exception_name(sig, buf, sizeof(buf)));
 276       }
 277       return true;
 278     }
 279   }
 280 
 281   JavaThread* thread = NULL;
 282   VMThread* vmthread = NULL;
 283   if (os::Linux::signal_handlers_are_installed) {
 284     if (t != NULL ){
 285       if(t->is_Java_thread()) {
 286         thread = (JavaThread*)t;
 287       }
 288       else if(t->is_VM_thread()){
 289         vmthread = (VMThread *)t;
 290       }
 291     }
 292   }
 293 /*
 294   NOTE: does not seem to work on linux.
 295   if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) {
 296     // can't decode this kind of signal
 297     info = NULL;
 298   } else {
 299     assert(sig == info->si_signo, "bad siginfo");
 300   }
 301 */
 302   // decide if this trap can be handled by a stub
 303   address stub = NULL;
 304 
 305   address pc          = NULL;
 306 
 307   //%note os_trap_1
 308   if (info != NULL && uc != NULL && thread != NULL) {
 309     pc = (address) os::Linux::ucontext_get_pc(uc);
 310 
 311 #ifdef BUILTIN_SIM
 312     if (pc == (address) Fetch32PFI) {
 313        uc->uc_mcontext.gregs[REG_PC] = intptr_t(Fetch32Resume) ;
 314        return 1 ;
 315     }
 316     if (pc == (address) FetchNPFI) {
 317        uc->uc_mcontext.gregs[REG_PC] = intptr_t (FetchNResume) ;
 318        return 1 ;
 319     }
 320 #else
 321     if (StubRoutines::is_safefetch_fault(pc)) {
 322       os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 323       return 1;
 324     }
 325 #endif
 326 
 327     // Handle ALL stack overflow variations here
 328     if (sig == SIGSEGV) {
 329       address addr = (address) info->si_addr;
 330 
 331       // check if fault address is within thread stack
 332       if (addr < thread->stack_base() &&
 333           addr >= thread->stack_base() - thread->stack_size()) {
 334         // stack overflow
 335         if (thread->in_stack_yellow_zone(addr)) {
 336           thread->disable_stack_yellow_zone();
 337           if (thread->thread_state() == _thread_in_Java) {
 338             // Throw a stack overflow exception.  Guard pages will be reenabled
 339             // while unwinding the stack.
 340             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 341           } else {
 342             // Thread was in the vm or native code.  Return and try to finish.
 343             return 1;
 344           }
 345         } else if (thread->in_stack_red_zone(addr)) {
 346           // Fatal red zone violation.  Disable the guard pages and fall through
 347           // to handle_unexpected_exception way down below.
 348           thread->disable_stack_red_zone();
 349           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 350 
 351           // This is a likely cause, but hard to verify. Let's just print
 352           // it as a hint.
 353           tty->print_raw_cr("Please check if any of your loaded .so files has "
 354                             "enabled executable stack (see man page execstack(8))");
 355         } else {
 356           // Accessing stack address below sp may cause SEGV if current
 357           // thread has MAP_GROWSDOWN stack. This should only happen when
 358           // current thread was created by user code with MAP_GROWSDOWN flag
 359           // and then attached to VM. See notes in os_linux.cpp.
 360           if (thread->osthread()->expanding_stack() == 0) {
 361              thread->osthread()->set_expanding_stack();
 362              if (os::Linux::manually_expand_stack(thread, addr)) {
 363                thread->osthread()->clear_expanding_stack();
 364                return 1;
 365              }
 366              thread->osthread()->clear_expanding_stack();
 367           } else {
 368              fatal("recursive segv. expanding stack.");
 369           }
 370         }
 371       }
 372     }
 373 
 374     if (thread->thread_state() == _thread_in_Java) {
 375       // Java thread running in Java code => find exception handler if any
 376       // a fault inside compiled code, the interpreter, or a stub
 377 
 378       // Handle signal from NativeJump::patch_verified_entry().
 379       if ((sig == SIGILL || sig == SIGTRAP)
 380           && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) {
 381         if (TraceTraps) {
 382           tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL");
 383         }
 384         stub = SharedRuntime::get_handle_wrong_method_stub();
 385       } else if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
 386         stub = SharedRuntime::get_poll_stub(pc);
 387       } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) {
 388         // BugId 4454115: A read from a MappedByteBuffer can fault
 389         // here if the underlying file has been truncated.
 390         // Do not crash the VM in such a case.
 391         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 392         nmethod* nm = (cb != NULL && cb->is_nmethod()) ? (nmethod*)cb : NULL;
 393         if (nm != NULL && nm->has_unsafe_access()) {
 394           stub = handle_unsafe_access(thread, pc);
 395         }
 396       }
 397       else
 398 
 399       if (sig == SIGFPE  &&
 400           (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {
 401         stub =
 402           SharedRuntime::
 403           continuation_for_implicit_exception(thread,
 404                                               pc,
 405                                               SharedRuntime::
 406                                               IMPLICIT_DIVIDE_BY_ZERO);
 407       } else if (sig == SIGSEGV &&
 408                !MacroAssembler::needs_explicit_null_check((intptr_t)info->si_addr)) {
 409           // Determination of interpreter/vtable stub/compiled code null exception
 410           stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 411       }
 412     } else if (thread->thread_state() == _thread_in_vm &&
 413                sig == SIGBUS && /* info->si_code == BUS_OBJERR && */
 414                thread->doing_unsafe_access()) {
 415         stub = handle_unsafe_access(thread, pc);
 416     }
 417 
 418     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 419     // and the heap gets shrunk before the field access.
 420     if ((sig == SIGSEGV) || (sig == SIGBUS)) {
 421       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 422       if (addr != (address)-1) {
 423         stub = addr;
 424       }
 425     }
 426 
 427     // Check to see if we caught the safepoint code in the
 428     // process of write protecting the memory serialization page.
 429     // It write enables the page immediately after protecting it
 430     // so we can just return to retry the write.
 431     if ((sig == SIGSEGV) &&
 432         os::is_memory_serialize_page(thread, (address) info->si_addr)) {
 433       // Block current thread until the memory serialize page permission restored.
 434       os::block_on_serialize_page_trap();
 435       return true;
 436     }
 437   }
 438 
 439   if (stub != NULL) {
 440     // save all thread context in case we need to restore it
 441     if (thread != NULL) thread->set_saved_exception_pc(pc);
 442 
 443     os::Linux::ucontext_set_pc(uc, stub);
 444     return true;
 445   }
 446 
 447   // signal-chaining
 448   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 449      return true;
 450   }
 451 
 452   if (!abort_if_unrecognized) {
 453     // caller wants another chance, so give it to him
 454     return false;
 455   }
 456 
 457   if (pc == NULL && uc != NULL) {
 458     pc = os::Linux::ucontext_get_pc(uc);
 459   }
 460 
 461   // unmask current signal
 462   sigset_t newset;
 463   sigemptyset(&newset);
 464   sigaddset(&newset, sig);
 465   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 466 
 467   VMError::report_and_die(t, sig, pc, info, ucVoid);
 468 
 469   ShouldNotReachHere();
 470   return true; // Mute compiler
 471 }
 472 
 473 void os::Linux::init_thread_fpu_state(void) {
 474 }
 475 
 476 int os::Linux::get_fpu_control_word(void) {
 477   return 0;
 478 }
 479 
 480 void os::Linux::set_fpu_control_word(int fpu_control) {
 481 }
 482 
 483 // Check that the linux kernel version is 2.4 or higher since earlier
 484 // versions do not support SSE without patches.
 485 bool os::supports_sse() {
 486   return true;
 487 }
 488 
 489 bool os::is_allocatable(size_t bytes) {
 490   return true;
 491 }
 492 
 493 ////////////////////////////////////////////////////////////////////////////////
 494 // thread stack
 495 
 496 size_t os::Linux::min_stack_allowed  = 64 * K;
 497 
 498 // return default stack size for thr_type
 499 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 500   // default stack size (compiler thread needs larger stack)
 501   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 502   return s;
 503 }
 504 
 505 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 506   // Creating guard page is very expensive. Java thread has HotSpot
 507   // guard page, only enable glibc guard page for non-Java threads.
 508   return (thr_type == java_thread ? 0 : page_size());
 509 }
 510 
 511 // Java thread:
 512 //
 513 //   Low memory addresses
 514 //    +------------------------+
 515 //    |                        |\  JavaThread created by VM does not have glibc
 516 //    |    glibc guard page    | - guard, attached Java thread usually has
 517 //    |                        |/  1 page glibc guard.
 518 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 519 //    |                        |\
 520 //    |  HotSpot Guard Pages   | - red and yellow pages
 521 //    |                        |/
 522 //    +------------------------+ JavaThread::stack_yellow_zone_base()
 523 //    |                        |\
 524 //    |      Normal Stack      | -
 525 //    |                        |/
 526 // P2 +------------------------+ Thread::stack_base()
 527 //
 528 // Non-Java thread:
 529 //
 530 //   Low memory addresses
 531 //    +------------------------+
 532 //    |                        |\
 533 //    |  glibc guard page      | - usually 1 page
 534 //    |                        |/
 535 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 536 //    |                        |\
 537 //    |      Normal Stack      | -
 538 //    |                        |/
 539 // P2 +------------------------+ Thread::stack_base()
 540 //
 541 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
 542 //    pthread_attr_getstack()
 543 
 544 static void current_stack_region(address * bottom, size_t * size) {
 545   if (os::Linux::is_initial_thread()) {
 546      // initial thread needs special handling because pthread_getattr_np()
 547      // may return bogus value.
 548      *bottom = os::Linux::initial_thread_stack_bottom();
 549      *size   = os::Linux::initial_thread_stack_size();
 550   } else {
 551      pthread_attr_t attr;
 552 
 553      int rslt = pthread_getattr_np(pthread_self(), &attr);
 554 
 555      // JVM needs to know exact stack location, abort if it fails
 556      if (rslt != 0) {
 557        if (rslt == ENOMEM) {
 558          vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");
 559        } else {
 560          fatal("pthread_getattr_np failed with errno = %d", rslt);
 561        }
 562      }
 563 
 564      if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
 565          fatal("Can not locate current stack attributes!");
 566      }
 567 
 568      pthread_attr_destroy(&attr);
 569 
 570   }
 571   assert(os::current_stack_pointer() >= *bottom &&
 572          os::current_stack_pointer() < *bottom + *size, "just checking");
 573 }
 574 
 575 address os::current_stack_base() {
 576   address bottom;
 577   size_t size;
 578   current_stack_region(&bottom, &size);
 579   return (bottom + size);
 580 }
 581 
 582 size_t os::current_stack_size() {
 583   // stack size includes normal stack and HotSpot guard pages
 584   address bottom;
 585   size_t size;
 586   current_stack_region(&bottom, &size);
 587   return size;
 588 }
 589 
 590 /////////////////////////////////////////////////////////////////////////////
 591 // helper functions for fatal error handler
 592 
 593 void os::print_context(outputStream *st, void *context) {
 594   if (context == NULL) return;
 595 
 596   ucontext_t *uc = (ucontext_t*)context;
 597   st->print_cr("Registers:");
 598 #ifdef BUILTIN_SIM
 599   st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
 600   st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
 601   st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
 602   st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
 603   st->cr();
 604   st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
 605   st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
 606   st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
 607   st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
 608   st->cr();
 609   st->print(  "R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
 610   st->print(", R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
 611   st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
 612   st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
 613   st->cr();
 614   st->print(  "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
 615   st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
 616   st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
 617   st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
 618   st->cr();
 619   st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
 620   st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);
 621   st->print(", CSGSFS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_CSGSFS]);
 622   st->print(", ERR=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ERR]);
 623   st->cr();
 624   st->print("  TRAPNO=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_TRAPNO]);
 625   st->cr();
 626 #else
 627   for (int r = 0; r < 31; r++)
 628     st->print_cr(  "R%d=" INTPTR_FORMAT, r, (size_t)uc->uc_mcontext.regs[r]);
 629 #endif
 630   st->cr();
 631 
 632   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 633   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 634   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 635   st->cr();
 636 
 637   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 638   // point to garbage if entry point in an nmethod is corrupted. Leave
 639   // this at the end, and hope for the best.
 640   address pc = os::Linux::ucontext_get_pc(uc);
 641   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
 642   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 643 }
 644 
 645 void os::print_register_info(outputStream *st, void *context) {
 646   if (context == NULL) return;
 647 
 648   ucontext_t *uc = (ucontext_t*)context;
 649 
 650   st->print_cr("Register to memory mapping:");
 651   st->cr();
 652 
 653   // this is horrendously verbose but the layout of the registers in the
 654   // context does not match how we defined our abstract Register set, so
 655   // we can't just iterate through the gregs area
 656 
 657   // this is only for the "general purpose" registers
 658 
 659 #ifdef BUILTIN_SIM
 660   st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
 661   st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
 662   st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
 663   st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
 664   st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
 665   st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
 666   st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
 667   st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
 668   st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
 669   st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
 670   st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
 671   st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
 672   st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
 673   st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
 674   st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
 675   st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
 676 #else
 677   for (int r = 0; r < 31; r++)
 678     st->print_cr(  "R%d=" INTPTR_FORMAT, r, (uintptr_t)uc->uc_mcontext.regs[r]);
 679 #endif
 680   st->cr();
 681 }
 682 
 683 void os::setup_fpu() {
 684 }
 685 
 686 #ifndef PRODUCT
 687 void os::verify_stack_alignment() {
 688   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 689 }
 690 #endif
 691 
 692 int os::extra_bang_size_in_bytes() {
 693   // AArch64 does not require the additional stack bang.
 694   return 0;
 695 }
 696 
 697 extern "C" {
 698   int SpinPause() {
 699     return 0;
 700   }
 701 
 702   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
 703     if (from > to) {
 704       jshort *end = from + count;
 705       while (from < end)
 706         *(to++) = *(from++);
 707     }
 708     else if (from < to) {
 709       jshort *end = from;
 710       from += count - 1;
 711       to   += count - 1;
 712       while (from >= end)
 713         *(to--) = *(from--);
 714     }
 715   }
 716   void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
 717     if (from > to) {
 718       jint *end = from + count;
 719       while (from < end)
 720         *(to++) = *(from++);
 721     }
 722     else if (from < to) {
 723       jint *end = from;
 724       from += count - 1;
 725       to   += count - 1;
 726       while (from >= end)
 727         *(to--) = *(from--);
 728     }
 729   }
 730   void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
 731     if (from > to) {
 732       jlong *end = from + count;
 733       while (from < end)
 734         os::atomic_copy64(from++, to++);
 735     }
 736     else if (from < to) {
 737       jlong *end = from;
 738       from += count - 1;
 739       to   += count - 1;
 740       while (from >= end)
 741         os::atomic_copy64(from--, to--);
 742     }
 743   }
 744 
 745   void _Copy_arrayof_conjoint_bytes(HeapWord* from,
 746                                     HeapWord* to,
 747                                     size_t    count) {
 748     memmove(to, from, count);
 749   }
 750   void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
 751                                       HeapWord* to,
 752                                       size_t    count) {
 753     memmove(to, from, count * 2);
 754   }
 755   void _Copy_arrayof_conjoint_jints(HeapWord* from,
 756                                     HeapWord* to,
 757                                     size_t    count) {
 758     memmove(to, from, count * 4);
 759   }
 760   void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
 761                                      HeapWord* to,
 762                                      size_t    count) {
 763     memmove(to, from, count * 8);
 764   }
 765 };