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