Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_READ_BARRIER_H_ |
| 18 | #define ART_RUNTIME_READ_BARRIER_H_ |
| 19 | |
Andreas Gampe | aea05c1 | 2017-05-19 08:45:02 -0700 | [diff] [blame] | 20 | #include "base/logging.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 21 | #include "base/macros.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "base/mutex.h" |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 23 | #include "gc_root.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 24 | #include "jni.h" |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 25 | #include "mirror/object_reference.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 26 | #include "offsets.h" |
Andreas Gampe | 217488a | 2017-09-18 08:34:42 -0700 | [diff] [blame^] | 27 | #include "read_barrier_config.h" |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 28 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | namespace mirror { |
| 31 | class Object; |
| 32 | template<typename MirrorType> class HeapReference; |
| 33 | } // namespace mirror |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | class ArtMethod; |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 35 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 36 | class ReadBarrier { |
| 37 | public: |
Andreas Gampe | aea05c1 | 2017-05-19 08:45:02 -0700 | [diff] [blame] | 38 | // Enable the to-space invariant checks. This is slow and happens very often. Do not enable in |
| 39 | // fast-debug environment. |
| 40 | DECLARE_RUNTIME_DEBUG_FLAG(kEnableToSpaceInvariantChecks); |
| 41 | |
| 42 | // Enable the read barrier checks. This is slow and happens very often. Do not enable in |
| 43 | // fast-debug environment. |
| 44 | DECLARE_RUNTIME_DEBUG_FLAG(kEnableReadBarrierInvariantChecks); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 45 | |
Hans Boehm | cc55e1d | 2017-07-27 15:28:07 -0700 | [diff] [blame] | 46 | // Return the reference at ref_addr, invoking read barrier as appropriate. |
| 47 | // Ref_addr is an address within obj. |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 48 | // It's up to the implementation whether the given field gets updated whereas the return value |
| 49 | // must be an updated reference unless kAlwaysUpdateField is true. |
Hans Boehm | cc55e1d | 2017-07-27 15:28:07 -0700 | [diff] [blame] | 50 | template <typename MirrorType, |
| 51 | bool kIsVolatile, |
| 52 | ReadBarrierOption kReadBarrierOption = kWithReadBarrier, |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 53 | bool kAlwaysUpdateField = false> |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 54 | ALWAYS_INLINE static MirrorType* Barrier( |
| 55 | mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 56 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 57 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 58 | // It's up to the implementation whether the given root gets updated |
| 59 | // whereas the return value must be an updated reference. |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 60 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 61 | ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root, |
| 62 | GcRootSource* gc_root_source = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 64 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 65 | // It's up to the implementation whether the given root gets updated |
| 66 | // whereas the return value must be an updated reference. |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 67 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 68 | ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root, |
| 69 | GcRootSource* gc_root_source = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 71 | |
Nicolas Geoffray | 13056a1 | 2017-05-11 11:48:28 +0000 | [diff] [blame] | 72 | // Return the mirror Object if it is marked, or null if not. |
| 73 | template <typename MirrorType> |
| 74 | ALWAYS_INLINE static MirrorType* IsMarked(MirrorType* ref) |
| 75 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 76 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 77 | static bool IsDuringStartup(); |
| 78 | |
| 79 | // Without the holder object. |
| 80 | static void AssertToSpaceInvariant(mirror::Object* ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 81 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 82 | AssertToSpaceInvariant(nullptr, MemberOffset(0), ref); |
| 83 | } |
| 84 | // With the holder object. |
| 85 | static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, |
| 86 | mirror::Object* ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 87 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 88 | // With GcRootSource. |
| 89 | static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 91 | |
Hiroshi Yamauchi | 2e5de78 | 2016-01-29 12:06:36 -0800 | [diff] [blame] | 92 | // ALWAYS_INLINE on this caused a performance regression b/26744236. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 93 | static mirror::Object* Mark(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 94 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 95 | static constexpr uint32_t WhiteState() { |
| 96 | return white_state_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 97 | } |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 98 | static constexpr uint32_t GrayState() { |
| 99 | return gray_state_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 102 | // fake_address_dependency will be zero which should be bitwise-or'ed with the address of the |
| 103 | // subsequent load to prevent the reordering of the read barrier bit load and the subsequent |
| 104 | // object reference load (from one of `obj`'s fields). |
| 105 | // *fake_address_dependency will be set to 0. |
| 106 | ALWAYS_INLINE static bool IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 107 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 108 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 109 | // This uses a load-acquire to load the read barrier bit internally to prevent the reordering of |
| 110 | // the read barrier bit load and the subsequent load. |
| 111 | ALWAYS_INLINE static bool IsGray(mirror::Object* obj) |
| 112 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 113 | |
| 114 | static bool IsValidReadBarrierState(uint32_t rb_state) { |
| 115 | return rb_state == white_state_ || rb_state == gray_state_; |
| 116 | } |
| 117 | |
| 118 | static constexpr uint32_t white_state_ = 0x0; // Not marked. |
| 119 | static constexpr uint32_t gray_state_ = 0x1; // Marked, but not marked through. On mark stack. |
| 120 | static constexpr uint32_t rb_state_mask_ = 0x1; // The low bits for white|gray. |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 121 | }; |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 122 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 123 | } // namespace art |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 124 | |
| 125 | #endif // ART_RUNTIME_READ_BARRIER_H_ |