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