2 * Copyright (c) 2002-2005 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.
28 * Authors: Steve Reinhardt
32 #ifndef __CPU_BASE_HH__
33 #define __CPU_BASE_HH__
37 #include "arch/isa_traits.hh"
38 #include "base/statistics.hh"
39 #include "config/full_system.hh"
40 #include "sim/eventq.hh"
41 #include "sim/insttracer.hh"
42 #include "mem/mem_object.hh"
45 #include "arch/interrupts.hh"
59 class CPUProgressEvent : public Event
67 CPUProgressEvent(EventQueue *q, Tick ival, BaseCPU *_cpu);
71 virtual const char *description();
74 class BaseCPU : public MemObject
77 // CPU's clock period in terms of the number of ticks of curTime.
79 // @todo remove me after debugging with legion done
84 inline Tick frequency() const { return Clock::Frequency / clock; }
85 inline Tick ticks(int numCycles) const { return clock * numCycles; }
86 inline Tick curCycle() const { return curTick / clock; }
87 inline Tick tickToCycles(Tick val) const { return val / clock; }
88 // @todo remove me after debugging with legion done
89 Tick instCount() { return instCnt; }
91 /** The next cycle the CPU should be scheduled, given a cache
92 * access or quiesce event returning on this cycle. This function
93 * may return curTick if the CPU should run on the current cycle.
97 /** The next cycle the CPU should be scheduled, given a cache
98 * access or quiesce event returning on the given Tick. This
99 * function may return curTick if the CPU should run on the
101 * @param begin_tick The tick that the event is completing on.
103 Tick nextCycle(Tick begin_tick);
107 // uint64_t interrupts[TheISA::NumInterruptLevels];
108 // uint64_t intstatus;
109 TheISA::Interrupts interrupts;
112 virtual void post_interrupt(int int_num, int index);
113 virtual void clear_interrupt(int int_num, int index);
114 virtual void clear_interrupts();
115 virtual uint64_t get_interrupts(int int_num);
117 bool check_interrupts(ThreadContext * tc) const
118 { return interrupts.check_interrupts(tc); }
120 class ProfileEvent : public Event
127 ProfileEvent(BaseCPU *cpu, int interval);
130 ProfileEvent *profileEvent;
134 std::vector<ThreadContext *> threadContexts;
135 std::vector<TheISA::Predecoder *> predecoders;
137 Trace::InstTracer * tracer;
141 /// Provide access to the tracer pointer
142 Trace::InstTracer * getTracer() { return tracer; }
144 /// Notify the CPU that the indicated context is now active. The
145 /// delay parameter indicates the number of ticks to wait before
146 /// executing (typically 0 or 1).
147 virtual void activateContext(int thread_num, int delay) {}
149 /// Notify the CPU that the indicated context is now suspended.
150 virtual void suspendContext(int thread_num) {}
152 /// Notify the CPU that the indicated context is now deallocated.
153 virtual void deallocateContext(int thread_num) {}
155 /// Notify the CPU that the indicated context is now halted.
156 virtual void haltContext(int thread_num) {}
158 /// Given a Thread Context pointer return the thread num
159 int findContext(ThreadContext *tc);
161 /// Given a thread num get tho thread context for it
162 ThreadContext *getContext(int tn) { return threadContexts[tn]; }
169 bool deferRegistration;
170 Counter max_insts_any_thread;
171 Counter max_insts_all_threads;
172 Counter max_loads_any_thread;
173 Counter max_loads_all_threads;
176 Tick functionTraceStart;
179 Trace::InstTracer * tracer;
185 bool do_statistics_insts;
186 bool do_checkpoint_insts;
189 Tick progress_interval;
195 const Params *params;
197 BaseCPU(Params *params);
201 virtual void startup();
202 virtual void regStats();
204 virtual void activateWhenReady(int tid) {};
206 void registerThreadContexts();
208 /// Prepare for another CPU to take over execution. When it is
209 /// is ready (drained pipe) it signals the sampler.
210 virtual void switchOut();
212 /// Take over execution from the given CPU. Used for warm-up and
214 virtual void takeOverFrom(BaseCPU *, Port *ic, Port *dc);
217 * Number of threads we're actually simulating (<= SMT_MAX_THREADS).
218 * This is a constant for the duration of the simulation.
220 int number_of_threads;
223 * Vector of per-thread instruction-based event queues. Used for
224 * scheduling events based on number of instructions committed by
225 * a particular thread.
227 EventQueue **comInstEventQueue;
230 * Vector of per-thread load-based event queues. Used for
231 * scheduling events based on number of loads committed by
232 *a particular thread.
234 EventQueue **comLoadEventQueue;
242 * Serialize this object to the given output stream.
243 * @param os The stream to serialize to.
245 virtual void serialize(std::ostream &os);
248 * Reconstruct the state of this object from a checkpoint.
249 * @param cp The checkpoint use.
250 * @param section The section name of this object
252 virtual void unserialize(Checkpoint *cp, const std::string §ion);
257 * Return pointer to CPU's branch predictor (NULL if none).
258 * @return Branch predictor pointer.
260 virtual BranchPred *getBranchPred() { return NULL; };
262 virtual Counter totalInstructions() const { return 0; }
266 bool functionTracingEnabled;
267 std::ostream *functionTraceStream;
268 Addr currentFunctionStart;
269 Addr currentFunctionEnd;
270 Tick functionEntryTick;
271 void enableFunctionTrace();
272 void traceFunctionsInternal(Addr pc);
275 void traceFunctions(Addr pc)
277 if (functionTracingEnabled)
278 traceFunctionsInternal(pc);
282 static std::vector<BaseCPU *> cpuList; //!< Static global cpu list
285 static int numSimulatedCPUs() { return cpuList.size(); }
286 static Counter numSimulatedInstructions()
290 int size = cpuList.size();
291 for (int i = 0; i < size; ++i)
292 total += cpuList[i]->totalInstructions();
298 // Number of CPU cycles simulated
299 Stats::Scalar<> numCycles;
302 #endif // __CPU_BASE_HH__