12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
17bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers#include "context_arm.h"
18bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
19ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method.h"
2004d7aa92bc5548bc4d272b9480614f06248194ccIan Rogers#include "mirror/object-inl.h"
212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "stack.h"
222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "thread.h"
23bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
24bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersnamespace art {
25bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersnamespace arm {
26bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
276702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartierstatic const uint32_t gZero = 0;
286702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier
296702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartiervoid ArmContext::Reset() {
306702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
316702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    gprs_[i] = NULL;
32bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
336702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  for (size_t i = 0; i < kNumberOfSRegisters; i++) {
346702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    fprs_[i] = NULL;
3515fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers  }
366702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[SP] = &sp_;
376702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[PC] = &pc_;
386702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  // Initialize registers with easy to spot debug values.
396702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  sp_ = ArmContext::kBadGprBase + SP;
406702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  pc_ = ArmContext::kBadGprBase + PC;
41bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}
42bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersvoid ArmContext::FillCalleeSaves(const StackVisitor& fr) {
44ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method = fr.GetMethod();
45bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  uint32_t core_spills = method->GetCoreSpillMask();
4615fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers  uint32_t fp_core_spills = method->GetFpSpillMask();
47bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  size_t spill_count = __builtin_popcount(core_spills);
4815fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers  size_t fp_spill_count = __builtin_popcount(fp_core_spills);
490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  size_t frame_size = method->GetFrameSizeInBytes();
50bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  if (spill_count > 0) {
516702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    // Lowest number spill is farthest away, walk registers and fill into context
52bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    int j = 1;
536702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
54bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers      if (((core_spills >> i) & 1) != 0) {
556702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier        gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
56bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers        j++;
57bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers      }
58bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers    }
59bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers  }
6015fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers  if (fp_spill_count > 0) {
616702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    // Lowest number spill is farthest away, walk registers and fill into context
6215fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers    int j = 1;
636702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    for (size_t i = 0; i < kNumberOfSRegisters; i++) {
6415fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers      if (((fp_core_spills >> i) & 1) != 0) {
656702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier        fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j, frame_size);
6615fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers        j++;
6715fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers      }
6815fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers    }
6915fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers  }
70bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}
71bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
726702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartiervoid ArmContext::SetGPR(uint32_t reg, uintptr_t value) {
73d74e41b1cce5373aa24fd2fbea735173f6113d5aBrian Carlstrom  DCHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  DCHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
754f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  DCHECK(gprs_[reg] != NULL);
766702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  *gprs_[reg] = value;
776702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier}
786702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier
799c750f9b6283f62b3e6a93c0c6b2838abde5000eElliott Hughesvoid ArmContext::SmashCallerSaves() {
806702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  // This needs to be 0 because we want a null/zero return value.
816702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[R0] = const_cast<uint32_t*>(&gZero);
826702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[R1] = const_cast<uint32_t*>(&gZero);
836702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[R2] = NULL;
846702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  gprs_[R3] = NULL;
859c750f9b6283f62b3e6a93c0c6b2838abde5000eElliott Hughes}
869c750f9b6283f62b3e6a93c0c6b2838abde5000eElliott Hughes
878dbb708c7dc05c786329eb5c3fff3194ab6472acLogan Chienextern "C" void art_quick_do_long_jump(uint32_t*, uint32_t*);
8857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers
89bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersvoid ArmContext::DoLongJump() {
906702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  uintptr_t gprs[16];
916702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  uint32_t fprs[32];
926702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
936702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    gprs[i] = gprs_[i] != NULL ? *gprs_[i] : ArmContext::kBadGprBase + i;
946702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  }
956702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  for (size_t i = 0; i < kNumberOfSRegisters; ++i) {
966702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier    fprs[i] = fprs_[i] != NULL ? *fprs_[i] : ArmContext::kBadGprBase + i;
976702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  }
986702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier  DCHECK_EQ(reinterpret_cast<uintptr_t>(Thread::Current()), gprs[TR]);
998dbb708c7dc05c786329eb5c3fff3194ab6472acLogan Chien  art_quick_do_long_jump(gprs, fprs);
100bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}
101bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers
102bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}  // namespace arm
103bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}  // namespace art
104