Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MEMORY_REGION_H_ |
| 18 | #define ART_RUNTIME_MEMORY_REGION_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 21 | #include <type_traits> |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
| 24 | #include "base/bit_utils.h" |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 25 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 28 | #include "base/value_object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "globals.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 30 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 31 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 32 | |
| 33 | // Memory regions are useful for accessing memory with bounds check in |
| 34 | // debug mode. They can be safely passed by value and do not assume ownership |
| 35 | // of the region. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 36 | class MemoryRegion FINAL : public ValueObject { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 37 | public: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 38 | MemoryRegion() : pointer_(nullptr), size_(0) {} |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 39 | MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {} |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 40 | |
| 41 | void* pointer() const { return pointer_; } |
| 42 | size_t size() const { return size_; } |
| 43 | size_t size_in_bits() const { return size_ * kBitsPerByte; } |
| 44 | |
| 45 | static size_t pointer_offset() { |
| 46 | return OFFSETOF_MEMBER(MemoryRegion, pointer_); |
| 47 | } |
| 48 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | uint8_t* start() const { return reinterpret_cast<uint8_t*>(pointer_); } |
| 50 | uint8_t* end() const { return start() + size_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 52 | // Load value of type `T` at `offset`. The memory address corresponding |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 53 | // to `offset` should be word-aligned (on ARM, this is a requirement). |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 54 | template<typename T> |
| 55 | ALWAYS_INLINE T Load(uintptr_t offset) const { |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 56 | T* address = ComputeInternalPointer<T>(offset); |
| 57 | DCHECK(IsWordAligned(address)); |
| 58 | return *address; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 61 | // Store `value` (of type `T`) at `offset`. The memory address |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 62 | // corresponding to `offset` should be word-aligned (on ARM, this is |
| 63 | // a requirement). |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 64 | template<typename T> |
| 65 | ALWAYS_INLINE void Store(uintptr_t offset, T value) const { |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 66 | T* address = ComputeInternalPointer<T>(offset); |
| 67 | DCHECK(IsWordAligned(address)); |
| 68 | *address = value; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 71 | // Load value of type `T` at `offset`. The memory address corresponding |
| 72 | // to `offset` does not need to be word-aligned. |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 73 | template<typename T> |
| 74 | ALWAYS_INLINE T LoadUnaligned(uintptr_t offset) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 75 | // Equivalent unsigned integer type corresponding to T. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 76 | typedef typename std::make_unsigned<T>::type U; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 77 | U equivalent_unsigned_integer_value = 0; |
| 78 | // Read the value byte by byte in a little-endian fashion. |
| 79 | for (size_t i = 0; i < sizeof(U); ++i) { |
| 80 | equivalent_unsigned_integer_value += |
| 81 | *ComputeInternalPointer<uint8_t>(offset + i) << (i * kBitsPerByte); |
| 82 | } |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 83 | return bit_cast<T, U>(equivalent_unsigned_integer_value); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Store `value` (of type `T`) at `offset`. The memory address |
| 87 | // corresponding to `offset` does not need to be word-aligned. |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 88 | template<typename T> |
| 89 | ALWAYS_INLINE void StoreUnaligned(uintptr_t offset, T value) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 90 | // Equivalent unsigned integer type corresponding to T. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 91 | typedef typename std::make_unsigned<T>::type U; |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 92 | U equivalent_unsigned_integer_value = bit_cast<U, T>(value); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 93 | // Write the value byte by byte in a little-endian fashion. |
| 94 | for (size_t i = 0; i < sizeof(U); ++i) { |
| 95 | *ComputeInternalPointer<uint8_t>(offset + i) = |
| 96 | (equivalent_unsigned_integer_value >> (i * kBitsPerByte)) & 0xFF; |
| 97 | } |
| 98 | } |
| 99 | |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 100 | template<typename T> |
| 101 | ALWAYS_INLINE T* PointerTo(uintptr_t offset) const { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 102 | return ComputeInternalPointer<T>(offset); |
| 103 | } |
| 104 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 105 | // Load a single bit in the region. The bit at offset 0 is the least |
| 106 | // significant bit in the first byte. |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 107 | ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 108 | uint8_t bit_mask; |
| 109 | uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask); |
| 110 | return byte & bit_mask; |
| 111 | } |
| 112 | |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 113 | ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 114 | uint8_t bit_mask; |
| 115 | uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask); |
| 116 | if (value) { |
| 117 | *byte |= bit_mask; |
| 118 | } else { |
| 119 | *byte &= ~bit_mask; |
| 120 | } |
| 121 | } |
| 122 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 123 | // Load `length` bits from the region starting at bit offset `bit_offset`. |
| 124 | // The bit at the smallest offset is the least significant bit in the |
| 125 | // loaded value. `length` must not be larger than the number of bits |
| 126 | // contained in the return value (32). |
| 127 | uint32_t LoadBits(uintptr_t bit_offset, size_t length) const { |
| 128 | CHECK_LE(length, sizeof(uint32_t) * kBitsPerByte); |
| 129 | uint32_t value = 0u; |
| 130 | for (size_t i = 0; i < length; ++i) { |
| 131 | value |= LoadBit(bit_offset + i) << i; |
| 132 | } |
| 133 | return value; |
| 134 | } |
| 135 | |
| 136 | // Store `value` on `length` bits in the region starting at bit offset |
| 137 | // `bit_offset`. The bit at the smallest offset is the least significant |
| 138 | // bit of the stored `value`. `value` must not be larger than `length` |
| 139 | // bits. |
| 140 | void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length) { |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 141 | CHECK_LE(value, MaxInt<uint32_t>(length)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 142 | for (size_t i = 0; i < length; ++i) { |
| 143 | bool ith_bit = value & (1 << i); |
| 144 | StoreBit(bit_offset + i, ith_bit); |
| 145 | } |
| 146 | } |
| 147 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 148 | void CopyFrom(size_t offset, const MemoryRegion& from) const; |
| 149 | |
| 150 | // Compute a sub memory region based on an existing one. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 151 | MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const { |
| 152 | CHECK_GE(this->size(), size_in); |
| 153 | CHECK_LE(offset, this->size() - size_in); |
| 154 | return MemoryRegion(reinterpret_cast<void*>(start() + offset), size_in); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Compute an extended memory region based on an existing one. |
| 158 | void Extend(const MemoryRegion& region, uintptr_t extra) { |
| 159 | pointer_ = region.pointer(); |
| 160 | size_ = (region.size() + extra); |
| 161 | } |
| 162 | |
| 163 | private: |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 164 | template<typename T> |
| 165 | ALWAYS_INLINE T* ComputeInternalPointer(size_t offset) const { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 166 | CHECK_GE(size(), sizeof(T)); |
| 167 | CHECK_LE(offset, size() - sizeof(T)); |
| 168 | return reinterpret_cast<T*>(start() + offset); |
| 169 | } |
| 170 | |
| 171 | // Locate the bit with the given offset. Returns a pointer to the byte |
| 172 | // containing the bit, and sets bit_mask to the bit within that byte. |
David Brazdil | b765683 | 2015-03-30 10:08:19 +0100 | [diff] [blame] | 173 | ALWAYS_INLINE uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 174 | uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1)); |
| 175 | *bit_mask = (1U << bit_remainder); |
| 176 | uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 177 | return ComputeInternalPointer<uint8_t>(byte_offset); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 180 | // Is `address` aligned on a machine word? |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 181 | template<typename T> static constexpr bool IsWordAligned(const T* address) { |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 182 | // Word alignment in bytes. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 183 | size_t kWordAlignment = static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)); |
Roland Levillain | bdba92d | 2015-03-31 12:27:44 +0100 | [diff] [blame] | 184 | return IsAlignedParam(address, kWordAlignment); |
| 185 | } |
| 186 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 187 | void* pointer_; |
| 188 | size_t size_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 191 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 193 | #endif // ART_RUNTIME_MEMORY_REGION_H_ |