interfaceSupport.hpp revision 11658:8a5735c11a84
174532Sru/*
21022Sache * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3319119Sngie * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
479111Sjoerg *
574532Sru * This code is free software; you can redistribute it and/or modify it
6326918Scy * under the terms of the GNU General Public License version 2 only, as
779111Sjoerg * published by the Free Software Foundation.
874532Sru *
9319119Sngie * This code is distributed in the hope that it will be useful, but WITHOUT
101022Sache * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11127522Snyan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12127522Snyan * version 2 for more details (a copy is included in the LICENSE file that
13127522Snyan * accompanied this code).
14127522Snyan *
151022Sache * 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#ifndef SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP
26#define SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP
27
28#include "gc/shared/gcLocker.hpp"
29#include "runtime/handles.inline.hpp"
30#include "runtime/mutexLocker.hpp"
31#include "runtime/orderAccess.hpp"
32#include "runtime/os.hpp"
33#include "runtime/safepoint.hpp"
34#include "runtime/thread.inline.hpp"
35#include "runtime/vmThread.hpp"
36#include "utilities/globalDefinitions.hpp"
37#include "utilities/macros.hpp"
38#include "utilities/preserveException.hpp"
39
40// Wrapper for all entry points to the virtual machine.
41// The HandleMarkCleaner is a faster version of HandleMark.
42// It relies on the fact that there is a HandleMark further
43// down the stack (in JavaCalls::call_helper), and just resets
44// to the saved values in that HandleMark.
45
46class HandleMarkCleaner: public StackObj {
47 private:
48  Thread* _thread;
49 public:
50  HandleMarkCleaner(Thread* thread) {
51    _thread = thread;
52    _thread->last_handle_mark()->push();
53  }
54  ~HandleMarkCleaner() {
55    _thread->last_handle_mark()->pop_and_restore();
56  }
57
58 private:
59  inline void* operator new(size_t size, void* ptr) throw() {
60    return ptr;
61  }
62};
63
64// InterfaceSupport provides functionality used by the VM_LEAF_BASE and
65// VM_ENTRY_BASE macros. These macros are used to guard entry points into
66// the VM and perform checks upon leave of the VM.
67
68
69class InterfaceSupport: AllStatic {
70# ifdef ASSERT
71 public:
72  static long _scavenge_alot_counter;
73  static long _fullgc_alot_counter;
74  static long _number_of_calls;
75  static long _fullgc_alot_invocation;
76
77  // Helper methods used to implement +ScavengeALot and +FullGCALot
78  static void check_gc_alot() { if (ScavengeALot || FullGCALot) gc_alot(); }
79  static void gc_alot();
80
81  static void walk_stack_from(vframe* start_vf);
82  static void walk_stack();
83
84  static void zombieAll();
85  static void unlinkSymbols();
86  static void deoptimizeAll();
87  static void stress_derived_pointers();
88  static void verify_stack();
89  static void verify_last_frame();
90# endif
91
92 public:
93  // OS dependent stuff
94
95#include OS_HEADER(interfaceSupport)
96
97};
98
99
100// Basic class for all thread transition classes.
101
102class ThreadStateTransition : public StackObj {
103 protected:
104  JavaThread* _thread;
105 public:
106  ThreadStateTransition(JavaThread *thread) {
107    _thread = thread;
108    assert(thread != NULL && thread->is_Java_thread(), "must be Java thread");
109  }
110
111  // Change threadstate in a manner, so safepoint can detect changes.
112  // Time-critical: called on exit from every runtime routine
113  static inline void transition(JavaThread *thread, JavaThreadState from, JavaThreadState to) {
114    assert(from != _thread_in_Java, "use transition_from_java");
115    assert(from != _thread_in_native, "use transition_from_native");
116    assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states");
117    assert(thread->thread_state() == from, "coming from wrong thread state");
118    // Change to transition state (assumes total store ordering!  -Urs)
119    thread->set_thread_state((JavaThreadState)(from + 1));
120
121    // Make sure new state is seen by VM thread
122    if (os::is_MP()) {
123      if (UseMembar) {
124        // Force a fence between the write above and read below
125        OrderAccess::fence();
126      } else {
127        // store to serialize page so VM thread can do pseudo remote membar
128        os::write_memory_serialize_page(thread);
129      }
130    }
131
132    if (SafepointSynchronize::do_call_back()) {
133      SafepointSynchronize::block(thread);
134    }
135    thread->set_thread_state(to);
136
137    CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
138  }
139
140  // transition_and_fence must be used on any thread state transition
141  // where there might not be a Java call stub on the stack, in
142  // particular on Windows where the Structured Exception Handler is
143  // set up in the call stub. os::write_memory_serialize_page() can
144  // fault and we can't recover from it on Windows without a SEH in
145  // place.
146  static inline void transition_and_fence(JavaThread *thread, JavaThreadState from, JavaThreadState to) {
147    assert(thread->thread_state() == from, "coming from wrong thread state");
148    assert((from & 1) == 0 && (to & 1) == 0, "odd numbers are transitions states");
149    // Change to transition state (assumes total store ordering!  -Urs)
150    thread->set_thread_state((JavaThreadState)(from + 1));
151
152    // Make sure new state is seen by VM thread
153    if (os::is_MP()) {
154      if (UseMembar) {
155        // Force a fence between the write above and read below
156        OrderAccess::fence();
157      } else {
158        // Must use this rather than serialization page in particular on Windows
159        InterfaceSupport::serialize_memory(thread);
160      }
161    }
162
163    if (SafepointSynchronize::do_call_back()) {
164      SafepointSynchronize::block(thread);
165    }
166    thread->set_thread_state(to);
167
168    CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
169  }
170
171  // Same as above, but assumes from = _thread_in_Java. This is simpler, since we
172  // never block on entry to the VM. This will break the code, since e.g. preserve arguments
173  // have not been setup.
174  static inline void transition_from_java(JavaThread *thread, JavaThreadState to) {
175    assert(thread->thread_state() == _thread_in_Java, "coming from wrong thread state");
176    thread->set_thread_state(to);
177  }
178
179  static inline void transition_from_native(JavaThread *thread, JavaThreadState to) {
180    assert((to & 1) == 0, "odd numbers are transitions states");
181    assert(thread->thread_state() == _thread_in_native, "coming from wrong thread state");
182    // Change to transition state (assumes total store ordering!  -Urs)
183    thread->set_thread_state(_thread_in_native_trans);
184
185    // Make sure new state is seen by GC thread
186    if (os::is_MP()) {
187      if (UseMembar) {
188        // Force a fence between the write above and read below
189        OrderAccess::fence();
190      } else {
191        // Must use this rather than serialization page in particular on Windows
192        InterfaceSupport::serialize_memory(thread);
193      }
194    }
195
196    // We never install asynchronous exceptions when coming (back) in
197    // to the runtime from native code because the runtime is not set
198    // up to handle exceptions floating around at arbitrary points.
199    if (SafepointSynchronize::do_call_back() || thread->is_suspend_after_native()) {
200      JavaThread::check_safepoint_and_suspend_for_native_trans(thread);
201
202      // Clear unhandled oops anywhere where we could block, even if we don't.
203      CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
204    }
205
206    thread->set_thread_state(to);
207  }
208 protected:
209   void trans(JavaThreadState from, JavaThreadState to)  { transition(_thread, from, to); }
210   void trans_from_java(JavaThreadState to)              { transition_from_java(_thread, to); }
211   void trans_from_native(JavaThreadState to)            { transition_from_native(_thread, to); }
212   void trans_and_fence(JavaThreadState from, JavaThreadState to) { transition_and_fence(_thread, from, to); }
213};
214
215
216class ThreadInVMfromJava : public ThreadStateTransition {
217 public:
218  ThreadInVMfromJava(JavaThread* thread) : ThreadStateTransition(thread) {
219    trans_from_java(_thread_in_vm);
220  }
221  ~ThreadInVMfromJava()  {
222    trans(_thread_in_vm, _thread_in_Java);
223    // Check for pending. async. exceptions or suspends.
224    if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition();
225  }
226};
227
228
229class ThreadInVMfromUnknown {
230 private:
231  JavaThread* _thread;
232 public:
233  ThreadInVMfromUnknown() : _thread(NULL) {
234    Thread* t = Thread::current();
235    if (t->is_Java_thread()) {
236      JavaThread* t2 = (JavaThread*) t;
237      if (t2->thread_state() == _thread_in_native) {
238        _thread = t2;
239        ThreadStateTransition::transition_from_native(t2, _thread_in_vm);
240        // Used to have a HandleMarkCleaner but that is dangerous as
241        // it could free a handle in our (indirect, nested) caller.
242        // We expect any handles will be short lived and figure we
243        // don't need an actual HandleMark.
244      }
245    }
246  }
247  ~ThreadInVMfromUnknown()  {
248    if (_thread) {
249      ThreadStateTransition::transition_and_fence(_thread, _thread_in_vm, _thread_in_native);
250    }
251  }
252};
253
254
255class ThreadInVMfromNative : public ThreadStateTransition {
256 public:
257  ThreadInVMfromNative(JavaThread* thread) : ThreadStateTransition(thread) {
258    trans_from_native(_thread_in_vm);
259  }
260  ~ThreadInVMfromNative() {
261    trans_and_fence(_thread_in_vm, _thread_in_native);
262  }
263};
264
265
266class ThreadToNativeFromVM : public ThreadStateTransition {
267 public:
268  ThreadToNativeFromVM(JavaThread *thread) : ThreadStateTransition(thread) {
269    // We are leaving the VM at this point and going directly to native code.
270    // Block, if we are in the middle of a safepoint synchronization.
271    assert(!thread->owns_locks(), "must release all locks when leaving VM");
272    thread->frame_anchor()->make_walkable(thread);
273    trans_and_fence(_thread_in_vm, _thread_in_native);
274    // Check for pending. async. exceptions or suspends.
275    if (_thread->has_special_runtime_exit_condition()) _thread->handle_special_runtime_exit_condition(false);
276  }
277
278  ~ThreadToNativeFromVM() {
279    trans_from_native(_thread_in_vm);
280    // We don't need to clear_walkable because it will happen automagically when we return to java
281  }
282};
283
284
285class ThreadBlockInVM : public ThreadStateTransition {
286 public:
287  ThreadBlockInVM(JavaThread *thread)
288  : ThreadStateTransition(thread) {
289    // Once we are blocked vm expects stack to be walkable
290    thread->frame_anchor()->make_walkable(thread);
291    trans_and_fence(_thread_in_vm, _thread_blocked);
292  }
293  ~ThreadBlockInVM() {
294    trans_and_fence(_thread_blocked, _thread_in_vm);
295    // We don't need to clear_walkable because it will happen automagically when we return to java
296  }
297};
298
299
300// This special transition class is only used to prevent asynchronous exceptions
301// from being installed on vm exit in situations where we can't tolerate them.
302// See bugs: 4324348, 4854693, 4998314, 5040492, 5050705.
303class ThreadInVMfromJavaNoAsyncException : public ThreadStateTransition {
304 public:
305  ThreadInVMfromJavaNoAsyncException(JavaThread* thread) : ThreadStateTransition(thread) {
306    trans_from_java(_thread_in_vm);
307  }
308  ~ThreadInVMfromJavaNoAsyncException()  {
309    trans(_thread_in_vm, _thread_in_Java);
310    // NOTE: We do not check for pending. async. exceptions.
311    // If we did and moved the pending async exception over into the
312    // pending exception field, we would need to deopt (currently C2
313    // only). However, to do so would require that we transition back
314    // to the _thread_in_vm state. Instead we postpone the handling of
315    // the async exception.
316
317    // Check for pending. suspends only.
318    if (_thread->has_special_runtime_exit_condition())
319      _thread->handle_special_runtime_exit_condition(false);
320  }
321};
322
323// Debug class instantiated in JRT_ENTRY and ITR_ENTRY macro.
324// Can be used to verify properties on enter/exit of the VM.
325
326#ifdef ASSERT
327class VMEntryWrapper {
328 public:
329  VMEntryWrapper() {
330    if (VerifyLastFrame) {
331      InterfaceSupport::verify_last_frame();
332    }
333  }
334
335  ~VMEntryWrapper() {
336    InterfaceSupport::check_gc_alot();
337    if (WalkStackALot) {
338      InterfaceSupport::walk_stack();
339    }
340#ifdef COMPILER2
341    // This option is not used by Compiler 1
342    if (StressDerivedPointers) {
343      InterfaceSupport::stress_derived_pointers();
344    }
345#endif
346    if (DeoptimizeALot || DeoptimizeRandom) {
347      InterfaceSupport::deoptimizeAll();
348    }
349    if (ZombieALot) {
350      InterfaceSupport::zombieAll();
351    }
352    if (UnlinkSymbolsALot) {
353      InterfaceSupport::unlinkSymbols();
354    }
355    // do verification AFTER potential deoptimization
356    if (VerifyStack) {
357      InterfaceSupport::verify_stack();
358    }
359
360  }
361};
362
363
364class VMNativeEntryWrapper {
365 public:
366  VMNativeEntryWrapper() {
367    if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
368  }
369
370  ~VMNativeEntryWrapper() {
371    if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
372  }
373};
374
375#endif
376
377
378// VM-internal runtime interface support
379
380#ifdef ASSERT
381
382class RuntimeHistogramElement : public HistogramElement {
383  public:
384   RuntimeHistogramElement(const char* name);
385};
386
387#define TRACE_CALL(result_type, header)                            \
388  InterfaceSupport::_number_of_calls++;                            \
389  if (CountRuntimeCalls) {                                         \
390    static RuntimeHistogramElement* e = new RuntimeHistogramElement(#header); \
391    if (e != NULL) e->increment_count();                           \
392  }
393#else
394#define TRACE_CALL(result_type, header)                            \
395  /* do nothing */
396#endif
397
398
399// LEAF routines do not lock, GC or throw exceptions
400
401#define VM_LEAF_BASE(result_type, header)                            \
402  TRACE_CALL(result_type, header)                                    \
403  debug_only(NoHandleMark __hm;)                                     \
404  os::verify_stack_alignment();                                      \
405  /* begin of body */
406
407#define VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread)         \
408  TRACE_CALL(result_type, header)                                    \
409  debug_only(ResetNoHandleMark __rnhm;)                              \
410  HandleMarkCleaner __hm(thread);                                    \
411  Thread* THREAD = thread;                                           \
412  os::verify_stack_alignment();                                      \
413  /* begin of body */
414
415
416// ENTRY routines may lock, GC and throw exceptions
417
418#define VM_ENTRY_BASE(result_type, header, thread)                   \
419  TRACE_CALL(result_type, header)                                    \
420  HandleMarkCleaner __hm(thread);                                    \
421  Thread* THREAD = thread;                                           \
422  os::verify_stack_alignment();                                      \
423  /* begin of body */
424
425
426// QUICK_ENTRY routines behave like ENTRY but without a handle mark
427
428#define VM_QUICK_ENTRY_BASE(result_type, header, thread)             \
429  TRACE_CALL(result_type, header)                                    \
430  debug_only(NoHandleMark __hm;)                                     \
431  Thread* THREAD = thread;                                           \
432  os::verify_stack_alignment();                                      \
433  /* begin of body */
434
435
436// Definitions for IRT (Interpreter Runtime)
437// (thread is an argument passed in to all these routines)
438
439#define IRT_ENTRY(result_type, header)                               \
440  result_type header {                                               \
441    ThreadInVMfromJava __tiv(thread);                                \
442    VM_ENTRY_BASE(result_type, header, thread)                       \
443    debug_only(VMEntryWrapper __vew;)
444
445
446#define IRT_LEAF(result_type, header)                                \
447  result_type header {                                               \
448    VM_LEAF_BASE(result_type, header)                                \
449    debug_only(NoSafepointVerifier __nspv(true);)
450
451
452#define IRT_ENTRY_NO_ASYNC(result_type, header)                      \
453  result_type header {                                               \
454    ThreadInVMfromJavaNoAsyncException __tiv(thread);                \
455    VM_ENTRY_BASE(result_type, header, thread)                       \
456    debug_only(VMEntryWrapper __vew;)
457
458#define IRT_END }
459
460
461// Definitions for JRT (Java (Compiler/Shared) Runtime)
462
463#define JRT_ENTRY(result_type, header)                               \
464  result_type header {                                               \
465    ThreadInVMfromJava __tiv(thread);                                \
466    VM_ENTRY_BASE(result_type, header, thread)                       \
467    debug_only(VMEntryWrapper __vew;)
468
469
470#define JRT_LEAF(result_type, header)                                \
471  result_type header {                                               \
472  VM_LEAF_BASE(result_type, header)                                  \
473  debug_only(JRTLeafVerifier __jlv;)
474
475
476#define JRT_ENTRY_NO_ASYNC(result_type, header)                      \
477  result_type header {                                               \
478    ThreadInVMfromJavaNoAsyncException __tiv(thread);                \
479    VM_ENTRY_BASE(result_type, header, thread)                       \
480    debug_only(VMEntryWrapper __vew;)
481
482// Same as JRT Entry but allows for return value after the safepoint
483// to get back into Java from the VM
484#define JRT_BLOCK_ENTRY(result_type, header)                         \
485  result_type header {                                               \
486    TRACE_CALL(result_type, header)                                  \
487    HandleMarkCleaner __hm(thread);
488
489#define JRT_BLOCK                                                    \
490    {                                                                \
491    ThreadInVMfromJava __tiv(thread);                                \
492    Thread* THREAD = thread;                                         \
493    debug_only(VMEntryWrapper __vew;)
494
495#define JRT_BLOCK_NO_ASYNC                                           \
496    {                                                                \
497    ThreadInVMfromJavaNoAsyncException __tiv(thread);                \
498    Thread* THREAD = thread;                                         \
499    debug_only(VMEntryWrapper __vew;)
500
501#define JRT_BLOCK_END }
502
503#define JRT_END }
504
505// Definitions for JNI
506
507#define JNI_ENTRY(result_type, header)                               \
508    JNI_ENTRY_NO_PRESERVE(result_type, header)                       \
509    WeakPreserveExceptionMark __wem(thread);
510
511#define JNI_ENTRY_NO_PRESERVE(result_type, header)                   \
512extern "C" {                                                         \
513  result_type JNICALL header {                                       \
514    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
515    assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
516    ThreadInVMfromNative __tiv(thread);                              \
517    debug_only(VMNativeEntryWrapper __vew;)                          \
518    VM_ENTRY_BASE(result_type, header, thread)
519
520
521// Ensure that the VMNativeEntryWrapper constructor, which can cause
522// a GC, is called outside the NoHandleMark (set via VM_QUICK_ENTRY_BASE).
523#define JNI_QUICK_ENTRY(result_type, header)                         \
524extern "C" {                                                         \
525  result_type JNICALL header {                                       \
526    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
527    assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
528    ThreadInVMfromNative __tiv(thread);                              \
529    debug_only(VMNativeEntryWrapper __vew;)                          \
530    VM_QUICK_ENTRY_BASE(result_type, header, thread)
531
532
533#define JNI_LEAF(result_type, header)                                \
534extern "C" {                                                         \
535  result_type JNICALL header {                                       \
536    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
537    assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
538    VM_LEAF_BASE(result_type, header)
539
540
541// Close the routine and the extern "C"
542#define JNI_END } }
543
544
545
546// Definitions for JVM
547
548#define JVM_ENTRY(result_type, header)                               \
549extern "C" {                                                         \
550  result_type JNICALL header {                                       \
551    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
552    ThreadInVMfromNative __tiv(thread);                              \
553    debug_only(VMNativeEntryWrapper __vew;)                          \
554    VM_ENTRY_BASE(result_type, header, thread)
555
556
557#define JVM_ENTRY_NO_ENV(result_type, header)                        \
558extern "C" {                                                         \
559  result_type JNICALL header {                                       \
560    JavaThread* thread = JavaThread::current();                      \
561    ThreadInVMfromNative __tiv(thread);                              \
562    debug_only(VMNativeEntryWrapper __vew;)                          \
563    VM_ENTRY_BASE(result_type, header, thread)
564
565
566#define JVM_QUICK_ENTRY(result_type, header)                         \
567extern "C" {                                                         \
568  result_type JNICALL header {                                       \
569    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
570    ThreadInVMfromNative __tiv(thread);                              \
571    debug_only(VMNativeEntryWrapper __vew;)                          \
572    VM_QUICK_ENTRY_BASE(result_type, header, thread)
573
574
575#define JVM_LEAF(result_type, header)                                \
576extern "C" {                                                         \
577  result_type JNICALL header {                                       \
578    VM_Exit::block_if_vm_exited();                                   \
579    VM_LEAF_BASE(result_type, header)
580
581
582#define JVM_ENTRY_FROM_LEAF(env, result_type, header)                \
583  { {                                                                \
584    JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
585    ThreadInVMfromNative __tiv(thread);                              \
586    debug_only(VMNativeEntryWrapper __vew;)                          \
587    VM_ENTRY_BASE_FROM_LEAF(result_type, header, thread)
588
589
590#define JVM_END } }
591
592#endif // SHARE_VM_RUNTIME_INTERFACESUPPORT_HPP
593