2 * Copyright (c) 2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef __CPU_THREAD_CONTEXT_HH__
32 #define __CPU_THREAD_CONTEXT_HH__
34 #include "arch/regfile.hh"
35 #include "arch/types.hh"
36 #include "config/full_system.hh"
37 #include "mem/request.hh"
38 #include "sim/faults.hh"
39 #include "sim/host.hh"
40 #include "sim/serialize.hh"
41 #include "sim/syscallreturn.hh"
42 #include "sim/byteswap.hh"
44 // @todo: Figure out a more architecture independent way to obtain the ITB and
52 class EndQuiesceEvent;
54 class TranslatingPort;
66 * ThreadContext is the external interface to all thread state for
67 * anything outside of the CPU. It provides all accessor methods to
68 * state that might be needed by external objects, ranging from
69 * register values to things such as kernel stats. It is an abstract
70 * base class; the CPU can create its own ThreadContext by either
71 * deriving from it, or using the templated ProxyThreadContext.
73 * The ThreadContext is slightly different than the ExecContext. The
74 * ThreadContext provides access to an individual thread's state; an
75 * ExecContext provides ISA access to the CPU (meaning it is
76 * implicitly multithreaded on SMT systems). Additionally the
77 * ThreadState is an abstract class that exactly defines the
78 * interface; the ExecContext is a more implicit interface that must
79 * be implemented so that the ISA can access whatever state it needs.
84 typedef TheISA::RegFile RegFile;
85 typedef TheISA::MachInst MachInst;
86 typedef TheISA::IntReg IntReg;
87 typedef TheISA::FloatReg FloatReg;
88 typedef TheISA::FloatRegBits FloatRegBits;
89 typedef TheISA::MiscRegFile MiscRegFile;
90 typedef TheISA::MiscReg MiscReg;
94 /// Initialized but not running yet. All CPUs start in
95 /// this state, but most transition to Active on cycle 1.
96 /// In MP or SMT systems, non-primary contexts will stay
97 /// in this state until a thread is assigned to them.
100 /// Running. Instructions should be executed only when
101 /// the context is in this state.
104 /// Temporarily inactive. Entered while waiting for
105 /// synchronization, etc.
108 /// Permanently shut down. Entered when target executes
109 /// m5exit pseudo-instruction. When all contexts enter
110 /// this state, the simulation will terminate.
114 virtual ~ThreadContext() { };
116 virtual BaseCPU *getCpuPtr() = 0;
118 virtual void setCpuId(int id) = 0;
120 virtual int readCpuId() = 0;
122 virtual TheISA::ITB *getITBPtr() = 0;
124 virtual TheISA::DTB *getDTBPtr() = 0;
127 virtual System *getSystemPtr() = 0;
129 virtual TheISA::Kernel::Statistics *getKernelStats() = 0;
131 virtual FunctionalPort *getPhysPort() = 0;
133 virtual VirtualPort *getVirtPort() = 0;
135 virtual void connectMemPorts(ThreadContext *tc) = 0;
137 virtual TranslatingPort *getMemPort() = 0;
139 virtual Process *getProcessPtr() = 0;
142 virtual Status status() const = 0;
144 virtual void setStatus(Status new_status) = 0;
146 /// Set the status to Active. Optional delay indicates number of
147 /// cycles to wait before beginning execution.
148 virtual void activate(int delay = 1) = 0;
150 /// Set the status to Suspended.
151 virtual void suspend(int delay = 0) = 0;
153 /// Set the status to Unallocated.
154 virtual void deallocate(int delay = 0) = 0;
156 /// Set the status to Halted.
157 virtual void halt(int delay = 0) = 0;
160 virtual void dumpFuncProfile() = 0;
163 virtual void takeOverFrom(ThreadContext *old_context) = 0;
165 virtual void regStats(const std::string &name) = 0;
167 virtual void serialize(std::ostream &os) = 0;
168 virtual void unserialize(Checkpoint *cp, const std::string §ion) = 0;
171 virtual EndQuiesceEvent *getQuiesceEvent() = 0;
173 // Not necessarily the best location for these...
174 // Having an extra function just to read these is obnoxious
175 virtual Tick readLastActivate() = 0;
176 virtual Tick readLastSuspend() = 0;
178 virtual void profileClear() = 0;
179 virtual void profileSample() = 0;
182 virtual int getThreadNum() = 0;
184 // Also somewhat obnoxious. Really only used for the TLB fault.
185 // However, may be quite useful in SPARC.
186 virtual TheISA::MachInst getInst() = 0;
188 virtual void copyArchRegs(ThreadContext *tc) = 0;
190 virtual void clearArchRegs() = 0;
193 // New accessors for new decoder.
195 virtual uint64_t readIntReg(int reg_idx) = 0;
197 virtual FloatReg readFloatReg(int reg_idx, int width) = 0;
199 virtual FloatReg readFloatReg(int reg_idx) = 0;
201 virtual FloatRegBits readFloatRegBits(int reg_idx, int width) = 0;
203 virtual FloatRegBits readFloatRegBits(int reg_idx) = 0;
205 virtual void setIntReg(int reg_idx, uint64_t val) = 0;
207 virtual void setFloatReg(int reg_idx, FloatReg val, int width) = 0;
209 virtual void setFloatReg(int reg_idx, FloatReg val) = 0;
211 virtual void setFloatRegBits(int reg_idx, FloatRegBits val) = 0;
213 virtual void setFloatRegBits(int reg_idx, FloatRegBits val, int width) = 0;
215 virtual uint64_t readPC() = 0;
217 virtual void setPC(uint64_t val) = 0;
219 virtual uint64_t readNextPC() = 0;
221 virtual void setNextPC(uint64_t val) = 0;
223 virtual uint64_t readNextNPC() = 0;
225 virtual void setNextNPC(uint64_t val) = 0;
227 virtual uint64_t readMicroPC() = 0;
229 virtual void setMicroPC(uint64_t val) = 0;
231 virtual uint64_t readNextMicroPC() = 0;
233 virtual void setNextMicroPC(uint64_t val) = 0;
235 virtual MiscReg readMiscRegNoEffect(int misc_reg) = 0;
237 virtual MiscReg readMiscReg(int misc_reg) = 0;
239 virtual void setMiscRegNoEffect(int misc_reg, const MiscReg &val) = 0;
241 virtual void setMiscReg(int misc_reg, const MiscReg &val) = 0;
243 virtual uint64_t readRegOtherThread(int misc_reg, unsigned tid) { return 0; }
245 virtual void setRegOtherThread(int misc_reg, const MiscReg &val, unsigned tid) { };
247 // Also not necessarily the best location for these two. Hopefully will go
248 // away once we decide upon where st cond failures goes.
249 virtual unsigned readStCondFailures() = 0;
251 virtual void setStCondFailures(unsigned sc_failures) = 0;
253 // Only really makes sense for old CPU model. Still could be useful though.
254 virtual bool misspeculating() = 0;
257 virtual IntReg getSyscallArg(int i) = 0;
259 // used to shift args for indirect syscall
260 virtual void setSyscallArg(int i, IntReg val) = 0;
262 virtual void setSyscallReturn(SyscallReturn return_value) = 0;
264 // Same with st cond failures.
265 virtual Counter readFuncExeInst() = 0;
267 virtual void syscall(int64_t callnum) = 0;
269 // This function exits the thread context in the CPU and returns
270 // 1 if the CPU has no more active threads (meaning it's OK to exit);
271 // Used in syscall-emulation mode when a thread calls the exit syscall.
272 virtual int exit() { return 1; };
275 virtual void changeRegFileContext(TheISA::RegContextParam param,
276 TheISA::RegContextVal val) = 0;
278 /** function to compare two thread contexts (for debugging) */
279 static void compare(ThreadContext *one, ThreadContext *two);
283 * ProxyThreadContext class that provides a way to implement a
284 * ThreadContext without having to derive from it. ThreadContext is an
285 * abstract class, so anything that derives from it and uses its
286 * interface will pay the overhead of virtual function calls. This
287 * class is created to enable a user-defined Thread object to be used
288 * wherever ThreadContexts are used, without paying the overhead of
289 * virtual function calls when it is used by itself. See
290 * simple_thread.hh for an example of this.
293 class ProxyThreadContext : public ThreadContext
296 ProxyThreadContext(TC *actual_tc)
297 { actualTC = actual_tc; }
304 BaseCPU *getCpuPtr() { return actualTC->getCpuPtr(); }
306 void setCpuId(int id) { actualTC->setCpuId(id); }
308 int readCpuId() { return actualTC->readCpuId(); }
310 TheISA::ITB *getITBPtr() { return actualTC->getITBPtr(); }
312 TheISA::DTB *getDTBPtr() { return actualTC->getDTBPtr(); }
315 System *getSystemPtr() { return actualTC->getSystemPtr(); }
317 TheISA::Kernel::Statistics *getKernelStats()
318 { return actualTC->getKernelStats(); }
320 FunctionalPort *getPhysPort() { return actualTC->getPhysPort(); }
322 VirtualPort *getVirtPort() { return actualTC->getVirtPort(); }
324 void connectMemPorts(ThreadContext *tc) { actualTC->connectMemPorts(tc); }
326 TranslatingPort *getMemPort() { return actualTC->getMemPort(); }
328 Process *getProcessPtr() { return actualTC->getProcessPtr(); }
331 Status status() const { return actualTC->status(); }
333 void setStatus(Status new_status) { actualTC->setStatus(new_status); }
335 /// Set the status to Active. Optional delay indicates number of
336 /// cycles to wait before beginning execution.
337 void activate(int delay = 1) { actualTC->activate(delay); }
339 /// Set the status to Suspended.
340 void suspend(int delay = 0) { actualTC->suspend(); }
342 /// Set the status to Unallocated.
343 void deallocate(int delay = 0) { actualTC->deallocate(); }
345 /// Set the status to Halted.
346 void halt(int delay = 0) { actualTC->halt(); }
349 void dumpFuncProfile() { actualTC->dumpFuncProfile(); }
352 void takeOverFrom(ThreadContext *oldContext)
353 { actualTC->takeOverFrom(oldContext); }
355 void regStats(const std::string &name) { actualTC->regStats(name); }
357 void serialize(std::ostream &os) { actualTC->serialize(os); }
358 void unserialize(Checkpoint *cp, const std::string §ion)
359 { actualTC->unserialize(cp, section); }
362 EndQuiesceEvent *getQuiesceEvent() { return actualTC->getQuiesceEvent(); }
364 Tick readLastActivate() { return actualTC->readLastActivate(); }
365 Tick readLastSuspend() { return actualTC->readLastSuspend(); }
367 void profileClear() { return actualTC->profileClear(); }
368 void profileSample() { return actualTC->profileSample(); }
371 int getThreadNum() { return actualTC->getThreadNum(); }
373 // @todo: Do I need this?
374 MachInst getInst() { return actualTC->getInst(); }
376 // @todo: Do I need this?
377 void copyArchRegs(ThreadContext *tc) { actualTC->copyArchRegs(tc); }
379 void clearArchRegs() { actualTC->clearArchRegs(); }
382 // New accessors for new decoder.
384 uint64_t readIntReg(int reg_idx)
385 { return actualTC->readIntReg(reg_idx); }
387 FloatReg readFloatReg(int reg_idx, int width)
388 { return actualTC->readFloatReg(reg_idx, width); }
390 FloatReg readFloatReg(int reg_idx)
391 { return actualTC->readFloatReg(reg_idx); }
393 FloatRegBits readFloatRegBits(int reg_idx, int width)
394 { return actualTC->readFloatRegBits(reg_idx, width); }
396 FloatRegBits readFloatRegBits(int reg_idx)
397 { return actualTC->readFloatRegBits(reg_idx); }
399 void setIntReg(int reg_idx, uint64_t val)
400 { actualTC->setIntReg(reg_idx, val); }
402 void setFloatReg(int reg_idx, FloatReg val, int width)
403 { actualTC->setFloatReg(reg_idx, val, width); }
405 void setFloatReg(int reg_idx, FloatReg val)
406 { actualTC->setFloatReg(reg_idx, val); }
408 void setFloatRegBits(int reg_idx, FloatRegBits val, int width)
409 { actualTC->setFloatRegBits(reg_idx, val, width); }
411 void setFloatRegBits(int reg_idx, FloatRegBits val)
412 { actualTC->setFloatRegBits(reg_idx, val); }
414 uint64_t readPC() { return actualTC->readPC(); }
416 void setPC(uint64_t val) { actualTC->setPC(val); }
418 uint64_t readNextPC() { return actualTC->readNextPC(); }
420 void setNextPC(uint64_t val) { actualTC->setNextPC(val); }
422 uint64_t readNextNPC() { return actualTC->readNextNPC(); }
424 void setNextNPC(uint64_t val) { actualTC->setNextNPC(val); }
426 uint64_t readMicroPC() { return actualTC->readMicroPC(); }
428 void setMicroPC(uint64_t val) { actualTC->setMicroPC(val); }
430 uint64_t readNextMicroPC() { return actualTC->readMicroPC(); }
432 void setNextMicroPC(uint64_t val) { actualTC->setNextMicroPC(val); }
434 MiscReg readMiscRegNoEffect(int misc_reg)
435 { return actualTC->readMiscRegNoEffect(misc_reg); }
437 MiscReg readMiscReg(int misc_reg)
438 { return actualTC->readMiscReg(misc_reg); }
440 void setMiscRegNoEffect(int misc_reg, const MiscReg &val)
441 { return actualTC->setMiscRegNoEffect(misc_reg, val); }
443 void setMiscReg(int misc_reg, const MiscReg &val)
444 { return actualTC->setMiscReg(misc_reg, val); }
446 unsigned readStCondFailures()
447 { return actualTC->readStCondFailures(); }
449 void setStCondFailures(unsigned sc_failures)
450 { actualTC->setStCondFailures(sc_failures); }
453 bool misspeculating() { return actualTC->misspeculating(); }
456 IntReg getSyscallArg(int i) { return actualTC->getSyscallArg(i); }
458 // used to shift args for indirect syscall
459 void setSyscallArg(int i, IntReg val)
460 { actualTC->setSyscallArg(i, val); }
462 void setSyscallReturn(SyscallReturn return_value)
463 { actualTC->setSyscallReturn(return_value); }
465 void syscall(int64_t callnum)
466 { actualTC->syscall(callnum); }
468 Counter readFuncExeInst() { return actualTC->readFuncExeInst(); }
471 void changeRegFileContext(TheISA::RegContextParam param,
472 TheISA::RegContextVal val)
474 actualTC->changeRegFileContext(param, val);