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 err(t, sig, pc, info, ucVoid);
 468   err.report_and_die();
 469 
 470   ShouldNotReachHere();
 471   return true; // Mute compiler
 472 }
 473 
 474 void os::Linux::init_thread_fpu_state(void) {
 475 }
 476 
 477 int os::Linux::get_fpu_control_word(void) {
 478   return 0;
 479 }
 480 
 481 void os::Linux::set_fpu_control_word(int fpu_control) {
 482 }
 483 
 484 // Check that the linux kernel version is 2.4 or higher since earlier
 485 // versions do not support SSE without patches.
 486 bool os::supports_sse() {
 487   return true;
 488 }
 489 
 490 bool os::is_allocatable(size_t bytes) {
 491   return true;
 492 }
 493 
 494 ////////////////////////////////////////////////////////////////////////////////
 495 // thread stack
 496 
 497 size_t os::Linux::min_stack_allowed  = 64 * K;
 498 
 499 // return default stack size for thr_type
 500 size_t os::Linux::default_stack_size(os::ThreadType thr_type) {
 501   // default stack size (compiler thread needs larger stack)
 502   size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M);
 503   return s;
 504 }
 505 
 506 size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
 507   // Creating guard page is very expensive. Java thread has HotSpot
 508   // guard page, only enable glibc guard page for non-Java threads.
 509   return (thr_type == java_thread ? 0 : page_size());
 510 }
 511 
 512 // Java thread:
 513 //
 514 //   Low memory addresses
 515 //    +------------------------+
 516 //    |                        |\  JavaThread created by VM does not have glibc
 517 //    |    glibc guard page    | - guard, attached Java thread usually has
 518 //    |                        |/  1 page glibc guard.
 519 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 520 //    |                        |\
 521 //    |  HotSpot Guard Pages   | - red and yellow pages
 522 //    |                        |/
 523 //    +------------------------+ JavaThread::stack_yellow_zone_base()
 524 //    |                        |\
 525 //    |      Normal Stack      | -
 526 //    |                        |/
 527 // P2 +------------------------+ Thread::stack_base()
 528 //
 529 // Non-Java thread:
 530 //
 531 //   Low memory addresses
 532 //    +------------------------+
 533 //    |                        |\
 534 //    |  glibc guard page      | - usually 1 page
 535 //    |                        |/
 536 // P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
 537 //    |                        |\
 538 //    |      Normal Stack      | -
 539 //    |                        |/
 540 // P2 +------------------------+ Thread::stack_base()
 541 //
 542 // ** P1 (aka bottom) and size ( P2 = P1 - size) are the address and stack size returned from
 543 //    pthread_attr_getstack()
 544 
 545 static void current_stack_region(address * bottom, size_t * size) {
 546   if (os::Linux::is_initial_thread()) {
 547      // initial thread needs special handling because pthread_getattr_np()
 548      // may return bogus value.
 549      *bottom = os::Linux::initial_thread_stack_bottom();
 550      *size   = os::Linux::initial_thread_stack_size();
 551   } else {
 552      pthread_attr_t attr;
 553 
 554      int rslt = pthread_getattr_np(pthread_self(), &attr);
 555 
 556      // JVM needs to know exact stack location, abort if it fails
 557      if (rslt != 0) {
 558        if (rslt == ENOMEM) {
 559          vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np");
 560        } else {
 561          fatal(err_msg("pthread_getattr_np failed with errno = %d", rslt));
 562        }
 563      }
 564 
 565      if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
 566          fatal("Can not locate current stack attributes!");
 567      }
 568 
 569      pthread_attr_destroy(&attr);
 570 
 571   }
 572   assert(os::current_stack_pointer() >= *bottom &&
 573          os::current_stack_pointer() < *bottom + *size, "just checking");
 574 }
 575 
 576 address os::current_stack_base() {
 577   address bottom;
 578   size_t size;
 579   current_stack_region(&bottom, &size);
 580   return (bottom + size);
 581 }
 582 
 583 size_t os::current_stack_size() {
 584   // stack size includes normal stack and HotSpot guard pages
 585   address bottom;
 586   size_t size;
 587   current_stack_region(&bottom, &size);
 588   return size;
 589 }
 590 
 591 /////////////////////////////////////////////////////////////////////////////
 592 // helper functions for fatal error handler
 593 
 594 void os::print_context(outputStream *st, void *context) {
 595   if (context == NULL) return;
 596 
 597   ucontext_t *uc = (ucontext_t*)context;
 598   st->print_cr("Registers:");
 599 #ifdef BUILTIN_SIM
 600   st->print(  "RAX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RAX]);
 601   st->print(", RBX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBX]);
 602   st->print(", RCX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RCX]);
 603   st->print(", RDX=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDX]);
 604   st->cr();
 605   st->print(  "RSP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSP]);
 606   st->print(", RBP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RBP]);
 607   st->print(", RSI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RSI]);
 608   st->print(", RDI=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RDI]);
 609   st->cr();
 610   st->print(  "R8 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R8]);
 611   st->print(", R9 =" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R9]);
 612   st->print(", R10=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R10]);
 613   st->print(", R11=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R11]);
 614   st->cr();
 615   st->print(  "R12=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R12]);
 616   st->print(", R13=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R13]);
 617   st->print(", R14=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R14]);
 618   st->print(", R15=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_R15]);
 619   st->cr();
 620   st->print(  "RIP=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_RIP]);
 621   st->print(", EFLAGS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_EFL]);
 622   st->print(", CSGSFS=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_CSGSFS]);
 623   st->print(", ERR=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_ERR]);
 624   st->cr();
 625   st->print("  TRAPNO=" INTPTR_FORMAT, uc->uc_mcontext.gregs[REG_TRAPNO]);
 626   st->cr();
 627 #else
 628   for (int r = 0; r < 31; r++)
 629     st->print_cr(  "R%d=" INTPTR_FORMAT, r, (size_t)uc->uc_mcontext.regs[r]);
 630 #endif
 631   st->cr();
 632 
 633   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 634   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 635   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 636   st->cr();
 637 
 638   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 639   // point to garbage if entry point in an nmethod is corrupted. Leave
 640   // this at the end, and hope for the best.
 641   address pc = os::Linux::ucontext_get_pc(uc);
 642   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
 643   print_hex_dump(st, pc - 32, pc + 32, sizeof(char));
 644 }
 645 
 646 void os::print_register_info(outputStream *st, void *context) {
 647   if (context == NULL) return;
 648 
 649   ucontext_t *uc = (ucontext_t*)context;
 650 
 651   st->print_cr("Register to memory mapping:");
 652   st->cr();
 653 
 654   // this is horrendously verbose but the layout of the registers in the
 655   // context does not match how we defined our abstract Register set, so
 656   // we can't just iterate through the gregs area
 657 
 658   // this is only for the "general purpose" registers
 659 
 660 #ifdef BUILTIN_SIM
 661   st->print("RAX="); print_location(st, uc->uc_mcontext.gregs[REG_RAX]);
 662   st->print("RBX="); print_location(st, uc->uc_mcontext.gregs[REG_RBX]);
 663   st->print("RCX="); print_location(st, uc->uc_mcontext.gregs[REG_RCX]);
 664   st->print("RDX="); print_location(st, uc->uc_mcontext.gregs[REG_RDX]);
 665   st->print("RSP="); print_location(st, uc->uc_mcontext.gregs[REG_RSP]);
 666   st->print("RBP="); print_location(st, uc->uc_mcontext.gregs[REG_RBP]);
 667   st->print("RSI="); print_location(st, uc->uc_mcontext.gregs[REG_RSI]);
 668   st->print("RDI="); print_location(st, uc->uc_mcontext.gregs[REG_RDI]);
 669   st->print("R8 ="); print_location(st, uc->uc_mcontext.gregs[REG_R8]);
 670   st->print("R9 ="); print_location(st, uc->uc_mcontext.gregs[REG_R9]);
 671   st->print("R10="); print_location(st, uc->uc_mcontext.gregs[REG_R10]);
 672   st->print("R11="); print_location(st, uc->uc_mcontext.gregs[REG_R11]);
 673   st->print("R12="); print_location(st, uc->uc_mcontext.gregs[REG_R12]);
 674   st->print("R13="); print_location(st, uc->uc_mcontext.gregs[REG_R13]);
 675   st->print("R14="); print_location(st, uc->uc_mcontext.gregs[REG_R14]);
 676   st->print("R15="); print_location(st, uc->uc_mcontext.gregs[REG_R15]);
 677 #else
 678   for (int r = 0; r < 31; r++)
 679     st->print_cr(  "R%d=" INTPTR_FORMAT, r, (uintptr_t)uc->uc_mcontext.regs[r]);
 680 #endif
 681   st->cr();
 682 }
 683 
 684 void os::setup_fpu() {
 685 }
 686 
 687 #ifndef PRODUCT
 688 void os::verify_stack_alignment() {
 689   assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment");
 690 }
 691 #endif
 692 
 693 int os::extra_bang_size_in_bytes() {
 694   // AArch64 does not require the additional stack bang.
 695   return 0;
 696 }
 697 
 698 extern "C" {
 699   int SpinPause() {
 700     return 0;
 701   }
 702 
 703   void _Copy_conjoint_jshorts_atomic(jshort* from, jshort* to, size_t count) {
 704     if (from > to) {
 705       jshort *end = from + count;
 706       while (from < end)
 707         *(to++) = *(from++);
 708     }
 709     else if (from < to) {
 710       jshort *end = from;
 711       from += count - 1;
 712       to   += count - 1;
 713       while (from >= end)
 714         *(to--) = *(from--);
 715     }
 716   }
 717   void _Copy_conjoint_jints_atomic(jint* from, jint* to, size_t count) {
 718     if (from > to) {
 719       jint *end = from + count;
 720       while (from < end)
 721         *(to++) = *(from++);
 722     }
 723     else if (from < to) {
 724       jint *end = from;
 725       from += count - 1;
 726       to   += count - 1;
 727       while (from >= end)
 728         *(to--) = *(from--);
 729     }
 730   }
 731   void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
 732     if (from > to) {
 733       jlong *end = from + count;
 734       while (from < end)
 735         os::atomic_copy64(from++, to++);
 736     }
 737     else if (from < to) {
 738       jlong *end = from;
 739       from += count - 1;
 740       to   += count - 1;
 741       while (from >= end)
 742         os::atomic_copy64(from--, to--);
 743     }
 744   }
 745 
 746   void _Copy_arrayof_conjoint_bytes(HeapWord* from,
 747                                     HeapWord* to,
 748                                     size_t    count) {
 749     memmove(to, from, count);
 750   }
 751   void _Copy_arrayof_conjoint_jshorts(HeapWord* from,
 752                                       HeapWord* to,
 753                                       size_t    count) {
 754     memmove(to, from, count * 2);
 755   }
 756   void _Copy_arrayof_conjoint_jints(HeapWord* from,
 757                                     HeapWord* to,
 758                                     size_t    count) {
 759     memmove(to, from, count * 4);
 760   }
 761   void _Copy_arrayof_conjoint_jlongs(HeapWord* from,
 762                                      HeapWord* to,
 763                                      size_t    count) {
 764     memmove(to, from, count * 8);
 765   }
 766 };