2 * Copyright (c) 2011-2013, 2017, 2020 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2011 Regents of the University of California
16 * All rights reserved.
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.
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.
42 #ifndef __CPU_BASE_HH__
43 #define __CPU_BASE_HH__
47 // Before we do anything else, check if this build is the NULL ISA,
48 // and if so stop here
49 #include "config/the_isa.hh"
50 #if THE_ISA == NULL_ISA
51 #include "arch/null/cpu_dummy.hh"
53 #include "arch/generic/interrupts.hh"
54 #include "base/statistics.hh"
55 #include "mem/port_proxy.hh"
56 #include "sim/clocked_object.hh"
57 #include "sim/eventq.hh"
58 #include "sim/full_system.hh"
59 #include "sim/insttracer.hh"
60 #include "sim/probe/pmu.hh"
61 #include "sim/probe/probe.hh"
62 #include "sim/system.hh"
63 #include "debug/Mwait.hh"
73 bool doMonitor(PacketPtr pkt);
79 bool waiting; // 0=normal, 1=mwaiting
83 class CPUProgressEvent : public Event
92 CPUProgressEvent(BaseCPU *_cpu, Tick ival = 0);
96 void interval(Tick ival) { _interval = ival; }
97 Tick interval() { return _interval; }
99 void repeatEvent(bool repeat) { _repeatEvent = repeat; }
101 virtual const char *description() const;
104 class BaseCPU : public ClockedObject
108 /// Instruction count used for SPARC misc register
109 /// @todo unify this with the counters that cpus individually keep
112 // every cpu has an id, put it in the base cpu
113 // Set at initialization, only time a cpuId might change is during a
114 // takeover (which should be done from within the BaseCPU anyway,
115 // therefore no setCpuId() method is provided
118 /** Each cpu will have a socket ID that corresponds to its physical location
119 * in the system. This is usually used to bucket cpu cores under single DVFS
120 * domain. This information may also be required by the OS to identify the
121 * cpu core grouping (as in the case of ARM via MPIDR register)
123 const uint32_t _socketId;
125 /** instruction side request id that must be placed in all requests */
126 MasterID _instMasterId;
128 /** data side request id that must be placed in all requests */
129 MasterID _dataMasterId;
131 /** An intrenal representation of a task identifier within gem5. This is
132 * used so the CPU can add which taskId (which is an internal representation
133 * of the OS process ID) to each request so components in the memory system
134 * can track which process IDs are ultimately interacting with them
138 /** The current OS process ID that is executing on this processor. This is
139 * used to generate a taskId */
142 /** Is the CPU switched out or active? */
145 /** Cache the cache line size that we get from the system */
146 const unsigned int _cacheLineSize;
151 * Purely virtual method that returns a reference to the data
152 * port. All subclasses must implement this method.
154 * @return a reference to the data port
156 virtual Port &getDataPort() = 0;
159 * Returns a sendFunctional delegate for use with port proxies.
161 virtual PortProxy::SendFunctionalFunc
164 auto port = dynamic_cast<RequestPort *>(&getDataPort());
166 return [port](PacketPtr pkt)->void { port->sendFunctional(pkt); };
170 * Purely virtual method that returns a reference to the instruction
171 * port. All subclasses must implement this method.
173 * @return a reference to the instruction port
175 virtual Port &getInstPort() = 0;
177 /** Reads this CPU's ID. */
178 int cpuId() const { return _cpuId; }
180 /** Reads this CPU's Socket ID. */
181 uint32_t socketId() const { return _socketId; }
183 /** Reads this CPU's unique data requestor ID */
184 MasterID dataMasterId() const { return _dataMasterId; }
185 /** Reads this CPU's unique instruction requestor ID */
186 MasterID instMasterId() const { return _instMasterId; }
189 * Get a port on this CPU. All CPUs have a data and
190 * instruction port, and this method uses getDataPort and
191 * getInstPort of the subclasses to resolve the two ports.
193 * @param if_name the port name
194 * @param idx ignored index
196 * @return a reference to the port with the given name
198 Port &getPort(const std::string &if_name,
199 PortID idx=InvalidPortID) override;
201 /** Get cpu task id */
202 uint32_t taskId() const { return _taskId; }
203 /** Set cpu task id */
204 void taskId(uint32_t id) { _taskId = id; }
206 uint32_t getPid() const { return _pid; }
207 void setPid(uint32_t pid) { _pid = pid; }
209 inline void workItemBegin() { numWorkItemsStarted++; }
210 inline void workItemEnd() { numWorkItemsCompleted++; }
211 // @todo remove me after debugging with legion done
212 Tick instCount() { return instCnt; }
215 std::vector<BaseInterrupts*> interrupts;
219 getInterruptController(ThreadID tid)
221 if (interrupts.empty())
224 assert(interrupts.size() > tid);
225 return interrupts[tid];
228 virtual void wakeup(ThreadID tid) = 0;
231 postInterrupt(ThreadID tid, int int_num, int index);
234 clearInterrupt(ThreadID tid, int int_num, int index)
236 interrupts[tid]->clear(int_num, index);
240 clearInterrupts(ThreadID tid)
242 interrupts[tid]->clearAll();
246 checkInterrupts(ThreadID tid) const
248 return FullSystem && interrupts[tid]->checkInterrupts();
252 std::vector<ThreadContext *> threadContexts;
254 Trace::InstTracer * tracer;
259 /** Invalid or unknown Pid. Possible when operating system is not present
260 * or has not assigned a pid yet */
261 static const uint32_t invldPid = std::numeric_limits<uint32_t>::max();
263 // Mask to align PCs to MachInst sized boundaries
264 static const Addr PCMask = ~((Addr)sizeof(TheISA::MachInst) - 1);
266 /// Provide access to the tracer pointer
267 Trace::InstTracer * getTracer() { return tracer; }
269 /// Notify the CPU that the indicated context is now active.
270 virtual void activateContext(ThreadID thread_num);
272 /// Notify the CPU that the indicated context is now suspended.
273 /// Check if possible to enter a lower power state
274 virtual void suspendContext(ThreadID thread_num);
276 /// Notify the CPU that the indicated context is now halted.
277 virtual void haltContext(ThreadID thread_num);
279 /// Given a Thread Context pointer return the thread num
280 int findContext(ThreadContext *tc);
282 /// Given a thread num get tho thread context for it
283 virtual ThreadContext *getContext(int tn) { return threadContexts[tn]; }
285 /// Get the number of thread contexts available
286 unsigned numContexts() {
287 return static_cast<unsigned>(threadContexts.size());
290 /// Convert ContextID to threadID
291 ThreadID contextToThread(ContextID cid)
292 { return static_cast<ThreadID>(cid - threadContexts[0]->contextId()); }
295 typedef BaseCPUParams Params;
296 const Params *params() const
297 { return reinterpret_cast<const Params *>(_params); }
298 BaseCPU(Params *params, bool is_checker = false);
301 void init() override;
302 void startup() override;
303 void regStats() override;
305 void regProbePoints() override;
307 void registerThreadContexts();
309 // Functions to deschedule and reschedule the events to enter the
310 // power gating sleep before and after checkpoiting respectively.
311 void deschedulePowerGatingEvent();
312 void schedulePowerGatingEvent();
315 * Prepare for another CPU to take over execution.
317 * When this method exits, all internal state should have been
318 * flushed. After the method returns, the simulator calls
319 * takeOverFrom() on the new CPU with this CPU as its parameter.
321 virtual void switchOut();
324 * Load the state of a CPU from the previous CPU object, invoked
325 * on all new CPUs that are about to be switched in.
327 * A CPU model implementing this method is expected to initialize
328 * its state from the old CPU and connect its memory (unless they
329 * are already connected) to the memories connected to the old
332 * @param cpu CPU to initialize read state from.
334 virtual void takeOverFrom(BaseCPU *cpu);
337 * Flush all TLBs in the CPU.
339 * This method is mainly used to flush stale translations when
340 * switching CPUs. It is also exported to the Python world to
341 * allow it to request a TLB flush after draining the CPU to make
342 * it easier to compare traces when debugging
343 * handover/checkpointing.
348 * Determine if the CPU is switched out.
350 * @return True if the CPU is switched out, false otherwise.
352 bool switchedOut() const { return _switchedOut; }
355 * Verify that the system is in a memory mode supported by the
358 * Implementations are expected to query the system for the
359 * current memory mode and ensure that it is what the CPU model
360 * expects. If the check fails, the implementation should
361 * terminate the simulation using fatal().
363 virtual void verifyMemoryMode() const { };
366 * Number of threads we're actually simulating (<= SMT_MAX_THREADS).
367 * This is a constant for the duration of the simulation.
374 * Get the cache line size of the system.
376 inline unsigned int cacheLineSize() const { return _cacheLineSize; }
379 * Serialize this object to the given output stream.
381 * @note CPU models should normally overload the serializeThread()
382 * method instead of the serialize() method as this provides a
383 * uniform data format for all CPU models and promotes better code
386 * @param cp The stream to serialize to.
388 void serialize(CheckpointOut &cp) const override;
391 * Reconstruct the state of this object from a checkpoint.
393 * @note CPU models should normally overload the
394 * unserializeThread() method instead of the unserialize() method
395 * as this provides a uniform data format for all CPU models and
396 * promotes better code reuse.
398 * @param cp The checkpoint use.
400 void unserialize(CheckpointIn &cp) override;
403 * Serialize a single thread.
405 * @param cp The stream to serialize to.
406 * @param tid ID of the current thread.
408 virtual void serializeThread(CheckpointOut &cp, ThreadID tid) const {};
411 * Unserialize one thread.
413 * @param cp The checkpoint use.
414 * @param tid ID of the current thread.
416 virtual void unserializeThread(CheckpointIn &cp, ThreadID tid) {};
418 virtual Counter totalInsts() const = 0;
420 virtual Counter totalOps() const = 0;
423 * Schedule an event that exits the simulation loops after a
424 * predefined number of instructions.
426 * This method is usually called from the configuration script to
427 * get an exit event some time in the future. It is typically used
428 * when the script wants to simulate for a specific number of
429 * instructions rather than ticks.
431 * @param tid Thread monitor.
432 * @param insts Number of instructions into the future.
433 * @param cause Cause to signal in the exit event.
435 void scheduleInstStop(ThreadID tid, Counter insts, const char *cause);
438 * Get the number of instructions executed by the specified thread
439 * on this CPU. Used by Python to control simulation.
441 * @param tid Thread monitor
442 * @return Number of instructions executed
444 uint64_t getCurrentInstCount(ThreadID tid);
449 * @name PMU Probe points.
453 * Helper method to trigger PMU probes for a committed
456 * @param inst Instruction that just committed
457 * @param pc PC of the instruction that just committed
459 virtual void probeInstCommit(const StaticInstPtr &inst, Addr pc);
463 * Helper method to instantiate probe points belonging to this
466 * @param name Name of the probe point.
467 * @return A unique_ptr to the new probe point.
469 ProbePoints::PMUUPtr pmuProbePoint(const char *name);
472 * Instruction commit probe point.
474 * This probe point is triggered whenever one or more instructions
475 * are committed. It is normally triggered once for every
476 * instruction. However, CPU models committing bundles of
477 * instructions may call notify once for the entire bundle.
479 ProbePoints::PMUUPtr ppRetiredInsts;
480 ProbePoints::PMUUPtr ppRetiredInstsPC;
482 /** Retired load instructions */
483 ProbePoints::PMUUPtr ppRetiredLoads;
484 /** Retired store instructions */
485 ProbePoints::PMUUPtr ppRetiredStores;
487 /** Retired branches (any type) */
488 ProbePoints::PMUUPtr ppRetiredBranches;
490 /** CPU cycle counter even if any thread Context is suspended*/
491 ProbePoints::PMUUPtr ppAllCycles;
493 /** CPU cycle counter, only counts if any thread contexts is active **/
494 ProbePoints::PMUUPtr ppActiveCycles;
497 * ProbePoint that signals transitions of threadContexts sets.
498 * The ProbePoint reports information through it bool parameter.
499 * - If the parameter is true then the last enabled threadContext of the
500 * CPU object was disabled.
501 * - If the parameter is false then a threadContext was enabled, all the
502 * remaining threadContexts are disabled.
504 ProbePointArg<bool> *ppSleeping;
513 Cycles previousCycle;
514 CPUState previousState;
516 /** base method keeping track of cycle progression **/
517 inline void updateCycleCounters(CPUState state)
519 uint32_t delta = curCycle() - previousCycle;
521 if (previousState == CPU_STATE_ON) {
522 ppActiveCycles->notify(delta);
527 case CPU_STATE_WAKEUP:
528 ppSleeping->notify(false);
530 case CPU_STATE_SLEEP:
531 ppSleeping->notify(true);
537 ppAllCycles->notify(delta);
539 previousCycle = curCycle();
540 previousState = state;
545 bool functionTracingEnabled;
546 std::ostream *functionTraceStream;
547 Addr currentFunctionStart;
548 Addr currentFunctionEnd;
549 Tick functionEntryTick;
550 void enableFunctionTrace();
551 void traceFunctionsInternal(Addr pc);
554 static std::vector<BaseCPU *> cpuList; //!< Static global cpu list
557 void traceFunctions(Addr pc)
559 if (functionTracingEnabled)
560 traceFunctionsInternal(pc);
563 static int numSimulatedCPUs() { return cpuList.size(); }
564 static Counter numSimulatedInsts()
568 int size = cpuList.size();
569 for (int i = 0; i < size; ++i)
570 total += cpuList[i]->totalInsts();
575 static Counter numSimulatedOps()
579 int size = cpuList.size();
580 for (int i = 0; i < size; ++i)
581 total += cpuList[i]->totalOps();
587 // Number of CPU cycles simulated
588 Stats::Scalar numCycles;
589 Stats::Scalar numWorkItemsStarted;
590 Stats::Scalar numWorkItemsCompleted;
593 std::vector<AddressMonitor> addressMonitor;
596 void armMonitor(ThreadID tid, Addr address);
597 bool mwait(ThreadID tid, PacketPtr pkt);
598 void mwaitAtomic(ThreadID tid, ThreadContext *tc, BaseTLB *dtb);
599 AddressMonitor *getCpuAddrMonitor(ThreadID tid)
601 assert(tid < numThreads);
602 return &addressMonitor[tid];
605 bool waitForRemoteGDB() const;
607 Cycles syscallRetryLatency;
609 // Enables CPU to enter power gating on a configurable cycle count
611 void enterPwrGating();
613 const Cycles pwrGatingLatency;
614 const bool powerGatingOnIdle;
615 EventFunctionWrapper enterPwrGatingEvent;
618 #endif // THE_ISA == NULL_ISA
620 #endif // __CPU_BASE_HH__