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