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/enums.h"
22#include "base/mutex.h"
23#include "runtime.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
35namespace art {
36class ArtMethod;
37
38class ScopedQuickEntrypointChecks {
39 public:
40  explicit ScopedQuickEntrypointChecks(Thread *self,
41                                       bool entry_check = kIsDebugBuild,
42                                       bool exit_check = kIsDebugBuild)
43      REQUIRES_SHARED(Locks::mutator_lock_) : self_(self), exit_check_(exit_check) {
44    if (entry_check) {
45      TestsOnEntry();
46    }
47  }
48
49  ScopedQuickEntrypointChecks() REQUIRES_SHARED(Locks::mutator_lock_)
50      : self_(kIsDebugBuild ? Thread::Current() : nullptr), exit_check_(kIsDebugBuild) {
51    if (kIsDebugBuild) {
52      TestsOnEntry();
53    }
54  }
55
56  ~ScopedQuickEntrypointChecks() REQUIRES_SHARED(Locks::mutator_lock_) {
57    if (exit_check_) {
58      TestsOnExit();
59    }
60  }
61
62 private:
63  void TestsOnEntry() REQUIRES_SHARED(Locks::mutator_lock_) {
64    Locks::mutator_lock_->AssertSharedHeld(self_);
65    self_->VerifyStack();
66  }
67
68  void TestsOnExit() REQUIRES_SHARED(Locks::mutator_lock_) {
69    Locks::mutator_lock_->AssertSharedHeld(self_);
70    self_->VerifyStack();
71  }
72
73  Thread* const self_;
74  bool exit_check_;
75};
76
77static constexpr size_t GetCalleeSaveFrameSize(InstructionSet isa, Runtime::CalleeSaveType type) {
78  // constexpr must be a return statement.
79  return (isa == kArm || isa == kThumb2) ? arm::ArmCalleeSaveFrameSize(type) :
80         isa == kArm64 ? arm64::Arm64CalleeSaveFrameSize(type) :
81         isa == kMips ? mips::MipsCalleeSaveFrameSize(type) :
82         isa == kMips64 ? mips64::Mips64CalleeSaveFrameSize(type) :
83         isa == kX86 ? x86::X86CalleeSaveFrameSize(type) :
84         isa == kX86_64 ? x86_64::X86_64CalleeSaveFrameSize(type) :
85         isa == kNone ? (LOG(FATAL) << "kNone has no frame size", 0) :
86         (LOG(FATAL) << "Unknown instruction set" << isa, 0);
87}
88
89// Note: this specialized statement is sanity-checked in the quick-trampoline gtest.
90static constexpr PointerSize GetConstExprPointerSize(InstructionSet isa) {
91  // constexpr must be a return statement.
92  return (isa == kArm || isa == kThumb2) ? kArmPointerSize :
93         isa == kArm64 ? kArm64PointerSize :
94         isa == kMips ? kMipsPointerSize :
95         isa == kMips64 ? kMips64PointerSize :
96         isa == kX86 ? kX86PointerSize :
97         isa == kX86_64 ? kX86_64PointerSize :
98         isa == kNone ? (LOG(FATAL) << "kNone has no pointer size", PointerSize::k32) :
99         (LOG(FATAL) << "Unknown instruction set" << isa, PointerSize::k32);
100}
101
102// Note: this specialized statement is sanity-checked in the quick-trampoline gtest.
103static constexpr size_t GetCalleeSaveReturnPcOffset(InstructionSet isa,
104                                                    Runtime::CalleeSaveType type) {
105  return GetCalleeSaveFrameSize(isa, type) - static_cast<size_t>(GetConstExprPointerSize(isa));
106}
107
108}  // namespace art
109
110#endif  // ART_RUNTIME_ENTRYPOINTS_QUICK_CALLEE_SAVE_FRAME_H_
111