xref: /aosp_15_r20/art/libartbase/arch/instruction_set.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "instruction_set.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
20*795d594fSAndroid Build Coastguard Worker #include "android-base/properties.h"
21*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
InstructionSetAbort(InstructionSet isa)27*795d594fSAndroid Build Coastguard Worker void InstructionSetAbort(InstructionSet isa) {
28*795d594fSAndroid Build Coastguard Worker   switch (isa) {
29*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm:
30*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kThumb2:
31*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm64:
32*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kRiscv64:
33*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86:
34*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86_64:
35*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kNone:
36*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Unsupported instruction set " << isa;
37*795d594fSAndroid Build Coastguard Worker       UNREACHABLE();
38*795d594fSAndroid Build Coastguard Worker   }
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker 
GetInstructionSetString(InstructionSet isa)41*795d594fSAndroid Build Coastguard Worker const char* GetInstructionSetString(InstructionSet isa) {
42*795d594fSAndroid Build Coastguard Worker   switch (isa) {
43*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm:
44*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kThumb2:
45*795d594fSAndroid Build Coastguard Worker       return "arm";
46*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm64:
47*795d594fSAndroid Build Coastguard Worker       return "arm64";
48*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kRiscv64:
49*795d594fSAndroid Build Coastguard Worker       return "riscv64";
50*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86:
51*795d594fSAndroid Build Coastguard Worker       return "x86";
52*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86_64:
53*795d594fSAndroid Build Coastguard Worker       return "x86_64";
54*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kNone:
55*795d594fSAndroid Build Coastguard Worker       return "none";
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker }
58*795d594fSAndroid Build Coastguard Worker 
GetInstructionSetFromString(const char * isa_str)59*795d594fSAndroid Build Coastguard Worker InstructionSet GetInstructionSetFromString(const char* isa_str) {
60*795d594fSAndroid Build Coastguard Worker   CHECK(isa_str != nullptr);
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker   if (strcmp("arm", isa_str) == 0) {
63*795d594fSAndroid Build Coastguard Worker     return InstructionSet::kArm;
64*795d594fSAndroid Build Coastguard Worker   } else if (strcmp("arm64", isa_str) == 0) {
65*795d594fSAndroid Build Coastguard Worker     return InstructionSet::kArm64;
66*795d594fSAndroid Build Coastguard Worker   } else if (strcmp("riscv64", isa_str) == 0) {
67*795d594fSAndroid Build Coastguard Worker     return InstructionSet::kRiscv64;
68*795d594fSAndroid Build Coastguard Worker   } else if (strcmp("x86", isa_str) == 0) {
69*795d594fSAndroid Build Coastguard Worker     return InstructionSet::kX86;
70*795d594fSAndroid Build Coastguard Worker   } else if (strcmp("x86_64", isa_str) == 0) {
71*795d594fSAndroid Build Coastguard Worker     return InstructionSet::kX86_64;
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   return InstructionSet::kNone;
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
GetSupportedInstructionSets(std::string * error_msg)77*795d594fSAndroid Build Coastguard Worker std::vector<InstructionSet> GetSupportedInstructionSets(std::string* error_msg) {
78*795d594fSAndroid Build Coastguard Worker   std::string zygote_kinds = android::base::GetProperty("ro.zygote", {});
79*795d594fSAndroid Build Coastguard Worker   if (zygote_kinds.empty()) {
80*795d594fSAndroid Build Coastguard Worker     *error_msg = "Unable to get Zygote kinds";
81*795d594fSAndroid Build Coastguard Worker     return {};
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker   switch (kRuntimeISA) {
85*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm:
86*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm64:
87*795d594fSAndroid Build Coastguard Worker       if (zygote_kinds == "zygote64_32" || zygote_kinds == "zygote32_64") {
88*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kArm64, InstructionSet::kArm};
89*795d594fSAndroid Build Coastguard Worker       } else if (zygote_kinds == "zygote64") {
90*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kArm64};
91*795d594fSAndroid Build Coastguard Worker       } else if (zygote_kinds == "zygote32") {
92*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kArm};
93*795d594fSAndroid Build Coastguard Worker       } else {
94*795d594fSAndroid Build Coastguard Worker         *error_msg = android::base::StringPrintf("Unknown Zygote kinds '%s'", zygote_kinds.c_str());
95*795d594fSAndroid Build Coastguard Worker         return {};
96*795d594fSAndroid Build Coastguard Worker       }
97*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kRiscv64:
98*795d594fSAndroid Build Coastguard Worker       return {InstructionSet::kRiscv64};
99*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86:
100*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86_64:
101*795d594fSAndroid Build Coastguard Worker       if (zygote_kinds == "zygote64_32" || zygote_kinds == "zygote32_64") {
102*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kX86_64, InstructionSet::kX86};
103*795d594fSAndroid Build Coastguard Worker       } else if (zygote_kinds == "zygote64") {
104*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kX86_64};
105*795d594fSAndroid Build Coastguard Worker       } else if (zygote_kinds == "zygote32") {
106*795d594fSAndroid Build Coastguard Worker         return {InstructionSet::kX86};
107*795d594fSAndroid Build Coastguard Worker       } else {
108*795d594fSAndroid Build Coastguard Worker         *error_msg = android::base::StringPrintf("Unknown Zygote kinds '%s'", zygote_kinds.c_str());
109*795d594fSAndroid Build Coastguard Worker         return {};
110*795d594fSAndroid Build Coastguard Worker       }
111*795d594fSAndroid Build Coastguard Worker     default:
112*795d594fSAndroid Build Coastguard Worker       *error_msg = android::base::StringPrintf("Unknown runtime ISA '%s'",
113*795d594fSAndroid Build Coastguard Worker                                                GetInstructionSetString(kRuntimeISA));
114*795d594fSAndroid Build Coastguard Worker       return {};
115*795d594fSAndroid Build Coastguard Worker   }
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker namespace instruction_set_details {
119*795d594fSAndroid Build Coastguard Worker 
120*795d594fSAndroid Build Coastguard Worker #if !defined(ART_FRAME_SIZE_LIMIT)
121*795d594fSAndroid Build Coastguard Worker #error "ART frame size limit missing"
122*795d594fSAndroid Build Coastguard Worker #endif
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker // TODO: Should we require an extra page (RoundUp(SIZE) + gPageSize)?
125*795d594fSAndroid Build Coastguard Worker static_assert(ART_FRAME_SIZE_LIMIT < kArmStackOverflowReservedBytes, "Frame size limit too large");
126*795d594fSAndroid Build Coastguard Worker static_assert(ART_FRAME_SIZE_LIMIT < kArm64StackOverflowReservedBytes,
127*795d594fSAndroid Build Coastguard Worker               "Frame size limit too large");
128*795d594fSAndroid Build Coastguard Worker static_assert(ART_FRAME_SIZE_LIMIT < kRiscv64StackOverflowReservedBytes,
129*795d594fSAndroid Build Coastguard Worker               "Frame size limit too large");
130*795d594fSAndroid Build Coastguard Worker static_assert(ART_FRAME_SIZE_LIMIT < kX86StackOverflowReservedBytes,
131*795d594fSAndroid Build Coastguard Worker               "Frame size limit too large");
132*795d594fSAndroid Build Coastguard Worker static_assert(ART_FRAME_SIZE_LIMIT < kX86_64StackOverflowReservedBytes,
133*795d594fSAndroid Build Coastguard Worker               "Frame size limit too large");
134*795d594fSAndroid Build Coastguard Worker 
GetStackOverflowReservedBytesFailure(const char * error_msg)135*795d594fSAndroid Build Coastguard Worker NO_RETURN void GetStackOverflowReservedBytesFailure(const char* error_msg) {
136*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << error_msg;
137*795d594fSAndroid Build Coastguard Worker   UNREACHABLE();
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker 
140*795d594fSAndroid Build Coastguard Worker }  // namespace instruction_set_details
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker }  // namespace art
143