gem5  v22.1.0.0
nativetrace.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2011, 2014, 2016-2017 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2006 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include "arch/arm/nativetrace.hh"
42 
43 #include "arch/arm/regs/cc.hh"
44 #include "arch/arm/regs/int.hh"
45 #include "arch/arm/regs/misc.hh"
46 #include "arch/arm/regs/vec.hh"
47 #include "base/compiler.hh"
48 #include "cpu/thread_context.hh"
49 #include "debug/ExecRegDelta.hh"
50 #include "params/ArmNativeTrace.hh"
51 #include "sim/byteswap.hh"
52 
53 namespace gem5
54 {
55 
56 using namespace ArmISA;
57 
58 namespace trace {
59 
60 [[maybe_unused]] static const char *regNames[] = {
61  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
62  "r8", "r9", "r10", "fp", "r12", "sp", "lr", "pc",
63  "cpsr", "f0", "f1", "f2", "f3", "f4", "f5", "f6",
64  "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14",
65  "f15", "f16", "f17", "f18", "f19", "f20", "f21", "f22",
66  "f23", "f24", "f25", "f26", "f27", "f28", "f29", "f30",
67  "f31", "fpscr"
68 };
69 
70 void
71 ArmNativeTrace::ThreadState::update(NativeTrace *parent)
72 {
73  oldState = state[current];
74  current = (current + 1) % 2;
75  newState = state[current];
76 
77  memcpy(newState, oldState, sizeof(state[0]));
78 
79  uint64_t diffVector;
80  parent->read(&diffVector, sizeof(diffVector));
81  diffVector = letoh(diffVector);
82 
83  int changes = 0;
84  for (int i = 0; i < STATE_NUMVALS; i++) {
85  if (diffVector & 0x1) {
86  changed[i] = true;
87  changes++;
88  } else {
89  changed[i] = false;
90  }
91  diffVector >>= 1;
92  }
93 
94  uint64_t values[changes];
95  parent->read(values, sizeof(values));
96  int pos = 0;
97  for (int i = 0; i < STATE_NUMVALS; i++) {
98  if (changed[i]) {
99  newState[i] = letoh(values[pos++]);
100  changed[i] = (newState[i] != oldState[i]);
101  }
102  }
103 }
104 
105 void
106 ArmNativeTrace::ThreadState::update(ThreadContext *tc)
107 {
108  oldState = state[current];
109  current = (current + 1) % 2;
110  newState = state[current];
111 
112  // Regular int regs
113  for (int i = 0; i < 15; i++) {
114  newState[i] = tc->getReg(intRegClass[i]);
115  changed[i] = (oldState[i] != newState[i]);
116  }
117 
118  //R15, aliased with the PC
119  newState[STATE_PC] = tc->pcState().as<ArmISA::PCState>().npc();
120  changed[STATE_PC] = (newState[STATE_PC] != oldState[STATE_PC]);
121 
122  //CPSR
123  CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
124  cpsr.nz = tc->getReg(cc_reg::Nz);
125  cpsr.c = tc->getReg(cc_reg::C);
126  cpsr.v = tc->getReg(cc_reg::V);
127  cpsr.ge = tc->getReg(cc_reg::Ge);
128 
129  newState[STATE_CPSR] = cpsr;
130  changed[STATE_CPSR] = (newState[STATE_CPSR] != oldState[STATE_CPSR]);
131 
132  for (int i = 0; i < NumVecV7ArchRegs; i++) {
133  ArmISA::VecRegContainer vec_container;
134  tc->getReg(vecRegClass[i], &vec_container);
135  auto *vec = vec_container.as<uint64_t>();
136  newState[STATE_F0 + 2*i] = vec[0];
137  newState[STATE_F0 + 2*i + 1] = vec[1];
138  }
139  newState[STATE_FPSCR] = tc->readMiscRegNoEffect(MISCREG_FPSCR) |
140  tc->getReg(cc_reg::Fp);
141 }
142 
143 void
144 ArmNativeTrace::check(NativeTraceRecord *record)
145 {
146  ThreadContext *tc = record->getThread();
147  // This area is read only on the target. It can't stop there to tell us
148  // what's going on, so we should skip over anything there also.
149  if (tc->pcState().as<ArmISA::PCState>().npc() > 0xffff0000)
150  return;
151  nState.update(this);
152  mState.update(tc);
153 
154  // If a syscall just happened native trace needs another tick
155  if ((mState.oldState[STATE_PC] == nState.oldState[STATE_PC]) &&
156  (mState.newState[STATE_PC] - 4 == nState.newState[STATE_PC])) {
157  DPRINTF(ExecRegDelta, "Advancing to match PCs after syscall\n");
158  nState.update(this);
159 
160  }
161 
162  bool errorFound = false;
163  // Regular int regs
164  for (int i = 0; i < STATE_NUMVALS; i++) {
165  if (nState.changed[i] || mState.changed[i]) {
166  bool oldMatch = (mState.oldState[i] == nState.oldState[i]);
167  bool newMatch = (mState.newState[i] == nState.newState[i]);
168  if (oldMatch && newMatch) {
169  // The more things change, the more they stay the same.
170  continue;
171  }
172 
173  errorFound = true;
174 
175 #ifndef NDEBUG
176  const char *vergence = " ";
177  if (oldMatch && !newMatch) {
178  vergence = "<>";
179  } else if (!oldMatch && newMatch) {
180  vergence = "><";
181  }
182 
183  if (!nState.changed[i]) {
184  DPRINTF(ExecRegDelta, "%s [%5s] "\
185  "Native: %#010x "\
186  "M5: %#010x => %#010x\n",
187  vergence, regNames[i],
188  nState.newState[i],
189  mState.oldState[i], mState.newState[i]);
190  } else if (!mState.changed[i]) {
191  DPRINTF(ExecRegDelta, "%s [%5s] "\
192  "Native: %#010x => %#010x "\
193  "M5: %#010x \n",
194  vergence, regNames[i],
195  nState.oldState[i], nState.newState[i],
196  mState.newState[i]);
197  } else {
198  DPRINTF(ExecRegDelta, "%s [%5s] "\
199  "Native: %#010x => %#010x "\
200  "M5: %#010x => %#010x\n",
201  vergence, regNames[i],
202  nState.oldState[i], nState.newState[i],
203  mState.oldState[i], mState.newState[i]);
204  }
205 #endif
206  }
207  }
208  if (errorFound) {
209  StaticInstPtr inst = record->getStaticInst();
210  assert(inst);
211  bool ran = true;
212  if (inst->isMicroop()) {
213  ran = false;
214  inst = record->getMacroStaticInst();
215  }
216  assert(inst);
217  record->traceInst(inst, ran);
218 
219  bool pcError = (mState.newState[STATE_PC] !=
220  nState.newState[STATE_PC]);
221  if (stopOnPCError && pcError)
222  panic("Native trace detected an error in control flow!");
223  }
224 }
225 
226 } // namespace trace
227 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
Target & as()
Definition: pcstate.hh:72
bool isMicroop() const
Definition: static_inst.hh:186
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual RegVal readMiscReg(RegIndex misc_reg)=0
virtual RegVal getReg(const RegId &reg) const
virtual const PCStateBase & pcState() const =0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
Vector Register Abstraction This generic class is the model in a particularization of MVC,...
Definition: vec_reg.hh:124
VecElem * as()
View interposers.
Definition: vec_reg.hh:189
void traceInst(const StaticInstPtr &inst, bool ran)
Definition: exetrace.cc:60
ThreadContext * getThread() const
Definition: insttracer.hh:266
StaticInstPtr getMacroStaticInst() const
Definition: insttracer.hh:269
StaticInstPtr getStaticInst() const
Definition: insttracer.hh:267
void read(void *ptr, size_t size)
Definition: nativetrace.hh:104
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
atomic_var_t state
Definition: helpers.cc:188
constexpr RegId V
Definition: cc.hh:75
constexpr RegId C
Definition: cc.hh:74
constexpr RegId Fp
Definition: cc.hh:77
constexpr RegId Ge
Definition: cc.hh:76
constexpr RegId Nz
Definition: cc.hh:73
Bitfield< 7 > i
Definition: misc_types.hh:67
const int NumVecV7ArchRegs
Definition: vec.hh:79
constexpr RegClass intRegClass
Definition: int.hh:173
@ MISCREG_CPSR
Definition: misc.hh:65
@ MISCREG_FPSCR
Definition: misc.hh:76
constexpr RegClass vecRegClass
Definition: vec.hh:101
GenericISA::DelaySlotPCState< 4 > PCState
Definition: pcstate.hh:40
Bitfield< 25 > vec
Definition: misc.hh:113
static const char * regNames[]
Definition: nativetrace.cc:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
T letoh(T value)
Definition: byteswap.hh:173

Generated on Wed Dec 21 2022 10:22:26 for gem5 by doxygen 1.9.1