1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "jvm.h"
  27 #include "assembler_arm.inline.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 "interpreter/interpreter.hpp"
  34 #include "memory/allocation.inline.hpp"
  35 #include "nativeInst_arm.hpp"
  36 #include "os_share_linux.hpp"
  37 #include "prims/jniFastGetField.hpp"
  38 #include "prims/jvm_misc.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/extendedPC.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/interfaceSupport.inline.hpp"
  43 #include "runtime/java.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/mutexLocker.hpp"
  46 #include "runtime/osThread.hpp"
  47 #include "runtime/sharedRuntime.hpp"
  48 #include "runtime/stubRoutines.hpp"
  49 #include "runtime/timer.hpp"
  50 #include "utilities/debug.hpp"
  51 #include "utilities/events.hpp"
  52 #include "utilities/vmError.hpp"
  53 
  54 // put OS-includes here
  55 # include <sys/types.h>
  56 # include <sys/mman.h>
  57 # include <pthread.h>
  58 # include <signal.h>
  59 # include <errno.h>
  60 # include <dlfcn.h>
  61 # include <stdlib.h>
  62 # include <stdio.h>
  63 # include <unistd.h>
  64 # include <sys/resource.h>
  65 # include <pthread.h>
  66 # include <sys/stat.h>
  67 # include <sys/time.h>
  68 # include <sys/utsname.h>
  69 # include <sys/socket.h>
  70 # include <sys/wait.h>
  71 # include <pwd.h>
  72 # include <poll.h>
  73 # include <ucontext.h>
  74 # include <fpu_control.h>
  75 # include <asm/ptrace.h>
  76 
  77 #define SPELL_REG_SP  "sp"
  78 
  79 // Don't #define SPELL_REG_FP for thumb because it is not safe to use, so this makes sure we never fetch it.
  80 #ifndef __thumb__
  81 #define SPELL_REG_FP "fp"
  82 #endif
  83 
  84 address os::current_stack_pointer() {
  85   register address sp __asm__ (SPELL_REG_SP);
  86   return sp;
  87 }
  88 
  89 char* os::non_memory_address_word() {
  90   // Must never look like an address returned by reserve_memory
  91   return (char*) -1;
  92 }
  93 
  94 
  95 #if NGREG == 16
  96 // These definitions are based on the observation that until
  97 // the certain version of GCC mcontext_t was defined as
  98 // a structure containing gregs[NGREG] array with 16 elements.
  99 // In later GCC versions mcontext_t was redefined as struct sigcontext,
 100 // along with NGREG constant changed to 18.
 101 #define arm_pc gregs[15]
 102 #define arm_sp gregs[13]
 103 #define arm_fp gregs[11]
 104 #define arm_r0 gregs[0]
 105 #endif
 106 
 107 #define ARM_REGS_IN_CONTEXT  16
 108 
 109 
 110 address os::Linux::ucontext_get_pc(const ucontext_t* uc) {
 111   return (address)uc->uc_mcontext.arm_pc;
 112 }
 113 
 114 void os::Linux::ucontext_set_pc(ucontext_t* uc, address pc) {
 115   uc->uc_mcontext.arm_pc = (uintx)pc;
 116 }
 117 
 118 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t* uc) {
 119   return (intptr_t*)uc->uc_mcontext.arm_sp;
 120 }
 121 
 122 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t* uc) {
 123   return (intptr_t*)uc->uc_mcontext.arm_fp;
 124 }
 125 
 126 bool is_safe_for_fp(address pc) {
 127 #ifdef __thumb__
 128   if (CodeCache::find_blob(pc) != NULL) {
 129     return true;
 130   }
 131   // For thumb C frames, given an fp we have no idea how to access the frame contents.
 132   return false;
 133 #else
 134   // Calling os::address_is_in_vm() here leads to a dladdr call. Calling any libc
 135   // function during os::get_native_stack() can result in a deadlock if JFR is
 136   // enabled. For now, be more lenient and allow all pc's. There are other
 137   // frame sanity checks in shared code, and to date they have been sufficient
 138   // for other platforms.
 139   //return os::address_is_in_vm(pc);
 140   return true;
 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) {
 169       intptr_t* fp = os::Linux::ucontext_get_fp(uc);
 170 #ifndef __thumb__
 171       if (CodeCache::find_blob(epc.pc()) == NULL) {
 172         // It's a C frame. We need to adjust the fp.
 173         fp += os::C_frame_offset;
 174       }
 175 #endif
 176       // Clear FP when stack walking is dangerous so that
 177       // the frame created will not be walked.
 178       // However, ensure FP is set correctly when reliable and
 179       // potentially necessary.
 180       if (!is_safe_for_fp(epc.pc())) {
 181         // FP unreliable
 182         fp = (intptr_t *)NULL;
 183       }
 184       *ret_fp = fp;
 185     }
 186   } else {
 187     // construct empty ExtendedPC for return value checking
 188     epc = ExtendedPC(NULL);
 189     if (ret_sp) *ret_sp = (intptr_t *)NULL;
 190     if (ret_fp) *ret_fp = (intptr_t *)NULL;
 191   }
 192 
 193   return epc;
 194 }
 195 
 196 frame os::fetch_frame_from_context(const void* ucVoid) {
 197   intptr_t* sp;
 198   intptr_t* fp;
 199   ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp);
 200   return frame(sp, fp, epc.pc());
 201 }
 202 
 203 frame os::get_sender_for_C_frame(frame* fr) {
 204 #ifdef __thumb__
 205   // We can't reliably get anything from a thumb C frame.
 206   return frame();
 207 #else
 208   address pc = fr->sender_pc();
 209   if (! is_safe_for_fp(pc)) {
 210     return frame(fr->sender_sp(), (intptr_t *)NULL, pc);
 211   } else {
 212     return frame(fr->sender_sp(), fr->link() + os::C_frame_offset, pc);
 213   }
 214 #endif
 215 }
 216 
 217 //
 218 // This actually returns two frames up. It does not return os::current_frame(),
 219 // which is the actual current frame. Nor does it return os::get_native_stack(),
 220 // which is the caller. It returns whoever called os::get_native_stack(). Not
 221 // very intuitive, but consistent with how this API is implemented on other
 222 // platforms.
 223 //
 224 frame os::current_frame() {
 225 #ifdef __thumb__
 226   // We can't reliably get anything from a thumb C frame.
 227   return frame();
 228 #else
 229   register intptr_t* fp __asm__ (SPELL_REG_FP);
 230   // fp is for os::current_frame. We want the fp for our caller.
 231   frame myframe((intptr_t*)os::current_stack_pointer(), fp + os::C_frame_offset,
 232                  CAST_FROM_FN_PTR(address, os::current_frame));
 233   frame caller_frame = os::get_sender_for_C_frame(&myframe);
 234 
 235   if (os::is_first_C_frame(&caller_frame)) {
 236     // stack is not walkable
 237     // Assert below was added because it does not seem like this can ever happen.
 238     // How can this frame ever be the first C frame since it is called from C code?
 239     // If it does ever happen, undo the assert and comment here on when/why it happens.
 240     assert(false, "this should never happen");
 241     return frame();
 242   }
 243 
 244   // return frame for our caller's caller
 245   return os::get_sender_for_C_frame(&caller_frame);
 246 #endif
 247 }
 248 
 249 extern "C" address check_vfp_fault_instr;
 250 extern "C" address check_vfp3_32_fault_instr;
 251 extern "C" address check_simd_fault_instr;
 252 extern "C" address check_mp_ext_fault_instr;
 253 
 254 address check_vfp_fault_instr = NULL;
 255 address check_vfp3_32_fault_instr = NULL;
 256 address check_simd_fault_instr = NULL;
 257 address check_mp_ext_fault_instr = NULL;
 258 
 259 // Utility functions
 260 
 261 extern "C" int JVM_handle_linux_signal(int sig, siginfo_t* info,
 262                                        void* ucVoid, int abort_if_unrecognized) {
 263   ucontext_t* uc = (ucontext_t*) ucVoid;
 264 
 265   Thread* t = Thread::current_or_null_safe();
 266 
 267   // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away
 268   // (no destructors can be run)
 269   os::ThreadCrashProtection::check_crash_protection(sig, t);
 270 
 271   SignalHandlerMark shm(t);
 272 
 273   if (sig == SIGILL &&
 274       ((info->si_addr == (caddr_t)check_simd_fault_instr)
 275        || info->si_addr == (caddr_t)check_vfp_fault_instr
 276        || info->si_addr == (caddr_t)check_vfp3_32_fault_instr
 277        || info->si_addr == (caddr_t)check_mp_ext_fault_instr)) {
 278     // skip faulty instruction + instruction that sets return value to
 279     // success and set return value to failure.
 280     os::Linux::ucontext_set_pc(uc, (address)info->si_addr + 8);
 281     uc->uc_mcontext.arm_r0 = 0;
 282     return true;
 283   }
 284 
 285   // Note: it's not uncommon that JNI code uses signal/sigset to install
 286   // then restore certain signal handler (e.g. to temporarily block SIGPIPE,
 287   // or have a SIGILL handler when detecting CPU type). When that happens,
 288   // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To
 289   // avoid unnecessary crash when libjsig is not preloaded, try handle signals
 290   // that do not require siginfo/ucontext first.
 291 
 292   if (sig == SIGPIPE || sig == SIGXFSZ) {
 293     // allow chained handler to go first
 294     if (os::Linux::chained_handler(sig, info, ucVoid)) {
 295       return true;
 296     } else {
 297       // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219
 298       return true;
 299     }
 300   }
 301 
 302 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
 303   if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) {
 304     handle_assert_poison_fault(ucVoid, info->si_addr);
 305     return 1;
 306   }
 307 #endif
 308 
 309   JavaThread* thread = NULL;
 310   VMThread* vmthread = NULL;
 311   if (os::Linux::signal_handlers_are_installed) {
 312     if (t != NULL ){
 313       if(t->is_Java_thread()) {
 314         thread = (JavaThread*)t;
 315       }
 316       else if(t->is_VM_thread()){
 317         vmthread = (VMThread *)t;
 318       }
 319     }
 320   }
 321 
 322   address stub = NULL;
 323   address pc = NULL;
 324   bool unsafe_access = false;
 325 
 326   if (info != NULL && uc != NULL && thread != NULL) {
 327     pc = (address) os::Linux::ucontext_get_pc(uc);
 328 
 329     // Handle ALL stack overflow variations here
 330     if (sig == SIGSEGV) {
 331       address addr = (address) info->si_addr;
 332 
 333       if (StubRoutines::is_safefetch_fault(pc)) {
 334         os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 335         return 1;
 336       }
 337       // check if fault address is within thread stack
 338       if (addr < thread->stack_base() &&
 339           addr >= thread->stack_base() - thread->stack_size()) {
 340         // stack overflow
 341         if (thread->in_stack_yellow_reserved_zone(addr)) {
 342           thread->disable_stack_yellow_reserved_zone();
 343           if (thread->thread_state() == _thread_in_Java) {
 344             // Throw a stack overflow exception.  Guard pages will be reenabled
 345             // while unwinding the stack.
 346             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
 347           } else {
 348             // Thread was in the vm or native code.  Return and try to finish.
 349             return 1;
 350           }
 351         } else if (thread->in_stack_red_zone(addr)) {
 352           // Fatal red zone violation.  Disable the guard pages and fall through
 353           // to handle_unexpected_exception way down below.
 354           thread->disable_stack_red_zone();
 355           tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
 356         } else {
 357           // Accessing stack address below sp may cause SEGV if current
 358           // thread has MAP_GROWSDOWN stack. This should only happen when
 359           // current thread was created by user code with MAP_GROWSDOWN flag
 360           // and then attached to VM. See notes in os_linux.cpp.
 361           if (thread->osthread()->expanding_stack() == 0) {
 362              thread->osthread()->set_expanding_stack();
 363              if (os::Linux::manually_expand_stack(thread, addr)) {
 364                thread->osthread()->clear_expanding_stack();
 365                return 1;
 366              }
 367              thread->osthread()->clear_expanding_stack();
 368           } else {
 369              fatal("recursive segv. expanding stack.");
 370           }
 371         }
 372       }
 373     }
 374 
 375     if (thread->thread_state() == _thread_in_Java) {
 376       // Java thread running in Java code => find exception handler if any
 377       // a fault inside compiled code, the interpreter, or a stub
 378 
 379       if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
 380         stub = SharedRuntime::get_poll_stub(pc);
 381       } else if (sig == SIGBUS) {
 382         // BugId 4454115: A read from a MappedByteBuffer can fault
 383         // here if the underlying file has been truncated.
 384         // Do not crash the VM in such a case.
 385         CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 386         CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL;
 387         if (nm != NULL && nm->has_unsafe_access()) {
 388           unsafe_access = true;
 389         }
 390       } else if (sig == SIGSEGV &&
 391                  MacroAssembler::uses_implicit_null_check(info->si_addr)) {
 392           // Determination of interpreter/vtable stub/compiled code null exception
 393           CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
 394           if (cb != NULL) {
 395             stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
 396           }
 397       } else if (sig == SIGILL && *(int *)pc == NativeInstruction::zombie_illegal_instruction) {
 398         // Zombie
 399         stub = SharedRuntime::get_handle_wrong_method_stub();
 400       }
 401     } else if (thread->thread_state() == _thread_in_vm &&
 402                sig == SIGBUS && thread->doing_unsafe_access()) {
 403         unsafe_access = true;
 404     }
 405 
 406     // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in
 407     // and the heap gets shrunk before the field access.
 408     if (sig == SIGSEGV || sig == SIGBUS) {
 409       address addr = JNI_FastGetField::find_slowcase_pc(pc);
 410       if (addr != (address)-1) {
 411         stub = addr;
 412       }
 413     }
 414   }
 415 
 416   if (unsafe_access && stub == NULL) {
 417     // it can be an unsafe access and we haven't found
 418     // any other suitable exception reason,
 419     // so assume it is an unsafe access.
 420     address next_pc = pc + Assembler::InstructionSize;
 421 #ifdef __thumb__
 422     if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {
 423       next_pc = (address)((intptr_t)next_pc | 0x1);
 424     }
 425 #endif
 426 
 427     stub = SharedRuntime::handle_unsafe_access(thread, next_pc);
 428   }
 429 
 430   if (stub != NULL) {
 431 #ifdef __thumb__
 432     if (uc->uc_mcontext.arm_cpsr & PSR_T_BIT) {
 433       intptr_t p = (intptr_t)pc | 0x1;
 434       pc = (address)p;
 435 
 436       // Clear Thumb mode bit if we're redirected into the ARM ISA based code
 437       if (((intptr_t)stub & 0x1) == 0) {
 438         uc->uc_mcontext.arm_cpsr &= ~PSR_T_BIT;
 439       }
 440     } else {
 441       // No Thumb2 compiled stubs are triggered from ARM ISA compiled JIT'd code today.
 442       // The support needs to be added if that changes
 443       assert((((intptr_t)stub & 0x1) == 0), "can't return to Thumb code");
 444     }
 445 #endif
 446 
 447     // save all thread context in case we need to restore it
 448     if (thread != NULL) thread->set_saved_exception_pc(pc);
 449 
 450     os::Linux::ucontext_set_pc(uc, stub);
 451     return true;
 452   }
 453 
 454   // signal-chaining
 455   if (os::Linux::chained_handler(sig, info, ucVoid)) {
 456      return true;
 457   }
 458 
 459   if (!abort_if_unrecognized) {
 460     // caller wants another chance, so give it to him
 461     return false;
 462   }
 463 
 464   if (pc == NULL && uc != NULL) {
 465     pc = os::Linux::ucontext_get_pc(uc);
 466   }
 467 
 468   // unmask current signal
 469   sigset_t newset;
 470   sigemptyset(&newset);
 471   sigaddset(&newset, sig);
 472   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 473 
 474   VMError::report_and_die(t, sig, pc, info, ucVoid);
 475 
 476   ShouldNotReachHere();
 477   return false;
 478 }
 479 
 480 void os::Linux::init_thread_fpu_state(void) {
 481   os::setup_fpu();
 482 }
 483 
 484 int os::Linux::get_fpu_control_word(void) {
 485   return 0;
 486 }
 487 
 488 void os::Linux::set_fpu_control_word(int fpu_control) {
 489   // Nothing to do
 490 }
 491 
 492 void os::setup_fpu() {
 493 #if !defined(__SOFTFP__) && defined(__VFP_FP__)
 494   // Turn on IEEE-754 compliant VFP mode
 495   __asm__ volatile (
 496     "mov %%r0, #0;"
 497     "fmxr fpscr, %%r0"
 498     : /* no output */ : /* no input */ : "r0"
 499   );
 500 #endif
 501 }
 502 
 503 bool os::is_allocatable(size_t bytes) {
 504   return true;
 505 }
 506 
 507 ////////////////////////////////////////////////////////////////////////////////
 508 // thread stack
 509 
 510 // Minimum usable stack sizes required to get to user code. Space for
 511 // HotSpot guard pages is added later.
 512 size_t os::Posix::_compiler_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;
 513 size_t os::Posix::_java_thread_min_stack_allowed = (32 DEBUG_ONLY(+ 4)) * K;
 514 size_t os::Posix::_vm_internal_thread_min_stack_allowed = (48 DEBUG_ONLY(+ 4)) * K;
 515 
 516 // return default stack size for thr_type
 517 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
 518   // default stack size (compiler thread needs larger stack)
 519   size_t s = (thr_type == os::compiler_thread ? 2 * M : 512 * K);
 520   return s;
 521 }
 522 
 523 /////////////////////////////////////////////////////////////////////////////
 524 // helper functions for fatal error handler
 525 
 526 void os::print_context(outputStream *st, const void *context) {
 527   if (context == NULL) return;
 528   const ucontext_t *uc = (const ucontext_t*)context;
 529 
 530   st->print_cr("Registers:");
 531   intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;
 532   for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {
 533     st->print_cr("  %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);
 534   }
 535 #define U64_FORMAT "0x%016llx"
 536   // now print flag register
 537   st->print_cr("  %-4s = 0x%08lx", "cpsr",uc->uc_mcontext.arm_cpsr);
 538   st->cr();
 539 
 540   intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc);
 541   st->print_cr("Top of Stack: (sp=" INTPTR_FORMAT ")", p2i(sp));
 542   print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t));
 543   st->cr();
 544 
 545   // Note: it may be unsafe to inspect memory near pc. For example, pc may
 546   // point to garbage if entry point in an nmethod is corrupted. Leave
 547   // this at the end, and hope for the best.
 548   address pc = os::Linux::ucontext_get_pc(uc);
 549   print_instructions(st, pc, Assembler::InstructionSize);
 550   st->cr();
 551 }
 552 
 553 void os::print_register_info(outputStream *st, const void *context) {
 554   if (context == NULL) return;
 555 
 556   const ucontext_t *uc = (const ucontext_t*)context;
 557   intx* reg_area = (intx*)&uc->uc_mcontext.arm_r0;
 558 
 559   st->print_cr("Register to memory mapping:");
 560   st->cr();
 561   for (int r = 0; r < ARM_REGS_IN_CONTEXT; r++) {
 562     st->print_cr("  %-3s = " INTPTR_FORMAT, as_Register(r)->name(), reg_area[r]);
 563     print_location(st, reg_area[r]);
 564     st->cr();
 565   }
 566   st->cr();
 567 }
 568 
 569 
 570 
 571 typedef int64_t cmpxchg_long_func_t(int64_t, int64_t, volatile int64_t*);
 572 
 573 cmpxchg_long_func_t* os::atomic_cmpxchg_long_func = os::atomic_cmpxchg_long_bootstrap;
 574 
 575 int64_t os::atomic_cmpxchg_long_bootstrap(int64_t compare_value, int64_t exchange_value, volatile int64_t* dest) {
 576   // try to use the stub:
 577   cmpxchg_long_func_t* func = CAST_TO_FN_PTR(cmpxchg_long_func_t*, StubRoutines::atomic_cmpxchg_long_entry());
 578 
 579   if (func != NULL) {
 580     os::atomic_cmpxchg_long_func = func;
 581     return (*func)(compare_value, exchange_value, dest);
 582   }
 583   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 584 
 585   int64_t old_value = *dest;
 586   if (old_value == compare_value)
 587     *dest = exchange_value;
 588   return old_value;
 589 }
 590 typedef int64_t load_long_func_t(const volatile int64_t*);
 591 
 592 load_long_func_t* os::atomic_load_long_func = os::atomic_load_long_bootstrap;
 593 
 594 int64_t os::atomic_load_long_bootstrap(const volatile int64_t* src) {
 595   // try to use the stub:
 596   load_long_func_t* func = CAST_TO_FN_PTR(load_long_func_t*, StubRoutines::atomic_load_long_entry());
 597 
 598   if (func != NULL) {
 599     os::atomic_load_long_func = func;
 600     return (*func)(src);
 601   }
 602   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 603 
 604   int64_t old_value = *src;
 605   return old_value;
 606 }
 607 
 608 typedef void store_long_func_t(int64_t, volatile int64_t*);
 609 
 610 store_long_func_t* os::atomic_store_long_func = os::atomic_store_long_bootstrap;
 611 
 612 void os::atomic_store_long_bootstrap(int64_t val, volatile int64_t* dest) {
 613   // try to use the stub:
 614   store_long_func_t* func = CAST_TO_FN_PTR(store_long_func_t*, StubRoutines::atomic_store_long_entry());
 615 
 616   if (func != NULL) {
 617     os::atomic_store_long_func = func;
 618     return (*func)(val, dest);
 619   }
 620   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 621 
 622   *dest = val;
 623 }
 624 
 625 typedef int32_t  atomic_add_func_t(int32_t add_value, volatile int32_t *dest);
 626 
 627 atomic_add_func_t * os::atomic_add_func = os::atomic_add_bootstrap;
 628 
 629 int32_t  os::atomic_add_bootstrap(int32_t add_value, volatile int32_t *dest) {
 630   atomic_add_func_t * func = CAST_TO_FN_PTR(atomic_add_func_t*,
 631                                             StubRoutines::atomic_add_entry());
 632   if (func != NULL) {
 633     os::atomic_add_func = func;
 634     return (*func)(add_value, dest);
 635   }
 636 
 637   int32_t old_value = *dest;
 638   *dest = old_value + add_value;
 639   return (old_value + add_value);
 640 }
 641 
 642 typedef int32_t  atomic_xchg_func_t(int32_t exchange_value, volatile int32_t *dest);
 643 
 644 atomic_xchg_func_t * os::atomic_xchg_func = os::atomic_xchg_bootstrap;
 645 
 646 int32_t  os::atomic_xchg_bootstrap(int32_t exchange_value, volatile int32_t *dest) {
 647   atomic_xchg_func_t * func = CAST_TO_FN_PTR(atomic_xchg_func_t*,
 648                                             StubRoutines::atomic_xchg_entry());
 649   if (func != NULL) {
 650     os::atomic_xchg_func = func;
 651     return (*func)(exchange_value, dest);
 652   }
 653 
 654   int32_t old_value = *dest;
 655   *dest = exchange_value;
 656   return (old_value);
 657 }
 658 
 659 typedef int32_t cmpxchg_func_t(int32_t, int32_t, volatile int32_t*);
 660 
 661 cmpxchg_func_t* os::atomic_cmpxchg_func = os::atomic_cmpxchg_bootstrap;
 662 
 663 int32_t os::atomic_cmpxchg_bootstrap(int32_t compare_value, int32_t exchange_value, volatile int32_t* dest) {
 664   // try to use the stub:
 665   cmpxchg_func_t* func = CAST_TO_FN_PTR(cmpxchg_func_t*, StubRoutines::atomic_cmpxchg_entry());
 666 
 667   if (func != NULL) {
 668     os::atomic_cmpxchg_func = func;
 669     return (*func)(compare_value, exchange_value, dest);
 670   }
 671   assert(Threads::number_of_threads() == 0, "for bootstrap only");
 672 
 673   int32_t old_value = *dest;
 674   if (old_value == compare_value)
 675     *dest = exchange_value;
 676   return old_value;
 677 }
 678 
 679 
 680 #ifndef PRODUCT
 681 void os::verify_stack_alignment() {
 682 }
 683 #endif
 684 
 685 int os::extra_bang_size_in_bytes() {
 686   // ARM does not require an additional stack bang.
 687   return 0;
 688 }