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