gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
thread_context.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012, 2016-2017 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2006 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "cpu/thread_context.hh"
43
44#include <vector>
45
47#include "base/logging.hh"
48#include "base/trace.hh"
49#include "cpu/base.hh"
50#include "debug/Context.hh"
51#include "debug/Quiesce.hh"
52#include "mem/port.hh"
53#include "params/BaseCPU.hh"
54#include "sim/full_system.hh"
55#include "sim/system.hh"
56
57namespace gem5
58{
59
60void
62{
63 const auto &regClasses = one->getIsaPtr()->regClasses();
64
65 DPRINTF(Context, "Comparing thread contexts\n");
66
67 // First loop through the integer registers.
68 for (auto &id: *regClasses.at(IntRegClass)) {
69 RegVal t1 = one->getReg(id);
70 RegVal t2 = two->getReg(id);
71 if (t1 != t2)
72 panic("Int reg idx %d doesn't match, one: %#x, two: %#x",
73 id.index(), t1, t2);
74 }
75
76 // Then loop through the floating point registers.
77 for (auto &id: *regClasses.at(FloatRegClass)) {
78 RegVal t1 = one->getReg(id);
79 RegVal t2 = two->getReg(id);
80 if (t1 != t2)
81 panic("Float reg idx %d doesn't match, one: %#x, two: %#x",
82 id.index(), t1, t2);
83 }
84
85 // Then loop through the vector registers.
86 const auto *vec_class = regClasses.at(VecRegClass);
87 std::vector<uint8_t> vec1(vec_class->regBytes());
88 std::vector<uint8_t> vec2(vec_class->regBytes());
89 for (auto &id: *regClasses.at(VecRegClass)) {
90 one->getReg(id, vec1.data());
91 two->getReg(id, vec2.data());
92 if (vec1 != vec2) {
93 panic("Vec reg idx %d doesn't match, one: %#x, two: %#x",
94 id.index(), vec_class->valString(vec1.data()),
95 vec_class->valString(vec2.data()));
96 }
97 }
98
99 // Then loop through the predicate registers.
100 const auto *vec_pred_class = regClasses.at(VecPredRegClass);
101 std::vector<uint8_t> pred1(vec_pred_class->regBytes());
102 std::vector<uint8_t> pred2(vec_pred_class->regBytes());
103 for (auto &id: *regClasses.at(VecPredRegClass)) {
104 one->getReg(id, pred1.data());
105 two->getReg(id, pred2.data());
106 if (pred1 != pred2) {
107 panic("Pred reg idx %d doesn't match, one: %s, two: %s",
108 id.index(), vec_pred_class->valString(pred1.data()),
109 vec_pred_class->valString(pred2.data()));
110 }
111 }
112
113 // Then loop through the matrix registers.
114 const auto *mat_class = regClasses.at(MatRegClass);
115 std::vector<uint8_t> mat1(mat_class->regBytes());
116 std::vector<uint8_t> mat2(mat_class->regBytes());
117 for (auto &id: *regClasses.at(MatRegClass)) {
118 one->getReg(id, mat1.data());
119 two->getReg(id, mat2.data());
120 if (mat1 != mat2) {
121 panic("Mat reg idx %d doesn't match, one: %#x, two: %#x",
122 id.index(), mat_class->valString(mat1.data()),
123 mat_class->valString(mat2.data()));
124 }
125 }
126
127 for (int i = 0; i < regClasses.at(MiscRegClass)->numRegs(); ++i) {
128 RegVal t1 = one->readMiscRegNoEffect(i);
130 if (t1 != t2)
131 panic("Misc reg idx %d doesn't match, one: %#x, two: %#x",
132 i, t1, t2);
133 }
134
135 // loop through the Condition Code registers.
136 for (auto &id: *regClasses.at(CCRegClass)) {
137 RegVal t1 = one->getReg(id);
138 RegVal t2 = two->getReg(id);
139 if (t1 != t2)
140 panic("CC reg idx %d doesn't match, one: %#x, two: %#x",
141 id.index(), t1, t2);
142 }
143 if (one->pcState() != two->pcState())
144 panic("PC state doesn't match.");
145 int id1 = one->cpuId();
146 int id2 = two->cpuId();
147 if (id1 != id2)
148 panic("CPU ids don't match, one: %d, two: %d", id1, id2);
149
150 const ContextID cid1 = one->contextId();
151 const ContextID cid2 = two->contextId();
152 if (cid1 != cid2)
153 panic("Context ids don't match, one: %d, two: %d", id1, id2);
154
155
156}
157
158void
160{
161 const auto *port =
162 dynamic_cast<const RequestPort *>(&getCpuPtr()->getDataPort());
163 assert(port);
164 port->sendFunctional(pkt);
165}
166
167void
172
173
174void
179
180RegVal
182{
183 RegVal val;
184 getReg(reg, &val);
185 return val;
186}
187
188void
193
194void
196{
197 for (const auto *reg_class: tc.getIsaPtr()->regClasses()) {
198 // MiscRegs are serialized elsewhere.
199 if (reg_class->type() == MiscRegClass)
200 continue;
201
202 const size_t reg_bytes = reg_class->regBytes();
203 const size_t reg_count = reg_class->numRegs();
204 const size_t array_bytes = reg_bytes * reg_count;
205
206 auto regs = std::make_unique<uint8_t[]>(array_bytes);
207 auto *reg_ptr = regs.get();
208 for (const auto &id: *reg_class) {
209 tc.getReg(id, reg_ptr);
210 reg_ptr += reg_bytes;
211 }
212
213 arrayParamOut(cp, std::string("regs.") + reg_class->name(), regs.get(),
214 array_bytes);
215 }
216
217 tc.pcState().serialize(cp);
218
219 // thread_num and cpu_id are deterministic from the config
220}
221
222void
224{
225 for (const auto *reg_class: tc.getIsaPtr()->regClasses()) {
226 // MiscRegs are serialized elsewhere.
227 if (reg_class->type() == MiscRegClass)
228 continue;
229
230 const size_t reg_bytes = reg_class->regBytes();
231 const size_t reg_count = reg_class->numRegs();
232 const size_t array_bytes = reg_bytes * reg_count;
233
234 auto regs = std::make_unique<uint8_t[]>(array_bytes);
235 arrayParamIn(cp, std::string("regs.") + reg_class->name(), regs.get(),
236 array_bytes);
237
238 auto *reg_ptr = regs.get();
239 for (const auto &id: *reg_class) {
240 tc.setReg(id, reg_ptr);
241 reg_ptr += reg_bytes;
242 }
243 }
244
245 std::unique_ptr<PCStateBase> pc_state(tc.pcState().clone());
246 pc_state->unserialize(cp);
247 tc.pcState(*pc_state);
248
249 // thread_num and cpu_id are deterministic from the config
250}
251
252void
254{
255 assert(ntc.getProcessPtr() == otc.getProcessPtr());
256
257 ntc.setStatus(otc.status());
258 ntc.copyArchRegs(&otc);
259 ntc.setContextId(otc.contextId());
260 ntc.setThreadId(otc.threadId());
261
262 if (FullSystem)
263 assert(ntc.getSystemPtr() == otc.getSystemPtr());
264
266}
267
268} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
virtual Port & getDataPort()=0
Purely virtual method that returns a reference to the data port.
const RegClasses & regClasses() const
Definition isa.hh:93
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pcstate.hh:141
virtual PCStateBase * clone() const =0
Register ID: describe an architectural register with its class and index.
Definition reg_class.hh:94
A RequestPort is a specialisation of a Port, which implements the default protocol for the three diff...
Definition port.hh:136
void quiesceTick(ContextID id, Tick when)
Definition system.cc:154
void quiesce(ContextID id)
Definition system.cc:145
Threads threads
Definition system.hh:315
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setStatus(Status new_status)=0
virtual RegVal getReg(const RegId &reg) const
virtual System * getSystemPtr()=0
virtual void copyArchRegs(ThreadContext *tc)=0
static void compare(ThreadContext *one, ThreadContext *two)
function to compare two thread contexts (for debugging)
virtual BaseISA * getIsaPtr() const =0
virtual BaseCPU * getCpuPtr()=0
void quiesceTick(Tick resume)
Quiesce, suspend, and schedule activate at resume.
virtual void setReg(const RegId &reg, RegVal val)
void quiesce()
Quiesce thread context.
@ Halted
Permanently shut down.
virtual const PCStateBase & pcState() const =0
virtual int threadId() const =0
virtual Status status() const =0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
virtual Process * getProcessPtr()=0
virtual ContextID contextId() const =0
virtual void sendFunctional(PacketPtr pkt)
virtual void setThreadId(int id)=0
virtual void setContextId(ContextID id)=0
virtual int cpuId() const =0
STL vector class.
Definition stl.hh:37
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
Port Object Declaration.
Bitfield< 2 > t2
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 1 > t1
Bitfield< 30, 0 > index
Bitfield< 5, 3 > reg
Definition types.hh:92
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
std::ostream CheckpointOut
Definition serialize.hh:66
void unserialize(ThreadContext &tc, CheckpointIn &cp)
void arrayParamOut(CheckpointOut &cp, const std::string &name, const CircleBuf< T > &param)
Definition circlebuf.hh:247
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition root.cc:220
uint64_t Tick
Tick count type.
Definition types.hh:58
Packet * PacketPtr
void takeOverFrom(ThreadContext &ntc, ThreadContext &otc)
Copy state between thread contexts in preparation for CPU handover.
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
int ContextID
Globally unique thread context ID.
Definition types.hh:239
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > &param)
Definition circlebuf.hh:257
@ VecPredRegClass
Definition reg_class.hh:67
@ MatRegClass
Matrix Register.
Definition reg_class.hh:68
@ FloatRegClass
Floating-point register.
Definition reg_class.hh:62
@ CCRegClass
Condition-code register.
Definition reg_class.hh:69
@ VecRegClass
Vector Register.
Definition reg_class.hh:64
@ IntRegClass
Integer register.
Definition reg_class.hh:61
@ MiscRegClass
Control (misc) register.
Definition reg_class.hh:70

Generated on Mon Oct 27 2025 04:13:00 for gem5 by doxygen 1.14.0