1 /*
  2  * Copyright (c) 2015, 2021, 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 #ifndef SHARE_GC_Z_ZBARRIER_HPP
 25 #define SHARE_GC_Z_ZBARRIER_HPP
 26 
 27 #include "memory/allocation.hpp"
 28 #include "memory/iterator.hpp"
 29 #include "oops/oop.hpp"
 30 #include "oops/inlineKlass.hpp"
 31 
 32 typedef bool (*ZBarrierFastPath)(uintptr_t);
 33 typedef uintptr_t (*ZBarrierSlowPath)(uintptr_t);
 34 
 35 class ZBarrier : public AllStatic {
 36 private:
 37   static const bool GCThread    = true;
 38   static const bool AnyThread   = false;
 39 
 40   static const bool Follow      = true;
 41   static const bool DontFollow  = false;
 42 
 43   static const bool Strong      = false;
 44   static const bool Finalizable = true;
 45 
 46   static const bool Publish     = true;
 47   static const bool Overflow    = false;
 48 
 49   template <ZBarrierFastPath fast_path> static void self_heal(volatile oop* p, uintptr_t addr, uintptr_t heal_addr);
 50 
 51   template <ZBarrierFastPath fast_path, ZBarrierSlowPath slow_path> static oop barrier(volatile oop* p, oop o);
 52   template <ZBarrierFastPath fast_path, ZBarrierSlowPath slow_path> static oop weak_barrier(volatile oop* p, oop o);
 53   template <ZBarrierFastPath fast_path, ZBarrierSlowPath slow_path> static void root_barrier(oop* p, oop o);
 54 
 55   static bool is_good_or_null_fast_path(uintptr_t addr);
 56   static bool is_weak_good_or_null_fast_path(uintptr_t addr);
 57   static bool is_marked_or_null_fast_path(uintptr_t addr);
 58 
 59   static bool during_mark();
 60   static bool during_relocate();
 61   template <bool finalizable> static bool should_mark_through(uintptr_t addr);
 62   template <bool gc_thread, bool follow, bool finalizable, bool publish> static uintptr_t mark(uintptr_t addr);
 63   static uintptr_t remap(uintptr_t addr);
 64   static uintptr_t relocate(uintptr_t addr);
 65   static uintptr_t relocate_or_mark(uintptr_t addr);
 66   static uintptr_t relocate_or_mark_no_follow(uintptr_t addr);
 67   static uintptr_t relocate_or_remap(uintptr_t addr);
 68 
 69   static uintptr_t load_barrier_on_oop_slow_path(uintptr_t addr);
 70   static uintptr_t load_barrier_on_invisible_root_oop_slow_path(uintptr_t addr);
 71 
 72   static uintptr_t weak_load_barrier_on_oop_slow_path(uintptr_t addr);
 73   static uintptr_t weak_load_barrier_on_weak_oop_slow_path(uintptr_t addr);
 74   static uintptr_t weak_load_barrier_on_phantom_oop_slow_path(uintptr_t addr);
 75 
 76   static uintptr_t keep_alive_barrier_on_oop_slow_path(uintptr_t addr);
 77   static uintptr_t keep_alive_barrier_on_weak_oop_slow_path(uintptr_t addr);
 78   static uintptr_t keep_alive_barrier_on_phantom_oop_slow_path(uintptr_t addr);
 79 
 80   static uintptr_t mark_barrier_on_oop_slow_path(uintptr_t addr);
 81   static uintptr_t mark_barrier_on_finalizable_oop_slow_path(uintptr_t addr);
 82 
 83   static void verify_on_weak(volatile oop* referent_addr) NOT_DEBUG_RETURN;
 84 
 85 public:
 86   // Load barrier
 87   static  oop load_barrier_on_oop(oop o);
 88   static  oop load_barrier_on_oop_field(volatile oop* p);
 89   static  oop load_barrier_on_oop_field_preloaded(volatile oop* p, oop o);
 90   static void load_barrier_on_oop_array(volatile oop* p, size_t length);
 91   static void load_barrier_on_oop_fields(oop o);
 92   static  oop load_barrier_on_weak_oop_field_preloaded(volatile oop* p, oop o);
 93   static  oop load_barrier_on_phantom_oop_field_preloaded(volatile oop* p, oop o);
 94   static void load_barrier_on_root_oop_field(oop* p);
 95   static void load_barrier_on_invisible_root_oop_field(oop* p);
 96 
 97   // Weak load barrier
 98   static oop weak_load_barrier_on_oop_field(volatile oop* p);
 99   static oop weak_load_barrier_on_oop_field_preloaded(volatile oop* p, oop o);
100   static oop weak_load_barrier_on_weak_oop(oop o);
101   static oop weak_load_barrier_on_weak_oop_field_preloaded(volatile oop* p, oop o);
102   static oop weak_load_barrier_on_phantom_oop(oop o);
103   static oop weak_load_barrier_on_phantom_oop_field_preloaded(volatile oop* p, oop o);
104 
105   // Is alive barrier
106   static bool is_alive_barrier_on_weak_oop(oop o);
107   static bool is_alive_barrier_on_phantom_oop(oop o);
108 
109   // Keep alive barrier
110   static void keep_alive_barrier_on_oop(oop o);
111   static void keep_alive_barrier_on_weak_oop_field(volatile oop* p);
112   static void keep_alive_barrier_on_phantom_oop_field(volatile oop* p);
113   static void keep_alive_barrier_on_phantom_root_oop_field(oop* p);
114 
115   // Mark barrier
116   static void mark_barrier_on_oop_field(volatile oop* p, bool finalizable);
117   static void mark_barrier_on_oop_array(volatile oop* p, size_t length, bool finalizable);
118 
119   // Narrow oop variants, never used.
120   static oop  load_barrier_on_oop_field(volatile narrowOop* p);
121   static oop  load_barrier_on_oop_field_preloaded(volatile narrowOop* p, oop o);
122   static void load_barrier_on_oop_array(volatile narrowOop* p, size_t length);
123   static oop  load_barrier_on_weak_oop_field_preloaded(volatile narrowOop* p, oop o);
124   static oop  load_barrier_on_phantom_oop_field_preloaded(volatile narrowOop* p, oop o);
125   static oop  weak_load_barrier_on_oop_field_preloaded(volatile narrowOop* p, oop o);
126   static oop  weak_load_barrier_on_weak_oop_field_preloaded(volatile narrowOop* p, oop o);
127   static oop  weak_load_barrier_on_phantom_oop_field_preloaded(volatile narrowOop* p, oop o);
128 };
129 
130 class ZLoadBarrierOopClosure : public BasicOopIterateClosure {
131 public:
132   virtual void do_oop(oop* p);
133   virtual void do_oop(narrowOop* p);
134 };
135 
136 #endif // SHARE_GC_Z_ZBARRIER_HPP