Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | // Copyright 2014, VIXL authors |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 | // used to endorse or promote products derived from this software without |
| 14 | // specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame^] | 27 | extern "C" { |
| 28 | #include <sys/mman.h> |
| 29 | } |
| 30 | |
Alexandre Rames | 1f9074d | 2016-05-23 15:50:01 +0100 | [diff] [blame] | 31 | #include "code-buffer-vixl.h" |
| 32 | #include "utils-vixl.h" |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 33 | |
| 34 | namespace vixl { |
| 35 | |
| 36 | |
Alexandre Rames | 7cd99a8 | 2016-08-03 13:32:02 +0100 | [diff] [blame] | 37 | CodeBuffer::CodeBuffer(size_t capacity) |
| 38 | : buffer_(NULL), |
| 39 | managed_(true), |
| 40 | cursor_(NULL), |
| 41 | dirty_(false), |
| 42 | capacity_(capacity) { |
| 43 | if (capacity_ == 0) { |
| 44 | return; |
| 45 | } |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame^] | 46 | buffer_ = reinterpret_cast<byte*>(mmap(NULL, |
| 47 | capacity, |
| 48 | PROT_READ | PROT_WRITE, |
| 49 | MAP_PRIVATE | MAP_ANONYMOUS, |
| 50 | -1, |
| 51 | 0)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 52 | VIXL_CHECK(buffer_ != NULL); |
Alexandre Rames | d383296 | 2016-07-04 15:03:43 +0100 | [diff] [blame] | 53 | // Aarch64 instructions must be word aligned, we assert the default allocator |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 54 | // always returns word align memory. |
| 55 | VIXL_ASSERT(IsWordAligned(buffer_)); |
| 56 | |
| 57 | cursor_ = buffer_; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | CodeBuffer::CodeBuffer(void* buffer, size_t capacity) |
| 62 | : buffer_(reinterpret_cast<byte*>(buffer)), |
| 63 | managed_(false), |
| 64 | cursor_(reinterpret_cast<byte*>(buffer)), |
| 65 | dirty_(false), |
| 66 | capacity_(capacity) { |
| 67 | VIXL_ASSERT(buffer_ != NULL); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | CodeBuffer::~CodeBuffer() { |
| 72 | VIXL_ASSERT(!IsDirty()); |
| 73 | if (managed_) { |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame^] | 74 | munmap(buffer_, capacity_); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame^] | 79 | void CodeBuffer::SetExecutable() { |
| 80 | int ret = mprotect(buffer_, capacity_, PROT_READ | PROT_EXEC); |
| 81 | VIXL_CHECK(ret == 0); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void CodeBuffer::SetWritable() { |
| 86 | int ret = mprotect(buffer_, capacity_, PROT_READ | PROT_WRITE); |
| 87 | VIXL_CHECK(ret == 0); |
| 88 | } |
| 89 | |
| 90 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 91 | void CodeBuffer::EmitString(const char* string) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 92 | VIXL_ASSERT(HasSpaceFor(strlen(string) + 1)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 93 | char* dst = reinterpret_cast<char*>(cursor_); |
| 94 | dirty_ = true; |
| 95 | char* null_char = stpcpy(dst, string); |
| 96 | cursor_ = reinterpret_cast<byte*>(null_char) + 1; |
| 97 | } |
| 98 | |
| 99 | |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 100 | void CodeBuffer::EmitData(const void* data, size_t size) { |
| 101 | VIXL_ASSERT(HasSpaceFor(size)); |
| 102 | dirty_ = true; |
| 103 | memcpy(cursor_, data, size); |
| 104 | cursor_ = cursor_ + size; |
| 105 | } |
| 106 | |
| 107 | |
Vincent Belliard | 3e1b899 | 2016-07-13 16:02:19 -0700 | [diff] [blame] | 108 | void CodeBuffer::UpdateData(size_t offset, const void* data, size_t size) { |
| 109 | dirty_ = true; |
| 110 | byte* dst = buffer_ + offset; |
| 111 | VIXL_ASSERT(dst + size <= cursor_); |
| 112 | memcpy(dst, data, size); |
| 113 | } |
| 114 | |
| 115 | |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 116 | void CodeBuffer::Align() { |
| 117 | byte* end = AlignUp(cursor_, 4); |
| 118 | VIXL_ASSERT(end >= cursor_); |
| 119 | const size_t padding_size = end - cursor_; |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 120 | VIXL_ASSERT(HasSpaceFor(padding_size)); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 121 | VIXL_ASSERT(padding_size <= 4); |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 122 | const byte padding[] = {0, 0, 0, 0}; |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 123 | dirty_ = true; |
| 124 | memcpy(cursor_, padding, padding_size); |
| 125 | cursor_ = end; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void CodeBuffer::Reset() { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 130 | #ifdef VIXL_DEBUG |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 131 | if (managed_) { |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 132 | // Fill with zeros (there is no useful value common to A32 and T32). |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 133 | memset(buffer_, 0, capacity_); |
| 134 | } |
| 135 | #endif |
| 136 | cursor_ = buffer_; |
| 137 | SetClean(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void CodeBuffer::Grow(size_t new_capacity) { |
| 142 | VIXL_ASSERT(managed_); |
| 143 | VIXL_ASSERT(new_capacity > capacity_); |
Pierre Langlois | 88c46b8 | 2016-06-02 18:15:32 +0100 | [diff] [blame] | 144 | size_t size = GetCursorOffset(); |
Alex Gilday | 31dd2ae | 2016-07-05 16:34:41 +0100 | [diff] [blame^] | 145 | buffer_ = static_cast<byte*>( |
| 146 | mremap(buffer_, capacity_, new_capacity, MREMAP_MAYMOVE)); |
| 147 | VIXL_CHECK(buffer_ != MAP_FAILED); |
armvixl | c68cb64 | 2014-09-25 18:49:30 +0100 | [diff] [blame] | 148 | |
| 149 | cursor_ = buffer_ + size; |
| 150 | capacity_ = new_capacity; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | } // namespace vixl |