1 /*
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  */
16 
17 #ifndef ART_RUNTIME_ARCH_CONTEXT_H_
18 #define ART_RUNTIME_ARCH_CONTEXT_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "base/mutex.h"
24 
25 namespace art {
26 
27 class StackVisitor;
28 
29 // Representation of a thread's context on the executing machine, used to implement long jumps in
30 // the quick stack frame layout.
31 class Context {
32  public:
33   // Creates a context for the running architecture
34   static Context* Create();
35 
~Context()36   virtual ~Context() {}
37 
38   // Re-initializes the registers for context re-use.
39   virtual void Reset() = 0;
40 
41   // Reads values from callee saves in the given frame. The frame also holds
42   // the method that holds the layout.
43   virtual void FillCalleeSaves(const StackVisitor& fr)
44       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
45 
46   // Sets the stack pointer value.
47   virtual void SetSP(uintptr_t new_sp) = 0;
48 
49   // Sets the program counter value.
50   virtual void SetPC(uintptr_t new_pc) = 0;
51 
52   // Gets the given GPRs address.
53   virtual uintptr_t* GetGPRAddress(uint32_t reg) = 0;
54 
55   // Reads the given GPR. Returns true if we successfully read the register and
56   // set its value into 'val', returns false otherwise.
57   virtual bool GetGPR(uint32_t reg, uintptr_t* val) = 0;
58 
59   // Sets the given GPR. Returns true if we successfully write the given value
60   // into the register, returns false otherwise.
61   virtual bool SetGPR(uint32_t reg, uintptr_t value) = 0;
62 
63   // Reads the given FPR. Returns true if we successfully read the register and
64   // set its value into 'val', returns false otherwise.
65   virtual bool GetFPR(uint32_t reg, uintptr_t* val) = 0;
66 
67   // Sets the given FPR. Returns true if we successfully write the given value
68   // into the register, returns false otherwise.
69   virtual bool SetFPR(uint32_t reg, uintptr_t value) = 0;
70 
71   // Smashes the caller save registers. If we're throwing, we don't want to return bogus values.
72   virtual void SmashCallerSaves() = 0;
73 
74   // Switches execution of the executing context to this context
75   virtual void DoLongJump() = 0;
76 
77  protected:
78   enum {
79     kBadGprBase = 0xebad6070,
80     kBadFprBase = 0xebad8070,
81   };
82 };
83 
84 }  // namespace art
85 
86 #endif  // ART_RUNTIME_ARCH_CONTEXT_H_
87