1 /*
2  * Copyright (C) 2012 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_ENTRYPOINTS_QUICK_CALLEE_SAVE_FRAME_H_
18 #define ART_RUNTIME_ENTRYPOINTS_QUICK_CALLEE_SAVE_FRAME_H_
19 
20 #include "arch/instruction_set.h"
21 #include "base/callee_save_type.h"
22 #include "base/enums.h"
23 #include "base/mutex.h"
24 #include "thread-inl.h"
25 
26 // Specific frame size code is in architecture-specific files. We include this to compile-time
27 // specialize the code.
28 #include "arch/arm/quick_method_frame_info_arm.h"
29 #include "arch/arm64/quick_method_frame_info_arm64.h"
30 #include "arch/mips/quick_method_frame_info_mips.h"
31 #include "arch/mips64/quick_method_frame_info_mips64.h"
32 #include "arch/x86/quick_method_frame_info_x86.h"
33 #include "arch/x86_64/quick_method_frame_info_x86_64.h"
34 
35 namespace art {
36 class ArtMethod;
37 
38 class ScopedQuickEntrypointChecks {
39  public:
40   explicit ScopedQuickEntrypointChecks(Thread *self,
41                                        bool entry_check = kIsDebugBuild,
42                                        bool exit_check = kIsDebugBuild)
REQUIRES_SHARED(Locks::mutator_lock_)43       REQUIRES_SHARED(Locks::mutator_lock_) : self_(self), exit_check_(exit_check) {
44     if (entry_check) {
45       TestsOnEntry();
46     }
47   }
48 
REQUIRES_SHARED(Locks::mutator_lock_)49   ~ScopedQuickEntrypointChecks() REQUIRES_SHARED(Locks::mutator_lock_) {
50     if (exit_check_) {
51       TestsOnExit();
52     }
53   }
54 
55  private:
TestsOnEntry()56   void TestsOnEntry() REQUIRES_SHARED(Locks::mutator_lock_) {
57     Locks::mutator_lock_->AssertSharedHeld(self_);
58     self_->VerifyStack();
59   }
60 
TestsOnExit()61   void TestsOnExit() REQUIRES_SHARED(Locks::mutator_lock_) {
62     Locks::mutator_lock_->AssertSharedHeld(self_);
63     self_->VerifyStack();
64   }
65 
66   Thread* const self_;
67   bool exit_check_;
68 };
69 
GetCalleeSaveFrameSize(InstructionSet isa,CalleeSaveType type)70 static constexpr size_t GetCalleeSaveFrameSize(InstructionSet isa, CalleeSaveType type) {
71   switch (isa) {
72     case InstructionSet::kArm:
73     case InstructionSet::kThumb2:
74       return arm::ArmCalleeSaveFrameSize(type);
75     case InstructionSet::kArm64:
76       return arm64::Arm64CalleeSaveFrameSize(type);
77     case InstructionSet::kMips:
78       return mips::MipsCalleeSaveFrameSize(type);
79     case InstructionSet::kMips64:
80       return mips64::Mips64CalleeSaveFrameSize(type);
81     case InstructionSet::kX86:
82       return x86::X86CalleeSaveFrameSize(type);
83     case InstructionSet::kX86_64:
84       return x86_64::X86_64CalleeSaveFrameSize(type);
85     case InstructionSet::kNone:
86       LOG(FATAL) << "kNone has no frame size";
87       UNREACHABLE();
88   }
89   LOG(FATAL) << "Unknown ISA " << isa;
90   UNREACHABLE();
91 }
92 
93 // Note: this specialized statement is sanity-checked in the quick-trampoline gtest.
GetConstExprPointerSize(InstructionSet isa)94 static constexpr PointerSize GetConstExprPointerSize(InstructionSet isa) {
95   switch (isa) {
96     case InstructionSet::kArm:
97     case InstructionSet::kThumb2:
98       return kArmPointerSize;
99     case InstructionSet::kArm64:
100       return kArm64PointerSize;
101     case InstructionSet::kMips:
102       return kMipsPointerSize;
103     case InstructionSet::kMips64:
104       return kMips64PointerSize;
105     case InstructionSet::kX86:
106       return kX86PointerSize;
107     case InstructionSet::kX86_64:
108       return kX86_64PointerSize;
109     case InstructionSet::kNone:
110       LOG(FATAL) << "kNone has no pointer size";
111       UNREACHABLE();
112   }
113   LOG(FATAL) << "Unknown ISA " << isa;
114   UNREACHABLE();
115 }
116 
117 // Note: this specialized statement is sanity-checked in the quick-trampoline gtest.
GetCalleeSaveReturnPcOffset(InstructionSet isa,CalleeSaveType type)118 static constexpr size_t GetCalleeSaveReturnPcOffset(InstructionSet isa, CalleeSaveType type) {
119   return GetCalleeSaveFrameSize(isa, type) - static_cast<size_t>(GetConstExprPointerSize(isa));
120 }
121 
122 }  // namespace art
123 
124 #endif  // ART_RUNTIME_ENTRYPOINTS_QUICK_CALLEE_SAVE_FRAME_H_
125