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