1 // Copyright 2015, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef VIXL_AARCH64_SIMULATOR_CONSTANTS_AARCH64_H_
28 #define VIXL_AARCH64_SIMULATOR_CONSTANTS_AARCH64_H_
29 
30 #include "instructions-aarch64.h"
31 
32 namespace vixl {
33 namespace aarch64 {
34 
35 // Debug instructions.
36 //
37 // VIXL's macro-assembler and simulator support a few pseudo instructions to
38 // make debugging easier. These pseudo instructions do not exist on real
39 // hardware.
40 //
41 // TODO: Also consider allowing these pseudo-instructions to be disabled in the
42 // simulator, so that users can check that the input is a valid native code.
43 // (This isn't possible in all cases. Printf won't work, for example.)
44 //
45 // Each debug pseudo instruction is represented by a HLT instruction. The HLT
46 // immediate field is used to identify the type of debug pseudo instruction.
47 
48 enum DebugHltOpcode {
49   kUnreachableOpcode = 0xdeb0,
50   kPrintfOpcode,
51   kTraceOpcode,
52   kLogOpcode,
53   kRuntimeCallOpcode,
54   kSetCPUFeaturesOpcode,
55   kEnableCPUFeaturesOpcode,
56   kDisableCPUFeaturesOpcode,
57   kSaveCPUFeaturesOpcode,
58   kRestoreCPUFeaturesOpcode,
59   kMTEActive,
60   kMTEInactive,
61   // Aliases.
62   kDebugHltFirstOpcode = kUnreachableOpcode,
63   kDebugHltLastOpcode = kLogOpcode
64 };
65 VIXL_DEPRECATED("DebugHltOpcode", typedef DebugHltOpcode DebugHltOpcodes);
66 
67 // Each pseudo instruction uses a custom encoding for additional arguments, as
68 // described below.
69 
70 // Unreachable - kUnreachableOpcode
71 //
72 // Instruction which should never be executed. This is used as a guard in parts
73 // of the code that should not be reachable, such as in data encoded inline in
74 // the instructions.
75 
76 // Printf - kPrintfOpcode
77 //  - arg_count: The number of arguments.
78 //  - arg_pattern: A set of PrintfArgPattern values, packed into two-bit fields.
79 //
80 // Simulate a call to printf.
81 //
82 // Floating-point and integer arguments are passed in separate sets of registers
83 // in AAPCS64 (even for varargs functions), so it is not possible to determine
84 // the type of each argument without some information about the values that were
85 // passed in. This information could be retrieved from the printf format string,
86 // but the format string is not trivial to parse so we encode the relevant
87 // information with the HLT instruction.
88 //
89 // Also, the following registers are populated (as if for a native Aarch64
90 // call):
91 //    x0: The format string
92 // x1-x7: Optional arguments, if type == CPURegister::kRegister
93 // d0-d7: Optional arguments, if type == CPURegister::kVRegister
94 const unsigned kPrintfArgCountOffset = 1 * kInstructionSize;
95 const unsigned kPrintfArgPatternListOffset = 2 * kInstructionSize;
96 const unsigned kPrintfLength = 3 * kInstructionSize;
97 
98 const unsigned kPrintfMaxArgCount = 4;
99 
100 // The argument pattern is a set of two-bit-fields, each with one of the
101 // following values:
102 enum PrintfArgPattern {
103   kPrintfArgW = 1,
104   kPrintfArgX = 2,
105   // There is no kPrintfArgS because floats are always converted to doubles in C
106   // varargs calls.
107   kPrintfArgD = 3
108 };
109 static const unsigned kPrintfArgPatternBits = 2;
110 
111 // Trace - kTraceOpcode
112 //  - parameter: TraceParameter stored as a uint32_t
113 //  - command: TraceCommand stored as a uint32_t
114 //
115 // Allow for trace management in the generated code. This enables or disables
116 // automatic tracing of the specified information for every simulated
117 // instruction.
118 const unsigned kTraceParamsOffset = 1 * kInstructionSize;
119 const unsigned kTraceCommandOffset = 2 * kInstructionSize;
120 const unsigned kTraceLength = 3 * kInstructionSize;
121 
122 // Trace parameters.
123 enum TraceParameters {
124   LOG_DISASM = 1 << 0,   // Log disassembly.
125   LOG_REGS = 1 << 1,     // Log general purpose registers.
126   LOG_VREGS = 1 << 2,    // Log SVE, NEON and floating-point registers.
127   LOG_SYSREGS = 1 << 3,  // Log the flags and system registers.
128   LOG_WRITE = 1 << 4,    // Log writes to memory.
129   LOG_BRANCH = 1 << 5,   // Log taken branches.
130 
131   LOG_NONE = 0,
132   LOG_STATE = LOG_REGS | LOG_VREGS | LOG_SYSREGS,
133   LOG_ALL = LOG_DISASM | LOG_STATE | LOG_WRITE | LOG_BRANCH
134 };
135 
136 // Trace commands.
137 enum TraceCommand { TRACE_ENABLE = 1, TRACE_DISABLE = 2 };
138 
139 // Log - kLogOpcode
140 //  - parameter: TraceParameter stored as a uint32_t
141 //
142 // Print the specified information once. This mechanism is separate from Trace.
143 // In particular, _all_ of the specified registers are printed, rather than just
144 // the registers that the instruction writes.
145 //
146 // Any combination of the TraceParameters values can be used, except that
147 // LOG_DISASM is not supported for Log.
148 const unsigned kLogParamsOffset = 1 * kInstructionSize;
149 const unsigned kLogLength = 2 * kInstructionSize;
150 
151 // Runtime call simulation - kRuntimeCallOpcode
152 enum RuntimeCallType { kCallRuntime, kTailCallRuntime };
153 
154 const unsigned kRuntimeCallWrapperOffset = 1 * kInstructionSize;
155 // The size of a pointer on host.
156 const unsigned kRuntimeCallAddressSize = sizeof(uintptr_t);
157 const unsigned kRuntimeCallFunctionOffset =
158     kRuntimeCallWrapperOffset + kRuntimeCallAddressSize;
159 const unsigned kRuntimeCallTypeOffset =
160     kRuntimeCallFunctionOffset + kRuntimeCallAddressSize;
161 const unsigned kRuntimeCallLength = kRuntimeCallTypeOffset + sizeof(uint32_t);
162 
163 // Enable or disable CPU features - kSetCPUFeaturesOpcode
164 //                                - kEnableCPUFeaturesOpcode
165 //                                - kDisableCPUFeaturesOpcode
166 //  - parameter[...]: A list of `CPUFeatures::Feature`s, encoded as
167 //    ConfigureCPUFeaturesElementType and terminated with CPUFeatures::kNone.
168 //  - [Padding to align to kInstructionSize.]
169 //
170 // 'Set' completely overwrites the existing CPU features.
171 // 'Enable' and 'Disable' update the existing CPU features.
172 //
173 // These mechanisms allows users to strictly check the use of CPU features in
174 // different regions of code.
175 //
176 // These have no effect on the set of 'seen' features (as reported by
177 // CPUFeaturesAuditor::HasSeen(...)).
178 typedef uint8_t ConfigureCPUFeaturesElementType;
179 const unsigned kConfigureCPUFeaturesListOffset = 1 * kInstructionSize;
180 
181 // Save or restore CPU features - kSaveCPUFeaturesOpcode
182 //                              - kRestoreCPUFeaturesOpcode
183 //
184 // These mechanisms provide a stack-like mechanism for preserving the CPU
185 // features, or restoring the last-preserved features. These pseudo-instructions
186 // take no arguments.
187 //
188 // These have no effect on the set of 'seen' features (as reported by
189 // CPUFeaturesAuditor::HasSeen(...)).
190 
191 }  // namespace aarch64
192 }  // namespace vixl
193 
194 #endif  // VIXL_AARCH64_SIMULATOR_CONSTANTS_AARCH64_H_
195