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 "arch/isa_traits.hh"
55 #include "arch/microcode_rom.hh"
56 #include "base/statistics.hh"
57 #include "mem/port_proxy.hh"
58 #include "sim/clocked_object.hh"
59 #include "sim/eventq.hh"
60 #include "sim/full_system.hh"
61 #include "sim/insttracer.hh"
62 #include "sim/probe/pmu.hh"
63 #include "sim/probe/probe.hh"
64 #include "sim/system.hh"
65 #include "debug/Mwait.hh"
75 bool doMonitor(PacketPtr pkt);
81 bool waiting; // 0=normal, 1=mwaiting
85 class CPUProgressEvent : public Event
94 CPUProgressEvent(BaseCPU *_cpu, Tick ival = 0);
98 void interval(Tick ival) { _interval = ival; }
99 Tick interval() { return _interval; }
101 void repeatEvent(bool repeat) { _repeatEvent = repeat; }
103 virtual const char *description() const;
106 class BaseCPU : public ClockedObject
110 /// Instruction count used for SPARC misc register
111 /// @todo unify this with the counters that cpus individually keep
114 // every cpu has an id, put it in the base cpu
115 // Set at initialization, only time a cpuId might change is during a
116 // takeover (which should be done from within the BaseCPU anyway,
117 // therefore no setCpuId() method is provided
120 /** Each cpu will have a socket ID that corresponds to its physical location
121 * in the system. This is usually used to bucket cpu cores under single DVFS
122 * domain. This information may also be required by the OS to identify the
123 * cpu core grouping (as in the case of ARM via MPIDR register)
125 const uint32_t _socketId;
127 /** instruction side request id that must be placed in all requests */
128 MasterID _instMasterId;
130 /** data side request id that must be placed in all requests */
131 MasterID _dataMasterId;
133 /** An intrenal representation of a task identifier within gem5. This is
134 * used so the CPU can add which taskId (which is an internal representation
135 * of the OS process ID) to each request so components in the memory system
136 * can track which process IDs are ultimately interacting with them
140 /** The current OS process ID that is executing on this processor. This is
141 * used to generate a taskId */
144 /** Is the CPU switched out or active? */
147 /** Cache the cache line size that we get from the system */
148 const unsigned int _cacheLineSize;
153 * Purely virtual method that returns a reference to the data
154 * port. All subclasses must implement this method.
156 * @return a reference to the data port
158 virtual Port &getDataPort() = 0;
161 * Returns a sendFunctional delegate for use with port proxies.
163 virtual PortProxy::SendFunctionalFunc
166 auto port = dynamic_cast<MasterPort *>(&getDataPort());
168 return [port](PacketPtr pkt)->void { port->sendFunctional(pkt); };
172 * Purely virtual method that returns a reference to the instruction
173 * port. All subclasses must implement this method.
175 * @return a reference to the instruction port
177 virtual Port &getInstPort() = 0;
179 /** Reads this CPU's ID. */
180 int cpuId() const { return _cpuId; }
182 /** Reads this CPU's Socket ID. */
183 uint32_t socketId() const { return _socketId; }
185 /** Reads this CPU's unique data requestor ID */
186 MasterID dataMasterId() const { return _dataMasterId; }
187 /** Reads this CPU's unique instruction requestor ID */
188 MasterID instMasterId() const { return _instMasterId; }
191 * Get a port on this CPU. All CPUs have a data and
192 * instruction port, and this method uses getDataPort and
193 * getInstPort of the subclasses to resolve the two ports.
195 * @param if_name the port name
196 * @param idx ignored index
198 * @return a reference to the port with the given name
200 Port &getPort(const std::string &if_name,
201 PortID idx=InvalidPortID) override;
203 /** Get cpu task id */
204 uint32_t taskId() const { return _taskId; }
205 /** Set cpu task id */
206 void taskId(uint32_t id) { _taskId = id; }
208 uint32_t getPid() const { return _pid; }
209 void setPid(uint32_t pid) { _pid = pid; }
211 inline void workItemBegin() { numWorkItemsStarted++; }
212 inline void workItemEnd() { numWorkItemsCompleted++; }
213 // @todo remove me after debugging with legion done
214 Tick instCount() { return instCnt; }
216 TheISA::MicrocodeRom microcodeRom;
219 std::vector<BaseInterrupts*> interrupts;
223 getInterruptController(ThreadID tid)
225 if (interrupts.empty())
228 assert(interrupts.size() > tid);
229 return interrupts[tid];
232 virtual void wakeup(ThreadID tid) = 0;
235 postInterrupt(ThreadID tid, int int_num, int index);
238 clearInterrupt(ThreadID tid, int int_num, int index)
240 interrupts[tid]->clear(int_num, index);
244 clearInterrupts(ThreadID tid)
246 interrupts[tid]->clearAll();
250 checkInterrupts(ThreadID tid) const
252 return FullSystem && interrupts[tid]->checkInterrupts();
256 std::vector<ThreadContext *> threadContexts;
258 Trace::InstTracer * tracer;
263 /** Invalid or unknown Pid. Possible when operating system is not present
264 * or has not assigned a pid yet */
265 static const uint32_t invldPid = std::numeric_limits<uint32_t>::max();
267 // Mask to align PCs to MachInst sized boundaries
268 static const Addr PCMask = ~((Addr)sizeof(TheISA::MachInst) - 1);
270 /// Provide access to the tracer pointer
271 Trace::InstTracer * getTracer() { return tracer; }
273 /// Notify the CPU that the indicated context is now active.
274 virtual void activateContext(ThreadID thread_num);
276 /// Notify the CPU that the indicated context is now suspended.
277 /// Check if possible to enter a lower power state
278 virtual void suspendContext(ThreadID thread_num);
280 /// Notify the CPU that the indicated context is now halted.
281 virtual void haltContext(ThreadID thread_num);
283 /// Given a Thread Context pointer return the thread num
284 int findContext(ThreadContext *tc);
286 /// Given a thread num get tho thread context for it
287 virtual ThreadContext *getContext(int tn) { return threadContexts[tn]; }
289 /// Get the number of thread contexts available
290 unsigned numContexts() {
291 return static_cast<unsigned>(threadContexts.size());
294 /// Convert ContextID to threadID
295 ThreadID contextToThread(ContextID cid)
296 { return static_cast<ThreadID>(cid - threadContexts[0]->contextId()); }
299 typedef BaseCPUParams Params;
300 const Params *params() const
301 { return reinterpret_cast<const Params *>(_params); }
302 BaseCPU(Params *params, bool is_checker = false);
305 void init() override;
306 void startup() override;
307 void regStats() override;
309 void regProbePoints() override;
311 void registerThreadContexts();
313 // Functions to deschedule and reschedule the events to enter the
314 // power gating sleep before and after checkpoiting respectively.
315 void deschedulePowerGatingEvent();
316 void schedulePowerGatingEvent();
319 * Prepare for another CPU to take over execution.
321 * When this method exits, all internal state should have been
322 * flushed. After the method returns, the simulator calls
323 * takeOverFrom() on the new CPU with this CPU as its parameter.
325 virtual void switchOut();
328 * Load the state of a CPU from the previous CPU object, invoked
329 * on all new CPUs that are about to be switched in.
331 * A CPU model implementing this method is expected to initialize
332 * its state from the old CPU and connect its memory (unless they
333 * are already connected) to the memories connected to the old
336 * @param cpu CPU to initialize read state from.
338 virtual void takeOverFrom(BaseCPU *cpu);
341 * Flush all TLBs in the CPU.
343 * This method is mainly used to flush stale translations when
344 * switching CPUs. It is also exported to the Python world to
345 * allow it to request a TLB flush after draining the CPU to make
346 * it easier to compare traces when debugging
347 * handover/checkpointing.
352 * Determine if the CPU is switched out.
354 * @return True if the CPU is switched out, false otherwise.
356 bool switchedOut() const { return _switchedOut; }
359 * Verify that the system is in a memory mode supported by the
362 * Implementations are expected to query the system for the
363 * current memory mode and ensure that it is what the CPU model
364 * expects. If the check fails, the implementation should
365 * terminate the simulation using fatal().
367 virtual void verifyMemoryMode() const { };
370 * Number of threads we're actually simulating (<= SMT_MAX_THREADS).
371 * This is a constant for the duration of the simulation.
378 * Get the cache line size of the system.
380 inline unsigned int cacheLineSize() const { return _cacheLineSize; }
383 * Serialize this object to the given output stream.
385 * @note CPU models should normally overload the serializeThread()
386 * method instead of the serialize() method as this provides a
387 * uniform data format for all CPU models and promotes better code
390 * @param cp The stream to serialize to.
392 void serialize(CheckpointOut &cp) const override;
395 * Reconstruct the state of this object from a checkpoint.
397 * @note CPU models should normally overload the
398 * unserializeThread() method instead of the unserialize() method
399 * as this provides a uniform data format for all CPU models and
400 * promotes better code reuse.
402 * @param cp The checkpoint use.
404 void unserialize(CheckpointIn &cp) override;
407 * Serialize a single thread.
409 * @param cp The stream to serialize to.
410 * @param tid ID of the current thread.
412 virtual void serializeThread(CheckpointOut &cp, ThreadID tid) const {};
415 * Unserialize one thread.
417 * @param cp The checkpoint use.
418 * @param tid ID of the current thread.
420 virtual void unserializeThread(CheckpointIn &cp, ThreadID tid) {};
422 virtual Counter totalInsts() const = 0;
424 virtual Counter totalOps() const = 0;
427 * Schedule an event that exits the simulation loops after a
428 * predefined number of instructions.
430 * This method is usually called from the configuration script to
431 * get an exit event some time in the future. It is typically used
432 * when the script wants to simulate for a specific number of
433 * instructions rather than ticks.
435 * @param tid Thread monitor.
436 * @param insts Number of instructions into the future.
437 * @param cause Cause to signal in the exit event.
439 void scheduleInstStop(ThreadID tid, Counter insts, const char *cause);
442 * Get the number of instructions executed by the specified thread
443 * on this CPU. Used by Python to control simulation.
445 * @param tid Thread monitor
446 * @return Number of instructions executed
448 uint64_t getCurrentInstCount(ThreadID tid);
453 * @name PMU Probe points.
457 * Helper method to trigger PMU probes for a committed
460 * @param inst Instruction that just committed
461 * @param pc PC of the instruction that just committed
463 virtual void probeInstCommit(const StaticInstPtr &inst, Addr pc);
467 * Helper method to instantiate probe points belonging to this
470 * @param name Name of the probe point.
471 * @return A unique_ptr to the new probe point.
473 ProbePoints::PMUUPtr pmuProbePoint(const char *name);
476 * Instruction commit probe point.
478 * This probe point is triggered whenever one or more instructions
479 * are committed. It is normally triggered once for every
480 * instruction. However, CPU models committing bundles of
481 * instructions may call notify once for the entire bundle.
483 ProbePoints::PMUUPtr ppRetiredInsts;
484 ProbePoints::PMUUPtr ppRetiredInstsPC;
486 /** Retired load instructions */
487 ProbePoints::PMUUPtr ppRetiredLoads;
488 /** Retired store instructions */
489 ProbePoints::PMUUPtr ppRetiredStores;
491 /** Retired branches (any type) */
492 ProbePoints::PMUUPtr ppRetiredBranches;
494 /** CPU cycle counter even if any thread Context is suspended*/
495 ProbePoints::PMUUPtr ppAllCycles;
497 /** CPU cycle counter, only counts if any thread contexts is active **/
498 ProbePoints::PMUUPtr ppActiveCycles;
501 * ProbePoint that signals transitions of threadContexts sets.
502 * The ProbePoint reports information through it bool parameter.
503 * - If the parameter is true then the last enabled threadContext of the
504 * CPU object was disabled.
505 * - If the parameter is false then a threadContext was enabled, all the
506 * remaining threadContexts are disabled.
508 ProbePointArg<bool> *ppSleeping;
517 Cycles previousCycle;
518 CPUState previousState;
520 /** base method keeping track of cycle progression **/
521 inline void updateCycleCounters(CPUState state)
523 uint32_t delta = curCycle() - previousCycle;
525 if (previousState == CPU_STATE_ON) {
526 ppActiveCycles->notify(delta);
531 case CPU_STATE_WAKEUP:
532 ppSleeping->notify(false);
534 case CPU_STATE_SLEEP:
535 ppSleeping->notify(true);
541 ppAllCycles->notify(delta);
543 previousCycle = curCycle();
544 previousState = state;
549 bool functionTracingEnabled;
550 std::ostream *functionTraceStream;
551 Addr currentFunctionStart;
552 Addr currentFunctionEnd;
553 Tick functionEntryTick;
554 void enableFunctionTrace();
555 void traceFunctionsInternal(Addr pc);
558 static std::vector<BaseCPU *> cpuList; //!< Static global cpu list
561 void traceFunctions(Addr pc)
563 if (functionTracingEnabled)
564 traceFunctionsInternal(pc);
567 static int numSimulatedCPUs() { return cpuList.size(); }
568 static Counter numSimulatedInsts()
572 int size = cpuList.size();
573 for (int i = 0; i < size; ++i)
574 total += cpuList[i]->totalInsts();
579 static Counter numSimulatedOps()
583 int size = cpuList.size();
584 for (int i = 0; i < size; ++i)
585 total += cpuList[i]->totalOps();
591 // Number of CPU cycles simulated
592 Stats::Scalar numCycles;
593 Stats::Scalar numWorkItemsStarted;
594 Stats::Scalar numWorkItemsCompleted;
597 std::vector<AddressMonitor> addressMonitor;
600 void armMonitor(ThreadID tid, Addr address);
601 bool mwait(ThreadID tid, PacketPtr pkt);
602 void mwaitAtomic(ThreadID tid, ThreadContext *tc, BaseTLB *dtb);
603 AddressMonitor *getCpuAddrMonitor(ThreadID tid)
605 assert(tid < numThreads);
606 return &addressMonitor[tid];
609 bool waitForRemoteGDB() const;
611 Cycles syscallRetryLatency;
613 // Enables CPU to enter power gating on a configurable cycle count
615 void enterPwrGating();
617 const Cycles pwrGatingLatency;
618 const bool powerGatingOnIdle;
619 EventFunctionWrapper enterPwrGatingEvent;
622 #endif // THE_ISA == NULL_ISA
624 #endif // __CPU_BASE_HH__