inorder: use flattenIdx for reg indexing
[gem5.git] / src / cpu / inorder / thread_context.hh
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Korey Sewell
29 *
30 */
31
32 #ifndef __CPU_INORDER_THREAD_CONTEXT_HH__
33 #define __CPU_INORDER_THREAD_CONTEXT_HH__
34
35 #include "config/the_isa.hh"
36 #include "cpu/inorder/cpu.hh"
37 #include "cpu/inorder/thread_state.hh"
38 #include "cpu/exetrace.hh"
39 #include "cpu/thread_context.hh"
40
41 class TranslatingPort;
42
43 /**
44 * Derived ThreadContext class for use with the InOrderCPU. It
45 * provides the interface for any external objects to access a
46 * single thread's state and some general CPU state. Any time
47 * external objects try to update state through this interface,
48 * the CPU will create an event to squash all in-flight
49 * instructions in order to ensure state is maintained correctly.
50 * It must be defined specifically for the InOrderCPU because
51 * not all architectural state is located within the O3ThreadState
52 * (such as the commit PC, and registers), and specific actions
53 * must be taken when using this interface (such as squashing all
54 * in-flight instructions when doing a write to this interface).
55 */
56 class InOrderThreadContext : public ThreadContext
57 {
58 public:
59 InOrderThreadContext() { }
60
61 /** Pointer to the CPU. */
62 InOrderCPU *cpu;
63
64 /** Pointer to the thread state that this TC corrseponds to. */
65 InOrderThreadState *thread;
66
67 /** Returns a pointer to the ITB. */
68 /** @TODO: PERF: Should we bind this to a pointer in constructor? */
69 TheISA::TLB *getITBPtr() { return cpu->getITBPtr(); }
70
71 /** Returns a pointer to the DTB. */
72 /** @TODO: PERF: Should we bind this to a pointer in constructor? */
73 TheISA::TLB *getDTBPtr() { return cpu->getDTBPtr(); }
74
75 System *getSystemPtr() { return cpu->system; }
76
77 /** Returns a pointer to this CPU. */
78 BaseCPU *getCpuPtr() { return cpu; }
79
80 /** Returns a pointer to this CPU. */
81 std::string getCpuName() { return cpu->name(); }
82
83 /** Reads this CPU's ID. */
84 int cpuId() { return cpu->cpuId(); }
85
86 int contextId() { return thread->contextId(); }
87
88 void setContextId(int id) { thread->setContextId(id); }
89
90 /** Returns this thread's ID number. */
91 int threadId() { return thread->threadId(); }
92 void setThreadId(int id) { return thread->setThreadId(id); }
93
94 uint64_t readMicroPC()
95 { return 0; }
96
97 void setMicroPC(uint64_t val) { };
98
99 uint64_t readNextMicroPC()
100 { return 0; }
101
102 void setNextMicroPC(uint64_t val) { };
103
104 #if FULL_SYSTEM
105 /** Returns a pointer to physical memory. */
106 PhysicalMemory *getPhysMemPtr()
107 { assert(0); return 0; /*return cpu->physmem;*/ }
108
109 /** Returns a pointer to this thread's kernel statistics. */
110 TheISA::Kernel::Statistics *getKernelStats()
111 { return thread->kernelStats; }
112
113 FunctionalPort *getPhysPort() { return thread->getPhysPort(); }
114
115 VirtualPort *getVirtPort();
116
117 void connectMemPorts(ThreadContext *tc)
118 { thread->connectMemPorts(tc); }
119
120 /** Dumps the function profiling information.
121 * @todo: Implement.
122 */
123 void dumpFuncProfile();
124
125 /** Reads the last tick that this thread was activated on. */
126 Tick readLastActivate();
127 /** Reads the last tick that this thread was suspended on. */
128 Tick readLastSuspend();
129
130 /** Clears the function profiling information. */
131 void profileClear();
132
133 /** Samples the function profiling information. */
134 void profileSample();
135
136 /** Returns pointer to the quiesce event. */
137 EndQuiesceEvent *getQuiesceEvent()
138 {
139 return this->thread->quiesceEvent;
140 }
141 #else
142 TranslatingPort *getMemPort() { return thread->getMemPort(); }
143
144 /** Returns a pointer to this thread's process. */
145 Process *getProcessPtr() { return thread->getProcessPtr(); }
146 #endif
147
148 /** Returns this thread's status. */
149 Status status() const { return thread->status(); }
150
151 /** Sets this thread's status. */
152 void setStatus(Status new_status)
153 { thread->setStatus(new_status); }
154
155 /** Set the status to Active. Optional delay indicates number of
156 * cycles to wait before beginning execution. */
157 void activate(int delay = 1);
158
159 /** Set the status to Suspended. */
160 void suspend(int delay = 0);
161
162 /** Set the status to Halted. */
163 void halt(int delay = 0);
164
165 /** Takes over execution of a thread from another CPU. */
166 void takeOverFrom(ThreadContext *old_context);
167
168 /** Registers statistics associated with this TC. */
169 void regStats(const std::string &name);
170
171 /** Serializes state. */
172 void serialize(std::ostream &os);
173
174 /** Unserializes state. */
175 void unserialize(Checkpoint *cp, const std::string &section);
176
177 /** Returns this thread's ID number. */
178 int getThreadNum() { return thread->threadId(); }
179
180 /** Copies the architectural registers from another TC into this TC. */
181 void copyArchRegs(ThreadContext *src_tc);
182
183 /** Resets all architectural registers to 0. */
184 void clearArchRegs();
185
186 /** Reads an integer register. */
187 uint64_t readIntReg(int reg_idx);
188
189 FloatReg readFloatReg(int reg_idx);
190
191 FloatRegBits readFloatRegBits(int reg_idx);
192
193 uint64_t readRegOtherThread(int misc_reg, ThreadID tid);
194
195 /** Sets an integer register to a value. */
196 void setIntReg(int reg_idx, uint64_t val);
197
198 void setFloatReg(int reg_idx, FloatReg val);
199
200 void setFloatRegBits(int reg_idx, FloatRegBits val);
201
202 void setRegOtherThread(int misc_reg,
203 const MiscReg &val,
204 ThreadID tid);
205
206 /** Reads this thread's PC. */
207 TheISA::PCState pcState()
208 { return cpu->pcState(thread->threadId()); }
209
210 /** Sets this thread's PC. */
211 void pcState(const TheISA::PCState &val)
212 { cpu->pcState(val, thread->threadId()); }
213
214 Addr instAddr()
215 { return cpu->instAddr(thread->threadId()); }
216
217 Addr nextInstAddr()
218 { return cpu->nextInstAddr(thread->threadId()); }
219
220 MicroPC microPC()
221 { return cpu->microPC(thread->threadId()); }
222
223 /** Reads a miscellaneous register. */
224 MiscReg readMiscRegNoEffect(int misc_reg)
225 { return cpu->readMiscRegNoEffect(misc_reg, thread->threadId()); }
226
227 /** Reads a misc. register, including any side-effects the
228 * read might have as defined by the architecture. */
229 MiscReg readMiscReg(int misc_reg)
230 { return cpu->readMiscReg(misc_reg, thread->threadId()); }
231
232 /** Sets a misc. register. */
233 void setMiscRegNoEffect(int misc_reg, const MiscReg &val);
234
235 /** Sets a misc. register, including any side-effects the
236 * write might have as defined by the architecture. */
237 void setMiscReg(int misc_reg, const MiscReg &val);
238
239 int flattenIntIndex(int reg)
240 { return cpu->isa[thread->threadId()].flattenIntIndex(reg); }
241
242 int flattenFloatIndex(int reg)
243 { return cpu->isa[thread->threadId()].flattenFloatIndex(reg); }
244
245 void activateContext(int delay)
246 { cpu->activateContext(thread->threadId(), delay); }
247
248 void deallocateContext()
249 { cpu->deallocateContext(thread->threadId()); }
250
251 /** Returns the number of consecutive store conditional failures. */
252 // @todo: Figure out where these store cond failures should go.
253 unsigned readStCondFailures()
254 { return thread->storeCondFailures; }
255
256 /** Sets the number of consecutive store conditional failures. */
257 void setStCondFailures(unsigned sc_failures)
258 { thread->storeCondFailures = sc_failures; }
259
260 // Only really makes sense for old CPU model. Lots of code
261 // outside the CPU still checks this function, so it will
262 // always return false to keep everything working.
263 /** Checks if the thread is misspeculating. Because it is
264 * very difficult to determine if the thread is
265 * misspeculating, this is set as false. */
266 bool misspeculating() { return false; }
267
268 #if !FULL_SYSTEM
269 /** Executes a syscall in SE mode. */
270 void syscall(int64_t callnum)
271 { return cpu->syscall(callnum, thread->threadId()); }
272 #endif
273
274 /** Reads the funcExeInst counter. */
275 Counter readFuncExeInst() { return thread->funcExeInst; }
276
277 void changeRegFileContext(unsigned param,
278 unsigned val)
279 { panic("Not supported!"); }
280 };
281
282 #endif