Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -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 | */ |
| 16 | |
| 17 | #include "instruction_set.h" |
| 18 | |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 19 | // Explicitly include our own elf.h to avoid Linux and other dependencies. |
| 20 | #include "../elf.h" |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 21 | #include "android-base/logging.h" |
Andreas Gampe | d126228 | 2016-08-11 18:35:58 -0700 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame^] | 23 | #include "base/globals.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 27 | void InstructionSetAbort(InstructionSet isa) { |
| 28 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 29 | case InstructionSet::kArm: |
| 30 | case InstructionSet::kThumb2: |
| 31 | case InstructionSet::kArm64: |
| 32 | case InstructionSet::kX86: |
| 33 | case InstructionSet::kX86_64: |
| 34 | case InstructionSet::kMips: |
| 35 | case InstructionSet::kMips64: |
| 36 | case InstructionSet::kNone: |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 37 | LOG(FATAL) << "Unsupported instruction set " << isa; |
| 38 | UNREACHABLE(); |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 39 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 40 | LOG(FATAL) << "Unknown ISA " << isa; |
| 41 | UNREACHABLE(); |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const char* GetInstructionSetString(InstructionSet isa) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 45 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 46 | case InstructionSet::kArm: |
| 47 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 48 | return "arm"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 49 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 50 | return "arm64"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 51 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 52 | return "x86"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 53 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 54 | return "x86_64"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 55 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 56 | return "mips"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 57 | case InstructionSet::kMips64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 58 | return "mips64"; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 59 | case InstructionSet::kNone: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 60 | return "none"; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 61 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 62 | LOG(FATAL) << "Unknown ISA " << isa; |
| 63 | UNREACHABLE(); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | InstructionSet GetInstructionSetFromString(const char* isa_str) { |
| 67 | CHECK(isa_str != nullptr); |
| 68 | |
| 69 | if (strcmp("arm", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 70 | return InstructionSet::kArm; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 71 | } else if (strcmp("arm64", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 72 | return InstructionSet::kArm64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 73 | } else if (strcmp("x86", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 74 | return InstructionSet::kX86; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 75 | } else if (strcmp("x86_64", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 76 | return InstructionSet::kX86_64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 77 | } else if (strcmp("mips", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 78 | return InstructionSet::kMips; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 79 | } else if (strcmp("mips64", isa_str) == 0) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 80 | return InstructionSet::kMips64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 83 | return InstructionSet::kNone; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 86 | InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags) { |
| 87 | switch (e_machine) { |
| 88 | case EM_ARM: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 89 | return InstructionSet::kArm; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 90 | case EM_AARCH64: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 91 | return InstructionSet::kArm64; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 92 | case EM_386: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 93 | return InstructionSet::kX86; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 94 | case EM_X86_64: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 95 | return InstructionSet::kX86_64; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 96 | case EM_MIPS: { |
| 97 | if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 || |
| 98 | (e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 99 | return InstructionSet::kMips; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 100 | } else if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 101 | return InstructionSet::kMips64; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 102 | } |
| 103 | break; |
| 104 | } |
| 105 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 106 | return InstructionSet::kNone; |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 109 | size_t GetInstructionSetAlignment(InstructionSet isa) { |
| 110 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 111 | case InstructionSet::kArm: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 112 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 113 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 114 | return kArmAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 115 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 116 | return kArm64Alignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 117 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 118 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 119 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 120 | return kX86Alignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 121 | case InstructionSet::kMips: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 122 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 123 | case InstructionSet::kMips64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 124 | return kMipsAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 125 | case InstructionSet::kNone: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 126 | LOG(FATAL) << "ISA kNone does not have alignment."; |
| 127 | UNREACHABLE(); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 128 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 129 | LOG(FATAL) << "Unknown ISA " << isa; |
| 130 | UNREACHABLE(); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Andreas Gampe | d126228 | 2016-08-11 18:35:58 -0700 | [diff] [blame] | 133 | #if !defined(ART_STACK_OVERFLOW_GAP_arm) || !defined(ART_STACK_OVERFLOW_GAP_arm64) || \ |
| 134 | !defined(ART_STACK_OVERFLOW_GAP_mips) || !defined(ART_STACK_OVERFLOW_GAP_mips64) || \ |
| 135 | !defined(ART_STACK_OVERFLOW_GAP_x86) || !defined(ART_STACK_OVERFLOW_GAP_x86_64) |
| 136 | #error "Missing defines for stack overflow gap" |
| 137 | #endif |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 138 | |
Andreas Gampe | d126228 | 2016-08-11 18:35:58 -0700 | [diff] [blame] | 139 | static constexpr size_t kArmStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm; |
| 140 | static constexpr size_t kArm64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm64; |
| 141 | static constexpr size_t kMipsStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips; |
| 142 | static constexpr size_t kMips64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips64; |
| 143 | static constexpr size_t kX86StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86; |
| 144 | static constexpr size_t kX86_64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86_64; |
| 145 | |
| 146 | static_assert(IsAligned<kPageSize>(kArmStackOverflowReservedBytes), "ARM gap not page aligned"); |
| 147 | static_assert(IsAligned<kPageSize>(kArm64StackOverflowReservedBytes), "ARM64 gap not page aligned"); |
| 148 | static_assert(IsAligned<kPageSize>(kMipsStackOverflowReservedBytes), "Mips gap not page aligned"); |
| 149 | static_assert(IsAligned<kPageSize>(kMips64StackOverflowReservedBytes), |
| 150 | "Mips64 gap not page aligned"); |
| 151 | static_assert(IsAligned<kPageSize>(kX86StackOverflowReservedBytes), "X86 gap not page aligned"); |
| 152 | static_assert(IsAligned<kPageSize>(kX86_64StackOverflowReservedBytes), |
| 153 | "X86_64 gap not page aligned"); |
| 154 | |
| 155 | #if !defined(ART_FRAME_SIZE_LIMIT) |
| 156 | #error "ART frame size limit missing" |
| 157 | #endif |
| 158 | |
| 159 | // TODO: Should we require an extra page (RoundUp(SIZE) + kPageSize)? |
| 160 | static_assert(ART_FRAME_SIZE_LIMIT < kArmStackOverflowReservedBytes, "Frame size limit too large"); |
| 161 | static_assert(ART_FRAME_SIZE_LIMIT < kArm64StackOverflowReservedBytes, |
| 162 | "Frame size limit too large"); |
| 163 | static_assert(ART_FRAME_SIZE_LIMIT < kMipsStackOverflowReservedBytes, |
| 164 | "Frame size limit too large"); |
| 165 | static_assert(ART_FRAME_SIZE_LIMIT < kMips64StackOverflowReservedBytes, |
| 166 | "Frame size limit too large"); |
| 167 | static_assert(ART_FRAME_SIZE_LIMIT < kX86StackOverflowReservedBytes, |
| 168 | "Frame size limit too large"); |
| 169 | static_assert(ART_FRAME_SIZE_LIMIT < kX86_64StackOverflowReservedBytes, |
| 170 | "Frame size limit too large"); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 171 | |
| 172 | size_t GetStackOverflowReservedBytes(InstructionSet isa) { |
| 173 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 174 | case InstructionSet::kArm: // Intentional fall-through. |
| 175 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 176 | return kArmStackOverflowReservedBytes; |
| 177 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 178 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 179 | return kArm64StackOverflowReservedBytes; |
| 180 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 181 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 182 | return kMipsStackOverflowReservedBytes; |
| 183 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 184 | case InstructionSet::kMips64: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 185 | return kMips64StackOverflowReservedBytes; |
| 186 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 187 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 188 | return kX86StackOverflowReservedBytes; |
| 189 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 190 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 191 | return kX86_64StackOverflowReservedBytes; |
| 192 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 193 | case InstructionSet::kNone: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 194 | LOG(FATAL) << "kNone has no stack overflow size"; |
| 195 | UNREACHABLE(); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 196 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 197 | LOG(FATAL) << "Unknown instruction set" << isa; |
| 198 | UNREACHABLE(); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | } // namespace art |