blob: 711452cffb6cf2ebdb4dc436507e2c1dbe1c2bcc [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Ian Rogersbdb03912011-09-14 00:55:44 -070016
17#include "context_arm.h"
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070020#include "base/bit_utils_iterator.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010021#include "quick/quick_method_frame_info.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070022#include "thread-current-inl.h"
Ian Rogersbdb03912011-09-14 00:55:44 -070023
24namespace art {
25namespace arm {
26
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020027static constexpr uint32_t gZero = 0;
Mathieu Chartier67022432012-11-29 18:04:50 -080028
29void ArmContext::Reset() {
Vladimir Marko80afd022015-05-19 18:08:00 +010030 std::fill_n(gprs_, arraysize(gprs_), nullptr);
31 std::fill_n(fprs_, arraysize(fprs_), nullptr);
Mathieu Chartier67022432012-11-29 18:04:50 -080032 gprs_[SP] = &sp_;
33 gprs_[PC] = &pc_;
Andreas Gampe639bdd12015-06-03 11:22:45 -070034 gprs_[R0] = &arg0_;
Mathieu Chartier67022432012-11-29 18:04:50 -080035 // Initialize registers with easy to spot debug values.
36 sp_ = ArmContext::kBadGprBase + SP;
37 pc_ = ArmContext::kBadGprBase + PC;
Andreas Gampe639bdd12015-06-03 11:22:45 -070038 arg0_ = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -070039}
40
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010041void ArmContext::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
Vladimir Marko80afd022015-05-19 18:08:00 +010042 int spill_pos = 0;
43
44 // Core registers come first, from the highest down to the lowest.
45 uint32_t core_regs = frame_info.CoreSpillMask();
46 DCHECK_EQ(0u, core_regs & (static_cast<uint32_t>(-1) << kNumberOfCoreRegisters));
47 for (uint32_t core_reg : HighToLowBits(core_regs)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010048 gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
Vladimir Marko80afd022015-05-19 18:08:00 +010049 ++spill_pos;
Ian Rogersbdb03912011-09-14 00:55:44 -070050 }
Vladimir Marko80afd022015-05-19 18:08:00 +010051 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
52
53 // FP registers come second, from the highest down to the lowest.
54 for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010055 fprs_[fp_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
Vladimir Marko80afd022015-05-19 18:08:00 +010056 ++spill_pos;
Ian Rogers15fdb8c2011-09-25 15:45:07 -070057 }
Vladimir Marko80afd022015-05-19 18:08:00 +010058 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
Ian Rogersbdb03912011-09-14 00:55:44 -070059}
60
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010061void ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstromd74e41b2013-03-24 23:47:01 -070062 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010063 DCHECK(IsAccessibleGPR(reg));
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064 DCHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010065 *gprs_[reg] = value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020066}
67
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010068void ArmContext::SetFPR(uint32_t reg, uintptr_t value) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020069 DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfSRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010070 DCHECK(IsAccessibleFPR(reg));
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020071 DCHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010072 *fprs_[reg] = value;
Mathieu Chartier67022432012-11-29 18:04:50 -080073}
74
Elliott Hughes9c750f92012-04-05 12:07:59 -070075void ArmContext::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080076 // This needs to be 0 because we want a null/zero return value.
77 gprs_[R0] = const_cast<uint32_t*>(&gZero);
78 gprs_[R1] = const_cast<uint32_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020079 gprs_[R2] = nullptr;
80 gprs_[R3] = nullptr;
Zheng Xu5667fdb2014-10-23 18:29:55 +080081
82 fprs_[S0] = nullptr;
83 fprs_[S1] = nullptr;
84 fprs_[S2] = nullptr;
85 fprs_[S3] = nullptr;
86 fprs_[S4] = nullptr;
87 fprs_[S5] = nullptr;
88 fprs_[S6] = nullptr;
89 fprs_[S7] = nullptr;
90 fprs_[S8] = nullptr;
91 fprs_[S9] = nullptr;
92 fprs_[S10] = nullptr;
93 fprs_[S11] = nullptr;
94 fprs_[S12] = nullptr;
95 fprs_[S13] = nullptr;
96 fprs_[S14] = nullptr;
97 fprs_[S15] = nullptr;
Elliott Hughes9c750f92012-04-05 12:07:59 -070098}
99
Andreas Gampe794ad762015-02-23 08:12:24 -0800100extern "C" NO_RETURN void art_quick_do_long_jump(uint32_t*, uint32_t*);
Ian Rogers57b86d42012-03-27 16:05:41 -0700101
Ian Rogersbdb03912011-09-14 00:55:44 -0700102void ArmContext::DoLongJump() {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200103 uintptr_t gprs[kNumberOfCoreRegisters];
104 uint32_t fprs[kNumberOfSRegisters];
Mathieu Chartier67022432012-11-29 18:04:50 -0800105 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200106 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : ArmContext::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800107 }
108 for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200109 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : ArmContext::kBadFprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800110 }
Roland Levillain6d729a72017-06-30 18:34:01 +0100111 // Ensure the Thread Register contains the address of the current thread.
Mathieu Chartier67022432012-11-29 18:04:50 -0800112 DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
Roland Levillain6d729a72017-06-30 18:34:01 +0100113 // The Marking Register will be updated by art_quick_do_long_jump.
Logan Chien8dbb7082013-01-25 20:31:17 +0800114 art_quick_do_long_jump(gprs, fprs);
Ian Rogersbdb03912011-09-14 00:55:44 -0700115}
116
117} // namespace arm
118} // namespace art